From 2acef81eb8c0901b459ca5a7f91507ecf30ba7f6 Mon Sep 17 00:00:00 2001 From: Andrew Bright Date: Wed, 21 Sep 2022 14:26:58 +1000 Subject: [PATCH] separate individual frc46 tests into blocks --- .../tests/frc46_multi_actor_tests.rs | 257 ++++++++++-------- .../tests/frc46_single_actor_tests.rs | 190 +++++++------ 2 files changed, 245 insertions(+), 202 deletions(-) diff --git a/testing/fil_token_integration/tests/frc46_multi_actor_tests.rs b/testing/fil_token_integration/tests/frc46_multi_actor_tests.rs index 05e3c32a..411a9813 100644 --- a/testing/fil_token_integration/tests/frc46_multi_actor_tests.rs +++ b/testing/fil_token_integration/tests/frc46_multi_actor_tests.rs @@ -44,128 +44,157 @@ fn frc46_multi_actor_tests() { } // TEST: alice sends bob a transfer of zero amount (rejecting first time and then accepting) - // first, tell bob to reject it - let params = action_params(token_actor, TestAction::Transfer(bob, action(TestAction::Reject))); - let ret_val = tester.call_method(operator[0].1, alice, method_hash!("Action"), Some(params)); - // we told bob to reject, so the action call should return success but give us the error result as return data - assert!(ret_val.msg_receipt.exit_code.is_success()); - // check the receipt we got in return data - let receipt = ret_val.msg_receipt.return_data.deserialize::().unwrap(); - assert!(!receipt.exit_code.is_success()); - - // this time tell bob to accept it - let params = action_params(token_actor, TestAction::Transfer(bob, action(TestAction::Accept))); - let ret_val = tester.call_method(operator[0].1, alice, method_hash!("Action"), Some(params)); - // we told bob to accept this time, so transfer should succeed - assert!(ret_val.msg_receipt.exit_code.is_success()); - - // balance should remain zero - let balance = tester.token_balance(operator[0].1, token_actor, alice); - assert_eq!(balance, TokenAmount::from_atto(0)); - let balance = tester.token_balance(operator[0].1, token_actor, bob); - assert_eq!(balance, TokenAmount::from_atto(0)); + { + // first, tell bob to reject it + let params = + action_params(token_actor, TestAction::Transfer(bob, action(TestAction::Reject))); + let ret_val = + tester.call_method(operator[0].1, alice, method_hash!("Action"), Some(params)); + // we told bob to reject, so the action call should return success but give us the error result as return data + assert!(ret_val.msg_receipt.exit_code.is_success()); + // check the receipt we got in return data + let receipt = ret_val.msg_receipt.return_data.deserialize::().unwrap(); + assert!(!receipt.exit_code.is_success()); + } + { + // this time tell bob to accept it + let params = + action_params(token_actor, TestAction::Transfer(bob, action(TestAction::Accept))); + let ret_val = + tester.call_method(operator[0].1, alice, method_hash!("Action"), Some(params)); + // we told bob to accept this time, so transfer should succeed + assert!(ret_val.msg_receipt.exit_code.is_success()); + // balance should remain zero + let balance = tester.token_balance(operator[0].1, token_actor, alice); + assert_eq!(balance, TokenAmount::from_atto(0)); + let balance = tester.token_balance(operator[0].1, token_actor, bob); + assert_eq!(balance, TokenAmount::from_atto(0)); + } // TEST: alice sends bob a transfer of a non-zero amounnt. As before, we'll reject it the first time then accept - // mint some tokens to alice first - let ret_val = tester.mint_tokens( - operator[0].1, - token_actor, - alice, - TokenAmount::from_atto(100), - action(TestAction::Accept), - ); - assert!(ret_val.msg_receipt.exit_code.is_success()); - let balance = tester.token_balance(operator[0].1, token_actor, alice); - assert_eq!(balance, TokenAmount::from_atto(100)); - // now send to bob, who will reject them - let params = action_params(token_actor, TestAction::Transfer(bob, action(TestAction::Reject))); - let ret_val = tester.call_method(operator[0].1, alice, method_hash!("Action"), Some(params)); - assert!(ret_val.msg_receipt.exit_code.is_success()); - // check the receipt we got in return data - let receipt = ret_val.msg_receipt.return_data.deserialize::().unwrap(); - assert!(!receipt.exit_code.is_success()); - - // transfer to bob who will accept it this time - let params = action_params(token_actor, TestAction::Transfer(bob, action(TestAction::Accept))); - let ret_val = tester.call_method(operator[0].1, alice, method_hash!("Action"), Some(params)); - assert!(ret_val.msg_receipt.exit_code.is_success()); - // check balances - let balance = tester.token_balance(operator[0].1, token_actor, alice); - assert_eq!(balance, TokenAmount::from_atto(0)); - let balance = tester.token_balance(operator[0].1, token_actor, bob); - assert_eq!(balance, TokenAmount::from_atto(100)); + { + // mint some tokens to alice first + let ret_val = tester.mint_tokens( + operator[0].1, + token_actor, + alice, + TokenAmount::from_atto(100), + action(TestAction::Accept), + ); + assert!(ret_val.msg_receipt.exit_code.is_success()); + let balance = tester.token_balance(operator[0].1, token_actor, alice); + assert_eq!(balance, TokenAmount::from_atto(100)); + // now send to bob, who will reject them + let params = + action_params(token_actor, TestAction::Transfer(bob, action(TestAction::Reject))); + let ret_val = + tester.call_method(operator[0].1, alice, method_hash!("Action"), Some(params)); + assert!(ret_val.msg_receipt.exit_code.is_success()); + // check the receipt we got in return data + let receipt = ret_val.msg_receipt.return_data.deserialize::().unwrap(); + assert!(!receipt.exit_code.is_success()); + } + { + // transfer to bob who will accept it this time + let params = + action_params(token_actor, TestAction::Transfer(bob, action(TestAction::Accept))); + let ret_val = + tester.call_method(operator[0].1, alice, method_hash!("Action"), Some(params)); + assert!(ret_val.msg_receipt.exit_code.is_success()); + // check balances + let balance = tester.token_balance(operator[0].1, token_actor, alice); + assert_eq!(balance, TokenAmount::from_atto(0)); + let balance = tester.token_balance(operator[0].1, token_actor, bob); + assert_eq!(balance, TokenAmount::from_atto(100)); + } // TEST: mint to alice who transfers to bob inside receiver hook, bob accepts - let ret_val = tester.mint_tokens( - operator[0].1, - token_actor, - alice, - TokenAmount::from_atto(100), - action(TestAction::Transfer(bob, action(TestAction::Accept))), - ); - assert!(ret_val.msg_receipt.exit_code.is_success()); - let balance = tester.token_balance(operator[0].1, token_actor, alice); - assert_eq!(balance, TokenAmount::from_atto(0)); - let balance = tester.token_balance(operator[0].1, token_actor, bob); - assert_eq!(balance, TokenAmount::from_atto(200)); + { + let ret_val = tester.mint_tokens( + operator[0].1, + token_actor, + alice, + TokenAmount::from_atto(100), + action(TestAction::Transfer(bob, action(TestAction::Accept))), + ); + assert!(ret_val.msg_receipt.exit_code.is_success()); + let balance = tester.token_balance(operator[0].1, token_actor, alice); + assert_eq!(balance, TokenAmount::from_atto(0)); + let balance = tester.token_balance(operator[0].1, token_actor, bob); + assert_eq!(balance, TokenAmount::from_atto(200)); + } // TEST: mint to alice who transfers to bob inside receiver hook, bob rejects - let ret_val = tester.mint_tokens( - operator[0].1, - token_actor, - alice, - TokenAmount::from_atto(100), - action(TestAction::Transfer(bob, action(TestAction::Reject))), - ); - // mint succeeds but the transfer inside the receiver hook would have failed - assert!(ret_val.msg_receipt.exit_code.is_success()); - // alice should keep tokens in this case - let balance = tester.token_balance(operator[0].1, token_actor, alice); - assert_eq!(balance, TokenAmount::from_atto(100)); - // bob's balance should remain unchanged - let balance = tester.token_balance(operator[0].1, token_actor, bob); - assert_eq!(balance, TokenAmount::from_atto(200)); + { + let ret_val = tester.mint_tokens( + operator[0].1, + token_actor, + alice, + TokenAmount::from_atto(100), + action(TestAction::Transfer(bob, action(TestAction::Reject))), + ); + // mint succeeds but the transfer inside the receiver hook would have failed + assert!(ret_val.msg_receipt.exit_code.is_success()); + // alice should keep tokens in this case + let balance = tester.token_balance(operator[0].1, token_actor, alice); + assert_eq!(balance, TokenAmount::from_atto(100)); + // bob's balance should remain unchanged + let balance = tester.token_balance(operator[0].1, token_actor, bob); + assert_eq!(balance, TokenAmount::from_atto(200)); + } // TEST: alice transfers to bob, bob transfers to carol (from hook), carol accepts - let params = action_params( - token_actor, - TestAction::Transfer(bob, action(TestAction::Transfer(carol, action(TestAction::Accept)))), - ); - let ret_val = tester.call_method(operator[0].1, alice, method_hash!("Action"), Some(params)); - assert!(ret_val.msg_receipt.exit_code.is_success()); - // check balances - alice should be empty, bob should keep 200, carol sitting on 100 - let balance = tester.token_balance(operator[0].1, token_actor, alice); - assert_eq!(balance, TokenAmount::from_atto(0)); - let balance = tester.token_balance(operator[0].1, token_actor, bob); - assert_eq!(balance, TokenAmount::from_atto(200)); - let balance = tester.token_balance(operator[0].1, token_actor, carol); - assert_eq!(balance, TokenAmount::from_atto(100)); + { + let params = action_params( + token_actor, + TestAction::Transfer( + bob, + action(TestAction::Transfer(carol, action(TestAction::Accept))), + ), + ); + let ret_val = + tester.call_method(operator[0].1, alice, method_hash!("Action"), Some(params)); + assert!(ret_val.msg_receipt.exit_code.is_success()); + // check balances - alice should be empty, bob should keep 200, carol sitting on 100 + let balance = tester.token_balance(operator[0].1, token_actor, alice); + assert_eq!(balance, TokenAmount::from_atto(0)); + let balance = tester.token_balance(operator[0].1, token_actor, bob); + assert_eq!(balance, TokenAmount::from_atto(200)); + let balance = tester.token_balance(operator[0].1, token_actor, carol); + assert_eq!(balance, TokenAmount::from_atto(100)); + } // TEST: alice transfers to bob, bob transfers to carol (from hook), carol burns (from hook) - // mint a bit to alice first - let ret_val = tester.mint_tokens( - operator[0].1, - token_actor, - alice, - TokenAmount::from_atto(100), - action(TestAction::Accept), - ); - assert!(ret_val.msg_receipt.exit_code.is_success()); - // now transfer alice->bob->carol and have carol burn the incoming balance - let params = action_params( - token_actor, - TestAction::Transfer(bob, action(TestAction::Transfer(carol, action(TestAction::Burn)))), - ); - let ret_val = tester.call_method(operator[0].1, alice, method_hash!("Action"), Some(params)); - assert!(ret_val.msg_receipt.exit_code.is_success()); - // check the receipt we got in return data - let receipt = ret_val.msg_receipt.return_data.deserialize::().unwrap(); - assert!(receipt.exit_code.is_success()); - // check balances - alice should be empty, bob should keep 200, carol sitting on 100 - let balance = tester.token_balance(operator[0].1, token_actor, alice); - assert_eq!(balance, TokenAmount::from_atto(0)); - let balance = tester.token_balance(operator[0].1, token_actor, bob); - assert_eq!(balance, TokenAmount::from_atto(200)); - let balance = tester.token_balance(operator[0].1, token_actor, carol); - assert_eq!(balance, TokenAmount::from_atto(100)); + { + // mint a bit to alice first + let ret_val = tester.mint_tokens( + operator[0].1, + token_actor, + alice, + TokenAmount::from_atto(100), + action(TestAction::Accept), + ); + assert!(ret_val.msg_receipt.exit_code.is_success()); + // now transfer alice->bob->carol and have carol burn the incoming balance + let params = action_params( + token_actor, + TestAction::Transfer( + bob, + action(TestAction::Transfer(carol, action(TestAction::Burn))), + ), + ); + let ret_val = + tester.call_method(operator[0].1, alice, method_hash!("Action"), Some(params)); + assert!(ret_val.msg_receipt.exit_code.is_success()); + // check the receipt we got in return data + let receipt = ret_val.msg_receipt.return_data.deserialize::().unwrap(); + assert!(receipt.exit_code.is_success()); + // check balances - alice should be empty, bob should keep 200, carol sitting on 100 + let balance = tester.token_balance(operator[0].1, token_actor, alice); + assert_eq!(balance, TokenAmount::from_atto(0)); + let balance = tester.token_balance(operator[0].1, token_actor, bob); + assert_eq!(balance, TokenAmount::from_atto(200)); + let balance = tester.token_balance(operator[0].1, token_actor, carol); + assert_eq!(balance, TokenAmount::from_atto(100)); + } } diff --git a/testing/fil_token_integration/tests/frc46_single_actor_tests.rs b/testing/fil_token_integration/tests/frc46_single_actor_tests.rs index a8c1cea3..4adb3519 100644 --- a/testing/fil_token_integration/tests/frc46_single_actor_tests.rs +++ b/testing/fil_token_integration/tests/frc46_single_actor_tests.rs @@ -41,106 +41,120 @@ fn frc46_single_actor_tests() { tester.instantiate_machine(DummyExterns).unwrap(); // construct actors - let ret_val = tester.call_method(operator[0].1, token_actor, method_hash!("Constructor"), None); - assert!(ret_val.msg_receipt.exit_code.is_success()); - let ret_val = tester.call_method(operator[0].1, test_actor, method_hash!("Constructor"), None); - assert!(ret_val.msg_receipt.exit_code.is_success()); + for actor in [token_actor, test_actor] { + let ret_val = tester.call_method(operator[0].1, actor, method_hash!("Constructor"), None); + assert!(ret_val.msg_receipt.exit_code.is_success()); + } // TEST: mint to test actor who rejects hook - let ret_val = tester.mint_tokens( - operator[0].1, - token_actor, - test_actor, - TokenAmount::from_atto(100), - action(TestAction::Reject), - ); - assert!(!ret_val.msg_receipt.exit_code.is_success()); - - // check balance of test actor, should be zero - let balance = tester.token_balance(operator[0].1, token_actor, test_actor); - assert_eq!(balance, TokenAmount::from_atto(0)); + { + let ret_val = tester.mint_tokens( + operator[0].1, + token_actor, + test_actor, + TokenAmount::from_atto(100), + action(TestAction::Reject), + ); + assert!(!ret_val.msg_receipt.exit_code.is_success()); + + // check balance of test actor, should be zero + let balance = tester.token_balance(operator[0].1, token_actor, test_actor); + assert_eq!(balance, TokenAmount::from_atto(0)); + } // TEST: mint to self (token actor), should be rejected - let ret_val = tester.mint_tokens( - operator[0].1, - token_actor, - token_actor, - TokenAmount::from_atto(100), - action(TestAction::Reject), - ); - // should fail because the token actor has no receiver hook - assert!(!ret_val.msg_receipt.exit_code.is_success()); + { + let ret_val = tester.mint_tokens( + operator[0].1, + token_actor, + token_actor, + TokenAmount::from_atto(100), + action(TestAction::Reject), + ); + // should fail because the token actor has no receiver hook + assert!(!ret_val.msg_receipt.exit_code.is_success()); + } // TEST: mint to test actor, hook burns tokens immediately - let ret_val = tester.mint_tokens( - operator[0].1, - token_actor, - test_actor, - TokenAmount::from_atto(100), - action(TestAction::Burn), - ); - let mint_result: MintReturn = ret_val.msg_receipt.return_data.deserialize().unwrap(); - // tokens were burned so supply reduces back to zero - assert_eq!(mint_result.supply, TokenAmount::from_atto(0)); - - // check balance of test actor, should also be zero - let balance = tester.token_balance(operator[0].1, token_actor, test_actor); - assert_eq!(balance, TokenAmount::from_atto(0)); + { + let ret_val = tester.mint_tokens( + operator[0].1, + token_actor, + test_actor, + TokenAmount::from_atto(100), + action(TestAction::Burn), + ); + let mint_result: MintReturn = ret_val.msg_receipt.return_data.deserialize().unwrap(); + // tokens were burned so supply reduces back to zero + assert_eq!(mint_result.supply, TokenAmount::from_atto(0)); + + // check balance of test actor, should also be zero + let balance = tester.token_balance(operator[0].1, token_actor, test_actor); + assert_eq!(balance, TokenAmount::from_atto(0)); + } // TEST: test actor transfers to self (zero amount) - let test_action = ActionParams { - token_address: token_actor, - action: TestAction::Transfer(test_actor, action(TestAction::Accept)), - }; - let params = RawBytes::serialize(test_action).unwrap(); - let ret_val = - tester.call_method(operator[0].1, test_actor, method_hash!("Action"), Some(params)); - assert!(ret_val.msg_receipt.exit_code.is_success()); - - // balance should remain zero - let balance = tester.token_balance(operator[0].1, token_actor, test_actor); - assert_eq!(balance, TokenAmount::from_atto(0)); + { + let test_action = ActionParams { + token_address: token_actor, + action: TestAction::Transfer(test_actor, action(TestAction::Accept)), + }; + let params = RawBytes::serialize(test_action).unwrap(); + let ret_val = + tester.call_method(operator[0].1, test_actor, method_hash!("Action"), Some(params)); + assert!(ret_val.msg_receipt.exit_code.is_success()); + + // balance should remain zero + let balance = tester.token_balance(operator[0].1, token_actor, test_actor); + assert_eq!(balance, TokenAmount::from_atto(0)); + } // SETUP: we need a balance on the test actor for the next few tests - let ret_val = tester.mint_tokens( - operator[0].1, - token_actor, - test_actor, - TokenAmount::from_atto(100), - action(TestAction::Accept), - ); - let mint_result: MintReturn = ret_val.msg_receipt.return_data.deserialize().unwrap(); - assert_eq!(mint_result.supply, TokenAmount::from_atto(100)); - let balance = tester.token_balance(operator[0].1, token_actor, test_actor); - assert_eq!(balance, TokenAmount::from_atto(100)); + { + let ret_val = tester.mint_tokens( + operator[0].1, + token_actor, + test_actor, + TokenAmount::from_atto(100), + action(TestAction::Accept), + ); + let mint_result: MintReturn = ret_val.msg_receipt.return_data.deserialize().unwrap(); + assert_eq!(mint_result.supply, TokenAmount::from_atto(100)); + let balance = tester.token_balance(operator[0].1, token_actor, test_actor); + assert_eq!(balance, TokenAmount::from_atto(100)); + } // TEST: test actor transfers back to token actor (rejected, token actor has no hook) - let test_action = ActionParams { - token_address: token_actor, - action: TestAction::Transfer(token_actor, RawBytes::default()), - }; - let params = RawBytes::serialize(test_action).unwrap(); - let ret_val = - tester.call_method(operator[0].1, test_actor, method_hash!("Action"), Some(params)); - // action call should succeed, we'll dig into the return data to see the transfer call failure - assert!(ret_val.msg_receipt.exit_code.is_success()); - // return data is the Receipt from calling Transfer, which should show failure - let receipt: Receipt = ret_val.msg_receipt.return_data.deserialize().unwrap(); - assert!(!receipt.exit_code.is_success()); - // check that our test actor balance hasn't changed - let balance = tester.token_balance(operator[0].1, token_actor, test_actor); - assert_eq!(balance, TokenAmount::from_atto(100)); + { + let test_action = ActionParams { + token_address: token_actor, + action: TestAction::Transfer(token_actor, RawBytes::default()), + }; + let params = RawBytes::serialize(test_action).unwrap(); + let ret_val = + tester.call_method(operator[0].1, test_actor, method_hash!("Action"), Some(params)); + // action call should succeed, we'll dig into the return data to see the transfer call failure + assert!(ret_val.msg_receipt.exit_code.is_success()); + // return data is the Receipt from calling Transfer, which should show failure + let receipt: Receipt = ret_val.msg_receipt.return_data.deserialize().unwrap(); + assert!(!receipt.exit_code.is_success()); + // check that our test actor balance hasn't changed + let balance = tester.token_balance(operator[0].1, token_actor, test_actor); + assert_eq!(balance, TokenAmount::from_atto(100)); + } // TEST: test actor transfers to self (non-zero amount) - let test_action = ActionParams { - token_address: token_actor, - action: TestAction::Transfer(test_actor, action(TestAction::Accept)), - }; - let params = RawBytes::serialize(test_action).unwrap(); - let ret_val = - tester.call_method(operator[0].1, test_actor, method_hash!("Action"), Some(params)); - assert!(ret_val.msg_receipt.exit_code.is_success()); - // check that our test actor balance hasn't changed - let balance = tester.token_balance(operator[0].1, token_actor, test_actor); - assert_eq!(balance, TokenAmount::from_atto(100)); + { + let test_action = ActionParams { + token_address: token_actor, + action: TestAction::Transfer(test_actor, action(TestAction::Accept)), + }; + let params = RawBytes::serialize(test_action).unwrap(); + let ret_val = + tester.call_method(operator[0].1, test_actor, method_hash!("Action"), Some(params)); + assert!(ret_val.msg_receipt.exit_code.is_success()); + // check that our test actor balance hasn't changed + let balance = tester.token_balance(operator[0].1, token_actor, test_actor); + assert_eq!(balance, TokenAmount::from_atto(100)); + } }