Skip to content

Commit

Permalink
Remove uses of Map from most actors (#1363)
Browse files Browse the repository at this point in the history
* Remove uses of Map from most actors

* Use BigIntDe for datacap
  • Loading branch information
anorth authored Aug 11, 2023
1 parent f46d4dc commit 7e11334
Show file tree
Hide file tree
Showing 23 changed files with 442 additions and 502 deletions.
10 changes: 5 additions & 5 deletions actors/market/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub struct State {
}

pub type PendingDealAllocationsMap<BS> = Map2<BS, DealID, AllocationID>;
pub const PENDING_ALLOCATIONS_CONF: Config =
pub const PENDING_ALLOCATIONS_CONFIG: Config =
Config { bit_width: HAMT_BIT_WIDTH, ..DEFAULT_HAMT_CONFIG };

impl State {
Expand Down Expand Up @@ -104,7 +104,7 @@ impl State {

let empty_pending_deal_allocation_map = Map2::<&BS, DealID, AllocationID>::empty(
store,
PENDING_ALLOCATIONS_CONF,
PENDING_ALLOCATIONS_CONFIG,
"pending deal allocations",
)
.flush()?;
Expand Down Expand Up @@ -301,7 +301,7 @@ impl State {
let mut pending_deal_allocation_ids = PendingDealAllocationsMap::load(
store,
&self.pending_deal_allocation_ids,
PENDING_ALLOCATIONS_CONF,
PENDING_ALLOCATIONS_CONFIG,
"pending deal allocations",
)?;

Expand All @@ -327,7 +327,7 @@ impl State {
let pending_deal_allocation_ids = PendingDealAllocationsMap::load(
store,
&self.pending_deal_allocation_ids,
PENDING_ALLOCATIONS_CONF,
PENDING_ALLOCATIONS_CONFIG,
"pending deal allocations",
)?;

Expand All @@ -354,7 +354,7 @@ impl State {
let mut pending_deal_allocation_ids = PendingDealAllocationsMap::load(
store,
&self.pending_deal_allocation_ids,
PENDING_ALLOCATIONS_CONF,
PENDING_ALLOCATIONS_CONFIG,
"pending deal allocations",
)?;

Expand Down
4 changes: 2 additions & 2 deletions actors/market/tests/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use cid::Cid;
use fil_actor_market::{
BatchActivateDealsParams, BatchActivateDealsResult, PendingDealAllocationsMap,
PENDING_ALLOCATIONS_CONF,
PENDING_ALLOCATIONS_CONFIG,
};
use frc46_token::token::types::{TransferFromParams, TransferFromReturn};
use num_traits::{FromPrimitive, Zero};
Expand Down Expand Up @@ -403,7 +403,7 @@ pub fn get_pending_deal_allocation(rt: &MockRuntime, deal_id: DealID) -> Allocat
let pending_allocations = PendingDealAllocationsMap::load(
&rt.store,
&st.pending_deal_allocation_ids,
PENDING_ALLOCATIONS_CONF,
PENDING_ALLOCATIONS_CONFIG,
"pending deal allocations",
)
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions actors/market/tests/market_actor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use fil_actor_market::{
DealArray, DealMetaArray, Label, MarketNotifyDealParams, Method, PendingDealAllocationsMap,
PublishStorageDealsParams, PublishStorageDealsReturn, SectorDeals, State,
WithdrawBalanceParams, EX_DEAL_EXPIRED, MARKET_NOTIFY_DEAL_METHOD, NO_ALLOCATION_ID,
PENDING_ALLOCATIONS_CONF, PROPOSALS_AMT_BITWIDTH, STATES_AMT_BITWIDTH,
PENDING_ALLOCATIONS_CONFIG, PROPOSALS_AMT_BITWIDTH, STATES_AMT_BITWIDTH,
};
use fil_actors_runtime::cbor::{deserialize, serialize};
use fil_actors_runtime::network::EPOCHS_IN_DAY;
Expand Down Expand Up @@ -759,7 +759,7 @@ fn deal_expires() {
let pending_allocs = PendingDealAllocationsMap::load(
&rt.store,
&st.pending_deal_allocation_ids,
PENDING_ALLOCATIONS_CONF,
PENDING_ALLOCATIONS_CONFIG,
"pending allocations",
)
.unwrap();
Expand Down
114 changes: 47 additions & 67 deletions actors/multisig/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
// Copyright 2019-2022 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use fvm_actor_utils::receiver::UniversalReceiverParams;
use std::collections::BTreeSet;

use fil_actors_runtime::FIRST_EXPORTED_METHOD_NUMBER;
use fvm_actor_utils::receiver::UniversalReceiverParams;
use fvm_ipld_blockstore::Blockstore;
use fvm_ipld_encoding::ipld_block::IpldBlock;
use fvm_ipld_encoding::RawBytes;
use fvm_shared::address::Address;
use fvm_shared::econ::TokenAmount;
use fvm_shared::error::ExitCode;
use fvm_shared::MethodNum;
use fvm_shared::{HAMT_BIT_WIDTH, METHOD_CONSTRUCTOR};
use fvm_shared::METHOD_CONSTRUCTOR;
use num_derive::FromPrimitive;
use num_traits::Zero;

use fil_actors_runtime::cbor::serialize_vec;
use fil_actors_runtime::runtime::{ActorCode, Primitives, Runtime};
use fil_actors_runtime::FIRST_EXPORTED_METHOD_NUMBER;
use fil_actors_runtime::{
actor_dispatch, actor_error, extract_send_result, make_empty_map, make_map_with_root,
resolve_to_actor_id, ActorContext, ActorError, AsActorError, Map, INIT_ACTOR_ADDR,
actor_dispatch, actor_error, extract_send_result, resolve_to_actor_id, ActorContext,
ActorError, AsActorError, INIT_ACTOR_ADDR,
};

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

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

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

let mut ptx = make_map_with_root(&st.pending_txs, rt.store())
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load pending transactions")?;

let mut ptx = PendingTxnMap::load(
rt.store(),
&st.pending_txs,
PENDING_TXN_CONFIG,
"pending txns",
)?;
let t_id = st.next_tx_id;
st.next_tx_id.0 += 1;

Expand All @@ -155,21 +156,12 @@ impl Actor {
approved: Vec::new(),
};

ptx.set(t_id.key(), txn.clone()).context_code(
ExitCode::USR_ILLEGAL_STATE,
"failed to put transaction for propose",
)?;

st.pending_txs = ptx.flush().context_code(
ExitCode::USR_ILLEGAL_STATE,
"failed to flush pending transactions",
)?;

ptx.set(&t_id, txn.clone())?;
st.pending_txs = ptx.flush()?;
Ok((t_id, txn))
})?;

let (applied, ret, code) = Self::approve_transaction(rt, txn_id, txn)?;

Ok(ProposeReturn { txn_id, applied, code, ret })
}

Expand All @@ -183,9 +175,12 @@ impl Actor {
if !st.is_signer(&approver) {
return Err(actor_error!(forbidden; "{} is not a signer", approver));
}

let ptx = make_map_with_root(&st.pending_txs, rt.store())
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load pending transactions")?;
let ptx = PendingTxnMap::load(
rt.store(),
&st.pending_txs,
PENDING_TXN_CONFIG,
"pending txns",
)?;

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

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

let mut ptx = make_map_with_root::<_, Transaction>(&st.pending_txs, rt.store())
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load pending transactions")?;
let mut ptx = PendingTxnMap::load(
rt.store(),
&st.pending_txs,
PENDING_TXN_CONFIG,
"pending txns",
)?;

let (_, tx) = ptx
.delete(&params.id.key())
.with_context_code(ExitCode::USR_ILLEGAL_STATE, || {
format!("failed to pop transaction {:?} for cancel", params.id)
})?
.ok_or_else(|| {
actor_error!(not_found, "no such transaction {:?} to cancel", params.id)
})?;
let tx = ptx.delete(&params.id)?.ok_or_else(|| {
actor_error!(not_found, "no such transaction {:?} to cancel", params.id)
})?;

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

st.pending_txs = ptx.flush().context_code(
ExitCode::USR_ILLEGAL_STATE,
"failed to flush pending transactions",
)?;

st.pending_txs = ptx.flush()?;
Ok(())
})
}
Expand Down Expand Up @@ -416,21 +406,18 @@ impl Actor {
}

let st = rt.transaction(|st: &mut State, rt| {
let mut ptx = make_map_with_root(&st.pending_txs, rt.store())
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load pending transactions")?;
let mut ptx = PendingTxnMap::load(
rt.store(),
&st.pending_txs,
PENDING_TXN_CONFIG,
"pending txns",
)?;

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

ptx.set(tx_id.key(), txn.clone())
.with_context_code(ExitCode::USR_ILLEGAL_STATE, || {
format!("failed to put transaction {} for approval", tx_id.0)
})?;

st.pending_txs = ptx.flush().context_code(
ExitCode::USR_ILLEGAL_STATE,
"failed to flush pending transactions",
)?;
ptx.set(&tx_id, txn.clone())?;
st.pending_txs = ptx.flush()?;

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

rt.transaction(|st: &mut State, rt| {
let mut ptx = make_map_with_root::<_, Transaction>(&st.pending_txs, rt.store())
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load pending transactions")?;

ptx.delete(&txn_id.key()).context_code(
ExitCode::USR_ILLEGAL_STATE,
"failed to delete transaction for cleanup",
)?;

st.pending_txs = ptx.flush().context_code(
ExitCode::USR_ILLEGAL_STATE,
"failed to flush pending transactions",
let mut ptx = PendingTxnMap::load(
rt.store(),
&st.pending_txs,
PENDING_TXN_CONFIG,
"pending txns",
)?;
ptx.delete(&txn_id)?;
st.pending_txs = ptx.flush()?;
Ok(())
})?;
}
Expand All @@ -514,7 +497,7 @@ fn execute_transaction_if_approved(

fn get_transaction<'m, BS, RT>(
rt: &RT,
ptx: &'m Map<'_, BS, Transaction>,
ptx: &'m PendingTxnMap<BS>,
txn_id: TxnID,
proposal_hash: Vec<u8>,
) -> Result<&'m Transaction, ActorError>
Expand All @@ -523,10 +506,7 @@ where
RT: Runtime,
{
let txn = ptx
.get(&txn_id.key())
.with_context_code(ExitCode::USR_ILLEGAL_STATE, || {
format!("failed to load transaction {:?} for approval", txn_id)
})?
.get(&txn_id)?
.ok_or_else(|| actor_error!(not_found, "no such transaction {:?} for approval", txn_id))?;

if !proposal_hash.is_empty() {
Expand Down
27 changes: 12 additions & 15 deletions actors/multisig/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@
// SPDX-License-Identifier: Apache-2.0, MIT

use cid::Cid;
use fil_actors_runtime::{actor_error, ActorError, AsActorError};
use fvm_ipld_blockstore::Blockstore;
use fvm_ipld_encoding::tuple::*;
use fvm_shared::address::Address;
use fvm_shared::bigint::BigInt;
use fvm_shared::bigint::Integer;
use fvm_shared::clock::ChainEpoch;
use fvm_shared::econ::TokenAmount;
use fvm_shared::error::ExitCode;
use indexmap::IndexMap;
use num_traits::Zero;

use fil_actors_runtime::{actor_error, ActorError, Config, Map2, DEFAULT_HAMT_CONFIG};

use super::types::Transaction;
use super::TxnID;
use crate::make_map_with_root;

pub type PendingTxnMap<BS> = Map2<BS, TxnID, Transaction>;
pub const PENDING_TXN_CONFIG: Config = DEFAULT_HAMT_CONFIG;

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

// Identify transactions that need updating
let mut txn_ids_to_purge = IndexMap::new();
txns.for_each(|tx_id, txn: &Transaction| {
for approver in txn.approved.iter() {
if approver == addr {
txn_ids_to_purge.insert(tx_id.0.clone(), txn.clone());
txn_ids_to_purge.insert(tx_id, txn.clone());
}
}
Ok(())
})
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to scan txns")?;
})?;

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

if !txn.approved.is_empty() {
txns.set(tx_id.into(), txn)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to update entry")?;
txns.set(&tx_id, txn)?;
} else {
txns.delete(&tx_id)
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to delete entry")?;
txns.delete(&tx_id)?;
}
}

self.pending_txs =
txns.flush().context_code(ExitCode::USR_ILLEGAL_STATE, "failed to store entries")?;

self.pending_txs = txns.flush()?;
Ok(())
}

Expand Down
Loading

0 comments on commit 7e11334

Please sign in to comment.