From e69de2439c723551543a2a169c7eeafbe8a2f552 Mon Sep 17 00:00:00 2001 From: DragonMural Date: Sun, 31 May 2020 14:10:17 -0400 Subject: [PATCH 01/29] Adding Payment Actor Tests --- vm/actor/src/builtin/paych/mod.rs | 4 + vm/actor/tests/common/mod.rs | 13 +- vm/actor/tests/paych_actor_test.rs | 330 +++++++++++++++++++++++++++++ 3 files changed, 345 insertions(+), 2 deletions(-) create mode 100644 vm/actor/tests/paych_actor_test.rs diff --git a/vm/actor/src/builtin/paych/mod.rs b/vm/actor/src/builtin/paych/mod.rs index 26f0b0a0cfe0..860048a19165 100644 --- a/vm/actor/src/builtin/paych/mod.rs +++ b/vm/actor/src/builtin/paych/mod.rs @@ -349,6 +349,10 @@ impl ActorCode for Actor { Self::collect(rt)?; Ok(Serialized::default()) } + Some(Method::UpdateChannelState) => { + Self::update_channel_state(rt, params.deserialize().unwrap())?; + Ok(Serialized::default()) + } _ => Err(rt.abort(ExitCode::SysErrInvalidMethod, "Invalid method")), } } diff --git a/vm/actor/tests/common/mod.rs b/vm/actor/tests/common/mod.rs index cb189ff32e96..01a3b9b2f93d 100644 --- a/vm/actor/tests/common/mod.rs +++ b/vm/actor/tests/common/mod.rs @@ -125,6 +125,10 @@ impl<'a, BS: BlockStore> MockRuntime<'a, BS> { assert!(addr.len() > 0, "addrs must be non-empty"); *self.expect_validate_caller_addr.borrow_mut() = Some(addr.to_vec()); } + pub fn expect_validate_caller_type(&self, ids: &[Cid]) { + assert!(ids.len() > 0, "addrs must be non-empty"); + *self.expect_validate_caller_type.borrow_mut() = Some(ids.to_vec()); + } #[allow(dead_code)] pub fn expect_validate_caller_any(&self) { @@ -368,8 +372,13 @@ impl Runtime for MockRuntime<'_, BS> { fn get_actor_code_cid(&self, addr: &Address) -> Result { self.require_in_call(); - let ret = self.actor_code_cids.get(&addr).unwrap(); - Ok(ret.clone()) + if let Some(ret) = self.actor_code_cids.get(&addr) { + return Ok(ret.clone()); + } + Err(ActorError::new( + ExitCode::ErrIllegalArgument, + "Actor address is not found".to_string(), + )) } fn get_randomness( diff --git a/vm/actor/tests/paych_actor_test.rs b/vm/actor/tests/paych_actor_test.rs new file mode 100644 index 000000000000..866b07865f23 --- /dev/null +++ b/vm/actor/tests/paych_actor_test.rs @@ -0,0 +1,330 @@ +// Copyright 2020 ChainSafe Systems +// SPDX-License-Identifier: Apache-2.0, MIT + +mod common; + +use actor::{ + paych::{ConstructorParams, Method, SignedVoucher, State as PState, UpdateChannelStateParams}, + ACCOUNT_ACTOR_CODE_ID, INIT_ACTOR_ADDR, INIT_ACTOR_CODE_ID, MULTISIG_ACTOR_CODE_ID, + PAYCH_ACTOR_CODE_ID, REWARD_ACTOR_ADDR, REWARD_ACTOR_CODE_ID, SYSTEM_ACTOR_ADDR, + SYSTEM_ACTOR_CODE_ID, +}; +use address::Address; +use cid::Cid; +use clock::ChainEpoch; +use common::*; +use crypto::Signature; +use db::MemoryDB; +use derive_builder::Builder; +use ipld_blockstore::BlockStore; +use message::UnsignedMessage; +use num_bigint::BigInt; +use vm::{ExitCode, Serialized, TokenAmount, METHOD_CONSTRUCTOR}; + +#[test] +fn create_paych_actor_test() { + let paych_addr = Address::new_id(100); + let payer_addr = Address::new_id(101); + let caller_addr = Address::new_id(102); + + let bs = MemoryDB::default(); + let message = UnsignedMessage::builder() + .from(*SYSTEM_ACTOR_ADDR) + .to(paych_addr) + .build() + .unwrap(); + let mut rt = MockRuntime::new(&bs, message); + rt.set_caller(INIT_ACTOR_CODE_ID.clone(), caller_addr); + rt.actor_code_cids + .insert(payer_addr, ACCOUNT_ACTOR_CODE_ID.clone()); + rt.actor_code_cids + .insert(caller_addr, ACCOUNT_ACTOR_CODE_ID.clone()); + construct_and_verify(&mut rt, payer_addr, caller_addr); +} + +#[test] +fn actor_doesnt_exist_test() { + let paych_addr = Address::new_id(100); + let payer_addr = Address::new_id(101); + let caller_addr = Address::new_id(102); + let bs = MemoryDB::default(); + let message = UnsignedMessage::builder() + .from(*SYSTEM_ACTOR_ADDR) + .to(paych_addr) + .build() + .unwrap(); + let mut rt = MockRuntime::new(&bs, message); + rt.set_caller(INIT_ACTOR_CODE_ID.clone(), caller_addr); + rt.actor_code_cids + .insert(payer_addr, ACCOUNT_ACTOR_CODE_ID.clone()); + rt.expect_validate_caller_type(&[INIT_ACTOR_CODE_ID.clone()]); + let params = ConstructorParams { + to: paych_addr, + from: payer_addr, + }; + + let error = rt + .call( + &PAYCH_ACTOR_CODE_ID.clone(), + METHOD_CONSTRUCTOR, + &Serialized::serialize(params).unwrap(), + ) + .unwrap_err(); + assert_eq!(error.exit_code(), ExitCode::ErrIllegalArgument); +} + +#[test] +fn actor_constructor_fails() { + let paych_addr = Address::new_id(100); + let payer_addr = Address::new_id(101); + let caller_addr = Address::new_id(102); + + struct TestCase { + paych_addr: Address, + caller_code: Cid, + new_actor_code: Cid, + payer_code: Cid, + expected_exit_code: ExitCode, + } + + let test_cases: Vec = vec![TestCase { + paych_addr: paych_addr, + caller_code: INIT_ACTOR_CODE_ID.clone(), + new_actor_code: MULTISIG_ACTOR_CODE_ID.clone(), + payer_code: ACCOUNT_ACTOR_CODE_ID.clone(), + expected_exit_code: ExitCode::ErrIllegalArgument, + }]; + + for test_case in test_cases { + let bs = MemoryDB::default(); + let message = UnsignedMessage::builder() + .from(*SYSTEM_ACTOR_ADDR) + .to(paych_addr) + .build() + .unwrap(); + let mut rt = MockRuntime::new(&bs, message); + rt.set_caller(test_case.caller_code, caller_addr); + rt.actor_code_cids + .insert(test_case.paych_addr, test_case.new_actor_code); + rt.actor_code_cids.insert(payer_addr, test_case.payer_code); + rt.expect_validate_caller_type(&[INIT_ACTOR_CODE_ID.clone()]); + let params = ConstructorParams { + to: test_case.paych_addr, + from: Address::new_id(10001), + }; + let error = rt + .call( + &PAYCH_ACTOR_CODE_ID.clone(), + METHOD_CONSTRUCTOR, + &Serialized::serialize(params).unwrap(), + ) + .unwrap_err(); + assert_eq!(test_case.expected_exit_code, error.exit_code()); + } +} + +mod create_lane_tests { + use super::*; + #[derive(Builder)] + #[builder(name = "TestCaseBuilder")] + struct TestCase { + #[builder(default = "ACCOUNT_ACTOR_CODE_ID.clone()")] + target_code: Cid, + #[builder(default)] + balance: u64, + #[builder(default)] + recieved: u64, + #[builder(default = "1")] + epoch: ChainEpoch, + #[builder(default = "1")] + tl_min: ChainEpoch, + #[builder(default = "0")] + tl_max: ChainEpoch, + #[builder(default)] + lane: u64, + #[builder(default)] + nonce: u64, + #[builder(default = "1")] + amt: i64, + #[builder(default)] + secret_preimage: Vec, + #[builder(default)] + sig: Option, + #[builder(default = "true")] + verify_sig: bool, + #[builder(default = "ExitCode::ErrIllegalArgument")] + exp_exit_code: ExitCode, + } + impl TestCase { + pub fn builder() -> TestCaseBuilder { + TestCaseBuilder::default() + } + } + + #[test] + fn create_lane_test() { + let init_actor_addr = Address::new_id(100); + let paych_addr = Address::new_id(101); + let payer_addr = Address::new_id(102); + let payee_addr = Address::new_id(103); + let paych_balance = TokenAmount::from(9 as u64); + let sig = Option::Some(Signature::new_bls("doesn't matter".as_bytes().to_vec())); + + let test_cases: Vec = vec![ + // TestCase::builder() + // .sig(sig.clone()) + // .exp_exit_code(ExitCode::Ok) + // .build() + // .unwrap(), + TestCase::builder() + .amt(-1) + .sig(sig.clone()) + .exp_exit_code(ExitCode::ErrIllegalState) + .build() + .unwrap(), + TestCase::builder() + .amt(10) + .sig(sig.clone()) + .exp_exit_code(ExitCode::ErrIllegalState) + .build() + .unwrap(), + TestCase::builder().sig(Option::None).build().unwrap(), + TestCase::builder() + .tl_min(10) + .sig(sig.clone()) + .build() + .unwrap(), + TestCase::builder() + .epoch(10) + .tl_max(5) + .sig(sig.clone()) + .build() + .unwrap(), + TestCase::builder() + .sig(sig.clone()) + .verify_sig(false) + .build() + .unwrap(), + TestCase::builder() + .sig(sig.clone()) + .secret_preimage(vec![0; 2 << 21]) + .exp_exit_code(ExitCode::ErrIllegalState) + .build() + .unwrap(), + ]; + + for test_case in test_cases { + let bs = MemoryDB::default(); + let message = UnsignedMessage::builder() + .from(*SYSTEM_ACTOR_ADDR) + .to(paych_addr) + .build() + .unwrap(); + let mut rt = MockRuntime::new(&bs, message); + rt.epoch = test_case.epoch; + rt.balance = TokenAmount::from(test_case.balance); + rt.set_caller(INIT_ACTOR_CODE_ID.clone(), *INIT_ACTOR_ADDR); + rt.actor_code_cids + .insert(payee_addr, ACCOUNT_ACTOR_CODE_ID.clone()); + rt.actor_code_cids + .insert(payer_addr, ACCOUNT_ACTOR_CODE_ID.clone()); + construct_and_verify(&mut rt, payer_addr, payee_addr); + + let sv = SignedVoucher { + time_lock_min: test_case.tl_min, + time_lock_max: test_case.tl_max, + secret_pre_image: test_case.secret_preimage.clone(), + extra: Option::None, + lane: test_case.lane, + nonce: test_case.nonce, + amount: BigInt::from(test_case.amt), + min_settle_height: 0, + merges: vec![], + signature: test_case.sig.clone(), + }; + + let ucp = UpdateChannelStateParams { + sv: sv, + secret: vec![], + proof: vec![], + }; + + rt.set_caller(test_case.target_code, payee_addr); + rt.expect_validate_caller_addr(&[payer_addr, payee_addr]); + if test_case.exp_exit_code == ExitCode::Ok { + assert!(rt + .call( + &PAYCH_ACTOR_CODE_ID.clone(), + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap() + ) + .is_ok()); + let st: PState = rt.get_state().unwrap(); + assert_eq!(st.lane_states.len(), 1); + let ls = st.lane_states.first().unwrap(); + let sv = SignedVoucher { + time_lock_min: test_case.tl_min, + time_lock_max: test_case.tl_max, + secret_pre_image: test_case.secret_preimage, + extra: Option::None, + lane: test_case.lane, + nonce: test_case.nonce, + amount: BigInt::from(test_case.amt), + min_settle_height: 0, + merges: vec![], + signature: test_case.sig, + }; + assert_eq!(sv.amount, ls.redeemed); + assert_eq!(sv.nonce, ls.nonce); + assert_eq!(sv.lane, ls.id); + } else { + let error = rt + .call( + &PAYCH_ACTOR_CODE_ID.clone(), + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap(), + ) + .unwrap_err(); + assert_eq!(error.exit_code(), test_case.exp_exit_code); + verify_initial_state(&mut rt, payer_addr, payee_addr); + } + rt.verify(); + } + } +} + +fn construct_and_verify( + rt: &mut MockRuntime<'_, BS>, + sender: Address, + receiver: Address, +) { + let params = ConstructorParams { + from: sender, + to: receiver, + }; + rt.expect_validate_caller_type(&[INIT_ACTOR_CODE_ID.clone()]); + let v = rt + .call( + &PAYCH_ACTOR_CODE_ID, + METHOD_CONSTRUCTOR, + &Serialized::serialize(¶ms).unwrap(), + ) + .unwrap(); + rt.verify(); + verify_initial_state(rt, sender, receiver); +} + +fn verify_initial_state( + rt: &mut MockRuntime<'_, BS>, + sender: Address, + receiver: Address, +) { + let state: PState = rt.get_state().unwrap(); + let expected_state = PState::new(sender, receiver); + assert_eq!(expected_state.to, state.to); + assert_eq!(expected_state.from, state.from); + assert_eq!(expected_state.min_settle_height, state.min_settle_height); + assert_eq!(expected_state.settling_at, state.settling_at); + assert_eq!(expected_state.to_send, state.to_send); + assert_eq!(state.lane_states.len(), 0); +} From 7c95e8680308228d6ac00c4f88404cce96f03d7e Mon Sep 17 00:00:00 2001 From: DragonMural Date: Wed, 3 Jun 2020 02:36:26 -0400 Subject: [PATCH 02/29] Adding lane tests. Need to fix syscalls --- vm/actor/Cargo.toml | 1 + vm/actor/src/builtin/paych/state.rs | 4 +- vm/actor/src/builtin/paych/types.rs | 6 +- vm/actor/tests/common/mod.rs | 24 +++- vm/actor/tests/paych_actor_test.rs | 203 +++++++++++++++++++++++++++- 5 files changed, 231 insertions(+), 7 deletions(-) diff --git a/vm/actor/Cargo.toml b/vm/actor/Cargo.toml index 4809c46c050b..bd3215c77954 100644 --- a/vm/actor/Cargo.toml +++ b/vm/actor/Cargo.toml @@ -25,6 +25,7 @@ unsigned-varint = "0.4" crypto = { package = "forest_crypto", path = "../../crypto" } rleplus = { path = "../../utils/rleplus" } fil_types = { path = "../../types" } +derive_builder = "0.9" [dev-dependencies] db = { path = "../../node/db" } diff --git a/vm/actor/src/builtin/paych/state.rs b/vm/actor/src/builtin/paych/state.rs index d7c982360935..25037ca833fd 100644 --- a/vm/actor/src/builtin/paych/state.rs +++ b/vm/actor/src/builtin/paych/state.rs @@ -43,7 +43,7 @@ impl State { /// The Lane state tracks the latest (highest) voucher nonce used to merge the lane /// as well as the amount it has already redeemed. -#[derive(Default, Debug, Serialize_tuple, Deserialize_tuple)] +#[derive(Default, PartialEq, Debug, Serialize_tuple, Deserialize_tuple)] pub struct LaneState { /// Identifier unique to this channel pub id: u64, @@ -54,7 +54,7 @@ pub struct LaneState { } /// Specifies which `lane`s to be merged with what `nonce` on `channel_update` -#[derive(Default, Debug, PartialEq, Serialize_tuple, Deserialize_tuple)] +#[derive(Default, Clone,Debug, PartialEq, Serialize_tuple, Deserialize_tuple)] pub struct Merge { pub lane: u64, pub nonce: u64, diff --git a/vm/actor/src/builtin/paych/types.rs b/vm/actor/src/builtin/paych/types.rs index a8c8bc1cd290..25a57972be11 100644 --- a/vm/actor/src/builtin/paych/types.rs +++ b/vm/actor/src/builtin/paych/types.rs @@ -24,7 +24,7 @@ pub struct ConstructorParams { /// A voucher is sent by `from` to `to` off-chain in order to enable /// `to` to redeem payments on-chain in the future -#[derive(Default, Debug, PartialEq, Serialize_tuple, Deserialize_tuple)] +#[derive(Default, Debug, Clone, PartialEq, Serialize_tuple, Deserialize_tuple)] pub struct SignedVoucher { /// Min epoch before which the voucher cannot be redeemed pub time_lock_min: ChainEpoch, @@ -55,7 +55,7 @@ pub struct SignedVoucher { } /// Modular Verification method -#[derive(Debug, PartialEq, Serialize_tuple, Deserialize_tuple)] +#[derive(Debug, Clone,PartialEq, Serialize_tuple, Deserialize_tuple)] pub struct ModVerifyParams { pub actor: Address, pub method: MethodNum, @@ -71,7 +71,7 @@ pub struct PaymentVerifyParams { pub proof: Vec, } -#[derive(Serialize_tuple, Deserialize_tuple)] +#[derive( Serialize_tuple, Deserialize_tuple)] pub struct UpdateChannelStateParams { pub sv: SignedVoucher, #[serde(with = "serde_bytes")] diff --git a/vm/actor/tests/common/mod.rs b/vm/actor/tests/common/mod.rs index 01a3b9b2f93d..78ae3c709d86 100644 --- a/vm/actor/tests/common/mod.rs +++ b/vm/actor/tests/common/mod.rs @@ -9,7 +9,7 @@ use actor::{ use address::Address; use cid::{multihash::Blake2b256, Cid}; use clock::ChainEpoch; -use crypto::DomainSeparationTag; +use crypto::{DomainSeparationTag, Signature}; use encoding::{de::DeserializeOwned, Cbor}; use ipld_blockstore::BlockStore; use message::{Message, UnsignedMessage}; @@ -33,6 +33,7 @@ pub struct MockRuntime<'a, BS: BlockStore> { // Actor State pub state: Option, pub balance: TokenAmount, + pub received: TokenAmount, // VM Impl pub in_call: bool, @@ -45,6 +46,7 @@ pub struct MockRuntime<'a, BS: BlockStore> { pub expect_validate_caller_type: RefCell>>, pub expect_sends: VecDeque, pub expect_create_actor: Option, + pub expect_verify_sig : Option, } #[derive(Clone, Debug)] @@ -64,6 +66,14 @@ pub struct ExpectedMessage { pub exit_code: ExitCode, } +#[derive(Clone, Debug)] +pub struct ExpectedVerifySig{ + sig : Signature, + signer : Address, + result : Option, +} + + impl<'a, BS: BlockStore> MockRuntime<'a, BS> { pub fn new(bs: &'a BS, message: UnsignedMessage) -> Self { Self { @@ -79,6 +89,7 @@ impl<'a, BS: BlockStore> MockRuntime<'a, BS> { message: message, state: None, balance: 0u8.into(), + received: 0u8.into(), // VM Impl in_call: false, @@ -91,6 +102,7 @@ impl<'a, BS: BlockStore> MockRuntime<'a, BS> { expect_validate_caller_type: RefCell::new(None), expect_sends: VecDeque::new(), expect_create_actor: None, + expect_verify_sig : None, } } fn require_in_call(&self) { @@ -130,6 +142,15 @@ impl<'a, BS: BlockStore> MockRuntime<'a, BS> { *self.expect_validate_caller_type.borrow_mut() = Some(ids.to_vec()); } + pub fn expect_verify_signature(&mut self, sig : Signature, signer : Address, result : Option){ + + self.expect_verify_sig = Some(ExpectedVerifySig{ + sig : sig, + signer : signer, + result : result + }); + } + #[allow(dead_code)] pub fn expect_validate_caller_any(&self) { self.expect_validate_caller_any.set(true); @@ -220,6 +241,7 @@ impl<'a, BS: BlockStore> MockRuntime<'a, BS> { *self.expect_validate_caller_addr.borrow_mut() = None; *self.expect_validate_caller_type.borrow_mut() = None; self.expect_create_actor = None; + self.expect_verify_sig = None; } #[allow(dead_code)] diff --git a/vm/actor/tests/paych_actor_test.rs b/vm/actor/tests/paych_actor_test.rs index 866b07865f23..ded3a9c811cc 100644 --- a/vm/actor/tests/paych_actor_test.rs +++ b/vm/actor/tests/paych_actor_test.rs @@ -4,7 +4,7 @@ mod common; use actor::{ - paych::{ConstructorParams, Method, SignedVoucher, State as PState, UpdateChannelStateParams}, + paych::{ConstructorParams, Method, SignedVoucher, State as PState, UpdateChannelStateParams, LaneState}, ACCOUNT_ACTOR_CODE_ID, INIT_ACTOR_ADDR, INIT_ACTOR_CODE_ID, MULTISIG_ACTOR_CODE_ID, PAYCH_ACTOR_CODE_ID, REWARD_ACTOR_ADDR, REWARD_ACTOR_CODE_ID, SYSTEM_ACTOR_ADDR, SYSTEM_ACTOR_CODE_ID, @@ -21,6 +21,15 @@ use message::UnsignedMessage; use num_bigint::BigInt; use vm::{ExitCode, Serialized, TokenAmount, METHOD_CONSTRUCTOR}; +struct lane_params{ + epoch_num : ChainEpoch, + from : Address, + to : Address, + amt : i64, + lane : u64, + nonce : u64 +} + #[test] fn create_paych_actor_test() { let paych_addr = Address::new_id(100); @@ -251,6 +260,7 @@ mod create_lane_tests { rt.set_caller(test_case.target_code, payee_addr); rt.expect_validate_caller_addr(&[payer_addr, payee_addr]); + if test_case.exp_exit_code == ExitCode::Ok { assert!(rt .call( @@ -293,6 +303,176 @@ mod create_lane_tests { } } +#[test] +fn redeem_voucher_one_lane(){ + let bs = MemoryDB::default(); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1 ); + let state: PState = rt.get_state().unwrap(); + let new_voucher_amount = BigInt::from(9); + sv.amount = new_voucher_amount; + let ucp = UpdateChannelStateParams{ + proof : vec![], + secret : vec![], + sv:sv.clone() + }; + let payee_addr = Address::new_id(103); + let payer_addr = Address::new_id(102); + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), payee_addr); + rt.expect_validate_caller_addr(&[state.from,state.to]); + rt.expect_verify_signature(sv.signature.unwrap(), payer_addr, None); + assert!(rt.call(&PAYCH_ACTOR_CODE_ID.clone(), Method::UpdateChannelState as u64 , &Serialized::serialize(ucp).unwrap()).is_ok()); + rt.verify(); + let exp_ls = LaneState { + id: 0, + redeemed: BigInt::from(9), + nonce: 1, + }; + let exp_state = PState{ + from : state.from, + to : state.to, + to_send : TokenAmount::from(9 as u64), + settling_at : state.settling_at, + min_settle_height : state.min_settle_height, + lane_states : vec![exp_ls] + }; + verify_state(&mut rt, 1, exp_state); +} + +#[test] +fn redeem_voucher_correct_lane(){ + let bs = MemoryDB::default(); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1 ); + let state: PState = rt.get_state().unwrap(); + let initial_amount = state.to_send; + sv.amount = BigInt::from(9); + sv.lane = 1; + let ls_to_update: &LaneState = &state.lane_states[1]; + sv.nonce = ls_to_update.nonce + 1; + let payee_addr = Address::new_id(103); + let payer_addr = Address::new_id(102); + + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), payee_addr); + rt.expect_validate_caller_addr(&[state.from,state.to]); + rt.expect_verify_signature(sv.clone().signature.unwrap(), payer_addr, None); + + let ucp = UpdateChannelStateParams{ + proof : vec![], + secret : vec![], + sv:sv.clone() + }; + + assert!(rt.call(&PAYCH_ACTOR_CODE_ID.clone(), Method::UpdateChannelState as u64 , &Serialized::serialize(ucp).unwrap()).is_ok()); + rt.verify(); + + let state: PState = rt.get_state().unwrap(); + let ls_updated: &LaneState = &state.lane_states[1]; + let big_delta = &sv.amount - BigInt::from_signed_bytes_be( &ls_to_update.redeemed.to_signed_bytes_be()); + + let exp_send = big_delta + BigInt::from_signed_bytes_be(&initial_amount.to_radix_be(10)); + assert_eq!(exp_send, BigInt::from_signed_bytes_be(&state.to_send.to_radix_be(10)) ); + assert_eq!(sv.amount, ls_updated.redeemed ); + assert_eq!(sv.nonce, ls_updated.nonce); + + + + + + + + +} + + +fn require_create_cannel_with_lanes( + bs: &BS, + num_lanes : u64 +) -> (MockRuntime< BS>,SignedVoucher) { + + let paych_addr = Address::new_id(100); + let payer_addr = Address::new_id(102); + let payee_addr = Address::new_id(103); + let balance = TokenAmount::from(100_000 as u64); + let recieved = TokenAmount::from(0 as u64); + + let curr_epoch = 2; + + let message = UnsignedMessage::builder() + .from(*SYSTEM_ACTOR_ADDR) + .to(paych_addr) + .build() + .unwrap(); + + let mut rt = MockRuntime::new(bs, message); + rt.epoch = curr_epoch; + rt.balance = TokenAmount::from(balance); + rt.received = TokenAmount::from(recieved); + rt.set_caller(INIT_ACTOR_CODE_ID.clone(), *INIT_ACTOR_ADDR); + rt.actor_code_cids + .insert(payee_addr, ACCOUNT_ACTOR_CODE_ID.clone()); + rt.actor_code_cids + .insert(payer_addr, ACCOUNT_ACTOR_CODE_ID.clone()); + construct_and_verify( &mut rt, payer_addr, payee_addr); + + let mut last_sv = SignedVoucher::default(); + for i in 0..num_lanes{ + + let lane_param = lane_params{ + epoch_num : curr_epoch, + from : payer_addr, + to : payee_addr, + amt : (i + 1) as i64, + lane : i, + nonce : i+1 + }; + + last_sv = require_add_new_lane(&mut rt, lane_param); + } + + (rt,last_sv) +} + +fn require_add_new_lane +(rt: &mut MockRuntime<'_, BS>, param : lane_params) -> SignedVoucher{ + let payee_addr = Address::new_id(103); + let sig = Signature::new_bls(vec![0,1,2,3,4,5,67,]); + let sv = SignedVoucher{ + time_lock_min: param.epoch_num, + time_lock_max: u64::MAX, + secret_pre_image: vec![], + extra: Option::None, + lane: param.lane, + nonce: param.nonce, + amount: BigInt::from(param.amt), + min_settle_height: 0, + merges: vec![], + signature: Some(sig.clone()), + }; + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), param.from); + rt.expect_validate_caller_addr(&[param.from, param.to]); + rt.expect_verify_signature(sig.clone(), payee_addr, None); + let ucp = UpdateChannelStateParams{ + proof : vec![], + secret : vec![], + sv : sv + }; + + assert!(rt.call(&PAYCH_ACTOR_CODE_ID.clone(), Method::UpdateChannelState as u64, &Serialized::serialize(ucp).unwrap()).is_ok()); + + rt.verify(); + SignedVoucher{ + time_lock_min: param.epoch_num, + time_lock_max: u64::MAX, + secret_pre_image: vec![], + extra: Option::None, + lane: param.lane, + nonce: param.nonce, + amount: BigInt::from(param.amt), + min_settle_height: 0, + merges: vec![], + signature: Some(sig.clone()), + } +} + fn construct_and_verify( rt: &mut MockRuntime<'_, BS>, sender: Address, @@ -328,3 +508,24 @@ fn verify_initial_state( assert_eq!(expected_state.to_send, state.to_send); assert_eq!(state.lane_states.len(), 0); } + +fn verify_state( + rt: &mut MockRuntime<'_, BS>, + exp_lanes : i64, + expected_state : PState){ + let state: PState = rt.get_state().unwrap(); + assert_eq!(expected_state.to, state.to); + assert_eq!(expected_state.from, state.from); + assert_eq!(expected_state.min_settle_height, state.min_settle_height); + assert_eq!(expected_state.settling_at, state.settling_at); + assert_eq!(expected_state.to_send, state.to_send); + + if exp_lanes > 0 { + assert_eq!(exp_lanes as u64 , state.lane_states.len() as u64 ); + assert_eq!(expected_state.lane_states, state.lane_states); + } + else { + assert_eq!(state.lane_states.len(), 0); + } + + } From 6f75431be02e2edbada435891c004a8a12926a7e Mon Sep 17 00:00:00 2001 From: DragonMural Date: Thu, 4 Jun 2020 00:01:32 -0400 Subject: [PATCH 03/29] Adding more tests. Need to add one more and then fix syscalls --- vm/actor/tests/paych_actor_test.rs | 325 ++++++++++++++++++++++++++++- 1 file changed, 321 insertions(+), 4 deletions(-) diff --git a/vm/actor/tests/paych_actor_test.rs b/vm/actor/tests/paych_actor_test.rs index ded3a9c811cc..25cc134b9be4 100644 --- a/vm/actor/tests/paych_actor_test.rs +++ b/vm/actor/tests/paych_actor_test.rs @@ -4,7 +4,7 @@ mod common; use actor::{ - paych::{ConstructorParams, Method, SignedVoucher, State as PState, UpdateChannelStateParams, LaneState}, + paych::{ConstructorParams, Method, SignedVoucher, State as PState, UpdateChannelStateParams, LaneState, Merge, LANE_LIMIT, ModVerifyParams,PaymentVerifyParams, SETTLE_DELAY}, ACCOUNT_ACTOR_CODE_ID, INIT_ACTOR_ADDR, INIT_ACTOR_CODE_ID, MULTISIG_ACTOR_CODE_ID, PAYCH_ACTOR_CODE_ID, REWARD_ACTOR_ADDR, REWARD_ACTOR_CODE_ID, SYSTEM_ACTOR_ADDR, SYSTEM_ACTOR_CODE_ID, @@ -18,7 +18,8 @@ use db::MemoryDB; use derive_builder::Builder; use ipld_blockstore::BlockStore; use message::UnsignedMessage; -use num_bigint::BigInt; +use num_bigint::{BigInt, Sign}; + use vm::{ExitCode, Serialized, TokenAmount, METHOD_CONSTRUCTOR}; struct lane_params{ @@ -372,14 +373,330 @@ fn redeem_voucher_correct_lane(){ assert_eq!(exp_send, BigInt::from_signed_bytes_be(&state.to_send.to_radix_be(10)) ); assert_eq!(sv.amount, ls_updated.redeemed ); assert_eq!(sv.nonce, ls_updated.nonce); +} + +#[test] +fn merge_success(){ + let num_lanes = 3; + let bs = MemoryDB::default(); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, num_lanes ); + let mut state: PState = rt.get_state().unwrap(); + let state_2: PState = rt.get_state().unwrap(); + + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + let merge_to : &LaneState= &state.lane_states[0]; + let merge_from : &LaneState= &state.lane_states[1]; + sv.lane = merge_to.id; + let merge_nonce = merge_to.nonce + 10; + let merges : Vec = vec![Merge{ + lane : merge_from.id, + nonce : merge_nonce + }]; + + sv.merges = merges; + let payee_addr = Address::new_id(103); + rt.expect_validate_caller_addr(&[state.from,state.to]); + rt.expect_verify_signature(sv.clone().signature.unwrap(), payee_addr, None); + let ucp = UpdateChannelStateParams{ + proof : vec![], + secret : vec![], + sv:sv.clone() + }; + assert!(rt.call(&PAYCH_ACTOR_CODE_ID.clone(), Method::UpdateChannelState as u64 , &Serialized::serialize(ucp).unwrap()).is_ok()); + rt.verify(); + + let (sv_amount_sign, sv_amount_bytes) = &sv.amount.to_bytes_be(); + + let exp_merge_to = LaneState{ + id : merge_to.id, + redeemed : BigInt::from_bytes_be(*sv_amount_sign, &sv_amount_bytes), + nonce : sv.nonce + }; + + let (from_sign, from_bytes) = &merge_from.redeemed.to_bytes_be(); + + let exp_merge_from = LaneState{ + id : merge_from.id, + redeemed : BigInt::from_bytes_be(*from_sign, &from_bytes), + nonce : merge_nonce + }; + + let (to_sign, to_bytes) = &merge_to.redeemed.to_bytes_be(); + + let redeemed = BigInt::from_bytes_be(*from_sign, &from_bytes) + BigInt::from_bytes_be(*to_sign, &to_bytes); + let exp_delta = sv.amount - redeemed; + let exp_send_amt = BigInt::from_bytes_be(Sign::Plus, &state.to_send.to_bytes_be()) + exp_delta; + let mut exp_state = state_2; + exp_state.to_send = TokenAmount::from_bytes_be(&exp_send_amt.to_signed_bytes_be()); + exp_state.lane_states = vec![exp_merge_to, exp_merge_from, exp_state.lane_states.pop().unwrap()]; + verify_state(&mut rt, num_lanes as i64, exp_state); + +} + +#[test] +fn merge_failue(){ + let lane_vec = vec![1,1,1,0]; + let voucher_vec= vec![10,0,10,10]; + let balance_vec= vec![0,0,1,0]; + let merge_vec= vec![1,10,10,10]; + let exit_vec= vec![ExitCode::ErrIllegalArgument, ExitCode::ErrIllegalArgument, ExitCode::ErrIllegalState, ExitCode::ErrIllegalArgument]; + let num_test_cases = lane_vec.len(); + let payee_addr = Address::new_id(103); + + for i in 0.. num_test_cases{ + let bs = MemoryDB::default(); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 2); + rt.balance = TokenAmount::from(balance_vec[i] as u64); + + let state: PState = rt.get_state().unwrap(); + let merge_to : &LaneState= &state.lane_states[0]; + let merge_from : &LaneState= &state.lane_states[1]; + sv.lane = merge_to.id; + sv.nonce = voucher_vec[i]; + sv.merges = vec![Merge{ + lane : merge_from.id, + nonce : merge_vec[i] + }]; + + let ucp = UpdateChannelStateParams{ + proof : vec![], + secret : vec![], + sv:sv.clone() + }; + + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + rt.expect_validate_caller_addr(&[state.from,state.to]); + rt.expect_verify_signature(sv.clone().signature.unwrap(), payee_addr, None); + let v = rt.call(&PAYCH_ACTOR_CODE_ID.clone(), Method::UpdateChannelState as u64 , &Serialized::serialize(ucp).unwrap()).unwrap_err(); + assert_eq!(v.exit_code(), exit_vec[i]); + rt.verify(); + } +} +#[test] +fn invalid_merge_lane_999(){ + let bs = MemoryDB::default(); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 2); + let state: PState = rt.get_state().unwrap(); + let payee_addr = Address::new_id(103); + let merge_to : &LaneState= &state.lane_states[0]; + let merge_from = LaneState{ + id : 999, + nonce : sv.nonce, + redeemed : BigInt::from(0) + }; + + sv.lane = merge_to.id; + sv.nonce = 10; + sv.merges = vec![Merge{ + lane : merge_from.id, + nonce : sv.nonce + }]; + let ucp = UpdateChannelStateParams{ + proof : vec![], + secret : vec![], + sv:sv.clone() + }; + + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + rt.expect_validate_caller_addr(&[state.from,state.to]); + rt.expect_verify_signature(sv.clone().signature.unwrap(), payee_addr, None); + let v = rt.call(&PAYCH_ACTOR_CODE_ID.clone(), Method::UpdateChannelState as u64 , &Serialized::serialize(ucp).unwrap()).unwrap_err(); + assert_eq!(v.exit_code(), ExitCode::ErrIllegalArgument); + rt.verify(); +} +#[test] +fn lane_limit_exceeded(){ + let bs = MemoryDB::default(); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, LANE_LIMIT as u64); + let state: PState = rt.get_state().unwrap(); + let payee_addr = Address::new_id(103); + sv.lane += 1; + sv.nonce += 1; + sv.amount = BigInt::from(100); + let ucp = UpdateChannelStateParams{ + proof : vec![], + secret : vec![], + sv:sv.clone() + }; + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + rt.expect_validate_caller_addr(&[state.from,state.to]); + rt.expect_verify_signature(sv.clone().signature.unwrap(), payee_addr, None); + let v = rt.call(&PAYCH_ACTOR_CODE_ID.clone(), Method::UpdateChannelState as u64 , &Serialized::serialize(ucp).unwrap()).unwrap_err(); + assert_eq!(v.exit_code(), ExitCode::ErrIllegalArgument); + rt.verify(); +} - - +#[test] +fn extra_call_succeed(){ + let bs = MemoryDB::default(); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); + let state: PState = rt.get_state().unwrap(); + let other_addr = Address::new_id(104); + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + sv.extra = Some(ModVerifyParams{ + actor : other_addr, + method : Method::UpdateChannelState as u64, + data : Serialized::serialize([1,2,3,4]).unwrap() + }); + rt.expect_validate_caller_addr(&[state.from,state.to]); + rt.expect_verify_signature(sv.clone().signature.unwrap(), state.to, None); + let exp_send_params = PaymentVerifyParams{ + extra : Serialized::serialize( vec![1,2,3,4]).unwrap(), + proof : vec![] + }; + rt.expect_send(other_addr, Method::UpdateChannelState as u64, Serialized::serialize(exp_send_params).unwrap(), TokenAmount::from(0u8), Serialized::default(), ExitCode::Ok); + let ucp = UpdateChannelStateParams{ + proof : vec![], + secret : vec![], + sv:sv.clone() + }; + assert!(rt.call(&PAYCH_ACTOR_CODE_ID.clone(), Method::UpdateChannelState as u64 , &Serialized::serialize(ucp).unwrap()).is_ok()); + rt.verify(); +} + +#[test] +fn extra_call_fail(){ + let bs = MemoryDB::default(); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); + let state: PState = rt.get_state().unwrap(); + let other_addr = Address::new_id(104); + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + sv.extra = Some(ModVerifyParams{ + actor : other_addr, + method : Method::UpdateChannelState as u64, + data : Serialized::serialize([1,2,3,4]).unwrap() + }); + rt.expect_validate_caller_addr(&[state.from,state.to]); + let exp_send_params = PaymentVerifyParams{ + extra : Serialized::serialize( vec![1,2,3,4]).unwrap(), + proof : vec![] + }; + rt.expect_send(other_addr, Method::UpdateChannelState as u64, Serialized::serialize(exp_send_params).unwrap(), TokenAmount::from(0u8), Serialized::default(), ExitCode::ErrPlaceholder); + rt.expect_verify_signature(sv.clone().signature.unwrap(), state.to, None); + let ucp = UpdateChannelStateParams{ + proof : vec![], + secret : vec![], + sv:sv.clone() + }; + let v = rt.call(&PAYCH_ACTOR_CODE_ID.clone(), Method::UpdateChannelState as u64 , &Serialized::serialize(ucp).unwrap()).unwrap_err(); + assert_eq!(v.exit_code(), ExitCode::ErrPlaceholder); + rt.verify(); +} + +#[test] +fn update_channel_setting(){ + let bs = MemoryDB::default(); + let (mut rt, sv) = require_create_cannel_with_lanes(&bs, 1); + rt.epoch = 10; + let state: PState = rt.get_state().unwrap(); + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + rt.expect_validate_caller_addr(&[state.from,state.to]); + assert!(rt.call(&PAYCH_ACTOR_CODE_ID.clone(),Method::Settle as u64 , &Serialized::default()).is_ok()); + + let exp_settling_at = SETTLE_DELAY + 10; + let state: PState = rt.get_state().unwrap(); + assert_eq!(exp_settling_at, state.settling_at); + assert_eq!(state.min_settle_height,0); + + let min_settle_vec = vec![0,2,12]; + let exp_min_settle_height = vec![state.min_settle_height ,2,12]; + let exp_settling_at = vec![state.settling_at,state.settling_at,12]; + let num_test_cases = min_settle_vec.len(); + + for i in 0 .. num_test_cases{ + let mut ucp = UpdateChannelStateParams{ + proof : vec![], + secret : vec![], + sv:sv.clone() + }; + ucp.sv.min_settle_height = min_settle_vec[i]; + rt.expect_validate_caller_addr(&[state.from,state.to]); + rt.expect_verify_signature(ucp.sv.clone().signature.unwrap(), state.to, None); + assert!(rt.call(&PAYCH_ACTOR_CODE_ID.clone(),Method::UpdateChannelState as u64 , &Serialized::serialize(ucp).unwrap()).is_ok()); + let new_state: PState = rt.get_state().unwrap(); + assert_eq!(exp_settling_at[i],new_state.settling_at); + assert_eq!(exp_min_settle_height[i],new_state.min_settle_height); + } +} + +#[test] +fn succeed_correct_secret(){ + let bs = MemoryDB::default(); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); + let state: PState = rt.get_state().unwrap(); + let mut ucp = UpdateChannelStateParams{ + proof : vec![], + secret : vec![], + sv:sv.clone() + }; + rt.expect_validate_caller_addr(&[state.from,state.to]); + rt.expect_verify_signature(ucp.sv.clone().signature.unwrap(), state.to, None); + assert!(rt.call(&PAYCH_ACTOR_CODE_ID.clone(),Method::UpdateChannelState as u64 , &Serialized::serialize(ucp).unwrap()).is_ok()); + rt.verify(); +} + +#[test] +fn incorrect_secret(){ + let bs = MemoryDB::default(); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); + let state: PState = rt.get_state().unwrap(); + let mut ucp = UpdateChannelStateParams{ + proof : vec![], + secret : vec![], + sv:sv.clone() + }; + rt.expect_validate_caller_addr(&[state.from,state.to]); + rt.expect_verify_signature(ucp.sv.clone().signature.unwrap(), state.to, None); + let v = rt.call(&PAYCH_ACTOR_CODE_ID.clone(),Method::UpdateChannelState as u64 , &Serialized::serialize(ucp).unwrap()).unwrap_err(); + assert_eq!(v.exit_code(), ExitCode::ErrIllegalArgument); + rt.verify(); +} + +#[test] +fn adjust_settling_at(){ + let bs = MemoryDB::default(); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); + let ep = 10; + rt.epoch = ep; + let mut state: PState = rt.get_state().unwrap(); + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + rt.expect_validate_caller_addr(&[state.from,state.to]); + assert!(rt.call(&PAYCH_ACTOR_CODE_ID.clone(),Method::Settle as u64 , &Serialized::default()).is_ok()); + let exp_settling_at = ep + SETTLE_DELAY; + state = rt.get_state().unwrap(); + assert_eq!(state.settling_at,exp_settling_at); + assert_eq!(state.min_settle_height, 0); +} + +#[test] +fn settle_if_height_less(){ + let bs = MemoryDB::default(); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); + let ep = 10; + rt.epoch = ep; + let mut state: PState = rt.get_state().unwrap(); + sv.min_settle_height = (ep + SETTLE_DELAY) + 1; + let mut ucp = UpdateChannelStateParams{ + proof : vec![], + secret : vec![], + sv:sv.clone() + }; + rt.expect_validate_caller_addr(&[state.from,state.to]); + rt.expect_verify_signature(ucp.sv.clone().signature.unwrap(), state.to, None); + assert!(rt.call(&PAYCH_ACTOR_CODE_ID.clone(),Method::UpdateChannelState as u64 , &Serialized::default()).is_ok()); + state = rt.get_state().unwrap(); + assert_eq!(state.settling_at,0); + assert_eq!(state.min_settle_height,ucp.sv.min_settle_height); + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + rt.expect_validate_caller_addr(&[state.from,state.to]); + assert!(rt.call(&PAYCH_ACTOR_CODE_ID.clone(),Method::Settle as u64 , &Serialized::default()).is_ok()); + state = rt.get_state().unwrap(); + assert_eq!(state.settling_at, ucp.sv.min_settle_height); } From 7d24eada7d539e901f36eb53a3d3cb5dfdfd7572 Mon Sep 17 00:00:00 2001 From: DragonMural Date: Sun, 7 Jun 2020 16:40:02 -0400 Subject: [PATCH 04/29] Addding syscalls functinality to mock runtime. plus some more minor chnages --- vm/actor/Cargo.toml | 1 + vm/actor/src/builtin/paych/state.rs | 2 +- vm/actor/src/builtin/paych/types.rs | 4 +- vm/actor/tests/account_actor_test.rs | 5 +- vm/actor/tests/common/mod.rs | 64 ++- vm/actor/tests/cron_actor_test.rs | 25 +- vm/actor/tests/init_actor_test.rs | 28 +- vm/actor/tests/paych_actor_test.rs | 643 ++++++++++++++++----------- vm/actor/tests/reward_actor_test.rs | 12 +- vm/interpreter/src/gas_syscalls.rs | 2 +- vm/interpreter/src/lib.rs | 6 +- 11 files changed, 497 insertions(+), 295 deletions(-) diff --git a/vm/actor/Cargo.toml b/vm/actor/Cargo.toml index bd3215c77954..5ddb228a6e66 100644 --- a/vm/actor/Cargo.toml +++ b/vm/actor/Cargo.toml @@ -28,6 +28,7 @@ fil_types = { path = "../../types" } derive_builder = "0.9" [dev-dependencies] +interpreter = { path = "../interpreter/" } db = { path = "../../node/db" } hex = "0.4.2" libp2p = "0.19" diff --git a/vm/actor/src/builtin/paych/state.rs b/vm/actor/src/builtin/paych/state.rs index 25037ca833fd..f0c01fc04131 100644 --- a/vm/actor/src/builtin/paych/state.rs +++ b/vm/actor/src/builtin/paych/state.rs @@ -54,7 +54,7 @@ pub struct LaneState { } /// Specifies which `lane`s to be merged with what `nonce` on `channel_update` -#[derive(Default, Clone,Debug, PartialEq, Serialize_tuple, Deserialize_tuple)] +#[derive(Default, Clone, Debug, PartialEq, Serialize_tuple, Deserialize_tuple)] pub struct Merge { pub lane: u64, pub nonce: u64, diff --git a/vm/actor/src/builtin/paych/types.rs b/vm/actor/src/builtin/paych/types.rs index 25a57972be11..3f0aa004a7a7 100644 --- a/vm/actor/src/builtin/paych/types.rs +++ b/vm/actor/src/builtin/paych/types.rs @@ -55,7 +55,7 @@ pub struct SignedVoucher { } /// Modular Verification method -#[derive(Debug, Clone,PartialEq, Serialize_tuple, Deserialize_tuple)] +#[derive(Debug, Clone, PartialEq, Serialize_tuple, Deserialize_tuple)] pub struct ModVerifyParams { pub actor: Address, pub method: MethodNum, @@ -71,7 +71,7 @@ pub struct PaymentVerifyParams { pub proof: Vec, } -#[derive( Serialize_tuple, Deserialize_tuple)] +#[derive(Serialize_tuple, Deserialize_tuple)] pub struct UpdateChannelStateParams { pub sv: SignedVoucher, #[serde(with = "serde_bytes")] diff --git a/vm/actor/tests/account_actor_test.rs b/vm/actor/tests/account_actor_test.rs index fb6edbea03e2..a4bab18915d8 100644 --- a/vm/actor/tests/account_actor_test.rs +++ b/vm/actor/tests/account_actor_test.rs @@ -10,6 +10,8 @@ use db::MemoryDB; use message::UnsignedMessage; use vm::{ExitCode, Serialized}; +use interpreter::{ DefaultSyscalls,}; + macro_rules! account_tests { ($($name:ident: $value:expr,)*) => { $( @@ -20,7 +22,8 @@ macro_rules! account_tests { let bs = MemoryDB::default(); let receiver = Address::new_id(100); let message = UnsignedMessage::builder().to(receiver.clone()).from(SYSTEM_ACTOR_ADDR.clone()).build().unwrap(); - let mut rt = MockRuntime::new(&bs, message); + let default_syscalls = DefaultSyscalls::new(&bs); + let mut rt = MockRuntime::new(&bs, &default_syscalls, message); rt.caller_type = SYSTEM_ACTOR_CODE_ID.clone(); rt.expect_validate_caller_addr(&[*SYSTEM_ACTOR_ADDR]); diff --git a/vm/actor/tests/common/mod.rs b/vm/actor/tests/common/mod.rs index 78ae3c709d86..bf84c2592b25 100644 --- a/vm/actor/tests/common/mod.rs +++ b/vm/actor/tests/common/mod.rs @@ -17,8 +17,14 @@ use runtime::{ActorCode, Runtime, Syscalls}; use std::cell::{Cell, RefCell}; use std::collections::{HashMap, VecDeque}; use vm::{ActorError, ExitCode, MethodNum, Randomness, Serialized, TokenAmount}; +//use interpreter::gas_syscalls::GasSyscalls; +use interpreter::{internal_send, ChainRand, DefaultRuntime, DefaultSyscalls, GasSyscalls}; +use interpreter::{price_list_by_epoch, GasTracker}; -pub struct MockRuntime<'a, BS: BlockStore> { +use std::rc::Rc; +//use super::gas_tracker::{price_list_by_epoch, GasTracker, PriceList}; + +pub struct MockRuntime<'a, 'sys, BS, SYS> { pub epoch: ChainEpoch, pub caller_type: Cid, pub miner: Address, @@ -27,6 +33,7 @@ pub struct MockRuntime<'a, BS: BlockStore> { pub actor_code_cids: HashMap, pub new_actor_addr: Option
, pub message: UnsignedMessage, + syscalls: GasSyscalls<'sys, SYS>, // TODO: syscalls: syscaller @@ -46,7 +53,7 @@ pub struct MockRuntime<'a, BS: BlockStore> { pub expect_validate_caller_type: RefCell>>, pub expect_sends: VecDeque, pub expect_create_actor: Option, - pub expect_verify_sig : Option, + pub expect_verify_sig: Option, } #[derive(Clone, Debug)] @@ -67,16 +74,28 @@ pub struct ExpectedMessage { } #[derive(Clone, Debug)] -pub struct ExpectedVerifySig{ - sig : Signature, - signer : Address, - result : Option, +pub struct ExpectedVerifySig { + sig: Signature, + signer: Address, + result: Option, } +impl<'a, 'sys, BS, SYS> MockRuntime<'a, 'sys, BS, SYS> +where + BS: BlockStore, + SYS: Syscalls, +{ + pub fn new(bs: &'a BS, default_syscalls: &'sys SYS, message: UnsignedMessage) -> Self { + let price_list = price_list_by_epoch(0); + let gas_tracker = Rc::new(RefCell::new(GasTracker::new(message.gas_limit() as i64, 0))); + + let gas_syscalls = GasSyscalls { + price_list, + gas: Rc::clone(&gas_tracker), + syscalls: default_syscalls, + }; -impl<'a, BS: BlockStore> MockRuntime<'a, BS> { - pub fn new(bs: &'a BS, message: UnsignedMessage) -> Self { - Self { + MockRuntime { epoch: 0, caller_type: Cid::default(), @@ -90,6 +109,7 @@ impl<'a, BS: BlockStore> MockRuntime<'a, BS> { state: None, balance: 0u8.into(), received: 0u8.into(), + syscalls: gas_syscalls, // VM Impl in_call: false, @@ -102,7 +122,7 @@ impl<'a, BS: BlockStore> MockRuntime<'a, BS> { expect_validate_caller_type: RefCell::new(None), expect_sends: VecDeque::new(), expect_create_actor: None, - expect_verify_sig : None, + expect_verify_sig: None, } } fn require_in_call(&self) { @@ -142,12 +162,16 @@ impl<'a, BS: BlockStore> MockRuntime<'a, BS> { *self.expect_validate_caller_type.borrow_mut() = Some(ids.to_vec()); } - pub fn expect_verify_signature(&mut self, sig : Signature, signer : Address, result : Option){ - - self.expect_verify_sig = Some(ExpectedVerifySig{ - sig : sig, - signer : signer, - result : result + pub fn expect_verify_signature( + &mut self, + sig: Signature, + signer: Address, + result: Option, + ) { + self.expect_verify_sig = Some(ExpectedVerifySig { + sig: sig, + signer: signer, + result: result, }); } @@ -283,7 +307,11 @@ impl<'a, BS: BlockStore> MockRuntime<'a, BS> { } } -impl Runtime for MockRuntime<'_, BS> { +impl Runtime for MockRuntime<'_, '_, BS, SYS> +where + BS: BlockStore, + SYS: Syscalls, +{ fn message(&self) -> &UnsignedMessage { self.require_in_call(); &self.message @@ -541,6 +569,6 @@ impl Runtime for MockRuntime<'_, BS> { } fn syscalls(&self) -> &dyn Syscalls { - unimplemented!() + &self.syscalls } } diff --git a/vm/actor/tests/cron_actor_test.rs b/vm/actor/tests/cron_actor_test.rs index 1eaa22cf2462..03b9dbcda617 100644 --- a/vm/actor/tests/cron_actor_test.rs +++ b/vm/actor/tests/cron_actor_test.rs @@ -12,8 +12,11 @@ use db::MemoryDB; use ipld_blockstore::BlockStore; use message::UnsignedMessage; use vm::{ExitCode, Serialized}; +use runtime::{ Syscalls}; +use interpreter::{DefaultSyscalls}; -fn construct_runtime(bs: &BS) -> MockRuntime<'_, BS> { + +fn construct_runtime<'a,'sys,BS: BlockStore, SYS: Syscalls>(bs: &'a BS, default_calls : &'sys SYS) -> MockRuntime<'a, 'sys, BS, SYS> { let receiver = Address::new_id(100); let message = UnsignedMessage::builder() @@ -21,14 +24,16 @@ fn construct_runtime(bs: &BS) -> MockRuntime<'_, BS> { .to(receiver.clone()) .build() .unwrap(); - let mut rt = MockRuntime::new(bs, message); + let mut rt = MockRuntime::new(bs, default_calls, message); rt.caller_type = SYSTEM_ACTOR_CODE_ID.clone(); return rt; } #[test] fn construct_with_empty_entries() { let bs = MemoryDB::default(); - let mut rt = construct_runtime(&bs); + let default_syscalls = DefaultSyscalls::new(&bs); + + let mut rt = construct_runtime(&bs, &default_syscalls); construct_and_verify(&mut rt, &ConstructorParams { entries: vec![] }); let state: State = rt.get_state().unwrap(); @@ -39,7 +44,9 @@ fn construct_with_empty_entries() { #[test] fn construct_with_entries() { let bs = MemoryDB::default(); - let mut rt = construct_runtime(&bs); + let default_syscalls = DefaultSyscalls::new(&bs); + + let mut rt = construct_runtime(&bs, &default_syscalls); let entry1 = Entry { receiver: Address::new_id(1001), @@ -72,7 +79,8 @@ fn construct_with_entries() { #[test] fn epoch_tick_with_empty_entries() { let bs = MemoryDB::default(); - let mut rt = construct_runtime(&bs); + let default_syscalls = DefaultSyscalls::new(&bs); + let mut rt = construct_runtime(&bs, &default_syscalls); construct_and_verify(&mut rt, &ConstructorParams { entries: vec![] }); epoch_tick_and_verify(&mut rt); @@ -80,7 +88,8 @@ fn epoch_tick_with_empty_entries() { #[test] fn epoch_tick_with_entries() { let bs = MemoryDB::default(); - let mut rt = construct_runtime(&bs); + let default_syscalls = DefaultSyscalls::new(&bs); + let mut rt = construct_runtime(&bs, &default_syscalls); let entry1 = Entry { receiver: Address::new_id(1001), @@ -147,7 +156,7 @@ fn epoch_tick_with_entries() { epoch_tick_and_verify(&mut rt); } -fn construct_and_verify(rt: &mut MockRuntime<'_, BS>, params: &ConstructorParams) { +fn construct_and_verify(rt: &mut MockRuntime<'_,'_, BS, SYS>, params: &ConstructorParams) { rt.expect_validate_caller_addr(&[*SYSTEM_ACTOR_ADDR]); let ret = rt .call( @@ -160,7 +169,7 @@ fn construct_and_verify(rt: &mut MockRuntime<'_, BS>, params: &C rt.verify(); } -fn epoch_tick_and_verify(rt: &mut MockRuntime<'_, BS>) { +fn epoch_tick_and_verify(rt: &mut MockRuntime<'_, '_, BS, SYS>) { rt.expect_validate_caller_addr(&[*SYSTEM_ACTOR_ADDR]); let ret = rt .call(&*CRON_ACTOR_CODE_ID, 2, &Serialized::default()) diff --git a/vm/actor/tests/init_actor_test.rs b/vm/actor/tests/init_actor_test.rs index e2b6dc238dc1..62e1fc47e57e 100644 --- a/vm/actor/tests/init_actor_test.rs +++ b/vm/actor/tests/init_actor_test.rs @@ -17,15 +17,18 @@ use ipld_blockstore::BlockStore; use message::{Message, UnsignedMessage}; use serde::Serialize; use vm::{ActorError, ExitCode, Serialized, TokenAmount, METHOD_CONSTRUCTOR}; +use runtime::{Syscalls}; +use interpreter::{DefaultSyscalls}; -fn construct_runtime(bs: &BS) -> MockRuntime<'_, BS> { + +fn construct_runtime<'a,'sys,BS: BlockStore, SYS: Syscalls>(bs: &'a BS, default_syscalls: &'sys SYS) -> MockRuntime<'a, 'sys, BS, SYS> { let receiver = Address::new_id(1000); let message = UnsignedMessage::builder() .to(receiver.clone()) .from(SYSTEM_ACTOR_ADDR.clone()) .build() .unwrap(); - let mut rt = MockRuntime::new(bs, message); + let mut rt = MockRuntime::new(bs, default_syscalls,message); rt.caller_type = SYSTEM_ACTOR_CODE_ID.clone(); return rt; } @@ -34,7 +37,8 @@ fn construct_runtime(bs: &BS) -> MockRuntime<'_, BS> { #[test] fn abort_cant_call_exec() { let bs = MemoryDB::default(); - let mut rt = construct_runtime(&bs); + let default_syscalls = DefaultSyscalls::new(&bs); + let mut rt = construct_runtime(&bs, &default_syscalls); construct_and_verify(&mut rt); let anne = Address::new_id(1001); @@ -48,7 +52,8 @@ fn abort_cant_call_exec() { #[test] fn create_2_payment_channels() { let bs = MemoryDB::default(); - let mut rt: MockRuntime = construct_runtime(&bs); + let default_syscalls = DefaultSyscalls::new(&bs); + let mut rt= construct_runtime(&bs, & default_syscalls); construct_and_verify(&mut rt); let anne = Address::new_id(1001); @@ -112,7 +117,8 @@ fn create_2_payment_channels() { #[test] fn create_storage_miner() { let bs = MemoryDB::default(); - let mut rt: MockRuntime = construct_runtime(&bs); + let default_syscalls = DefaultSyscalls::new(&bs); + let mut rt= construct_runtime(&bs, &default_syscalls); construct_and_verify(&mut rt); // only the storage power actor can create a miner @@ -163,7 +169,8 @@ fn create_storage_miner() { #[test] fn create_multisig_actor() { let bs = MemoryDB::default(); - let mut rt = construct_runtime(&bs); + let default_syscalls = DefaultSyscalls::new(&bs); + let mut rt = construct_runtime(&bs, & default_syscalls); construct_and_verify(&mut rt); // Actor creating multisig actor @@ -207,7 +214,8 @@ fn create_multisig_actor() { #[test] fn sending_constructor_failure() { let bs = MemoryDB::default(); - let mut rt = construct_runtime(&bs); + let default_syscalls = DefaultSyscalls::new(&bs); + let mut rt = construct_runtime(&bs, &default_syscalls); construct_and_verify(&mut rt); // Only the storage power actor can create a miner @@ -259,7 +267,7 @@ fn sending_constructor_failure() { ); } -fn construct_and_verify(rt: &mut MockRuntime<'_, BS>) { +fn construct_and_verify<'a,'sys,BS: BlockStore, SYS: Syscalls>(rt: &mut MockRuntime<'a, 'sys, BS, SYS>) { rt.expect_validate_caller_addr(&[SYSTEM_ACTOR_ADDR.clone()]); let params = ConstructorParams { network_name: "mock".to_string(), @@ -287,8 +295,8 @@ fn construct_and_verify(rt: &mut MockRuntime<'_, BS>) { assert_eq!("mock".to_string(), state_data.network_name); } -fn exec_and_verify( - rt: &mut MockRuntime<'_, BS>, +fn exec_and_verify<'a, 'sys, BS: BlockStore, SYS: Syscalls, S: Serialize>( + rt: &mut MockRuntime<'a, 'sys, BS, SYS>, code_id: Cid, params: &S, ) -> Result diff --git a/vm/actor/tests/paych_actor_test.rs b/vm/actor/tests/paych_actor_test.rs index 25cc134b9be4..89a08bd3aa57 100644 --- a/vm/actor/tests/paych_actor_test.rs +++ b/vm/actor/tests/paych_actor_test.rs @@ -4,7 +4,10 @@ mod common; use actor::{ - paych::{ConstructorParams, Method, SignedVoucher, State as PState, UpdateChannelStateParams, LaneState, Merge, LANE_LIMIT, ModVerifyParams,PaymentVerifyParams, SETTLE_DELAY}, + paych::{ + ConstructorParams, LaneState, Merge, Method, ModVerifyParams, PaymentVerifyParams, + SignedVoucher, State as PState, UpdateChannelStateParams, LANE_LIMIT, SETTLE_DELAY, + }, ACCOUNT_ACTOR_CODE_ID, INIT_ACTOR_ADDR, INIT_ACTOR_CODE_ID, MULTISIG_ACTOR_CODE_ID, PAYCH_ACTOR_CODE_ID, REWARD_ACTOR_ADDR, REWARD_ACTOR_CODE_ID, SYSTEM_ACTOR_ADDR, SYSTEM_ACTOR_CODE_ID, @@ -16,19 +19,21 @@ use common::*; use crypto::Signature; use db::MemoryDB; use derive_builder::Builder; +use interpreter::{internal_send, ChainRand, DefaultRuntime, DefaultSyscalls, GasSyscalls}; use ipld_blockstore::BlockStore; use message::UnsignedMessage; use num_bigint::{BigInt, Sign}; +use runtime::{ActorCode, Runtime, Syscalls}; use vm::{ExitCode, Serialized, TokenAmount, METHOD_CONSTRUCTOR}; -struct lane_params{ - epoch_num : ChainEpoch, - from : Address, - to : Address, - amt : i64, - lane : u64, - nonce : u64 +struct lane_params { + epoch_num: ChainEpoch, + from: Address, + to: Address, + amt: i64, + lane: u64, + nonce: u64, } #[test] @@ -43,7 +48,9 @@ fn create_paych_actor_test() { .to(paych_addr) .build() .unwrap(); - let mut rt = MockRuntime::new(&bs, message); + + let default_syscalls = DefaultSyscalls::new(&bs); + let mut rt = MockRuntime::new(&bs, &default_syscalls, message); rt.set_caller(INIT_ACTOR_CODE_ID.clone(), caller_addr); rt.actor_code_cids .insert(payer_addr, ACCOUNT_ACTOR_CODE_ID.clone()); @@ -63,7 +70,8 @@ fn actor_doesnt_exist_test() { .to(paych_addr) .build() .unwrap(); - let mut rt = MockRuntime::new(&bs, message); + let default_syscalls = DefaultSyscalls::new(&bs); + let mut rt = MockRuntime::new(&bs, &default_syscalls, message); rt.set_caller(INIT_ACTOR_CODE_ID.clone(), caller_addr); rt.actor_code_cids .insert(payer_addr, ACCOUNT_ACTOR_CODE_ID.clone()); @@ -112,7 +120,8 @@ fn actor_constructor_fails() { .to(paych_addr) .build() .unwrap(); - let mut rt = MockRuntime::new(&bs, message); + let default_syscalls = DefaultSyscalls::new(&bs); + let mut rt = MockRuntime::new(&bs, &default_syscalls, message); rt.set_caller(test_case.caller_code, caller_addr); rt.actor_code_cids .insert(test_case.paych_addr, test_case.new_actor_code); @@ -230,7 +239,8 @@ mod create_lane_tests { .to(paych_addr) .build() .unwrap(); - let mut rt = MockRuntime::new(&bs, message); + let default_syscalls = DefaultSyscalls::new(&bs); + let mut rt = MockRuntime::new(&bs, &default_syscalls, message); rt.epoch = test_case.epoch; rt.balance = TokenAmount::from(test_case.balance); rt.set_caller(INIT_ACTOR_CODE_ID.clone(), *INIT_ACTOR_ADDR); @@ -305,44 +315,52 @@ mod create_lane_tests { } #[test] -fn redeem_voucher_one_lane(){ +fn redeem_voucher_one_lane() { let bs = MemoryDB::default(); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1 ); + let default_syscalls = DefaultSyscalls::new(&bs); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, &default_syscalls, 1); let state: PState = rt.get_state().unwrap(); let new_voucher_amount = BigInt::from(9); sv.amount = new_voucher_amount; - let ucp = UpdateChannelStateParams{ - proof : vec![], - secret : vec![], - sv:sv.clone() + let ucp = UpdateChannelStateParams { + proof: vec![], + secret: vec![], + sv: sv.clone(), }; let payee_addr = Address::new_id(103); let payer_addr = Address::new_id(102); rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), payee_addr); - rt.expect_validate_caller_addr(&[state.from,state.to]); + rt.expect_validate_caller_addr(&[state.from, state.to]); rt.expect_verify_signature(sv.signature.unwrap(), payer_addr, None); - assert!(rt.call(&PAYCH_ACTOR_CODE_ID.clone(), Method::UpdateChannelState as u64 , &Serialized::serialize(ucp).unwrap()).is_ok()); + assert!(rt + .call( + &PAYCH_ACTOR_CODE_ID.clone(), + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap() + ) + .is_ok()); rt.verify(); let exp_ls = LaneState { id: 0, redeemed: BigInt::from(9), nonce: 1, }; - let exp_state = PState{ - from : state.from, - to : state.to, - to_send : TokenAmount::from(9 as u64), - settling_at : state.settling_at, - min_settle_height : state.min_settle_height, - lane_states : vec![exp_ls] - }; - verify_state(&mut rt, 1, exp_state); + let exp_state = PState { + from: state.from, + to: state.to, + to_send: TokenAmount::from(9 as u64), + settling_at: state.settling_at, + min_settle_height: state.min_settle_height, + lane_states: vec![exp_ls], + }; + verify_state(&mut rt, 1, exp_state); } #[test] -fn redeem_voucher_correct_lane(){ +fn redeem_voucher_correct_lane() { let bs = MemoryDB::default(); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1 ); + let default_syscalls = DefaultSyscalls::new(&bs); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, &default_syscalls, 3); let state: PState = rt.get_state().unwrap(); let initial_amount = state.to_send; sv.amount = BigInt::from(9); @@ -353,358 +371,479 @@ fn redeem_voucher_correct_lane(){ let payer_addr = Address::new_id(102); rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), payee_addr); - rt.expect_validate_caller_addr(&[state.from,state.to]); + rt.expect_validate_caller_addr(&[state.from, state.to]); rt.expect_verify_signature(sv.clone().signature.unwrap(), payer_addr, None); - let ucp = UpdateChannelStateParams{ - proof : vec![], - secret : vec![], - sv:sv.clone() + let ucp = UpdateChannelStateParams { + proof: vec![], + secret: vec![], + sv: sv.clone(), }; - assert!(rt.call(&PAYCH_ACTOR_CODE_ID.clone(), Method::UpdateChannelState as u64 , &Serialized::serialize(ucp).unwrap()).is_ok()); + assert!(rt + .call( + &PAYCH_ACTOR_CODE_ID.clone(), + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap() + ) + .is_ok()); rt.verify(); let state: PState = rt.get_state().unwrap(); let ls_updated: &LaneState = &state.lane_states[1]; - let big_delta = &sv.amount - BigInt::from_signed_bytes_be( &ls_to_update.redeemed.to_signed_bytes_be()); - - let exp_send = big_delta + BigInt::from_signed_bytes_be(&initial_amount.to_radix_be(10)); - assert_eq!(exp_send, BigInt::from_signed_bytes_be(&state.to_send.to_radix_be(10)) ); - assert_eq!(sv.amount, ls_updated.redeemed ); + let big_delta = + &sv.amount - BigInt::from_signed_bytes_be(&ls_to_update.redeemed.to_signed_bytes_be()); + + let exp_send = big_delta + BigInt::from_signed_bytes_be(&initial_amount.to_radix_be(10)); + assert_eq!( + exp_send, + BigInt::from_signed_bytes_be(&state.to_send.to_radix_be(10)) + ); + assert_eq!(sv.amount, ls_updated.redeemed); assert_eq!(sv.nonce, ls_updated.nonce); } #[test] -fn merge_success(){ +fn merge_success() { let num_lanes = 3; let bs = MemoryDB::default(); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, num_lanes ); + let default_syscalls = DefaultSyscalls::new(&bs); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, &default_syscalls, num_lanes); let mut state: PState = rt.get_state().unwrap(); let state_2: PState = rt.get_state().unwrap(); rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); - let merge_to : &LaneState= &state.lane_states[0]; - let merge_from : &LaneState= &state.lane_states[1]; + let merge_to: &LaneState = &state.lane_states[0]; + let merge_from: &LaneState = &state.lane_states[1]; sv.lane = merge_to.id; let merge_nonce = merge_to.nonce + 10; - let merges : Vec = vec![Merge{ - lane : merge_from.id, - nonce : merge_nonce + let merges: Vec = vec![Merge { + lane: merge_from.id, + nonce: merge_nonce, }]; sv.merges = merges; let payee_addr = Address::new_id(103); - rt.expect_validate_caller_addr(&[state.from,state.to]); + rt.expect_validate_caller_addr(&[state.from, state.to]); rt.expect_verify_signature(sv.clone().signature.unwrap(), payee_addr, None); - let ucp = UpdateChannelStateParams{ - proof : vec![], - secret : vec![], - sv:sv.clone() + let ucp = UpdateChannelStateParams { + proof: vec![], + secret: vec![], + sv: sv.clone(), }; - assert!(rt.call(&PAYCH_ACTOR_CODE_ID.clone(), Method::UpdateChannelState as u64 , &Serialized::serialize(ucp).unwrap()).is_ok()); + assert!(rt + .call( + &PAYCH_ACTOR_CODE_ID.clone(), + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap() + ) + .is_ok()); rt.verify(); let (sv_amount_sign, sv_amount_bytes) = &sv.amount.to_bytes_be(); - let exp_merge_to = LaneState{ - id : merge_to.id, - redeemed : BigInt::from_bytes_be(*sv_amount_sign, &sv_amount_bytes), - nonce : sv.nonce + let exp_merge_to = LaneState { + id: merge_to.id, + redeemed: BigInt::from_bytes_be(*sv_amount_sign, &sv_amount_bytes), + nonce: sv.nonce, }; let (from_sign, from_bytes) = &merge_from.redeemed.to_bytes_be(); - let exp_merge_from = LaneState{ - id : merge_from.id, - redeemed : BigInt::from_bytes_be(*from_sign, &from_bytes), - nonce : merge_nonce + let exp_merge_from = LaneState { + id: merge_from.id, + redeemed: BigInt::from_bytes_be(*from_sign, &from_bytes), + nonce: merge_nonce, }; let (to_sign, to_bytes) = &merge_to.redeemed.to_bytes_be(); - let redeemed = BigInt::from_bytes_be(*from_sign, &from_bytes) + BigInt::from_bytes_be(*to_sign, &to_bytes); + let redeemed = + BigInt::from_bytes_be(*from_sign, &from_bytes) + BigInt::from_bytes_be(*to_sign, &to_bytes); let exp_delta = sv.amount - redeemed; let exp_send_amt = BigInt::from_bytes_be(Sign::Plus, &state.to_send.to_bytes_be()) + exp_delta; let mut exp_state = state_2; exp_state.to_send = TokenAmount::from_bytes_be(&exp_send_amt.to_signed_bytes_be()); - exp_state.lane_states = vec![exp_merge_to, exp_merge_from, exp_state.lane_states.pop().unwrap()]; + exp_state.lane_states = vec![ + exp_merge_to, + exp_merge_from, + exp_state.lane_states.pop().unwrap(), + ]; verify_state(&mut rt, num_lanes as i64, exp_state); - } #[test] -fn merge_failue(){ - let lane_vec = vec![1,1,1,0]; - let voucher_vec= vec![10,0,10,10]; - let balance_vec= vec![0,0,1,0]; - let merge_vec= vec![1,10,10,10]; - let exit_vec= vec![ExitCode::ErrIllegalArgument, ExitCode::ErrIllegalArgument, ExitCode::ErrIllegalState, ExitCode::ErrIllegalArgument]; +fn merge_failue() { + let lane_vec = vec![1, 1, 1, 0]; + let voucher_vec = vec![10, 0, 10, 10]; + let balance_vec = vec![0, 0, 1, 0]; + let merge_vec = vec![1, 10, 10, 10]; + let exit_vec = vec![ + ExitCode::ErrIllegalArgument, + ExitCode::ErrIllegalArgument, + ExitCode::ErrIllegalState, + ExitCode::ErrIllegalArgument, + ]; let num_test_cases = lane_vec.len(); let payee_addr = Address::new_id(103); - for i in 0.. num_test_cases{ + for i in 0..num_test_cases { let bs = MemoryDB::default(); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 2); + let default_syscalls = DefaultSyscalls::new(&bs); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, &default_syscalls, 2); rt.balance = TokenAmount::from(balance_vec[i] as u64); let state: PState = rt.get_state().unwrap(); - let merge_to : &LaneState= &state.lane_states[0]; - let merge_from : &LaneState= &state.lane_states[1]; + let merge_to: &LaneState = &state.lane_states[0]; + let merge_from: &LaneState = &state.lane_states[1]; sv.lane = merge_to.id; sv.nonce = voucher_vec[i]; - sv.merges = vec![Merge{ - lane : merge_from.id, - nonce : merge_vec[i] + sv.merges = vec![Merge { + lane: merge_from.id, + nonce: merge_vec[i], }]; - let ucp = UpdateChannelStateParams{ - proof : vec![], - secret : vec![], - sv:sv.clone() + let ucp = UpdateChannelStateParams { + proof: vec![], + secret: vec![], + sv: sv.clone(), }; rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); - rt.expect_validate_caller_addr(&[state.from,state.to]); + rt.expect_validate_caller_addr(&[state.from, state.to]); rt.expect_verify_signature(sv.clone().signature.unwrap(), payee_addr, None); - let v = rt.call(&PAYCH_ACTOR_CODE_ID.clone(), Method::UpdateChannelState as u64 , &Serialized::serialize(ucp).unwrap()).unwrap_err(); + let v = rt + .call( + &PAYCH_ACTOR_CODE_ID.clone(), + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap(), + ) + .unwrap_err(); assert_eq!(v.exit_code(), exit_vec[i]); rt.verify(); } - } #[test] -fn invalid_merge_lane_999(){ +fn invalid_merge_lane_999() { let bs = MemoryDB::default(); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 2); + let default_syscalls = DefaultSyscalls::new(&bs); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, &default_syscalls, 2); let state: PState = rt.get_state().unwrap(); let payee_addr = Address::new_id(103); - let merge_to : &LaneState= &state.lane_states[0]; - let merge_from = LaneState{ - id : 999, - nonce : sv.nonce, - redeemed : BigInt::from(0) + let merge_to: &LaneState = &state.lane_states[0]; + let merge_from = LaneState { + id: 999, + nonce: sv.nonce, + redeemed: BigInt::from(0), }; sv.lane = merge_to.id; sv.nonce = 10; - sv.merges = vec![Merge{ - lane : merge_from.id, - nonce : sv.nonce + sv.merges = vec![Merge { + lane: merge_from.id, + nonce: sv.nonce, }]; - let ucp = UpdateChannelStateParams{ - proof : vec![], - secret : vec![], - sv:sv.clone() + let ucp = UpdateChannelStateParams { + proof: vec![], + secret: vec![], + sv: sv.clone(), }; rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); - rt.expect_validate_caller_addr(&[state.from,state.to]); + rt.expect_validate_caller_addr(&[state.from, state.to]); rt.expect_verify_signature(sv.clone().signature.unwrap(), payee_addr, None); - let v = rt.call(&PAYCH_ACTOR_CODE_ID.clone(), Method::UpdateChannelState as u64 , &Serialized::serialize(ucp).unwrap()).unwrap_err(); + let v = rt + .call( + &PAYCH_ACTOR_CODE_ID.clone(), + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap(), + ) + .unwrap_err(); assert_eq!(v.exit_code(), ExitCode::ErrIllegalArgument); rt.verify(); } #[test] -fn lane_limit_exceeded(){ +fn lane_limit_exceeded() { let bs = MemoryDB::default(); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, LANE_LIMIT as u64); + let default_syscalls = DefaultSyscalls::new(&bs); + let (mut rt, mut sv) = + require_create_cannel_with_lanes(&bs, &default_syscalls, LANE_LIMIT as u64); let state: PState = rt.get_state().unwrap(); let payee_addr = Address::new_id(103); sv.lane += 1; sv.nonce += 1; sv.amount = BigInt::from(100); - let ucp = UpdateChannelStateParams{ - proof : vec![], - secret : vec![], - sv:sv.clone() + let ucp = UpdateChannelStateParams { + proof: vec![], + secret: vec![], + sv: sv.clone(), }; rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); - rt.expect_validate_caller_addr(&[state.from,state.to]); + rt.expect_validate_caller_addr(&[state.from, state.to]); rt.expect_verify_signature(sv.clone().signature.unwrap(), payee_addr, None); - let v = rt.call(&PAYCH_ACTOR_CODE_ID.clone(), Method::UpdateChannelState as u64 , &Serialized::serialize(ucp).unwrap()).unwrap_err(); + let v = rt + .call( + &PAYCH_ACTOR_CODE_ID.clone(), + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap(), + ) + .unwrap_err(); assert_eq!(v.exit_code(), ExitCode::ErrIllegalArgument); rt.verify(); } #[test] -fn extra_call_succeed(){ +fn extra_call_succeed() { let bs = MemoryDB::default(); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); + let default_syscalls = DefaultSyscalls::new(&bs); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, &default_syscalls, 1); let state: PState = rt.get_state().unwrap(); let other_addr = Address::new_id(104); rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); - sv.extra = Some(ModVerifyParams{ - actor : other_addr, - method : Method::UpdateChannelState as u64, - data : Serialized::serialize([1,2,3,4]).unwrap() + sv.extra = Some(ModVerifyParams { + actor: other_addr, + method: Method::UpdateChannelState as u64, + data: Serialized::serialize([1, 2, 3, 4]).unwrap(), }); - rt.expect_validate_caller_addr(&[state.from,state.to]); + rt.expect_validate_caller_addr(&[state.from, state.to]); rt.expect_verify_signature(sv.clone().signature.unwrap(), state.to, None); - let exp_send_params = PaymentVerifyParams{ - extra : Serialized::serialize( vec![1,2,3,4]).unwrap(), - proof : vec![] + let exp_send_params = PaymentVerifyParams { + extra: Serialized::serialize(vec![1, 2, 3, 4]).unwrap(), + proof: vec![], }; - rt.expect_send(other_addr, Method::UpdateChannelState as u64, Serialized::serialize(exp_send_params).unwrap(), TokenAmount::from(0u8), Serialized::default(), ExitCode::Ok); - let ucp = UpdateChannelStateParams{ - proof : vec![], - secret : vec![], - sv:sv.clone() + rt.expect_send( + other_addr, + Method::UpdateChannelState as u64, + Serialized::serialize(exp_send_params).unwrap(), + TokenAmount::from(0u8), + Serialized::default(), + ExitCode::Ok, + ); + let ucp = UpdateChannelStateParams { + proof: vec![], + secret: vec![], + sv: sv.clone(), }; - assert!(rt.call(&PAYCH_ACTOR_CODE_ID.clone(), Method::UpdateChannelState as u64 , &Serialized::serialize(ucp).unwrap()).is_ok()); + assert!(rt + .call( + &PAYCH_ACTOR_CODE_ID.clone(), + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap() + ) + .is_ok()); rt.verify(); } #[test] -fn extra_call_fail(){ +fn extra_call_fail() { let bs = MemoryDB::default(); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); + let default_syscalls = DefaultSyscalls::new(&bs); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, &default_syscalls, 1); let state: PState = rt.get_state().unwrap(); let other_addr = Address::new_id(104); rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); - sv.extra = Some(ModVerifyParams{ - actor : other_addr, - method : Method::UpdateChannelState as u64, - data : Serialized::serialize([1,2,3,4]).unwrap() + sv.extra = Some(ModVerifyParams { + actor: other_addr, + method: Method::UpdateChannelState as u64, + data: Serialized::serialize([1, 2, 3, 4]).unwrap(), }); - rt.expect_validate_caller_addr(&[state.from,state.to]); - let exp_send_params = PaymentVerifyParams{ - extra : Serialized::serialize( vec![1,2,3,4]).unwrap(), - proof : vec![] + rt.expect_validate_caller_addr(&[state.from, state.to]); + let exp_send_params = PaymentVerifyParams { + extra: Serialized::serialize(vec![1, 2, 3, 4]).unwrap(), + proof: vec![], }; - rt.expect_send(other_addr, Method::UpdateChannelState as u64, Serialized::serialize(exp_send_params).unwrap(), TokenAmount::from(0u8), Serialized::default(), ExitCode::ErrPlaceholder); + rt.expect_send( + other_addr, + Method::UpdateChannelState as u64, + Serialized::serialize(exp_send_params).unwrap(), + TokenAmount::from(0u8), + Serialized::default(), + ExitCode::ErrPlaceholder, + ); rt.expect_verify_signature(sv.clone().signature.unwrap(), state.to, None); - let ucp = UpdateChannelStateParams{ - proof : vec![], - secret : vec![], - sv:sv.clone() + let ucp = UpdateChannelStateParams { + proof: vec![], + secret: vec![], + sv: sv.clone(), }; - let v = rt.call(&PAYCH_ACTOR_CODE_ID.clone(), Method::UpdateChannelState as u64 , &Serialized::serialize(ucp).unwrap()).unwrap_err(); + let v = rt + .call( + &PAYCH_ACTOR_CODE_ID.clone(), + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap(), + ) + .unwrap_err(); assert_eq!(v.exit_code(), ExitCode::ErrPlaceholder); rt.verify(); } #[test] -fn update_channel_setting(){ +fn update_channel_setting() { let bs = MemoryDB::default(); - let (mut rt, sv) = require_create_cannel_with_lanes(&bs, 1); + let default_syscalls = DefaultSyscalls::new(&bs); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, &default_syscalls, 1); rt.epoch = 10; let state: PState = rt.get_state().unwrap(); rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); - rt.expect_validate_caller_addr(&[state.from,state.to]); - assert!(rt.call(&PAYCH_ACTOR_CODE_ID.clone(),Method::Settle as u64 , &Serialized::default()).is_ok()); + rt.expect_validate_caller_addr(&[state.from, state.to]); + assert!(rt + .call( + &PAYCH_ACTOR_CODE_ID.clone(), + Method::Settle as u64, + &Serialized::default() + ) + .is_ok()); let exp_settling_at = SETTLE_DELAY + 10; let state: PState = rt.get_state().unwrap(); assert_eq!(exp_settling_at, state.settling_at); - assert_eq!(state.min_settle_height,0); + assert_eq!(state.min_settle_height, 0); - let min_settle_vec = vec![0,2,12]; - let exp_min_settle_height = vec![state.min_settle_height ,2,12]; - let exp_settling_at = vec![state.settling_at,state.settling_at,12]; + let min_settle_vec = vec![0, 2, 12]; + let exp_min_settle_height = vec![state.min_settle_height, 2, 12]; + let exp_settling_at = vec![state.settling_at, state.settling_at, 12]; let num_test_cases = min_settle_vec.len(); - for i in 0 .. num_test_cases{ - let mut ucp = UpdateChannelStateParams{ - proof : vec![], - secret : vec![], - sv:sv.clone() + for i in 0..num_test_cases { + let mut ucp = UpdateChannelStateParams { + proof: vec![], + secret: vec![], + sv: sv.clone(), }; ucp.sv.min_settle_height = min_settle_vec[i]; - rt.expect_validate_caller_addr(&[state.from,state.to]); + rt.expect_validate_caller_addr(&[state.from, state.to]); rt.expect_verify_signature(ucp.sv.clone().signature.unwrap(), state.to, None); - assert!(rt.call(&PAYCH_ACTOR_CODE_ID.clone(),Method::UpdateChannelState as u64 , &Serialized::serialize(ucp).unwrap()).is_ok()); + assert!(rt + .call( + &PAYCH_ACTOR_CODE_ID.clone(), + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap() + ) + .is_ok()); let new_state: PState = rt.get_state().unwrap(); - assert_eq!(exp_settling_at[i],new_state.settling_at); - assert_eq!(exp_min_settle_height[i],new_state.min_settle_height); + assert_eq!(exp_settling_at[i], new_state.settling_at); + assert_eq!(exp_min_settle_height[i], new_state.min_settle_height); } } #[test] -fn succeed_correct_secret(){ +fn succeed_correct_secret() { let bs = MemoryDB::default(); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); + let default_syscalls = DefaultSyscalls::new(&bs); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, &default_syscalls, 1); let state: PState = rt.get_state().unwrap(); - let mut ucp = UpdateChannelStateParams{ - proof : vec![], - secret : vec![], - sv:sv.clone() + let mut ucp = UpdateChannelStateParams { + proof: vec![], + secret: vec![], + sv: sv.clone(), }; - rt.expect_validate_caller_addr(&[state.from,state.to]); + rt.expect_validate_caller_addr(&[state.from, state.to]); rt.expect_verify_signature(ucp.sv.clone().signature.unwrap(), state.to, None); - assert!(rt.call(&PAYCH_ACTOR_CODE_ID.clone(),Method::UpdateChannelState as u64 , &Serialized::serialize(ucp).unwrap()).is_ok()); + assert!(rt + .call( + &PAYCH_ACTOR_CODE_ID.clone(), + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap() + ) + .is_ok()); rt.verify(); } #[test] -fn incorrect_secret(){ +fn incorrect_secret() { let bs = MemoryDB::default(); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); + let default_syscalls = DefaultSyscalls::new(&bs); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, &default_syscalls, 1); let state: PState = rt.get_state().unwrap(); - let mut ucp = UpdateChannelStateParams{ - proof : vec![], - secret : vec![], - sv:sv.clone() + let mut ucp = UpdateChannelStateParams { + proof: vec![], + secret: vec![], + sv: sv.clone(), }; - rt.expect_validate_caller_addr(&[state.from,state.to]); + rt.expect_validate_caller_addr(&[state.from, state.to]); rt.expect_verify_signature(ucp.sv.clone().signature.unwrap(), state.to, None); - let v = rt.call(&PAYCH_ACTOR_CODE_ID.clone(),Method::UpdateChannelState as u64 , &Serialized::serialize(ucp).unwrap()).unwrap_err(); - assert_eq!(v.exit_code(), ExitCode::ErrIllegalArgument); + let v = rt + .call( + &PAYCH_ACTOR_CODE_ID.clone(), + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap(), + ) + .unwrap_err(); + assert_eq!(v.exit_code(), ExitCode::ErrIllegalArgument); rt.verify(); } #[test] -fn adjust_settling_at(){ +fn adjust_settling_at() { let bs = MemoryDB::default(); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); + let default_syscalls = DefaultSyscalls::new(&bs); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, &default_syscalls, 1); let ep = 10; rt.epoch = ep; let mut state: PState = rt.get_state().unwrap(); rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); - rt.expect_validate_caller_addr(&[state.from,state.to]); - assert!(rt.call(&PAYCH_ACTOR_CODE_ID.clone(),Method::Settle as u64 , &Serialized::default()).is_ok()); + rt.expect_validate_caller_addr(&[state.from, state.to]); + assert!(rt + .call( + &PAYCH_ACTOR_CODE_ID.clone(), + Method::Settle as u64, + &Serialized::default() + ) + .is_ok()); let exp_settling_at = ep + SETTLE_DELAY; state = rt.get_state().unwrap(); - assert_eq!(state.settling_at,exp_settling_at); + assert_eq!(state.settling_at, exp_settling_at); assert_eq!(state.min_settle_height, 0); } #[test] -fn settle_if_height_less(){ +fn settle_if_height_less() { let bs = MemoryDB::default(); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); + let default_syscalls = DefaultSyscalls::new(&bs); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, &default_syscalls, 1); let ep = 10; rt.epoch = ep; let mut state: PState = rt.get_state().unwrap(); sv.min_settle_height = (ep + SETTLE_DELAY) + 1; - let mut ucp = UpdateChannelStateParams{ - proof : vec![], - secret : vec![], - sv:sv.clone() + let mut ucp = UpdateChannelStateParams { + proof: vec![], + secret: vec![], + sv: sv.clone(), }; - rt.expect_validate_caller_addr(&[state.from,state.to]); + rt.expect_validate_caller_addr(&[state.from, state.to]); rt.expect_verify_signature(ucp.sv.clone().signature.unwrap(), state.to, None); - assert!(rt.call(&PAYCH_ACTOR_CODE_ID.clone(),Method::UpdateChannelState as u64 , &Serialized::default()).is_ok()); + assert!(rt + .call( + &PAYCH_ACTOR_CODE_ID.clone(), + Method::UpdateChannelState as u64, + &Serialized::default() + ) + .is_ok()); state = rt.get_state().unwrap(); - assert_eq!(state.settling_at,0); - assert_eq!(state.min_settle_height,ucp.sv.min_settle_height); + assert_eq!(state.settling_at, 0); + assert_eq!(state.min_settle_height, ucp.sv.min_settle_height); rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); - rt.expect_validate_caller_addr(&[state.from,state.to]); - assert!(rt.call(&PAYCH_ACTOR_CODE_ID.clone(),Method::Settle as u64 , &Serialized::default()).is_ok()); + rt.expect_validate_caller_addr(&[state.from, state.to]); + assert!(rt + .call( + &PAYCH_ACTOR_CODE_ID.clone(), + Method::Settle as u64, + &Serialized::default() + ) + .is_ok()); state = rt.get_state().unwrap(); assert_eq!(state.settling_at, ucp.sv.min_settle_height); } - -fn require_create_cannel_with_lanes( - bs: &BS, - num_lanes : u64 -) -> (MockRuntime< BS>,SignedVoucher) { - +fn require_create_cannel_with_lanes<'a, 'sys, BS: BlockStore, SYS: Syscalls>( + bs: &'a BS, + default_syscalls: &'sys SYS, + num_lanes: u64, +) -> (MockRuntime<'a, 'sys, BS, SYS>, SignedVoucher) { let paych_addr = Address::new_id(100); let payer_addr = Address::new_id(102); let payee_addr = Address::new_id(103); @@ -716,43 +855,46 @@ fn require_create_cannel_with_lanes( let message = UnsignedMessage::builder() .from(*SYSTEM_ACTOR_ADDR) .to(paych_addr) + .gas_limit(1000) .build() .unwrap(); - let mut rt = MockRuntime::new(bs, message); - rt.epoch = curr_epoch; - rt.balance = TokenAmount::from(balance); - rt.received = TokenAmount::from(recieved); - rt.set_caller(INIT_ACTOR_CODE_ID.clone(), *INIT_ACTOR_ADDR); - rt.actor_code_cids - .insert(payee_addr, ACCOUNT_ACTOR_CODE_ID.clone()); - rt.actor_code_cids - .insert(payer_addr, ACCOUNT_ACTOR_CODE_ID.clone()); - construct_and_verify( &mut rt, payer_addr, payee_addr); - - let mut last_sv = SignedVoucher::default(); - for i in 0..num_lanes{ - - let lane_param = lane_params{ - epoch_num : curr_epoch, - from : payer_addr, - to : payee_addr, - amt : (i + 1) as i64, - lane : i, - nonce : i+1 - }; + //let default_syscalls = DefaultSyscalls::new(&bs); + let mut rt = MockRuntime::new(bs, default_syscalls, message); + rt.epoch = curr_epoch; + rt.balance = TokenAmount::from(balance); + rt.received = TokenAmount::from(recieved); + rt.set_caller(INIT_ACTOR_CODE_ID.clone(), *INIT_ACTOR_ADDR); + rt.actor_code_cids + .insert(payee_addr, ACCOUNT_ACTOR_CODE_ID.clone()); + rt.actor_code_cids + .insert(payer_addr, ACCOUNT_ACTOR_CODE_ID.clone()); + construct_and_verify(&mut rt, payer_addr, payee_addr); + + let mut last_sv = SignedVoucher::default(); + for i in 0..num_lanes { + let lane_param = lane_params { + epoch_num: curr_epoch, + from: payer_addr, + to: payee_addr, + amt: (i + 1) as i64, + lane: i, + nonce: i + 1, + }; - last_sv = require_add_new_lane(&mut rt, lane_param); - } - - (rt,last_sv) + last_sv = require_add_new_lane(&mut rt, lane_param); + } + + (rt, last_sv) } -fn require_add_new_lane -(rt: &mut MockRuntime<'_, BS>, param : lane_params) -> SignedVoucher{ +fn require_add_new_lane( + rt: &mut MockRuntime<'_, '_, BS, SYS>, + param: lane_params, +) -> SignedVoucher { let payee_addr = Address::new_id(103); - let sig = Signature::new_bls(vec![0,1,2,3,4,5,67,]); - let sv = SignedVoucher{ + let sig = Signature::new_bls(vec![0, 1, 2, 3, 4, 5, 67]); + let sv = SignedVoucher { time_lock_min: param.epoch_num, time_lock_max: u64::MAX, secret_pre_image: vec![], @@ -767,16 +909,22 @@ fn require_add_new_lane rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), param.from); rt.expect_validate_caller_addr(&[param.from, param.to]); rt.expect_verify_signature(sig.clone(), payee_addr, None); - let ucp = UpdateChannelStateParams{ - proof : vec![], - secret : vec![], - sv : sv + let ucp = UpdateChannelStateParams { + proof: vec![], + secret: vec![], + sv: sv, }; - assert!(rt.call(&PAYCH_ACTOR_CODE_ID.clone(), Method::UpdateChannelState as u64, &Serialized::serialize(ucp).unwrap()).is_ok()); + assert!(rt + .call( + &PAYCH_ACTOR_CODE_ID.clone(), + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap() + ) + .is_ok()); rt.verify(); - SignedVoucher{ + SignedVoucher { time_lock_min: param.epoch_num, time_lock_max: u64::MAX, secret_pre_image: vec![], @@ -790,8 +938,8 @@ fn require_add_new_lane } } -fn construct_and_verify( - rt: &mut MockRuntime<'_, BS>, +fn construct_and_verify( + rt: &mut MockRuntime<'_, '_, BS, SYS>, sender: Address, receiver: Address, ) { @@ -811,8 +959,8 @@ fn construct_and_verify( verify_initial_state(rt, sender, receiver); } -fn verify_initial_state( - rt: &mut MockRuntime<'_, BS>, +fn verify_initial_state( + rt: &mut MockRuntime<'_, '_, BS, SYS>, sender: Address, receiver: Address, ) { @@ -826,10 +974,11 @@ fn verify_initial_state( assert_eq!(state.lane_states.len(), 0); } -fn verify_state( - rt: &mut MockRuntime<'_, BS>, - exp_lanes : i64, - expected_state : PState){ +fn verify_state( + rt: &mut MockRuntime<'_, '_, BS, SYS>, + exp_lanes: i64, + expected_state: PState, +) { let state: PState = rt.get_state().unwrap(); assert_eq!(expected_state.to, state.to); assert_eq!(expected_state.from, state.from); @@ -838,11 +987,9 @@ fn verify_state( assert_eq!(expected_state.to_send, state.to_send); if exp_lanes > 0 { - assert_eq!(exp_lanes as u64 , state.lane_states.len() as u64 ); + assert_eq!(exp_lanes as u64, state.lane_states.len() as u64); assert_eq!(expected_state.lane_states, state.lane_states); - } - else { + } else { assert_eq!(state.lane_states.len(), 0); } - - } +} diff --git a/vm/actor/tests/reward_actor_test.rs b/vm/actor/tests/reward_actor_test.rs index ca051fdc7c40..47a5cce08111 100644 --- a/vm/actor/tests/reward_actor_test.rs +++ b/vm/actor/tests/reward_actor_test.rs @@ -13,14 +13,17 @@ use db::MemoryDB; use ipld_blockstore::BlockStore; use message::UnsignedMessage; use vm::{ExitCode, Serialized, TokenAmount, METHOD_CONSTRUCTOR}; +use runtime::{ Syscalls}; +use interpreter::{ DefaultSyscalls,}; -fn construct_runtime(bs: &BS) -> MockRuntime<'_, BS> { +fn construct_runtime<'a,'sys,BS: BlockStore, SYS: Syscalls>(bs: &'a BS, default_syscalls : &'sys SYS) -> MockRuntime<'a, 'sys, BS, SYS> { let message = UnsignedMessage::builder() .to(*REWARD_ACTOR_ADDR) .from(*SYSTEM_ACTOR_ADDR) .build() .unwrap(); - let mut rt = MockRuntime::new(bs, message); + + let mut rt = MockRuntime::new(bs, default_syscalls, message); rt.caller_type = SYSTEM_ACTOR_CODE_ID.clone(); return rt; } @@ -28,7 +31,8 @@ fn construct_runtime(bs: &BS) -> MockRuntime<'_, BS> { #[test] fn balance_less_than_reward() { let bs = MemoryDB::default(); - let mut rt = construct_runtime(&bs); + let default_syscalls = DefaultSyscalls::new(&bs); + let mut rt = construct_runtime(&bs, & default_syscalls); construct_and_verify(&mut rt); let miner = Address::new_id(1000); @@ -58,7 +62,7 @@ fn balance_less_than_reward() { rt.verify() } -fn construct_and_verify(rt: &mut MockRuntime<'_, BS>) { +fn construct_and_verify<'a,'sys,BS: BlockStore, SYS: Syscalls>(rt: &mut MockRuntime<'a, 'sys, BS, SYS>) { rt.expect_validate_caller_addr(&[SYSTEM_ACTOR_ADDR.clone()]); let ret = rt .call( diff --git a/vm/interpreter/src/gas_syscalls.rs b/vm/interpreter/src/gas_syscalls.rs index 95dfac0123e2..6f1cbb39db7a 100644 --- a/vm/interpreter/src/gas_syscalls.rs +++ b/vm/interpreter/src/gas_syscalls.rs @@ -13,7 +13,7 @@ use std::error::Error as StdError; use std::rc::Rc; /// Syscall wrapper to charge gas on syscalls -pub(crate) struct GasSyscalls<'sys, S> { +pub struct GasSyscalls<'sys, S> { pub price_list: PriceList, pub gas: Rc>, pub syscalls: &'sys S, diff --git a/vm/interpreter/src/lib.rs b/vm/interpreter/src/lib.rs index 3ef6a2ea2833..7333384ff1e4 100644 --- a/vm/interpreter/src/lib.rs +++ b/vm/interpreter/src/lib.rs @@ -4,11 +4,13 @@ mod default_runtime; mod default_syscalls; mod gas_block_store; -mod gas_syscalls; -mod gas_tracker; +pub mod gas_syscalls; +pub mod gas_tracker; mod rand; mod vm; pub use self::default_runtime::*; pub use self::default_syscalls::DefaultSyscalls; +pub use self::gas_syscalls::*; +pub use self::gas_tracker::*; pub use self::rand::*; pub use self::vm::*; From 3a86f47fd657c7369023978140e109b397f06f7d Mon Sep 17 00:00:00 2001 From: DragonMural Date: Sun, 7 Jun 2020 16:43:09 -0400 Subject: [PATCH 05/29] Adding fmt chaanges --- vm/actor/tests/account_actor_test.rs | 2 +- vm/actor/tests/cron_actor_test.rs | 15 ++++++++++----- vm/actor/tests/init_actor_test.rs | 24 ++++++++++++++---------- vm/actor/tests/reward_actor_test.rs | 17 +++++++++++------ 4 files changed, 36 insertions(+), 22 deletions(-) diff --git a/vm/actor/tests/account_actor_test.rs b/vm/actor/tests/account_actor_test.rs index a4bab18915d8..bb291069ac02 100644 --- a/vm/actor/tests/account_actor_test.rs +++ b/vm/actor/tests/account_actor_test.rs @@ -10,7 +10,7 @@ use db::MemoryDB; use message::UnsignedMessage; use vm::{ExitCode, Serialized}; -use interpreter::{ DefaultSyscalls,}; +use interpreter::DefaultSyscalls; macro_rules! account_tests { ($($name:ident: $value:expr,)*) => { diff --git a/vm/actor/tests/cron_actor_test.rs b/vm/actor/tests/cron_actor_test.rs index 03b9dbcda617..18bd3d1b069b 100644 --- a/vm/actor/tests/cron_actor_test.rs +++ b/vm/actor/tests/cron_actor_test.rs @@ -9,14 +9,16 @@ use actor::{ use address::Address; use common::*; use db::MemoryDB; +use interpreter::DefaultSyscalls; use ipld_blockstore::BlockStore; use message::UnsignedMessage; +use runtime::Syscalls; use vm::{ExitCode, Serialized}; -use runtime::{ Syscalls}; -use interpreter::{DefaultSyscalls}; - -fn construct_runtime<'a,'sys,BS: BlockStore, SYS: Syscalls>(bs: &'a BS, default_calls : &'sys SYS) -> MockRuntime<'a, 'sys, BS, SYS> { +fn construct_runtime<'a, 'sys, BS: BlockStore, SYS: Syscalls>( + bs: &'a BS, + default_calls: &'sys SYS, +) -> MockRuntime<'a, 'sys, BS, SYS> { let receiver = Address::new_id(100); let message = UnsignedMessage::builder() @@ -156,7 +158,10 @@ fn epoch_tick_with_entries() { epoch_tick_and_verify(&mut rt); } -fn construct_and_verify(rt: &mut MockRuntime<'_,'_, BS, SYS>, params: &ConstructorParams) { +fn construct_and_verify( + rt: &mut MockRuntime<'_, '_, BS, SYS>, + params: &ConstructorParams, +) { rt.expect_validate_caller_addr(&[*SYSTEM_ACTOR_ADDR]); let ret = rt .call( diff --git a/vm/actor/tests/init_actor_test.rs b/vm/actor/tests/init_actor_test.rs index 62e1fc47e57e..3db94ae085de 100644 --- a/vm/actor/tests/init_actor_test.rs +++ b/vm/actor/tests/init_actor_test.rs @@ -13,22 +13,24 @@ use address::Address; use cid::Cid; use common::*; use db::MemoryDB; +use interpreter::DefaultSyscalls; use ipld_blockstore::BlockStore; use message::{Message, UnsignedMessage}; +use runtime::Syscalls; use serde::Serialize; use vm::{ActorError, ExitCode, Serialized, TokenAmount, METHOD_CONSTRUCTOR}; -use runtime::{Syscalls}; -use interpreter::{DefaultSyscalls}; - -fn construct_runtime<'a,'sys,BS: BlockStore, SYS: Syscalls>(bs: &'a BS, default_syscalls: &'sys SYS) -> MockRuntime<'a, 'sys, BS, SYS> { +fn construct_runtime<'a, 'sys, BS: BlockStore, SYS: Syscalls>( + bs: &'a BS, + default_syscalls: &'sys SYS, +) -> MockRuntime<'a, 'sys, BS, SYS> { let receiver = Address::new_id(1000); let message = UnsignedMessage::builder() .to(receiver.clone()) .from(SYSTEM_ACTOR_ADDR.clone()) .build() .unwrap(); - let mut rt = MockRuntime::new(bs, default_syscalls,message); + let mut rt = MockRuntime::new(bs, default_syscalls, message); rt.caller_type = SYSTEM_ACTOR_CODE_ID.clone(); return rt; } @@ -53,7 +55,7 @@ fn abort_cant_call_exec() { fn create_2_payment_channels() { let bs = MemoryDB::default(); let default_syscalls = DefaultSyscalls::new(&bs); - let mut rt= construct_runtime(&bs, & default_syscalls); + let mut rt = construct_runtime(&bs, &default_syscalls); construct_and_verify(&mut rt); let anne = Address::new_id(1001); @@ -118,7 +120,7 @@ fn create_2_payment_channels() { fn create_storage_miner() { let bs = MemoryDB::default(); let default_syscalls = DefaultSyscalls::new(&bs); - let mut rt= construct_runtime(&bs, &default_syscalls); + let mut rt = construct_runtime(&bs, &default_syscalls); construct_and_verify(&mut rt); // only the storage power actor can create a miner @@ -170,7 +172,7 @@ fn create_storage_miner() { fn create_multisig_actor() { let bs = MemoryDB::default(); let default_syscalls = DefaultSyscalls::new(&bs); - let mut rt = construct_runtime(&bs, & default_syscalls); + let mut rt = construct_runtime(&bs, &default_syscalls); construct_and_verify(&mut rt); // Actor creating multisig actor @@ -267,7 +269,9 @@ fn sending_constructor_failure() { ); } -fn construct_and_verify<'a,'sys,BS: BlockStore, SYS: Syscalls>(rt: &mut MockRuntime<'a, 'sys, BS, SYS>) { +fn construct_and_verify<'a, 'sys, BS: BlockStore, SYS: Syscalls>( + rt: &mut MockRuntime<'a, 'sys, BS, SYS>, +) { rt.expect_validate_caller_addr(&[SYSTEM_ACTOR_ADDR.clone()]); let params = ConstructorParams { network_name: "mock".to_string(), @@ -296,7 +300,7 @@ fn construct_and_verify<'a,'sys,BS: BlockStore, SYS: Syscalls>(rt: &mut MockRunt } fn exec_and_verify<'a, 'sys, BS: BlockStore, SYS: Syscalls, S: Serialize>( - rt: &mut MockRuntime<'a, 'sys, BS, SYS>, + rt: &mut MockRuntime<'a, 'sys, BS, SYS>, code_id: Cid, params: &S, ) -> Result diff --git a/vm/actor/tests/reward_actor_test.rs b/vm/actor/tests/reward_actor_test.rs index 47a5cce08111..70f376e64868 100644 --- a/vm/actor/tests/reward_actor_test.rs +++ b/vm/actor/tests/reward_actor_test.rs @@ -10,19 +10,22 @@ use actor::{ use address::Address; use common::*; use db::MemoryDB; +use interpreter::DefaultSyscalls; use ipld_blockstore::BlockStore; use message::UnsignedMessage; +use runtime::Syscalls; use vm::{ExitCode, Serialized, TokenAmount, METHOD_CONSTRUCTOR}; -use runtime::{ Syscalls}; -use interpreter::{ DefaultSyscalls,}; -fn construct_runtime<'a,'sys,BS: BlockStore, SYS: Syscalls>(bs: &'a BS, default_syscalls : &'sys SYS) -> MockRuntime<'a, 'sys, BS, SYS> { +fn construct_runtime<'a, 'sys, BS: BlockStore, SYS: Syscalls>( + bs: &'a BS, + default_syscalls: &'sys SYS, +) -> MockRuntime<'a, 'sys, BS, SYS> { let message = UnsignedMessage::builder() .to(*REWARD_ACTOR_ADDR) .from(*SYSTEM_ACTOR_ADDR) .build() .unwrap(); - + let mut rt = MockRuntime::new(bs, default_syscalls, message); rt.caller_type = SYSTEM_ACTOR_CODE_ID.clone(); return rt; @@ -32,7 +35,7 @@ fn construct_runtime<'a,'sys,BS: BlockStore, SYS: Syscalls>(bs: &'a BS, default_ fn balance_less_than_reward() { let bs = MemoryDB::default(); let default_syscalls = DefaultSyscalls::new(&bs); - let mut rt = construct_runtime(&bs, & default_syscalls); + let mut rt = construct_runtime(&bs, &default_syscalls); construct_and_verify(&mut rt); let miner = Address::new_id(1000); @@ -62,7 +65,9 @@ fn balance_less_than_reward() { rt.verify() } -fn construct_and_verify<'a,'sys,BS: BlockStore, SYS: Syscalls>(rt: &mut MockRuntime<'a, 'sys, BS, SYS>) { +fn construct_and_verify<'a, 'sys, BS: BlockStore, SYS: Syscalls>( + rt: &mut MockRuntime<'a, 'sys, BS, SYS>, +) { rt.expect_validate_caller_addr(&[SYSTEM_ACTOR_ADDR.clone()]); let ret = rt .call( From fc07aceb8b767a59dd237a4928b1789f70681deb Mon Sep 17 00:00:00 2001 From: DragonMural Date: Mon, 8 Jun 2020 12:19:26 -0400 Subject: [PATCH 06/29] Reverting syscall changes and using trait for MockRunTime instead --- vm/actor/src/builtin/paych/mod.rs | 4 + vm/actor/tests/account_actor_test.rs | 3 +- vm/actor/tests/common/mod.rs | 65 ++++++++++---- vm/actor/tests/cron_actor_test.rs | 26 ++---- vm/actor/tests/init_actor_test.rs | 30 +++---- vm/actor/tests/paych_actor_test.rs | 127 ++++++++++++--------------- vm/actor/tests/reward_actor_test.rs | 14 +-- vm/interpreter/src/gas_syscalls.rs | 2 + 8 files changed, 137 insertions(+), 134 deletions(-) diff --git a/vm/actor/src/builtin/paych/mod.rs b/vm/actor/src/builtin/paych/mod.rs index 860048a19165..6ed9b0a90156 100644 --- a/vm/actor/src/builtin/paych/mod.rs +++ b/vm/actor/src/builtin/paych/mod.rs @@ -109,6 +109,8 @@ impl Actor { ) })?; + println!("Passing sig line 112 sv_bz is {:?}", sv_bz.to_vec()); + // Validate signature rt.syscalls() .verify_signature(&sig, &signer, &sv_bz) @@ -119,6 +121,8 @@ impl Actor { ) })?; + println!("Passing sig line 124"); + if rt.curr_epoch() < sv.time_lock_min { return Err(rt.abort(ExitCode::ErrIllegalArgument, "cannot use this voucher yet")); } diff --git a/vm/actor/tests/account_actor_test.rs b/vm/actor/tests/account_actor_test.rs index bb291069ac02..db9a4b3e4ca9 100644 --- a/vm/actor/tests/account_actor_test.rs +++ b/vm/actor/tests/account_actor_test.rs @@ -22,8 +22,7 @@ macro_rules! account_tests { let bs = MemoryDB::default(); let receiver = Address::new_id(100); let message = UnsignedMessage::builder().to(receiver.clone()).from(SYSTEM_ACTOR_ADDR.clone()).build().unwrap(); - let default_syscalls = DefaultSyscalls::new(&bs); - let mut rt = MockRuntime::new(&bs, &default_syscalls, message); + let mut rt = MockRuntime::new(&bs, message); rt.caller_type = SYSTEM_ACTOR_CODE_ID.clone(); rt.expect_validate_caller_addr(&[*SYSTEM_ACTOR_ADDR]); diff --git a/vm/actor/tests/common/mod.rs b/vm/actor/tests/common/mod.rs index bf84c2592b25..240ffed75899 100644 --- a/vm/actor/tests/common/mod.rs +++ b/vm/actor/tests/common/mod.rs @@ -18,13 +18,19 @@ use std::cell::{Cell, RefCell}; use std::collections::{HashMap, VecDeque}; use vm::{ActorError, ExitCode, MethodNum, Randomness, Serialized, TokenAmount}; //use interpreter::gas_syscalls::GasSyscalls; +use fil_types::{ + zero_piece_commitment, PaddedPieceSize, PieceInfo, RegisteredProof, SealVerifyInfo, SectorInfo, + WindowPoStVerifyInfo, +}; use interpreter::{internal_send, ChainRand, DefaultRuntime, DefaultSyscalls, GasSyscalls}; use interpreter::{price_list_by_epoch, GasTracker}; +use runtime::{ConsensusFault, ConsensusFaultType}; +use std::error::Error as StdError; use std::rc::Rc; //use super::gas_tracker::{price_list_by_epoch, GasTracker, PriceList}; -pub struct MockRuntime<'a, 'sys, BS, SYS> { +pub struct MockRuntime<'a, BS: BlockStore> { pub epoch: ChainEpoch, pub caller_type: Cid, pub miner: Address, @@ -33,7 +39,6 @@ pub struct MockRuntime<'a, 'sys, BS, SYS> { pub actor_code_cids: HashMap, pub new_actor_addr: Option
, pub message: UnsignedMessage, - syscalls: GasSyscalls<'sys, SYS>, // TODO: syscalls: syscaller @@ -80,21 +85,14 @@ pub struct ExpectedVerifySig { result: Option, } -impl<'a, 'sys, BS, SYS> MockRuntime<'a, 'sys, BS, SYS> +impl<'a, BS> MockRuntime<'a, BS> where BS: BlockStore, - SYS: Syscalls, { - pub fn new(bs: &'a BS, default_syscalls: &'sys SYS, message: UnsignedMessage) -> Self { + pub fn new(bs: &'a BS, message: UnsignedMessage) -> Self { let price_list = price_list_by_epoch(0); let gas_tracker = Rc::new(RefCell::new(GasTracker::new(message.gas_limit() as i64, 0))); - let gas_syscalls = GasSyscalls { - price_list, - gas: Rc::clone(&gas_tracker), - syscalls: default_syscalls, - }; - MockRuntime { epoch: 0, caller_type: Cid::default(), @@ -109,7 +107,6 @@ where state: None, balance: 0u8.into(), received: 0u8.into(), - syscalls: gas_syscalls, // VM Impl in_call: false, @@ -307,10 +304,9 @@ where } } -impl Runtime for MockRuntime<'_, '_, BS, SYS> +impl Runtime for MockRuntime<'_, BS> where BS: BlockStore, - SYS: Syscalls, { fn message(&self) -> &UnsignedMessage { self.require_in_call(); @@ -569,6 +565,45 @@ where } fn syscalls(&self) -> &dyn Syscalls { - &self.syscalls + self + } +} + +impl Syscalls for MockRuntime<'_, BS> +where + BS: BlockStore, +{ + fn verify_signature( + &self, + _signature: &Signature, + _signer: &Address, + _plaintext: &[u8], + ) -> Result<(), Box> { + unimplemented!(); + } + fn hash_blake2b(&self, _data: &[u8]) -> Result<[u8; 32], Box> { + unimplemented!(); + } + fn compute_unsealed_sector_cid( + &self, + _reg: RegisteredProof, + _pieces: &[PieceInfo], + ) -> Result> { + unimplemented!(); + } + fn verify_seal(&self, _vi: &SealVerifyInfo) -> Result<(), Box> { + unimplemented!(); + } + fn verify_post(&self, _vi: &WindowPoStVerifyInfo) -> Result<(), Box> { + unimplemented!(); + } + fn verify_consensus_fault( + &self, + _h1: &[u8], + _h2: &[u8], + _extra: &[u8], + _earliest: ChainEpoch, + ) -> Result, Box> { + unimplemented!(); } } diff --git a/vm/actor/tests/cron_actor_test.rs b/vm/actor/tests/cron_actor_test.rs index 18bd3d1b069b..04713c5ae294 100644 --- a/vm/actor/tests/cron_actor_test.rs +++ b/vm/actor/tests/cron_actor_test.rs @@ -15,10 +15,7 @@ use message::UnsignedMessage; use runtime::Syscalls; use vm::{ExitCode, Serialized}; -fn construct_runtime<'a, 'sys, BS: BlockStore, SYS: Syscalls>( - bs: &'a BS, - default_calls: &'sys SYS, -) -> MockRuntime<'a, 'sys, BS, SYS> { +fn construct_runtime<'a, BS: BlockStore>(bs: &'a BS) -> MockRuntime<'a, BS> { let receiver = Address::new_id(100); let message = UnsignedMessage::builder() @@ -26,16 +23,15 @@ fn construct_runtime<'a, 'sys, BS: BlockStore, SYS: Syscalls>( .to(receiver.clone()) .build() .unwrap(); - let mut rt = MockRuntime::new(bs, default_calls, message); + let mut rt = MockRuntime::new(bs, message); rt.caller_type = SYSTEM_ACTOR_CODE_ID.clone(); return rt; } #[test] fn construct_with_empty_entries() { let bs = MemoryDB::default(); - let default_syscalls = DefaultSyscalls::new(&bs); - let mut rt = construct_runtime(&bs, &default_syscalls); + let mut rt = construct_runtime(&bs); construct_and_verify(&mut rt, &ConstructorParams { entries: vec![] }); let state: State = rt.get_state().unwrap(); @@ -46,9 +42,8 @@ fn construct_with_empty_entries() { #[test] fn construct_with_entries() { let bs = MemoryDB::default(); - let default_syscalls = DefaultSyscalls::new(&bs); - let mut rt = construct_runtime(&bs, &default_syscalls); + let mut rt = construct_runtime(&bs); let entry1 = Entry { receiver: Address::new_id(1001), @@ -81,8 +76,7 @@ fn construct_with_entries() { #[test] fn epoch_tick_with_empty_entries() { let bs = MemoryDB::default(); - let default_syscalls = DefaultSyscalls::new(&bs); - let mut rt = construct_runtime(&bs, &default_syscalls); + let mut rt = construct_runtime(&bs); construct_and_verify(&mut rt, &ConstructorParams { entries: vec![] }); epoch_tick_and_verify(&mut rt); @@ -90,8 +84,7 @@ fn epoch_tick_with_empty_entries() { #[test] fn epoch_tick_with_entries() { let bs = MemoryDB::default(); - let default_syscalls = DefaultSyscalls::new(&bs); - let mut rt = construct_runtime(&bs, &default_syscalls); + let mut rt = construct_runtime(&bs); let entry1 = Entry { receiver: Address::new_id(1001), @@ -158,10 +151,7 @@ fn epoch_tick_with_entries() { epoch_tick_and_verify(&mut rt); } -fn construct_and_verify( - rt: &mut MockRuntime<'_, '_, BS, SYS>, - params: &ConstructorParams, -) { +fn construct_and_verify(rt: &mut MockRuntime<'_, BS>, params: &ConstructorParams) { rt.expect_validate_caller_addr(&[*SYSTEM_ACTOR_ADDR]); let ret = rt .call( @@ -174,7 +164,7 @@ fn construct_and_verify( rt.verify(); } -fn epoch_tick_and_verify(rt: &mut MockRuntime<'_, '_, BS, SYS>) { +fn epoch_tick_and_verify(rt: &mut MockRuntime<'_, BS>) { rt.expect_validate_caller_addr(&[*SYSTEM_ACTOR_ADDR]); let ret = rt .call(&*CRON_ACTOR_CODE_ID, 2, &Serialized::default()) diff --git a/vm/actor/tests/init_actor_test.rs b/vm/actor/tests/init_actor_test.rs index 3db94ae085de..a8eda4045151 100644 --- a/vm/actor/tests/init_actor_test.rs +++ b/vm/actor/tests/init_actor_test.rs @@ -20,17 +20,14 @@ use runtime::Syscalls; use serde::Serialize; use vm::{ActorError, ExitCode, Serialized, TokenAmount, METHOD_CONSTRUCTOR}; -fn construct_runtime<'a, 'sys, BS: BlockStore, SYS: Syscalls>( - bs: &'a BS, - default_syscalls: &'sys SYS, -) -> MockRuntime<'a, 'sys, BS, SYS> { +fn construct_runtime<'a, BS: BlockStore>(bs: &'a BS) -> MockRuntime<'a, BS> { let receiver = Address::new_id(1000); let message = UnsignedMessage::builder() .to(receiver.clone()) .from(SYSTEM_ACTOR_ADDR.clone()) .build() .unwrap(); - let mut rt = MockRuntime::new(bs, default_syscalls, message); + let mut rt = MockRuntime::new(bs, message); rt.caller_type = SYSTEM_ACTOR_CODE_ID.clone(); return rt; } @@ -39,8 +36,7 @@ fn construct_runtime<'a, 'sys, BS: BlockStore, SYS: Syscalls>( #[test] fn abort_cant_call_exec() { let bs = MemoryDB::default(); - let default_syscalls = DefaultSyscalls::new(&bs); - let mut rt = construct_runtime(&bs, &default_syscalls); + let mut rt = construct_runtime(&bs); construct_and_verify(&mut rt); let anne = Address::new_id(1001); @@ -54,8 +50,7 @@ fn abort_cant_call_exec() { #[test] fn create_2_payment_channels() { let bs = MemoryDB::default(); - let default_syscalls = DefaultSyscalls::new(&bs); - let mut rt = construct_runtime(&bs, &default_syscalls); + let mut rt = construct_runtime(&bs); construct_and_verify(&mut rt); let anne = Address::new_id(1001); @@ -119,8 +114,7 @@ fn create_2_payment_channels() { #[test] fn create_storage_miner() { let bs = MemoryDB::default(); - let default_syscalls = DefaultSyscalls::new(&bs); - let mut rt = construct_runtime(&bs, &default_syscalls); + let mut rt = construct_runtime(&bs); construct_and_verify(&mut rt); // only the storage power actor can create a miner @@ -171,8 +165,7 @@ fn create_storage_miner() { #[test] fn create_multisig_actor() { let bs = MemoryDB::default(); - let default_syscalls = DefaultSyscalls::new(&bs); - let mut rt = construct_runtime(&bs, &default_syscalls); + let mut rt = construct_runtime(&bs); construct_and_verify(&mut rt); // Actor creating multisig actor @@ -216,8 +209,7 @@ fn create_multisig_actor() { #[test] fn sending_constructor_failure() { let bs = MemoryDB::default(); - let default_syscalls = DefaultSyscalls::new(&bs); - let mut rt = construct_runtime(&bs, &default_syscalls); + let mut rt = construct_runtime(&bs); construct_and_verify(&mut rt); // Only the storage power actor can create a miner @@ -269,9 +261,7 @@ fn sending_constructor_failure() { ); } -fn construct_and_verify<'a, 'sys, BS: BlockStore, SYS: Syscalls>( - rt: &mut MockRuntime<'a, 'sys, BS, SYS>, -) { +fn construct_and_verify<'a, BS: BlockStore>(rt: &mut MockRuntime<'a, BS>) { rt.expect_validate_caller_addr(&[SYSTEM_ACTOR_ADDR.clone()]); let params = ConstructorParams { network_name: "mock".to_string(), @@ -299,8 +289,8 @@ fn construct_and_verify<'a, 'sys, BS: BlockStore, SYS: Syscalls>( assert_eq!("mock".to_string(), state_data.network_name); } -fn exec_and_verify<'a, 'sys, BS: BlockStore, SYS: Syscalls, S: Serialize>( - rt: &mut MockRuntime<'a, 'sys, BS, SYS>, +fn exec_and_verify<'a, BS: BlockStore, S: Serialize>( + rt: &mut MockRuntime<'a, BS>, code_id: Cid, params: &S, ) -> Result diff --git a/vm/actor/tests/paych_actor_test.rs b/vm/actor/tests/paych_actor_test.rs index 89a08bd3aa57..78760366b0aa 100644 --- a/vm/actor/tests/paych_actor_test.rs +++ b/vm/actor/tests/paych_actor_test.rs @@ -49,8 +49,7 @@ fn create_paych_actor_test() { .build() .unwrap(); - let default_syscalls = DefaultSyscalls::new(&bs); - let mut rt = MockRuntime::new(&bs, &default_syscalls, message); + let mut rt = MockRuntime::new(&bs, message); rt.set_caller(INIT_ACTOR_CODE_ID.clone(), caller_addr); rt.actor_code_cids .insert(payer_addr, ACCOUNT_ACTOR_CODE_ID.clone()); @@ -70,8 +69,7 @@ fn actor_doesnt_exist_test() { .to(paych_addr) .build() .unwrap(); - let default_syscalls = DefaultSyscalls::new(&bs); - let mut rt = MockRuntime::new(&bs, &default_syscalls, message); + let mut rt = MockRuntime::new(&bs, message); rt.set_caller(INIT_ACTOR_CODE_ID.clone(), caller_addr); rt.actor_code_cids .insert(payer_addr, ACCOUNT_ACTOR_CODE_ID.clone()); @@ -120,8 +118,7 @@ fn actor_constructor_fails() { .to(paych_addr) .build() .unwrap(); - let default_syscalls = DefaultSyscalls::new(&bs); - let mut rt = MockRuntime::new(&bs, &default_syscalls, message); + let mut rt = MockRuntime::new(&bs, message); rt.set_caller(test_case.caller_code, caller_addr); rt.actor_code_cids .insert(test_case.paych_addr, test_case.new_actor_code); @@ -180,7 +177,7 @@ mod create_lane_tests { } } - #[test] + //#[test] fn create_lane_test() { let init_actor_addr = Address::new_id(100); let paych_addr = Address::new_id(101); @@ -233,17 +230,19 @@ mod create_lane_tests { ]; for test_case in test_cases { + println!("Executing the next test"); let bs = MemoryDB::default(); let message = UnsignedMessage::builder() .from(*SYSTEM_ACTOR_ADDR) .to(paych_addr) + .gas_limit(1000) .build() .unwrap(); - let default_syscalls = DefaultSyscalls::new(&bs); - let mut rt = MockRuntime::new(&bs, &default_syscalls, message); + let mut rt = MockRuntime::new(&bs, message); rt.epoch = test_case.epoch; - rt.balance = TokenAmount::from(test_case.balance); - rt.set_caller(INIT_ACTOR_CODE_ID.clone(), *INIT_ACTOR_ADDR); + rt.balance = TokenAmount::from(paych_balance.clone()); + rt.set_caller(INIT_ACTOR_CODE_ID.clone(), init_actor_addr); + rt.actor_code_cids .insert(payee_addr, ACCOUNT_ACTOR_CODE_ID.clone()); rt.actor_code_cids @@ -306,6 +305,7 @@ mod create_lane_tests { &Serialized::serialize(ucp).unwrap(), ) .unwrap_err(); + println!("error message is {}", error.msg()); assert_eq!(error.exit_code(), test_case.exp_exit_code); verify_initial_state(&mut rt, payer_addr, payee_addr); } @@ -317,8 +317,7 @@ mod create_lane_tests { #[test] fn redeem_voucher_one_lane() { let bs = MemoryDB::default(); - let default_syscalls = DefaultSyscalls::new(&bs); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, &default_syscalls, 1); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); let state: PState = rt.get_state().unwrap(); let new_voucher_amount = BigInt::from(9); sv.amount = new_voucher_amount; @@ -332,13 +331,15 @@ fn redeem_voucher_one_lane() { rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), payee_addr); rt.expect_validate_caller_addr(&[state.from, state.to]); rt.expect_verify_signature(sv.signature.unwrap(), payer_addr, None); - assert!(rt + let v = rt .call( &PAYCH_ACTOR_CODE_ID.clone(), Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap() + &Serialized::serialize(ucp).unwrap(), ) - .is_ok()); + .unwrap_err(); + + println!("Error message is {}", v.msg()); rt.verify(); let exp_ls = LaneState { id: 0, @@ -356,11 +357,10 @@ fn redeem_voucher_one_lane() { verify_state(&mut rt, 1, exp_state); } -#[test] +//#[test] fn redeem_voucher_correct_lane() { let bs = MemoryDB::default(); - let default_syscalls = DefaultSyscalls::new(&bs); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, &default_syscalls, 3); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 3); let state: PState = rt.get_state().unwrap(); let initial_amount = state.to_send; sv.amount = BigInt::from(9); @@ -403,12 +403,11 @@ fn redeem_voucher_correct_lane() { assert_eq!(sv.nonce, ls_updated.nonce); } -#[test] +//#[test] fn merge_success() { let num_lanes = 3; let bs = MemoryDB::default(); - let default_syscalls = DefaultSyscalls::new(&bs); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, &default_syscalls, num_lanes); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, num_lanes); let mut state: PState = rt.get_state().unwrap(); let state_2: PState = rt.get_state().unwrap(); @@ -472,7 +471,7 @@ fn merge_success() { verify_state(&mut rt, num_lanes as i64, exp_state); } -#[test] +//#[test] fn merge_failue() { let lane_vec = vec![1, 1, 1, 0]; let voucher_vec = vec![10, 0, 10, 10]; @@ -489,8 +488,7 @@ fn merge_failue() { for i in 0..num_test_cases { let bs = MemoryDB::default(); - let default_syscalls = DefaultSyscalls::new(&bs); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, &default_syscalls, 2); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 2); rt.balance = TokenAmount::from(balance_vec[i] as u64); let state: PState = rt.get_state().unwrap(); @@ -524,11 +522,10 @@ fn merge_failue() { } } -#[test] +//#[test] fn invalid_merge_lane_999() { let bs = MemoryDB::default(); - let default_syscalls = DefaultSyscalls::new(&bs); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, &default_syscalls, 2); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 2); let state: PState = rt.get_state().unwrap(); let payee_addr = Address::new_id(103); let merge_to: &LaneState = &state.lane_states[0]; @@ -564,12 +561,10 @@ fn invalid_merge_lane_999() { rt.verify(); } -#[test] +//#[test] fn lane_limit_exceeded() { let bs = MemoryDB::default(); - let default_syscalls = DefaultSyscalls::new(&bs); - let (mut rt, mut sv) = - require_create_cannel_with_lanes(&bs, &default_syscalls, LANE_LIMIT as u64); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, LANE_LIMIT as u64); let state: PState = rt.get_state().unwrap(); let payee_addr = Address::new_id(103); sv.lane += 1; @@ -595,11 +590,10 @@ fn lane_limit_exceeded() { rt.verify(); } -#[test] +//#[test] fn extra_call_succeed() { let bs = MemoryDB::default(); - let default_syscalls = DefaultSyscalls::new(&bs); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, &default_syscalls, 1); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); let state: PState = rt.get_state().unwrap(); let other_addr = Address::new_id(104); rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); @@ -638,11 +632,10 @@ fn extra_call_succeed() { rt.verify(); } -#[test] +//#[test] fn extra_call_fail() { let bs = MemoryDB::default(); - let default_syscalls = DefaultSyscalls::new(&bs); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, &default_syscalls, 1); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); let state: PState = rt.get_state().unwrap(); let other_addr = Address::new_id(104); rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); @@ -681,11 +674,10 @@ fn extra_call_fail() { rt.verify(); } -#[test] +//#[test] fn update_channel_setting() { let bs = MemoryDB::default(); - let default_syscalls = DefaultSyscalls::new(&bs); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, &default_syscalls, 1); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); rt.epoch = 10; let state: PState = rt.get_state().unwrap(); rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); @@ -730,11 +722,10 @@ fn update_channel_setting() { } } -#[test] +//#[test] fn succeed_correct_secret() { let bs = MemoryDB::default(); - let default_syscalls = DefaultSyscalls::new(&bs); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, &default_syscalls, 1); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); let state: PState = rt.get_state().unwrap(); let mut ucp = UpdateChannelStateParams { proof: vec![], @@ -753,11 +744,10 @@ fn succeed_correct_secret() { rt.verify(); } -#[test] +//#[test] fn incorrect_secret() { let bs = MemoryDB::default(); - let default_syscalls = DefaultSyscalls::new(&bs); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, &default_syscalls, 1); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); let state: PState = rt.get_state().unwrap(); let mut ucp = UpdateChannelStateParams { proof: vec![], @@ -777,11 +767,10 @@ fn incorrect_secret() { rt.verify(); } -#[test] +//#[test] fn adjust_settling_at() { let bs = MemoryDB::default(); - let default_syscalls = DefaultSyscalls::new(&bs); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, &default_syscalls, 1); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); let ep = 10; rt.epoch = ep; let mut state: PState = rt.get_state().unwrap(); @@ -800,11 +789,10 @@ fn adjust_settling_at() { assert_eq!(state.min_settle_height, 0); } -#[test] +//#[test] fn settle_if_height_less() { let bs = MemoryDB::default(); - let default_syscalls = DefaultSyscalls::new(&bs); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, &default_syscalls, 1); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); let ep = 10; rt.epoch = ep; let mut state: PState = rt.get_state().unwrap(); @@ -839,11 +827,10 @@ fn settle_if_height_less() { assert_eq!(state.settling_at, ucp.sv.min_settle_height); } -fn require_create_cannel_with_lanes<'a, 'sys, BS: BlockStore, SYS: Syscalls>( +fn require_create_cannel_with_lanes<'a, BS: BlockStore>( bs: &'a BS, - default_syscalls: &'sys SYS, num_lanes: u64, -) -> (MockRuntime<'a, 'sys, BS, SYS>, SignedVoucher) { +) -> (MockRuntime<'a, BS>, SignedVoucher) { let paych_addr = Address::new_id(100); let payer_addr = Address::new_id(102); let payee_addr = Address::new_id(103); @@ -859,8 +846,8 @@ fn require_create_cannel_with_lanes<'a, 'sys, BS: BlockStore, SYS: Syscalls>( .build() .unwrap(); - //let default_syscalls = DefaultSyscalls::new(&bs); - let mut rt = MockRuntime::new(bs, default_syscalls, message); + let mut rt = MockRuntime::new(bs, message); + rt.epoch = curr_epoch; rt.balance = TokenAmount::from(balance); rt.received = TokenAmount::from(recieved); @@ -888,8 +875,8 @@ fn require_create_cannel_with_lanes<'a, 'sys, BS: BlockStore, SYS: Syscalls>( (rt, last_sv) } -fn require_add_new_lane( - rt: &mut MockRuntime<'_, '_, BS, SYS>, +fn require_add_new_lane( + rt: &mut MockRuntime<'_, BS>, param: lane_params, ) -> SignedVoucher { let payee_addr = Address::new_id(103); @@ -915,13 +902,15 @@ fn require_add_new_lane( sv: sv, }; - assert!(rt + let v = rt .call( &PAYCH_ACTOR_CODE_ID.clone(), Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap() + &Serialized::serialize(ucp).unwrap(), ) - .is_ok()); + .unwrap_err(); + + println!("Failed becuase {:?}", v.msg()); rt.verify(); SignedVoucher { @@ -938,8 +927,8 @@ fn require_add_new_lane( } } -fn construct_and_verify( - rt: &mut MockRuntime<'_, '_, BS, SYS>, +fn construct_and_verify( + rt: &mut MockRuntime<'_, BS>, sender: Address, receiver: Address, ) { @@ -959,8 +948,8 @@ fn construct_and_verify( verify_initial_state(rt, sender, receiver); } -fn verify_initial_state( - rt: &mut MockRuntime<'_, '_, BS, SYS>, +fn verify_initial_state( + rt: &mut MockRuntime<'_, BS>, sender: Address, receiver: Address, ) { @@ -974,8 +963,8 @@ fn verify_initial_state( assert_eq!(state.lane_states.len(), 0); } -fn verify_state( - rt: &mut MockRuntime<'_, '_, BS, SYS>, +fn verify_state( + rt: &mut MockRuntime<'_, BS>, exp_lanes: i64, expected_state: PState, ) { diff --git a/vm/actor/tests/reward_actor_test.rs b/vm/actor/tests/reward_actor_test.rs index 70f376e64868..3b81d47c79b9 100644 --- a/vm/actor/tests/reward_actor_test.rs +++ b/vm/actor/tests/reward_actor_test.rs @@ -16,17 +16,14 @@ use message::UnsignedMessage; use runtime::Syscalls; use vm::{ExitCode, Serialized, TokenAmount, METHOD_CONSTRUCTOR}; -fn construct_runtime<'a, 'sys, BS: BlockStore, SYS: Syscalls>( - bs: &'a BS, - default_syscalls: &'sys SYS, -) -> MockRuntime<'a, 'sys, BS, SYS> { +fn construct_runtime<'a, BS: BlockStore>(bs: &'a BS) -> MockRuntime<'a, BS> { let message = UnsignedMessage::builder() .to(*REWARD_ACTOR_ADDR) .from(*SYSTEM_ACTOR_ADDR) .build() .unwrap(); - let mut rt = MockRuntime::new(bs, default_syscalls, message); + let mut rt = MockRuntime::new(bs, message); rt.caller_type = SYSTEM_ACTOR_CODE_ID.clone(); return rt; } @@ -34,8 +31,7 @@ fn construct_runtime<'a, 'sys, BS: BlockStore, SYS: Syscalls>( #[test] fn balance_less_than_reward() { let bs = MemoryDB::default(); - let default_syscalls = DefaultSyscalls::new(&bs); - let mut rt = construct_runtime(&bs, &default_syscalls); + let mut rt = construct_runtime(&bs); construct_and_verify(&mut rt); let miner = Address::new_id(1000); @@ -65,9 +61,7 @@ fn balance_less_than_reward() { rt.verify() } -fn construct_and_verify<'a, 'sys, BS: BlockStore, SYS: Syscalls>( - rt: &mut MockRuntime<'a, 'sys, BS, SYS>, -) { +fn construct_and_verify<'a, BS: BlockStore>(rt: &mut MockRuntime<'a, BS>) { rt.expect_validate_caller_addr(&[SYSTEM_ACTOR_ADDR.clone()]); let ret = rt .call( diff --git a/vm/interpreter/src/gas_syscalls.rs b/vm/interpreter/src/gas_syscalls.rs index 6f1cbb39db7a..9e8e40e31bc4 100644 --- a/vm/interpreter/src/gas_syscalls.rs +++ b/vm/interpreter/src/gas_syscalls.rs @@ -29,6 +29,7 @@ where signer: &Address, plaintext: &[u8], ) -> Result<(), Box> { + println!("In this func"); self.gas .borrow_mut() .charge_gas( @@ -36,6 +37,7 @@ where .on_verify_signature(signature.signature_type(), plaintext.len()), ) .unwrap(); + println!("Passed this partr"); self.syscalls.verify_signature(signature, signer, plaintext) } fn hash_blake2b(&self, data: &[u8]) -> Result<[u8; 32], Box> { From e90b29fc37988b3fe9d40913ecd629730b96bb66 Mon Sep 17 00:00:00 2001 From: DragonMural Date: Mon, 8 Jun 2020 22:52:30 -0400 Subject: [PATCH 07/29] Added one syscall method, one more test passing and fixed minor issues --- vm/actor/src/builtin/paych/mod.rs | 4 +- vm/actor/tests/common/mod.rs | 41 +++++++++++++++---- vm/actor/tests/paych_actor_test.rs | 66 +++++++++++++++++------------- 3 files changed, 72 insertions(+), 39 deletions(-) diff --git a/vm/actor/src/builtin/paych/mod.rs b/vm/actor/src/builtin/paych/mod.rs index 6ed9b0a90156..dc8a3f883f56 100644 --- a/vm/actor/src/builtin/paych/mod.rs +++ b/vm/actor/src/builtin/paych/mod.rs @@ -98,7 +98,7 @@ impl Actor { // Pull signature from signed voucher let sig = sv .signature - .take() + .clone() .ok_or_else(|| rt.abort(ExitCode::ErrIllegalArgument, "voucher has no signature"))?; // Generate unsigned bytes @@ -215,7 +215,7 @@ impl Actor { // 2. To prevent double counting, remove already redeemed amounts (from // voucher or other lanes) from the voucher amount st.lane_states[idx].nonce = sv.nonce; - let balance_delta = &sv.amount - redeemed + &st.lane_states[idx].redeemed; + let balance_delta = &sv.amount - (redeemed + &st.lane_states[idx].redeemed); // 3. set new redeemed value for merged-into lane st.lane_states[idx].redeemed = sv.amount; diff --git a/vm/actor/tests/common/mod.rs b/vm/actor/tests/common/mod.rs index 240ffed75899..849febc4bcc9 100644 --- a/vm/actor/tests/common/mod.rs +++ b/vm/actor/tests/common/mod.rs @@ -58,7 +58,7 @@ pub struct MockRuntime<'a, BS: BlockStore> { pub expect_validate_caller_type: RefCell>>, pub expect_sends: VecDeque, pub expect_create_actor: Option, - pub expect_verify_sig: Option, + pub expect_verify_sig: RefCell>, } #[derive(Clone, Debug)] @@ -82,7 +82,8 @@ pub struct ExpectedMessage { pub struct ExpectedVerifySig { sig: Signature, signer: Address, - result: Option, + plaintext : Vec, + result: ExitCode } impl<'a, BS> MockRuntime<'a, BS> @@ -119,7 +120,7 @@ where expect_validate_caller_type: RefCell::new(None), expect_sends: VecDeque::new(), expect_create_actor: None, - expect_verify_sig: None, + expect_verify_sig: RefCell::new(None), } } fn require_in_call(&self) { @@ -159,17 +160,21 @@ where *self.expect_validate_caller_type.borrow_mut() = Some(ids.to_vec()); } + #[allow(dead_code)] pub fn expect_verify_signature( &mut self, sig: Signature, signer: Address, - result: Option, + plaintext: Vec, + result: ExitCode, ) { - self.expect_verify_sig = Some(ExpectedVerifySig { + println!("Plain text is {:?}", plaintext); + self.expect_verify_sig = RefCell::new( Some(ExpectedVerifySig { sig: sig, signer: signer, + plaintext: plaintext, result: result, - }); + })); } #[allow(dead_code)] @@ -262,7 +267,7 @@ where *self.expect_validate_caller_addr.borrow_mut() = None; *self.expect_validate_caller_type.borrow_mut() = None; self.expect_create_actor = None; - self.expect_verify_sig = None; + *self.expect_verify_sig.borrow_mut() = None; } #[allow(dead_code)] @@ -579,8 +584,28 @@ where _signer: &Address, _plaintext: &[u8], ) -> Result<(), Box> { - unimplemented!(); + + let op_exp = self.expect_verify_sig.replace(Option::None); + + if let Some(exp) = op_exp{ + + if exp.sig == *_signature && exp.signer == *_signer && (exp.plaintext[..]) == *_plaintext{ + if exp.result == ExitCode::Ok{ + return Ok(()); + } + else{ + return Err(Box::new(ActorError::new(exp.result, "Expected failure".to_string()))); + } + } + else{ + return Err(Box::new(ActorError::new(ExitCode::ErrIllegalState, "Signatures did not matchcarg".to_string()))); + } + } + else{ + return Err(Box::new(ActorError::new(ExitCode::ErrPlaceholder, "Expected verify sig not there ".to_string()))); + } } + fn hash_blake2b(&self, _data: &[u8]) -> Result<[u8; 32], Box> { unimplemented!(); } diff --git a/vm/actor/tests/paych_actor_test.rs b/vm/actor/tests/paych_actor_test.rs index 78760366b0aa..b45a4631e538 100644 --- a/vm/actor/tests/paych_actor_test.rs +++ b/vm/actor/tests/paych_actor_test.rs @@ -24,7 +24,7 @@ use ipld_blockstore::BlockStore; use message::UnsignedMessage; use num_bigint::{BigInt, Sign}; use runtime::{ActorCode, Runtime, Syscalls}; - +use encoding::to_vec; use vm::{ExitCode, Serialized, TokenAmount, METHOD_CONSTRUCTOR}; struct lane_params { @@ -330,16 +330,20 @@ fn redeem_voucher_one_lane() { let payer_addr = Address::new_id(102); rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), payee_addr); rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature(sv.signature.unwrap(), payer_addr, None); - let v = rt - .call( - &PAYCH_ACTOR_CODE_ID.clone(), - Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap(), - ) - .unwrap_err(); + let s = sv.signature.clone().unwrap(); + + rt.expect_verify_signature(s.clone(), payer_addr, to_vec(&sv).unwrap() , ExitCode::Ok); + println!("line 334 it is {:?}",rt.expect_verify_sig); - println!("Error message is {}", v.msg()); + assert!(rt + .call( + &PAYCH_ACTOR_CODE_ID.clone(), + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap(), + ) + .is_ok() + ); + rt.verify(); let exp_ls = LaneState { id: 0, @@ -372,7 +376,7 @@ fn redeem_voucher_correct_lane() { rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), payee_addr); rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature(sv.clone().signature.unwrap(), payer_addr, None); + rt.expect_verify_signature(sv.clone().signature.unwrap(), payer_addr, vec![], ExitCode::Ok); let ucp = UpdateChannelStateParams { proof: vec![], @@ -424,7 +428,7 @@ fn merge_success() { sv.merges = merges; let payee_addr = Address::new_id(103); rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature(sv.clone().signature.unwrap(), payee_addr, None); + rt.expect_verify_signature(sv.clone().signature.unwrap(), payee_addr, vec![], ExitCode::Ok); let ucp = UpdateChannelStateParams { proof: vec![], secret: vec![], @@ -509,7 +513,7 @@ fn merge_failue() { rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature(sv.clone().signature.unwrap(), payee_addr, None); + rt.expect_verify_signature(sv.clone().signature.unwrap(), payee_addr, vec![], ExitCode::Ok); let v = rt .call( &PAYCH_ACTOR_CODE_ID.clone(), @@ -549,7 +553,7 @@ fn invalid_merge_lane_999() { rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature(sv.clone().signature.unwrap(), payee_addr, None); + rt.expect_verify_signature(sv.clone().signature.unwrap(), payee_addr, vec![], ExitCode::Ok); let v = rt .call( &PAYCH_ACTOR_CODE_ID.clone(), @@ -578,7 +582,7 @@ fn lane_limit_exceeded() { rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature(sv.clone().signature.unwrap(), payee_addr, None); + rt.expect_verify_signature(sv.clone().signature.unwrap(), payee_addr, vec![], ExitCode::Ok); let v = rt .call( &PAYCH_ACTOR_CODE_ID.clone(), @@ -603,7 +607,7 @@ fn extra_call_succeed() { data: Serialized::serialize([1, 2, 3, 4]).unwrap(), }); rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature(sv.clone().signature.unwrap(), state.to, None); + rt.expect_verify_signature(sv.clone().signature.unwrap(), state.to, vec![], ExitCode::Ok); let exp_send_params = PaymentVerifyParams { extra: Serialized::serialize(vec![1, 2, 3, 4]).unwrap(), proof: vec![], @@ -657,7 +661,7 @@ fn extra_call_fail() { Serialized::default(), ExitCode::ErrPlaceholder, ); - rt.expect_verify_signature(sv.clone().signature.unwrap(), state.to, None); + rt.expect_verify_signature(sv.clone().signature.unwrap(), state.to, vec![], ExitCode::Ok); let ucp = UpdateChannelStateParams { proof: vec![], secret: vec![], @@ -708,7 +712,7 @@ fn update_channel_setting() { }; ucp.sv.min_settle_height = min_settle_vec[i]; rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature(ucp.sv.clone().signature.unwrap(), state.to, None); + rt.expect_verify_signature(ucp.sv.clone().signature.unwrap(), state.to, vec![], ExitCode::Ok); assert!(rt .call( &PAYCH_ACTOR_CODE_ID.clone(), @@ -733,7 +737,7 @@ fn succeed_correct_secret() { sv: sv.clone(), }; rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature(ucp.sv.clone().signature.unwrap(), state.to, None); + rt.expect_verify_signature(ucp.sv.clone().signature.unwrap(), state.to, vec![], ExitCode::Ok); assert!(rt .call( &PAYCH_ACTOR_CODE_ID.clone(), @@ -755,7 +759,7 @@ fn incorrect_secret() { sv: sv.clone(), }; rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature(ucp.sv.clone().signature.unwrap(), state.to, None); + rt.expect_verify_signature(ucp.sv.clone().signature.unwrap(), state.to, vec![], ExitCode::Ok); let v = rt .call( &PAYCH_ACTOR_CODE_ID.clone(), @@ -803,7 +807,7 @@ fn settle_if_height_less() { sv: sv.clone(), }; rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature(ucp.sv.clone().signature.unwrap(), state.to, None); + rt.expect_verify_signature(ucp.sv.clone().signature.unwrap(), state.to, vec![], ExitCode::Ok); assert!(rt .call( &PAYCH_ACTOR_CODE_ID.clone(), @@ -865,7 +869,7 @@ fn require_create_cannel_with_lanes<'a, BS: BlockStore>( from: payer_addr, to: payee_addr, amt: (i + 1) as i64, - lane: i, + lane: i as u64, nonce: i + 1, }; @@ -880,7 +884,8 @@ fn require_add_new_lane( param: lane_params, ) -> SignedVoucher { let payee_addr = Address::new_id(103); - let sig = Signature::new_bls(vec![0, 1, 2, 3, 4, 5, 67]); + let sig = Signature::new_bls(vec![0, 1, 2, 3, 4, 5, 6,7]); + // println!("sig is {:?}", sig.bytes()); let sv = SignedVoucher { time_lock_min: param.epoch_num, time_lock_max: u64::MAX, @@ -895,23 +900,25 @@ fn require_add_new_lane( }; rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), param.from); rt.expect_validate_caller_addr(&[param.from, param.to]); - rt.expect_verify_signature(sig.clone(), payee_addr, None); + rt.expect_verify_signature(sig.clone(), payee_addr, to_vec(&sv).unwrap(), ExitCode::Ok); + println!(" sv vec bytes is {:?}",to_vec(&sv).unwrap()); + let ucp = UpdateChannelStateParams { proof: vec![], secret: vec![], sv: sv, }; - let v = rt + + assert!( rt .call( &PAYCH_ACTOR_CODE_ID.clone(), Method::UpdateChannelState as u64, &Serialized::serialize(ucp).unwrap(), ) - .unwrap_err(); - - println!("Failed becuase {:?}", v.msg()); - + .is_ok() + ); + rt.verify(); SignedVoucher { time_lock_min: param.epoch_num, @@ -927,6 +934,7 @@ fn require_add_new_lane( } } + fn construct_and_verify( rt: &mut MockRuntime<'_, BS>, sender: Address, From 9f3bb025590e2e64851fd67313f6bba597db6f4d Mon Sep 17 00:00:00 2001 From: DragonMural Date: Tue, 9 Jun 2020 00:56:23 -0400 Subject: [PATCH 08/29] Adding changes for succesful tests. 2 tests still failing --- vm/actor/src/builtin/paych/mod.rs | 1 + vm/actor/tests/common/mod.rs | 41 ++++---- vm/actor/tests/paych_actor_test.rs | 148 ++++++++++++++++++++--------- 3 files changed, 126 insertions(+), 64 deletions(-) diff --git a/vm/actor/src/builtin/paych/mod.rs b/vm/actor/src/builtin/paych/mod.rs index dc8a3f883f56..7adbd0a3c5cc 100644 --- a/vm/actor/src/builtin/paych/mod.rs +++ b/vm/actor/src/builtin/paych/mod.rs @@ -187,6 +187,7 @@ impl Actor { // 1. (optional) sum already redeemed value of all merging lanes let mut redeemed = BigInt::default(); for merge in sv.merges { + println!("IN for loop lanes are {:?} and {:?}", merge.lane, sv.lane); if merge.lane == sv.lane { return Err(ActorError::new( ExitCode::ErrIllegalArgument, diff --git a/vm/actor/tests/common/mod.rs b/vm/actor/tests/common/mod.rs index 849febc4bcc9..04600e4c143d 100644 --- a/vm/actor/tests/common/mod.rs +++ b/vm/actor/tests/common/mod.rs @@ -82,8 +82,8 @@ pub struct ExpectedMessage { pub struct ExpectedVerifySig { sig: Signature, signer: Address, - plaintext : Vec, - result: ExitCode + plaintext: Vec, + result: ExitCode, } impl<'a, BS> MockRuntime<'a, BS> @@ -169,7 +169,7 @@ where result: ExitCode, ) { println!("Plain text is {:?}", plaintext); - self.expect_verify_sig = RefCell::new( Some(ExpectedVerifySig { + self.expect_verify_sig = RefCell::new(Some(ExpectedVerifySig { sig: sig, signer: signer, plaintext: plaintext, @@ -584,25 +584,32 @@ where _signer: &Address, _plaintext: &[u8], ) -> Result<(), Box> { - let op_exp = self.expect_verify_sig.replace(Option::None); - if let Some(exp) = op_exp{ - - if exp.sig == *_signature && exp.signer == *_signer && (exp.plaintext[..]) == *_plaintext{ - if exp.result == ExitCode::Ok{ + if let Some(exp) = op_exp { + if exp.sig == *_signature + && exp.signer == *_signer + && (exp.plaintext[..]) == *_plaintext + { + if exp.result == ExitCode::Ok { return Ok(()); + } else { + return Err(Box::new(ActorError::new( + exp.result, + "Expected failure".to_string(), + ))); } - else{ - return Err(Box::new(ActorError::new(exp.result, "Expected failure".to_string()))); - } + } else { + return Err(Box::new(ActorError::new( + ExitCode::ErrIllegalState, + "Signatures did not matchcarg".to_string(), + ))); } - else{ - return Err(Box::new(ActorError::new(ExitCode::ErrIllegalState, "Signatures did not matchcarg".to_string()))); - } - } - else{ - return Err(Box::new(ActorError::new(ExitCode::ErrPlaceholder, "Expected verify sig not there ".to_string()))); + } else { + return Err(Box::new(ActorError::new( + ExitCode::ErrPlaceholder, + "Expected verify sig not there ".to_string(), + ))); } } diff --git a/vm/actor/tests/paych_actor_test.rs b/vm/actor/tests/paych_actor_test.rs index b45a4631e538..87b73b273b36 100644 --- a/vm/actor/tests/paych_actor_test.rs +++ b/vm/actor/tests/paych_actor_test.rs @@ -19,12 +19,12 @@ use common::*; use crypto::Signature; use db::MemoryDB; use derive_builder::Builder; +use encoding::to_vec; use interpreter::{internal_send, ChainRand, DefaultRuntime, DefaultSyscalls, GasSyscalls}; use ipld_blockstore::BlockStore; use message::UnsignedMessage; use num_bigint::{BigInt, Sign}; use runtime::{ActorCode, Runtime, Syscalls}; -use encoding::to_vec; use vm::{ExitCode, Serialized, TokenAmount, METHOD_CONSTRUCTOR}; struct lane_params { @@ -331,19 +331,17 @@ fn redeem_voucher_one_lane() { rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), payee_addr); rt.expect_validate_caller_addr(&[state.from, state.to]); let s = sv.signature.clone().unwrap(); - - rt.expect_verify_signature(s.clone(), payer_addr, to_vec(&sv).unwrap() , ExitCode::Ok); - println!("line 334 it is {:?}",rt.expect_verify_sig); + + rt.expect_verify_signature(s.clone(), payer_addr, to_vec(&sv).unwrap(), ExitCode::Ok); assert!(rt - .call( - &PAYCH_ACTOR_CODE_ID.clone(), - Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap(), - ) - .is_ok() - ); - + .call( + &PAYCH_ACTOR_CODE_ID.clone(), + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap(), + ) + .is_ok()); + rt.verify(); let exp_ls = LaneState { id: 0, @@ -361,7 +359,7 @@ fn redeem_voucher_one_lane() { verify_state(&mut rt, 1, exp_state); } -//#[test] +#[test] fn redeem_voucher_correct_lane() { let bs = MemoryDB::default(); let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 3); @@ -376,7 +374,12 @@ fn redeem_voucher_correct_lane() { rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), payee_addr); rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature(sv.clone().signature.unwrap(), payer_addr, vec![], ExitCode::Ok); + rt.expect_verify_signature( + sv.clone().signature.unwrap(), + payer_addr, + to_vec(&sv).unwrap(), + ExitCode::Ok, + ); let ucp = UpdateChannelStateParams { proof: vec![], @@ -399,15 +402,16 @@ fn redeem_voucher_correct_lane() { &sv.amount - BigInt::from_signed_bytes_be(&ls_to_update.redeemed.to_signed_bytes_be()); let exp_send = big_delta + BigInt::from_signed_bytes_be(&initial_amount.to_radix_be(10)); + println!("amount is {:?}", state.to_send); assert_eq!( exp_send, - BigInt::from_signed_bytes_be(&state.to_send.to_radix_be(10)) + BigInt::from_radix_be(Sign::Plus, &state.to_send.to_radix_be(10), 10).unwrap() ); assert_eq!(sv.amount, ls_updated.redeemed); assert_eq!(sv.nonce, ls_updated.nonce); } -//#[test] +#[test] fn merge_success() { let num_lanes = 3; let bs = MemoryDB::default(); @@ -428,7 +432,12 @@ fn merge_success() { sv.merges = merges; let payee_addr = Address::new_id(103); rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature(sv.clone().signature.unwrap(), payee_addr, vec![], ExitCode::Ok); + rt.expect_verify_signature( + sv.clone().signature.unwrap(), + payee_addr, + to_vec(&sv).unwrap(), + ExitCode::Ok, + ); let ucp = UpdateChannelStateParams { proof: vec![], secret: vec![], @@ -475,7 +484,7 @@ fn merge_success() { verify_state(&mut rt, num_lanes as i64, exp_state); } -//#[test] +#[test] fn merge_failue() { let lane_vec = vec![1, 1, 1, 0]; let voucher_vec = vec![10, 0, 10, 10]; @@ -491,13 +500,14 @@ fn merge_failue() { let payee_addr = Address::new_id(103); for i in 0..num_test_cases { + println!("Test case {}", i); let bs = MemoryDB::default(); let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 2); rt.balance = TokenAmount::from(balance_vec[i] as u64); let state: PState = rt.get_state().unwrap(); let merge_to: &LaneState = &state.lane_states[0]; - let merge_from: &LaneState = &state.lane_states[1]; + let merge_from: &LaneState = &state.lane_states[lane_vec[i]]; sv.lane = merge_to.id; sv.nonce = voucher_vec[i]; sv.merges = vec![Merge { @@ -513,7 +523,12 @@ fn merge_failue() { rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature(sv.clone().signature.unwrap(), payee_addr, vec![], ExitCode::Ok); + rt.expect_verify_signature( + sv.clone().signature.unwrap(), + payee_addr, + to_vec(&sv).unwrap(), + ExitCode::Ok, + ); let v = rt .call( &PAYCH_ACTOR_CODE_ID.clone(), @@ -521,12 +536,13 @@ fn merge_failue() { &Serialized::serialize(ucp).unwrap(), ) .unwrap_err(); + println!("Message is {:?}", v.msg()); assert_eq!(v.exit_code(), exit_vec[i]); rt.verify(); } } -//#[test] +#[test] fn invalid_merge_lane_999() { let bs = MemoryDB::default(); let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 2); @@ -553,7 +569,12 @@ fn invalid_merge_lane_999() { rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature(sv.clone().signature.unwrap(), payee_addr, vec![], ExitCode::Ok); + rt.expect_verify_signature( + sv.clone().signature.unwrap(), + payee_addr, + to_vec(&sv).unwrap(), + ExitCode::Ok, + ); let v = rt .call( &PAYCH_ACTOR_CODE_ID.clone(), @@ -565,7 +586,7 @@ fn invalid_merge_lane_999() { rt.verify(); } -//#[test] +#[test] fn lane_limit_exceeded() { let bs = MemoryDB::default(); let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, LANE_LIMIT as u64); @@ -582,7 +603,12 @@ fn lane_limit_exceeded() { rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature(sv.clone().signature.unwrap(), payee_addr, vec![], ExitCode::Ok); + rt.expect_verify_signature( + sv.clone().signature.unwrap(), + payee_addr, + to_vec(&sv).unwrap(), + ExitCode::Ok, + ); let v = rt .call( &PAYCH_ACTOR_CODE_ID.clone(), @@ -594,7 +620,7 @@ fn lane_limit_exceeded() { rt.verify(); } -//#[test] +#[test] fn extra_call_succeed() { let bs = MemoryDB::default(); let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); @@ -607,7 +633,12 @@ fn extra_call_succeed() { data: Serialized::serialize([1, 2, 3, 4]).unwrap(), }); rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature(sv.clone().signature.unwrap(), state.to, vec![], ExitCode::Ok); + rt.expect_verify_signature( + sv.clone().signature.unwrap(), + state.to, + to_vec(&sv).unwrap(), + ExitCode::Ok, + ); let exp_send_params = PaymentVerifyParams { extra: Serialized::serialize(vec![1, 2, 3, 4]).unwrap(), proof: vec![], @@ -636,7 +667,7 @@ fn extra_call_succeed() { rt.verify(); } -//#[test] +#[test] fn extra_call_fail() { let bs = MemoryDB::default(); let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); @@ -661,7 +692,12 @@ fn extra_call_fail() { Serialized::default(), ExitCode::ErrPlaceholder, ); - rt.expect_verify_signature(sv.clone().signature.unwrap(), state.to, vec![], ExitCode::Ok); + rt.expect_verify_signature( + sv.clone().signature.unwrap(), + state.to, + to_vec(&sv).unwrap(), + ExitCode::Ok, + ); let ucp = UpdateChannelStateParams { proof: vec![], secret: vec![], @@ -678,7 +714,7 @@ fn extra_call_fail() { rt.verify(); } -//#[test] +#[test] fn update_channel_setting() { let bs = MemoryDB::default(); let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); @@ -710,9 +746,14 @@ fn update_channel_setting() { secret: vec![], sv: sv.clone(), }; - ucp.sv.min_settle_height = min_settle_vec[i]; + ucp.sv.min_settle_height = exp_min_settle_height[i]; rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature(ucp.sv.clone().signature.unwrap(), state.to, vec![], ExitCode::Ok); + rt.expect_verify_signature( + ucp.sv.clone().signature.unwrap(), + state.to, + to_vec(&ucp.sv).unwrap(), + ExitCode::Ok, + ); assert!(rt .call( &PAYCH_ACTOR_CODE_ID.clone(), @@ -726,7 +767,7 @@ fn update_channel_setting() { } } -//#[test] +#[test] fn succeed_correct_secret() { let bs = MemoryDB::default(); let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); @@ -737,7 +778,12 @@ fn succeed_correct_secret() { sv: sv.clone(), }; rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature(ucp.sv.clone().signature.unwrap(), state.to, vec![], ExitCode::Ok); + rt.expect_verify_signature( + ucp.sv.clone().signature.unwrap(), + state.to, + to_vec(&sv).unwrap(), + ExitCode::Ok, + ); assert!(rt .call( &PAYCH_ACTOR_CODE_ID.clone(), @@ -755,11 +801,17 @@ fn incorrect_secret() { let state: PState = rt.get_state().unwrap(); let mut ucp = UpdateChannelStateParams { proof: vec![], - secret: vec![], + secret: b"Profesr".to_vec(), sv: sv.clone(), }; + ucp.sv.secret_pre_image = b"Magneto".to_vec(); rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature(ucp.sv.clone().signature.unwrap(), state.to, vec![], ExitCode::Ok); + rt.expect_verify_signature( + ucp.sv.clone().signature.unwrap(), + state.to, + to_vec(&ucp.sv).unwrap(), + ExitCode::Ok, + ); let v = rt .call( &PAYCH_ACTOR_CODE_ID.clone(), @@ -771,7 +823,7 @@ fn incorrect_secret() { rt.verify(); } -//#[test] +#[test] fn adjust_settling_at() { let bs = MemoryDB::default(); let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); @@ -793,7 +845,7 @@ fn adjust_settling_at() { assert_eq!(state.min_settle_height, 0); } -//#[test] +#[test] fn settle_if_height_less() { let bs = MemoryDB::default(); let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); @@ -807,12 +859,17 @@ fn settle_if_height_less() { sv: sv.clone(), }; rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature(ucp.sv.clone().signature.unwrap(), state.to, vec![], ExitCode::Ok); + rt.expect_verify_signature( + ucp.sv.clone().signature.unwrap(), + state.to, + to_vec(&sv).unwrap(), + ExitCode::Ok, + ); assert!(rt .call( &PAYCH_ACTOR_CODE_ID.clone(), Method::UpdateChannelState as u64, - &Serialized::default() + &Serialized::serialize(&ucp).unwrap() ) .is_ok()); state = rt.get_state().unwrap(); @@ -884,7 +941,7 @@ fn require_add_new_lane( param: lane_params, ) -> SignedVoucher { let payee_addr = Address::new_id(103); - let sig = Signature::new_bls(vec![0, 1, 2, 3, 4, 5, 6,7]); + let sig = Signature::new_bls(vec![0, 1, 2, 3, 4, 5, 6, 7]); // println!("sig is {:?}", sig.bytes()); let sv = SignedVoucher { time_lock_min: param.epoch_num, @@ -901,7 +958,7 @@ fn require_add_new_lane( rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), param.from); rt.expect_validate_caller_addr(&[param.from, param.to]); rt.expect_verify_signature(sig.clone(), payee_addr, to_vec(&sv).unwrap(), ExitCode::Ok); - println!(" sv vec bytes is {:?}",to_vec(&sv).unwrap()); + println!(" sv vec bytes is {:?}", to_vec(&sv).unwrap()); let ucp = UpdateChannelStateParams { proof: vec![], @@ -909,16 +966,14 @@ fn require_add_new_lane( sv: sv, }; - - assert!( rt + assert!(rt .call( &PAYCH_ACTOR_CODE_ID.clone(), Method::UpdateChannelState as u64, &Serialized::serialize(ucp).unwrap(), ) - .is_ok() - ); - + .is_ok()); + rt.verify(); SignedVoucher { time_lock_min: param.epoch_num, @@ -934,7 +989,6 @@ fn require_add_new_lane( } } - fn construct_and_verify( rt: &mut MockRuntime<'_, BS>, sender: Address, From dfaf8bd428ef4ca0bc10272465ac3c42b506fbfc Mon Sep 17 00:00:00 2001 From: DragonMural Date: Tue, 9 Jun 2020 10:14:04 -0400 Subject: [PATCH 09/29] Adding serialization vector update --- tests/serialization-vectors | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/serialization-vectors b/tests/serialization-vectors index 70442e2f60be..da34ded5c452 160000 --- a/tests/serialization-vectors +++ b/tests/serialization-vectors @@ -1 +1 @@ -Subproject commit 70442e2f60bee12d44e2d5bcd3008aa287c99a98 +Subproject commit da34ded5c4527860d30df68930049047a51dfb05 From 05588009e2ac572c97b7ed2ce2cdb1b71692d8b4 Mon Sep 17 00:00:00 2001 From: DragonMural Date: Tue, 9 Jun 2020 23:47:27 -0400 Subject: [PATCH 10/29] Adding all tests and some sys call fucntionality --- vm/actor/src/builtin/paych/mod.rs | 5 +- vm/actor/tests/common/mod.rs | 7 +- vm/actor/tests/paych_actor_test.rs | 166 ++++++++++++++++++++++++++--- 3 files changed, 157 insertions(+), 21 deletions(-) diff --git a/vm/actor/src/builtin/paych/mod.rs b/vm/actor/src/builtin/paych/mod.rs index 7adbd0a3c5cc..a766f2ca5805 100644 --- a/vm/actor/src/builtin/paych/mod.rs +++ b/vm/actor/src/builtin/paych/mod.rs @@ -93,8 +93,7 @@ impl Actor { st.from }; - let mut sv = params.sv; - + let sv = params.sv; // Pull signature from signed voucher let sig = sv .signature @@ -109,7 +108,7 @@ impl Actor { ) })?; - println!("Passing sig line 112 sv_bz is {:?}", sv_bz.to_vec()); + //println!("Passing sig line 112 sv_bz is {:?}", sv_bz.to_vec()); // Validate signature rt.syscalls() diff --git a/vm/actor/tests/common/mod.rs b/vm/actor/tests/common/mod.rs index 04600e4c143d..2018f65233f4 100644 --- a/vm/actor/tests/common/mod.rs +++ b/vm/actor/tests/common/mod.rs @@ -18,6 +18,7 @@ use std::cell::{Cell, RefCell}; use std::collections::{HashMap, VecDeque}; use vm::{ActorError, ExitCode, MethodNum, Randomness, Serialized, TokenAmount}; //use interpreter::gas_syscalls::GasSyscalls; +use encoding::blake2b_256; use fil_types::{ zero_piece_commitment, PaddedPieceSize, PieceInfo, RegisteredProof, SealVerifyInfo, SectorInfo, WindowPoStVerifyInfo, @@ -168,7 +169,6 @@ where plaintext: Vec, result: ExitCode, ) { - println!("Plain text is {:?}", plaintext); self.expect_verify_sig = RefCell::new(Some(ExpectedVerifySig { sig: sig, signer: signer, @@ -591,6 +591,7 @@ where && exp.signer == *_signer && (exp.plaintext[..]) == *_plaintext { + //println!("Exp is {:?}",exp); if exp.result == ExitCode::Ok { return Ok(()); } else { @@ -602,7 +603,7 @@ where } else { return Err(Box::new(ActorError::new( ExitCode::ErrIllegalState, - "Signatures did not matchcarg".to_string(), + "Signatures did not match".to_string(), ))); } } else { @@ -614,7 +615,7 @@ where } fn hash_blake2b(&self, _data: &[u8]) -> Result<[u8; 32], Box> { - unimplemented!(); + Ok(blake2b_256(&_data)) } fn compute_unsealed_sector_cid( &self, diff --git a/vm/actor/tests/paych_actor_test.rs b/vm/actor/tests/paych_actor_test.rs index 87b73b273b36..2327ad74f416 100644 --- a/vm/actor/tests/paych_actor_test.rs +++ b/vm/actor/tests/paych_actor_test.rs @@ -25,7 +25,7 @@ use ipld_blockstore::BlockStore; use message::UnsignedMessage; use num_bigint::{BigInt, Sign}; use runtime::{ActorCode, Runtime, Syscalls}; -use vm::{ExitCode, Serialized, TokenAmount, METHOD_CONSTRUCTOR}; +use vm::{ExitCode, Serialized, TokenAmount, METHOD_CONSTRUCTOR, METHOD_SEND}; struct lane_params { epoch_num: ChainEpoch, @@ -141,7 +141,7 @@ fn actor_constructor_fails() { mod create_lane_tests { use super::*; - #[derive(Builder)] + #[derive(Builder, Debug)] #[builder(name = "TestCaseBuilder")] struct TestCase { #[builder(default = "ACCOUNT_ACTOR_CODE_ID.clone()")] @@ -177,7 +177,7 @@ mod create_lane_tests { } } - //#[test] + #[test] fn create_lane_test() { let init_actor_addr = Address::new_id(100); let paych_addr = Address::new_id(101); @@ -187,11 +187,11 @@ mod create_lane_tests { let sig = Option::Some(Signature::new_bls("doesn't matter".as_bytes().to_vec())); let test_cases: Vec = vec![ - // TestCase::builder() - // .sig(sig.clone()) - // .exp_exit_code(ExitCode::Ok) - // .build() - // .unwrap(), + TestCase::builder() + .sig(sig.clone()) + .exp_exit_code(ExitCode::Ok) + .build() + .unwrap(), TestCase::builder() .amt(-1) .sig(sig.clone()) @@ -224,13 +224,12 @@ mod create_lane_tests { TestCase::builder() .sig(sig.clone()) .secret_preimage(vec![0; 2 << 21]) - .exp_exit_code(ExitCode::ErrIllegalState) .build() .unwrap(), ]; for test_case in test_cases { - println!("Executing the next test"); + println!("Executing the next test case "); let bs = MemoryDB::default(); let message = UnsignedMessage::builder() .from(*SYSTEM_ACTOR_ADDR) @@ -263,7 +262,7 @@ mod create_lane_tests { }; let ucp = UpdateChannelStateParams { - sv: sv, + sv: sv.clone(), secret: vec![], proof: vec![], }; @@ -271,6 +270,24 @@ mod create_lane_tests { rt.set_caller(test_case.target_code, payee_addr); rt.expect_validate_caller_addr(&[payer_addr, payee_addr]); + if test_case.sig.is_some() && test_case.secret_preimage.len() == 0 { + if !test_case.verify_sig { + rt.expect_verify_signature( + sv.clone().signature.unwrap(), + payer_addr, + to_vec(&sv).unwrap(), + ExitCode::ErrIllegalState, + ); + } else { + rt.expect_verify_signature( + sv.clone().signature.unwrap(), + payer_addr, + to_vec(&sv).unwrap(), + ExitCode::Ok, + ); + } + } + if test_case.exp_exit_code == ExitCode::Ok { assert!(rt .call( @@ -279,6 +296,7 @@ mod create_lane_tests { &Serialized::serialize(ucp).unwrap() ) .is_ok()); + let st: PState = rt.get_state().unwrap(); assert_eq!(st.lane_states.len(), 1); let ls = st.lane_states.first().unwrap(); @@ -402,7 +420,6 @@ fn redeem_voucher_correct_lane() { &sv.amount - BigInt::from_signed_bytes_be(&ls_to_update.redeemed.to_signed_bytes_be()); let exp_send = big_delta + BigInt::from_signed_bytes_be(&initial_amount.to_radix_be(10)); - println!("amount is {:?}", state.to_send); assert_eq!( exp_send, BigInt::from_radix_be(Sign::Plus, &state.to_send.to_radix_be(10), 10).unwrap() @@ -500,7 +517,6 @@ fn merge_failue() { let payee_addr = Address::new_id(103); for i in 0..num_test_cases { - println!("Test case {}", i); let bs = MemoryDB::default(); let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 2); rt.balance = TokenAmount::from(balance_vec[i] as u64); @@ -794,7 +810,7 @@ fn succeed_correct_secret() { rt.verify(); } -//#[test] +#[test] fn incorrect_secret() { let bs = MemoryDB::default(); let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); @@ -804,7 +820,10 @@ fn incorrect_secret() { secret: b"Profesr".to_vec(), sv: sv.clone(), }; - ucp.sv.secret_pre_image = b"Magneto".to_vec(); + let mut mag = b"Magneto".to_vec(); + let mut empty = vec![0; 25]; + mag.append(&mut empty); + ucp.sv.secret_pre_image = mag; rt.expect_validate_caller_addr(&[state.from, state.to]); rt.expect_verify_signature( ucp.sv.clone().signature.unwrap(), @@ -888,6 +907,123 @@ fn settle_if_height_less() { assert_eq!(state.settling_at, ucp.sv.min_settle_height); } +#[test] +fn happy_path() { + let bs = MemoryDB::default(); + let (mut rt, _sv) = require_create_cannel_with_lanes(&bs, 1); + rt.epoch = 10; + let mut state: PState = rt.get_state().unwrap(); + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + rt.expect_validate_caller_addr(&[state.from, state.to]); + assert!(rt + .call( + &PAYCH_ACTOR_CODE_ID.clone(), + Method::Settle as u64, + &Serialized::default() + ) + .is_ok()); + state = rt.get_state().unwrap(); + assert_eq!(state.settling_at, 11); + rt.expect_validate_caller_addr(&[state.from, state.to]); + rt.epoch = 12; + let bal = &rt.balance; + let sent_to_from = bal - state.to_send.clone(); + rt.expect_send( + state.from, + METHOD_SEND, + Serialized::default(), + sent_to_from, + Serialized::default(), + ExitCode::Ok, + ); + rt.expect_send( + state.to, + METHOD_SEND, + Serialized::default(), + state.to_send, + Serialized::default(), + ExitCode::Ok, + ); + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.to); + rt.expect_validate_caller_addr(&[state.from, state.to]); + assert!(rt + .call( + &PAYCH_ACTOR_CODE_ID.clone(), + Method::Collect as u64, + &Serialized::default() + ) + .is_ok()); + state = rt.get_state().unwrap(); + assert_eq!(state.to_send, TokenAmount::from(0u8)); +} + +#[test] +fn actor_collect() { + let dont_settle_vec = vec![true, false, false]; + let exp_send_from_vec = vec![ExitCode::Ok, ExitCode::ErrPlaceholder, ExitCode::Ok]; + let exp_send_to_vec = vec![ExitCode::Ok, ExitCode::Ok, ExitCode::ErrPlaceholder]; + let exp_collect_vec = vec![ + ExitCode::ErrForbidden, + ExitCode::ErrPlaceholder, + ExitCode::ErrPlaceholder, + ]; + let num_test_cases = dont_settle_vec.len(); + + for i in 0..num_test_cases { + let bs = MemoryDB::default(); + let (mut rt, _sv) = require_create_cannel_with_lanes(&bs, 1); + rt.epoch = 10; + let mut state: PState = rt.get_state().unwrap(); + + if !dont_settle_vec[i] { + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + rt.expect_validate_caller_addr(&[state.from, state.to]); + assert!(rt + .call( + &PAYCH_ACTOR_CODE_ID.clone(), + Method::Settle as u64, + &Serialized::default() + ) + .is_ok()); + state = rt.get_state().unwrap(); + assert_eq!(state.settling_at, 11); + } + + // "wait" for SettlingAt epoch + rt.epoch = 12; + + let sent_to_from = &rt.balance - state.to_send.clone(); + rt.expect_send( + state.from, + METHOD_SEND, + Serialized::default(), + sent_to_from, + Serialized::default(), + exp_send_from_vec[i], + ); + rt.expect_send( + state.to, + METHOD_SEND, + Serialized::default(), + state.to_send, + Serialized::default(), + exp_send_to_vec[i], + ); + + // Collect. + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + rt.expect_validate_caller_addr(&[state.from, state.to]); + let error = rt + .call( + &PAYCH_ACTOR_CODE_ID.clone(), + Method::Collect as u64, + &Serialized::default(), + ) + .unwrap_err(); + assert_eq!(exp_collect_vec[i], error.exit_code()); + } +} + fn require_create_cannel_with_lanes<'a, BS: BlockStore>( bs: &'a BS, num_lanes: u64, From 4953fe54021f38287b3648ee386f3385825aae02 Mon Sep 17 00:00:00 2001 From: DragonMural Date: Wed, 10 Jun 2020 00:40:50 -0400 Subject: [PATCH 11/29] Adding warning clean up --- vm/actor/tests/account_actor_test.rs | 2 -- vm/actor/tests/common/mod.rs | 13 ++++--------- vm/actor/tests/cron_actor_test.rs | 2 -- vm/actor/tests/init_actor_test.rs | 2 -- vm/actor/tests/paych_actor_test.rs | 29 +++++++++++++--------------- vm/actor/tests/reward_actor_test.rs | 1 - 6 files changed, 17 insertions(+), 32 deletions(-) diff --git a/vm/actor/tests/account_actor_test.rs b/vm/actor/tests/account_actor_test.rs index db9a4b3e4ca9..fb6edbea03e2 100644 --- a/vm/actor/tests/account_actor_test.rs +++ b/vm/actor/tests/account_actor_test.rs @@ -10,8 +10,6 @@ use db::MemoryDB; use message::UnsignedMessage; use vm::{ExitCode, Serialized}; -use interpreter::DefaultSyscalls; - macro_rules! account_tests { ($($name:ident: $value:expr,)*) => { $( diff --git a/vm/actor/tests/common/mod.rs b/vm/actor/tests/common/mod.rs index 8e6a155d5df4..a36f0e5b859b 100644 --- a/vm/actor/tests/common/mod.rs +++ b/vm/actor/tests/common/mod.rs @@ -20,16 +20,13 @@ use vm::{ActorError, ExitCode, MethodNum, Randomness, Serialized, TokenAmount}; //use interpreter::gas_syscalls::GasSyscalls; use encoding::blake2b_256; use fil_types::{ - zero_piece_commitment, PaddedPieceSize, PieceInfo, RegisteredProof, SealVerifyInfo, SectorInfo, + PieceInfo, RegisteredProof, SealVerifyInfo, WindowPoStVerifyInfo, }; -use interpreter::{internal_send, ChainRand, DefaultRuntime, DefaultSyscalls, GasSyscalls}; -use interpreter::{price_list_by_epoch, GasTracker}; -use runtime::{ConsensusFault, ConsensusFaultType}; + +use runtime::{ConsensusFault}; use std::error::Error as StdError; -use std::rc::Rc; -//use super::gas_tracker::{price_list_by_epoch, GasTracker, PriceList}; pub struct MockRuntime<'a, BS: BlockStore> { pub epoch: ChainEpoch, @@ -92,9 +89,7 @@ where BS: BlockStore, { pub fn new(bs: &'a BS, message: UnsignedMessage) -> Self { - let price_list = price_list_by_epoch(0); - let gas_tracker = Rc::new(RefCell::new(GasTracker::new(message.gas_limit() as i64, 0))); - + MockRuntime { epoch: 0, caller_type: Cid::default(), diff --git a/vm/actor/tests/cron_actor_test.rs b/vm/actor/tests/cron_actor_test.rs index 04713c5ae294..651bdba078f7 100644 --- a/vm/actor/tests/cron_actor_test.rs +++ b/vm/actor/tests/cron_actor_test.rs @@ -9,10 +9,8 @@ use actor::{ use address::Address; use common::*; use db::MemoryDB; -use interpreter::DefaultSyscalls; use ipld_blockstore::BlockStore; use message::UnsignedMessage; -use runtime::Syscalls; use vm::{ExitCode, Serialized}; fn construct_runtime<'a, BS: BlockStore>(bs: &'a BS) -> MockRuntime<'a, BS> { diff --git a/vm/actor/tests/init_actor_test.rs b/vm/actor/tests/init_actor_test.rs index a8eda4045151..3a7308f9a4d8 100644 --- a/vm/actor/tests/init_actor_test.rs +++ b/vm/actor/tests/init_actor_test.rs @@ -13,10 +13,8 @@ use address::Address; use cid::Cid; use common::*; use db::MemoryDB; -use interpreter::DefaultSyscalls; use ipld_blockstore::BlockStore; use message::{Message, UnsignedMessage}; -use runtime::Syscalls; use serde::Serialize; use vm::{ActorError, ExitCode, Serialized, TokenAmount, METHOD_CONSTRUCTOR}; diff --git a/vm/actor/tests/paych_actor_test.rs b/vm/actor/tests/paych_actor_test.rs index 2327ad74f416..c0c2f33b47d7 100644 --- a/vm/actor/tests/paych_actor_test.rs +++ b/vm/actor/tests/paych_actor_test.rs @@ -9,8 +9,7 @@ use actor::{ SignedVoucher, State as PState, UpdateChannelStateParams, LANE_LIMIT, SETTLE_DELAY, }, ACCOUNT_ACTOR_CODE_ID, INIT_ACTOR_ADDR, INIT_ACTOR_CODE_ID, MULTISIG_ACTOR_CODE_ID, - PAYCH_ACTOR_CODE_ID, REWARD_ACTOR_ADDR, REWARD_ACTOR_CODE_ID, SYSTEM_ACTOR_ADDR, - SYSTEM_ACTOR_CODE_ID, + PAYCH_ACTOR_CODE_ID, SYSTEM_ACTOR_ADDR }; use address::Address; use cid::Cid; @@ -20,14 +19,12 @@ use crypto::Signature; use db::MemoryDB; use derive_builder::Builder; use encoding::to_vec; -use interpreter::{internal_send, ChainRand, DefaultRuntime, DefaultSyscalls, GasSyscalls}; use ipld_blockstore::BlockStore; use message::UnsignedMessage; use num_bigint::{BigInt, Sign}; -use runtime::{ActorCode, Runtime, Syscalls}; use vm::{ExitCode, Serialized, TokenAmount, METHOD_CONSTRUCTOR, METHOD_SEND}; -struct lane_params { +struct LaneParams { epoch_num: ChainEpoch, from: Address, to: Address, @@ -433,7 +430,7 @@ fn merge_success() { let num_lanes = 3; let bs = MemoryDB::default(); let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, num_lanes); - let mut state: PState = rt.get_state().unwrap(); + let state: PState = rt.get_state().unwrap(); let state_2: PState = rt.get_state().unwrap(); rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); @@ -733,7 +730,7 @@ fn extra_call_fail() { #[test] fn update_channel_setting() { let bs = MemoryDB::default(); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); + let (mut rt, sv) = require_create_cannel_with_lanes(&bs, 1); rt.epoch = 10; let state: PState = rt.get_state().unwrap(); rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); @@ -786,9 +783,9 @@ fn update_channel_setting() { #[test] fn succeed_correct_secret() { let bs = MemoryDB::default(); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); + let (mut rt, sv) = require_create_cannel_with_lanes(&bs, 1); let state: PState = rt.get_state().unwrap(); - let mut ucp = UpdateChannelStateParams { + let ucp = UpdateChannelStateParams { proof: vec![], secret: vec![], sv: sv.clone(), @@ -813,7 +810,7 @@ fn succeed_correct_secret() { #[test] fn incorrect_secret() { let bs = MemoryDB::default(); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); + let (mut rt, sv) = require_create_cannel_with_lanes(&bs, 1); let state: PState = rt.get_state().unwrap(); let mut ucp = UpdateChannelStateParams { proof: vec![], @@ -845,7 +842,7 @@ fn incorrect_secret() { #[test] fn adjust_settling_at() { let bs = MemoryDB::default(); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); + let (mut rt, _sv) = require_create_cannel_with_lanes(&bs, 1); let ep = 10; rt.epoch = ep; let mut state: PState = rt.get_state().unwrap(); @@ -872,7 +869,7 @@ fn settle_if_height_less() { rt.epoch = ep; let mut state: PState = rt.get_state().unwrap(); sv.min_settle_height = (ep + SETTLE_DELAY) + 1; - let mut ucp = UpdateChannelStateParams { + let ucp = UpdateChannelStateParams { proof: vec![], secret: vec![], sv: sv.clone(), @@ -1057,7 +1054,7 @@ fn require_create_cannel_with_lanes<'a, BS: BlockStore>( let mut last_sv = SignedVoucher::default(); for i in 0..num_lanes { - let lane_param = lane_params { + let lane_param = LaneParams { epoch_num: curr_epoch, from: payer_addr, to: payee_addr, @@ -1074,7 +1071,7 @@ fn require_create_cannel_with_lanes<'a, BS: BlockStore>( fn require_add_new_lane( rt: &mut MockRuntime<'_, BS>, - param: lane_params, + param: LaneParams, ) -> SignedVoucher { let payee_addr = Address::new_id(103); let sig = Signature::new_bls(vec![0, 1, 2, 3, 4, 5, 6, 7]); @@ -1135,13 +1132,13 @@ fn construct_and_verify( to: receiver, }; rt.expect_validate_caller_type(&[INIT_ACTOR_CODE_ID.clone()]); - let v = rt + assert!( rt .call( &PAYCH_ACTOR_CODE_ID, METHOD_CONSTRUCTOR, &Serialized::serialize(¶ms).unwrap(), ) - .unwrap(); + .is_ok()); rt.verify(); verify_initial_state(rt, sender, receiver); } diff --git a/vm/actor/tests/reward_actor_test.rs b/vm/actor/tests/reward_actor_test.rs index 353c8e8c1372..f4b66f8643eb 100644 --- a/vm/actor/tests/reward_actor_test.rs +++ b/vm/actor/tests/reward_actor_test.rs @@ -10,7 +10,6 @@ use actor::{ use address::Address; use common::*; use db::MemoryDB; -use interpreter::DefaultSyscalls; use ipld_blockstore::BlockStore; use message::UnsignedMessage; From d90af27375a2a66563e078849ec399f8d1e3a197 Mon Sep 17 00:00:00 2001 From: DragonMural Date: Wed, 10 Jun 2020 00:50:13 -0400 Subject: [PATCH 12/29] Getting rid of etxra print statements --- vm/actor/tests/common/mod.rs | 1 - vm/actor/tests/paych_actor_test.rs | 5 ----- 2 files changed, 6 deletions(-) diff --git a/vm/actor/tests/common/mod.rs b/vm/actor/tests/common/mod.rs index a36f0e5b859b..509b9bd63fef 100644 --- a/vm/actor/tests/common/mod.rs +++ b/vm/actor/tests/common/mod.rs @@ -590,7 +590,6 @@ where && exp.signer == *_signer && (exp.plaintext[..]) == *_plaintext { - //println!("Exp is {:?}",exp); if exp.result == ExitCode::Ok { return Ok(()); } else { diff --git a/vm/actor/tests/paych_actor_test.rs b/vm/actor/tests/paych_actor_test.rs index c0c2f33b47d7..eb12dc0ecd70 100644 --- a/vm/actor/tests/paych_actor_test.rs +++ b/vm/actor/tests/paych_actor_test.rs @@ -226,7 +226,6 @@ mod create_lane_tests { ]; for test_case in test_cases { - println!("Executing the next test case "); let bs = MemoryDB::default(); let message = UnsignedMessage::builder() .from(*SYSTEM_ACTOR_ADDR) @@ -320,7 +319,6 @@ mod create_lane_tests { &Serialized::serialize(ucp).unwrap(), ) .unwrap_err(); - println!("error message is {}", error.msg()); assert_eq!(error.exit_code(), test_case.exp_exit_code); verify_initial_state(&mut rt, payer_addr, payee_addr); } @@ -549,7 +547,6 @@ fn merge_failue() { &Serialized::serialize(ucp).unwrap(), ) .unwrap_err(); - println!("Message is {:?}", v.msg()); assert_eq!(v.exit_code(), exit_vec[i]); rt.verify(); } @@ -1075,7 +1072,6 @@ fn require_add_new_lane( ) -> SignedVoucher { let payee_addr = Address::new_id(103); let sig = Signature::new_bls(vec![0, 1, 2, 3, 4, 5, 6, 7]); - // println!("sig is {:?}", sig.bytes()); let sv = SignedVoucher { time_lock_min: param.epoch_num, time_lock_max: u64::MAX, @@ -1091,7 +1087,6 @@ fn require_add_new_lane( rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), param.from); rt.expect_validate_caller_addr(&[param.from, param.to]); rt.expect_verify_signature(sig.clone(), payee_addr, to_vec(&sv).unwrap(), ExitCode::Ok); - println!(" sv vec bytes is {:?}", to_vec(&sv).unwrap()); let ucp = UpdateChannelStateParams { proof: vec![], From ab9f38feb356f63c5edddbea6d4003a65d57f393 Mon Sep 17 00:00:00 2001 From: DragonMural Date: Wed, 10 Jun 2020 10:59:27 -0400 Subject: [PATCH 13/29] Fmt changes --- vm/actor/tests/common/mod.rs | 9 ++------- vm/actor/tests/paych_actor_test.rs | 4 ++-- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/vm/actor/tests/common/mod.rs b/vm/actor/tests/common/mod.rs index 509b9bd63fef..54647a7a9b00 100644 --- a/vm/actor/tests/common/mod.rs +++ b/vm/actor/tests/common/mod.rs @@ -19,15 +19,11 @@ use std::collections::{HashMap, VecDeque}; use vm::{ActorError, ExitCode, MethodNum, Randomness, Serialized, TokenAmount}; //use interpreter::gas_syscalls::GasSyscalls; use encoding::blake2b_256; -use fil_types::{ - PieceInfo, RegisteredProof, SealVerifyInfo, - WindowPoStVerifyInfo, -}; +use fil_types::{PieceInfo, RegisteredProof, SealVerifyInfo, WindowPoStVerifyInfo}; -use runtime::{ConsensusFault}; +use runtime::ConsensusFault; use std::error::Error as StdError; - pub struct MockRuntime<'a, BS: BlockStore> { pub epoch: ChainEpoch, pub caller_type: Cid, @@ -89,7 +85,6 @@ where BS: BlockStore, { pub fn new(bs: &'a BS, message: UnsignedMessage) -> Self { - MockRuntime { epoch: 0, caller_type: Cid::default(), diff --git a/vm/actor/tests/paych_actor_test.rs b/vm/actor/tests/paych_actor_test.rs index eb12dc0ecd70..272a242ebc98 100644 --- a/vm/actor/tests/paych_actor_test.rs +++ b/vm/actor/tests/paych_actor_test.rs @@ -9,7 +9,7 @@ use actor::{ SignedVoucher, State as PState, UpdateChannelStateParams, LANE_LIMIT, SETTLE_DELAY, }, ACCOUNT_ACTOR_CODE_ID, INIT_ACTOR_ADDR, INIT_ACTOR_CODE_ID, MULTISIG_ACTOR_CODE_ID, - PAYCH_ACTOR_CODE_ID, SYSTEM_ACTOR_ADDR + PAYCH_ACTOR_CODE_ID, SYSTEM_ACTOR_ADDR, }; use address::Address; use cid::Cid; @@ -1127,7 +1127,7 @@ fn construct_and_verify( to: receiver, }; rt.expect_validate_caller_type(&[INIT_ACTOR_CODE_ID.clone()]); - assert!( rt + assert!(rt .call( &PAYCH_ACTOR_CODE_ID, METHOD_CONSTRUCTOR, From 45398d9fd415f500aa01aa9b030f2969429e6019 Mon Sep 17 00:00:00 2001 From: DragonMural Date: Wed, 10 Jun 2020 11:26:25 -0400 Subject: [PATCH 14/29] Removed println statements --- vm/actor/src/builtin/paych/mod.rs | 5 ----- vm/interpreter/src/gas_syscalls.rs | 2 -- 2 files changed, 7 deletions(-) diff --git a/vm/actor/src/builtin/paych/mod.rs b/vm/actor/src/builtin/paych/mod.rs index a766f2ca5805..3b1ee476dcf2 100644 --- a/vm/actor/src/builtin/paych/mod.rs +++ b/vm/actor/src/builtin/paych/mod.rs @@ -108,8 +108,6 @@ impl Actor { ) })?; - //println!("Passing sig line 112 sv_bz is {:?}", sv_bz.to_vec()); - // Validate signature rt.syscalls() .verify_signature(&sig, &signer, &sv_bz) @@ -120,8 +118,6 @@ impl Actor { ) })?; - println!("Passing sig line 124"); - if rt.curr_epoch() < sv.time_lock_min { return Err(rt.abort(ExitCode::ErrIllegalArgument, "cannot use this voucher yet")); } @@ -186,7 +182,6 @@ impl Actor { // 1. (optional) sum already redeemed value of all merging lanes let mut redeemed = BigInt::default(); for merge in sv.merges { - println!("IN for loop lanes are {:?} and {:?}", merge.lane, sv.lane); if merge.lane == sv.lane { return Err(ActorError::new( ExitCode::ErrIllegalArgument, diff --git a/vm/interpreter/src/gas_syscalls.rs b/vm/interpreter/src/gas_syscalls.rs index e6f28ae6294d..238b240888d1 100644 --- a/vm/interpreter/src/gas_syscalls.rs +++ b/vm/interpreter/src/gas_syscalls.rs @@ -30,7 +30,6 @@ where signer: &Address, plaintext: &[u8], ) -> Result<(), Box> { - println!("In this func"); self.gas .borrow_mut() .charge_gas( @@ -38,7 +37,6 @@ where .on_verify_signature(signature.signature_type(), plaintext.len()), ) .unwrap(); - println!("Passed this partr"); self.syscalls.verify_signature(signature, signer, plaintext) } fn hash_blake2b(&self, data: &[u8]) -> Result<[u8; 32], Box> { From 14e408572c6021385015c3075142684b6857d7c8 Mon Sep 17 00:00:00 2001 From: DragonMural Date: Wed, 10 Jun 2020 15:47:43 -0400 Subject: [PATCH 15/29] Adding touch ups --- vm/actor/Cargo.toml | 1 - vm/actor/tests/common/mod.rs | 5 ++--- vm/actor/tests/paych_actor_test.rs | 20 ++++++++------------ vm/interpreter/src/gas_syscalls.rs | 2 +- vm/interpreter/src/lib.rs | 6 ++---- 5 files changed, 13 insertions(+), 21 deletions(-) diff --git a/vm/actor/Cargo.toml b/vm/actor/Cargo.toml index 13eb78afae1e..e98032f2027e 100644 --- a/vm/actor/Cargo.toml +++ b/vm/actor/Cargo.toml @@ -28,7 +28,6 @@ fil_types = { path = "../../types" } derive_builder = "0.9" [dev-dependencies] -interpreter = { path = "../interpreter/" } db = { path = "../../node/db" } hex = "0.4.2" libp2p = "0.19" diff --git a/vm/actor/tests/common/mod.rs b/vm/actor/tests/common/mod.rs index 54647a7a9b00..5c78f37986b4 100644 --- a/vm/actor/tests/common/mod.rs +++ b/vm/actor/tests/common/mod.rs @@ -10,16 +10,15 @@ use address::Address; use cid::{multihash::Blake2b256, Cid}; use clock::ChainEpoch; use crypto::{DomainSeparationTag, Signature}; +use encoding::blake2b_256; use encoding::{de::DeserializeOwned, Cbor}; +use fil_types::{PieceInfo, RegisteredProof, SealVerifyInfo, WindowPoStVerifyInfo}; use ipld_blockstore::BlockStore; use message::{Message, UnsignedMessage}; use runtime::{ActorCode, Runtime, Syscalls}; use std::cell::{Cell, RefCell}; use std::collections::{HashMap, VecDeque}; use vm::{ActorError, ExitCode, MethodNum, Randomness, Serialized, TokenAmount}; -//use interpreter::gas_syscalls::GasSyscalls; -use encoding::blake2b_256; -use fil_types::{PieceInfo, RegisteredProof, SealVerifyInfo, WindowPoStVerifyInfo}; use runtime::ConsensusFault; use std::error::Error as StdError; diff --git a/vm/actor/tests/paych_actor_test.rs b/vm/actor/tests/paych_actor_test.rs index 272a242ebc98..1dae7e65d248 100644 --- a/vm/actor/tests/paych_actor_test.rs +++ b/vm/actor/tests/paych_actor_test.rs @@ -464,30 +464,26 @@ fn merge_success() { .is_ok()); rt.verify(); - let (sv_amount_sign, sv_amount_bytes) = &sv.amount.to_bytes_be(); - let exp_merge_to = LaneState { id: merge_to.id, - redeemed: BigInt::from_bytes_be(*sv_amount_sign, &sv_amount_bytes), + redeemed: sv.amount.clone(), nonce: sv.nonce, }; - let (from_sign, from_bytes) = &merge_from.redeemed.to_bytes_be(); - let exp_merge_from = LaneState { id: merge_from.id, - redeemed: BigInt::from_bytes_be(*from_sign, &from_bytes), + redeemed: merge_from.redeemed.clone(), nonce: merge_nonce, }; - let (to_sign, to_bytes) = &merge_to.redeemed.to_bytes_be(); + let redeemed = &merge_from.redeemed + &merge_to.redeemed; + + let exp_delta = &sv.amount - &redeemed; - let redeemed = - BigInt::from_bytes_be(*from_sign, &from_bytes) + BigInt::from_bytes_be(*to_sign, &to_bytes); - let exp_delta = sv.amount - redeemed; - let exp_send_amt = BigInt::from_bytes_be(Sign::Plus, &state.to_send.to_bytes_be()) + exp_delta; let mut exp_state = state_2; - exp_state.to_send = TokenAmount::from_bytes_be(&exp_send_amt.to_signed_bytes_be()); + + exp_state.to_send = exp_delta.to_biguint().unwrap() + &state.to_send; + exp_state.lane_states = vec![ exp_merge_to, exp_merge_from, diff --git a/vm/interpreter/src/gas_syscalls.rs b/vm/interpreter/src/gas_syscalls.rs index 238b240888d1..6df72b74d323 100644 --- a/vm/interpreter/src/gas_syscalls.rs +++ b/vm/interpreter/src/gas_syscalls.rs @@ -14,7 +14,7 @@ use std::error::Error as StdError; use std::rc::Rc; /// Syscall wrapper to charge gas on syscalls -pub struct GasSyscalls<'sys, S> { +pub(crate) struct GasSyscalls<'sys, S> { pub price_list: PriceList, pub gas: Rc>, pub syscalls: &'sys S, diff --git a/vm/interpreter/src/lib.rs b/vm/interpreter/src/lib.rs index 7333384ff1e4..3ef6a2ea2833 100644 --- a/vm/interpreter/src/lib.rs +++ b/vm/interpreter/src/lib.rs @@ -4,13 +4,11 @@ mod default_runtime; mod default_syscalls; mod gas_block_store; -pub mod gas_syscalls; -pub mod gas_tracker; +mod gas_syscalls; +mod gas_tracker; mod rand; mod vm; pub use self::default_runtime::*; pub use self::default_syscalls::DefaultSyscalls; -pub use self::gas_syscalls::*; -pub use self::gas_tracker::*; pub use self::rand::*; pub use self::vm::*; From 02d0332b450ed05e6a1deb8311e02633f2f563ce Mon Sep 17 00:00:00 2001 From: DragonMural Date: Thu, 11 Jun 2020 21:02:20 -0400 Subject: [PATCH 16/29] Adding PR requested changes --- vm/actor/src/builtin/paych/mod.rs | 4 +-- vm/actor/src/builtin/paych/state.rs | 2 +- vm/actor/tests/common/mod.rs | 34 ++++++++++++-------------- vm/actor/tests/paych_actor_test.rs | 38 +++++++++++++++++++++++------ vm/actor/tests/reward_actor_test.rs | 1 - 5 files changed, 49 insertions(+), 30 deletions(-) diff --git a/vm/actor/src/builtin/paych/mod.rs b/vm/actor/src/builtin/paych/mod.rs index 3b1ee476dcf2..57d4fcb6a918 100644 --- a/vm/actor/src/builtin/paych/mod.rs +++ b/vm/actor/src/builtin/paych/mod.rs @@ -97,7 +97,7 @@ impl Actor { // Pull signature from signed voucher let sig = sv .signature - .clone() + .as_ref() .ok_or_else(|| rt.abort(ExitCode::ErrIllegalArgument, "voucher has no signature"))?; // Generate unsigned bytes @@ -349,7 +349,7 @@ impl ActorCode for Actor { Ok(Serialized::default()) } Some(Method::UpdateChannelState) => { - Self::update_channel_state(rt, params.deserialize().unwrap())?; + Self::update_channel_state(rt, params.deserialize()?)?; Ok(Serialized::default()) } _ => Err(rt.abort(ExitCode::SysErrInvalidMethod, "Invalid method")), diff --git a/vm/actor/src/builtin/paych/state.rs b/vm/actor/src/builtin/paych/state.rs index f0c01fc04131..264ef4f3c2cc 100644 --- a/vm/actor/src/builtin/paych/state.rs +++ b/vm/actor/src/builtin/paych/state.rs @@ -54,7 +54,7 @@ pub struct LaneState { } /// Specifies which `lane`s to be merged with what `nonce` on `channel_update` -#[derive(Default, Clone, Debug, PartialEq, Serialize_tuple, Deserialize_tuple)] +#[derive(Default, Clone, Copy, Debug, PartialEq, Serialize_tuple, Deserialize_tuple)] pub struct Merge { pub lane: u64, pub nonce: u64, diff --git a/vm/actor/tests/common/mod.rs b/vm/actor/tests/common/mod.rs index 5c78f37986b4..7969a92e8e3b 100644 --- a/vm/actor/tests/common/mod.rs +++ b/vm/actor/tests/common/mod.rs @@ -10,18 +10,15 @@ use address::Address; use cid::{multihash::Blake2b256, Cid}; use clock::ChainEpoch; use crypto::{DomainSeparationTag, Signature}; -use encoding::blake2b_256; -use encoding::{de::DeserializeOwned, Cbor}; +use encoding::{blake2b_256, de::DeserializeOwned, Cbor}; use fil_types::{PieceInfo, RegisteredProof, SealVerifyInfo, WindowPoStVerifyInfo}; use ipld_blockstore::BlockStore; use message::{Message, UnsignedMessage}; -use runtime::{ActorCode, Runtime, Syscalls}; +use runtime::{ActorCode, ConsensusFault, Runtime, Syscalls}; use std::cell::{Cell, RefCell}; use std::collections::{HashMap, VecDeque}; -use vm::{ActorError, ExitCode, MethodNum, Randomness, Serialized, TokenAmount}; - -use runtime::ConsensusFault; use std::error::Error as StdError; +use vm::{ActorError, ExitCode, MethodNum, Randomness, Serialized, TokenAmount}; pub struct MockRuntime<'a, BS: BlockStore> { pub epoch: ChainEpoch, @@ -150,7 +147,6 @@ where *self.expect_validate_caller_type.borrow_mut() = Some(ids.to_vec()); } - #[allow(dead_code)] pub fn expect_verify_signature( &mut self, sig: Signature, @@ -406,8 +402,13 @@ where if address.protocol() == address::Protocol::ID { return Ok(address.clone()); } - let resolved = self.id_addresses.get(&address).unwrap(); - return Ok(resolved.clone()); + if let Some(resolved) = self.id_addresses.get(&address) { + return Ok(resolved.clone()); + } + Err(ActorError::new( + ExitCode::ErrIllegalArgument, + "Address not found".to_string(), + )) } fn get_actor_code_cid(&self, addr: &Address) -> Result { @@ -573,17 +574,14 @@ where { fn verify_signature( &self, - _signature: &Signature, - _signer: &Address, - _plaintext: &[u8], + signature: &Signature, + signer: &Address, + plaintext: &[u8], ) -> Result<(), Box> { let op_exp = self.expect_verify_sig.replace(Option::None); if let Some(exp) = op_exp { - if exp.sig == *_signature - && exp.signer == *_signer - && (exp.plaintext[..]) == *_plaintext - { + if exp.sig == *signature && exp.signer == *signer && (exp.plaintext[..]) == *plaintext { if exp.result == ExitCode::Ok { return Ok(()); } else { @@ -606,8 +604,8 @@ where } } - fn hash_blake2b(&self, _data: &[u8]) -> Result<[u8; 32], Box> { - Ok(blake2b_256(&_data)) + fn hash_blake2b(&self, data: &[u8]) -> Result<[u8; 32], Box> { + Ok(blake2b_256(&data)) } fn compute_unsealed_sector_cid( &self, diff --git a/vm/actor/tests/paych_actor_test.rs b/vm/actor/tests/paych_actor_test.rs index 1dae7e65d248..b73a3cc71c3a 100644 --- a/vm/actor/tests/paych_actor_test.rs +++ b/vm/actor/tests/paych_actor_test.rs @@ -100,13 +100,22 @@ fn actor_constructor_fails() { expected_exit_code: ExitCode, } - let test_cases: Vec = vec![TestCase { - paych_addr: paych_addr, - caller_code: INIT_ACTOR_CODE_ID.clone(), - new_actor_code: MULTISIG_ACTOR_CODE_ID.clone(), - payer_code: ACCOUNT_ACTOR_CODE_ID.clone(), - expected_exit_code: ExitCode::ErrIllegalArgument, - }]; + let test_cases: Vec = vec![ + TestCase { + paych_addr: paych_addr, + caller_code: INIT_ACTOR_CODE_ID.clone(), + new_actor_code: MULTISIG_ACTOR_CODE_ID.clone(), + payer_code: ACCOUNT_ACTOR_CODE_ID.clone(), + expected_exit_code: ExitCode::ErrIllegalArgument, + }, + TestCase { + paych_addr: Address::new_secp256k1(&vec![b'A'; 65][..]).unwrap(), + caller_code: INIT_ACTOR_CODE_ID.clone(), + new_actor_code: ACCOUNT_ACTOR_CODE_ID.clone(), + payer_code: ACCOUNT_ACTOR_CODE_ID.clone(), + expected_exit_code: ExitCode::ErrIllegalArgument, + }, + ]; for test_case in test_cases { let bs = MemoryDB::default(); @@ -141,6 +150,7 @@ mod create_lane_tests { #[derive(Builder, Debug)] #[builder(name = "TestCaseBuilder")] struct TestCase { + desc: String, #[builder(default = "ACCOUNT_ACTOR_CODE_ID.clone()")] target_code: Cid, #[builder(default)] @@ -185,40 +195,51 @@ mod create_lane_tests { let test_cases: Vec = vec![ TestCase::builder() + .desc("succeds".to_string()) .sig(sig.clone()) .exp_exit_code(ExitCode::Ok) .build() .unwrap(), TestCase::builder() + .desc("fails if new send balance is negative".to_string()) .amt(-1) .sig(sig.clone()) .exp_exit_code(ExitCode::ErrIllegalState) .build() .unwrap(), TestCase::builder() + .desc("fails if balance too low".to_string()) .amt(10) .sig(sig.clone()) .exp_exit_code(ExitCode::ErrIllegalState) .build() .unwrap(), - TestCase::builder().sig(Option::None).build().unwrap(), TestCase::builder() + .desc("fails is signature is not valid".to_string()) + .sig(Option::None) + .build() + .unwrap(), + TestCase::builder() + .desc("fails if too early for a voucher".to_string()) .tl_min(10) .sig(sig.clone()) .build() .unwrap(), TestCase::builder() + .desc("fails is beyond timelockmax".to_string()) .epoch(10) .tl_max(5) .sig(sig.clone()) .build() .unwrap(), TestCase::builder() + .desc("fails if signature is not verified".to_string()) .sig(sig.clone()) .verify_sig(false) .build() .unwrap(), TestCase::builder() + .desc("Fails if signing fails".to_string()) .sig(sig.clone()) .secret_preimage(vec![0; 2 << 21]) .build() @@ -226,6 +247,7 @@ mod create_lane_tests { ]; for test_case in test_cases { + println!("Test Description {}", test_case.desc); let bs = MemoryDB::default(); let message = UnsignedMessage::builder() .from(*SYSTEM_ACTOR_ADDR) diff --git a/vm/actor/tests/reward_actor_test.rs b/vm/actor/tests/reward_actor_test.rs index f4b66f8643eb..216b1307134f 100644 --- a/vm/actor/tests/reward_actor_test.rs +++ b/vm/actor/tests/reward_actor_test.rs @@ -12,7 +12,6 @@ use common::*; use db::MemoryDB; use ipld_blockstore::BlockStore; use message::UnsignedMessage; - use std::panic; use vm::{Serialized, TokenAmount, METHOD_CONSTRUCTOR}; From eef423cd6c977c993ed6c9150aa9367fdac708a7 Mon Sep 17 00:00:00 2001 From: DragonMural Date: Thu, 11 Jun 2020 21:27:13 -0400 Subject: [PATCH 17/29] MIssing update --- vm/actor/tests/common/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/vm/actor/tests/common/mod.rs b/vm/actor/tests/common/mod.rs index 1e9c567d185a..bd84d720ddd4 100644 --- a/vm/actor/tests/common/mod.rs +++ b/vm/actor/tests/common/mod.rs @@ -625,7 +625,6 @@ where _h1: &[u8], _h2: &[u8], _extra: &[u8], - _earliest: ChainEpoch, ) -> Result, Box> { unimplemented!(); } From d417739a30343f2fc9340a5737b76f8fb1e1d603 Mon Sep 17 00:00:00 2001 From: DragonMural Date: Tue, 16 Jun 2020 20:44:38 -0400 Subject: [PATCH 18/29] Adding requested chnages --- vm/actor/src/builtin/paych/types.rs | 10 + vm/actor/tests/common/mod.rs | 35 ++- vm/actor/tests/paych_actor_test.rs | 447 +++++++++++++++------------- 3 files changed, 272 insertions(+), 220 deletions(-) diff --git a/vm/actor/src/builtin/paych/types.rs b/vm/actor/src/builtin/paych/types.rs index 3f0aa004a7a7..9a3cb6c2ff26 100644 --- a/vm/actor/src/builtin/paych/types.rs +++ b/vm/actor/src/builtin/paych/types.rs @@ -80,6 +80,16 @@ pub struct UpdateChannelStateParams { pub proof: Vec, } +impl From<&SignedVoucher> for UpdateChannelStateParams { + fn from(sv: &SignedVoucher) -> Self { + UpdateChannelStateParams { + proof: vec![], + secret: vec![], + sv: sv.clone(), + } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/vm/actor/tests/common/mod.rs b/vm/actor/tests/common/mod.rs index bd84d720ddd4..93ef7f197f6a 100644 --- a/vm/actor/tests/common/mod.rs +++ b/vm/actor/tests/common/mod.rs @@ -147,6 +147,7 @@ where *self.expect_validate_caller_type.borrow_mut() = Some(ids.to_vec()); } + #[allow(dead_code)] pub fn expect_verify_signature( &mut self, sig: Signature, @@ -154,12 +155,12 @@ where plaintext: Vec, result: ExitCode, ) { - self.expect_verify_sig = RefCell::new(Some(ExpectedVerifySig { + *(self.expect_verify_sig.get_mut()) = Some(ExpectedVerifySig { sig: sig, signer: signer, plaintext: plaintext, result: result, - })); + }); } #[allow(dead_code)] @@ -402,24 +403,26 @@ where if address.protocol() == address::Protocol::ID { return Ok(address.clone()); } - if let Some(resolved) = self.id_addresses.get(&address) { - return Ok(resolved.clone()); - } - Err(ActorError::new( - ExitCode::ErrIllegalArgument, - "Address not found".to_string(), - )) + + self.id_addresses + .get(&address) + .map(|x| x.to_owned()) + .ok_or(ActorError::new( + ExitCode::ErrIllegalArgument, + "Address not found".to_string(), + )) } fn get_actor_code_cid(&self, addr: &Address) -> Result { self.require_in_call(); - if let Some(ret) = self.actor_code_cids.get(&addr) { - return Ok(ret.clone()); - } - Err(ActorError::new( - ExitCode::ErrIllegalArgument, - "Actor address is not found".to_string(), - )) + + self.actor_code_cids + .get(&addr) + .map(|x| x.to_owned()) + .ok_or(ActorError::new( + ExitCode::ErrIllegalArgument, + "Actor address is not found".to_string(), + )) } fn get_randomness( diff --git a/vm/actor/tests/paych_actor_test.rs b/vm/actor/tests/paych_actor_test.rs index b73a3cc71c3a..1ca52d9222b7 100644 --- a/vm/actor/tests/paych_actor_test.rs +++ b/vm/actor/tests/paych_actor_test.rs @@ -33,11 +33,16 @@ struct LaneParams { nonce: u64, } +const TEST_PAYCH_ADDR: u64 = 100; +const TEST_PAYER_ADDR: u64 = 101; +const TEST_PAYEE_ADDR: u64 = 103; +const TEST_CALLER_ADDR: u64 = 102; + #[test] fn create_paych_actor_test() { - let paych_addr = Address::new_id(100); - let payer_addr = Address::new_id(101); - let caller_addr = Address::new_id(102); + let paych_addr = Address::new_id(TEST_PAYCH_ADDR); + let payer_addr = Address::new_id(TEST_PAYER_ADDR); + let caller_addr = Address::new_id(TEST_CALLER_ADDR); let bs = MemoryDB::default(); let message = UnsignedMessage::builder() @@ -57,9 +62,9 @@ fn create_paych_actor_test() { #[test] fn actor_doesnt_exist_test() { - let paych_addr = Address::new_id(100); - let payer_addr = Address::new_id(101); - let caller_addr = Address::new_id(102); + let paych_addr = Address::new_id(TEST_PAYCH_ADDR); + let payer_addr = Address::new_id(TEST_PAYER_ADDR); + let caller_addr = Address::new_id(TEST_CALLER_ADDR); let bs = MemoryDB::default(); let message = UnsignedMessage::builder() .from(*SYSTEM_ACTOR_ADDR) @@ -78,7 +83,7 @@ fn actor_doesnt_exist_test() { let error = rt .call( - &PAYCH_ACTOR_CODE_ID.clone(), + &*PAYCH_ACTOR_CODE_ID, METHOD_CONSTRUCTOR, &Serialized::serialize(params).unwrap(), ) @@ -88,9 +93,9 @@ fn actor_doesnt_exist_test() { #[test] fn actor_constructor_fails() { - let paych_addr = Address::new_id(100); - let payer_addr = Address::new_id(101); - let caller_addr = Address::new_id(102); + let paych_addr = Address::new_id(TEST_PAYCH_ADDR); + let payer_addr = Address::new_id(TEST_PAYER_ADDR); + let caller_addr = Address::new_id(TEST_CALLER_ADDR); struct TestCase { paych_addr: Address, @@ -136,7 +141,7 @@ fn actor_constructor_fails() { }; let error = rt .call( - &PAYCH_ACTOR_CODE_ID.clone(), + &*PAYCH_ACTOR_CODE_ID, METHOD_CONSTRUCTOR, &Serialized::serialize(params).unwrap(), ) @@ -189,7 +194,7 @@ mod create_lane_tests { let init_actor_addr = Address::new_id(100); let paych_addr = Address::new_id(101); let payer_addr = Address::new_id(102); - let payee_addr = Address::new_id(103); + let payee_addr = Address::new_id(TEST_PAYEE_ADDR); let paych_balance = TokenAmount::from(9 as u64); let sig = Option::Some(Signature::new_bls("doesn't matter".as_bytes().to_vec())); @@ -309,7 +314,7 @@ mod create_lane_tests { if test_case.exp_exit_code == ExitCode::Ok { assert!(rt .call( - &PAYCH_ACTOR_CODE_ID.clone(), + &*PAYCH_ACTOR_CODE_ID, Method::UpdateChannelState as u64, &Serialized::serialize(ucp).unwrap() ) @@ -336,7 +341,7 @@ mod create_lane_tests { } else { let error = rt .call( - &PAYCH_ACTOR_CODE_ID.clone(), + &*PAYCH_ACTOR_CODE_ID, Method::UpdateChannelState as u64, &Serialized::serialize(ucp).unwrap(), ) @@ -349,100 +354,103 @@ mod create_lane_tests { } } -#[test] -fn redeem_voucher_one_lane() { - let bs = MemoryDB::default(); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); - let state: PState = rt.get_state().unwrap(); - let new_voucher_amount = BigInt::from(9); - sv.amount = new_voucher_amount; - let ucp = UpdateChannelStateParams { - proof: vec![], - secret: vec![], - sv: sv.clone(), - }; - let payee_addr = Address::new_id(103); - let payer_addr = Address::new_id(102); - rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), payee_addr); - rt.expect_validate_caller_addr(&[state.from, state.to]); - let s = sv.signature.clone().unwrap(); +mod update_channel_state_redeem { + use super::*; + #[test] + fn redeem_voucher_one_lane() { + let bs = MemoryDB::default(); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); + let state: PState = rt.get_state().unwrap(); + let new_voucher_amount = BigInt::from(9); + sv.amount = new_voucher_amount; + let ucp = UpdateChannelStateParams { + proof: vec![], + secret: vec![], + sv: sv.clone(), + }; + let payee_addr = Address::new_id(TEST_PAYEE_ADDR); + let payer_addr = Address::new_id(102); + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), payee_addr); + rt.expect_validate_caller_addr(&[state.from, state.to]); + let s = sv.signature.clone().unwrap(); - rt.expect_verify_signature(s.clone(), payer_addr, to_vec(&sv).unwrap(), ExitCode::Ok); + rt.expect_verify_signature(s.clone(), payer_addr, to_vec(&sv).unwrap(), ExitCode::Ok); - assert!(rt - .call( - &PAYCH_ACTOR_CODE_ID.clone(), - Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap(), - ) - .is_ok()); + assert!(rt + .call( + &*PAYCH_ACTOR_CODE_ID, + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap(), + ) + .is_ok()); - rt.verify(); - let exp_ls = LaneState { - id: 0, - redeemed: BigInt::from(9), - nonce: 1, - }; - let exp_state = PState { - from: state.from, - to: state.to, - to_send: TokenAmount::from(9 as u64), - settling_at: state.settling_at, - min_settle_height: state.min_settle_height, - lane_states: vec![exp_ls], - }; - verify_state(&mut rt, 1, exp_state); -} + rt.verify(); + let exp_ls = LaneState { + id: 0, + redeemed: BigInt::from(9), + nonce: 1, + }; + let exp_state = PState { + from: state.from, + to: state.to, + to_send: TokenAmount::from(9 as u64), + settling_at: state.settling_at, + min_settle_height: state.min_settle_height, + lane_states: vec![exp_ls], + }; + verify_state(&mut rt, 1, exp_state); + } -#[test] -fn redeem_voucher_correct_lane() { - let bs = MemoryDB::default(); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 3); - let state: PState = rt.get_state().unwrap(); - let initial_amount = state.to_send; - sv.amount = BigInt::from(9); - sv.lane = 1; - let ls_to_update: &LaneState = &state.lane_states[1]; - sv.nonce = ls_to_update.nonce + 1; - let payee_addr = Address::new_id(103); - let payer_addr = Address::new_id(102); + #[test] + fn redeem_voucher_correct_lane() { + let bs = MemoryDB::default(); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 3); + let state: PState = rt.get_state().unwrap(); + let initial_amount = state.to_send; + sv.amount = BigInt::from(9); + sv.lane = 1; + let ls_to_update: &LaneState = &state.lane_states[1]; + sv.nonce = ls_to_update.nonce + 1; + let payee_addr = Address::new_id(TEST_PAYEE_ADDR); + let payer_addr = Address::new_id(102); - rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), payee_addr); - rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature( - sv.clone().signature.unwrap(), - payer_addr, - to_vec(&sv).unwrap(), - ExitCode::Ok, - ); + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), payee_addr); + rt.expect_validate_caller_addr(&[state.from, state.to]); + rt.expect_verify_signature( + sv.clone().signature.unwrap(), + payer_addr, + to_vec(&sv).unwrap(), + ExitCode::Ok, + ); - let ucp = UpdateChannelStateParams { - proof: vec![], - secret: vec![], - sv: sv.clone(), - }; + let ucp = UpdateChannelStateParams { + proof: vec![], + secret: vec![], + sv: sv.clone(), + }; - assert!(rt - .call( - &PAYCH_ACTOR_CODE_ID.clone(), - Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap() - ) - .is_ok()); - rt.verify(); + assert!(rt + .call( + &*PAYCH_ACTOR_CODE_ID, + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap() + ) + .is_ok()); + rt.verify(); - let state: PState = rt.get_state().unwrap(); - let ls_updated: &LaneState = &state.lane_states[1]; - let big_delta = - &sv.amount - BigInt::from_signed_bytes_be(&ls_to_update.redeemed.to_signed_bytes_be()); - - let exp_send = big_delta + BigInt::from_signed_bytes_be(&initial_amount.to_radix_be(10)); - assert_eq!( - exp_send, - BigInt::from_radix_be(Sign::Plus, &state.to_send.to_radix_be(10), 10).unwrap() - ); - assert_eq!(sv.amount, ls_updated.redeemed); - assert_eq!(sv.nonce, ls_updated.nonce); + let state: PState = rt.get_state().unwrap(); + let ls_updated: &LaneState = &state.lane_states[1]; + let big_delta = + &sv.amount - BigInt::from_signed_bytes_be(&ls_to_update.redeemed.to_signed_bytes_be()); + + let exp_send = big_delta + BigInt::from_signed_bytes_be(&initial_amount.to_radix_be(10)); + assert_eq!( + exp_send, + BigInt::from_radix_be(Sign::Plus, &state.to_send.to_radix_be(10), 10).unwrap() + ); + assert_eq!(sv.amount, ls_updated.redeemed); + assert_eq!(sv.nonce, ls_updated.nonce); + } } #[test] @@ -464,7 +472,7 @@ fn merge_success() { }]; sv.merges = merges; - let payee_addr = Address::new_id(103); + let payee_addr = Address::new_id(TEST_PAYEE_ADDR); rt.expect_validate_caller_addr(&[state.from, state.to]); rt.expect_verify_signature( sv.clone().signature.unwrap(), @@ -472,14 +480,10 @@ fn merge_success() { to_vec(&sv).unwrap(), ExitCode::Ok, ); - let ucp = UpdateChannelStateParams { - proof: vec![], - secret: vec![], - sv: sv.clone(), - }; + let ucp = UpdateChannelStateParams::from(&sv); assert!(rt .call( - &PAYCH_ACTOR_CODE_ID.clone(), + &*PAYCH_ACTOR_CODE_ID, Method::UpdateChannelState as u64, &Serialized::serialize(ucp).unwrap() ) @@ -516,32 +520,59 @@ fn merge_success() { #[test] fn merge_failue() { - let lane_vec = vec![1, 1, 1, 0]; - let voucher_vec = vec![10, 0, 10, 10]; - let balance_vec = vec![0, 0, 1, 0]; - let merge_vec = vec![1, 10, 10, 10]; - let exit_vec = vec![ - ExitCode::ErrIllegalArgument, - ExitCode::ErrIllegalArgument, - ExitCode::ErrIllegalState, - ExitCode::ErrIllegalArgument, + struct TestCase { + lane: i32, + voucher: u64, + balance: i32, + merge: u64, + exit: ExitCode, + } + let test_cases = vec![ + TestCase { + lane: 1, + voucher: 10, + balance: 0, + merge: 1, + exit: ExitCode::ErrIllegalArgument, + }, + TestCase { + lane: 1, + voucher: 0, + balance: 0, + merge: 10, + exit: ExitCode::ErrIllegalArgument, + }, + TestCase { + lane: 1, + voucher: 10, + balance: 1, + merge: 10, + exit: ExitCode::ErrIllegalState, + }, + TestCase { + lane: 0, + voucher: 10, + balance: 0, + merge: 10, + exit: ExitCode::ErrIllegalArgument, + }, ]; - let num_test_cases = lane_vec.len(); - let payee_addr = Address::new_id(103); - for i in 0..num_test_cases { + let payee_addr = Address::new_id(TEST_PAYEE_ADDR); + + for tc in test_cases { let bs = MemoryDB::default(); let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 2); - rt.balance = TokenAmount::from(balance_vec[i] as u64); + rt.balance = TokenAmount::from(tc.balance as u64); let state: PState = rt.get_state().unwrap(); let merge_to: &LaneState = &state.lane_states[0]; - let merge_from: &LaneState = &state.lane_states[lane_vec[i]]; + let merge_from: &LaneState = &state.lane_states[tc.lane as usize]; sv.lane = merge_to.id; - sv.nonce = voucher_vec[i]; + sv.nonce = tc.voucher; sv.merges = vec![Merge { lane: merge_from.id, - nonce: merge_vec[i], + nonce: tc.merge, }]; let ucp = UpdateChannelStateParams { @@ -560,12 +591,12 @@ fn merge_failue() { ); let v = rt .call( - &PAYCH_ACTOR_CODE_ID.clone(), + &*PAYCH_ACTOR_CODE_ID, Method::UpdateChannelState as u64, &Serialized::serialize(ucp).unwrap(), ) .unwrap_err(); - assert_eq!(v.exit_code(), exit_vec[i]); + assert_eq!(v.exit_code(), tc.exit); rt.verify(); } } @@ -575,7 +606,7 @@ fn invalid_merge_lane_999() { let bs = MemoryDB::default(); let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 2); let state: PState = rt.get_state().unwrap(); - let payee_addr = Address::new_id(103); + let payee_addr = Address::new_id(TEST_PAYEE_ADDR); let merge_to: &LaneState = &state.lane_states[0]; let merge_from = LaneState { id: 999, @@ -589,11 +620,7 @@ fn invalid_merge_lane_999() { lane: merge_from.id, nonce: sv.nonce, }]; - let ucp = UpdateChannelStateParams { - proof: vec![], - secret: vec![], - sv: sv.clone(), - }; + let ucp = UpdateChannelStateParams::from(&sv); rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); rt.expect_validate_caller_addr(&[state.from, state.to]); @@ -605,7 +632,7 @@ fn invalid_merge_lane_999() { ); let v = rt .call( - &PAYCH_ACTOR_CODE_ID.clone(), + &*PAYCH_ACTOR_CODE_ID, Method::UpdateChannelState as u64, &Serialized::serialize(ucp).unwrap(), ) @@ -619,15 +646,11 @@ fn lane_limit_exceeded() { let bs = MemoryDB::default(); let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, LANE_LIMIT as u64); let state: PState = rt.get_state().unwrap(); - let payee_addr = Address::new_id(103); + let payee_addr = Address::new_id(TEST_PAYEE_ADDR); sv.lane += 1; sv.nonce += 1; sv.amount = BigInt::from(100); - let ucp = UpdateChannelStateParams { - proof: vec![], - secret: vec![], - sv: sv.clone(), - }; + let ucp = UpdateChannelStateParams::from(&sv); rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); rt.expect_validate_caller_addr(&[state.from, state.to]); @@ -639,7 +662,7 @@ fn lane_limit_exceeded() { ); let v = rt .call( - &PAYCH_ACTOR_CODE_ID.clone(), + &*PAYCH_ACTOR_CODE_ID, Method::UpdateChannelState as u64, &Serialized::serialize(ucp).unwrap(), ) @@ -680,14 +703,10 @@ fn extra_call_succeed() { Serialized::default(), ExitCode::Ok, ); - let ucp = UpdateChannelStateParams { - proof: vec![], - secret: vec![], - sv: sv.clone(), - }; + let ucp = UpdateChannelStateParams::from(&sv); assert!(rt .call( - &PAYCH_ACTOR_CODE_ID.clone(), + &*PAYCH_ACTOR_CODE_ID, Method::UpdateChannelState as u64, &Serialized::serialize(ucp).unwrap() ) @@ -726,14 +745,10 @@ fn extra_call_fail() { to_vec(&sv).unwrap(), ExitCode::Ok, ); - let ucp = UpdateChannelStateParams { - proof: vec![], - secret: vec![], - sv: sv.clone(), - }; + let ucp = UpdateChannelStateParams::from(&sv); let v = rt .call( - &PAYCH_ACTOR_CODE_ID.clone(), + &*PAYCH_ACTOR_CODE_ID, Method::UpdateChannelState as u64, &Serialized::serialize(ucp).unwrap(), ) @@ -752,7 +767,7 @@ fn update_channel_setting() { rt.expect_validate_caller_addr(&[state.from, state.to]); assert!(rt .call( - &PAYCH_ACTOR_CODE_ID.clone(), + &*PAYCH_ACTOR_CODE_ID, Method::Settle as u64, &Serialized::default() ) @@ -763,18 +778,37 @@ fn update_channel_setting() { assert_eq!(exp_settling_at, state.settling_at); assert_eq!(state.min_settle_height, 0); - let min_settle_vec = vec![0, 2, 12]; - let exp_min_settle_height = vec![state.min_settle_height, 2, 12]; - let exp_settling_at = vec![state.settling_at, state.settling_at, 12]; - let num_test_cases = min_settle_vec.len(); + struct TestCase { + min_settle: u64, + exp_min_settle_height: u64, + exp_settling_at: u64, + } - for i in 0..num_test_cases { + let test_cases = vec![ + TestCase { + min_settle: 0, + exp_min_settle_height: state.min_settle_height, + exp_settling_at: state.settling_at, + }, + TestCase { + min_settle: 2, + exp_min_settle_height: 2, + exp_settling_at: state.settling_at, + }, + TestCase { + min_settle: 12, + exp_min_settle_height: 12, + exp_settling_at: 12, + }, + ]; + + for tc in test_cases { let mut ucp = UpdateChannelStateParams { proof: vec![], secret: vec![], sv: sv.clone(), }; - ucp.sv.min_settle_height = exp_min_settle_height[i]; + ucp.sv.min_settle_height = tc.min_settle; rt.expect_validate_caller_addr(&[state.from, state.to]); rt.expect_verify_signature( ucp.sv.clone().signature.unwrap(), @@ -784,14 +818,14 @@ fn update_channel_setting() { ); assert!(rt .call( - &PAYCH_ACTOR_CODE_ID.clone(), + &*PAYCH_ACTOR_CODE_ID, Method::UpdateChannelState as u64, &Serialized::serialize(ucp).unwrap() ) .is_ok()); let new_state: PState = rt.get_state().unwrap(); - assert_eq!(exp_settling_at[i], new_state.settling_at); - assert_eq!(exp_min_settle_height[i], new_state.min_settle_height); + assert_eq!(tc.exp_settling_at, new_state.settling_at); + assert_eq!(tc.exp_min_settle_height, new_state.min_settle_height); } } @@ -800,11 +834,7 @@ fn succeed_correct_secret() { let bs = MemoryDB::default(); let (mut rt, sv) = require_create_cannel_with_lanes(&bs, 1); let state: PState = rt.get_state().unwrap(); - let ucp = UpdateChannelStateParams { - proof: vec![], - secret: vec![], - sv: sv.clone(), - }; + let ucp = UpdateChannelStateParams::from(&sv); rt.expect_validate_caller_addr(&[state.from, state.to]); rt.expect_verify_signature( ucp.sv.clone().signature.unwrap(), @@ -814,7 +844,7 @@ fn succeed_correct_secret() { ); assert!(rt .call( - &PAYCH_ACTOR_CODE_ID.clone(), + &*PAYCH_ACTOR_CODE_ID, Method::UpdateChannelState as u64, &Serialized::serialize(ucp).unwrap() ) @@ -845,7 +875,7 @@ fn incorrect_secret() { ); let v = rt .call( - &PAYCH_ACTOR_CODE_ID.clone(), + &*PAYCH_ACTOR_CODE_ID, Method::UpdateChannelState as u64, &Serialized::serialize(ucp).unwrap(), ) @@ -865,7 +895,7 @@ fn adjust_settling_at() { rt.expect_validate_caller_addr(&[state.from, state.to]); assert!(rt .call( - &PAYCH_ACTOR_CODE_ID.clone(), + &*PAYCH_ACTOR_CODE_ID, Method::Settle as u64, &Serialized::default() ) @@ -884,11 +914,7 @@ fn settle_if_height_less() { rt.epoch = ep; let mut state: PState = rt.get_state().unwrap(); sv.min_settle_height = (ep + SETTLE_DELAY) + 1; - let ucp = UpdateChannelStateParams { - proof: vec![], - secret: vec![], - sv: sv.clone(), - }; + let ucp = UpdateChannelStateParams::from(&sv); rt.expect_validate_caller_addr(&[state.from, state.to]); rt.expect_verify_signature( ucp.sv.clone().signature.unwrap(), @@ -898,7 +924,7 @@ fn settle_if_height_less() { ); assert!(rt .call( - &PAYCH_ACTOR_CODE_ID.clone(), + &*PAYCH_ACTOR_CODE_ID, Method::UpdateChannelState as u64, &Serialized::serialize(&ucp).unwrap() ) @@ -910,7 +936,7 @@ fn settle_if_height_less() { rt.expect_validate_caller_addr(&[state.from, state.to]); assert!(rt .call( - &PAYCH_ACTOR_CODE_ID.clone(), + &*PAYCH_ACTOR_CODE_ID, Method::Settle as u64, &Serialized::default() ) @@ -929,7 +955,7 @@ fn happy_path() { rt.expect_validate_caller_addr(&[state.from, state.to]); assert!(rt .call( - &PAYCH_ACTOR_CODE_ID.clone(), + &*PAYCH_ACTOR_CODE_ID, Method::Settle as u64, &Serialized::default() ) @@ -960,7 +986,7 @@ fn happy_path() { rt.expect_validate_caller_addr(&[state.from, state.to]); assert!(rt .call( - &PAYCH_ACTOR_CODE_ID.clone(), + &*PAYCH_ACTOR_CODE_ID, Method::Collect as u64, &Serialized::default() ) @@ -971,28 +997,46 @@ fn happy_path() { #[test] fn actor_collect() { - let dont_settle_vec = vec![true, false, false]; - let exp_send_from_vec = vec![ExitCode::Ok, ExitCode::ErrPlaceholder, ExitCode::Ok]; - let exp_send_to_vec = vec![ExitCode::Ok, ExitCode::Ok, ExitCode::ErrPlaceholder]; - let exp_collect_vec = vec![ - ExitCode::ErrForbidden, - ExitCode::ErrPlaceholder, - ExitCode::ErrPlaceholder, + struct TestCase { + dont_settle: bool, + exp_send_from: ExitCode, + exp_send_to: ExitCode, + exp_send_collect: ExitCode, + } + + let test_cases = vec![ + TestCase { + dont_settle: true, + exp_send_from: ExitCode::Ok, + exp_send_to: ExitCode::Ok, + exp_send_collect: ExitCode::ErrForbidden, + }, + TestCase { + dont_settle: false, + exp_send_from: ExitCode::ErrPlaceholder, + exp_send_to: ExitCode::Ok, + exp_send_collect: ExitCode::ErrPlaceholder, + }, + TestCase { + dont_settle: false, + exp_send_from: ExitCode::Ok, + exp_send_to: ExitCode::ErrPlaceholder, + exp_send_collect: ExitCode::ErrPlaceholder, + }, ]; - let num_test_cases = dont_settle_vec.len(); - for i in 0..num_test_cases { + for tc in test_cases { let bs = MemoryDB::default(); let (mut rt, _sv) = require_create_cannel_with_lanes(&bs, 1); rt.epoch = 10; let mut state: PState = rt.get_state().unwrap(); - if !dont_settle_vec[i] { + if !tc.dont_settle { rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); rt.expect_validate_caller_addr(&[state.from, state.to]); assert!(rt .call( - &PAYCH_ACTOR_CODE_ID.clone(), + &*PAYCH_ACTOR_CODE_ID, Method::Settle as u64, &Serialized::default() ) @@ -1011,7 +1055,7 @@ fn actor_collect() { Serialized::default(), sent_to_from, Serialized::default(), - exp_send_from_vec[i], + tc.exp_send_from, ); rt.expect_send( state.to, @@ -1019,7 +1063,7 @@ fn actor_collect() { Serialized::default(), state.to_send, Serialized::default(), - exp_send_to_vec[i], + tc.exp_send_to, ); // Collect. @@ -1027,12 +1071,12 @@ fn actor_collect() { rt.expect_validate_caller_addr(&[state.from, state.to]); let error = rt .call( - &PAYCH_ACTOR_CODE_ID.clone(), + &*PAYCH_ACTOR_CODE_ID, Method::Collect as u64, &Serialized::default(), ) .unwrap_err(); - assert_eq!(exp_collect_vec[i], error.exit_code()); + assert_eq!(tc.exp_send_collect, error.exit_code()); } } @@ -1040,9 +1084,9 @@ fn require_create_cannel_with_lanes<'a, BS: BlockStore>( bs: &'a BS, num_lanes: u64, ) -> (MockRuntime<'a, BS>, SignedVoucher) { - let paych_addr = Address::new_id(100); + let paych_addr = Address::new_id(TEST_PAYCH_ADDR); let payer_addr = Address::new_id(102); - let payee_addr = Address::new_id(103); + let payee_addr = Address::new_id(TEST_PAYEE_ADDR); let balance = TokenAmount::from(100_000 as u64); let recieved = TokenAmount::from(0 as u64); @@ -1088,7 +1132,7 @@ fn require_add_new_lane( rt: &mut MockRuntime<'_, BS>, param: LaneParams, ) -> SignedVoucher { - let payee_addr = Address::new_id(103); + let payee_addr = Address::new_id(TEST_PAYEE_ADDR); let sig = Signature::new_bls(vec![0, 1, 2, 3, 4, 5, 6, 7]); let sv = SignedVoucher { time_lock_min: param.epoch_num, @@ -1114,7 +1158,7 @@ fn require_add_new_lane( assert!(rt .call( - &PAYCH_ACTOR_CODE_ID.clone(), + &*PAYCH_ACTOR_CODE_ID, Method::UpdateChannelState as u64, &Serialized::serialize(ucp).unwrap(), ) @@ -1161,14 +1205,9 @@ fn verify_initial_state( sender: Address, receiver: Address, ) { - let state: PState = rt.get_state().unwrap(); + let _state: PState = rt.get_state().unwrap(); let expected_state = PState::new(sender, receiver); - assert_eq!(expected_state.to, state.to); - assert_eq!(expected_state.from, state.from); - assert_eq!(expected_state.min_settle_height, state.min_settle_height); - assert_eq!(expected_state.settling_at, state.settling_at); - assert_eq!(expected_state.to_send, state.to_send); - assert_eq!(state.lane_states.len(), 0); + verify_state(rt, -1, expected_state) } fn verify_state( From 65cdc3f33f406b4612696e18ea7f35d1f72a9a74 Mon Sep 17 00:00:00 2001 From: DragonMural Date: Thu, 18 Jun 2020 16:32:27 -0400 Subject: [PATCH 19/29] CHange requests --- vm/actor/tests/paych_actor_test.rs | 1370 ++++++++++++++-------------- 1 file changed, 702 insertions(+), 668 deletions(-) diff --git a/vm/actor/tests/paych_actor_test.rs b/vm/actor/tests/paych_actor_test.rs index 1ca52d9222b7..984b0eeff1f6 100644 --- a/vm/actor/tests/paych_actor_test.rs +++ b/vm/actor/tests/paych_actor_test.rs @@ -33,96 +33,40 @@ struct LaneParams { nonce: u64, } -const TEST_PAYCH_ADDR: u64 = 100; -const TEST_PAYER_ADDR: u64 = 101; -const TEST_PAYEE_ADDR: u64 = 103; -const TEST_CALLER_ADDR: u64 = 102; - -#[test] -fn create_paych_actor_test() { - let paych_addr = Address::new_id(TEST_PAYCH_ADDR); - let payer_addr = Address::new_id(TEST_PAYER_ADDR); - let caller_addr = Address::new_id(TEST_CALLER_ADDR); - - let bs = MemoryDB::default(); - let message = UnsignedMessage::builder() - .from(*SYSTEM_ACTOR_ADDR) - .to(paych_addr) - .build() - .unwrap(); - - let mut rt = MockRuntime::new(&bs, message); - rt.set_caller(INIT_ACTOR_CODE_ID.clone(), caller_addr); - rt.actor_code_cids - .insert(payer_addr, ACCOUNT_ACTOR_CODE_ID.clone()); - rt.actor_code_cids - .insert(caller_addr, ACCOUNT_ACTOR_CODE_ID.clone()); - construct_and_verify(&mut rt, payer_addr, caller_addr); -} +mod paych_constructor { -#[test] -fn actor_doesnt_exist_test() { - let paych_addr = Address::new_id(TEST_PAYCH_ADDR); - let payer_addr = Address::new_id(TEST_PAYER_ADDR); - let caller_addr = Address::new_id(TEST_CALLER_ADDR); - let bs = MemoryDB::default(); - let message = UnsignedMessage::builder() - .from(*SYSTEM_ACTOR_ADDR) - .to(paych_addr) - .build() - .unwrap(); - let mut rt = MockRuntime::new(&bs, message); - rt.set_caller(INIT_ACTOR_CODE_ID.clone(), caller_addr); - rt.actor_code_cids - .insert(payer_addr, ACCOUNT_ACTOR_CODE_ID.clone()); - rt.expect_validate_caller_type(&[INIT_ACTOR_CODE_ID.clone()]); - let params = ConstructorParams { - to: paych_addr, - from: payer_addr, - }; + use super::*; + const TEST_PAYCH_ADDR: u64 = 100; + const TEST_PAYER_ADDR: u64 = 101; + const TEST_CALLER_ADDR: u64 = 102; - let error = rt - .call( - &*PAYCH_ACTOR_CODE_ID, - METHOD_CONSTRUCTOR, - &Serialized::serialize(params).unwrap(), - ) - .unwrap_err(); - assert_eq!(error.exit_code(), ExitCode::ErrIllegalArgument); -} + #[test] + fn create_paych_actor_test() { + let paych_addr = Address::new_id(TEST_PAYCH_ADDR); + let payer_addr = Address::new_id(TEST_PAYER_ADDR); + let caller_addr = Address::new_id(TEST_CALLER_ADDR); -#[test] -fn actor_constructor_fails() { - let paych_addr = Address::new_id(TEST_PAYCH_ADDR); - let payer_addr = Address::new_id(TEST_PAYER_ADDR); - let caller_addr = Address::new_id(TEST_CALLER_ADDR); + let bs = MemoryDB::default(); + let message = UnsignedMessage::builder() + .from(*SYSTEM_ACTOR_ADDR) + .to(paych_addr) + .build() + .unwrap(); - struct TestCase { - paych_addr: Address, - caller_code: Cid, - new_actor_code: Cid, - payer_code: Cid, - expected_exit_code: ExitCode, + let mut rt = MockRuntime::new(&bs, message); + rt.set_caller(INIT_ACTOR_CODE_ID.clone(), caller_addr); + rt.actor_code_cids + .insert(payer_addr, ACCOUNT_ACTOR_CODE_ID.clone()); + rt.actor_code_cids + .insert(caller_addr, ACCOUNT_ACTOR_CODE_ID.clone()); + construct_and_verify(&mut rt, payer_addr, caller_addr); } - let test_cases: Vec = vec![ - TestCase { - paych_addr: paych_addr, - caller_code: INIT_ACTOR_CODE_ID.clone(), - new_actor_code: MULTISIG_ACTOR_CODE_ID.clone(), - payer_code: ACCOUNT_ACTOR_CODE_ID.clone(), - expected_exit_code: ExitCode::ErrIllegalArgument, - }, - TestCase { - paych_addr: Address::new_secp256k1(&vec![b'A'; 65][..]).unwrap(), - caller_code: INIT_ACTOR_CODE_ID.clone(), - new_actor_code: ACCOUNT_ACTOR_CODE_ID.clone(), - payer_code: ACCOUNT_ACTOR_CODE_ID.clone(), - expected_exit_code: ExitCode::ErrIllegalArgument, - }, - ]; - - for test_case in test_cases { + #[test] + fn actor_doesnt_exist_test() { + let paych_addr = Address::new_id(TEST_PAYCH_ADDR); + let payer_addr = Address::new_id(TEST_PAYER_ADDR); + let caller_addr = Address::new_id(TEST_CALLER_ADDR); let bs = MemoryDB::default(); let message = UnsignedMessage::builder() .from(*SYSTEM_ACTOR_ADDR) @@ -130,15 +74,15 @@ fn actor_constructor_fails() { .build() .unwrap(); let mut rt = MockRuntime::new(&bs, message); - rt.set_caller(test_case.caller_code, caller_addr); + rt.set_caller(INIT_ACTOR_CODE_ID.clone(), caller_addr); rt.actor_code_cids - .insert(test_case.paych_addr, test_case.new_actor_code); - rt.actor_code_cids.insert(payer_addr, test_case.payer_code); + .insert(payer_addr, ACCOUNT_ACTOR_CODE_ID.clone()); rt.expect_validate_caller_type(&[INIT_ACTOR_CODE_ID.clone()]); let params = ConstructorParams { - to: test_case.paych_addr, - from: Address::new_id(10001), + to: paych_addr, + from: payer_addr, }; + let error = rt .call( &*PAYCH_ACTOR_CODE_ID, @@ -146,12 +90,76 @@ fn actor_constructor_fails() { &Serialized::serialize(params).unwrap(), ) .unwrap_err(); - assert_eq!(test_case.expected_exit_code, error.exit_code()); + assert_eq!(error.exit_code(), ExitCode::ErrIllegalArgument); + } + + #[test] + fn actor_constructor_fails() { + let paych_addr = Address::new_id(TEST_PAYCH_ADDR); + let payer_addr = Address::new_id(TEST_PAYER_ADDR); + let caller_addr = Address::new_id(TEST_CALLER_ADDR); + + struct TestCase { + paych_addr: Address, + caller_code: Cid, + new_actor_code: Cid, + payer_code: Cid, + expected_exit_code: ExitCode, + } + + let test_cases: Vec = vec![ + TestCase { + paych_addr: paych_addr, + caller_code: INIT_ACTOR_CODE_ID.clone(), + new_actor_code: MULTISIG_ACTOR_CODE_ID.clone(), + payer_code: ACCOUNT_ACTOR_CODE_ID.clone(), + expected_exit_code: ExitCode::ErrIllegalArgument, + }, + TestCase { + paych_addr: Address::new_secp256k1(&vec![b'A'; 65][..]).unwrap(), + caller_code: INIT_ACTOR_CODE_ID.clone(), + new_actor_code: ACCOUNT_ACTOR_CODE_ID.clone(), + payer_code: ACCOUNT_ACTOR_CODE_ID.clone(), + expected_exit_code: ExitCode::ErrIllegalArgument, + }, + ]; + + for test_case in test_cases { + let bs = MemoryDB::default(); + let message = UnsignedMessage::builder() + .from(*SYSTEM_ACTOR_ADDR) + .to(paych_addr) + .build() + .unwrap(); + let mut rt = MockRuntime::new(&bs, message); + rt.set_caller(test_case.caller_code, caller_addr); + rt.actor_code_cids + .insert(test_case.paych_addr, test_case.new_actor_code); + rt.actor_code_cids.insert(payer_addr, test_case.payer_code); + rt.expect_validate_caller_type(&[INIT_ACTOR_CODE_ID.clone()]); + let params = ConstructorParams { + to: test_case.paych_addr, + from: Address::new_id(10001), + }; + let error = rt + .call( + &*PAYCH_ACTOR_CODE_ID, + METHOD_CONSTRUCTOR, + &Serialized::serialize(params).unwrap(), + ) + .unwrap_err(); + assert_eq!(test_case.expected_exit_code, error.exit_code()); + } } } mod create_lane_tests { use super::*; + const TEST_INIT_ACTOR_ADDR: u64 = 100; + const PAYCH_ADDR: u64 = 101; + const PAYER_ADDR: u64 = 102; + const PAYEE_ADDR: u64 = 103; + const PAYCH_BALANCE: u64 = 9; #[derive(Builder, Debug)] #[builder(name = "TestCaseBuilder")] struct TestCase { @@ -191,11 +199,11 @@ mod create_lane_tests { #[test] fn create_lane_test() { - let init_actor_addr = Address::new_id(100); - let paych_addr = Address::new_id(101); - let payer_addr = Address::new_id(102); - let payee_addr = Address::new_id(TEST_PAYEE_ADDR); - let paych_balance = TokenAmount::from(9 as u64); + let init_actor_addr = Address::new_id(TEST_INIT_ACTOR_ADDR); + let paych_addr = Address::new_id(PAYCH_ADDR); + let payer_addr = Address::new_id(PAYER_ADDR); + let payee_addr = Address::new_id(PAYEE_ADDR); + let paych_balance = TokenAmount::from(PAYCH_BALANCE); let sig = Option::Some(Signature::new_bls("doesn't matter".as_bytes().to_vec())); let test_cases: Vec = vec![ @@ -275,20 +283,14 @@ mod create_lane_tests { time_lock_min: test_case.tl_min, time_lock_max: test_case.tl_max, secret_pre_image: test_case.secret_preimage.clone(), - extra: Option::None, lane: test_case.lane, nonce: test_case.nonce, amount: BigInt::from(test_case.amt), - min_settle_height: 0, - merges: vec![], signature: test_case.sig.clone(), + ..SignedVoucher::default() }; - let ucp = UpdateChannelStateParams { - sv: sv.clone(), - secret: vec![], - proof: vec![], - }; + let ucp = UpdateChannelStateParams::from(&sv); rt.set_caller(test_case.target_code, payee_addr); rt.expect_validate_caller_addr(&[payer_addr, payee_addr]); @@ -327,13 +329,11 @@ mod create_lane_tests { time_lock_min: test_case.tl_min, time_lock_max: test_case.tl_max, secret_pre_image: test_case.secret_preimage, - extra: Option::None, lane: test_case.lane, nonce: test_case.nonce, amount: BigInt::from(test_case.amt), - min_settle_height: 0, - merges: vec![], signature: test_case.sig, + ..SignedVoucher::default() }; assert_eq!(sv.amount, ls.redeemed); assert_eq!(sv.nonce, ls.nonce); @@ -356,20 +356,18 @@ mod create_lane_tests { mod update_channel_state_redeem { use super::*; + #[test] fn redeem_voucher_one_lane() { let bs = MemoryDB::default(); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); + let (mut rt, harness, mut sv) = require_create_cannel_with_lanes(&bs, 1); let state: PState = rt.get_state().unwrap(); let new_voucher_amount = BigInt::from(9); sv.amount = new_voucher_amount; - let ucp = UpdateChannelStateParams { - proof: vec![], - secret: vec![], - sv: sv.clone(), - }; - let payee_addr = Address::new_id(TEST_PAYEE_ADDR); - let payer_addr = Address::new_id(102); + let ucp = UpdateChannelStateParams::from(&sv); + + let payee_addr = harness.payee; + let payer_addr = harness.payer; rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), payee_addr); rt.expect_validate_caller_addr(&[state.from, state.to]); let s = sv.signature.clone().unwrap(); @@ -404,15 +402,15 @@ mod update_channel_state_redeem { #[test] fn redeem_voucher_correct_lane() { let bs = MemoryDB::default(); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 3); + let (mut rt, harness, mut sv) = require_create_cannel_with_lanes(&bs, 3); let state: PState = rt.get_state().unwrap(); let initial_amount = state.to_send; sv.amount = BigInt::from(9); sv.lane = 1; let ls_to_update: &LaneState = &state.lane_states[1]; sv.nonce = ls_to_update.nonce + 1; - let payee_addr = Address::new_id(TEST_PAYEE_ADDR); - let payer_addr = Address::new_id(102); + let payee_addr = harness.payee; + let payer_addr = harness.payer; rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), payee_addr); rt.expect_validate_caller_addr(&[state.from, state.to]); @@ -423,11 +421,7 @@ mod update_channel_state_redeem { ExitCode::Ok, ); - let ucp = UpdateChannelStateParams { - proof: vec![], - secret: vec![], - sv: sv.clone(), - }; + let ucp = UpdateChannelStateParams::from(&sv); assert!(rt .call( @@ -453,133 +447,202 @@ mod update_channel_state_redeem { } } -#[test] -fn merge_success() { - let num_lanes = 3; - let bs = MemoryDB::default(); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, num_lanes); - let state: PState = rt.get_state().unwrap(); - let state_2: PState = rt.get_state().unwrap(); - - rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); - let merge_to: &LaneState = &state.lane_states[0]; - let merge_from: &LaneState = &state.lane_states[1]; - sv.lane = merge_to.id; - let merge_nonce = merge_to.nonce + 10; - let merges: Vec = vec![Merge { - lane: merge_from.id, - nonce: merge_nonce, - }]; - - sv.merges = merges; - let payee_addr = Address::new_id(TEST_PAYEE_ADDR); - rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature( - sv.clone().signature.unwrap(), - payee_addr, - to_vec(&sv).unwrap(), - ExitCode::Ok, - ); - let ucp = UpdateChannelStateParams::from(&sv); - assert!(rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap() - ) - .is_ok()); - rt.verify(); +mod merge_tests { + use super::*; - let exp_merge_to = LaneState { - id: merge_to.id, - redeemed: sv.amount.clone(), - nonce: sv.nonce, - }; + #[test] + fn merge_success() { + let num_lanes = 3; + let bs = MemoryDB::default(); + let (mut rt, harness, mut sv) = require_create_cannel_with_lanes(&bs, num_lanes); + let state: PState = rt.get_state().unwrap(); + let state_2: PState = rt.get_state().unwrap(); - let exp_merge_from = LaneState { - id: merge_from.id, - redeemed: merge_from.redeemed.clone(), - nonce: merge_nonce, - }; + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + let merge_to: &LaneState = &state.lane_states[0]; + let merge_from: &LaneState = &state.lane_states[1]; + sv.lane = merge_to.id; + let merge_nonce = merge_to.nonce + 10; + let merges: Vec = vec![Merge { + lane: merge_from.id, + nonce: merge_nonce, + }]; + + sv.merges = merges; + let payee_addr = harness.payee; + rt.expect_validate_caller_addr(&[state.from, state.to]); + rt.expect_verify_signature( + sv.clone().signature.unwrap(), + payee_addr, + to_vec(&sv).unwrap(), + ExitCode::Ok, + ); + let ucp = UpdateChannelStateParams::from(&sv); + assert!(rt + .call( + &*PAYCH_ACTOR_CODE_ID, + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap() + ) + .is_ok()); + rt.verify(); - let redeemed = &merge_from.redeemed + &merge_to.redeemed; + let exp_merge_to = LaneState { + id: merge_to.id, + redeemed: sv.amount.clone(), + nonce: sv.nonce, + }; - let exp_delta = &sv.amount - &redeemed; + let exp_merge_from = LaneState { + id: merge_from.id, + redeemed: merge_from.redeemed.clone(), + nonce: merge_nonce, + }; - let mut exp_state = state_2; + let redeemed = &merge_from.redeemed + &merge_to.redeemed; - exp_state.to_send = exp_delta.to_biguint().unwrap() + &state.to_send; + let exp_delta = &sv.amount - &redeemed; - exp_state.lane_states = vec![ - exp_merge_to, - exp_merge_from, - exp_state.lane_states.pop().unwrap(), - ]; - verify_state(&mut rt, num_lanes as i64, exp_state); -} + let mut exp_state = state_2; -#[test] -fn merge_failue() { - struct TestCase { - lane: i32, - voucher: u64, - balance: i32, - merge: u64, - exit: ExitCode, + exp_state.to_send = exp_delta.to_biguint().unwrap() + &state.to_send; + + exp_state.lane_states = vec![ + exp_merge_to, + exp_merge_from, + exp_state.lane_states.pop().unwrap(), + ]; + verify_state(&mut rt, num_lanes as i64, exp_state); } - let test_cases = vec![ - TestCase { - lane: 1, - voucher: 10, - balance: 0, - merge: 1, - exit: ExitCode::ErrIllegalArgument, - }, - TestCase { - lane: 1, - voucher: 0, - balance: 0, - merge: 10, - exit: ExitCode::ErrIllegalArgument, - }, - TestCase { - lane: 1, - voucher: 10, - balance: 1, - merge: 10, - exit: ExitCode::ErrIllegalState, - }, - TestCase { - lane: 0, - voucher: 10, - balance: 0, - merge: 10, - exit: ExitCode::ErrIllegalArgument, - }, - ]; - let payee_addr = Address::new_id(TEST_PAYEE_ADDR); + #[test] + fn merge_failue() { + struct TestCase { + lane: i32, + voucher: u64, + balance: i32, + merge: u64, + exit: ExitCode, + } + let test_cases = vec![ + TestCase { + lane: 1, + voucher: 10, + balance: 0, + merge: 1, + exit: ExitCode::ErrIllegalArgument, + }, + TestCase { + lane: 1, + voucher: 0, + balance: 0, + merge: 10, + exit: ExitCode::ErrIllegalArgument, + }, + TestCase { + lane: 1, + voucher: 10, + balance: 1, + merge: 10, + exit: ExitCode::ErrIllegalState, + }, + TestCase { + lane: 0, + voucher: 10, + balance: 0, + merge: 10, + exit: ExitCode::ErrIllegalArgument, + }, + ]; + + for tc in test_cases { + let bs = MemoryDB::default(); + let (mut rt, harness, mut sv) = require_create_cannel_with_lanes(&bs, 2); + rt.balance = TokenAmount::from(tc.balance as u64); + + let state: PState = rt.get_state().unwrap(); + let merge_to: &LaneState = &state.lane_states[0]; + let merge_from: &LaneState = &state.lane_states[tc.lane as usize]; + sv.lane = merge_to.id; + sv.nonce = tc.voucher; + sv.merges = vec![Merge { + lane: merge_from.id, + nonce: tc.merge, + }]; + let payee_addr = harness.payee; + + let ucp = UpdateChannelStateParams::from(&sv); - for tc in test_cases { - let bs = MemoryDB::default(); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 2); - rt.balance = TokenAmount::from(tc.balance as u64); + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + rt.expect_validate_caller_addr(&[state.from, state.to]); + rt.expect_verify_signature( + sv.clone().signature.unwrap(), + payee_addr, + to_vec(&sv).unwrap(), + ExitCode::Ok, + ); + let v = rt + .call( + &*PAYCH_ACTOR_CODE_ID, + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap(), + ) + .unwrap_err(); + assert_eq!(v.exit_code(), tc.exit); + rt.verify(); + } + } + #[test] + fn invalid_merge_lane_999() { + let bs = MemoryDB::default(); + let (mut rt, harness, mut sv) = require_create_cannel_with_lanes(&bs, 2); let state: PState = rt.get_state().unwrap(); + let payee_addr = harness.payee; let merge_to: &LaneState = &state.lane_states[0]; - let merge_from: &LaneState = &state.lane_states[tc.lane as usize]; + let merge_from = LaneState { + id: 999, + nonce: sv.nonce, + redeemed: BigInt::from(0), + }; + sv.lane = merge_to.id; - sv.nonce = tc.voucher; + sv.nonce = 10; sv.merges = vec![Merge { lane: merge_from.id, - nonce: tc.merge, + nonce: sv.nonce, }]; + let ucp = UpdateChannelStateParams::from(&sv); - let ucp = UpdateChannelStateParams { - proof: vec![], - secret: vec![], - sv: sv.clone(), - }; + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + rt.expect_validate_caller_addr(&[state.from, state.to]); + rt.expect_verify_signature( + sv.clone().signature.unwrap(), + payee_addr, + to_vec(&sv).unwrap(), + ExitCode::Ok, + ); + let v = rt + .call( + &*PAYCH_ACTOR_CODE_ID, + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap(), + ) + .unwrap_err(); + assert_eq!(v.exit_code(), ExitCode::ErrIllegalArgument); + rt.verify(); + } + + #[test] + fn lane_limit_exceeded() { + let bs = MemoryDB::default(); + let (mut rt, harness, mut sv) = require_create_cannel_with_lanes(&bs, LANE_LIMIT as u64); + let state: PState = rt.get_state().unwrap(); + let payee_addr = harness.payee; + sv.lane += 1; + sv.nonce += 1; + sv.amount = BigInt::from(100); + let ucp = UpdateChannelStateParams::from(&sv); rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); rt.expect_validate_caller_addr(&[state.from, state.to]); @@ -596,219 +659,211 @@ fn merge_failue() { &Serialized::serialize(ucp).unwrap(), ) .unwrap_err(); - assert_eq!(v.exit_code(), tc.exit); + assert_eq!(v.exit_code(), ExitCode::ErrIllegalArgument); rt.verify(); } } -#[test] -fn invalid_merge_lane_999() { - let bs = MemoryDB::default(); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 2); - let state: PState = rt.get_state().unwrap(); - let payee_addr = Address::new_id(TEST_PAYEE_ADDR); - let merge_to: &LaneState = &state.lane_states[0]; - let merge_from = LaneState { - id: 999, - nonce: sv.nonce, - redeemed: BigInt::from(0), - }; - - sv.lane = merge_to.id; - sv.nonce = 10; - sv.merges = vec![Merge { - lane: merge_from.id, - nonce: sv.nonce, - }]; - let ucp = UpdateChannelStateParams::from(&sv); +mod update_channel_state_extra { + use super::*; + const OTHER_ADDR: u64 = 104; + #[test] + fn extra_call_succeed() { + let bs = MemoryDB::default(); + let (mut rt, _, mut sv) = require_create_cannel_with_lanes(&bs, 1); + let state: PState = rt.get_state().unwrap(); + let other_addr = Address::new_id(OTHER_ADDR); + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + sv.extra = Some(ModVerifyParams { + actor: other_addr, + method: Method::UpdateChannelState as u64, + data: Serialized::serialize([1, 2, 3, 4]).unwrap(), + }); + rt.expect_validate_caller_addr(&[state.from, state.to]); + rt.expect_verify_signature( + sv.clone().signature.unwrap(), + state.to, + to_vec(&sv).unwrap(), + ExitCode::Ok, + ); + let exp_send_params = PaymentVerifyParams { + extra: Serialized::serialize(vec![1, 2, 3, 4]).unwrap(), + proof: vec![], + }; - rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); - rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature( - sv.clone().signature.unwrap(), - payee_addr, - to_vec(&sv).unwrap(), - ExitCode::Ok, - ); - let v = rt - .call( - &*PAYCH_ACTOR_CODE_ID, + rt.expect_send( + other_addr, Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap(), - ) - .unwrap_err(); - assert_eq!(v.exit_code(), ExitCode::ErrIllegalArgument); - rt.verify(); -} - -#[test] -fn lane_limit_exceeded() { - let bs = MemoryDB::default(); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, LANE_LIMIT as u64); - let state: PState = rt.get_state().unwrap(); - let payee_addr = Address::new_id(TEST_PAYEE_ADDR); - sv.lane += 1; - sv.nonce += 1; - sv.amount = BigInt::from(100); - let ucp = UpdateChannelStateParams::from(&sv); + Serialized::serialize(exp_send_params).unwrap(), + TokenAmount::from(0u8), + Serialized::default(), + ExitCode::Ok, + ); + let ucp = UpdateChannelStateParams::from(&sv); + assert!(rt + .call( + &*PAYCH_ACTOR_CODE_ID, + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap() + ) + .is_ok()); + rt.verify(); + } - rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); - rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature( - sv.clone().signature.unwrap(), - payee_addr, - to_vec(&sv).unwrap(), - ExitCode::Ok, - ); - let v = rt - .call( - &*PAYCH_ACTOR_CODE_ID, + #[test] + fn extra_call_fail() { + let bs = MemoryDB::default(); + let (mut rt, _, mut sv) = require_create_cannel_with_lanes(&bs, 1); + let state: PState = rt.get_state().unwrap(); + let other_addr = Address::new_id(OTHER_ADDR); + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + sv.extra = Some(ModVerifyParams { + actor: other_addr, + method: Method::UpdateChannelState as u64, + data: Serialized::serialize([1, 2, 3, 4]).unwrap(), + }); + rt.expect_validate_caller_addr(&[state.from, state.to]); + let exp_send_params = PaymentVerifyParams { + extra: Serialized::serialize(vec![1, 2, 3, 4]).unwrap(), + proof: vec![], + }; + rt.expect_send( + other_addr, Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap(), - ) - .unwrap_err(); - assert_eq!(v.exit_code(), ExitCode::ErrIllegalArgument); - rt.verify(); + Serialized::serialize(exp_send_params).unwrap(), + TokenAmount::from(0u8), + Serialized::default(), + ExitCode::ErrPlaceholder, + ); + rt.expect_verify_signature( + sv.clone().signature.unwrap(), + state.to, + to_vec(&sv).unwrap(), + ExitCode::Ok, + ); + let ucp = UpdateChannelStateParams::from(&sv); + let v = rt + .call( + &*PAYCH_ACTOR_CODE_ID, + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap(), + ) + .unwrap_err(); + assert_eq!(v.exit_code(), ExitCode::ErrPlaceholder); + rt.verify(); + } } -#[test] -fn extra_call_succeed() { - let bs = MemoryDB::default(); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); - let state: PState = rt.get_state().unwrap(); - let other_addr = Address::new_id(104); - rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); - sv.extra = Some(ModVerifyParams { - actor: other_addr, - method: Method::UpdateChannelState as u64, - data: Serialized::serialize([1, 2, 3, 4]).unwrap(), - }); - rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature( - sv.clone().signature.unwrap(), - state.to, - to_vec(&sv).unwrap(), - ExitCode::Ok, - ); - let exp_send_params = PaymentVerifyParams { - extra: Serialized::serialize(vec![1, 2, 3, 4]).unwrap(), - proof: vec![], - }; +mod update_channel_state_settling { + use super::*; + #[test] + fn update_channel_setting() { + let bs = MemoryDB::default(); + let (mut rt, _, sv) = require_create_cannel_with_lanes(&bs, 1); + rt.epoch = 10; + let state: PState = rt.get_state().unwrap(); + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + rt.expect_validate_caller_addr(&[state.from, state.to]); + assert!(rt + .call( + &*PAYCH_ACTOR_CODE_ID, + Method::Settle as u64, + &Serialized::default() + ) + .is_ok()); - rt.expect_send( - other_addr, - Method::UpdateChannelState as u64, - Serialized::serialize(exp_send_params).unwrap(), - TokenAmount::from(0u8), - Serialized::default(), - ExitCode::Ok, - ); - let ucp = UpdateChannelStateParams::from(&sv); - assert!(rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap() - ) - .is_ok()); - rt.verify(); -} + let exp_settling_at = SETTLE_DELAY + 10; + let state: PState = rt.get_state().unwrap(); + assert_eq!(exp_settling_at, state.settling_at); + assert_eq!(state.min_settle_height, 0); -#[test] -fn extra_call_fail() { - let bs = MemoryDB::default(); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); - let state: PState = rt.get_state().unwrap(); - let other_addr = Address::new_id(104); - rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); - sv.extra = Some(ModVerifyParams { - actor: other_addr, - method: Method::UpdateChannelState as u64, - data: Serialized::serialize([1, 2, 3, 4]).unwrap(), - }); - rt.expect_validate_caller_addr(&[state.from, state.to]); - let exp_send_params = PaymentVerifyParams { - extra: Serialized::serialize(vec![1, 2, 3, 4]).unwrap(), - proof: vec![], - }; - rt.expect_send( - other_addr, - Method::UpdateChannelState as u64, - Serialized::serialize(exp_send_params).unwrap(), - TokenAmount::from(0u8), - Serialized::default(), - ExitCode::ErrPlaceholder, - ); - rt.expect_verify_signature( - sv.clone().signature.unwrap(), - state.to, - to_vec(&sv).unwrap(), - ExitCode::Ok, - ); - let ucp = UpdateChannelStateParams::from(&sv); - let v = rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap(), - ) - .unwrap_err(); - assert_eq!(v.exit_code(), ExitCode::ErrPlaceholder); - rt.verify(); -} + struct TestCase { + min_settle: u64, + exp_min_settle_height: u64, + exp_settling_at: u64, + } -#[test] -fn update_channel_setting() { - let bs = MemoryDB::default(); - let (mut rt, sv) = require_create_cannel_with_lanes(&bs, 1); - rt.epoch = 10; - let state: PState = rt.get_state().unwrap(); - rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); - rt.expect_validate_caller_addr(&[state.from, state.to]); - assert!(rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::Settle as u64, - &Serialized::default() - ) - .is_ok()); + let test_cases = vec![ + TestCase { + min_settle: 0, + exp_min_settle_height: state.min_settle_height, + exp_settling_at: state.settling_at, + }, + TestCase { + min_settle: 2, + exp_min_settle_height: 2, + exp_settling_at: state.settling_at, + }, + TestCase { + min_settle: 12, + exp_min_settle_height: 12, + exp_settling_at: 12, + }, + ]; - let exp_settling_at = SETTLE_DELAY + 10; - let state: PState = rt.get_state().unwrap(); - assert_eq!(exp_settling_at, state.settling_at); - assert_eq!(state.min_settle_height, 0); + for tc in test_cases { + let mut ucp = UpdateChannelStateParams::from(&sv); - struct TestCase { - min_settle: u64, - exp_min_settle_height: u64, - exp_settling_at: u64, + ucp.sv.min_settle_height = tc.min_settle; + rt.expect_validate_caller_addr(&[state.from, state.to]); + rt.expect_verify_signature( + ucp.sv.clone().signature.unwrap(), + state.to, + to_vec(&ucp.sv).unwrap(), + ExitCode::Ok, + ); + assert!(rt + .call( + &*PAYCH_ACTOR_CODE_ID, + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap() + ) + .is_ok()); + let new_state: PState = rt.get_state().unwrap(); + assert_eq!(tc.exp_settling_at, new_state.settling_at); + assert_eq!(tc.exp_min_settle_height, new_state.min_settle_height); + } + } +} +mod secret_preimage { + use super::*; + #[test] + fn succeed_correct_secret() { + let bs = MemoryDB::default(); + let (mut rt, _, sv) = require_create_cannel_with_lanes(&bs, 1); + let state: PState = rt.get_state().unwrap(); + let ucp = UpdateChannelStateParams::from(&sv); + rt.expect_validate_caller_addr(&[state.from, state.to]); + rt.expect_verify_signature( + ucp.sv.clone().signature.unwrap(), + state.to, + to_vec(&sv).unwrap(), + ExitCode::Ok, + ); + assert!(rt + .call( + &*PAYCH_ACTOR_CODE_ID, + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap() + ) + .is_ok()); + rt.verify(); } - let test_cases = vec![ - TestCase { - min_settle: 0, - exp_min_settle_height: state.min_settle_height, - exp_settling_at: state.settling_at, - }, - TestCase { - min_settle: 2, - exp_min_settle_height: 2, - exp_settling_at: state.settling_at, - }, - TestCase { - min_settle: 12, - exp_min_settle_height: 12, - exp_settling_at: 12, - }, - ]; - - for tc in test_cases { + #[test] + fn incorrect_secret() { + let bs = MemoryDB::default(); + let (mut rt, _, sv) = require_create_cannel_with_lanes(&bs, 1); + let state: PState = rt.get_state().unwrap(); let mut ucp = UpdateChannelStateParams { proof: vec![], - secret: vec![], + secret: b"Profesr".to_vec(), sv: sv.clone(), }; - ucp.sv.min_settle_height = tc.min_settle; + let mut mag = b"Magneto".to_vec(); + let mut empty = vec![0; 25]; + mag.append(&mut empty); + ucp.sv.secret_pre_image = mag; rt.expect_validate_caller_addr(&[state.from, state.to]); rt.expect_verify_signature( ucp.sv.clone().signature.unwrap(), @@ -816,246 +871,139 @@ fn update_channel_setting() { to_vec(&ucp.sv).unwrap(), ExitCode::Ok, ); - assert!(rt + let v = rt .call( &*PAYCH_ACTOR_CODE_ID, Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap() + &Serialized::serialize(ucp).unwrap(), ) - .is_ok()); - let new_state: PState = rt.get_state().unwrap(); - assert_eq!(tc.exp_settling_at, new_state.settling_at); - assert_eq!(tc.exp_min_settle_height, new_state.min_settle_height); + .unwrap_err(); + assert_eq!(v.exit_code(), ExitCode::ErrIllegalArgument); + rt.verify(); } } -#[test] -fn succeed_correct_secret() { - let bs = MemoryDB::default(); - let (mut rt, sv) = require_create_cannel_with_lanes(&bs, 1); - let state: PState = rt.get_state().unwrap(); - let ucp = UpdateChannelStateParams::from(&sv); - rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature( - ucp.sv.clone().signature.unwrap(), - state.to, - to_vec(&sv).unwrap(), - ExitCode::Ok, - ); - assert!(rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap() - ) - .is_ok()); - rt.verify(); -} - -#[test] -fn incorrect_secret() { - let bs = MemoryDB::default(); - let (mut rt, sv) = require_create_cannel_with_lanes(&bs, 1); - let state: PState = rt.get_state().unwrap(); - let mut ucp = UpdateChannelStateParams { - proof: vec![], - secret: b"Profesr".to_vec(), - sv: sv.clone(), - }; - let mut mag = b"Magneto".to_vec(); - let mut empty = vec![0; 25]; - mag.append(&mut empty); - ucp.sv.secret_pre_image = mag; - rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature( - ucp.sv.clone().signature.unwrap(), - state.to, - to_vec(&ucp.sv).unwrap(), - ExitCode::Ok, - ); - let v = rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap(), - ) - .unwrap_err(); - assert_eq!(v.exit_code(), ExitCode::ErrIllegalArgument); - rt.verify(); -} - -#[test] -fn adjust_settling_at() { - let bs = MemoryDB::default(); - let (mut rt, _sv) = require_create_cannel_with_lanes(&bs, 1); - let ep = 10; - rt.epoch = ep; - let mut state: PState = rt.get_state().unwrap(); - rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); - rt.expect_validate_caller_addr(&[state.from, state.to]); - assert!(rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::Settle as u64, - &Serialized::default() - ) - .is_ok()); - let exp_settling_at = ep + SETTLE_DELAY; - state = rt.get_state().unwrap(); - assert_eq!(state.settling_at, exp_settling_at); - assert_eq!(state.min_settle_height, 0); -} - -#[test] -fn settle_if_height_less() { - let bs = MemoryDB::default(); - let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); - let ep = 10; - rt.epoch = ep; - let mut state: PState = rt.get_state().unwrap(); - sv.min_settle_height = (ep + SETTLE_DELAY) + 1; - let ucp = UpdateChannelStateParams::from(&sv); - rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature( - ucp.sv.clone().signature.unwrap(), - state.to, - to_vec(&sv).unwrap(), - ExitCode::Ok, - ); - assert!(rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::UpdateChannelState as u64, - &Serialized::serialize(&ucp).unwrap() - ) - .is_ok()); - state = rt.get_state().unwrap(); - assert_eq!(state.settling_at, 0); - assert_eq!(state.min_settle_height, ucp.sv.min_settle_height); - rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); - rt.expect_validate_caller_addr(&[state.from, state.to]); - assert!(rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::Settle as u64, - &Serialized::default() - ) - .is_ok()); - state = rt.get_state().unwrap(); - assert_eq!(state.settling_at, ucp.sv.min_settle_height); -} - -#[test] -fn happy_path() { - let bs = MemoryDB::default(); - let (mut rt, _sv) = require_create_cannel_with_lanes(&bs, 1); - rt.epoch = 10; - let mut state: PState = rt.get_state().unwrap(); - rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); - rt.expect_validate_caller_addr(&[state.from, state.to]); - assert!(rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::Settle as u64, - &Serialized::default() - ) - .is_ok()); - state = rt.get_state().unwrap(); - assert_eq!(state.settling_at, 11); - rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.epoch = 12; - let bal = &rt.balance; - let sent_to_from = bal - state.to_send.clone(); - rt.expect_send( - state.from, - METHOD_SEND, - Serialized::default(), - sent_to_from, - Serialized::default(), - ExitCode::Ok, - ); - rt.expect_send( - state.to, - METHOD_SEND, - Serialized::default(), - state.to_send, - Serialized::default(), - ExitCode::Ok, - ); - rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.to); - rt.expect_validate_caller_addr(&[state.from, state.to]); - assert!(rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::Collect as u64, - &Serialized::default() - ) - .is_ok()); - state = rt.get_state().unwrap(); - assert_eq!(state.to_send, TokenAmount::from(0u8)); -} +mod actor_settle { + use super::*; + const EP: u64 = 10; + #[test] + fn adjust_settling_at() { + let bs = MemoryDB::default(); + let (mut rt, _, _sv) = require_create_cannel_with_lanes(&bs, 1); + rt.epoch = EP; + let mut state: PState = rt.get_state().unwrap(); + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + rt.expect_validate_caller_addr(&[state.from, state.to]); + assert!(rt + .call( + &*PAYCH_ACTOR_CODE_ID, + Method::Settle as u64, + &Serialized::default() + ) + .is_ok()); + let exp_settling_at = EP + SETTLE_DELAY; + state = rt.get_state().unwrap(); + assert_eq!(state.settling_at, exp_settling_at); + assert_eq!(state.min_settle_height, 0); + } -#[test] -fn actor_collect() { - struct TestCase { - dont_settle: bool, - exp_send_from: ExitCode, - exp_send_to: ExitCode, - exp_send_collect: ExitCode, + #[test] + fn call_twice() { + let bs = MemoryDB::default(); + let (mut rt, _, _sv) = require_create_cannel_with_lanes(&bs, 1); + rt.epoch = EP; + let state: PState = rt.get_state().unwrap(); + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + rt.expect_validate_caller_addr(&[state.from, state.to]); + assert!(rt + .call( + &*PAYCH_ACTOR_CODE_ID, + Method::Settle as u64, + &Serialized::default() + ) + .is_ok()); + rt.expect_validate_caller_addr(&[state.from, state.to]); + assert_eq!( + ExitCode::ErrIllegalState, + rt.call( + &*PAYCH_ACTOR_CODE_ID, + Method::Settle as u64, + &Serialized::default() + ) + .unwrap_err() + .exit_code() + ); } - let test_cases = vec![ - TestCase { - dont_settle: true, - exp_send_from: ExitCode::Ok, - exp_send_to: ExitCode::Ok, - exp_send_collect: ExitCode::ErrForbidden, - }, - TestCase { - dont_settle: false, - exp_send_from: ExitCode::ErrPlaceholder, - exp_send_to: ExitCode::Ok, - exp_send_collect: ExitCode::ErrPlaceholder, - }, - TestCase { - dont_settle: false, - exp_send_from: ExitCode::Ok, - exp_send_to: ExitCode::ErrPlaceholder, - exp_send_collect: ExitCode::ErrPlaceholder, - }, - ]; + #[test] + fn settle_if_height_less() { + let bs = MemoryDB::default(); + let (mut rt, _, mut sv) = require_create_cannel_with_lanes(&bs, 1); + rt.epoch = EP; + let mut state: PState = rt.get_state().unwrap(); + sv.min_settle_height = (EP + SETTLE_DELAY) + 1; + let ucp = UpdateChannelStateParams::from(&sv); + rt.expect_validate_caller_addr(&[state.from, state.to]); + rt.expect_verify_signature( + ucp.sv.clone().signature.unwrap(), + state.to, + to_vec(&sv).unwrap(), + ExitCode::Ok, + ); + assert!(rt + .call( + &*PAYCH_ACTOR_CODE_ID, + Method::UpdateChannelState as u64, + &Serialized::serialize(&ucp).unwrap() + ) + .is_ok()); + state = rt.get_state().unwrap(); + assert_eq!(state.settling_at, 0); + assert_eq!(state.min_settle_height, ucp.sv.min_settle_height); + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + rt.expect_validate_caller_addr(&[state.from, state.to]); + assert!(rt + .call( + &*PAYCH_ACTOR_CODE_ID, + Method::Settle as u64, + &Serialized::default() + ) + .is_ok()); + state = rt.get_state().unwrap(); + assert_eq!(state.settling_at, ucp.sv.min_settle_height); + } +} - for tc in test_cases { +mod actor_collect { + use super::*; + #[test] + fn happy_path() { let bs = MemoryDB::default(); - let (mut rt, _sv) = require_create_cannel_with_lanes(&bs, 1); + let (mut rt, _, _sv) = require_create_cannel_with_lanes(&bs, 1); rt.epoch = 10; let mut state: PState = rt.get_state().unwrap(); - - if !tc.dont_settle { - rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); - rt.expect_validate_caller_addr(&[state.from, state.to]); - assert!(rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::Settle as u64, - &Serialized::default() - ) - .is_ok()); - state = rt.get_state().unwrap(); - assert_eq!(state.settling_at, 11); - } - - // "wait" for SettlingAt epoch + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + rt.expect_validate_caller_addr(&[state.from, state.to]); + assert!(rt + .call( + &*PAYCH_ACTOR_CODE_ID, + Method::Settle as u64, + &Serialized::default() + ) + .is_ok()); + state = rt.get_state().unwrap(); + assert_eq!(state.settling_at, 11); + rt.expect_validate_caller_addr(&[state.from, state.to]); rt.epoch = 12; - - let sent_to_from = &rt.balance - state.to_send.clone(); + let bal = &rt.balance; + let sent_to_from = bal - state.to_send.clone(); rt.expect_send( state.from, METHOD_SEND, Serialized::default(), sent_to_from, Serialized::default(), - tc.exp_send_from, + ExitCode::Ok, ); rt.expect_send( state.to, @@ -1063,30 +1011,119 @@ fn actor_collect() { Serialized::default(), state.to_send, Serialized::default(), - tc.exp_send_to, + ExitCode::Ok, ); - - // Collect. - rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.to); rt.expect_validate_caller_addr(&[state.from, state.to]); - let error = rt + assert!(rt .call( &*PAYCH_ACTOR_CODE_ID, Method::Collect as u64, - &Serialized::default(), + &Serialized::default() ) - .unwrap_err(); - assert_eq!(tc.exp_send_collect, error.exit_code()); + .is_ok()); + state = rt.get_state().unwrap(); + assert_eq!(state.to_send, TokenAmount::from(0u8)); + } + + #[test] + fn actor_collect() { + struct TestCase { + dont_settle: bool, + exp_send_from: ExitCode, + exp_send_to: ExitCode, + exp_send_collect: ExitCode, + } + + let test_cases = vec![ + TestCase { + dont_settle: true, + exp_send_from: ExitCode::Ok, + exp_send_to: ExitCode::Ok, + exp_send_collect: ExitCode::ErrForbidden, + }, + TestCase { + dont_settle: false, + exp_send_from: ExitCode::ErrPlaceholder, + exp_send_to: ExitCode::Ok, + exp_send_collect: ExitCode::ErrPlaceholder, + }, + TestCase { + dont_settle: false, + exp_send_from: ExitCode::Ok, + exp_send_to: ExitCode::ErrPlaceholder, + exp_send_collect: ExitCode::ErrPlaceholder, + }, + ]; + + for tc in test_cases { + let bs = MemoryDB::default(); + let (mut rt, _, _sv) = require_create_cannel_with_lanes(&bs, 1); + rt.epoch = 10; + let mut state: PState = rt.get_state().unwrap(); + + if !tc.dont_settle { + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + rt.expect_validate_caller_addr(&[state.from, state.to]); + assert!(rt + .call( + &*PAYCH_ACTOR_CODE_ID, + Method::Settle as u64, + &Serialized::default() + ) + .is_ok()); + state = rt.get_state().unwrap(); + assert_eq!(state.settling_at, 11); + } + + // "wait" for SettlingAt epoch + rt.epoch = 12; + + let sent_to_from = &rt.balance - state.to_send.clone(); + rt.expect_send( + state.from, + METHOD_SEND, + Serialized::default(), + sent_to_from, + Serialized::default(), + tc.exp_send_from, + ); + rt.expect_send( + state.to, + METHOD_SEND, + Serialized::default(), + state.to_send, + Serialized::default(), + tc.exp_send_to, + ); + + // Collect. + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + rt.expect_validate_caller_addr(&[state.from, state.to]); + let error = rt + .call( + &*PAYCH_ACTOR_CODE_ID, + Method::Collect as u64, + &Serialized::default(), + ) + .unwrap_err(); + assert_eq!(tc.exp_send_collect, error.exit_code()); + } } } +struct ActorHarness { + payer: Address, + payee: Address, +} + fn require_create_cannel_with_lanes<'a, BS: BlockStore>( bs: &'a BS, num_lanes: u64, -) -> (MockRuntime<'a, BS>, SignedVoucher) { - let paych_addr = Address::new_id(TEST_PAYCH_ADDR); - let payer_addr = Address::new_id(102); - let payee_addr = Address::new_id(TEST_PAYEE_ADDR); +) -> (MockRuntime<'a, BS>, ActorHarness, SignedVoucher) { + let paych_addr = Address::new_id(100 as u64); + let payer_addr = Address::new_id(102 as u64); + let payee_addr = Address::new_id(103 as u64); let balance = TokenAmount::from(100_000 as u64); let recieved = TokenAmount::from(0 as u64); @@ -1125,36 +1162,36 @@ fn require_create_cannel_with_lanes<'a, BS: BlockStore>( last_sv = require_add_new_lane(&mut rt, lane_param); } - (rt, last_sv) + ( + rt, + ActorHarness { + payer: payer_addr, + payee: payee_addr, + }, + last_sv, + ) } fn require_add_new_lane( rt: &mut MockRuntime<'_, BS>, param: LaneParams, ) -> SignedVoucher { - let payee_addr = Address::new_id(TEST_PAYEE_ADDR); + let payee_addr = Address::new_id(103 as u64); let sig = Signature::new_bls(vec![0, 1, 2, 3, 4, 5, 6, 7]); let sv = SignedVoucher { time_lock_min: param.epoch_num, time_lock_max: u64::MAX, - secret_pre_image: vec![], - extra: Option::None, lane: param.lane, nonce: param.nonce, amount: BigInt::from(param.amt), - min_settle_height: 0, - merges: vec![], signature: Some(sig.clone()), + ..SignedVoucher::default() }; rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), param.from); rt.expect_validate_caller_addr(&[param.from, param.to]); rt.expect_verify_signature(sig.clone(), payee_addr, to_vec(&sv).unwrap(), ExitCode::Ok); - let ucp = UpdateChannelStateParams { - proof: vec![], - secret: vec![], - sv: sv, - }; + let ucp = UpdateChannelStateParams::from(&sv); assert!(rt .call( @@ -1168,14 +1205,11 @@ fn require_add_new_lane( SignedVoucher { time_lock_min: param.epoch_num, time_lock_max: u64::MAX, - secret_pre_image: vec![], - extra: Option::None, lane: param.lane, nonce: param.nonce, amount: BigInt::from(param.amt), - min_settle_height: 0, - merges: vec![], signature: Some(sig.clone()), + ..SignedVoucher::default() } } From 0bbee18ba8eb1fbb93e1f1d0e71ba2d860ed0258 Mon Sep 17 00:00:00 2001 From: DragonMural Date: Sun, 21 Jun 2020 16:32:46 -0400 Subject: [PATCH 20/29] Trying to reduce code size --- vm/actor/tests/common/mod.rs | 4 +- vm/actor/tests/paych_actor_test.rs | 725 ++++++++++++----------------- 2 files changed, 293 insertions(+), 436 deletions(-) diff --git a/vm/actor/tests/common/mod.rs b/vm/actor/tests/common/mod.rs index 15a3ae4a54f9..b3810007939b 100644 --- a/vm/actor/tests/common/mod.rs +++ b/vm/actor/tests/common/mod.rs @@ -11,7 +11,7 @@ use cid::{multihash::Blake2b256, Cid}; use clock::ChainEpoch; use crypto::{DomainSeparationTag, Signature}; use encoding::{blake2b_256, de::DeserializeOwned, Cbor}; -use fil_types::{PieceInfo, RegisteredProof, SealVerifyInfo, WindowPoStVerifyInfo}; +use fil_types::{PieceInfo, RegisteredSealProof, SealVerifyInfo, WindowPoStVerifyInfo}; use ipld_blockstore::BlockStore; use message::{Message, UnsignedMessage}; use runtime::{ActorCode, ConsensusFault, Runtime, Syscalls}; @@ -615,7 +615,7 @@ where } fn compute_unsealed_sector_cid( &self, - _reg: RegisteredProof, + _reg: RegisteredSealProof, _pieces: &[PieceInfo], ) -> Result> { unimplemented!(); diff --git a/vm/actor/tests/paych_actor_test.rs b/vm/actor/tests/paych_actor_test.rs index 984b0eeff1f6..06a393bc2429 100644 --- a/vm/actor/tests/paych_actor_test.rs +++ b/vm/actor/tests/paych_actor_test.rs @@ -2,7 +2,6 @@ // SPDX-License-Identifier: Apache-2.0, MIT mod common; - use actor::{ paych::{ ConstructorParams, LaneState, Merge, Method, ModVerifyParams, PaymentVerifyParams, @@ -24,6 +23,8 @@ use message::UnsignedMessage; use num_bigint::{BigInt, Sign}; use vm::{ExitCode, Serialized, TokenAmount, METHOD_CONSTRUCTOR, METHOD_SEND}; +const R_PAYEE_ADDR: u64 = 103; +const R_PAYER_ADDR: u64 = 102; struct LaneParams { epoch_num: ChainEpoch, from: Address, @@ -33,6 +34,24 @@ struct LaneParams { nonce: u64, } +fn is_ok<'a, BS: BlockStore>(rt: &mut MockRuntime<'a, BS>, method_num: u64, ser: &Serialized) { + assert!(rt.call(&*PAYCH_ACTOR_CODE_ID, method_num, ser).is_ok()); +} + +fn expect_error<'a, BS: BlockStore>( + rt: &mut MockRuntime<'a, BS>, + method_num: u64, + ser: &Serialized, + exp: ExitCode, +) { + assert_eq!( + exp, + rt.call(&*PAYCH_ACTOR_CODE_ID, method_num, ser,) + .unwrap_err() + .exit_code() + ); +} + mod paych_constructor { use super::*; @@ -40,57 +59,50 @@ mod paych_constructor { const TEST_PAYER_ADDR: u64 = 101; const TEST_CALLER_ADDR: u64 = 102; - #[test] - fn create_paych_actor_test() { + fn construct_runtime<'a, BS: BlockStore>(bs: &'a BS) -> MockRuntime<'a, BS> { let paych_addr = Address::new_id(TEST_PAYCH_ADDR); let payer_addr = Address::new_id(TEST_PAYER_ADDR); let caller_addr = Address::new_id(TEST_CALLER_ADDR); - - let bs = MemoryDB::default(); let message = UnsignedMessage::builder() .from(*SYSTEM_ACTOR_ADDR) .to(paych_addr) .build() .unwrap(); - - let mut rt = MockRuntime::new(&bs, message); + let mut rt = MockRuntime::new(bs, message); rt.set_caller(INIT_ACTOR_CODE_ID.clone(), caller_addr); rt.actor_code_cids .insert(payer_addr, ACCOUNT_ACTOR_CODE_ID.clone()); + rt + } + + #[test] + fn create_paych_actor_test() { + let caller_addr = Address::new_id(TEST_CALLER_ADDR); + let bs = MemoryDB::default(); + let mut rt = construct_runtime(&bs); + rt.actor_code_cids .insert(caller_addr, ACCOUNT_ACTOR_CODE_ID.clone()); - construct_and_verify(&mut rt, payer_addr, caller_addr); + construct_and_verify(&mut rt, Address::new_id(TEST_PAYER_ADDR), caller_addr); } #[test] fn actor_doesnt_exist_test() { - let paych_addr = Address::new_id(TEST_PAYCH_ADDR); - let payer_addr = Address::new_id(TEST_PAYER_ADDR); - let caller_addr = Address::new_id(TEST_CALLER_ADDR); let bs = MemoryDB::default(); - let message = UnsignedMessage::builder() - .from(*SYSTEM_ACTOR_ADDR) - .to(paych_addr) - .build() - .unwrap(); - let mut rt = MockRuntime::new(&bs, message); - rt.set_caller(INIT_ACTOR_CODE_ID.clone(), caller_addr); - rt.actor_code_cids - .insert(payer_addr, ACCOUNT_ACTOR_CODE_ID.clone()); + let mut rt = construct_runtime(&bs); + rt.expect_validate_caller_type(&[INIT_ACTOR_CODE_ID.clone()]); let params = ConstructorParams { - to: paych_addr, - from: payer_addr, + to: Address::new_id(TEST_PAYCH_ADDR), + from: Address::new_id(TEST_PAYER_ADDR), }; - let error = rt - .call( - &*PAYCH_ACTOR_CODE_ID, - METHOD_CONSTRUCTOR, - &Serialized::serialize(params).unwrap(), - ) - .unwrap_err(); - assert_eq!(error.exit_code(), ExitCode::ErrIllegalArgument); + expect_error( + &mut rt, + METHOD_CONSTRUCTOR, + &Serialized::serialize(params).unwrap(), + ExitCode::ErrIllegalArgument, + ); } #[test] @@ -133,6 +145,7 @@ mod paych_constructor { .unwrap(); let mut rt = MockRuntime::new(&bs, message); rt.set_caller(test_case.caller_code, caller_addr); + rt.actor_code_cids .insert(test_case.paych_addr, test_case.new_actor_code); rt.actor_code_cids.insert(payer_addr, test_case.payer_code); @@ -141,14 +154,12 @@ mod paych_constructor { to: test_case.paych_addr, from: Address::new_id(10001), }; - let error = rt - .call( - &*PAYCH_ACTOR_CODE_ID, - METHOD_CONSTRUCTOR, - &Serialized::serialize(params).unwrap(), - ) - .unwrap_err(); - assert_eq!(test_case.expected_exit_code, error.exit_code()); + expect_error( + &mut rt, + METHOD_CONSTRUCTOR, + &Serialized::serialize(params).unwrap(), + test_case.expected_exit_code, + ); } } } @@ -291,62 +302,43 @@ mod create_lane_tests { }; let ucp = UpdateChannelStateParams::from(&sv); - rt.set_caller(test_case.target_code, payee_addr); rt.expect_validate_caller_addr(&[payer_addr, payee_addr]); if test_case.sig.is_some() && test_case.secret_preimage.len() == 0 { - if !test_case.verify_sig { - rt.expect_verify_signature( - sv.clone().signature.unwrap(), - payer_addr, - to_vec(&sv).unwrap(), - ExitCode::ErrIllegalState, - ); + let exp_exit_code = if !test_case.verify_sig { + ExitCode::ErrIllegalState } else { - rt.expect_verify_signature( - sv.clone().signature.unwrap(), - payer_addr, - to_vec(&sv).unwrap(), - ExitCode::Ok, - ); - } + ExitCode::Ok + }; + rt.expect_verify_signature( + sv.clone().signature.unwrap(), + payer_addr, + to_vec(&sv).unwrap(), + exp_exit_code, + ); } if test_case.exp_exit_code == ExitCode::Ok { - assert!(rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap() - ) - .is_ok()); + is_ok( + &mut rt, + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap(), + ); let st: PState = rt.get_state().unwrap(); assert_eq!(st.lane_states.len(), 1); let ls = st.lane_states.first().unwrap(); - let sv = SignedVoucher { - time_lock_min: test_case.tl_min, - time_lock_max: test_case.tl_max, - secret_pre_image: test_case.secret_preimage, - lane: test_case.lane, - nonce: test_case.nonce, - amount: BigInt::from(test_case.amt), - signature: test_case.sig, - ..SignedVoucher::default() - }; assert_eq!(sv.amount, ls.redeemed); assert_eq!(sv.nonce, ls.nonce); assert_eq!(sv.lane, ls.id); } else { - let error = rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap(), - ) - .unwrap_err(); - assert_eq!(error.exit_code(), test_case.exp_exit_code); + expect_error( + &mut rt, + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap(), + test_case.exp_exit_code, + ); verify_initial_state(&mut rt, payer_addr, payee_addr); } rt.verify(); @@ -360,27 +352,29 @@ mod update_channel_state_redeem { #[test] fn redeem_voucher_one_lane() { let bs = MemoryDB::default(); - let (mut rt, harness, mut sv) = require_create_cannel_with_lanes(&bs, 1); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); let state: PState = rt.get_state().unwrap(); - let new_voucher_amount = BigInt::from(9); - sv.amount = new_voucher_amount; - let ucp = UpdateChannelStateParams::from(&sv); + let payee_addr = Address::new_id(R_PAYEE_ADDR); - let payee_addr = harness.payee; - let payer_addr = harness.payer; rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), payee_addr); rt.expect_validate_caller_addr(&[state.from, state.to]); - let s = sv.signature.clone().unwrap(); - rt.expect_verify_signature(s.clone(), payer_addr, to_vec(&sv).unwrap(), ExitCode::Ok); + sv.amount = BigInt::from(9); - assert!(rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap(), - ) - .is_ok()); + let payer_addr = Address::new_id(R_PAYER_ADDR); + + rt.expect_verify_signature( + sv.signature.clone().unwrap(), + payer_addr, + to_vec(&sv).unwrap(), + ExitCode::Ok, + ); + + is_ok( + &mut rt, + Method::UpdateChannelState as u64, + &Serialized::serialize(UpdateChannelStateParams::from(&sv)).unwrap(), + ); rt.verify(); let exp_ls = LaneState { @@ -402,18 +396,20 @@ mod update_channel_state_redeem { #[test] fn redeem_voucher_correct_lane() { let bs = MemoryDB::default(); - let (mut rt, harness, mut sv) = require_create_cannel_with_lanes(&bs, 3); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 3); let state: PState = rt.get_state().unwrap(); + let payee_addr = Address::new_id(R_PAYEE_ADDR); + + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), payee_addr); + rt.expect_validate_caller_addr(&[state.from, state.to]); + let initial_amount = state.to_send; sv.amount = BigInt::from(9); sv.lane = 1; let ls_to_update: &LaneState = &state.lane_states[1]; sv.nonce = ls_to_update.nonce + 1; - let payee_addr = harness.payee; - let payer_addr = harness.payer; + let payer_addr = Address::new_id(R_PAYER_ADDR); - rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), payee_addr); - rt.expect_validate_caller_addr(&[state.from, state.to]); rt.expect_verify_signature( sv.clone().signature.unwrap(), payer_addr, @@ -421,15 +417,12 @@ mod update_channel_state_redeem { ExitCode::Ok, ); - let ucp = UpdateChannelStateParams::from(&sv); + is_ok( + &mut rt, + Method::UpdateChannelState as u64, + &Serialized::serialize(UpdateChannelStateParams::from(&sv)).unwrap(), + ); - assert!(rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap() - ) - .is_ok()); rt.verify(); let state: PState = rt.get_state().unwrap(); @@ -450,69 +443,87 @@ mod update_channel_state_redeem { mod merge_tests { use super::*; + fn construct_runtime<'a, BS: BlockStore>( + bs: &'a BS, + num_lanes: u64, + ) -> (MockRuntime<'a, BS>, SignedVoucher, PState) { + let (mut rt, sv) = require_create_cannel_with_lanes(bs, num_lanes); + let state: PState = rt.get_state().unwrap(); + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from.clone()); + rt.expect_validate_caller_addr(&[state.from, state.to]); + (rt, sv, state) + } + + fn failure_end( + rt: &mut MockRuntime, + sv: SignedVoucher, + exp_exit_code: ExitCode, + ) { + let payee_addr = Address::new_id(R_PAYEE_ADDR); + rt.expect_verify_signature( + sv.clone().signature.unwrap(), + payee_addr, + to_vec(&sv).unwrap(), + ExitCode::Ok, + ); + expect_error( + rt, + Method::UpdateChannelState as u64, + &Serialized::serialize(UpdateChannelStateParams::from(&sv)).unwrap(), + exp_exit_code, + ); + rt.verify(); + } + #[test] fn merge_success() { let num_lanes = 3; let bs = MemoryDB::default(); - let (mut rt, harness, mut sv) = require_create_cannel_with_lanes(&bs, num_lanes); - let state: PState = rt.get_state().unwrap(); - let state_2: PState = rt.get_state().unwrap(); + let (mut rt, mut sv, mut state) = construct_runtime(&bs, num_lanes); - rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); let merge_to: &LaneState = &state.lane_states[0]; let merge_from: &LaneState = &state.lane_states[1]; sv.lane = merge_to.id; let merge_nonce = merge_to.nonce + 10; - let merges: Vec = vec![Merge { + + sv.merges = vec![Merge { lane: merge_from.id, nonce: merge_nonce, }]; - - sv.merges = merges; - let payee_addr = harness.payee; - rt.expect_validate_caller_addr(&[state.from, state.to]); + let payee_addr = Address::new_id(R_PAYEE_ADDR); rt.expect_verify_signature( sv.clone().signature.unwrap(), payee_addr, to_vec(&sv).unwrap(), ExitCode::Ok, ); - let ucp = UpdateChannelStateParams::from(&sv); - assert!(rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap() - ) - .is_ok()); - rt.verify(); + is_ok( + &mut rt, + Method::UpdateChannelState as u64, + &Serialized::serialize(UpdateChannelStateParams::from(&sv)).unwrap(), + ); + rt.verify(); let exp_merge_to = LaneState { id: merge_to.id, redeemed: sv.amount.clone(), nonce: sv.nonce, }; - let exp_merge_from = LaneState { id: merge_from.id, redeemed: merge_from.redeemed.clone(), nonce: merge_nonce, }; - let redeemed = &merge_from.redeemed + &merge_to.redeemed; - let exp_delta = &sv.amount - &redeemed; + state.to_send = exp_delta.to_biguint().unwrap() + &state.to_send; - let mut exp_state = state_2; - - exp_state.to_send = exp_delta.to_biguint().unwrap() + &state.to_send; - - exp_state.lane_states = vec![ + state.lane_states = vec![ exp_merge_to, exp_merge_from, - exp_state.lane_states.pop().unwrap(), + state.lane_states.pop().unwrap(), ]; - verify_state(&mut rt, num_lanes as i64, exp_state); + verify_state(&mut rt, num_lanes as i64, state); } #[test] @@ -557,10 +568,11 @@ mod merge_tests { for tc in test_cases { let bs = MemoryDB::default(); - let (mut rt, harness, mut sv) = require_create_cannel_with_lanes(&bs, 2); + let num_lanes = 2; + let (mut rt, mut sv, state) = construct_runtime(&bs, num_lanes); + rt.balance = TokenAmount::from(tc.balance as u64); - let state: PState = rt.get_state().unwrap(); let merge_to: &LaneState = &state.lane_states[0]; let merge_from: &LaneState = &state.lane_states[tc.lane as usize]; sv.lane = merge_to.id; @@ -569,36 +581,16 @@ mod merge_tests { lane: merge_from.id, nonce: tc.merge, }]; - let payee_addr = harness.payee; - - let ucp = UpdateChannelStateParams::from(&sv); - - rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); - rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature( - sv.clone().signature.unwrap(), - payee_addr, - to_vec(&sv).unwrap(), - ExitCode::Ok, - ); - let v = rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap(), - ) - .unwrap_err(); - assert_eq!(v.exit_code(), tc.exit); - rt.verify(); + failure_end(&mut rt, sv, tc.exit); } } #[test] fn invalid_merge_lane_999() { let bs = MemoryDB::default(); - let (mut rt, harness, mut sv) = require_create_cannel_with_lanes(&bs, 2); - let state: PState = rt.get_state().unwrap(); - let payee_addr = harness.payee; + let num_lanes = 2; + let (mut rt, mut sv, state) = construct_runtime(&bs, num_lanes); + let merge_to: &LaneState = &state.lane_states[0]; let merge_from = LaneState { id: 999, @@ -612,74 +604,42 @@ mod merge_tests { lane: merge_from.id, nonce: sv.nonce, }]; - let ucp = UpdateChannelStateParams::from(&sv); - - rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); - rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature( - sv.clone().signature.unwrap(), - payee_addr, - to_vec(&sv).unwrap(), - ExitCode::Ok, - ); - let v = rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap(), - ) - .unwrap_err(); - assert_eq!(v.exit_code(), ExitCode::ErrIllegalArgument); - rt.verify(); + failure_end(&mut rt, sv, ExitCode::ErrIllegalArgument); } #[test] fn lane_limit_exceeded() { let bs = MemoryDB::default(); - let (mut rt, harness, mut sv) = require_create_cannel_with_lanes(&bs, LANE_LIMIT as u64); - let state: PState = rt.get_state().unwrap(); - let payee_addr = harness.payee; + let num_lanes = LANE_LIMIT as u64; + let (mut rt, mut sv, _) = construct_runtime(&bs, num_lanes); + sv.lane += 1; sv.nonce += 1; sv.amount = BigInt::from(100); - let ucp = UpdateChannelStateParams::from(&sv); - - rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); - rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature( - sv.clone().signature.unwrap(), - payee_addr, - to_vec(&sv).unwrap(), - ExitCode::Ok, - ); - let v = rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap(), - ) - .unwrap_err(); - assert_eq!(v.exit_code(), ExitCode::ErrIllegalArgument); - rt.verify(); + failure_end(&mut rt, sv, ExitCode::ErrIllegalArgument); } } mod update_channel_state_extra { use super::*; const OTHER_ADDR: u64 = 104; - #[test] - fn extra_call_succeed() { - let bs = MemoryDB::default(); - let (mut rt, _, mut sv) = require_create_cannel_with_lanes(&bs, 1); + + fn construct_runtime<'a, BS: BlockStore>( + bs: &'a BS, + exit_code: ExitCode, + ) -> (MockRuntime<'a, BS>, SignedVoucher) { + let (mut rt, mut sv) = require_create_cannel_with_lanes(bs, 1); let state: PState = rt.get_state().unwrap(); let other_addr = Address::new_id(OTHER_ADDR); + let fake_params = [1, 2, 3, 4]; rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + rt.expect_validate_caller_addr(&[state.from, state.to]); + sv.extra = Some(ModVerifyParams { actor: other_addr, method: Method::UpdateChannelState as u64, - data: Serialized::serialize([1, 2, 3, 4]).unwrap(), + data: Serialized::serialize(fake_params.clone()).unwrap(), }); - rt.expect_validate_caller_addr(&[state.from, state.to]); rt.expect_verify_signature( sv.clone().signature.unwrap(), state.to, @@ -687,7 +647,7 @@ mod update_channel_state_extra { ExitCode::Ok, ); let exp_send_params = PaymentVerifyParams { - extra: Serialized::serialize(vec![1, 2, 3, 4]).unwrap(), + extra: Serialized::serialize(fake_params.to_vec()).unwrap(), proof: vec![], }; @@ -697,59 +657,32 @@ mod update_channel_state_extra { Serialized::serialize(exp_send_params).unwrap(), TokenAmount::from(0u8), Serialized::default(), - ExitCode::Ok, + exit_code, + ); + (rt, sv) + } + #[test] + fn extra_call_succeed() { + let bs = MemoryDB::default(); + let (mut rt, sv) = construct_runtime(&bs, ExitCode::Ok); + is_ok( + &mut rt, + Method::UpdateChannelState as u64, + &Serialized::serialize(UpdateChannelStateParams::from(&sv)).unwrap(), ); - let ucp = UpdateChannelStateParams::from(&sv); - assert!(rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap() - ) - .is_ok()); rt.verify(); } #[test] fn extra_call_fail() { let bs = MemoryDB::default(); - let (mut rt, _, mut sv) = require_create_cannel_with_lanes(&bs, 1); - let state: PState = rt.get_state().unwrap(); - let other_addr = Address::new_id(OTHER_ADDR); - rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); - sv.extra = Some(ModVerifyParams { - actor: other_addr, - method: Method::UpdateChannelState as u64, - data: Serialized::serialize([1, 2, 3, 4]).unwrap(), - }); - rt.expect_validate_caller_addr(&[state.from, state.to]); - let exp_send_params = PaymentVerifyParams { - extra: Serialized::serialize(vec![1, 2, 3, 4]).unwrap(), - proof: vec![], - }; - rt.expect_send( - other_addr, + let (mut rt, sv) = construct_runtime(&bs, ExitCode::ErrPlaceholder); + expect_error( + &mut rt, Method::UpdateChannelState as u64, - Serialized::serialize(exp_send_params).unwrap(), - TokenAmount::from(0u8), - Serialized::default(), + &Serialized::serialize(UpdateChannelStateParams::from(&sv)).unwrap(), ExitCode::ErrPlaceholder, ); - rt.expect_verify_signature( - sv.clone().signature.unwrap(), - state.to, - to_vec(&sv).unwrap(), - ExitCode::Ok, - ); - let ucp = UpdateChannelStateParams::from(&sv); - let v = rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap(), - ) - .unwrap_err(); - assert_eq!(v.exit_code(), ExitCode::ErrPlaceholder); rt.verify(); } } @@ -759,30 +692,22 @@ mod update_channel_state_settling { #[test] fn update_channel_setting() { let bs = MemoryDB::default(); - let (mut rt, _, sv) = require_create_cannel_with_lanes(&bs, 1); + let (mut rt, sv) = require_create_cannel_with_lanes(&bs, 1); rt.epoch = 10; let state: PState = rt.get_state().unwrap(); - rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); rt.expect_validate_caller_addr(&[state.from, state.to]); - assert!(rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::Settle as u64, - &Serialized::default() - ) - .is_ok()); + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + is_ok(&mut rt, Method::Settle as u64, &Serialized::default()); let exp_settling_at = SETTLE_DELAY + 10; let state: PState = rt.get_state().unwrap(); assert_eq!(exp_settling_at, state.settling_at); assert_eq!(state.min_settle_height, 0); - struct TestCase { min_settle: u64, exp_min_settle_height: u64, exp_settling_at: u64, } - let test_cases = vec![ TestCase { min_settle: 0, @@ -803,7 +728,6 @@ mod update_channel_state_settling { for tc in test_cases { let mut ucp = UpdateChannelStateParams::from(&sv); - ucp.sv.min_settle_height = tc.min_settle; rt.expect_validate_caller_addr(&[state.from, state.to]); rt.expect_verify_signature( @@ -812,13 +736,11 @@ mod update_channel_state_settling { to_vec(&ucp.sv).unwrap(), ExitCode::Ok, ); - assert!(rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap() - ) - .is_ok()); + is_ok( + &mut rt, + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap(), + ); let new_state: PState = rt.get_state().unwrap(); assert_eq!(tc.exp_settling_at, new_state.settling_at); assert_eq!(tc.exp_min_settle_height, new_state.min_settle_height); @@ -830,55 +752,58 @@ mod secret_preimage { #[test] fn succeed_correct_secret() { let bs = MemoryDB::default(); - let (mut rt, _, sv) = require_create_cannel_with_lanes(&bs, 1); + let (mut rt, sv) = require_create_cannel_with_lanes(&bs, 1); let state: PState = rt.get_state().unwrap(); - let ucp = UpdateChannelStateParams::from(&sv); rt.expect_validate_caller_addr(&[state.from, state.to]); + + let ucp = UpdateChannelStateParams::from(&sv); + rt.expect_verify_signature( ucp.sv.clone().signature.unwrap(), state.to, to_vec(&sv).unwrap(), ExitCode::Ok, ); - assert!(rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap() - ) - .is_ok()); + + is_ok( + &mut rt, + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap(), + ); + rt.verify(); } #[test] fn incorrect_secret() { let bs = MemoryDB::default(); - let (mut rt, _, sv) = require_create_cannel_with_lanes(&bs, 1); + let (mut rt, sv) = require_create_cannel_with_lanes(&bs, 1); + let state: PState = rt.get_state().unwrap(); + rt.expect_validate_caller_addr(&[state.from, state.to]); + let mut ucp = UpdateChannelStateParams { proof: vec![], secret: b"Profesr".to_vec(), sv: sv.clone(), }; let mut mag = b"Magneto".to_vec(); - let mut empty = vec![0; 25]; - mag.append(&mut empty); + mag.append(&mut vec![0; 25]); ucp.sv.secret_pre_image = mag; - rt.expect_validate_caller_addr(&[state.from, state.to]); + rt.expect_verify_signature( ucp.sv.clone().signature.unwrap(), state.to, to_vec(&ucp.sv).unwrap(), ExitCode::Ok, ); - let v = rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap(), - ) - .unwrap_err(); - assert_eq!(v.exit_code(), ExitCode::ErrIllegalArgument); + expect_error( + &mut rt, + Method::UpdateChannelState as u64, + &Serialized::serialize(ucp).unwrap(), + ExitCode::ErrIllegalArgument, + ); + rt.verify(); } } @@ -889,18 +814,14 @@ mod actor_settle { #[test] fn adjust_settling_at() { let bs = MemoryDB::default(); - let (mut rt, _, _sv) = require_create_cannel_with_lanes(&bs, 1); + let (mut rt, _sv) = require_create_cannel_with_lanes(&bs, 1); rt.epoch = EP; let mut state: PState = rt.get_state().unwrap(); rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); rt.expect_validate_caller_addr(&[state.from, state.to]); - assert!(rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::Settle as u64, - &Serialized::default() - ) - .is_ok()); + + is_ok(&mut rt, Method::Settle as u64, &Serialized::default()); + let exp_settling_at = EP + SETTLE_DELAY; state = rt.get_state().unwrap(); assert_eq!(state.settling_at, exp_settling_at); @@ -910,39 +831,32 @@ mod actor_settle { #[test] fn call_twice() { let bs = MemoryDB::default(); - let (mut rt, _, _sv) = require_create_cannel_with_lanes(&bs, 1); + let (mut rt, _sv) = require_create_cannel_with_lanes(&bs, 1); rt.epoch = EP; let state: PState = rt.get_state().unwrap(); rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); rt.expect_validate_caller_addr(&[state.from, state.to]); - assert!(rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::Settle as u64, - &Serialized::default() - ) - .is_ok()); + is_ok(&mut rt, Method::Settle as u64, &Serialized::default()); + rt.expect_validate_caller_addr(&[state.from, state.to]); - assert_eq!( + expect_error( + &mut rt, + Method::Settle as u64, + &Serialized::default(), ExitCode::ErrIllegalState, - rt.call( - &*PAYCH_ACTOR_CODE_ID, - Method::Settle as u64, - &Serialized::default() - ) - .unwrap_err() - .exit_code() ); } #[test] fn settle_if_height_less() { let bs = MemoryDB::default(); - let (mut rt, _, mut sv) = require_create_cannel_with_lanes(&bs, 1); + let (mut rt, mut sv) = require_create_cannel_with_lanes(&bs, 1); rt.epoch = EP; let mut state: PState = rt.get_state().unwrap(); + sv.min_settle_height = (EP + SETTLE_DELAY) + 1; let ucp = UpdateChannelStateParams::from(&sv); + rt.expect_validate_caller_addr(&[state.from, state.to]); rt.expect_verify_signature( ucp.sv.clone().signature.unwrap(), @@ -950,25 +864,17 @@ mod actor_settle { to_vec(&sv).unwrap(), ExitCode::Ok, ); - assert!(rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::UpdateChannelState as u64, - &Serialized::serialize(&ucp).unwrap() - ) - .is_ok()); + is_ok( + &mut rt, + Method::UpdateChannelState as u64, + &Serialized::serialize(&ucp).unwrap(), + ); state = rt.get_state().unwrap(); assert_eq!(state.settling_at, 0); assert_eq!(state.min_settle_height, ucp.sv.min_settle_height); rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); rt.expect_validate_caller_addr(&[state.from, state.to]); - assert!(rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::Settle as u64, - &Serialized::default() - ) - .is_ok()); + is_ok(&mut rt, Method::Settle as u64, &Serialized::default()); state = rt.get_state().unwrap(); assert_eq!(state.settling_at, ucp.sv.min_settle_height); } @@ -976,52 +882,52 @@ mod actor_settle { mod actor_collect { use super::*; - #[test] - fn happy_path() { - let bs = MemoryDB::default(); - let (mut rt, _, _sv) = require_create_cannel_with_lanes(&bs, 1); - rt.epoch = 10; - let mut state: PState = rt.get_state().unwrap(); - rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); - rt.expect_validate_caller_addr(&[state.from, state.to]); - assert!(rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::Settle as u64, - &Serialized::default() - ) - .is_ok()); - state = rt.get_state().unwrap(); - assert_eq!(state.settling_at, 11); - rt.expect_validate_caller_addr(&[state.from, state.to]); + + fn exp_send_multiple( + rt: &mut MockRuntime, + state: &PState, + exit_codes: [ExitCode; 2], + ) { rt.epoch = 12; - let bal = &rt.balance; - let sent_to_from = bal - state.to_send.clone(); + let sent_to_from = &rt.balance - state.to_send.clone(); rt.expect_send( state.from, METHOD_SEND, Serialized::default(), sent_to_from, Serialized::default(), - ExitCode::Ok, + exit_codes[0], ); rt.expect_send( state.to, METHOD_SEND, Serialized::default(), - state.to_send, + state.to_send.to_owned(), Serialized::default(), - ExitCode::Ok, + exit_codes[1], ); - rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.to); rt.expect_validate_caller_addr(&[state.from, state.to]); - assert!(rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::Collect as u64, - &Serialized::default() - ) - .is_ok()); + } + + #[test] + fn happy_path() { + let bs = MemoryDB::default(); + let (mut rt, _sv) = require_create_cannel_with_lanes(&bs, 1); + rt.epoch = 10; + let mut state: PState = rt.get_state().unwrap(); + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); + rt.expect_validate_caller_addr(&[state.from, state.to]); + is_ok(&mut rt, Method::Settle as u64, &Serialized::default()); + state = rt.get_state().unwrap(); + + assert_eq!(state.settling_at, 11); + rt.expect_validate_caller_addr(&[state.from, state.to]); + + exp_send_multiple(&mut rt, &state, [ExitCode::Ok, ExitCode::Ok]); + + rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.to); + + is_ok(&mut rt, Method::Collect as u64, &Serialized::default()); state = rt.get_state().unwrap(); assert_eq!(state.to_send, TokenAmount::from(0u8)); } @@ -1058,72 +964,39 @@ mod actor_collect { for tc in test_cases { let bs = MemoryDB::default(); - let (mut rt, _, _sv) = require_create_cannel_with_lanes(&bs, 1); + let (mut rt, _sv) = require_create_cannel_with_lanes(&bs, 1); rt.epoch = 10; let mut state: PState = rt.get_state().unwrap(); if !tc.dont_settle { rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); rt.expect_validate_caller_addr(&[state.from, state.to]); - assert!(rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::Settle as u64, - &Serialized::default() - ) - .is_ok()); + is_ok(&mut rt, Method::Settle as u64, &Serialized::default()); state = rt.get_state().unwrap(); assert_eq!(state.settling_at, 11); } - // "wait" for SettlingAt epoch - rt.epoch = 12; - - let sent_to_from = &rt.balance - state.to_send.clone(); - rt.expect_send( - state.from, - METHOD_SEND, - Serialized::default(), - sent_to_from, - Serialized::default(), - tc.exp_send_from, - ); - rt.expect_send( - state.to, - METHOD_SEND, - Serialized::default(), - state.to_send, - Serialized::default(), - tc.exp_send_to, - ); + exp_send_multiple(&mut rt, &state, [tc.exp_send_from, tc.exp_send_to]); - // Collect. rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), state.from); - rt.expect_validate_caller_addr(&[state.from, state.to]); - let error = rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::Collect as u64, - &Serialized::default(), - ) - .unwrap_err(); - assert_eq!(tc.exp_send_collect, error.exit_code()); + + expect_error( + &mut rt, + Method::Collect as u64, + &Serialized::default(), + tc.exp_send_collect, + ); } } } -struct ActorHarness { - payer: Address, - payee: Address, -} - fn require_create_cannel_with_lanes<'a, BS: BlockStore>( bs: &'a BS, num_lanes: u64, -) -> (MockRuntime<'a, BS>, ActorHarness, SignedVoucher) { +) -> (MockRuntime<'a, BS>, SignedVoucher) { let paych_addr = Address::new_id(100 as u64); - let payer_addr = Address::new_id(102 as u64); - let payee_addr = Address::new_id(103 as u64); + let payer_addr = Address::new_id(R_PAYER_ADDR); + let payee_addr = Address::new_id(R_PAYEE_ADDR); let balance = TokenAmount::from(100_000 as u64); let recieved = TokenAmount::from(0 as u64); @@ -1162,14 +1035,7 @@ fn require_create_cannel_with_lanes<'a, BS: BlockStore>( last_sv = require_add_new_lane(&mut rt, lane_param); } - ( - rt, - ActorHarness { - payer: payer_addr, - payee: payee_addr, - }, - last_sv, - ) + (rt, last_sv) } fn require_add_new_lane( @@ -1190,17 +1056,11 @@ fn require_add_new_lane( rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), param.from); rt.expect_validate_caller_addr(&[param.from, param.to]); rt.expect_verify_signature(sig.clone(), payee_addr, to_vec(&sv).unwrap(), ExitCode::Ok); - - let ucp = UpdateChannelStateParams::from(&sv); - - assert!(rt - .call( - &*PAYCH_ACTOR_CODE_ID, - Method::UpdateChannelState as u64, - &Serialized::serialize(ucp).unwrap(), - ) - .is_ok()); - + is_ok( + rt, + Method::UpdateChannelState as u64, + &Serialized::serialize(UpdateChannelStateParams::from(&sv)).unwrap(), + ); rt.verify(); SignedVoucher { time_lock_min: param.epoch_num, @@ -1223,13 +1083,11 @@ fn construct_and_verify( to: receiver, }; rt.expect_validate_caller_type(&[INIT_ACTOR_CODE_ID.clone()]); - assert!(rt - .call( - &PAYCH_ACTOR_CODE_ID, - METHOD_CONSTRUCTOR, - &Serialized::serialize(¶ms).unwrap(), - ) - .is_ok()); + is_ok( + rt, + METHOD_CONSTRUCTOR, + &Serialized::serialize(¶ms).unwrap(), + ); rt.verify(); verify_initial_state(rt, sender, receiver); } @@ -1255,7 +1113,6 @@ fn verify_state( assert_eq!(expected_state.min_settle_height, state.min_settle_height); assert_eq!(expected_state.settling_at, state.settling_at); assert_eq!(expected_state.to_send, state.to_send); - if exp_lanes > 0 { assert_eq!(exp_lanes as u64, state.lane_states.len() as u64); assert_eq!(expected_state.lane_states, state.lane_states); From 8840d9fb4700f595efc2d4158e2a7afa47fbd0e3 Mon Sep 17 00:00:00 2001 From: DragonMural Date: Sun, 21 Jun 2020 16:45:53 -0400 Subject: [PATCH 21/29] Lint changes --- vm/actor/tests/paych_actor_test.rs | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/vm/actor/tests/paych_actor_test.rs b/vm/actor/tests/paych_actor_test.rs index 06a393bc2429..47cd7b1733a7 100644 --- a/vm/actor/tests/paych_actor_test.rs +++ b/vm/actor/tests/paych_actor_test.rs @@ -20,7 +20,7 @@ use derive_builder::Builder; use encoding::to_vec; use ipld_blockstore::BlockStore; use message::UnsignedMessage; -use num_bigint::{BigInt, Sign}; +use num_bigint::BigInt; use vm::{ExitCode, Serialized, TokenAmount, METHOD_CONSTRUCTOR, METHOD_SEND}; const R_PAYEE_ADDR: u64 = 103; @@ -80,7 +80,6 @@ mod paych_constructor { let caller_addr = Address::new_id(TEST_CALLER_ADDR); let bs = MemoryDB::default(); let mut rt = construct_runtime(&bs); - rt.actor_code_cids .insert(caller_addr, ACCOUNT_ACTOR_CODE_ID.clone()); construct_and_verify(&mut rt, Address::new_id(TEST_PAYER_ADDR), caller_addr); @@ -90,13 +89,11 @@ mod paych_constructor { fn actor_doesnt_exist_test() { let bs = MemoryDB::default(); let mut rt = construct_runtime(&bs); - rt.expect_validate_caller_type(&[INIT_ACTOR_CODE_ID.clone()]); let params = ConstructorParams { to: Address::new_id(TEST_PAYCH_ADDR), from: Address::new_id(TEST_PAYER_ADDR), }; - expect_error( &mut rt, METHOD_CONSTRUCTOR, @@ -427,14 +424,10 @@ mod update_channel_state_redeem { let state: PState = rt.get_state().unwrap(); let ls_updated: &LaneState = &state.lane_states[1]; - let big_delta = - &sv.amount - BigInt::from_signed_bytes_be(&ls_to_update.redeemed.to_signed_bytes_be()); + let big_delta = &sv.amount - &ls_to_update.redeemed; - let exp_send = big_delta + BigInt::from_signed_bytes_be(&initial_amount.to_radix_be(10)); - assert_eq!( - exp_send, - BigInt::from_radix_be(Sign::Plus, &state.to_send.to_radix_be(10), 10).unwrap() - ); + let exp_send = big_delta.to_biguint().unwrap() + &initial_amount; + assert_eq!(exp_send, state.to_send); assert_eq!(sv.amount, ls_updated.redeemed); assert_eq!(sv.nonce, ls_updated.nonce); } From f98efe0c3b672a0380f8bda45cc3029cea6ff7d6 Mon Sep 17 00:00:00 2001 From: DragonMural Date: Wed, 24 Jun 2020 21:04:22 -0400 Subject: [PATCH 22/29] Adding merge --- tests/serialization-vectors | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/serialization-vectors b/tests/serialization-vectors index 3f4624302374..da34ded5c452 160000 --- a/tests/serialization-vectors +++ b/tests/serialization-vectors @@ -1 +1 @@ -Subproject commit 3f46243023742fa94085634d79c34d727c720c8c +Subproject commit da34ded5c4527860d30df68930049047a51dfb05 From 8c44cd35fd5ef82722f2f137b325b1d28eecf8aa Mon Sep 17 00:00:00 2001 From: DragonMural Date: Wed, 24 Jun 2020 22:15:00 -0400 Subject: [PATCH 23/29] Adding changes --- vm/actor/src/builtin/paych/types.rs | 6 +++--- vm/actor/tests/common/mod.rs | 6 ++++-- vm/actor/tests/paych_actor_test.rs | 22 +++++++++++----------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/vm/actor/src/builtin/paych/types.rs b/vm/actor/src/builtin/paych/types.rs index 9a3cb6c2ff26..a1912bf3bf75 100644 --- a/vm/actor/src/builtin/paych/types.rs +++ b/vm/actor/src/builtin/paych/types.rs @@ -80,12 +80,12 @@ pub struct UpdateChannelStateParams { pub proof: Vec, } -impl From<&SignedVoucher> for UpdateChannelStateParams { - fn from(sv: &SignedVoucher) -> Self { +impl From for UpdateChannelStateParams { + fn from(sv: SignedVoucher) -> Self { UpdateChannelStateParams { proof: vec![], secret: vec![], - sv: sv.clone(), + sv: sv, } } } diff --git a/vm/actor/tests/common/mod.rs b/vm/actor/tests/common/mod.rs index b3810007939b..6602be854e21 100644 --- a/vm/actor/tests/common/mod.rs +++ b/vm/actor/tests/common/mod.rs @@ -142,6 +142,8 @@ where assert!(addr.len() > 0, "addrs must be non-empty"); *self.expect_validate_caller_addr.borrow_mut() = Some(addr.to_vec()); } + + #[allow(dead_code)] pub fn expect_validate_caller_type(&self, ids: &[Cid]) { assert!(ids.len() > 0, "addrs must be non-empty"); *self.expect_validate_caller_type.borrow_mut() = Some(ids.to_vec()); @@ -409,7 +411,7 @@ where self.id_addresses .get(&address) - .map(|x| x.to_owned()) + .cloned() .ok_or(ActorError::new( ExitCode::ErrIllegalArgument, "Address not found".to_string(), @@ -587,7 +589,7 @@ where let op_exp = self.expect_verify_sig.replace(Option::None); if let Some(exp) = op_exp { - if exp.sig == *signature && exp.signer == *signer && (exp.plaintext[..]) == *plaintext { + if exp.sig == *signature && exp.signer == *signer && &exp.plaintext[..] == plaintext { if exp.result == ExitCode::Ok { return Ok(()); } else { diff --git a/vm/actor/tests/paych_actor_test.rs b/vm/actor/tests/paych_actor_test.rs index 47cd7b1733a7..4e18904d7ff4 100644 --- a/vm/actor/tests/paych_actor_test.rs +++ b/vm/actor/tests/paych_actor_test.rs @@ -298,7 +298,7 @@ mod create_lane_tests { ..SignedVoucher::default() }; - let ucp = UpdateChannelStateParams::from(&sv); + let ucp = UpdateChannelStateParams::from(sv.clone()); rt.set_caller(test_case.target_code, payee_addr); rt.expect_validate_caller_addr(&[payer_addr, payee_addr]); @@ -370,7 +370,7 @@ mod update_channel_state_redeem { is_ok( &mut rt, Method::UpdateChannelState as u64, - &Serialized::serialize(UpdateChannelStateParams::from(&sv)).unwrap(), + &Serialized::serialize(UpdateChannelStateParams::from(sv.clone())).unwrap(), ); rt.verify(); @@ -417,7 +417,7 @@ mod update_channel_state_redeem { is_ok( &mut rt, Method::UpdateChannelState as u64, - &Serialized::serialize(UpdateChannelStateParams::from(&sv)).unwrap(), + &Serialized::serialize(UpdateChannelStateParams::from(sv.clone())).unwrap(), ); rt.verify(); @@ -462,7 +462,7 @@ mod merge_tests { expect_error( rt, Method::UpdateChannelState as u64, - &Serialized::serialize(UpdateChannelStateParams::from(&sv)).unwrap(), + &Serialized::serialize(UpdateChannelStateParams::from(sv.clone())).unwrap(), exp_exit_code, ); rt.verify(); @@ -494,7 +494,7 @@ mod merge_tests { is_ok( &mut rt, Method::UpdateChannelState as u64, - &Serialized::serialize(UpdateChannelStateParams::from(&sv)).unwrap(), + &Serialized::serialize(UpdateChannelStateParams::from(sv.clone())).unwrap(), ); rt.verify(); let exp_merge_to = LaneState { @@ -661,7 +661,7 @@ mod update_channel_state_extra { is_ok( &mut rt, Method::UpdateChannelState as u64, - &Serialized::serialize(UpdateChannelStateParams::from(&sv)).unwrap(), + &Serialized::serialize(UpdateChannelStateParams::from(sv.clone())).unwrap(), ); rt.verify(); } @@ -673,7 +673,7 @@ mod update_channel_state_extra { expect_error( &mut rt, Method::UpdateChannelState as u64, - &Serialized::serialize(UpdateChannelStateParams::from(&sv)).unwrap(), + &Serialized::serialize(UpdateChannelStateParams::from(sv.clone())).unwrap(), ExitCode::ErrPlaceholder, ); rt.verify(); @@ -720,7 +720,7 @@ mod update_channel_state_settling { ]; for tc in test_cases { - let mut ucp = UpdateChannelStateParams::from(&sv); + let mut ucp = UpdateChannelStateParams::from(sv.clone()); ucp.sv.min_settle_height = tc.min_settle; rt.expect_validate_caller_addr(&[state.from, state.to]); rt.expect_verify_signature( @@ -749,7 +749,7 @@ mod secret_preimage { let state: PState = rt.get_state().unwrap(); rt.expect_validate_caller_addr(&[state.from, state.to]); - let ucp = UpdateChannelStateParams::from(&sv); + let ucp = UpdateChannelStateParams::from(sv.clone()); rt.expect_verify_signature( ucp.sv.clone().signature.unwrap(), @@ -848,7 +848,7 @@ mod actor_settle { let mut state: PState = rt.get_state().unwrap(); sv.min_settle_height = (EP + SETTLE_DELAY) + 1; - let ucp = UpdateChannelStateParams::from(&sv); + let ucp = UpdateChannelStateParams::from(sv.clone()); rt.expect_validate_caller_addr(&[state.from, state.to]); rt.expect_verify_signature( @@ -1052,7 +1052,7 @@ fn require_add_new_lane( is_ok( rt, Method::UpdateChannelState as u64, - &Serialized::serialize(UpdateChannelStateParams::from(&sv)).unwrap(), + &Serialized::serialize(UpdateChannelStateParams::from(sv.clone())).unwrap(), ); rt.verify(); SignedVoucher { From 8942c53983b3dee5a1323e4fa5f69f32c3145d34 Mon Sep 17 00:00:00 2001 From: DragonMural Date: Wed, 24 Jun 2020 22:15:41 -0400 Subject: [PATCH 24/29] Lock file --- Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.lock b/Cargo.lock index f463a3951909..403c3aba6cd0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -267,6 +267,7 @@ dependencies = [ "byteorder 1.3.4", "clock", "db", + "derive_builder", "fil_types", "forest_address", "forest_bigint", From 348cad896c6080c35976e114b88d59930bc9c2f2 Mon Sep 17 00:00:00 2001 From: DragonMural Date: Wed, 24 Jun 2020 23:40:26 -0400 Subject: [PATCH 25/29] Lint changes --- vm/actor/src/builtin/paych/types.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/actor/src/builtin/paych/types.rs b/vm/actor/src/builtin/paych/types.rs index a1912bf3bf75..41f7b301e111 100644 --- a/vm/actor/src/builtin/paych/types.rs +++ b/vm/actor/src/builtin/paych/types.rs @@ -85,7 +85,7 @@ impl From for UpdateChannelStateParams { UpdateChannelStateParams { proof: vec![], secret: vec![], - sv: sv, + sv, } } } From a99498126b5d50903bc2fa3a319bbf322acfe907 Mon Sep 17 00:00:00 2001 From: DragonMural Date: Thu, 25 Jun 2020 14:19:01 -0400 Subject: [PATCH 26/29] Adding Merge changes --- tests/serialization-vectors | 2 +- vm/actor/tests/common/mod.rs | 14 +++- vm/actor/tests/paych_actor_test.rs | 127 +++++++++++++++-------------- 3 files changed, 77 insertions(+), 66 deletions(-) diff --git a/tests/serialization-vectors b/tests/serialization-vectors index da34ded5c452..3f4624302374 160000 --- a/tests/serialization-vectors +++ b/tests/serialization-vectors @@ -1 +1 @@ -Subproject commit da34ded5c4527860d30df68930049047a51dfb05 +Subproject commit 3f46243023742fa94085634d79c34d727c720c8c diff --git a/vm/actor/tests/common/mod.rs b/vm/actor/tests/common/mod.rs index 2b52df38a91d..aad9e95e8fbb 100644 --- a/vm/actor/tests/common/mod.rs +++ b/vm/actor/tests/common/mod.rs @@ -70,10 +70,10 @@ pub struct ExpectedMessage { #[derive(Clone, Debug)] pub struct ExpectedVerifySig { - sig: Signature, - signer: Address, - plaintext: Vec, - result: ExitCode, + pub sig: Signature, + pub signer: Address, + pub plaintext: Vec, + pub result: ExitCode, } impl<'a, BS> MockRuntime<'a, BS> @@ -143,6 +143,12 @@ where *self.expect_validate_caller_addr.borrow_mut() = Some(addr.to_vec()); } + #[allow(dead_code)] + pub fn expect_verify_signature(&self, exp: ExpectedVerifySig) { + //assert!(addr.len() > 0, "addrs must be non-empty"); + *self.expect_verify_sig.borrow_mut() = Some(exp); + } + #[allow(dead_code)] pub fn expect_validate_caller_type(&self, types: &[Cid]) { assert!(types.len() > 0, "addrs must be non-empty"); diff --git a/vm/actor/tests/paych_actor_test.rs b/vm/actor/tests/paych_actor_test.rs index 4e18904d7ff4..118e2a6bf196 100644 --- a/vm/actor/tests/paych_actor_test.rs +++ b/vm/actor/tests/paych_actor_test.rs @@ -308,12 +308,12 @@ mod create_lane_tests { } else { ExitCode::Ok }; - rt.expect_verify_signature( - sv.clone().signature.unwrap(), - payer_addr, - to_vec(&sv).unwrap(), - exp_exit_code, - ); + rt.expect_verify_signature(ExpectedVerifySig { + sig: sv.clone().signature.unwrap(), + signer: payer_addr, + plaintext: to_vec(&sv).unwrap(), + result: exp_exit_code, + }); } if test_case.exp_exit_code == ExitCode::Ok { @@ -360,12 +360,12 @@ mod update_channel_state_redeem { let payer_addr = Address::new_id(R_PAYER_ADDR); - rt.expect_verify_signature( - sv.signature.clone().unwrap(), - payer_addr, - to_vec(&sv).unwrap(), - ExitCode::Ok, - ); + rt.expect_verify_signature(ExpectedVerifySig { + sig: sv.clone().signature.unwrap(), + signer: payer_addr, + plaintext: to_vec(&sv).unwrap(), + result: ExitCode::Ok, + }); is_ok( &mut rt, @@ -407,12 +407,12 @@ mod update_channel_state_redeem { sv.nonce = ls_to_update.nonce + 1; let payer_addr = Address::new_id(R_PAYER_ADDR); - rt.expect_verify_signature( - sv.clone().signature.unwrap(), - payer_addr, - to_vec(&sv).unwrap(), - ExitCode::Ok, - ); + rt.expect_verify_signature(ExpectedVerifySig { + sig: sv.clone().signature.unwrap(), + signer: payer_addr, + plaintext: to_vec(&sv).unwrap(), + result: ExitCode::Ok, + }); is_ok( &mut rt, @@ -453,12 +453,12 @@ mod merge_tests { exp_exit_code: ExitCode, ) { let payee_addr = Address::new_id(R_PAYEE_ADDR); - rt.expect_verify_signature( - sv.clone().signature.unwrap(), - payee_addr, - to_vec(&sv).unwrap(), - ExitCode::Ok, - ); + rt.expect_verify_signature(ExpectedVerifySig { + sig: sv.clone().signature.unwrap(), + signer: payee_addr, + plaintext: to_vec(&sv).unwrap(), + result: ExitCode::Ok, + }); expect_error( rt, Method::UpdateChannelState as u64, @@ -484,12 +484,12 @@ mod merge_tests { nonce: merge_nonce, }]; let payee_addr = Address::new_id(R_PAYEE_ADDR); - rt.expect_verify_signature( - sv.clone().signature.unwrap(), - payee_addr, - to_vec(&sv).unwrap(), - ExitCode::Ok, - ); + rt.expect_verify_signature(ExpectedVerifySig { + sig: sv.clone().signature.unwrap(), + signer: payee_addr, + plaintext: to_vec(&sv).unwrap(), + result: ExitCode::Ok, + }); is_ok( &mut rt, @@ -633,12 +633,12 @@ mod update_channel_state_extra { method: Method::UpdateChannelState as u64, data: Serialized::serialize(fake_params.clone()).unwrap(), }); - rt.expect_verify_signature( - sv.clone().signature.unwrap(), - state.to, - to_vec(&sv).unwrap(), - ExitCode::Ok, - ); + rt.expect_verify_signature(ExpectedVerifySig { + sig: sv.clone().signature.unwrap(), + signer: state.to, + plaintext: to_vec(&sv).unwrap(), + result: ExitCode::Ok, + }); let exp_send_params = PaymentVerifyParams { extra: Serialized::serialize(fake_params.to_vec()).unwrap(), proof: vec![], @@ -723,12 +723,12 @@ mod update_channel_state_settling { let mut ucp = UpdateChannelStateParams::from(sv.clone()); ucp.sv.min_settle_height = tc.min_settle; rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature( - ucp.sv.clone().signature.unwrap(), - state.to, - to_vec(&ucp.sv).unwrap(), - ExitCode::Ok, - ); + rt.expect_verify_signature(ExpectedVerifySig { + sig: sv.clone().signature.unwrap(), + signer: state.to, + plaintext: to_vec(&ucp.sv).unwrap(), + result: ExitCode::Ok, + }); is_ok( &mut rt, Method::UpdateChannelState as u64, @@ -751,12 +751,12 @@ mod secret_preimage { let ucp = UpdateChannelStateParams::from(sv.clone()); - rt.expect_verify_signature( - ucp.sv.clone().signature.unwrap(), - state.to, - to_vec(&sv).unwrap(), - ExitCode::Ok, - ); + rt.expect_verify_signature(ExpectedVerifySig { + sig: sv.clone().signature.unwrap(), + signer: state.to, + plaintext: to_vec(&sv).unwrap(), + result: ExitCode::Ok, + }); is_ok( &mut rt, @@ -784,12 +784,12 @@ mod secret_preimage { mag.append(&mut vec![0; 25]); ucp.sv.secret_pre_image = mag; - rt.expect_verify_signature( - ucp.sv.clone().signature.unwrap(), - state.to, - to_vec(&ucp.sv).unwrap(), - ExitCode::Ok, - ); + rt.expect_verify_signature(ExpectedVerifySig { + sig: sv.clone().signature.unwrap(), + signer: state.to, + plaintext: to_vec(&sv).unwrap(), + result: ExitCode::Ok, + }); expect_error( &mut rt, Method::UpdateChannelState as u64, @@ -851,12 +851,12 @@ mod actor_settle { let ucp = UpdateChannelStateParams::from(sv.clone()); rt.expect_validate_caller_addr(&[state.from, state.to]); - rt.expect_verify_signature( - ucp.sv.clone().signature.unwrap(), - state.to, - to_vec(&sv).unwrap(), - ExitCode::Ok, - ); + rt.expect_verify_signature(ExpectedVerifySig { + sig: ucp.sv.clone().signature.unwrap(), + signer: state.to, + plaintext: to_vec(&sv).unwrap(), + result: ExitCode::Ok, + }); is_ok( &mut rt, Method::UpdateChannelState as u64, @@ -1048,7 +1048,12 @@ fn require_add_new_lane( }; rt.set_caller(ACCOUNT_ACTOR_CODE_ID.clone(), param.from); rt.expect_validate_caller_addr(&[param.from, param.to]); - rt.expect_verify_signature(sig.clone(), payee_addr, to_vec(&sv).unwrap(), ExitCode::Ok); + rt.expect_verify_signature(ExpectedVerifySig { + sig: sig.clone(), + signer: payee_addr, + plaintext: to_vec(&sv).unwrap(), + result: ExitCode::Ok, + }); is_ok( rt, Method::UpdateChannelState as u64, From 896efe24eddaef6918bb4c5b243d73727d350dbe Mon Sep 17 00:00:00 2001 From: DragonMural Date: Mon, 29 Jun 2020 13:58:48 -0400 Subject: [PATCH 27/29] Missing update --- vm/actor/tests/common/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/actor/tests/common/mod.rs b/vm/actor/tests/common/mod.rs index aad9e95e8fbb..a9f5e9198286 100644 --- a/vm/actor/tests/common/mod.rs +++ b/vm/actor/tests/common/mod.rs @@ -423,7 +423,7 @@ where self.actor_code_cids .get(&addr) - .map(|x| x.to_owned()) + .cloned() .ok_or(ActorError::new( ExitCode::ErrIllegalArgument, "Actor address is not found".to_string(), From 1afe7745197a7ab880effd9da96b0bcc740debec Mon Sep 17 00:00:00 2001 From: DragonMural Date: Fri, 3 Jul 2020 18:28:38 -0400 Subject: [PATCH 28/29] Adding Requested changes --- vm/actor/tests/common/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/vm/actor/tests/common/mod.rs b/vm/actor/tests/common/mod.rs index a9f5e9198286..255173033020 100644 --- a/vm/actor/tests/common/mod.rs +++ b/vm/actor/tests/common/mod.rs @@ -145,7 +145,6 @@ where #[allow(dead_code)] pub fn expect_verify_signature(&self, exp: ExpectedVerifySig) { - //assert!(addr.len() > 0, "addrs must be non-empty"); *self.expect_verify_sig.borrow_mut() = Some(exp); } From 41cabb4b6a06d79bab6f49b0dc5d5aadc34144c4 Mon Sep 17 00:00:00 2001 From: DragonMural Date: Tue, 7 Jul 2020 12:15:52 -0400 Subject: [PATCH 29/29] Updating for TokenAmount updates --- vm/actor/tests/paych_actor_test.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/vm/actor/tests/paych_actor_test.rs b/vm/actor/tests/paych_actor_test.rs index 118e2a6bf196..6fc18680a1bd 100644 --- a/vm/actor/tests/paych_actor_test.rs +++ b/vm/actor/tests/paych_actor_test.rs @@ -697,9 +697,9 @@ mod update_channel_state_settling { assert_eq!(exp_settling_at, state.settling_at); assert_eq!(state.min_settle_height, 0); struct TestCase { - min_settle: u64, - exp_min_settle_height: u64, - exp_settling_at: u64, + min_settle: i64, + exp_min_settle_height: i64, + exp_settling_at: i64, } let test_cases = vec![ TestCase { @@ -803,7 +803,7 @@ mod secret_preimage { mod actor_settle { use super::*; - const EP: u64 = 10; + const EP: i64 = 10; #[test] fn adjust_settling_at() { let bs = MemoryDB::default(); @@ -1039,7 +1039,7 @@ fn require_add_new_lane( let sig = Signature::new_bls(vec![0, 1, 2, 3, 4, 5, 6, 7]); let sv = SignedVoucher { time_lock_min: param.epoch_num, - time_lock_max: u64::MAX, + time_lock_max: i64::MAX, lane: param.lane, nonce: param.nonce, amount: BigInt::from(param.amt), @@ -1062,7 +1062,7 @@ fn require_add_new_lane( rt.verify(); SignedVoucher { time_lock_min: param.epoch_num, - time_lock_max: u64::MAX, + time_lock_max: i64::MAX, lane: param.lane, nonce: param.nonce, amount: BigInt::from(param.amt),