forest/shim/
state_tree_v0.rs1use cid::Cid;
11use fil_actors_shared::fvm_ipld_hamt::Hamtv0 as Hamt;
12use fvm_ipld_blockstore::Blockstore;
13use fvm_ipld_encoding::CborStore;
14
15use super::state_tree::StateRoot;
16use super::state_tree::StateTreeVersion;
17use crate::shim::address::Address;
18pub use fvm2::state_tree::ActorState as ActorStateV2;
19
20const HAMTV0_BIT_WIDTH: u32 = 5;
21
22pub struct StateTreeV0<S> {
26 hamt: Hamt<S, ActorStateV2>,
27}
28
29impl<S> StateTreeV0<S>
30where
31 S: Blockstore,
32{
33 pub fn new_from_root(store: S, c: &Cid) -> anyhow::Result<Self> {
35 let (version, actors) = if let Ok(Some(StateRoot {
37 version, actors, ..
38 })) = store.get_cbor(c)
39 {
40 (StateTreeVersion::from(version), actors)
41 } else {
42 (StateTreeVersion::V0, *c)
44 };
45
46 match version {
47 StateTreeVersion::V0 => {
48 let hamt = Hamt::load_with_bit_width(&actors, store, HAMTV0_BIT_WIDTH)?;
49 Ok(Self { hamt })
50 }
51 _ => anyhow::bail!("unsupported state tree version: {:?}", version),
52 }
53 }
54
55 pub fn store(&self) -> &S {
57 self.hamt.store()
58 }
59
60 pub fn get_actor(&self, addr: &Address) -> anyhow::Result<Option<ActorStateV2>> {
62 let addr = match self.lookup_id(addr)? {
63 Some(addr) => addr,
64 None => return Ok(None),
65 };
66
67 let act = self.hamt.get(&addr.to_bytes())?.cloned();
69
70 Ok(act)
71 }
72
73 pub fn lookup_id(&self, addr: &Address) -> anyhow::Result<Option<Address>> {
75 if addr.protocol() == fvm_shared4::address::Protocol::ID {
76 return Ok(Some(*addr));
77 }
78 anyhow::bail!("StateTreeV0::lookup_id is only defined for ID addresses")
79 }
80}