Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mpapierski committed Oct 18, 2024
1 parent 0c3466c commit 4c578c5
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 36 deletions.
2 changes: 1 addition & 1 deletion executor/wasm-host/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ pub fn casper_call<S: GlobalStateReader + 'static, E: Executor + 'static>(
}
}

Ok(host_result)
Ok(dbg!(host_result))
}

pub fn casper_env_caller<S: GlobalStateReader, E: Executor>(
Expand Down
2 changes: 1 addition & 1 deletion executor/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl ExecutorV2 {

// 2. Store wasm

let bytecode = ByteCode::new(ByteCodeKind::V1CasperWasm, wasm_bytes.clone().into());
let bytecode = ByteCode::new(ByteCodeKind::V2CasperWasm, wasm_bytes.clone().into());
let bytecode_addr = ByteCodeAddr::V2CasperWasm(bytecode_hash);

tracking_copy.write(
Expand Down
24 changes: 18 additions & 6 deletions smart_contracts/contracts/vm2/vm2-harness/src/contracts/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ use casper_sdk::{
},
error::Error,
keyspace::Keyspace,
},
collections::Map,
host::{self, Entity},
log, revert,
}, collections::Map, host::{self, Entity}, log, revert, types::CallError, ContractHandle
};

use crate::traits::{DepositExt, DepositRef};

pub(crate) const INITIAL_GREETING: &str = "This is initial data set from a constructor";
pub(crate) const BALANCES_PREFIX: &str = "b";

Expand Down Expand Up @@ -64,6 +63,8 @@ pub enum CustomError {
Named { name: String, age: u64 },
#[error("transfer error {0}")]
Transfer(String),
#[error("custom")]
Deposit(CallError),
}

impl Default for Harness {
Expand Down Expand Up @@ -313,7 +314,7 @@ impl Harness {
}

#[casper(payable, revert_on_error)]
pub fn deposit(&mut self, balance_before: u128) -> Result<(), CustomError> {
pub fn perform_token_deposit(&mut self, balance_before: u128) -> Result<(), CustomError> {
let caller = host::get_caller();
let value = host::get_value();

Expand Down Expand Up @@ -358,7 +359,18 @@ impl Harness {
}
}
Entity::Contract(contract) => {
todo!("call contract to transfer tokens");
let result = ContractHandle::<DepositRef>::from_address(contract)
.build_call()
.with_transferred_value(amount)
.try_call(|harness| harness.deposit());

// dbg!(&result);
if let Err(call_error) = result.unwrap().result {
log!("Unable to perform a transfer: {call_error:?}");
return Err(CustomError::Deposit(call_error));
}
// log!("Result of invoking deposit interface on contract {result:?}");
// result?;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use casper_sdk::{
ContractHandle,
};

use crate::traits::{Fallback, FallbackExt};
use crate::traits::{Deposit, DepositExt};

use super::harness::HarnessRef;

Expand Down Expand Up @@ -72,7 +72,7 @@ impl TokenOwnerContract {
let res = ContractHandle::<HarnessRef>::from_address(contract_address)
.build_call()
.with_transferred_value(amount)
.call(|harness| harness.deposit(self_balance))?;
.call(|harness| harness.perform_token_deposit(self_balance))?;
match &res {
Ok(()) => log!("Token owner deposited {amount} to {contract_address:?}"),
Err(e) => log!("Token owner failed to deposit {amount} to {contract_address:?}: {e:?}"),
Expand Down Expand Up @@ -136,8 +136,9 @@ impl TokenOwnerContract {
}

#[casper(path = crate::traits)]
impl Fallback for TokenOwnerContract {
fn fallback(&mut self) {
impl Deposit for TokenOwnerContract {
fn deposit(&mut self) {
log!("Received deposit with value = {} current handler is {:?}", host::get_value(), self.fallback_handler);
match std::mem::replace(&mut self.fallback_handler, FallbackHandler::AcceptTokens) {
FallbackHandler::AcceptTokens => {
let value = host::get_value();
Expand All @@ -149,6 +150,7 @@ impl Fallback for TokenOwnerContract {
}
FallbackHandler::RejectWithRevert => {
// This will cause a revert.
log!("TokenOwnerContract rejected with revert");
revert!();
}
FallbackHandler::RejectWithTrap => {
Expand Down
4 changes: 2 additions & 2 deletions smart_contracts/contracts/vm2/vm2-harness/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ fn perform_test(flipper_address: Address) {
contract_handle
.build_call()
.with_transferred_value(100)
.call(|harness| harness.deposit(account_balance_1))
.call(|harness| harness.perform_token_deposit(account_balance_1))
.expect("Should call")
.expect("Should succeed");
let account_balance_2 = host::get_balance_of(&caller);
Expand All @@ -278,7 +278,7 @@ fn perform_test(flipper_address: Address) {
contract_handle
.build_call()
.with_transferred_value(25)
.call(|harness| harness.deposit(account_balance_2))
.call(|harness| harness.perform_token_deposit(account_balance_2))
.expect("Should call")
.expect("Should succeed");

Expand Down
10 changes: 7 additions & 3 deletions smart_contracts/contracts/vm2/vm2-harness/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use casper_macros::casper;

/// Deposit interface for contracts to receive tokens.
///
/// Useful for contracts that need to receive tokens.
#[casper]
pub trait Fallback {
#[casper(fallback)]
fn fallback(&mut self);
pub trait Deposit {
/// Deposit tokens into the contract.
#[casper(payable)]
fn deposit(&mut self);
}

#[casper]
Expand Down
2 changes: 1 addition & 1 deletion smart_contracts/sdk/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ impl<T: ToCallData> CallResult<T> {
{
match self.result {
Ok(()) | Err(CallError::CalleeReverted) => {
let data = dbg!(self.data).unwrap_or_default();
let data = self.data.unwrap_or_default();
Ok(borsh::from_slice(&data).unwrap())
}
Err(call_error) => Err(call_error),
Expand Down
35 changes: 17 additions & 18 deletions storage/src/system/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,24 +425,23 @@ impl TransferRuntimeArgsBuilder {
where
R: StateReader<Key, StoredValue, Error = GlobalStateError>,
{
let (to, target) = match self
.resolve_transfer_target_mode(protocol_version, Rc::clone(&tracking_copy))?
{
NewTransferTargetMode::ExistingAccount {
main_purse: purse_uref,
target_account_hash: target_account,
} => (Some(target_account), purse_uref),
NewTransferTargetMode::PurseExists {
target_account_hash,
purse_uref,
} => (target_account_hash, purse_uref),
NewTransferTargetMode::CreateAccount(_) => {
// Method "build()" is called after `resolve_transfer_target_mode` is first called
// and handled by creating a new account. Calling `resolve_transfer_target_mode`
// for the second time should never return `CreateAccount` variant.
return Err(TransferError::InvalidOperation);
}
};
let (to, target) =
match self.resolve_transfer_target_mode(protocol_version, Rc::clone(&tracking_copy))? {
NewTransferTargetMode::ExistingAccount {
main_purse: purse_uref,
target_account_hash: target_account,
} => (Some(target_account), purse_uref),
NewTransferTargetMode::PurseExists {
target_account_hash,
purse_uref,
} => (target_account_hash, purse_uref),
NewTransferTargetMode::CreateAccount(_) => {
// Method "build()" is called after `resolve_transfer_target_mode` is first called
// and handled by creating a new account. Calling `resolve_transfer_target_mode`
// for the second time should never return `CreateAccount` variant.
return Err(TransferError::InvalidOperation);
}
};

let source =
self.resolve_source_uref(from, entity_named_keys, Rc::clone(&tracking_copy))?;
Expand Down

0 comments on commit 4c578c5

Please sign in to comment.