diff --git a/contracts/stake/tests/common/utils.rs b/contracts/stake/tests/common/utils.rs index 584a1bf37..993bc2d64 100644 --- a/contracts/stake/tests/common/utils.rs +++ b/contracts/stake/tests/common/utils.rs @@ -13,10 +13,9 @@ use execution_core::transfer::phoenix::{ ViewKey as PhoenixViewKey, }; use execution_core::transfer::{Transaction, TRANSFER_CONTRACT}; -use execution_core::LUX; -use execution_core::{BlsScalar, ContractError}; +use execution_core::{BlsScalar, LUX}; use rand::rngs::StdRng; -use rusk_abi::{CallReceipt, PiecrustError, Session}; +use rusk_abi::{PiecrustError, Session}; use rusk_prover::LocalProver; pub const GAS_LIMIT: u64 = 0x100_000_000; @@ -98,44 +97,6 @@ pub fn filter_notes_owned_by>( .collect() } -/// Executes a transaction, returning the call receipt -pub fn execute( - session: &mut Session, - tx: impl Into, -) -> Result, ContractError>>, PiecrustError> { - let tx = tx.into(); - - // Spend the inputs and execute the call. If this errors the transaction is - // unspendable. - 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; - } - - // Refund the appropriate amount to the transaction. This call is guaranteed - // to never error. If it does, then a programming error has occurred. As - // such, the call to `Result::expect` is warranted. - 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) -} - /// Generate a TxCircuit given the sender secret-key, receiver public-key, the /// input note positions in the transaction tree and the new output-notes. pub fn create_transaction( diff --git a/contracts/stake/tests/partial_stake.rs b/contracts/stake/tests/partial_stake.rs index 537d4b03d..1e5e4b0fc 100644 --- a/contracts/stake/tests/partial_stake.rs +++ b/contracts/stake/tests/partial_stake.rs @@ -12,7 +12,7 @@ use execution_core::stake::{Reward, RewardReason, EPOCH, STAKE_CONTRACT}; use execution_core::transfer::TRANSFER_CONTRACT; use rand::rngs::StdRng; use rand::SeedableRng; -use rusk_abi::{ContractData, PiecrustError, Session, VM}; +use rusk_abi::{execute, ContractData, PiecrustError, Session, VM}; use wallet_core::transaction::{ moonlight_stake, moonlight_stake_reward, moonlight_unstake, }; @@ -59,7 +59,7 @@ fn stake() -> Result<(), PiecrustError> { CHAIN_ID, ) .expect("tx creation should pass"); - let receipt = execute(&mut session, tx)?; + let receipt = execute(&mut session, &tx, 0, 0)?; // verify 1st stake transaction let gas_spent_1 = receipt.gas_spent; @@ -86,7 +86,7 @@ fn stake() -> Result<(), PiecrustError> { CHAIN_ID, ) .expect("tx creation should pass"); - let receipt = execute(&mut session, tx)?; + let receipt = execute(&mut session, &tx, 0, 0)?; // verify 2nd stake transaction let gas_spent_2 = receipt.gas_spent; @@ -119,7 +119,7 @@ fn stake() -> Result<(), PiecrustError> { CHAIN_ID, ) .expect("tx creation should pass"); - let receipt = execute(&mut session, tx)?; + let receipt = execute(&mut session, &tx, 0, 0)?; // verify 3rd stake transaction let gas_spent_3 = receipt.gas_spent; @@ -153,7 +153,7 @@ fn stake() -> Result<(), PiecrustError> { CHAIN_ID, ) .expect("tx creation should pass"); - assert!(execute(&mut session, tx,).is_err()); + assert!(execute(&mut session, &tx, 0, 0,).is_err()); Ok(()) } @@ -186,7 +186,7 @@ fn unstake() -> Result<(), PiecrustError> { CHAIN_ID, ) .expect("tx creation should pass"); - let receipt = execute(&mut session, tx)?; + let receipt = execute(&mut session, &tx, 0, 0)?; let mut moonlight_balance = GENESIS_VALUE - STAKE_VALUE - receipt.gas_spent; assert_moonlight(&mut session, &moonlight_pk, moonlight_balance, nonce); @@ -207,7 +207,7 @@ fn unstake() -> Result<(), PiecrustError> { CHAIN_ID, ) .expect("tx creation should pass"); - let receipt = execute(&mut session, tx)?; + let receipt = execute(&mut session, &tx, 0, 0)?; // verify 1st unstake transaction let gas_spent_1 = receipt.gas_spent; @@ -236,7 +236,7 @@ fn unstake() -> Result<(), PiecrustError> { CHAIN_ID, ) .expect("tx creation should pass"); - let receipt = execute(&mut session, tx)?; + let receipt = execute(&mut session, &tx, 0, 0)?; total_stake = STAKE_VALUE; let mut locked = unstake_1 / 10; assert_stake(&mut session, &stake_pk, total_stake, locked, 0); @@ -261,7 +261,7 @@ fn unstake() -> Result<(), PiecrustError> { CHAIN_ID, ) .expect("tx creation should pass"); - let receipt = execute(&mut session, tx)?; + let receipt = execute(&mut session, &tx, 0, 0)?; // verify 2nd unstake transaction let gas_spent_2 = receipt.gas_spent; @@ -298,7 +298,7 @@ fn unstake() -> Result<(), PiecrustError> { CHAIN_ID, ) .expect("tx creation should pass"); - let receipt = execute(&mut session, tx)?; + let receipt = execute(&mut session, &tx, 0, 0)?; // verify 3rd unstake transaction let gas_spent_3 = receipt.gas_spent; @@ -340,7 +340,7 @@ fn withdraw_reward() -> Result<(), PiecrustError> { CHAIN_ID, ) .expect("tx creation should pass"); - let receipt = execute(&mut session, tx)?; + let receipt = execute(&mut session, &tx, 0, 0)?; let mut moonlight_balance = GENESIS_VALUE - STAKE_VALUE - receipt.gas_spent; assert_moonlight(&mut session, &moonlight_pk, moonlight_balance, nonce); // add a reward to the staked key @@ -364,7 +364,7 @@ fn withdraw_reward() -> Result<(), PiecrustError> { CHAIN_ID, ) .expect("tx creation should pass"); - let receipt = execute(&mut session, tx)?; + let receipt = execute(&mut session, &tx, 0, 0)?; // verify 1st reward withdrawal let gas_spent_1 = receipt.gas_spent; @@ -398,7 +398,7 @@ fn withdraw_reward() -> Result<(), PiecrustError> { CHAIN_ID, ) .expect("tx creation should pass"); - let receipt = execute(&mut session, tx)?; + let receipt = execute(&mut session, &tx, 0, 0)?; // verify 1st reward withdrawal let gas_spent_2 = receipt.gas_spent; diff --git a/contracts/stake/tests/stake.rs b/contracts/stake/tests/stake.rs index 7719bd700..3c2841278 100644 --- a/contracts/stake/tests/stake.rs +++ b/contracts/stake/tests/stake.rs @@ -6,26 +6,25 @@ pub mod common; +use execution_core::signatures::bls::{ + PublicKey as BlsPublicKey, SecretKey as BlsSecretKey, +}; +use execution_core::stake::{ + Reward, RewardReason, Stake, Withdraw as StakeWithdraw, STAKE_CONTRACT, +}; +use execution_core::transfer::data::ContractCall; +use execution_core::transfer::phoenix::{ + PublicKey as PhoenixPublicKey, SecretKey as PhoenixSecretKey, + ViewKey as PhoenixViewKey, +}; +use execution_core::transfer::withdraw::{ + Withdraw, WithdrawReceiver, WithdrawReplayToken, +}; +use execution_core::{dusk, JubJubScalar}; use ff::Field; use rand::rngs::StdRng; use rand::SeedableRng; - -use execution_core::{ - dusk, - signatures::bls::{PublicKey as BlsPublicKey, SecretKey as BlsSecretKey}, - stake::{ - Reward, RewardReason, Stake, Withdraw as StakeWithdraw, STAKE_CONTRACT, - }, - transfer::{ - data::ContractCall, - phoenix::{ - PublicKey as PhoenixPublicKey, SecretKey as PhoenixSecretKey, - ViewKey as PhoenixViewKey, - }, - withdraw::{Withdraw, WithdrawReceiver, WithdrawReplayToken}, - }, - JubJubScalar, -}; +use rusk_abi::execute; use crate::common::assert::{ assert_reward_event, assert_stake, assert_stake_event, @@ -89,7 +88,7 @@ fn stake_withdraw_unstake() { ); let receipt = - execute(&mut session, tx).expect("Executing TX should succeed"); + execute(&mut session, &tx, 0, 0).expect("Executing TX should succeed"); let gas_spent = receipt.gas_spent; receipt.data.expect("Executed TX should not error"); @@ -184,7 +183,7 @@ fn stake_withdraw_unstake() { .expect("Instantiating new session should succeed"); let receipt = - execute(&mut session, tx).expect("Executing TX should succeed"); + execute(&mut session, &tx, 0, 0).expect("Executing TX should succeed"); let gas_spent = receipt.gas_spent; receipt.data.expect("Executed TX should not error"); @@ -280,7 +279,7 @@ fn stake_withdraw_unstake() { .expect("Instantiating new session should succeed"); let receipt = - execute(&mut session, tx).expect("Executing TX should succeed"); + execute(&mut session, &tx, 0, 0).expect("Executing TX should succeed"); update_root(&mut session).expect("Updating the root should succeed"); let gas_spent = receipt.gas_spent;