Skip to content

fix: filecoin read state API #5620

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
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
122015d
fix: filecoin read state API
akaladarshi May 2, 2025
7632a53
Merge branch 'main' into akaladarshi/fix-state-read-state-api
akaladarshi May 5, 2025
36f6b6f
refactor: add errors in actor registry
akaladarshi May 13, 2025
5f5c3e2
bump up the fil actor crate to the latest version
akaladarshi May 15, 2025
cd03c1a
add LotusJson support for miner and vesting fund actor states
akaladarshi May 15, 2025
c40562a
add LotusJson support for market actor states
akaladarshi May 15, 2025
3ded21c
add LotusJson support for evm actor state
akaladarshi May 15, 2025
db0dfe1
uncomment dev profile
akaladarshi May 15, 2025
b55215a
Update fil-actor-state crate dependencies to version 22.2
akaladarshi May 16, 2025
2d459b3
Merge branch 'main' into akaladarshi/fix-state-read-state-api
akaladarshi May 16, 2025
49d4c70
Add LotusJson support for System actor state
akaladarshi May 16, 2025
f31d046
Merge branch 'main' into akaladarshi/fix-state-read-state-api
akaladarshi May 18, 2025
2abb98e
Merge branch 'main' into akaladarshi/fix-state-read-state-api
akaladarshi May 20, 2025
679e093
Merge branch 'main' into akaladarshi/fix-state-read-state-api
akaladarshi May 21, 2025
bf394ee
add all the actor types for read state api in state tests to generate…
akaladarshi May 21, 2025
405b892
add new test snapshots for read state API
akaladarshi May 22, 2025
99c82df
Merge branch 'main' into akaladarshi/fix-state-read-state-api
akaladarshi May 27, 2025
0874d91
address comments
akaladarshi May 24, 2025
9ccedbb
Merge branch 'main' into akaladarshi/fix-state-read-state-api
akaladarshi May 27, 2025
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
28 changes: 14 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,20 @@ either = "1"
enumflags2 = "0.7"
ethereum-types = { version = "0.15", features = ["ethbloom"] }
ez-jsonrpc-types = "0.5"
fil_actor_account_state = { version = "22" }
fil_actor_cron_state = { version = "22" }
fil_actor_datacap_state = { version = "22" }
fil_actor_eam_state = { version = "22" }
fil_actor_evm_state = { version = "22" }
fil_actor_init_state = { version = "22" }
fil_actor_market_state = { version = "22" }
fil_actor_miner_state = { version = "22" }
fil_actor_multisig_state = { version = "22" }
fil_actor_power_state = { version = "22" }
fil_actor_reward_state = { version = "22" }
fil_actor_system_state = { version = "22" }
fil_actor_verifreg_state = { version = "22" }
fil_actors_shared = { version = "22", features = ["json"] }
fil_actor_account_state = { version = "22.2" }
fil_actor_cron_state = { version = "22.2" }
fil_actor_datacap_state = { version = "22.2" }
fil_actor_eam_state = { version = "22.2" }
fil_actor_evm_state = { version = "22.2" }
fil_actor_init_state = { version = "22.2" }
fil_actor_market_state = { version = "22.2" }
fil_actor_miner_state = { version = "22.2" }
fil_actor_multisig_state = { version = "22.2" }
fil_actor_power_state = { version = "22.2" }
fil_actor_reward_state = { version = "22.2" }
fil_actor_system_state = { version = "22.2" }
fil_actor_verifreg_state = { version = "22.2" }
fil_actors_shared = { version = "22.2", features = ["json"] }
flate2 = "1"
flume = { workspace = true }
fs_extra = "1"
Expand Down
44 changes: 44 additions & 0 deletions src/lotus_json/actor_states/account_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2019-2025 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT
use super::*;
use crate::shim::{actors::account, address::Address};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
pub struct AccountStateLotusJson {
#[schemars(with = "LotusJson<Address>")]
#[serde(with = "crate::lotus_json")]
address: Address,
}

impl HasLotusJson for account::State {
type LotusJson = AccountStateLotusJson;

#[cfg(test)]
fn snapshots() -> Vec<(serde_json::Value, Self)> {
vec![(
json!({
"Address": "f00"
}),
// Create a test account state
account::State::V16(fil_actor_account_state::v16::State {
address: Address::default().into(),
}),
)]
}

fn into_lotus_json(self) -> Self::LotusJson {
AccountStateLotusJson {
address: self.pubkey_address().into(),
}
}

fn from_lotus_json(lotus_json: Self::LotusJson) -> Self {
// using V16 as a default version, because there is no way of knowing
// which version data belongs to.
account::State::V16(fil_actor_account_state::v16::State {
address: lotus_json.address.into(),
})
}
}
101 changes: 101 additions & 0 deletions src/lotus_json/actor_states/cron_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright 2019-2025 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT
use super::*;
use crate::shim::actors::cron::{Entry, State};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
pub struct CronStateLotusJson {
#[schemars(with = "LotusJson<Vec<Entry>>")]
#[serde(with = "crate::lotus_json")]
pub entries: Vec<Entry>,
}

impl HasLotusJson for State {
type LotusJson = CronStateLotusJson;

#[cfg(test)]
fn snapshots() -> Vec<(serde_json::Value, Self)> {
use crate::shim::address::Address;
vec![(
json!({
"Entries": [
{
"Receiver": "f01",
"MethodNum": 2
},
{
"Receiver": "f02",
"MethodNum": 3
}
]
}),
// Create a test cron state with some entries
State::V16(fil_actor_cron_state::v16::State {
entries: vec![
fil_actor_cron_state::v16::Entry {
receiver: Address::new_id(1).into(),
method_num: 2,
},
fil_actor_cron_state::v16::Entry {
receiver: Address::new_id(2).into(),
method_num: 3,
},
],
}),
)]
}

fn into_lotus_json(self) -> Self::LotusJson {
match self {
State::V8(s) => CronStateLotusJson {
entries: s.entries.into_iter().map(Entry::V8).collect(),
},
State::V9(s) => CronStateLotusJson {
entries: s.entries.into_iter().map(Entry::V9).collect(),
},
State::V10(s) => CronStateLotusJson {
entries: s.entries.into_iter().map(Entry::V10).collect(),
},
State::V11(s) => CronStateLotusJson {
entries: s.entries.into_iter().map(Entry::V11).collect(),
},
State::V12(s) => CronStateLotusJson {
entries: s.entries.into_iter().map(Entry::V12).collect(),
},
State::V13(s) => CronStateLotusJson {
entries: s.entries.into_iter().map(Entry::V13).collect(),
},
State::V14(s) => CronStateLotusJson {
entries: s.entries.into_iter().map(Entry::V14).collect(),
},
State::V15(s) => CronStateLotusJson {
entries: s.entries.into_iter().map(Entry::V15).collect(),
},
State::V16(s) => CronStateLotusJson {
entries: s.entries.into_iter().map(Entry::V16).collect(),
},
}
}

fn from_lotus_json(lotus_json: Self::LotusJson) -> Self {
// Default to the latest version (V16)
State::V16(fil_actor_cron_state::v16::State {
entries: lotus_json
.entries
.into_iter()
.map(|entry| match entry {
Entry::V16(e) => e,
_ => {
let lotus_entry = entry.into_lotus_json();
fil_actor_cron_state::v16::Entry {
receiver: lotus_entry.receiver.into(),
method_num: lotus_entry.method_num,
}
}
})
.collect(),
})
}
}
82 changes: 82 additions & 0 deletions src/lotus_json/actor_states/entry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2019-2025 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT
use super::*;
use crate::shim::actors::cron::Entry;
use crate::shim::address::Address;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
pub struct EntryLotusJson {
#[schemars(with = "LotusJson<Address>")]
#[serde(with = "crate::lotus_json")]
pub receiver: Address,
pub method_num: u64,
}

impl HasLotusJson for Entry {
type LotusJson = EntryLotusJson;

#[cfg(test)]
fn snapshots() -> Vec<(serde_json::Value, Self)> {
vec![(
json!({
"Receiver": "f00",
"MethodNum": 10
}),
// Create a test entry
Entry::V16(fil_actor_cron_state::v16::Entry {
receiver: Default::default(),
method_num: 0,
}),
)]
}

fn into_lotus_json(self) -> Self::LotusJson {
match self {
Entry::V8(e) => EntryLotusJson {
receiver: e.receiver.into(),
method_num: e.method_num,
},
Entry::V9(e) => EntryLotusJson {
receiver: e.receiver.into(),
method_num: e.method_num,
},
Entry::V10(e) => EntryLotusJson {
receiver: e.receiver.into(),
method_num: e.method_num,
},
Entry::V11(e) => EntryLotusJson {
receiver: e.receiver.into(),
method_num: e.method_num,
},
Entry::V12(e) => EntryLotusJson {
receiver: e.receiver.into(),
method_num: e.method_num,
},
Entry::V13(e) => EntryLotusJson {
receiver: e.receiver.into(),
method_num: e.method_num,
},
Entry::V14(e) => EntryLotusJson {
receiver: e.receiver.into(),
method_num: e.method_num,
},
Entry::V15(e) => EntryLotusJson {
receiver: e.receiver.into(),
method_num: e.method_num,
},
Entry::V16(e) => EntryLotusJson {
receiver: e.receiver.into(),
method_num: e.method_num,
},
}
}

fn from_lotus_json(lotus_json: Self::LotusJson) -> Self {
Entry::V16(fil_actor_cron_state::v16::Entry {
receiver: lotus_json.receiver.into(),
method_num: lotus_json.method_num,
})
}
}
100 changes: 100 additions & 0 deletions src/lotus_json/actor_states/evm_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright 2019-2025 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use super::*;
use crate::shim::actors::evm::{State, TombstoneState};
use ::cid::Cid;
use fil_actor_evm_state::v16::{BytecodeHash, Tombstone, TransientData};

#[derive(Debug, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
#[schemars(rename = "EVMState")]
pub struct EVMStateLotusJson {
#[schemars(with = "LotusJson<Cid>")]
#[serde(with = "crate::lotus_json")]
pub bytecode: Cid,
#[schemars(with = "LotusJson<BytecodeHash>")]
#[serde(with = "crate::lotus_json")]
pub bytecode_hash: BytecodeHash,

#[schemars(with = "LotusJson<Cid>")]
#[serde(with = "crate::lotus_json")]
pub contract_state: Cid,

#[schemars(with = "LotusJson<Option<TransientData>>")]
#[serde(with = "crate::lotus_json", skip_serializing_if = "Option::is_none")]
pub transient_data: Option<Option<TransientData>>, // only available in evm actor state v16

pub nonce: u64,

#[schemars(with = "LotusJson<Option<TombstoneState>>")]
#[serde(with = "crate::lotus_json")]
pub tombstone: Option<TombstoneState>,
}

macro_rules! common_evm_state_fields {
($state:expr, $version:ident) => {{
let data: [u8; 32] = $state.bytecode_hash.into();
EVMStateLotusJson {
bytecode: $state.bytecode.clone(),
bytecode_hash: BytecodeHash::from(data),
contract_state: $state.contract_state.clone(),
nonce: $state.nonce,
tombstone: $state.tombstone.map(|t| TombstoneState::$version(t)),
transient_data: None,
}
}};
}

macro_rules! impl_evm_state_lotus_json {
($($version:ident),*) => {
impl HasLotusJson for State {
type LotusJson = EVMStateLotusJson;

#[cfg(test)]
fn snapshots() -> Vec<(serde_json::Value, Self)> {
vec![]
}

fn into_lotus_json(self) -> Self::LotusJson {
match self {
State::V16(state) => {
EVMStateLotusJson {
transient_data: Option::from(state.transient_data),
..common_evm_state_fields!(state, V16)
}
},
$(
State::$version(state) => {
EVMStateLotusJson {
..common_evm_state_fields!(state, $version)
}
},
)*
}
}

fn from_lotus_json(lotus_json: Self::LotusJson) -> Self {
State::V16(fil_actor_evm_state::v16::State{
bytecode: lotus_json.bytecode,
bytecode_hash: lotus_json.bytecode_hash.into(),
contract_state: lotus_json.contract_state,
transient_data: lotus_json.transient_data.unwrap_or_default(),
nonce: lotus_json.nonce,
tombstone: lotus_json.tombstone.map(|t| match t {
TombstoneState::V16(t) => t,
_ => {
let lotus_entry = t.into_lotus_json();
Tombstone {
origin: lotus_entry.orign,
nonce: lotus_entry.nonce,
}
}
}),
})
}
}
};
}

impl_evm_state_lotus_json!(V15, V14, V13, V12, V11, V10);
Loading
Loading