Skip to content

EVM: fix mockruntime tipset-cid #1154

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

Merged
merged 2 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
52 changes: 52 additions & 0 deletions actors/evm/src/interpreter/instructions/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,59 @@ pub fn base_fee(
#[cfg(test)]
mod tests {
use crate::evm_unit_test;
use cid::Cid;
use fil_actors_evm_shared::uints::U256;
use fvm_ipld_encoding::{DAG_CBOR, IPLD_RAW};

#[test]
fn test_blockhash() {
// truncate to 32 bytes
let counting_byte_hash: Vec<u8> = (0..40u8).collect();
let long_unknown =
Cid::new_v1(IPLD_RAW, multihash::Multihash::wrap(0, &counting_byte_hash).unwrap());
let long_expect = counting_byte_hash[..32].try_into().unwrap();
// multihash code ignored
let cbor_odd_hash =
Cid::new_v1(DAG_CBOR, multihash::Multihash::wrap(123, &[0xfe; 32]).unwrap());
let cbor_odd_expect = [0xfe; 32];

let nothing = [0; 32];

for (current, getting, insert, expect, test) in [
(
12345,
12340u16,
Some(long_unknown),
long_expect,
"truncated tipset hash, (first 32 bytes)",
),
(1234, 1230u16, Some(cbor_odd_hash), cbor_odd_expect, "normal-ish tipset"),
(123, 222u16, None, nothing, "future tipset"),
(1234, 123u16, None, nothing, "requested older than finality (256)"),
] {
let [a, b] = getting.to_be_bytes();
evm_unit_test! {
(rt) {
rt.in_call = true;
rt.set_epoch(current);
rt.tipset_cids.resize(current as usize, Cid::default());
if let Some(cid) = insert {
rt.tipset_cids[getting as usize] = cid;
}
}
(m) {
PUSH2;
{a};
{b};
BLOCKHASH;
}
m.step().expect("execution step failed");
m.step().expect("execution step failed");
assert_eq!(m.state.stack.len(), 1);
assert_eq!(m.state.stack.pop().unwrap(), U256::from(expect), "{}", test);
};
}
}

#[test]
fn test_callvalue() {
Expand Down
6 changes: 3 additions & 3 deletions actors/evm/tests/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ return

let mut rt = util::construct_and_verify(contract);

rt.tipset_cids = (0..900)
rt.tipset_cids = (0..(0xffff + 512))
.map(|i| {
Cid::new_v1(DAG_CBOR, Multihash::wrap(0, format!("block-{:026}", i).as_ref()).unwrap())
})
Expand All @@ -60,14 +60,14 @@ return
let result = util::invoke_contract(&mut rt, &[]);
assert_eq!(
String::from_utf8_lossy(&result.to_vec()),
String::from_utf8_lossy(rt.tipset_cids[2].hash().digest())
String::from_utf8_lossy(rt.tipset_cids[0xffff].hash().digest())
);

rt.epoch = 0xffff + 256;
let result = util::invoke_contract(&mut rt, &[]);
assert_eq!(
String::from_utf8_lossy(&result.to_vec()),
String::from_utf8_lossy(rt.tipset_cids[256].hash().digest())
String::from_utf8_lossy(rt.tipset_cids[0xffff].hash().digest())
);

rt.epoch = 0xffff;
Expand Down
9 changes: 7 additions & 2 deletions runtime/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1285,12 +1285,17 @@ impl<BS: Blockstore> Runtime for MockRuntime<BS> {
}

fn tipset_cid(&self, epoch: i64) -> Result<Cid, ActorError> {
if epoch > self.epoch || epoch < 0 {
let offset = self.epoch - epoch;
// Can't get tipset for epochs:
// - not current or future epoch
// - not negative
// - before current finality
if offset <= 0 || epoch < 0 || offset > 256 {
return Err(
actor_error!(illegal_argument; "invalid epoch to fetch tipset_cid {}", epoch),
);
}
Ok(*self.tipset_cids.get((self.epoch - epoch) as usize).unwrap())
Ok(*self.tipset_cids.get(epoch as usize).unwrap())
}

fn emit_event(&self, event: &ActorEvent) -> Result<(), ActorError> {
Expand Down