use cid::Cid;
use fil_actors_shared::actor_error_v9;
use fil_actors_shared::v9::{
make_empty_map, make_map_with_root_and_bitwidth, ActorError, AsActorError,
FIRST_NON_SINGLETON_ADDR,
};
use fvm_ipld_blockstore::Blockstore;
use fvm_ipld_encoding::tuple::*;
use fvm_shared::address::{Address, Protocol};
use fvm_shared::error::ExitCode;
use fvm_shared::{ActorID, HAMT_BIT_WIDTH};
#[derive(Serialize_tuple, Deserialize_tuple, Clone, Debug)]
pub struct State {
pub address_map: Cid,
pub next_id: ActorID,
pub network_name: String,
}
impl State {
pub fn new<BS: Blockstore>(store: &BS, network_name: String) -> Result<Self, ActorError> {
let empty_map = make_empty_map::<_, ()>(store, HAMT_BIT_WIDTH)
.flush()
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to create empty map")?;
Ok(Self {
address_map: empty_map,
next_id: FIRST_NON_SINGLETON_ADDR,
network_name,
})
}
pub fn map_address_to_new_id<BS: Blockstore>(
&mut self,
store: &BS,
addr: &Address,
) -> Result<ActorID, ActorError> {
let id = self.next_id;
self.next_id += 1;
let mut map = make_map_with_root_and_bitwidth(&self.address_map, store, HAMT_BIT_WIDTH)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load address map")?;
let is_new = map
.set_if_absent(addr.to_bytes().into(), id)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to set map key")?;
if !is_new {
return Err(actor_error_v9!(
forbidden,
"robust address {} is already allocated in the address map",
addr
));
}
self.address_map = map
.flush()
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to store address map")?;
Ok(id)
}
pub fn resolve_address<BS: Blockstore>(
&self,
store: &BS,
addr: &Address,
) -> Result<Option<Address>, ActorError> {
if addr.protocol() == Protocol::ID {
return Ok(Some(*addr));
}
let map = make_map_with_root_and_bitwidth(&self.address_map, store, HAMT_BIT_WIDTH)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load address map")?;
let found = map
.get(&addr.to_bytes())
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to get address entry")?;
Ok(found.copied().map(Address::new_id))
}
}