From 0680ba0ba3698e107f7aa014e373852f12eb05f9 Mon Sep 17 00:00:00 2001 From: moana Date: Fri, 13 Dec 2024 16:09:15 +0100 Subject: [PATCH] transfer-contract: Use rusk-abi::execute for tests --- contracts/transfer/tests/common/utils.rs | 40 +---------- contracts/transfer/tests/moonlight.rs | 90 ++++++++++++------------ contracts/transfer/tests/phoenix.rs | 40 ++++++----- 3 files changed, 70 insertions(+), 100 deletions(-) diff --git a/contracts/transfer/tests/common/utils.rs b/contracts/transfer/tests/common/utils.rs index 44847090c..c72950dfb 100644 --- a/contracts/transfer/tests/common/utils.rs +++ b/contracts/transfer/tests/common/utils.rs @@ -11,9 +11,9 @@ use execution_core::transfer::moonlight::AccountData; use execution_core::transfer::phoenix::{ Note, NoteLeaf, ViewKey as PhoenixViewKey, }; -use execution_core::transfer::{Transaction, TRANSFER_CONTRACT}; -use execution_core::{BlsScalar, ContractError, ContractId}; -use rusk_abi::{CallReceipt, PiecrustError, Session}; +use execution_core::transfer::TRANSFER_CONTRACT; +use execution_core::{BlsScalar, ContractId}; +use rusk_abi::{PiecrustError, Session}; const GAS_LIMIT: u64 = 0x10_000_000; @@ -32,40 +32,6 @@ pub fn chain_id(session: &mut Session) -> Result { .map(|r| r.data) } -/// Executes a transaction. -/// Returns result containing gas spent. -pub fn execute( - session: &mut Session, - tx: impl Into, -) -> Result, ContractError>>, PiecrustError> { - let tx = tx.into(); - - let mut receipt = session.call::<_, Result, ContractError>>( - TRANSFER_CONTRACT, - "spend_and_execute", - &tx, - tx.gas_limit(), - )?; - - // Ensure all gas is consumed if there's an error in the contract call - if receipt.data.is_err() { - receipt.gas_spent = receipt.gas_limit; - } - - let refund_receipt = session - .call::<_, ()>( - TRANSFER_CONTRACT, - "refund", - &receipt.gas_spent, - u64::MAX, - ) - .expect("Refunding must succeed"); - - receipt.events.extend(refund_receipt.events); - - Ok(receipt) -} - // moonlight helper functions pub fn account( diff --git a/contracts/transfer/tests/moonlight.rs b/contracts/transfer/tests/moonlight.rs index fe9827e79..34adc5558 100644 --- a/contracts/transfer/tests/moonlight.rs +++ b/contracts/transfer/tests/moonlight.rs @@ -7,7 +7,7 @@ pub mod common; use crate::common::utils::{ - account, chain_id, contract_balance, execute, existing_nullifiers, + account, chain_id, contract_balance, existing_nullifiers, filter_notes_owned_by, leaves_from_height, owned_notes_value, update_root, }; @@ -15,24 +15,23 @@ use ff::Field; use rand::rngs::StdRng; use rand::SeedableRng; -use execution_core::{ - dusk, - signatures::bls::{ - PublicKey as AccountPublicKey, SecretKey as AccountSecretKey, - }, - transfer::{ - data::{ContractCall, TransactionData}, - moonlight::Transaction as MoonlightTransaction, - phoenix::{ - Note, PublicKey as PhoenixPublicKey, SecretKey as PhoenixSecretKey, - ViewKey as PhoenixViewKey, - }, - withdraw::{Withdraw, WithdrawReceiver, WithdrawReplayToken}, - ContractToAccount, ContractToContract, TRANSFER_CONTRACT, - }, - BlsScalar, ContractError, ContractId, JubJubScalar, LUX, +use execution_core::signatures::bls::{ + PublicKey as AccountPublicKey, SecretKey as AccountSecretKey, }; -use rusk_abi::{ContractData, Session}; +use execution_core::transfer::data::{ContractCall, TransactionData}; +use execution_core::transfer::moonlight::Transaction as MoonlightTransaction; +use execution_core::transfer::phoenix::{ + Note, PublicKey as PhoenixPublicKey, SecretKey as PhoenixSecretKey, + ViewKey as PhoenixViewKey, +}; +use execution_core::transfer::withdraw::{ + Withdraw, WithdrawReceiver, WithdrawReplayToken, +}; +use execution_core::transfer::{ + ContractToAccount, ContractToContract, Transaction, TRANSFER_CONTRACT, +}; +use execution_core::{dusk, ContractError, ContractId, JubJubScalar, LUX}; +use rusk_abi::{execute, ContractData, Session}; const MOONLIGHT_GENESIS_VALUE: u64 = dusk(1_000.0); const MOONLIGHT_GENESIS_NONCE: u64 = 0; @@ -172,7 +171,7 @@ fn transfer() { let session = &mut instantiate(&moonlight_sender_pk); - let transaction = MoonlightTransaction::new( + let transaction = Transaction::moonlight( &moonlight_sender_sk, Some(moonlight_receiver_pk), TRANSFER_VALUE, @@ -185,7 +184,7 @@ fn transfer() { ) .expect("Creating moonlight transaction should succeed"); - let gas_spent = execute(session, transaction) + let gas_spent = execute(session, &transaction, 0, 0) .expect("Transaction should succeed") .gas_spent; @@ -234,7 +233,7 @@ fn transfer_with_refund() { "The receiver account should be empty" ); - let transaction = MoonlightTransaction::new_with_refund( + let transaction: Transaction = MoonlightTransaction::new_with_refund( &moonlight_sender_sk, &moonlight_refund_pk, Some(moonlight_receiver_pk), @@ -246,10 +245,11 @@ fn transfer_with_refund() { CHAIN_ID, None::, ) - .expect("Creating moonlight transaction should succeed"); + .expect("Creating moonlight transaction should succeed") + .into(); let max_gas = GAS_LIMIT * LUX; - let gas_spent = execute(session, transaction) + let gas_spent = execute(session, &transaction, 0, 0) .expect("Transaction should succeed") .gas_spent; let gas_refund = max_gas - gas_spent; @@ -301,7 +301,7 @@ fn transfer_gas_fails() { "The receiver account should be empty" ); - let transaction = MoonlightTransaction::new( + let transaction = Transaction::moonlight( &moonlight_sender_sk, Some(moonlight_receiver_pk), TRANSFER_VALUE, @@ -314,7 +314,7 @@ fn transfer_gas_fails() { ) .expect("Creating moonlight transaction should succeed"); - let result = execute(session, transaction); + let result = execute(session, &transaction, 0, 0); assert!( result.is_err(), @@ -353,7 +353,7 @@ fn alice_ping() { fn_args: vec![], }); - let transaction = MoonlightTransaction::new( + let transaction = Transaction::moonlight( &moonlight_sk, None, 0, @@ -366,7 +366,7 @@ fn alice_ping() { ) .expect("Creating moonlight transaction should succeed"); - let gas_spent = execute(session, transaction) + let gas_spent = execute(session, &transaction, 0, 0) .expect("Transaction should succeed") .gas_spent; @@ -433,7 +433,7 @@ fn convert_to_phoenix() { .to_vec(), }; - let tx = MoonlightTransaction::new( + let tx = Transaction::moonlight( &moonlight_sk, None, 0, @@ -447,7 +447,7 @@ fn convert_to_phoenix() { ) .expect("Creating moonlight transaction should succeed"); - let gas_spent = execute(&mut session, tx) + let gas_spent = execute(&mut session, &tx, 0, 0) .expect("Executing transaction should succeed") .gas_spent; update_root(session).expect("Updating the root should succeed"); @@ -553,7 +553,7 @@ fn convert_to_moonlight_fails() { .to_vec(), }; - let tx = MoonlightTransaction::new( + let tx = Transaction::moonlight( &moonlight_sk, None, 0, @@ -568,7 +568,7 @@ fn convert_to_moonlight_fails() { .expect("Creating moonlight transaction should succeed"); let receipt = - execute(&mut session, tx).expect("Executing TX should succeed"); + execute(&mut session, &tx, 0, 0).expect("Executing TX should succeed"); // check that the transaction execution panicked with the correct message assert!(receipt.data.is_err()); @@ -660,7 +660,7 @@ fn convert_wrong_contract_targeted() { .to_vec(), }; - let tx = MoonlightTransaction::new( + let tx = Transaction::moonlight( &moonlight_sk, None, 0, @@ -673,7 +673,7 @@ fn convert_wrong_contract_targeted() { ) .expect("Creating moonlight transaction should succeed"); - let receipt = execute(&mut session, tx) + let receipt = execute(&mut session, &tx, 0, 0) .expect("Executing transaction should succeed"); update_root(session).expect("Updating the root should succeed"); @@ -732,7 +732,7 @@ fn contract_to_contract() { .to_vec(), }); - let transaction = MoonlightTransaction::new( + let transaction = Transaction::moonlight( &moonlight_sk, None, 0, @@ -745,8 +745,8 @@ fn contract_to_contract() { ) .expect("Creating moonlight transaction should succeed"); - let receipt = - execute(session, transaction).expect("Transaction should succeed"); + let receipt = execute(session, &transaction, 0, 0) + .expect("Transaction should succeed"); let gas_spent = receipt.gas_spent; println!("SEND TO CONTRACT: {:?}", receipt.data); @@ -799,7 +799,7 @@ fn contract_to_account() { .to_vec(), }); - let transaction = MoonlightTransaction::new( + let transaction = Transaction::moonlight( &moonlight_sk, None, 0, @@ -812,8 +812,8 @@ fn contract_to_account() { ) .expect("Creating moonlight transaction should succeed"); - let receipt = - execute(session, transaction).expect("Transaction should succeed"); + let receipt = execute(session, &transaction, 0, 0) + .expect("Transaction should succeed"); let gas_spent = receipt.gas_spent; println!("SEND TO ACCOUNT: {:?}", receipt.data); @@ -863,7 +863,7 @@ fn contract_to_account_insufficient_funds() { .to_vec(), }); - let transaction = MoonlightTransaction::new( + let transaction = Transaction::moonlight( &moonlight_sk, None, 0, @@ -876,8 +876,8 @@ fn contract_to_account_insufficient_funds() { ) .expect("Creating moonlight transaction should succeed"); - let receipt = - execute(session, transaction).expect("Transaction should succeed"); + let receipt = execute(session, &transaction, 0, 0) + .expect("Transaction should succeed"); let gas_spent = receipt.gas_spent; println!("SEND TO ACCOUNT (insufficient funds): {:?}", receipt.data); @@ -934,7 +934,7 @@ fn contract_to_account_direct_call() { .to_vec(), }); - let transaction = MoonlightTransaction::new( + let transaction = Transaction::moonlight( &moonlight_sk, None, 0, @@ -947,8 +947,8 @@ fn contract_to_account_direct_call() { ) .expect("Creating moonlight transaction should succeed"); - let receipt = - execute(session, transaction).expect("Transaction should succeed"); + let receipt = execute(session, &transaction, 0, 0) + .expect("Transaction should succeed"); let gas_spent = receipt.gas_spent; println!( diff --git a/contracts/transfer/tests/phoenix.rs b/contracts/transfer/tests/phoenix.rs index 38e85e526..cdef63ada 100644 --- a/contracts/transfer/tests/phoenix.rs +++ b/contracts/transfer/tests/phoenix.rs @@ -9,7 +9,7 @@ use std::sync::mpsc; pub mod common; use crate::common::utils::{ - account, chain_id, contract_balance, execute, existing_nullifiers, + account, chain_id, contract_balance, existing_nullifiers, filter_notes_owned_by, leaves_from_height, new_owned_notes_value, owned_notes_value, update_root, }; @@ -32,11 +32,11 @@ use execution_core::{ Transaction as PhoenixTransaction, ViewKey as PhoenixViewKey, }, withdraw::{Withdraw, WithdrawReceiver, WithdrawReplayToken}, - ContractToAccount, ContractToContract, TRANSFER_CONTRACT, + ContractToAccount, ContractToContract, Transaction, TRANSFER_CONTRACT, }, BlsScalar, ContractId, JubJubScalar, LUX, }; -use rusk_abi::{ContractData, PiecrustError, Session}; +use rusk_abi::{execute, ContractData, PiecrustError, Session}; use rusk_prover::LocalProver; const PHOENIX_GENESIS_VALUE: u64 = dusk(1_200.0); @@ -252,7 +252,7 @@ fn transfer_1_2() { contract_call, ); - let gas_spent = execute(session, tx) + let gas_spent = execute(session, &tx, 0, 0) .expect("Executing TX should succeed") .gas_spent; update_root(session).expect("Updating the root should succeed"); @@ -359,7 +359,7 @@ fn transfer_2_2() { contract_call, ); - let gas_spent = execute(session, tx) + let gas_spent = execute(session, &tx, 0, 0) .expect("Executing TX should succeed") .gas_spent; update_root(session).expect("Updating the root should succeed"); @@ -467,7 +467,7 @@ fn transfer_3_2() { contract_call, ); - let gas_spent = execute(session, tx) + let gas_spent = execute(session, &tx, 0, 0) .expect("Executing TX should succeed") .gas_spent; update_root(session).expect("Updating the root should succeed"); @@ -575,7 +575,7 @@ fn transfer_4_2() { contract_call, ); - let gas_spent = execute(session, tx) + let gas_spent = execute(session, &tx, 0, 0) .expect("Executing TX should succeed") .gas_spent; update_root(session).expect("Updating the root should succeed"); @@ -679,7 +679,7 @@ fn transfer_gas_fails() { let total_num_notes_before_tx = num_notes(session).expect("Getting num_notes should succeed"); - let result = execute(session, tx); + let result = execute(session, &tx, 0, 0); assert!( result.is_err(), @@ -750,7 +750,7 @@ fn alice_ping() { contract_call, ); - let gas_spent = execute(session, tx) + let gas_spent = execute(session, &tx, 0, 0) .expect("Executing TX should succeed") .gas_spent; update_root(session).expect("Updating the root should succeed"); @@ -820,7 +820,7 @@ fn contract_deposit() { contract_call, ); - let gas_spent = execute(session, tx.clone()) + let gas_spent = execute(session, &tx, 0, 0) .expect("Executing TX should succeed") .gas_spent; update_root(session).expect("Updating the root should succeed"); @@ -833,7 +833,7 @@ fn contract_deposit() { PHOENIX_GENESIS_VALUE, transfer_value + tx.deposit() - + tx.max_fee() + + tx.gas_limit() * tx.gas_price() + tx.outputs()[1] .value(Some(&PhoenixViewKey::from(&phoenix_sender_sk))) .unwrap() @@ -928,7 +928,7 @@ fn contract_withdraw() { contract_call, ); - let gas_spent = execute(session, tx) + let gas_spent = execute(session, &tx, 0, 0) .expect("Executing TX should succeed") .gas_spent; update_root(session).expect("Updating the root should succeed"); @@ -1054,7 +1054,8 @@ fn convert_to_phoenix_fails() { Some(contract_call), ); - let receipt = execute(session, tx).expect("Executing TX should succeed"); + let receipt = + execute(session, &tx, 0, 0).expect("Executing TX should succeed"); // check that the transaction execution panicked with the correct message assert!(receipt.data.is_err()); @@ -1174,7 +1175,7 @@ fn convert_to_moonlight() { Some(contract_call), ); - let gas_spent = execute(session, tx) + let gas_spent = execute(session, &tx, 0, 0) .expect("Executing TX should succeed") .gas_spent; update_root(session).expect("Updating the root should succeed"); @@ -1284,7 +1285,7 @@ fn convert_wrong_contract_targeted() { Some(contract_call), ); - let receipt = execute(&mut session, tx) + let receipt = execute(&mut session, &tx, 0, 0) .expect("Executing transaction should succeed"); update_root(session).expect("Updating the root should succeed"); @@ -1375,7 +1376,8 @@ fn contract_to_contract() { Some(contract_call), ); - let receipt = execute(session, tx).expect("Transaction should succeed"); + let receipt = + execute(session, &tx, 0, 0).expect("Transaction should succeed"); let gas_spent = receipt.gas_spent; println!("CONTRACT TO CONTRACT: {gas_spent} gas"); @@ -1468,7 +1470,8 @@ fn contract_to_account() { Some(contract_call), ); - let receipt = execute(session, tx).expect("Transaction should succeed"); + let receipt = + execute(session, &tx, 0, 0).expect("Transaction should succeed"); let gas_spent = receipt.gas_spent; println!("CONTRACT TO ACCOUNT: {gas_spent} gas"); @@ -1583,7 +1586,7 @@ fn create_phoenix_transaction( obfuscated_transaction: bool, deposit: u64, data: Option>, -) -> PhoenixTransaction { +) -> Transaction { // Get the root of the tree of phoenix-notes. let root = root(session).expect("Getting the anchor should be successful"); @@ -1627,4 +1630,5 @@ fn create_phoenix_transaction( &LocalProver, ) .expect("creating the creation shouldn't fail") + .into() }