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/runtime/Cargo.toml b/runtime/Cargo.toml index 8056e463d..7b1c1bd0e 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -34,6 +34,7 @@ serde_repr = { workspace = true } thiserror = { workspace = true } unsigned-varint = { workspace = true } vm_api = { workspace = true } +const-hex = { workspace = true } # A fake-proofs dependency but... we can't select on that feature here because we enable it from # build.rs. 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); + } + } +}