Skip to content

Commit b6fc3a2

Browse files
committed
Replace Map with Map2 in miner actor
1 parent 6e78144 commit b6fc3a2

File tree

6 files changed

+79
-101
lines changed

6 files changed

+79
-101
lines changed

actors/miner/src/deadline_state.rs

+27-8
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ use std::collections::BTreeSet;
77
use anyhow::anyhow;
88
use cid::multihash::Code;
99
use cid::Cid;
10-
use fil_actors_runtime::runtime::Policy;
11-
use fil_actors_runtime::{actor_error, ActorDowncast, ActorError, Array, AsActorError};
1210
use fvm_ipld_bitfield::BitField;
1311
use fvm_ipld_blockstore::Blockstore;
1412
use fvm_ipld_encoding::tuple::*;
@@ -19,11 +17,15 @@ use fvm_shared::error::ExitCode;
1917
use fvm_shared::sector::{PoStProof, SectorSize};
2018
use num_traits::{Signed, Zero};
2119

20+
use fil_actors_runtime::runtime::Policy;
21+
use fil_actors_runtime::{actor_error, ActorDowncast, ActorError, Array, AsActorError};
22+
23+
use crate::SECTORS_AMT_BITWIDTH;
24+
2225
use super::{
2326
BitFieldQueue, ExpirationSet, Partition, PartitionSectorMap, PoStPartition, PowerPair,
2427
QuantSpec, SectorOnChainInfo, Sectors, TerminationResult,
2528
};
26-
use crate::SECTORS_AMT_BITWIDTH;
2729

2830
// Bitwidth of AMTs determined empirically from mutation patterns and projections of mainnet data.
2931
// Usually a small array
@@ -184,24 +186,41 @@ pub struct DisputeInfo {
184186
}
185187

186188
impl Deadline {
187-
pub fn new<BS: Blockstore>(store: &BS) -> anyhow::Result<Self> {
189+
pub fn new<BS: Blockstore>(store: &BS) -> Result<Self, ActorError> {
188190
let empty_partitions_array =
189191
Array::<(), BS>::new_with_bit_width(store, DEADLINE_PARTITIONS_AMT_BITWIDTH)
190192
.flush()
191-
.map_err(|e| e.downcast_wrap("Failed to create empty states array"))?;
193+
.map_err(|e| {
194+
e.downcast_default(
195+
ExitCode::USR_ILLEGAL_STATE,
196+
"Failed to create empty states array",
197+
)
198+
})?;
192199
let empty_deadline_expiration_array =
193200
Array::<(), BS>::new_with_bit_width(store, DEADLINE_EXPIRATIONS_AMT_BITWIDTH)
194201
.flush()
195-
.map_err(|e| e.downcast_wrap("Failed to create empty states array"))?;
202+
.map_err(|e| {
203+
e.downcast_default(
204+
ExitCode::USR_ILLEGAL_STATE,
205+
"Failed to create empty states array",
206+
)
207+
})?;
196208
let empty_post_submissions_array = Array::<(), BS>::new_with_bit_width(
197209
store,
198210
DEADLINE_OPTIMISTIC_POST_SUBMISSIONS_AMT_BITWIDTH,
199211
)
200212
.flush()
201-
.map_err(|e| e.downcast_wrap("Failed to create empty states array"))?;
213+
.map_err(|e| {
214+
e.downcast_default(ExitCode::USR_ILLEGAL_STATE, "Failed to create empty states array")
215+
})?;
202216
let empty_sectors_array = Array::<(), BS>::new_with_bit_width(store, SECTORS_AMT_BITWIDTH)
203217
.flush()
204-
.map_err(|e| e.downcast_wrap("Failed to construct empty sectors snapshot array"))?;
218+
.map_err(|e| {
219+
e.downcast_default(
220+
ExitCode::USR_ILLEGAL_STATE,
221+
"Failed to construct empty sectors snapshot array",
222+
)
223+
})?;
205224
Ok(Self {
206225
partitions: empty_partitions_array,
207226
expirations_epochs: empty_deadline_expiration_array,

actors/miner/src/lib.rs

+5-19
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ use fil_actors_runtime::{
5454
BURNT_FUNDS_ACTOR_ADDR, INIT_ACTOR_ADDR, REWARD_ACTOR_ADDR, STORAGE_MARKET_ACTOR_ADDR,
5555
STORAGE_POWER_ACTOR_ADDR, SYSTEM_ACTOR_ADDR, VERIFIED_REGISTRY_ACTOR_ADDR,
5656
};
57-
58-
use crate::ext::market::NO_ALLOCATION_ID;
5957
pub use monies::*;
6058
pub use partition_state::*;
6159
pub use policy::*;
@@ -67,6 +65,7 @@ pub use termination::*;
6765
pub use types::*;
6866
pub use vesting_state::*;
6967

68+
use crate::ext::market::NO_ALLOCATION_ID;
7069
use crate::notifications::{notify_data_consumers, ActivationNotifications};
7170

7271
// The following errors are particular cases of illegal state.
@@ -239,12 +238,8 @@ impl Actor {
239238
e.downcast_default(ExitCode::USR_ILLEGAL_STATE, "failed to construct illegal state")
240239
})?;
241240

242-
let st =
243-
State::new(policy, rt.store(), info_cid, period_start, deadline_idx).map_err(|e| {
244-
e.downcast_default(ExitCode::USR_ILLEGAL_STATE, "failed to construct state")
245-
})?;
241+
let st = State::new(policy, rt.store(), info_cid, period_start, deadline_idx)?;
246242
rt.create(&st)?;
247-
248243
Ok(())
249244
}
250245

@@ -1555,7 +1550,7 @@ impl Actor {
15551550
None => return Err(actor_error!(
15561551
illegal_argument,
15571552
"unspecified CompactCommD not allowed past nv21, need explicit None value for CC or CommD")),
1558-
_ => {},
1553+
_ => {}
15591554
}
15601555

15611556
// Require sector lifetime meets minimum by assuming activation happens at last epoch permitted for seal proof.
@@ -1977,12 +1972,7 @@ impl Actor {
19771972
// Validate pre-commit.
19781973
let precommit = st
19791974
.get_precommitted_sector(store, params.sector_number)
1980-
.map_err(|e| {
1981-
e.downcast_default(
1982-
ExitCode::USR_ILLEGAL_STATE,
1983-
format!("failed to load pre-committed sector {}", params.sector_number),
1984-
)
1985-
})?
1975+
.with_context(|| format!("loading pre-commit {}", params.sector_number))?
19861976
.ok_or_else(|| {
19871977
actor_error!(not_found, "no pre-commited sector {}", params.sector_number)
19881978
})?;
@@ -5316,11 +5306,7 @@ fn activate_new_sector_infos(
53165306
state.put_sectors(store, new_sectors.clone()).map_err(|e| {
53175307
e.downcast_default(ExitCode::USR_ILLEGAL_STATE, "failed to put new sectors")
53185308
})?;
5319-
5320-
state.delete_precommitted_sectors(store, &new_sector_numbers).map_err(|e| {
5321-
e.downcast_default(ExitCode::USR_ILLEGAL_STATE, "failed to delete precommited sectors")
5322-
})?;
5323-
5309+
state.delete_precommitted_sectors(store, &new_sector_numbers)?;
53245310
state
53255311
.assign_sectors_to_deadlines(
53265312
policy,

actors/miner/src/state.rs

+34-46
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,12 @@ use std::ops::Neg;
88
use anyhow::{anyhow, Error};
99
use cid::multihash::Code;
1010
use cid::Cid;
11-
use fil_actors_runtime::runtime::policy_constants::MAX_SECTOR_NUMBER;
12-
use fil_actors_runtime::runtime::Policy;
13-
use fil_actors_runtime::{
14-
actor_error, make_empty_map, make_map_with_root_and_bitwidth, u64_key, ActorDowncast,
15-
ActorError, Array, AsActorError,
16-
};
1711
use fvm_ipld_amt::Error as AmtError;
1812
use fvm_ipld_bitfield::BitField;
1913
use fvm_ipld_blockstore::Blockstore;
2014
use fvm_ipld_encoding::tuple::*;
2115
use fvm_ipld_encoding::{strict_bytes, BytesDe, CborStore};
22-
use fvm_ipld_hamt::Error as HamtError;
2316
use fvm_shared::address::Address;
24-
2517
use fvm_shared::clock::{ChainEpoch, EPOCH_UNDEFINED};
2618
use fvm_shared::econ::TokenAmount;
2719
use fvm_shared::error::ExitCode;
@@ -30,6 +22,13 @@ use fvm_shared::{ActorID, HAMT_BIT_WIDTH};
3022
use itertools::Itertools;
3123
use num_traits::Zero;
3224

25+
use fil_actors_runtime::runtime::policy_constants::MAX_SECTOR_NUMBER;
26+
use fil_actors_runtime::runtime::Policy;
27+
use fil_actors_runtime::{
28+
actor_error, ActorContext, ActorDowncast, ActorError, Array, AsActorError, Config, Map2,
29+
DEFAULT_HAMT_CONFIG,
30+
};
31+
3332
use super::beneficiary::*;
3433
use super::deadlines::new_deadline_info;
3534
use super::policy::*;
@@ -40,6 +39,9 @@ use super::{
4039
PowerPair, QuantSpec, Sectors, TerminationResult, VestingFunds,
4140
};
4241

42+
pub type PreCommitMap<BS> = Map2<BS, SectorNumber, SectorPreCommitOnChainInfo>;
43+
pub const PRECOMMIT_CONFIG: Config = Config { bit_width: HAMT_BIT_WIDTH, ..DEFAULT_HAMT_CONFIG };
44+
4345
const PRECOMMIT_EXPIRY_AMT_BITWIDTH: u32 = 6;
4446
pub const SECTORS_AMT_BITWIDTH: u32 = 5;
4547

@@ -126,14 +128,10 @@ impl State {
126128
info_cid: Cid,
127129
period_start: ChainEpoch,
128130
deadline_idx: u64,
129-
) -> anyhow::Result<Self> {
131+
) -> Result<Self, ActorError> {
130132
let empty_precommit_map =
131-
make_empty_map::<_, ()>(store, HAMT_BIT_WIDTH).flush().map_err(|e| {
132-
e.downcast_default(
133-
ExitCode::USR_ILLEGAL_STATE,
134-
"failed to construct empty precommit map",
135-
)
136-
})?;
133+
PreCommitMap::empty(store, PRECOMMIT_CONFIG, "precommits").flush()?;
134+
137135
let empty_precommits_cleanup_array =
138136
Array::<BitField, BS>::new_with_bit_width(store, PRECOMMIT_EXPIRY_AMT_BITWIDTH)
139137
.flush()
@@ -292,14 +290,12 @@ impl State {
292290
precommits: Vec<SectorPreCommitOnChainInfo>,
293291
) -> anyhow::Result<()> {
294292
let mut precommitted =
295-
make_map_with_root_and_bitwidth(&self.pre_committed_sectors, store, HAMT_BIT_WIDTH)?;
293+
PreCommitMap::load(store, &self.pre_committed_sectors, PRECOMMIT_CONFIG, "precommits")?;
296294
for precommit in precommits.into_iter() {
297295
let sector_no = precommit.info.sector_number;
298296
let modified = precommitted
299-
.set_if_absent(u64_key(precommit.info.sector_number), precommit)
300-
.map_err(|e| {
301-
e.downcast_wrap(format!("failed to store precommitment for {:?}", sector_no,))
302-
})?;
297+
.set_if_absent(&sector_no, precommit)
298+
.with_context(|| format!("storing precommit for {}", sector_no))?;
303299
if !modified {
304300
return Err(anyhow!("sector {} already pre-commited", sector_no));
305301
}
@@ -313,10 +309,10 @@ impl State {
313309
&self,
314310
store: &BS,
315311
sector_num: SectorNumber,
316-
) -> Result<Option<SectorPreCommitOnChainInfo>, HamtError> {
312+
) -> Result<Option<SectorPreCommitOnChainInfo>, ActorError> {
317313
let precommitted =
318-
make_map_with_root_and_bitwidth(&self.pre_committed_sectors, store, HAMT_BIT_WIDTH)?;
319-
Ok(precommitted.get(&u64_key(sector_num))?.cloned())
314+
PreCommitMap::load(store, &self.pre_committed_sectors, PRECOMMIT_CONFIG, "precommits")?;
315+
Ok(precommitted.get(&sector_num)?.cloned())
320316
}
321317

322318
/// Gets and returns the requested pre-committed sectors, skipping missing sectors.
@@ -325,17 +321,15 @@ impl State {
325321
store: &BS,
326322
sector_numbers: &[SectorNumber],
327323
) -> anyhow::Result<Vec<SectorPreCommitOnChainInfo>> {
328-
let precommitted = make_map_with_root_and_bitwidth::<_, SectorPreCommitOnChainInfo>(
329-
&self.pre_committed_sectors,
330-
store,
331-
HAMT_BIT_WIDTH,
332-
)?;
324+
let precommitted =
325+
PreCommitMap::load(store, &self.pre_committed_sectors, PRECOMMIT_CONFIG, "precommits")?;
333326
let mut result = Vec::with_capacity(sector_numbers.len());
334327

335328
for &sector_number in sector_numbers {
336-
let info = match precommitted.get(&u64_key(sector_number)).map_err(|e| {
337-
e.downcast_wrap(format!("failed to load precommitment for {}", sector_number))
338-
})? {
329+
let info = match precommitted
330+
.get(&sector_number)
331+
.with_context(|| format!("loading precommit {}", sector_number))?
332+
{
339333
Some(info) => info.clone(),
340334
None => continue,
341335
};
@@ -350,17 +344,13 @@ impl State {
350344
&mut self,
351345
store: &BS,
352346
sector_nums: &[SectorNumber],
353-
) -> Result<(), HamtError> {
354-
let mut precommitted = make_map_with_root_and_bitwidth::<_, SectorPreCommitOnChainInfo>(
355-
&self.pre_committed_sectors,
356-
store,
357-
HAMT_BIT_WIDTH,
358-
)?;
359-
347+
) -> Result<(), ActorError> {
348+
let mut precommitted =
349+
PreCommitMap::load(store, &self.pre_committed_sectors, PRECOMMIT_CONFIG, "precommits")?;
360350
for &sector_num in sector_nums {
361-
let prev_entry = precommitted.delete(&u64_key(sector_num))?;
351+
let prev_entry = precommitted.delete(&sector_num)?;
362352
if prev_entry.is_none() {
363-
return Err(format!("sector {} doesn't exist", sector_num).into());
353+
return Err(actor_error!(illegal_state, "sector {} not pre-committed", sector_num));
364354
}
365355
}
366356

@@ -1052,13 +1042,12 @@ impl State {
10521042

10531043
let mut precommits_to_delete = Vec::new();
10541044
let precommitted =
1055-
make_map_with_root_and_bitwidth(&self.pre_committed_sectors, store, HAMT_BIT_WIDTH)?;
1045+
PreCommitMap::load(store, &self.pre_committed_sectors, PRECOMMIT_CONFIG, "precommits")?;
10561046

10571047
for i in sectors.iter() {
10581048
let sector_number = i as SectorNumber;
1059-
10601049
let sector: SectorPreCommitOnChainInfo =
1061-
match precommitted.get(&u64_key(sector_number))?.cloned() {
1050+
match precommitted.get(&sector_number)?.cloned() {
10621051
Some(sector) => sector,
10631052
// already committed/deleted
10641053
None => continue,
@@ -1179,15 +1168,14 @@ impl State {
11791168
) -> Result<Vec<SectorPreCommitOnChainInfo>, ActorError> {
11801169
let mut precommits = Vec::new();
11811170
let precommitted =
1182-
make_map_with_root_and_bitwidth(&self.pre_committed_sectors, store, HAMT_BIT_WIDTH)
1183-
.context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load precommitted sectors")?;
1171+
PreCommitMap::load(store, &self.pre_committed_sectors, PRECOMMIT_CONFIG, "precommits")?;
11841172
for sector_no in sector_nos.into_iter() {
11851173
let sector_no = *sector_no.borrow();
11861174
if sector_no > MAX_SECTOR_NUMBER {
11871175
return Err(actor_error!(illegal_argument; "sector number greater than maximum"));
11881176
}
11891177
let info: &SectorPreCommitOnChainInfo = precommitted
1190-
.get(&u64_key(sector_no))
1178+
.get(&sector_no)
11911179
.exit_code(ExitCode::USR_ILLEGAL_STATE)?
11921180
.ok_or_else(|| actor_error!(not_found, "sector {} not found", sector_no))?;
11931181
precommits.push(info.clone());

actors/miner/src/testing.rs

+6-20
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
use crate::{
22
power_for_sectors, BitFieldQueue, Deadline, ExpirationQueue, MinerInfo, Partition, PowerPair,
3-
QuantSpec, SectorOnChainInfo, SectorOnChainInfoFlags, SectorPreCommitOnChainInfo, Sectors,
4-
State, NO_QUANTIZATION,
3+
PreCommitMap, QuantSpec, SectorOnChainInfo, SectorOnChainInfoFlags,
4+
Sectors, State, NO_QUANTIZATION, PRECOMMIT_CONFIG,
55
};
66
use fil_actors_runtime::runtime::Policy;
7-
use fil_actors_runtime::{
8-
parse_uint_key, DealWeight, Map, MessageAccumulator, DEFAULT_HAMT_CONFIG,
9-
};
7+
use fil_actors_runtime::{DealWeight, MessageAccumulator};
108
use fvm_ipld_bitfield::BitField;
119
use fvm_ipld_blockstore::Blockstore;
1210
use fvm_ipld_encoding::CborStore;
@@ -349,23 +347,11 @@ fn check_precommits<BS: Blockstore>(
349347

350348
let mut precommit_total = TokenAmount::zero();
351349

352-
let precommited_sectors = Map::<_, SectorPreCommitOnChainInfo>::load_with_config(
353-
&state.pre_committed_sectors,
354-
store,
355-
DEFAULT_HAMT_CONFIG,
356-
);
357-
350+
let precommited_sectors =
351+
PreCommitMap::load(store, &state.pre_committed_sectors, PRECOMMIT_CONFIG, "precommits");
358352
match precommited_sectors {
359353
Ok(precommited_sectors) => {
360-
let ret = precommited_sectors.for_each(|key, precommit| {
361-
let sector_number = match parse_uint_key(key) {
362-
Ok(sector_number) => sector_number,
363-
Err(e) => {
364-
acc.add(format!("error parsing pre-commit key as uint: {e}"));
365-
return Ok(());
366-
}
367-
};
368-
354+
let ret = precommited_sectors.for_each(|sector_number, precommit| {
369355
acc.require(
370356
allocated_sectors.contains(&sector_number),
371357
format!("pre-commited sector number has not been allocated {sector_number}"),

actors/miner/tests/state_harness.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use fil_actors_runtime::{runtime::Policy, ActorError};
99
use fvm_ipld_bitfield::BitField;
1010
use fvm_ipld_encoding::BytesDe;
1111
use fvm_ipld_encoding::CborStore;
12-
use fvm_ipld_hamt::Error as HamtError;
1312
use fvm_shared::econ::TokenAmount;
1413
use fvm_shared::sector::{SectorNumber, SectorSize};
1514
use fvm_shared::{clock::ChainEpoch, sector::RegisteredPoStProof};
@@ -68,7 +67,7 @@ impl StateHarness {
6867
pub fn delete_precommitted_sectors(
6968
&mut self,
7069
sector_nums: &[SectorNumber],
71-
) -> Result<(), HamtError> {
70+
) -> Result<(), ActorError> {
7271
self.st.delete_precommitted_sectors(&self.store, sector_nums)
7372
}
7473

0 commit comments

Comments
 (0)