1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright 2019-2023 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

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};

/// verifreg actor address.
pub const ADDRESS: Address = Address::new_id(6);

/// Verifreg actor state.
#[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 {
    // The provider storing the data (from allocation).
    pub provider: ActorID,
    // The client which allocated the DataCap (from allocation).
    pub client: ActorID,
    // Identifier of the data committed (from allocation).
    pub data: Cid,
    // The (padded) size of data (from allocation).
    pub size: PaddedPieceSize,
    // The min period after term_start which the provider must commit to storing data
    pub term_min: ChainEpoch,
    // The max period after term_start for which provider can earn QA-power for the data
    pub term_max: ChainEpoch,
    // The epoch at which the (first range of the) piece was committed.
    pub term_start: ChainEpoch,
    // ID of the provider's sector in which the data is committed.
    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 {
    // The verified client which allocated the DataCap.
    pub client: ActorID,
    // The provider (miner actor) which may claim the allocation.
    pub provider: ActorID,
    // Identifier of the data to be committed.
    pub data: Cid,
    // The (padded) size of data.
    pub size: PaddedPieceSize,
    // The minimum duration which the provider must commit to storing the piece to avoid
    // early-termination penalties (epochs).
    pub term_min: ChainEpoch,
    // The maximum period for which a provider can earn quality-adjusted power
    // for the piece (epochs).
    pub term_max: ChainEpoch,
    // The latest epoch by which a provider must commit data before the allocation expires.
    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);