From a45fb87910bca74d62215b0d58ed90cf78b6c8ff Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Thu, 12 Sep 2024 10:58:17 +1000 Subject: [PATCH] chore(miner): add BatchReturn serialization tests (#1564) * chore(miner): add BatchReturn serialization tests * fix(deps): const-hex as a dev depenedency everywhere --- Cargo.lock | 1 + actors/miner/Cargo.toml | 2 +- actors/verifreg/Cargo.toml | 2 +- runtime/Cargo.toml | 1 + runtime/tests/types_test.rs | 54 +++++++++++++++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 runtime/tests/types_test.rs diff --git a/Cargo.lock b/Cargo.lock index 5865774e2..7d5121f43 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1777,6 +1777,7 @@ dependencies = [ "byteorder", "castaway", "cid 0.10.1", + "const-hex", "derive_builder", "fil_actors_runtime", "fvm_ipld_amt", diff --git a/actors/miner/Cargo.toml b/actors/miner/Cargo.toml index 047231f30..bc03e1ae9 100644 --- a/actors/miner/Cargo.toml +++ b/actors/miner/Cargo.toml @@ -33,7 +33,6 @@ lazy_static = { workspace = true } log = { workspace = true } byteorder = { workspace = true } itertools = { workspace = true } -const-hex = { workspace = true } [dev-dependencies] fil_actors_runtime = { workspace = true, features = ["test_utils", "sector-default"] } @@ -43,6 +42,7 @@ fil_actor_power = { workspace = true } fil_actor_market = { workspace = true } rand = { workspace = true } test-case = { workspace = true } +const-hex = { workspace = true } [features] fil-actor = ["fil_actors_runtime/fil-actor"] diff --git a/actors/verifreg/Cargo.toml b/actors/verifreg/Cargo.toml index 66bd676b5..2e003e5a8 100644 --- a/actors/verifreg/Cargo.toml +++ b/actors/verifreg/Cargo.toml @@ -30,9 +30,9 @@ log = { workspace = true } num-derive = { workspace = true } num-traits = { workspace = true } serde = { workspace = true } -const-hex = { workspace = true } [dev-dependencies] +const-hex = { workspace = true } fil_actors_runtime = { workspace = true, features = ["test_utils", "sector-default"] } [features] diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 8056e463d..3379ac93b 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -55,6 +55,7 @@ optional = true derive_builder = { workspace = true } hex = { workspace = true } rand = { workspace = true } +const-hex = { workspace = true } # Enable the test_utils feature when testing. fil_actors_runtime = { workspace = true, features = ["test_utils"] } diff --git a/runtime/tests/types_test.rs b/runtime/tests/types_test.rs new file mode 100644 index 000000000..47fe77d94 --- /dev/null +++ b/runtime/tests/types_test.rs @@ -0,0 +1,54 @@ +// Tests to match with Go github.com/filecoin-project/go-state-types/*/BatchReturn +mod serialization { + use fil_actors_runtime::{BatchReturn, BatchReturnGen}; + use fvm_ipld_encoding::ipld_block::IpldBlock; + use fvm_shared::error::ExitCode; + + #[test] + fn batch_return() { + let mut test_cases = vec![]; + + let mut gen = BatchReturnGen::new(0); + test_cases.push(( + gen.gen(), + // [0,[]] + "820080", + )); + + gen = BatchReturnGen::new(1); + gen.add_success(); + test_cases.push(( + gen.gen(), + // [1,[]] + "820180", + )); + + gen = BatchReturnGen::new(1); + gen.add_fail(ExitCode::USR_ILLEGAL_ARGUMENT); + test_cases.push(( + gen.gen(), + // [0,[[0,16]]] + "820081820010", + )); + + gen = BatchReturnGen::new(5); + gen.add_success(); + gen.add_fail(ExitCode::SYS_OUT_OF_GAS); + gen.add_fail(ExitCode::USR_ILLEGAL_STATE); + gen.add_success(); + gen.add_fail(ExitCode::USR_ILLEGAL_ARGUMENT); + + test_cases.push(( + gen.gen(), + // [2,[[1,7],[2,20],[4,16]]] + "820283820107820214820410", + )); + + for (params, expected_hex) in test_cases { + let encoded = IpldBlock::serialize_cbor(¶ms).unwrap().unwrap(); + assert_eq!(const_hex::encode(&encoded.data), expected_hex); + let decoded: BatchReturn = IpldBlock::deserialize(&encoded).unwrap(); + assert_eq!(params, decoded); + } + } +}