use anyhow::anyhow;
use cid::Cid;
use fil_actor_verifreg_state::v13::ClaimID;
use fil_actor_verifreg_state::{
v10::state::get_claim as get_claim_v10, v11::state::get_claim as get_claim_v11,
v12::state::get_claim as get_claim_v12, v13::state::get_claim as get_claim_v13,
v14::state::get_claim as get_claim_v14, v9::state::get_claim as get_claim_v9,
};
use fil_actors_shared::v8::{make_map_with_root_and_bitwidth, HAMT_BIT_WIDTH};
use fil_actors_shared::v9::Keyer;
use fvm_ipld_blockstore::Blockstore;
use fvm_ipld_encoding::tuple::serde_tuple;
use fvm_ipld_encoding::tuple::{Deserialize_tuple, Serialize_tuple};
use fvm_shared::address::{Address, Protocol};
use fvm_shared4::bigint::bigint_ser::BigIntDe;
use fvm_shared4::clock::ChainEpoch;
use fvm_shared4::piece::PaddedPieceSize;
use fvm_shared4::sector::SectorNumber;
use fvm_shared4::ActorID;
use num::BigInt;
use serde::{Deserialize, Serialize};
pub const ADDRESS: Address = Address::new_id(6);
#[derive(Serialize, Debug)]
#[serde(untagged)]
pub enum State {
V8(fil_actor_verifreg_state::v8::State),
V9(fil_actor_verifreg_state::v9::State),
V10(fil_actor_verifreg_state::v10::State),
V11(fil_actor_verifreg_state::v11::State),
V12(fil_actor_verifreg_state::v12::State),
V13(fil_actor_verifreg_state::v13::State),
V14(fil_actor_verifreg_state::v14::State),
}
impl State {
pub fn verified_client_data_cap<BS>(
&self,
store: &BS,
addr: Address,
) -> anyhow::Result<Option<BigInt>>
where
BS: Blockstore,
{
if addr.protocol() != Protocol::ID {
return Err(anyhow!("can only look up ID addresses"));
}
match self {
State::V8(state) => {
let vh = make_map_with_root_and_bitwidth(
&state.verified_clients,
store,
HAMT_BIT_WIDTH,
)?;
Ok(vh.get(&addr.key())?.map(|int: &BigIntDe| int.0.to_owned()))
}
_ => Err(anyhow!("not supported in actors > v8")),
}
}
pub fn verifier_data_cap<BS>(&self, store: &BS, addr: Address) -> anyhow::Result<Option<BigInt>>
where
BS: Blockstore,
{
if addr.protocol() != Protocol::ID {
return Err(anyhow!("can only look up ID addresses"));
}
match self {
State::V8(state) => {
let vh = make_map_with_root_and_bitwidth(&state.verifiers, store, HAMT_BIT_WIDTH)?;
Ok(vh.get(&addr.key())?.map(|int: &BigIntDe| int.0.to_owned()))
}
State::V9(state) => {
let vh = make_map_with_root_and_bitwidth(&state.verifiers, store, HAMT_BIT_WIDTH)?;
Ok(vh.get(&addr.key())?.map(|int: &BigIntDe| int.0.to_owned()))
}
State::V10(state) => {
let vh = make_map_with_root_and_bitwidth(&state.verifiers, store, HAMT_BIT_WIDTH)?;
Ok(vh.get(&addr.key())?.map(|int: &BigIntDe| int.0.to_owned()))
}
State::V11(state) => {
let vh = make_map_with_root_and_bitwidth(&state.verifiers, store, HAMT_BIT_WIDTH)?;
Ok(vh.get(&addr.key())?.map(|int: &BigIntDe| int.0.to_owned()))
}
State::V12(state) => {
let vh = make_map_with_root_and_bitwidth(&state.verifiers, store, HAMT_BIT_WIDTH)?;
Ok(vh.get(&addr.key())?.map(|int: &BigIntDe| int.0.to_owned()))
}
State::V13(state) => {
let vh = make_map_with_root_and_bitwidth(&state.verifiers, store, HAMT_BIT_WIDTH)?;
Ok(vh.get(&addr.key())?.map(|int: &BigIntDe| int.0.to_owned()))
}
State::V14(state) => {
let vh = make_map_with_root_and_bitwidth(&state.verifiers, store, HAMT_BIT_WIDTH)?;
Ok(vh.get(&addr.key())?.map(|int: &BigIntDe| int.0.to_owned()))
}
}
}
pub fn get_allocation<BS>(
&self,
store: &BS,
addr: ActorID,
allocation_id: AllocationID,
) -> anyhow::Result<Option<Allocation>>
where
BS: Blockstore,
{
match self {
State::V8(_) => Err(anyhow!("unsupported in actors v8")),
State::V9(state) => {
let mut map = state.load_allocs(store)?;
Ok(fil_actor_verifreg_state::v9::state::get_allocation(
&mut map,
addr,
allocation_id,
)?
.map(Allocation::from))
}
State::V10(state) => {
let mut map = state.load_allocs(store)?;
Ok(fil_actor_verifreg_state::v10::state::get_allocation(
&mut map,
addr,
allocation_id,
)?
.map(Allocation::from))
}
State::V11(state) => {
let mut map = state.load_allocs(store)?;
Ok(fil_actor_verifreg_state::v11::state::get_allocation(
&mut map,
addr,
allocation_id,
)?
.map(Allocation::from))
}
State::V12(state) => {
let mut map = state.load_allocs(store)?;
Ok(fil_actor_verifreg_state::v12::state::get_allocation(
&mut map,
addr,
allocation_id,
)?
.map(Allocation::from))
}
State::V13(state) => {
let mut map = state.load_allocs(store)?;
Ok(fil_actor_verifreg_state::v13::state::get_allocation(
&mut map,
addr,
allocation_id,
)?
.map(Allocation::from))
}
State::V14(state) => {
let mut map = state.load_allocs(store)?;
Ok(fil_actor_verifreg_state::v14::state::get_allocation(
&mut map,
addr,
allocation_id,
)?
.map(Allocation::from))
}
}
}
pub fn get_claim<BS>(
&self,
store: &BS,
addr: Address,
claim_id: ClaimID,
) -> anyhow::Result<Option<Claim>>
where
BS: Blockstore,
{
let provider_id = addr.id()?;
match self {
State::V8(_) => Err(anyhow!("unsupported in actors v8")),
State::V9(state) => {
Ok(
get_claim_v9(&mut state.load_claims(store)?, provider_id, claim_id)?
.map(Claim::from),
)
}
State::V10(state) => {
Ok(
get_claim_v10(&mut state.load_claims(store)?, provider_id, claim_id)?
.map(Claim::from),
)
}
State::V11(state) => {
Ok(
get_claim_v11(&mut state.load_claims(store)?, provider_id, claim_id)?
.map(Claim::from),
)
}
State::V12(state) => {
Ok(
get_claim_v12(&mut state.load_claims(store)?, provider_id, claim_id)?
.map(Claim::from),
)
}
State::V13(state) => {
Ok(
get_claim_v13(&mut state.load_claims(store)?, provider_id, claim_id)?
.map(Claim::from),
)
}
State::V14(state) => {
Ok(
get_claim_v14(&mut state.load_claims(store)?, provider_id, claim_id)?
.map(Claim::from),
)
}
}
}
}
#[derive(Serialize_tuple, Deserialize_tuple, Clone, Debug, PartialEq, Eq)]
pub struct Claim {
pub provider: ActorID,
pub client: ActorID,
pub data: Cid,
pub size: PaddedPieceSize,
pub term_min: ChainEpoch,
pub term_max: ChainEpoch,
pub term_start: ChainEpoch,
pub sector: SectorNumber,
}
macro_rules! from_claim {
($($type:ty),* $(,)*) => {
$(
impl From<&$type> for Claim {
fn from(claim: &$type) -> Self {
Self {
provider: claim.provider,
client: claim.client,
data: claim.data,
size: PaddedPieceSize(claim.size.0),
term_min: claim.term_min,
term_max: claim.term_max,
term_start: claim.term_start,
sector: claim.sector,
}
}
}
)*
};
}
from_claim!(
fil_actor_verifreg_state::v14::Claim,
fil_actor_verifreg_state::v13::Claim,
fil_actor_verifreg_state::v12::Claim,
fil_actor_verifreg_state::v11::Claim,
fil_actor_verifreg_state::v10::Claim,
fil_actor_verifreg_state::v9::Claim,
);
pub type AllocationID = u64;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct Allocation {
pub client: ActorID,
pub provider: ActorID,
pub data: Cid,
pub size: PaddedPieceSize,
pub term_min: ChainEpoch,
pub term_max: ChainEpoch,
pub expiration: ChainEpoch,
}
macro_rules! from_allocation {
($type: ty) => {
impl From<&$type> for Allocation {
fn from(alloc: &$type) -> Self {
Self {
client: alloc.client,
provider: alloc.provider,
data: alloc.data,
size: PaddedPieceSize(alloc.size.0),
term_min: alloc.term_min,
term_max: alloc.term_max,
expiration: alloc.expiration,
}
}
}
};
}
from_allocation!(fil_actor_verifreg_state::v14::Allocation);
from_allocation!(fil_actor_verifreg_state::v13::Allocation);
from_allocation!(fil_actor_verifreg_state::v12::Allocation);
from_allocation!(fil_actor_verifreg_state::v11::Allocation);
from_allocation!(fil_actor_verifreg_state::v10::Allocation);
from_allocation!(fil_actor_verifreg_state::v9::Allocation);