-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement could-be-standard token receiver hook in account and multis…
…ig (#555)
- Loading branch information
Showing
5 changed files
with
136 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,87 @@ | ||
// Copyright 2019-2022 ChainSafe Systems | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
|
||
use fil_actor_account::{testing::check_state_invariants, Actor as AccountActor, State}; | ||
use fil_actors_runtime::builtin::SYSTEM_ACTOR_ADDR; | ||
use fil_actors_runtime::test_utils::*; | ||
use fvm_ipld_encoding::RawBytes; | ||
use fvm_shared::address::Address; | ||
use fvm_shared::error::ExitCode; | ||
use fvm_shared::MethodNum; | ||
|
||
fn check_state(rt: &MockRuntime) { | ||
let test_address = Address::new_id(1000); | ||
let (_, acc) = check_state_invariants(&rt.get_state(), &test_address); | ||
acc.assert_empty(); | ||
} | ||
use fil_actor_account::{testing::check_state_invariants, Actor as AccountActor, Method, State}; | ||
use fil_actors_runtime::builtin::SYSTEM_ACTOR_ADDR; | ||
use fil_actors_runtime::test_utils::*; | ||
|
||
#[test] | ||
fn construction() { | ||
fn construct(addr: Address, exit_code: ExitCode) { | ||
let mut rt = MockRuntime { | ||
receiver: Address::new_id(100), | ||
caller: *SYSTEM_ACTOR_ADDR, | ||
caller_type: *SYSTEM_ACTOR_CODE_ID, | ||
..Default::default() | ||
}; | ||
rt.expect_validate_caller_addr(vec![*SYSTEM_ACTOR_ADDR]); | ||
|
||
macro_rules! account_tests { | ||
($($name:ident: $value:expr,)*) => { | ||
$( | ||
#[test] | ||
fn $name() { | ||
let (addr, exit_code) = $value; | ||
if exit_code.is_success() { | ||
rt.call::<AccountActor>( | ||
Method::Constructor as MethodNum, | ||
&RawBytes::serialize(addr).unwrap(), | ||
) | ||
.unwrap(); | ||
|
||
let mut rt = MockRuntime { | ||
receiver: fvm_shared::address::Address::new_id(100), | ||
caller: SYSTEM_ACTOR_ADDR.clone(), | ||
caller_type: SYSTEM_ACTOR_CODE_ID.clone(), | ||
..Default::default() | ||
}; | ||
rt.expect_validate_caller_addr(vec![*SYSTEM_ACTOR_ADDR]); | ||
let state: State = rt.get_state(); | ||
assert_eq!(state.address, addr); | ||
rt.expect_validate_caller_any(); | ||
|
||
if exit_code.is_success() { | ||
rt.call::<AccountActor>(1, &RawBytes::serialize(addr).unwrap()).unwrap(); | ||
let pk: Address = rt | ||
.call::<AccountActor>(Method::PubkeyAddress as MethodNum, &RawBytes::default()) | ||
.unwrap() | ||
.deserialize() | ||
.unwrap(); | ||
assert_eq!(pk, addr); | ||
check_state(&rt); | ||
} else { | ||
expect_abort(exit_code, rt.call::<AccountActor>(1, &RawBytes::serialize(addr).unwrap())) | ||
} | ||
rt.verify(); | ||
} | ||
|
||
let state: State = rt.get_state(); | ||
assert_eq!(state.address, addr); | ||
rt.expect_validate_caller_any(); | ||
construct( | ||
Address::new_secp256k1(&[2; fvm_shared::address::SECP_PUB_LEN]).unwrap(), | ||
ExitCode::OK, | ||
); | ||
construct(Address::new_bls(&[1; fvm_shared::address::BLS_PUB_LEN]).unwrap(), ExitCode::OK); | ||
construct(Address::new_id(1), ExitCode::USR_ILLEGAL_ARGUMENT); | ||
construct(Address::new_actor(&[1, 2, 3]), ExitCode::USR_ILLEGAL_ARGUMENT); | ||
} | ||
|
||
let pk: Address = rt | ||
.call::<AccountActor>(2, &RawBytes::default()) | ||
.unwrap() | ||
.deserialize() | ||
.unwrap(); | ||
assert_eq!(pk, addr); | ||
#[test] | ||
fn token_receiver() { | ||
let mut rt = MockRuntime { | ||
receiver: Address::new_id(100), | ||
caller: *SYSTEM_ACTOR_ADDR, | ||
caller_type: *SYSTEM_ACTOR_CODE_ID, | ||
..Default::default() | ||
}; | ||
rt.expect_validate_caller_addr(vec![*SYSTEM_ACTOR_ADDR]); | ||
|
||
check_state(&rt); | ||
} else { | ||
expect_abort( | ||
exit_code, | ||
rt.call::<AccountActor>(1,&RawBytes::serialize(addr).unwrap()) | ||
) | ||
} | ||
rt.verify(); | ||
} | ||
)* | ||
} | ||
let param = Address::new_secp256k1(&[2; fvm_shared::address::SECP_PUB_LEN]).unwrap(); | ||
rt.call::<AccountActor>( | ||
Method::Constructor as MethodNum, | ||
&RawBytes::serialize(¶m).unwrap(), | ||
) | ||
.unwrap(); | ||
|
||
rt.expect_validate_caller_any(); | ||
let ret = rt.call::<AccountActor>( | ||
Method::FungibleTokenReceiverHook as MethodNum, | ||
&RawBytes::new(vec![1, 2, 3]), | ||
); | ||
assert!(ret.is_ok()); | ||
assert_eq!(RawBytes::default(), ret.unwrap()); | ||
} | ||
|
||
account_tests! { | ||
happy_construct_secp256k1_address: ( | ||
Address::new_secp256k1(&[2; fvm_shared::address::SECP_PUB_LEN]).unwrap(), | ||
ExitCode::OK | ||
), | ||
happy_construct_bls_address: ( | ||
Address::new_bls(&[1; fvm_shared::address::BLS_PUB_LEN]).unwrap(), | ||
ExitCode::OK | ||
), | ||
fail_construct_id_address: ( | ||
Address::new_id(1), | ||
ExitCode::USR_ILLEGAL_ARGUMENT | ||
), | ||
fail_construct_actor_address: ( | ||
Address::new_actor(&[1, 2, 3]), | ||
ExitCode::USR_ILLEGAL_ARGUMENT | ||
), | ||
fn check_state(rt: &MockRuntime) { | ||
let test_address = Address::new_id(1000); | ||
let (_, acc) = check_state_invariants(&rt.get_state(), &test_address); | ||
acc.assert_empty(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters