Skip to content

Commit

Permalink
Optimization for #636 | Perform HAMT SHA256 hashes natively via a sys…
Browse files Browse the repository at this point in the history
…call (#712)

Signed-off-by: shamb0 <[email protected]>
  • Loading branch information
shamb0 authored Sep 26, 2022
1 parent d7eacef commit fc3c24b
Show file tree
Hide file tree
Showing 15 changed files with 106 additions and 45 deletions.
7 changes: 3 additions & 4 deletions actors/account/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ keywords = ["filecoin", "web3", "wasm"]
crate-type = ["cdylib", "lib"]

[dependencies]
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime", features = ["fil-actor"] }
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime" }
fvm_shared = { version = "2.0.0-alpha.2", default-features = false }
serde = { version = "1.0.136", features = ["derive"] }
num-traits = "0.2.14"
Expand All @@ -23,8 +23,7 @@ fvm_ipld_encoding = "0.2.2"
anyhow = "1.0.56"

[dev-dependencies]
fil_actors_runtime = { path = "../../runtime", features = ["test_utils", "sector-default"] }
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime", features = ["test_utils", "sector-default"] }

[features]
fil-actor = []

fil-actor = ["fil_actors_runtime/fil-actor"]
8 changes: 4 additions & 4 deletions actors/cron/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ keywords = ["filecoin", "web3", "wasm"]
crate-type = ["cdylib", "lib"]

[dependencies]
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime", features = ["fil-actor"] }
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime" }
fvm_shared = { version = "2.0.0-alpha.2", default-features = false }
num-traits = "0.2.14"
num-derive = "0.3.3"
Expand All @@ -24,7 +24,7 @@ fvm_ipld_blockstore = "0.1.1"
fvm_ipld_encoding = "0.2.2"

[dev-dependencies]
fil_actors_runtime = { path = "../../runtime", features = ["test_utils", "sector-default"] }
[features]
fil-actor = []
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime", features = ["test_utils", "sector-default"] }

[features]
fil-actor = ["fil_actors_runtime/fil-actor"]
8 changes: 4 additions & 4 deletions actors/init/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ keywords = ["filecoin", "web3", "wasm"]
crate-type = ["cdylib", "lib"]

[dependencies]
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime", features = ["fil-actor"] }
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime" }
fvm_shared = { version = "2.0.0-alpha.2", default-features = false }
fvm_ipld_hamt = "0.5.1"
serde = { version = "1.0.136", features = ["derive"] }
Expand All @@ -27,7 +27,7 @@ fvm_ipld_blockstore = "0.1.1"
fvm_ipld_encoding = "0.2.2"

[dev-dependencies]
fil_actors_runtime = { path = "../../runtime", features = ["test_utils", "sector-default"] }
[features]
fil-actor = []
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime", features = ["test_utils", "sector-default"] }

[features]
fil-actor = ["fil_actors_runtime/fil-actor"]
7 changes: 3 additions & 4 deletions actors/market/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ keywords = ["filecoin", "web3", "wasm"]
crate-type = ["cdylib", "lib"]

[dependencies]
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime", features = ["fil-actor"] }
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime" }
fvm_ipld_hamt = "0.5.1"
fvm_shared = { version = "2.0.0-alpha.2", default-features = false }
fvm_ipld_bitfield = "0.5.2"
Expand All @@ -29,7 +29,7 @@ fvm_ipld_encoding = "0.2.2"
libipld-core = { version = "0.13.1", features = ["serde-codec"] }

[dev-dependencies]
fil_actors_runtime = { path = "../../runtime", features = ["test_utils", "sector-default"] }
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime", features = ["test_utils", "sector-default"] }
fil_actor_power = { path = "../power" }
fil_actor_reward = { path = "../reward" }
fil_actor_verifreg = { path = "../verifreg" }
Expand All @@ -39,5 +39,4 @@ regex = "1"
itertools = "0.10"

[features]
fil-actor = []

fil-actor = ["fil_actors_runtime/fil-actor"]
8 changes: 4 additions & 4 deletions actors/miner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ keywords = ["filecoin", "web3", "wasm"]
crate-type = ["cdylib", "lib"]

[dependencies]
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime", features = ["fil-actor"] }
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime" }
fvm_shared = { version = "2.0.0-alpha.2", default-features = false }
fvm_ipld_bitfield = "0.5.2"
fvm_ipld_amt = { version = "0.4.2", features = ["go-interop"] }
Expand All @@ -33,7 +33,7 @@ fvm_ipld_encoding = "0.2.2"
multihash = { version = "0.16.2", default-features = false }

[dev-dependencies]
fil_actors_runtime = { path = "../../runtime", features = ["test_utils", "sector-default"] }
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime", features = ["test_utils", "sector-default"] }
fil_actor_account = { path = "../account" }
fil_actor_reward = { path = "../reward" }
fil_actor_power = { path = "../power" }
Expand All @@ -42,6 +42,6 @@ rand = "0.8.5"
cid = { version = "0.8.3", default-features = false, features = ["serde-codec"] }
multihash = { version = "0.16.1", default-features = false }
test-case = "2.2.1"
[features]
fil-actor = []

[features]
fil-actor = ["fil_actors_runtime/fil-actor"]
8 changes: 4 additions & 4 deletions actors/multisig/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ keywords = ["filecoin", "web3", "wasm"]
crate-type = ["cdylib", "lib"]

[dependencies]
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime", features = ["fil-actor"] }
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime" }
fvm_shared = { version = "2.0.0-alpha.2", default-features = false }
fvm_ipld_hamt = "0.5.1"
num-traits = "0.2.14"
Expand All @@ -28,8 +28,8 @@ fvm_ipld_blockstore = "0.1.1"
fvm_ipld_encoding = "0.2.2"

[dev-dependencies]
fil_actors_runtime = { path = "../../runtime", features = ["test_utils", "sector-default"] }
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime", features = ["test_utils", "sector-default"] }
lazy_static = "1.4.0"
[features]
fil-actor = []

[features]
fil-actor = ["fil_actors_runtime/fil-actor"]
8 changes: 4 additions & 4 deletions actors/paych/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ keywords = ["filecoin", "web3", "wasm"]
crate-type = ["cdylib", "lib"]

[dependencies]
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime", features = ["fil-actor"] }
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime" }
fvm_shared = { version = "2.0.0-alpha.2", default-features = false }
num-traits = "0.2.14"
num-derive = "0.3.3"
Expand All @@ -25,9 +25,9 @@ fvm_ipld_blockstore = "0.1.1"
fvm_ipld_encoding = "0.2.2"

[dev-dependencies]
fil_actors_runtime = { path = "../../runtime", features = ["test_utils", "sector-default"] }
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime", features = ["test_utils", "sector-default"] }
fvm_ipld_amt = { version = "0.4.2", features = ["go-interop"] }
derive_builder = "0.10.2"
[features]
fil-actor = []

[features]
fil-actor = ["fil_actors_runtime/fil-actor"]
8 changes: 4 additions & 4 deletions actors/power/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ keywords = ["filecoin", "web3", "wasm"]
crate-type = ["cdylib", "lib"]

[dependencies]
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime", features = ["fil-actor"] }
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime" }
fvm_shared = { version = "2.0.0-alpha.2", default-features = false }
fvm_ipld_hamt = "0.5.1"
num-traits = "0.2.14"
Expand All @@ -30,8 +30,8 @@ fvm_ipld_blockstore = "0.1.1"
fvm_ipld_encoding = "0.2.2"

[dev-dependencies]
fil_actors_runtime = { path = "../../runtime", features = ["test_utils", "sector-default"] }
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime", features = ["test_utils", "sector-default"] }
fil_actor_reward = { path = "../reward" }
[features]
fil-actor = []

[features]
fil-actor = ["fil_actors_runtime/fil-actor"]
7 changes: 4 additions & 3 deletions actors/reward/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ keywords = ["filecoin", "web3", "wasm"]
crate-type = ["cdylib", "lib"]

[dependencies]
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime", features = ["fil-actor"] }
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime" }
fvm_shared = { version = "2.0.0-alpha.2", default-features = false }
num-traits = "0.2.14"
num-derive = "0.3.3"
Expand All @@ -25,7 +25,8 @@ fvm_ipld_blockstore = "0.1.1"
fvm_ipld_encoding = "0.2.2"

[dev-dependencies]
fil_actors_runtime = { path = "../../runtime", features = ["test_utils", "sector-default"] }
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime", features = ["test_utils", "sector-default"] }
num = "0.4.0"

[features]
fil-actor = []
fil-actor = ["fil_actors_runtime/fil-actor"]
8 changes: 4 additions & 4 deletions actors/system/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ keywords = ["filecoin", "web3", "wasm"]
crate-type = ["cdylib", "lib"]

[dependencies]
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime", features = ["fil-actor"] }
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime" }
fvm_shared = { version = "2.0.0-alpha.2", default-features = false }
fvm_ipld_encoding = "0.2.2"
fvm_ipld_blockstore = "0.1.1"
Expand All @@ -25,7 +25,7 @@ serde = { version = "1.0.136", features = ["derive"] }
cid = { version = "0.8.3", default-features = false, features = ["serde-codec"] }

[dev-dependencies]
fil_actors_runtime = { path = "../../runtime", features = ["test_utils", "sector-default"] }
[features]
fil-actor = []
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime", features = ["test_utils", "sector-default"] }

[features]
fil-actor = ["fil_actors_runtime/fil-actor"]
7 changes: 4 additions & 3 deletions actors/verifreg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ keywords = ["filecoin", "web3", "wasm"]
crate-type = ["cdylib", "lib"]

[dependencies]
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime", features = ["fil-actor"] }
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime" }
fvm_shared = { version = "2.0.0-alpha.2", default-features = false }
serde = { version = "1.0.136", features = ["derive"] }
num-traits = "0.2.14"
Expand All @@ -27,7 +27,8 @@ fvm_ipld_blockstore = "0.1.1"
fvm_ipld_encoding = "0.2.2"

[dev-dependencies]
fil_actors_runtime = { path = "../../runtime", features = ["test_utils", "sector-default"] }
fil_actors_runtime = { version = "9.0.0-alpha.1", path = "../../runtime", features = ["test_utils", "sector-default"] }

[features]
fil-actor = []
fil-actor = ["fil_actors_runtime/fil-actor"]

14 changes: 13 additions & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ pub use self::builtin::*;
pub use self::util::*;
use crate::runtime::Runtime;

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

#[cfg(not(feature = "fil-actor"))]
use fvm_ipld_hamt::Sha256;

pub mod actor_error;
pub mod builtin;
pub mod runtime;
Expand All @@ -36,8 +42,14 @@ macro_rules! wasm_trampoline {
};
}

#[cfg(feature = "fil-actor")]
type Hasher = FvmHashSha256;

#[cfg(not(feature = "fil-actor"))]
type Hasher = Sha256;

/// Map type to be used within actors. The underlying type is a HAMT.
pub type Map<'bs, BS, V> = Hamt<&'bs BS, V, BytesKey>;
pub type Map<'bs, BS, V> = Hamt<&'bs BS, V, BytesKey, Hasher>;

/// Array type used within actors. The underlying type is an AMT.
pub type Array<'bs, V, BS> = Amt<V, &'bs BS>;
Expand Down
47 changes: 47 additions & 0 deletions runtime/src/runtime/hash_algorithm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2019-2022 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

// use fvm_ipld_hamt::{Hash, HashAlgorithm, HashedKey};
use fvm_ipld_hamt::{Hash, HashAlgorithm};
use fvm_sdk as fvm;
use fvm_shared::crypto::hash::SupportedHashes;
use std::hash::Hasher;

pub type HashedKey = [u8; 32];

#[derive(Default)]
pub struct RuntimeHasherWrapper(pub Vec<u8>);

impl Hasher for RuntimeHasherWrapper {
fn finish(&self) -> u64 {
// u64 hash not used in hamt
0
}

fn write(&mut self, bytes: &[u8]) {
self.0.extend_from_slice(bytes);
}
}

#[derive(Default, Debug)]
pub struct FvmHashSha256;

impl HashAlgorithm for FvmHashSha256 {
fn hash<X>(key: &X) -> HashedKey
where
X: Hash + ?Sized,
{
let mut rval_digest: HashedKey = Default::default();
let mut hasher = RuntimeHasherWrapper::default();
key.hash(&mut hasher);

// TODO :: Enable this when moving to
// fvm_sdk 3.0.0-alpha.2,
// looks like fvm_sdk 2.0.0-alpha.3 doesn't support `hash_into()` api
// fvm::crypto::hash_into(SupportedHashes::Sha2_256, &hasher.0, &mut rval_digest);

rval_digest.copy_from_slice(&fvm::crypto::hash(SupportedHashes::Sha2_256, &hasher.0));

rval_digest
}
}
2 changes: 2 additions & 0 deletions runtime/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ mod randomness;
mod actor_blockstore;
#[cfg(feature = "fil-actor")]
pub mod fvm;
#[cfg(feature = "fil-actor")]
pub(crate) mod hash_algorithm;

pub(crate) mod empty;
pub use empty::EMPTY_ARR_CID;
Expand Down
4 changes: 2 additions & 2 deletions state/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ serde = { version = "1.0.136", features = ["derive"] }
cid = { version = "0.8.3", default-features = false, features = ["serde-codec"] }

[dev-dependencies]
[features]
fil-actor = []

[features]
fil-actor = ["fil_actors_runtime/fil-actor"]

0 comments on commit fc3c24b

Please sign in to comment.