Skip to content

Commit 7e11334

Browse files
authored
Remove uses of Map from most actors (#1363)
* Remove uses of Map from most actors * Use BigIntDe for datacap
1 parent f46d4dc commit 7e11334

File tree

23 files changed

+442
-502
lines changed

23 files changed

+442
-502
lines changed

actors/market/src/state.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub struct State {
7474
}
7575

7676
pub type PendingDealAllocationsMap<BS> = Map2<BS, DealID, AllocationID>;
77-
pub const PENDING_ALLOCATIONS_CONF: Config =
77+
pub const PENDING_ALLOCATIONS_CONFIG: Config =
7878
Config { bit_width: HAMT_BIT_WIDTH, ..DEFAULT_HAMT_CONFIG };
7979

8080
impl State {
@@ -104,7 +104,7 @@ impl State {
104104

105105
let empty_pending_deal_allocation_map = Map2::<&BS, DealID, AllocationID>::empty(
106106
store,
107-
PENDING_ALLOCATIONS_CONF,
107+
PENDING_ALLOCATIONS_CONFIG,
108108
"pending deal allocations",
109109
)
110110
.flush()?;
@@ -301,7 +301,7 @@ impl State {
301301
let mut pending_deal_allocation_ids = PendingDealAllocationsMap::load(
302302
store,
303303
&self.pending_deal_allocation_ids,
304-
PENDING_ALLOCATIONS_CONF,
304+
PENDING_ALLOCATIONS_CONFIG,
305305
"pending deal allocations",
306306
)?;
307307

@@ -327,7 +327,7 @@ impl State {
327327
let pending_deal_allocation_ids = PendingDealAllocationsMap::load(
328328
store,
329329
&self.pending_deal_allocation_ids,
330-
PENDING_ALLOCATIONS_CONF,
330+
PENDING_ALLOCATIONS_CONFIG,
331331
"pending deal allocations",
332332
)?;
333333

@@ -354,7 +354,7 @@ impl State {
354354
let mut pending_deal_allocation_ids = PendingDealAllocationsMap::load(
355355
store,
356356
&self.pending_deal_allocation_ids,
357-
PENDING_ALLOCATIONS_CONF,
357+
PENDING_ALLOCATIONS_CONFIG,
358358
"pending deal allocations",
359359
)?;
360360

actors/market/tests/harness.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use cid::Cid;
44
use fil_actor_market::{
55
BatchActivateDealsParams, BatchActivateDealsResult, PendingDealAllocationsMap,
6-
PENDING_ALLOCATIONS_CONF,
6+
PENDING_ALLOCATIONS_CONFIG,
77
};
88
use frc46_token::token::types::{TransferFromParams, TransferFromReturn};
99
use num_traits::{FromPrimitive, Zero};
@@ -403,7 +403,7 @@ pub fn get_pending_deal_allocation(rt: &MockRuntime, deal_id: DealID) -> Allocat
403403
let pending_allocations = PendingDealAllocationsMap::load(
404404
&rt.store,
405405
&st.pending_deal_allocation_ids,
406-
PENDING_ALLOCATIONS_CONF,
406+
PENDING_ALLOCATIONS_CONFIG,
407407
"pending deal allocations",
408408
)
409409
.unwrap();

actors/market/tests/market_actor_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use fil_actor_market::{
88
DealArray, DealMetaArray, Label, MarketNotifyDealParams, Method, PendingDealAllocationsMap,
99
PublishStorageDealsParams, PublishStorageDealsReturn, SectorDeals, State,
1010
WithdrawBalanceParams, EX_DEAL_EXPIRED, MARKET_NOTIFY_DEAL_METHOD, NO_ALLOCATION_ID,
11-
PENDING_ALLOCATIONS_CONF, PROPOSALS_AMT_BITWIDTH, STATES_AMT_BITWIDTH,
11+
PENDING_ALLOCATIONS_CONFIG, PROPOSALS_AMT_BITWIDTH, STATES_AMT_BITWIDTH,
1212
};
1313
use fil_actors_runtime::cbor::{deserialize, serialize};
1414
use fil_actors_runtime::network::EPOCHS_IN_DAY;
@@ -759,7 +759,7 @@ fn deal_expires() {
759759
let pending_allocs = PendingDealAllocationsMap::load(
760760
&rt.store,
761761
&st.pending_deal_allocation_ids,
762-
PENDING_ALLOCATIONS_CONF,
762+
PENDING_ALLOCATIONS_CONFIG,
763763
"pending allocations",
764764
)
765765
.unwrap();

actors/multisig/src/lib.rs

Lines changed: 47 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
// Copyright 2019-2022 ChainSafe Systems
22
// SPDX-License-Identifier: Apache-2.0, MIT
33

4-
use fvm_actor_utils::receiver::UniversalReceiverParams;
54
use std::collections::BTreeSet;
65

7-
use fil_actors_runtime::FIRST_EXPORTED_METHOD_NUMBER;
6+
use fvm_actor_utils::receiver::UniversalReceiverParams;
87
use fvm_ipld_blockstore::Blockstore;
98
use fvm_ipld_encoding::ipld_block::IpldBlock;
109
use fvm_ipld_encoding::RawBytes;
1110
use fvm_shared::address::Address;
1211
use fvm_shared::econ::TokenAmount;
1312
use fvm_shared::error::ExitCode;
1413
use fvm_shared::MethodNum;
15-
use fvm_shared::{HAMT_BIT_WIDTH, METHOD_CONSTRUCTOR};
14+
use fvm_shared::METHOD_CONSTRUCTOR;
1615
use num_derive::FromPrimitive;
1716
use num_traits::Zero;
1817

1918
use fil_actors_runtime::cbor::serialize_vec;
2019
use fil_actors_runtime::runtime::{ActorCode, Primitives, Runtime};
20+
use fil_actors_runtime::FIRST_EXPORTED_METHOD_NUMBER;
2121
use fil_actors_runtime::{
22-
actor_dispatch, actor_error, extract_send_result, make_empty_map, make_map_with_root,
23-
resolve_to_actor_id, ActorContext, ActorError, AsActorError, Map, INIT_ACTOR_ADDR,
22+
actor_dispatch, actor_error, extract_send_result, resolve_to_actor_id, ActorContext,
23+
ActorError, AsActorError, INIT_ACTOR_ADDR,
2424
};
2525

2626
pub use self::state::*;
@@ -97,9 +97,7 @@ impl Actor {
9797
return Err(actor_error!(illegal_argument; "negative unlock duration disallowed"));
9898
}
9999

100-
let empty_root = make_empty_map::<_, ()>(rt.store(), HAMT_BIT_WIDTH)
101-
.flush()
102-
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to create empty map")?;
100+
let empty_root = PendingTxnMap::empty(rt.store(), PENDING_TXN_CONFIG, "empty").flush()?;
103101

104102
let mut st: State = State {
105103
signers: resolved_signers,
@@ -141,9 +139,12 @@ impl Actor {
141139
return Err(actor_error!(forbidden, "{} is not a signer", proposer));
142140
}
143141

144-
let mut ptx = make_map_with_root(&st.pending_txs, rt.store())
145-
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load pending transactions")?;
146-
142+
let mut ptx = PendingTxnMap::load(
143+
rt.store(),
144+
&st.pending_txs,
145+
PENDING_TXN_CONFIG,
146+
"pending txns",
147+
)?;
147148
let t_id = st.next_tx_id;
148149
st.next_tx_id.0 += 1;
149150

@@ -155,21 +156,12 @@ impl Actor {
155156
approved: Vec::new(),
156157
};
157158

158-
ptx.set(t_id.key(), txn.clone()).context_code(
159-
ExitCode::USR_ILLEGAL_STATE,
160-
"failed to put transaction for propose",
161-
)?;
162-
163-
st.pending_txs = ptx.flush().context_code(
164-
ExitCode::USR_ILLEGAL_STATE,
165-
"failed to flush pending transactions",
166-
)?;
167-
159+
ptx.set(&t_id, txn.clone())?;
160+
st.pending_txs = ptx.flush()?;
168161
Ok((t_id, txn))
169162
})?;
170163

171164
let (applied, ret, code) = Self::approve_transaction(rt, txn_id, txn)?;
172-
173165
Ok(ProposeReturn { txn_id, applied, code, ret })
174166
}
175167

@@ -183,9 +175,12 @@ impl Actor {
183175
if !st.is_signer(&approver) {
184176
return Err(actor_error!(forbidden; "{} is not a signer", approver));
185177
}
186-
187-
let ptx = make_map_with_root(&st.pending_txs, rt.store())
188-
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load pending transactions")?;
178+
let ptx = PendingTxnMap::load(
179+
rt.store(),
180+
&st.pending_txs,
181+
PENDING_TXN_CONFIG,
182+
"pending txns",
183+
)?;
189184

190185
let txn = get_transaction(rt, &ptx, params.id, params.proposal_hash)?;
191186

@@ -215,17 +210,16 @@ impl Actor {
215210
return Err(actor_error!(forbidden; "{} is not a signer", caller_addr));
216211
}
217212

218-
let mut ptx = make_map_with_root::<_, Transaction>(&st.pending_txs, rt.store())
219-
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load pending transactions")?;
213+
let mut ptx = PendingTxnMap::load(
214+
rt.store(),
215+
&st.pending_txs,
216+
PENDING_TXN_CONFIG,
217+
"pending txns",
218+
)?;
220219

221-
let (_, tx) = ptx
222-
.delete(&params.id.key())
223-
.with_context_code(ExitCode::USR_ILLEGAL_STATE, || {
224-
format!("failed to pop transaction {:?} for cancel", params.id)
225-
})?
226-
.ok_or_else(|| {
227-
actor_error!(not_found, "no such transaction {:?} to cancel", params.id)
228-
})?;
220+
let tx = ptx.delete(&params.id)?.ok_or_else(|| {
221+
actor_error!(not_found, "no such transaction {:?} to cancel", params.id)
222+
})?;
229223

230224
// Check to make sure transaction proposer is caller address
231225
if tx.approved.get(0) != Some(&caller_addr) {
@@ -241,11 +235,7 @@ impl Actor {
241235
return Err(actor_error!(illegal_state, "hash does not match proposal params"));
242236
}
243237

244-
st.pending_txs = ptx.flush().context_code(
245-
ExitCode::USR_ILLEGAL_STATE,
246-
"failed to flush pending transactions",
247-
)?;
248-
238+
st.pending_txs = ptx.flush()?;
249239
Ok(())
250240
})
251241
}
@@ -416,21 +406,18 @@ impl Actor {
416406
}
417407

418408
let st = rt.transaction(|st: &mut State, rt| {
419-
let mut ptx = make_map_with_root(&st.pending_txs, rt.store())
420-
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load pending transactions")?;
409+
let mut ptx = PendingTxnMap::load(
410+
rt.store(),
411+
&st.pending_txs,
412+
PENDING_TXN_CONFIG,
413+
"pending txns",
414+
)?;
421415

422416
// update approved on the transaction
423417
txn.approved.push(rt.message().caller());
424418

425-
ptx.set(tx_id.key(), txn.clone())
426-
.with_context_code(ExitCode::USR_ILLEGAL_STATE, || {
427-
format!("failed to put transaction {} for approval", tx_id.0)
428-
})?;
429-
430-
st.pending_txs = ptx.flush().context_code(
431-
ExitCode::USR_ILLEGAL_STATE,
432-
"failed to flush pending transactions",
433-
)?;
419+
ptx.set(&tx_id, txn.clone())?;
420+
st.pending_txs = ptx.flush()?;
434421

435422
// Go implementation holds reference to state after transaction so this must be cloned
436423
// to match to handle possible exit code inconsistency
@@ -493,18 +480,14 @@ fn execute_transaction_if_approved(
493480
applied = true;
494481

495482
rt.transaction(|st: &mut State, rt| {
496-
let mut ptx = make_map_with_root::<_, Transaction>(&st.pending_txs, rt.store())
497-
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load pending transactions")?;
498-
499-
ptx.delete(&txn_id.key()).context_code(
500-
ExitCode::USR_ILLEGAL_STATE,
501-
"failed to delete transaction for cleanup",
502-
)?;
503-
504-
st.pending_txs = ptx.flush().context_code(
505-
ExitCode::USR_ILLEGAL_STATE,
506-
"failed to flush pending transactions",
483+
let mut ptx = PendingTxnMap::load(
484+
rt.store(),
485+
&st.pending_txs,
486+
PENDING_TXN_CONFIG,
487+
"pending txns",
507488
)?;
489+
ptx.delete(&txn_id)?;
490+
st.pending_txs = ptx.flush()?;
508491
Ok(())
509492
})?;
510493
}
@@ -514,7 +497,7 @@ fn execute_transaction_if_approved(
514497

515498
fn get_transaction<'m, BS, RT>(
516499
rt: &RT,
517-
ptx: &'m Map<'_, BS, Transaction>,
500+
ptx: &'m PendingTxnMap<BS>,
518501
txn_id: TxnID,
519502
proposal_hash: Vec<u8>,
520503
) -> Result<&'m Transaction, ActorError>
@@ -523,10 +506,7 @@ where
523506
RT: Runtime,
524507
{
525508
let txn = ptx
526-
.get(&txn_id.key())
527-
.with_context_code(ExitCode::USR_ILLEGAL_STATE, || {
528-
format!("failed to load transaction {:?} for approval", txn_id)
529-
})?
509+
.get(&txn_id)?
530510
.ok_or_else(|| actor_error!(not_found, "no such transaction {:?} for approval", txn_id))?;
531511

532512
if !proposal_hash.is_empty() {

actors/multisig/src/state.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22
// SPDX-License-Identifier: Apache-2.0, MIT
33

44
use cid::Cid;
5-
use fil_actors_runtime::{actor_error, ActorError, AsActorError};
65
use fvm_ipld_blockstore::Blockstore;
76
use fvm_ipld_encoding::tuple::*;
87
use fvm_shared::address::Address;
98
use fvm_shared::bigint::BigInt;
109
use fvm_shared::bigint::Integer;
1110
use fvm_shared::clock::ChainEpoch;
1211
use fvm_shared::econ::TokenAmount;
13-
use fvm_shared::error::ExitCode;
1412
use indexmap::IndexMap;
1513
use num_traits::Zero;
1614

15+
use fil_actors_runtime::{actor_error, ActorError, Config, Map2, DEFAULT_HAMT_CONFIG};
16+
1717
use super::types::Transaction;
1818
use super::TxnID;
19-
use crate::make_map_with_root;
19+
20+
pub type PendingTxnMap<BS> = Map2<BS, TxnID, Transaction>;
21+
pub const PENDING_TXN_CONFIG: Config = DEFAULT_HAMT_CONFIG;
2022

2123
/// Multisig actor state
2224
#[derive(Serialize_tuple, Deserialize_tuple, Clone, Debug)]
@@ -76,37 +78,32 @@ impl State {
7678
store: &BS,
7779
addr: &Address,
7880
) -> Result<(), ActorError> {
79-
let mut txns = make_map_with_root(&self.pending_txs, store)
80-
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load txn map")?;
81+
let mut txns =
82+
PendingTxnMap::load(store, &self.pending_txs, PENDING_TXN_CONFIG, "pending txns")?;
8183

8284
// Identify transactions that need updating
8385
let mut txn_ids_to_purge = IndexMap::new();
8486
txns.for_each(|tx_id, txn: &Transaction| {
8587
for approver in txn.approved.iter() {
8688
if approver == addr {
87-
txn_ids_to_purge.insert(tx_id.0.clone(), txn.clone());
89+
txn_ids_to_purge.insert(tx_id, txn.clone());
8890
}
8991
}
9092
Ok(())
91-
})
92-
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to scan txns")?;
93+
})?;
9394

9495
// Update or remove those transactions.
9596
for (tx_id, mut txn) in txn_ids_to_purge {
9697
txn.approved.retain(|approver| approver != addr);
9798

9899
if !txn.approved.is_empty() {
99-
txns.set(tx_id.into(), txn)
100-
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to update entry")?;
100+
txns.set(&tx_id, txn)?;
101101
} else {
102-
txns.delete(&tx_id)
103-
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to delete entry")?;
102+
txns.delete(&tx_id)?;
104103
}
105104
}
106105

107-
self.pending_txs =
108-
txns.flush().context_code(ExitCode::USR_ILLEGAL_STATE, "failed to store entries")?;
109-
106+
self.pending_txs = txns.flush()?;
110107
Ok(())
111108
}
112109

0 commit comments

Comments
 (0)