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

Rupan/reward actor tests #403

Merged
merged 8 commits into from
May 11, 2020
26 changes: 24 additions & 2 deletions vm/actor/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ impl<'a, BS: BlockStore> MockRuntime<'a, BS> {
fn _get<T: DeserializeOwned>(&self, cid: Cid) -> Result<T, ActorError> {
Ok(self.store.get(&cid).unwrap().unwrap())
}

#[allow(dead_code)]
pub fn get_state<T: DeserializeOwned>(&self) -> Result<T, ActorError> {
let data: T = self
.store
Expand All @@ -128,6 +130,26 @@ impl<'a, BS: BlockStore> MockRuntime<'a, BS> {
pub fn expect_validate_caller_any(&self) {
self.expect_validate_caller_any.set(true);
}

#[allow(dead_code)]
pub fn expect_call_fail(
&mut self,
to_code: &Cid,
method_num: MethodNum,
params: &Serialized,
expected_code: ExitCode,
) {
// will record previous state, execute call. Will expect a certain exit code. Will throw assertion fail if exit code does not match
// if exit code matches will revert state
austinabell marked this conversation as resolved.
Show resolved Hide resolved
let prev_state = self.state.clone();

let call_result = self.call(to_code, method_num, params);

assert_eq!(expected_code, call_result.unwrap_err().exit_code());

self.state = prev_state;
}
austinabell marked this conversation as resolved.
Show resolved Hide resolved

pub fn call(
&mut self,
to_code: &Cid,
Expand Down Expand Up @@ -161,10 +183,10 @@ impl<'a, BS: BlockStore> MockRuntime<'a, BS> {
actor::paych::Actor.invoke_method(self, method_num, params)
}
x if x == &*MULTISIG_ACTOR_CODE_ID => {
actor::cron::Actor.invoke_method(self, method_num, params)
actor::multisig::Actor.invoke_method(self, method_num, params)
}
x if x == &*REWARD_ACTOR_CODE_ID => {
actor::cron::Actor.invoke_method(self, method_num, params)
actor::reward::Actor.invoke_method(self, method_num, params)
austinabell marked this conversation as resolved.
Show resolved Hide resolved
}
_ => Err(ActorError::new(
ExitCode::SysErrForbidden,
Expand Down
7 changes: 3 additions & 4 deletions vm/actor/tests/init_actor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
mod common;

use actor::{
init::{ConstructorParams, ExecParams, ExecReturn, State},
init::{ConstructorParams, ExecParams, ExecReturn, Method, State},
Multimap, ACCOUNT_ACTOR_CODE_ID, FIRST_NON_SINGLETON_ADDR, INIT_ACTOR_CODE_ID,
MINER_ACTOR_CODE_ID, MULTISIG_ACTOR_CODE_ID, PAYCH_ACTOR_CODE_ID, POWER_ACTOR_CODE_ID,
STORAGE_POWER_ACTOR_ADDR, SYSTEM_ACTOR_ADDR, SYSTEM_ACTOR_CODE_ID,
Expand All @@ -20,7 +20,6 @@ use vm::{ActorError, ExitCode, Serialized, TokenAmount, METHOD_CONSTRUCTOR};

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

let message = UnsignedMessage::builder()
.to(receiver.clone())
.from(SYSTEM_ACTOR_ADDR.clone())
Expand Down Expand Up @@ -268,7 +267,7 @@ fn construct_and_verify<BS: BlockStore>(rt: &mut MockRuntime<'_, BS>) {
let ret = rt
.call(
&*INIT_ACTOR_CODE_ID,
1,
METHOD_CONSTRUCTOR,
&Serialized::serialize(&params).unwrap(),
)
.unwrap();
Expand Down Expand Up @@ -307,7 +306,7 @@ where

let ret = rt.call(
&*INIT_ACTOR_CODE_ID,
2,
Method::Exec as u64,
&Serialized::serialize(&exec_params).unwrap(),
);

Expand Down
69 changes: 69 additions & 0 deletions vm/actor/tests/reward_actor_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

mod common;

use actor::{
reward::{AwardBlockRewardParams, Method},
REWARD_ACTOR_ADDR, REWARD_ACTOR_CODE_ID, SYSTEM_ACTOR_ADDR, SYSTEM_ACTOR_CODE_ID,
};
use address::Address;
use common::*;
use db::MemoryDB;
use ipld_blockstore::BlockStore;
use message::UnsignedMessage;
use vm::{ExitCode, Serialized, TokenAmount, METHOD_CONSTRUCTOR};

fn construct_runtime<BS: BlockStore>(bs: &BS) -> MockRuntime<'_, BS> {
let message = UnsignedMessage::builder()
.to(*REWARD_ACTOR_ADDR)
.from(*SYSTEM_ACTOR_ADDR)
.build()
.unwrap();
let mut rt = MockRuntime::new(bs, message);
rt.caller_type = SYSTEM_ACTOR_CODE_ID.clone();
return rt;
}

#[test]
fn test_balance_less_than_reward() {
let bs = MemoryDB::default();
let mut rt = construct_runtime(&bs);
construct_and_verify(&mut rt);

let miner = Address::new_id(1000);
let gas_reward = TokenAmount::from(10u8);

rt.expect_validate_caller_addr(&[*SYSTEM_ACTOR_ADDR]);

let params = AwardBlockRewardParams {
miner: miner,
penalty: TokenAmount::from(0u8),
gas_reward: gas_reward,
ticket_count: 0,
};

//Expect call to fail because actor doesnt have enough tokens to reward
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//Expect call to fail because actor doesnt have enough tokens to reward
// Expect call to fail because actor doesnt have enough tokens to reward

rt.expect_call_fail(
&*REWARD_ACTOR_CODE_ID,
Method::AwardBlockReward as u64,
&Serialized::serialize(&params).unwrap(),
ExitCode::ErrInsufficientFunds,
);

rt.verify()
}

fn construct_and_verify<BS: BlockStore>(rt: &mut MockRuntime<'_, BS>) {
rt.expect_validate_caller_addr(&[SYSTEM_ACTOR_ADDR.clone()]);
let ret = rt
.call(
&*REWARD_ACTOR_CODE_ID,
METHOD_CONSTRUCTOR,
&Serialized::default(),
)
.unwrap();

assert_eq!(Serialized::default(), ret);
rt.verify();
}
4 changes: 2 additions & 2 deletions vm/interpreter/src/default_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,10 +455,10 @@ where
actor::paych::Actor.invoke_method(runtime, method_num, msg.params())
}
x if x == *MULTISIG_ACTOR_CODE_ID => {
actor::cron::Actor.invoke_method(runtime, method_num, msg.params())
actor::multisig::Actor.invoke_method(runtime, method_num, msg.params())
}
x if x == *REWARD_ACTOR_CODE_ID => {
actor::cron::Actor.invoke_method(runtime, method_num, msg.params())
actor::reward::Actor.invoke_method(runtime, method_num, msg.params())
}
_ => Err(ActorError::new(
ExitCode::SysErrorIllegalActor,
Expand Down