Skip to content

Commit

Permalink
transfer-contract: Use rusk-abi::execute for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
moCello committed Dec 16, 2024
1 parent 9e2e0ec commit 0680ba0
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 100 deletions.
40 changes: 3 additions & 37 deletions contracts/transfer/tests/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,40 +32,6 @@ pub fn chain_id(session: &mut Session) -> Result<u8, PiecrustError> {
.map(|r| r.data)
}

/// Executes a transaction.
/// Returns result containing gas spent.
pub fn execute(
session: &mut Session,
tx: impl Into<Transaction>,
) -> Result<CallReceipt<Result<Vec<u8>, ContractError>>, PiecrustError> {
let tx = tx.into();

let mut receipt = session.call::<_, Result<Vec<u8>, 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(
Expand Down
90 changes: 45 additions & 45 deletions contracts/transfer/tests/moonlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,31 @@
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,
};

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;
Expand Down Expand Up @@ -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,
Expand All @@ -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;

Expand Down Expand Up @@ -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),
Expand All @@ -246,10 +245,11 @@ fn transfer_with_refund() {
CHAIN_ID,
None::<TransactionData>,
)
.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;
Expand Down Expand Up @@ -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,
Expand All @@ -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(),
Expand Down Expand Up @@ -353,7 +353,7 @@ fn alice_ping() {
fn_args: vec![],
});

let transaction = MoonlightTransaction::new(
let transaction = Transaction::moonlight(
&moonlight_sk,
None,
0,
Expand All @@ -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;

Expand Down Expand Up @@ -433,7 +433,7 @@ fn convert_to_phoenix() {
.to_vec(),
};

let tx = MoonlightTransaction::new(
let tx = Transaction::moonlight(
&moonlight_sk,
None,
0,
Expand All @@ -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");
Expand Down Expand Up @@ -553,7 +553,7 @@ fn convert_to_moonlight_fails() {
.to_vec(),
};

let tx = MoonlightTransaction::new(
let tx = Transaction::moonlight(
&moonlight_sk,
None,
0,
Expand All @@ -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());
Expand Down Expand Up @@ -660,7 +660,7 @@ fn convert_wrong_contract_targeted() {
.to_vec(),
};

let tx = MoonlightTransaction::new(
let tx = Transaction::moonlight(
&moonlight_sk,
None,
0,
Expand All @@ -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");

Expand Down Expand Up @@ -732,7 +732,7 @@ fn contract_to_contract() {
.to_vec(),
});

let transaction = MoonlightTransaction::new(
let transaction = Transaction::moonlight(
&moonlight_sk,
None,
0,
Expand All @@ -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);
Expand Down Expand Up @@ -799,7 +799,7 @@ fn contract_to_account() {
.to_vec(),
});

let transaction = MoonlightTransaction::new(
let transaction = Transaction::moonlight(
&moonlight_sk,
None,
0,
Expand All @@ -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);
Expand Down Expand Up @@ -863,7 +863,7 @@ fn contract_to_account_insufficient_funds() {
.to_vec(),
});

let transaction = MoonlightTransaction::new(
let transaction = Transaction::moonlight(
&moonlight_sk,
None,
0,
Expand All @@ -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);
Expand Down Expand Up @@ -934,7 +934,7 @@ fn contract_to_account_direct_call() {
.to_vec(),
});

let transaction = MoonlightTransaction::new(
let transaction = Transaction::moonlight(
&moonlight_sk,
None,
0,
Expand All @@ -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!(
Expand Down
Loading

0 comments on commit 0680ba0

Please sign in to comment.