From 48eca5eb3570073939f9ec5d1b36d86fc755f3bc Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Wed, 2 Oct 2024 01:51:57 +1000 Subject: [PATCH] chore(power): add CurrentTotalPowerReturn serialization tests (#1580) plus a minor naming fix --- Cargo.lock | 1 + actors/market/src/ext.rs | 2 +- actors/market/src/lib.rs | 2 +- actors/power/Cargo.toml | 1 + actors/power/tests/types_test.rs | 48 ++++++++++++++++++++++++++++++++ 5 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 actors/power/tests/types_test.rs diff --git a/Cargo.lock b/Cargo.lock index 7d5121f43..0bf77446f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1630,6 +1630,7 @@ version = "14.0.0" dependencies = [ "anyhow", "cid 0.10.1", + "const-hex", "fil_actor_reward", "fil_actors_runtime", "frc42_dispatch", diff --git a/actors/market/src/ext.rs b/actors/market/src/ext.rs index 0be8f9d85..b35993ddf 100644 --- a/actors/market/src/ext.rs +++ b/actors/market/src/ext.rs @@ -155,7 +155,7 @@ pub mod power { pub const CURRENT_TOTAL_POWER_METHOD: u64 = 9; #[derive(Serialize_tuple, Deserialize_tuple)] - pub struct CurrentTotalPowerReturnParams { + pub struct CurrentTotalPowerReturn { #[serde(with = "bigint_ser")] pub raw_byte_power: StoragePower, #[serde(with = "bigint_ser")] diff --git a/actors/market/src/lib.rs b/actors/market/src/lib.rs index 2c3b73f74..33c4db155 100644 --- a/actors/market/src/lib.rs +++ b/actors/market/src/lib.rs @@ -1775,7 +1775,7 @@ fn request_current_baseline_power(rt: &impl Runtime) -> Result Result<(StoragePower, StoragePower), ActorError> { - let ret: ext::power::CurrentTotalPowerReturnParams = + let ret: ext::power::CurrentTotalPowerReturn = deserialize_block(extract_send_result(rt.send_simple( &STORAGE_POWER_ACTOR_ADDR, ext::power::CURRENT_TOTAL_POWER_METHOD, diff --git a/actors/power/Cargo.toml b/actors/power/Cargo.toml index c241b4947..df67dcc9c 100644 --- a/actors/power/Cargo.toml +++ b/actors/power/Cargo.toml @@ -33,6 +33,7 @@ fvm_ipld_encoding = { workspace = true } [dev-dependencies] fil_actors_runtime = { workspace = true, features = ["test_utils", "sector-default"] } fil_actor_reward = { workspace = true } +const-hex = { workspace = true } [features] fil-actor = ["fil_actors_runtime/fil-actor"] diff --git a/actors/power/tests/types_test.rs b/actors/power/tests/types_test.rs new file mode 100644 index 000000000..3bef019ca --- /dev/null +++ b/actors/power/tests/types_test.rs @@ -0,0 +1,48 @@ +// Tests to match with Go github.com/filecoin-project/go-state-types/builtin/*/power +mod serialization { + use fil_actor_power::CurrentTotalPowerReturn; + use fvm_ipld_encoding::ipld_block::IpldBlock; + + use fil_actors_runtime::reward::FilterEstimate; + use fvm_shared::bigint::BigInt; + use fvm_shared::econ::TokenAmount; + use fvm_shared::sector::StoragePower; + + #[test] + fn current_total_power_return() { + let test_cases = vec![ + ( + CurrentTotalPowerReturn { + raw_byte_power: Default::default(), + quality_adj_power: Default::default(), + pledge_collateral: Default::default(), + quality_adj_power_smoothed: Default::default(), + ramp_start_epoch: Default::default(), + ramp_duration_epochs: Default::default(), + }, + // [byte[],byte[],byte[],[byte[],byte[]],0,0] + "864040408240400000", + ), + ( + CurrentTotalPowerReturn { + raw_byte_power: StoragePower::from(1 << 20), + quality_adj_power: StoragePower::from(1 << 21), + pledge_collateral: TokenAmount::from_atto(1 << 22), + quality_adj_power_smoothed: FilterEstimate::new(BigInt::from(1 << 23), BigInt::from(1 << 24)), + ramp_start_epoch: 25, + ramp_duration_epochs: 26, + }, + // FilterEstimate BigInts have a precision shift of 128, so they end up larger than the others. + // [byte[00100000],byte[00200000],byte[00400000],[byte[0080000000000000000000000000000000000000],byte[000100000000000000000000000000000000000000]],25,26] + "8644001000004400200000440040000082540080000000000000000000000000000000000000550001000000000000000000000000000000000000001819181a", + ), + ]; + + 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: CurrentTotalPowerReturn = IpldBlock::deserialize(&encoded).unwrap(); + assert_eq!(params, decoded); + } + } +}