Skip to content

Commit 939ab15

Browse files
committed
MapKey trait
1 parent 26ce49c commit 939ab15

File tree

6 files changed

+110
-87
lines changed

6 files changed

+110
-87
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

actors/init/src/state.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct State {
1717
pub network_name: String,
1818
}
1919

20-
pub type AddressMap<'bs, BS> = Map2<'bs, BS, ActorID>;
20+
pub type AddressMap<BS> = Map2<BS, Address, ActorID>;
2121

2222
impl State {
2323
pub fn new<BS: Blockstore>(store: &BS, network_name: String) -> Result<Self, ActorError> {
@@ -41,13 +41,12 @@ impl State {
4141
let (id, existing) = if let Some(delegated_addr) = delegated_addr {
4242
// If there's a delegated address, either recall the already-mapped actor ID or
4343
// create and map a new one.
44-
let key = delegated_addr.to_bytes();
45-
if let Some(existing_id) = map.get(&key)? {
44+
if let Some(existing_id) = map.get(delegated_addr)? {
4645
(*existing_id, true)
4746
} else {
4847
let new_id = self.next_id;
4948
self.next_id += 1;
50-
map.set(&key, new_id)?;
49+
map.set(delegated_addr, new_id)?;
5150
(new_id, false)
5251
}
5352
} else {
@@ -58,7 +57,7 @@ impl State {
5857
};
5958

6059
// Map the robust address to the ID, failing if it's already mapped to anything.
61-
let is_new = map.set_if_absent(&robust_addr.to_bytes(), id)?;
60+
let is_new = map.set_if_absent(robust_addr, id)?;
6261
if !is_new {
6362
return Err(actor_error!(
6463
forbidden,
@@ -89,7 +88,7 @@ impl State {
8988
return Ok(Some(*addr));
9089
}
9190
let map = AddressMap::load(store, &self.address_map, DEFAULT_CONF, "addresses")?;
92-
let found = map.get(&addr.to_bytes())?;
91+
let found = map.get(addr)?;
9392
Ok(found.copied().map(Address::new_id))
9493
}
9594
}

actors/init/src/testing.rs

+10-21
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
use std::collections::HashMap;
22

3-
use fil_actors_runtime::{
4-
AsActorError, MessageAccumulator, DEFAULT_CONF, FIRST_NON_SINGLETON_ADDR,
5-
};
63
use fvm_ipld_blockstore::Blockstore;
7-
use fvm_shared::error::ExitCode;
84
use fvm_shared::{
95
address::{Address, Protocol},
106
ActorID,
117
};
128

9+
use fil_actors_runtime::{MessageAccumulator, DEFAULT_CONF, FIRST_NON_SINGLETON_ADDR};
10+
1311
use crate::state::AddressMap;
1412
use crate::State;
1513

@@ -39,42 +37,33 @@ pub fn check_state_invariants<BS: Blockstore>(
3937
match AddressMap::load(store, &state.address_map, DEFAULT_CONF, "addresses") {
4038
Ok(address_map) => {
4139
let ret = address_map.for_each(|key, actor_id| {
42-
let key_address =
43-
Address::from_bytes(key).exit_code(ExitCode::USR_ILLEGAL_STATE)?;
44-
45-
acc.require(
46-
key_address.protocol() != Protocol::ID,
47-
format!("key {key_address} is an ID address"),
48-
);
40+
acc.require(key.protocol() != Protocol::ID, format!("key {key} is an ID address"));
4941
acc.require(
5042
actor_id >= &FIRST_NON_SINGLETON_ADDR,
5143
format!("unexpected singleton ID value {actor_id}"),
5244
);
5345

54-
match key_address.protocol() {
46+
match key.protocol() {
5547
Protocol::ID => {
56-
acc.add(format!("key {key_address} is an ID address"));
48+
acc.add(format!("key {key} is an ID address"));
5749
}
5850
Protocol::Delegated => {
59-
if let Some(duplicate) =
60-
delegated_address_by_id.insert(*actor_id, key_address)
61-
{
51+
if let Some(duplicate) = delegated_address_by_id.insert(*actor_id, key) {
6252
acc.add(format!(
63-
"duplicate mapping to ID {actor_id}: {key_address} {duplicate}"
53+
"duplicate mapping to ID {actor_id}: {key} {duplicate}"
6454
));
6555
}
6656
}
6757
_ => {
68-
if let Some(duplicate) = stable_address_by_id.insert(*actor_id, key_address)
69-
{
58+
if let Some(duplicate) = stable_address_by_id.insert(*actor_id, key) {
7059
acc.add(format!(
71-
"duplicate mapping to ID {actor_id}: {key_address} {duplicate}"
60+
"duplicate mapping to ID {actor_id}: {key} {duplicate}"
7261
));
7362
}
7463
}
7564
}
7665

77-
init_summary.ids_by_address.insert(key_address, *actor_id);
66+
init_summary.ids_by_address.insert(key, *actor_id);
7867

7968
Ok(())
8069
});

runtime/Cargo.toml

+21-20
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,41 @@ edition = "2021"
88
repository = "https://github.com/filecoin-project/builtin-actors"
99

1010
[dependencies]
11-
fvm_ipld_hamt = { workspace = true }
12-
fvm_ipld_amt = { workspace = true }
13-
fvm_shared = { workspace = true }
14-
num = { workspace = true }
15-
num-traits = { workspace = true }
16-
num-derive = { workspace = true }
17-
serde = { workspace = true }
18-
lazy_static = { workspace = true, optional = true }
19-
unsigned-varint = { workspace = true }
11+
anyhow = { workspace = true }
2012
byteorder = { workspace = true }
13+
castaway = { workspace = true }
2114
cid = { workspace = true }
22-
log = { workspace = true }
23-
thiserror = { workspace = true }
24-
anyhow = { workspace = true }
25-
fvm_sdk = { workspace = true, optional = true }
15+
fvm_ipld_amt = { workspace = true }
16+
fvm_ipld_bitfield = { workspace = true }
2617
fvm_ipld_blockstore = { workspace = true }
2718
fvm_ipld_encoding = { workspace = true }
28-
fvm_ipld_bitfield = { workspace = true }
29-
multihash = { workspace = true }
30-
serde_repr = { workspace = true }
31-
regex = { workspace = true }
19+
fvm_ipld_hamt = { workspace = true }
20+
fvm_sdk = { workspace = true, optional = true }
21+
fvm_shared = { workspace = true }
22+
integer-encoding = { workspace = true }
3223
itertools = { workspace = true }
24+
lazy_static = { workspace = true, optional = true }
25+
log = { workspace = true }
26+
multihash = { workspace = true }
27+
num = { workspace = true }
28+
num-derive = { workspace = true }
29+
num-traits = { workspace = true }
3330
paste = { workspace = true }
34-
castaway = { workspace = true }
31+
regex = { workspace = true }
32+
serde = { workspace = true }
33+
serde_repr = { workspace = true }
34+
thiserror = { workspace = true }
35+
unsigned-varint = { workspace = true }
3536

3637
# A fake-proofs dependency but... we can't select on that feature here because we enable it from
3738
# build.rs.
3839
sha2 = { workspace = true }
3940

4041
# test_util
41-
rand = { workspace = true, optional = true }
42-
hex = { workspace = true, optional = true }
4342
blake2b_simd = { workspace = true, optional = true }
43+
hex = { workspace = true, optional = true }
4444
pretty_env_logger = { workspace = true, optional = true }
45+
rand = { workspace = true, optional = true }
4546

4647
[dependencies.libsecp256k1]
4748
workspace = true

runtime/src/lib.rs

+9-17
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
// Copyright 2019-2022 ChainSafe Systems
22
// SPDX-License-Identifier: Apache-2.0, MIT
33

4-
use builtin::HAMT_BIT_WIDTH;
54
use cid::Cid;
65
use fvm_ipld_amt::Amt;
76
use fvm_ipld_blockstore::Blockstore;
7+
#[cfg(not(feature = "fil-actor"))]
8+
use fvm_ipld_hamt::Sha256;
89
use fvm_ipld_hamt::{BytesKey, Error as HamtError, Hamt};
9-
use fvm_shared::address::Address;
1010
use fvm_shared::bigint::BigInt;
1111
pub use fvm_shared::BLOCKS_PER_EPOCH as EXPECTED_LEADERS_PER_EPOCH;
1212
use serde::de::DeserializeOwned;
1313
use serde::Serialize;
1414
use unsigned_varint::decode::Error as UVarintError;
15-
pub use {fvm_ipld_amt, fvm_ipld_hamt};
1615

17-
pub use self::actor_error::*;
18-
pub use self::builtin::*;
19-
pub use self::util::*;
20-
use crate::runtime::Runtime;
16+
use builtin::HAMT_BIT_WIDTH;
17+
pub use dispatch::{dispatch, dispatch_default};
18+
pub use {fvm_ipld_amt, fvm_ipld_hamt};
2119

2220
#[cfg(feature = "fil-actor")]
2321
use crate::runtime::hash_algorithm::FvmHashSha256;
22+
use crate::runtime::Runtime;
2423

25-
#[cfg(not(feature = "fil-actor"))]
26-
use fvm_ipld_hamt::Sha256;
24+
pub use self::actor_error::*;
25+
pub use self::builtin::*;
26+
pub use self::util::*;
2727

2828
pub mod actor_error;
2929
pub mod builtin;
3030
pub mod runtime;
3131
pub mod util;
3232

3333
mod dispatch;
34-
pub use dispatch::{dispatch, dispatch_default};
3534
#[cfg(feature = "test_utils")]
3635
pub mod test_utils;
3736

@@ -45,7 +44,6 @@ macro_rules! wasm_trampoline {
4544
};
4645
}
4746

48-
/// XXX move to map
4947
#[cfg(feature = "fil-actor")]
5048
type Hasher = FvmHashSha256;
5149

@@ -113,12 +111,6 @@ pub trait Keyer {
113111
fn key(&self) -> BytesKey;
114112
}
115113

116-
impl Keyer for Address {
117-
fn key(&self) -> BytesKey {
118-
self.to_bytes().into()
119-
}
120-
}
121-
122114
impl Keyer for u64 {
123115
fn key(&self) -> BytesKey {
124116
u64_key(*self)

0 commit comments

Comments
 (0)