-
Notifications
You must be signed in to change notification settings - Fork 153
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
Init test porting #394
Init test porting #394
Changes from 18 commits
83ebe74
612d126
291d545
2c67947
3c09aa4
450ff50
2cb23be
a66432d
9d2178e
f421bd7
c3034d5
5378d43
c878465
9beb783
9a8806d
8e4f52d
fd7280c
f6a74bd
130153d
dfcd35e
12d8192
b3c9d33
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -90,6 +90,7 @@ impl Actor { | |||
rt.create_actor(¶ms.code_cid, &id_address)?; | ||||
|
||||
// Invoke constructor | ||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit
Suggested change
|
||||
rt.send( | ||||
&id_address, | ||||
METHOD_CONSTRUCTOR, | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,22 +12,21 @@ use clock::ChainEpoch; | |
use crypto::DomainSeparationTag; | ||
use encoding::{de::DeserializeOwned, Cbor}; | ||
use ipld_blockstore::BlockStore; | ||
use message::UnsignedMessage; | ||
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}; | ||
|
||
pub struct MockRuntime<'a, BS: BlockStore> { | ||
pub epoch: ChainEpoch, | ||
pub receiver: Address, | ||
pub caller: Address, | ||
pub caller_type: Cid, | ||
pub miner: Address, | ||
pub value_received: TokenAmount, | ||
pub id_addresses: HashMap<Address, Address>, | ||
pub actor_code_cids: HashMap<Address, Cid>, | ||
pub new_actor_addr: Option<Address>, | ||
pub message: UnsignedMessage, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So about this, since all you need from this is caller, receiver, value_received and is currently storing that data twice (see above) you either need to remove the three fields above and handle those through the message type or update the runtime interface to not be hard coded I think it makes sense to just do the former and add a TODO since that change is a bit more involved (cc: @ec2) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah it makes sense. I was thinking we need to make a Message trait. In this case, I think this is the only way to make it work. The message() function returns a reference to a UnsignedMessage, so its not like we can construct the message using the caller, receiever, value_received inside of that function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to be clear about that, it is a bit awkward to use, but only for the mock runtime. I'm not convinced that making it generic is objectively better, can just handle those things through an unsigned message in the mock rt since it's only used for testing, but maybe yeah a bit clunky still. Having the message trait makes the mockrt impl more straightforward, at the cost of the actual runtime being unnecessarily complicated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah. I was thinking about it from the perspective of the MockRt. I dont expect us to be building any more runtimes anyways. |
||
|
||
// TODO: syscalls: syscaller | ||
|
||
|
@@ -69,8 +68,6 @@ impl<'a, BS: BlockStore> MockRuntime<'a, BS> { | |
pub fn new(bs: &'a BS, receiver: Address) -> Self { | ||
austinabell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Self { | ||
epoch: 0, | ||
receiver: receiver, | ||
caller: Address::default(), | ||
caller_type: Cid::default(), | ||
|
||
miner: Address::default(), | ||
|
@@ -80,6 +77,12 @@ impl<'a, BS: BlockStore> MockRuntime<'a, BS> { | |
actor_code_cids: HashMap::new(), | ||
new_actor_addr: None, | ||
|
||
message: UnsignedMessage::builder() | ||
.to(receiver.clone()) | ||
.from(Address::default()) | ||
.build() | ||
.unwrap(), | ||
|
||
state: None, | ||
balance: 0u8.into(), | ||
|
||
|
@@ -231,12 +234,29 @@ impl<'a, BS: BlockStore> MockRuntime<'a, BS> { | |
exit_code, | ||
}) | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn expect_create_actor(&mut self, code_id: Cid, address: Address) { | ||
let a = ExpectCreateActor { code_id, address }; | ||
self.expect_create_actor = Some(a); | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn set_caller(&mut self, code_id: Cid, address: Address) { | ||
self.message = UnsignedMessage::builder() | ||
.to(self.message.to().clone()) | ||
.from(address.clone()) | ||
.build() | ||
.unwrap(); | ||
austinabell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.caller_type = code_id.clone(); | ||
self.actor_code_cids.insert(address, code_id); | ||
} | ||
} | ||
|
||
impl<BS: BlockStore> Runtime<BS> for MockRuntime<'_, BS> { | ||
fn message(&self) -> &UnsignedMessage { | ||
self.require_in_call(); | ||
todo!(); | ||
&self.message | ||
} | ||
|
||
fn curr_epoch(&self) -> ChainEpoch { | ||
|
@@ -275,7 +295,7 @@ impl<BS: BlockStore> Runtime<BS> for MockRuntime<'_, BS> { | |
); | ||
|
||
for expected in &addrs { | ||
if &self.caller == expected { | ||
if self.message().from() == expected { | ||
*self.expect_validate_caller_addr.borrow_mut() = None; | ||
return Ok(()); | ||
} | ||
|
@@ -285,7 +305,8 @@ impl<BS: BlockStore> Runtime<BS> for MockRuntime<'_, BS> { | |
ExitCode::ErrForbidden, | ||
format!( | ||
"caller address {:?} forbidden, allowed: {:?}", | ||
self.caller, &addrs | ||
self.message().from(), | ||
&addrs | ||
), | ||
)); | ||
} | ||
|
@@ -385,6 +406,7 @@ impl<BS: BlockStore> Runtime<BS> for MockRuntime<'_, BS> { | |
self.in_transaction = true; | ||
let ret = f(&mut read_only, &self); | ||
self.state = Some(self.put(&read_only).unwrap()); | ||
self.in_transaction = false; | ||
Ok(ret) | ||
} | ||
|
||
|
@@ -418,7 +440,7 @@ impl<BS: BlockStore> Runtime<BS> for MockRuntime<'_, BS> { | |
|
||
let expected_msg = self.expect_sends.pop_front().unwrap(); | ||
|
||
assert!(&expected_msg.to == to && expected_msg.method == method && &expected_msg.params == params && &expected_msg.value == value, "expectedMessage being sent does not match expectation.\nMessage -\t to: {:?} method: {:?} value: {:?} params: {:?}\nExpected -\t {:?}", to, method, value, params, &self.expect_sends[0]); | ||
assert!(&expected_msg.to == to && expected_msg.method == method && &expected_msg.params == params && &expected_msg.value == value, "expectedMessage being sent does not match expectation.\nMessage -\t to: {:?} method: {:?} value: {:?} params: {:?}\nExpected -\t {:?}", to, method, value, params, self.expect_sends[0]); | ||
|
||
if value > &self.balance { | ||
return Err(self.abort( | ||
|
@@ -431,7 +453,12 @@ impl<BS: BlockStore> Runtime<BS> for MockRuntime<'_, BS> { | |
} | ||
self.balance -= value; | ||
|
||
return Ok(expected_msg.send_return); | ||
match expected_msg.exit_code { | ||
ExitCode::Ok => return Ok(expected_msg.send_return), | ||
x => { | ||
return Err(ActorError::new(x, "Expected message Fail".to_string())); | ||
} | ||
} | ||
} | ||
|
||
fn abort<S: AsRef<str>>(&self, exit_code: ExitCode, msg: S) -> ActorError { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
revert back to what it was, this is ignoring that error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I was confused by this too. In the spec-actors repo they ignore any error or return values. I wasn't sure about why they did it but I just mimicked what they had. Are they doing something wrong?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ohh, you're right. Interesting why that error is ignored, but I guess it makes sense. Possibly add a comment here to indicate that this is intentional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would do
let _ =
instead oflet _v =
but yeah, so weird they dont use the error...