Skip to content

Commit

Permalink
stash commit
Browse files Browse the repository at this point in the history
  • Loading branch information
moCello committed Dec 13, 2024
1 parent 2aacfcb commit 7c53d2c
Show file tree
Hide file tree
Showing 21 changed files with 367 additions and 224 deletions.
7 changes: 4 additions & 3 deletions contracts/stake/tests/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -98,6 +97,7 @@ pub fn filter_notes_owned_by<I: IntoIterator<Item = Note>>(
.collect()
}

/*
/// Executes a transaction, returning the call receipt
pub fn execute(
session: &mut Session,
Expand Down Expand Up @@ -135,6 +135,7 @@ pub fn execute(
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.
Expand Down
26 changes: 13 additions & 13 deletions contracts/stake/tests/partial_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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);

Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
39 changes: 19 additions & 20 deletions contracts/stake/tests/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand Down
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
Loading

0 comments on commit 7c53d2c

Please sign in to comment.