Skip to content

Commit

Permalink
MapKey trait
Browse files Browse the repository at this point in the history
  • Loading branch information
anorth committed Aug 8, 2023
1 parent 26ce49c commit 939ab15
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 87 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions actors/init/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct State {
pub network_name: String,
}

pub type AddressMap<'bs, BS> = Map2<'bs, BS, ActorID>;
pub type AddressMap<BS> = Map2<BS, Address, ActorID>;

impl State {
pub fn new<BS: Blockstore>(store: &BS, network_name: String) -> Result<Self, ActorError> {
Expand All @@ -41,13 +41,12 @@ impl State {
let (id, existing) = if let Some(delegated_addr) = delegated_addr {
// If there's a delegated address, either recall the already-mapped actor ID or
// create and map a new one.
let key = delegated_addr.to_bytes();
if let Some(existing_id) = map.get(&key)? {
if let Some(existing_id) = map.get(delegated_addr)? {
(*existing_id, true)
} else {
let new_id = self.next_id;
self.next_id += 1;
map.set(&key, new_id)?;
map.set(delegated_addr, new_id)?;
(new_id, false)
}
} else {
Expand All @@ -58,7 +57,7 @@ impl State {
};

// Map the robust address to the ID, failing if it's already mapped to anything.
let is_new = map.set_if_absent(&robust_addr.to_bytes(), id)?;
let is_new = map.set_if_absent(robust_addr, id)?;
if !is_new {
return Err(actor_error!(
forbidden,
Expand Down Expand Up @@ -89,7 +88,7 @@ impl State {
return Ok(Some(*addr));
}
let map = AddressMap::load(store, &self.address_map, DEFAULT_CONF, "addresses")?;
let found = map.get(&addr.to_bytes())?;
let found = map.get(addr)?;
Ok(found.copied().map(Address::new_id))
}
}
31 changes: 10 additions & 21 deletions actors/init/src/testing.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use std::collections::HashMap;

use fil_actors_runtime::{
AsActorError, MessageAccumulator, DEFAULT_CONF, FIRST_NON_SINGLETON_ADDR,
};
use fvm_ipld_blockstore::Blockstore;
use fvm_shared::error::ExitCode;
use fvm_shared::{
address::{Address, Protocol},
ActorID,
};

use fil_actors_runtime::{MessageAccumulator, DEFAULT_CONF, FIRST_NON_SINGLETON_ADDR};

use crate::state::AddressMap;
use crate::State;

Expand Down Expand Up @@ -39,42 +37,33 @@ pub fn check_state_invariants<BS: Blockstore>(
match AddressMap::load(store, &state.address_map, DEFAULT_CONF, "addresses") {
Ok(address_map) => {
let ret = address_map.for_each(|key, actor_id| {
let key_address =
Address::from_bytes(key).exit_code(ExitCode::USR_ILLEGAL_STATE)?;

acc.require(
key_address.protocol() != Protocol::ID,
format!("key {key_address} is an ID address"),
);
acc.require(key.protocol() != Protocol::ID, format!("key {key} is an ID address"));
acc.require(
actor_id >= &FIRST_NON_SINGLETON_ADDR,
format!("unexpected singleton ID value {actor_id}"),
);

match key_address.protocol() {
match key.protocol() {
Protocol::ID => {
acc.add(format!("key {key_address} is an ID address"));
acc.add(format!("key {key} is an ID address"));
}
Protocol::Delegated => {
if let Some(duplicate) =
delegated_address_by_id.insert(*actor_id, key_address)
{
if let Some(duplicate) = delegated_address_by_id.insert(*actor_id, key) {
acc.add(format!(
"duplicate mapping to ID {actor_id}: {key_address} {duplicate}"
"duplicate mapping to ID {actor_id}: {key} {duplicate}"
));
}
}
_ => {
if let Some(duplicate) = stable_address_by_id.insert(*actor_id, key_address)
{
if let Some(duplicate) = stable_address_by_id.insert(*actor_id, key) {
acc.add(format!(
"duplicate mapping to ID {actor_id}: {key_address} {duplicate}"
"duplicate mapping to ID {actor_id}: {key} {duplicate}"
));
}
}
}

init_summary.ids_by_address.insert(key_address, *actor_id);
init_summary.ids_by_address.insert(key, *actor_id);

Ok(())
});
Expand Down
41 changes: 21 additions & 20 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,41 @@ edition = "2021"
repository = "https://github.com/filecoin-project/builtin-actors"

[dependencies]
fvm_ipld_hamt = { workspace = true }
fvm_ipld_amt = { workspace = true }
fvm_shared = { workspace = true }
num = { workspace = true }
num-traits = { workspace = true }
num-derive = { workspace = true }
serde = { workspace = true }
lazy_static = { workspace = true, optional = true }
unsigned-varint = { workspace = true }
anyhow = { workspace = true }
byteorder = { workspace = true }
castaway = { workspace = true }
cid = { workspace = true }
log = { workspace = true }
thiserror = { workspace = true }
anyhow = { workspace = true }
fvm_sdk = { workspace = true, optional = true }
fvm_ipld_amt = { workspace = true }
fvm_ipld_bitfield = { workspace = true }
fvm_ipld_blockstore = { workspace = true }
fvm_ipld_encoding = { workspace = true }
fvm_ipld_bitfield = { workspace = true }
multihash = { workspace = true }
serde_repr = { workspace = true }
regex = { workspace = true }
fvm_ipld_hamt = { workspace = true }
fvm_sdk = { workspace = true, optional = true }
fvm_shared = { workspace = true }
integer-encoding = { workspace = true }
itertools = { workspace = true }
lazy_static = { workspace = true, optional = true }
log = { workspace = true }
multihash = { workspace = true }
num = { workspace = true }
num-derive = { workspace = true }
num-traits = { workspace = true }
paste = { workspace = true }
castaway = { workspace = true }
regex = { workspace = true }
serde = { workspace = true }
serde_repr = { workspace = true }
thiserror = { workspace = true }
unsigned-varint = { workspace = true }

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

# test_util
rand = { workspace = true, optional = true }
hex = { workspace = true, optional = true }
blake2b_simd = { workspace = true, optional = true }
hex = { workspace = true, optional = true }
pretty_env_logger = { workspace = true, optional = true }
rand = { workspace = true, optional = true }

[dependencies.libsecp256k1]
workspace = true
Expand Down
26 changes: 9 additions & 17 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
// Copyright 2019-2022 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use builtin::HAMT_BIT_WIDTH;
use cid::Cid;
use fvm_ipld_amt::Amt;
use fvm_ipld_blockstore::Blockstore;
#[cfg(not(feature = "fil-actor"))]
use fvm_ipld_hamt::Sha256;
use fvm_ipld_hamt::{BytesKey, Error as HamtError, Hamt};
use fvm_shared::address::Address;
use fvm_shared::bigint::BigInt;
pub use fvm_shared::BLOCKS_PER_EPOCH as EXPECTED_LEADERS_PER_EPOCH;
use serde::de::DeserializeOwned;
use serde::Serialize;
use unsigned_varint::decode::Error as UVarintError;
pub use {fvm_ipld_amt, fvm_ipld_hamt};

pub use self::actor_error::*;
pub use self::builtin::*;
pub use self::util::*;
use crate::runtime::Runtime;
use builtin::HAMT_BIT_WIDTH;
pub use dispatch::{dispatch, dispatch_default};
pub use {fvm_ipld_amt, fvm_ipld_hamt};

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

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

pub mod actor_error;
pub mod builtin;
pub mod runtime;
pub mod util;

mod dispatch;
pub use dispatch::{dispatch, dispatch_default};
#[cfg(feature = "test_utils")]
pub mod test_utils;

Expand All @@ -45,7 +44,6 @@ macro_rules! wasm_trampoline {
};
}

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

Expand Down Expand Up @@ -113,12 +111,6 @@ pub trait Keyer {
fn key(&self) -> BytesKey;
}

impl Keyer for Address {
fn key(&self) -> BytesKey {
self.to_bytes().into()
}
}

impl Keyer for u64 {
fn key(&self) -> BytesKey {
u64_key(*self)
Expand Down
Loading

0 comments on commit 939ab15

Please sign in to comment.