Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

paych actor tests #492

Merged
merged 43 commits into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
e69de24
Adding Payment Actor Tests
RajarupanSampanthan May 31, 2020
7c95e86
Adding lane tests. Need to fix syscalls
RajarupanSampanthan Jun 3, 2020
6f75431
Adding more tests. Need to add one more and then fix syscalls
RajarupanSampanthan Jun 4, 2020
7d24ead
Addding syscalls functinality to mock runtime. plus some more minor c…
RajarupanSampanthan Jun 7, 2020
3a86f47
Adding fmt chaanges
RajarupanSampanthan Jun 7, 2020
fc07ace
Reverting syscall changes and using trait for MockRunTime instead
RajarupanSampanthan Jun 8, 2020
e90b29f
Added one syscall method, one more test passing and fixed minor issues
RajarupanSampanthan Jun 9, 2020
9f3bb02
Adding changes for succesful tests. 2 tests still failing
RajarupanSampanthan Jun 9, 2020
dfaf8bd
Adding serialization vector update
RajarupanSampanthan Jun 9, 2020
0558800
Adding all tests and some sys call fucntionality
RajarupanSampanthan Jun 10, 2020
2e39210
Merge with master
RajarupanSampanthan Jun 10, 2020
4953fe5
Adding warning clean up
RajarupanSampanthan Jun 10, 2020
d90af27
Getting rid of etxra print statements
RajarupanSampanthan Jun 10, 2020
ab9f38f
Fmt changes
RajarupanSampanthan Jun 10, 2020
45398d9
Removed println statements
RajarupanSampanthan Jun 10, 2020
14e4085
Adding touch ups
RajarupanSampanthan Jun 10, 2020
02d0332
Adding PR requested changes
RajarupanSampanthan Jun 12, 2020
f2c1287
Merge branch 'master' into rupan/paych_actor_tests
RajarupanSampanthan Jun 12, 2020
eef423c
MIssing update
RajarupanSampanthan Jun 12, 2020
28cc7e8
Merge branch 'master' into rupan/paych_actor_tests
RajarupanSampanthan Jun 16, 2020
d417739
Adding requested chnages
RajarupanSampanthan Jun 17, 2020
0d44c69
Merge branch 'master' into rupan/paych_actor_tests
RajarupanSampanthan Jun 17, 2020
65cdc3f
CHange requests
RajarupanSampanthan Jun 18, 2020
9c74ba6
Merge branch 'master' into rupan/paych_actor_tests
RajarupanSampanthan Jun 18, 2020
0bbee18
Trying to reduce code size
RajarupanSampanthan Jun 21, 2020
8840d9f
Lint changes
RajarupanSampanthan Jun 21, 2020
12a0d2b
Merge branch 'master' into rupan/paych_actor_tests
RajarupanSampanthan Jun 22, 2020
af829dc
Merge branch 'main' into rupan/paych_actor_tests
RajarupanSampanthan Jun 24, 2020
f98efe0
Adding merge
RajarupanSampanthan Jun 25, 2020
8c44cd3
Adding changes
RajarupanSampanthan Jun 25, 2020
8942c53
Lock file
RajarupanSampanthan Jun 25, 2020
348cad8
Lint changes
RajarupanSampanthan Jun 25, 2020
ec3fb7a
Merge branch 'main' into rupan/paych_actor_tests
RajarupanSampanthan Jun 25, 2020
a994981
Adding Merge changes
RajarupanSampanthan Jun 25, 2020
16d9825
Merge branch 'main' into rupan/paych_actor_tests
RajarupanSampanthan Jun 29, 2020
896efe2
Missing update
RajarupanSampanthan Jun 29, 2020
60ad70d
Merge branch 'main' into rupan/paych_actor_tests
RajarupanSampanthan Jun 29, 2020
097e8c8
Merge branch 'main' into rupan/paych_actor_tests
RajarupanSampanthan Jul 1, 2020
672cbf8
Merge branch 'main' into rupan/paych_actor_tests
RajarupanSampanthan Jul 2, 2020
1afe774
Adding Requested changes
RajarupanSampanthan Jul 3, 2020
c0450a9
Merge branch 'main' into rupan/paych_actor_tests
RajarupanSampanthan Jul 7, 2020
41cabb4
Updating for TokenAmount updates
RajarupanSampanthan Jul 7, 2020
706b933
Merge branch 'main' into rupan/paych_actor_tests
RajarupanSampanthan Jul 8, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vm/actor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ integer-encoding = "1.1.5"
crypto = { package = "forest_crypto", path = "../../crypto" }
bitfield = { path = "../../utils/bitfield" }
fil_types = { path = "../../types" }
derive_builder = "0.9"
byteorder = "1.3.4"
ahash = "0.4"

Expand Down
11 changes: 7 additions & 4 deletions vm/actor/src/builtin/paych/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,11 @@ impl Actor {
st.from
};

let mut sv = params.sv;

let sv = params.sv;
// Pull signature from signed voucher
let sig = sv
.signature
.take()
.as_ref()
.ok_or_else(|| rt.abort(ExitCode::ErrIllegalArgument, "voucher has no signature"))?;

// Generate unsigned bytes
Expand Down Expand Up @@ -211,7 +210,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;
Expand Down Expand Up @@ -349,6 +348,10 @@ impl ActorCode for Actor {
Self::collect(rt)?;
Ok(Serialized::default())
}
Some(Method::UpdateChannelState) => {
Self::update_channel_state(rt, params.deserialize()?)?;
Ok(Serialized::default())
}
_ => Err(rt.abort(ExitCode::SysErrInvalidMethod, "Invalid method")),
}
}
Expand Down
4 changes: 2 additions & 2 deletions vm/actor/src/builtin/paych/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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, Copy, Debug, PartialEq, Serialize_tuple, Deserialize_tuple)]
pub struct Merge {
pub lane: u64,
pub nonce: u64,
Expand Down
14 changes: 12 additions & 2 deletions vm/actor/src/builtin/paych/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -80,6 +80,16 @@ pub struct UpdateChannelStateParams {
pub proof: Vec<u8>,
}

impl From<SignedVoucher> for UpdateChannelStateParams {
fn from(sv: SignedVoucher) -> Self {
UpdateChannelStateParams {
proof: vec![],
secret: vec![],
sv,
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
126 changes: 113 additions & 13 deletions vm/actor/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ use actor::{
use address::Address;
use cid::{multihash::Blake2b256, Cid};
use clock::ChainEpoch;
use crypto::DomainSeparationTag;
use encoding::{de::DeserializeOwned, Cbor};
use crypto::{DomainSeparationTag, Signature};
use encoding::{blake2b_256, de::DeserializeOwned, Cbor};
use fil_types::{PieceInfo, RegisteredSealProof, 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 std::error::Error as StdError;
use vm::{ActorError, ExitCode, MethodNum, Randomness, Serialized, TokenAmount};

pub struct MockRuntime<'a, BS: BlockStore> {
Expand All @@ -33,6 +35,7 @@ pub struct MockRuntime<'a, BS: BlockStore> {
// Actor State
pub state: Option<Cid>,
pub balance: TokenAmount,
pub received: TokenAmount,

// VM Impl
pub in_call: bool,
Expand All @@ -45,6 +48,7 @@ pub struct MockRuntime<'a, BS: BlockStore> {
pub expect_validate_caller_type: RefCell<Option<Vec<Cid>>>,
pub expect_sends: VecDeque<ExpectedMessage>,
pub expect_create_actor: Option<ExpectCreateActor>,
pub expect_verify_sig: RefCell<Option<ExpectedVerifySig>>,
}

#[derive(Clone, Debug)]
Expand All @@ -64,9 +68,20 @@ pub struct ExpectedMessage {
pub exit_code: ExitCode,
}

impl<'a, BS: BlockStore> MockRuntime<'a, BS> {
#[derive(Clone, Debug)]
pub struct ExpectedVerifySig {
pub sig: Signature,
pub signer: Address,
pub plaintext: Vec<u8>,
pub result: ExitCode,
}

impl<'a, BS> MockRuntime<'a, BS>
where
BS: BlockStore,
{
pub fn new(bs: &'a BS, message: UnsignedMessage) -> Self {
Self {
MockRuntime {
RajarupanSampanthan marked this conversation as resolved.
Show resolved Hide resolved
epoch: 0,
caller_type: Cid::default(),

Expand All @@ -79,6 +94,7 @@ impl<'a, BS: BlockStore> MockRuntime<'a, BS> {
message: message,
state: None,
balance: 0u8.into(),
received: 0u8.into(),

// VM Impl
in_call: false,
Expand All @@ -91,6 +107,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: RefCell::new(None),
}
}
fn require_in_call(&self) {
Expand Down Expand Up @@ -126,6 +143,11 @@ impl<'a, BS: BlockStore> MockRuntime<'a, BS> {
*self.expect_validate_caller_addr.borrow_mut() = Some(addr.to_vec());
}

#[allow(dead_code)]
pub fn expect_verify_signature(&self, exp: ExpectedVerifySig) {
*self.expect_verify_sig.borrow_mut() = Some(exp);
}

#[allow(dead_code)]
austinabell marked this conversation as resolved.
Show resolved Hide resolved
pub fn expect_validate_caller_type(&self, types: &[Cid]) {
assert!(types.len() > 0, "addrs must be non-empty");
Expand Down Expand Up @@ -225,6 +247,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.borrow_mut() = None;
}

#[allow(dead_code)]
Expand Down Expand Up @@ -276,7 +299,10 @@ impl<'a, BS: BlockStore> MockRuntime<'a, BS> {
}
}

impl<BS: BlockStore> Runtime<BS> for MockRuntime<'_, BS> {
impl<BS> Runtime<BS> for MockRuntime<'_, BS>
where
BS: BlockStore,
{
RajarupanSampanthan marked this conversation as resolved.
Show resolved Hide resolved
fn message(&self) -> &UnsignedMessage {
self.require_in_call();
&self.message
Expand Down Expand Up @@ -381,14 +407,26 @@ impl<BS: BlockStore> Runtime<BS> for MockRuntime<'_, BS> {
if address.protocol() == address::Protocol::ID {
return Ok(address.clone());
}
let resolved = self.id_addresses.get(&address).unwrap();
return Ok(resolved.clone());

self.id_addresses
.get(&address)
.cloned()
.ok_or(ActorError::new(
ExitCode::ErrIllegalArgument,
"Address not found".to_string(),
))
}

fn get_actor_code_cid(&self, addr: &Address) -> Result<Cid, ActorError> {
self.require_in_call();
let ret = self.actor_code_cids.get(&addr).unwrap();
Ok(ret.clone())

self.actor_code_cids
.get(&addr)
.cloned()
.ok_or(ActorError::new(
ExitCode::ErrIllegalArgument,
"Actor address is not found".to_string(),
))
}

fn get_randomness(
Expand Down Expand Up @@ -528,11 +566,73 @@ impl<BS: BlockStore> Runtime<BS> for MockRuntime<'_, BS> {
todo!("implement me???")
}

fn total_fil_circ_supply(&self) -> Result<TokenAmount, ActorError> {
RajarupanSampanthan marked this conversation as resolved.
Show resolved Hide resolved
unimplemented!();
}

fn syscalls(&self) -> &dyn Syscalls {
unimplemented!()
self
}
}

impl<BS> Syscalls for MockRuntime<'_, BS>
where
BS: BlockStore,
{
fn verify_signature(
&self,
signature: &Signature,
signer: &Address,
plaintext: &[u8],
) -> Result<(), Box<dyn StdError>> {
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 match".to_string(),
)));
}
} else {
return Err(Box::new(ActorError::new(
ExitCode::ErrPlaceholder,
"Expected verify sig not there ".to_string(),
)));
}
}

fn total_fil_circ_supply(&self) -> Result<TokenAmount, ActorError> {
unimplemented!()
fn hash_blake2b(&self, data: &[u8]) -> Result<[u8; 32], Box<dyn StdError>> {
Ok(blake2b_256(&data))
}
fn compute_unsealed_sector_cid(
&self,
_reg: RegisteredSealProof,
_pieces: &[PieceInfo],
) -> Result<Cid, Box<dyn StdError>> {
unimplemented!();
}
fn verify_seal(&self, _vi: &SealVerifyInfo) -> Result<(), Box<dyn StdError>> {
unimplemented!();
}
fn verify_post(&self, _vi: &WindowPoStVerifyInfo) -> Result<(), Box<dyn StdError>> {
unimplemented!();
}
fn verify_consensus_fault(
&self,
_h1: &[u8],
_h2: &[u8],
_extra: &[u8],
) -> Result<Option<ConsensusFault>, Box<dyn StdError>> {
unimplemented!();
}
}
4 changes: 3 additions & 1 deletion vm/actor/tests/cron_actor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use ipld_blockstore::BlockStore;
use message::UnsignedMessage;
use vm::{ExitCode, Serialized};

fn construct_runtime<BS: BlockStore>(bs: &BS) -> MockRuntime<'_, BS> {
fn construct_runtime<'a, BS: BlockStore>(bs: &'a BS) -> MockRuntime<'a, BS> {
let receiver = Address::new_id(100);

let message = UnsignedMessage::builder()
Expand All @@ -28,6 +28,7 @@ fn construct_runtime<BS: BlockStore>(bs: &BS) -> MockRuntime<'_, BS> {
#[test]
fn construct_with_empty_entries() {
let bs = MemoryDB::default();

let mut rt = construct_runtime(&bs);

construct_and_verify(&mut rt, &ConstructorParams { entries: vec![] });
Expand All @@ -39,6 +40,7 @@ fn construct_with_empty_entries() {
#[test]
fn construct_with_entries() {
let bs = MemoryDB::default();

let mut rt = construct_runtime(&bs);

let entry1 = Entry {
Expand Down
12 changes: 6 additions & 6 deletions vm/actor/tests/init_actor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use message::{Message, UnsignedMessage};
use serde::Serialize;
use vm::{ActorError, ExitCode, Serialized, TokenAmount, METHOD_CONSTRUCTOR};

fn construct_runtime<BS: BlockStore>(bs: &BS) -> MockRuntime<'_, BS> {
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())
Expand Down Expand Up @@ -48,7 +48,7 @@ fn abort_cant_call_exec() {
#[test]
fn create_2_payment_channels() {
let bs = MemoryDB::default();
let mut rt: MockRuntime<MemoryDB> = construct_runtime(&bs);
let mut rt = construct_runtime(&bs);
construct_and_verify(&mut rt);
let anne = Address::new_id(1001);

Expand Down Expand Up @@ -112,7 +112,7 @@ fn create_2_payment_channels() {
#[test]
fn create_storage_miner() {
let bs = MemoryDB::default();
let mut rt: MockRuntime<MemoryDB> = construct_runtime(&bs);
let mut rt = construct_runtime(&bs);
construct_and_verify(&mut rt);

// only the storage power actor can create a miner
Expand Down Expand Up @@ -259,7 +259,7 @@ fn sending_constructor_failure() {
);
}

fn construct_and_verify<BS: BlockStore>(rt: &mut MockRuntime<'_, BS>) {
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(),
Expand Down Expand Up @@ -287,8 +287,8 @@ fn construct_and_verify<BS: BlockStore>(rt: &mut MockRuntime<'_, BS>) {
assert_eq!("mock".to_string(), state_data.network_name);
}

fn exec_and_verify<BS: BlockStore, S: Serialize>(
rt: &mut MockRuntime<'_, BS>,
fn exec_and_verify<'a, BS: BlockStore, S: Serialize>(
rt: &mut MockRuntime<'a, BS>,
code_id: Cid,
params: &S,
) -> Result<Serialized, ActorError>
Expand Down
Loading