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
// Copyright 2019-2022 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use cid::Cid;
use fil_actors_shared::actor_error_v12;
use fil_actors_shared::v12::{ActorError, AsActorError, Config, Map2, MapMap, DEFAULT_HAMT_CONFIG};
use fvm_ipld_blockstore::Blockstore;
use fvm_ipld_encoding::tuple::*;
use fvm_shared4::address::Address;
use fvm_shared4::bigint::bigint_ser::BigIntDe;
use fvm_shared4::clock::ChainEpoch;
use fvm_shared4::error::ExitCode;
use fvm_shared4::piece::PaddedPieceSize;
use fvm_shared4::sector::SectorNumber;
use fvm_shared4::{ActorID, HAMT_BIT_WIDTH};

use crate::v12::{AddrPairKey, AllocationID, ClaimID};
use crate::v12::{DataCap, RemoveDataCapProposalID};

pub type DataCapMap<BS> = Map2<BS, Address, BigIntDe>;
pub const DATACAP_MAP_CONFIG: Config = DEFAULT_HAMT_CONFIG;

pub type RemoveDataCapProposalMap<BS> = Map2<BS, AddrPairKey, RemoveDataCapProposalID>;
pub const REMOVE_DATACAP_PROPOSALS_CONFIG: Config = DEFAULT_HAMT_CONFIG;

#[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone)]
pub struct State {
    pub root_key: Address,
    // Maps verifier addresses to data cap minting allowance (in bytes).
    pub verifiers: Cid, // HAMT[Address]DataCap
    pub remove_data_cap_proposal_ids: Cid,
    // Maps client IDs to allocations made by that client.
    pub allocations: Cid, // HAMT[ActorID]HAMT[AllocationID]Allocation
    // Next allocation identifier to use.
    // The value 0 is reserved to mean "no allocation".
    pub next_allocation_id: u64,
    // Maps provider IDs to allocations claimed by that provider.
    pub claims: Cid, // HAMT[ActorID]HAMT[ClaimID]Claim
}

impl State {
    pub fn new<BS: Blockstore>(store: &BS, root_key: Address) -> Result<State, ActorError> {
        let empty_dcap = DataCapMap::empty(store, DATACAP_MAP_CONFIG, "empty").flush()?;
        let empty_allocs_claims =
            MapMap::<_, (), ActorID, u64>::new(store, HAMT_BIT_WIDTH, HAMT_BIT_WIDTH)
                .flush()
                .map_err(|e| {
                    actor_error_v12!(illegal_state, "failed to create empty multi map: {}", e)
                })?;

        Ok(State {
            root_key,
            verifiers: empty_dcap,
            remove_data_cap_proposal_ids: empty_dcap,
            allocations: empty_allocs_claims,
            next_allocation_id: 1,
            claims: empty_allocs_claims,
        })
    }

    // Adds a verifier and cap, overwriting any existing cap for that verifier.
    pub fn put_verifier(
        &mut self,
        store: &impl Blockstore,
        verifier: &Address,
        cap: &DataCap,
    ) -> Result<(), ActorError> {
        let mut verifiers = self.load_verifiers(store)?;
        verifiers.set(verifier, BigIntDe(cap.clone()))?;
        self.verifiers = verifiers.flush()?;
        Ok(())
    }

    pub fn remove_verifier(
        &mut self,
        store: &impl Blockstore,
        verifier: &Address,
    ) -> Result<(), ActorError> {
        let mut verifiers = self.load_verifiers(store)?;
        verifiers
            .delete(verifier)?
            .context_code(ExitCode::USR_ILLEGAL_ARGUMENT, "verifier not found")?;
        self.verifiers = verifiers.flush()?;
        Ok(())
    }

    pub fn get_verifier_cap(
        &self,
        store: &impl Blockstore,
        verifier: &Address,
    ) -> Result<Option<DataCap>, ActorError> {
        let verifiers = self.load_verifiers(store)?;
        let allowance = verifiers.get(verifier)?;
        Ok(allowance.map(|a| a.clone().0))
    }

    pub fn load_verifiers<BS: Blockstore>(&self, store: BS) -> Result<DataCapMap<BS>, ActorError> {
        DataCapMap::load(store, &self.verifiers, DATACAP_MAP_CONFIG, "verifiers")
    }

    pub fn load_allocs<'a, BS: Blockstore>(
        &self,
        store: &'a BS,
    ) -> Result<MapMap<'a, BS, Allocation, ActorID, AllocationID>, ActorError> {
        MapMap::<BS, Allocation, ActorID, AllocationID>::from_root(
            store,
            &self.allocations,
            HAMT_BIT_WIDTH,
            HAMT_BIT_WIDTH,
        )
        .context_code(
            ExitCode::USR_ILLEGAL_STATE,
            "failed to load allocations table",
        )
    }

    pub fn save_allocs<BS: Blockstore>(
        &mut self,
        allocs: &mut MapMap<'_, BS, Allocation, ActorID, AllocationID>,
    ) -> Result<(), ActorError> {
        self.allocations = allocs.flush().context_code(
            ExitCode::USR_ILLEGAL_STATE,
            "failed to flush allocations table",
        )?;
        Ok(())
    }

    /// Inserts a batch of allocations under a single client address.
    /// The allocations are assigned sequential IDs starting from the next available.
    pub fn insert_allocations<BS: Blockstore>(
        &mut self,
        store: &BS,
        client: ActorID,
        new_allocs: Vec<Allocation>,
    ) -> Result<Vec<AllocationID>, ActorError> {
        if new_allocs.is_empty() {
            return Ok(vec![]);
        }
        let mut allocs = self.load_allocs(store)?;
        // These local variables allow the id-associating map closure to move the allocations
        // from the iterator rather than clone, without moving self.
        let first_id = self.next_allocation_id;
        let mut count = 0;
        let count_ref = &mut count;
        allocs
            .put_many(
                client,
                new_allocs.into_iter().map(move |a| {
                    let id = first_id + *count_ref;
                    *count_ref += 1;
                    (id, a)
                }),
            )
            .context_code(ExitCode::USR_ILLEGAL_STATE, "failed to put allocations")?;
        self.save_allocs(&mut allocs)?;
        self.next_allocation_id += count;
        let allocated_ids = (first_id..first_id + count).collect();
        Ok(allocated_ids)
    }

    pub fn load_claims<'a, BS: Blockstore>(
        &self,
        store: &'a BS,
    ) -> Result<MapMap<'a, BS, Claim, ActorID, ClaimID>, ActorError> {
        MapMap::<BS, Claim, ActorID, ClaimID>::from_root(
            store,
            &self.claims,
            HAMT_BIT_WIDTH,
            HAMT_BIT_WIDTH,
        )
        .context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load claims table")
    }

    pub fn save_claims<BS: Blockstore>(
        &mut self,
        claims: &mut MapMap<'_, BS, Claim, ActorID, ClaimID>,
    ) -> Result<(), ActorError> {
        self.claims = claims
            .flush()
            .context_code(ExitCode::USR_ILLEGAL_STATE, "failed to flush claims table")?;
        Ok(())
    }

    pub fn put_claims<BS: Blockstore>(
        &mut self,
        store: &BS,
        claims: Vec<(ClaimID, Claim)>,
    ) -> Result<(), ActorError> {
        if claims.is_empty() {
            return Ok(());
        }
        let mut st_claims = self.load_claims(store)?;
        for (id, claim) in claims.into_iter() {
            st_claims
                .put(claim.provider, id, claim)
                .context_code(ExitCode::USR_ILLEGAL_STATE, "failed to put claim")?;
        }
        self.save_claims(&mut st_claims)?;
        Ok(())
    }
}
#[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,
}

#[derive(Serialize_tuple, Deserialize_tuple, 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,
}

pub fn get_allocation<'a, BS>(
    allocations: &'a mut MapMap<BS, Allocation, ActorID, AllocationID>,
    client: ActorID,
    id: AllocationID,
) -> Result<Option<&'a Allocation>, ActorError>
where
    BS: Blockstore,
{
    allocations.get(client, id).context_code(
        ExitCode::USR_ILLEGAL_STATE,
        "HAMT lookup failure getting allocation",
    )
}

pub fn get_claim<'a, BS>(
    claims: &'a mut MapMap<BS, Claim, ActorID, ClaimID>,
    provider: ActorID,
    id: ClaimID,
) -> Result<Option<&'a Claim>, ActorError>
where
    BS: Blockstore,
{
    claims.get(provider, id).context_code(
        ExitCode::USR_ILLEGAL_STATE,
        "HAMT lookup failure getting claim",
    )
}