Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fluence batching actor #6

Open
wants to merge 19 commits into
base: kras-block-cache-with-recover
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
705 changes: 408 additions & 297 deletions Cargo.lock

Large diffs are not rendered by default.

89 changes: 46 additions & 43 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
[workspace]
resolver = "2"
members = [
# contracts
"contracts/binding",
# ipc
"ipc/cli",
"ipc/wallet",
"ipc/provider",
"ipc/api",
"ipc/types",

# ipld
"ipld/resolver",
# contracts
"contracts/binding",
# ipc
"ipc/cli",
"ipc/wallet",
"ipc/provider",
"ipc/api",
"ipc/types",

# ipld
"ipld/resolver",

# fendermint
"fendermint/abci",
Expand Down Expand Up @@ -79,29 +79,29 @@ hex-literal = "0.4.1"
im = "15.1.0"
integer-encoding = { version = "3.0.3", default-features = false }
jsonrpc-v2 = { version = "0.11", default-features = false, features = [
"bytes-v10",
"bytes-v10",
] }
k256 = "0.11" # Same as tendermint-rs
lazy_static = "1.4"
libipld = { version = "0.16", default-features = false, features = [
"dag-cbor",
"dag-cbor",
] }
libp2p = { version = "0.53", default-features = false, features = [
"gossipsub",
"kad",
"identify",
"ping",
"noise",
"yamux",
"tcp",
"dns",
"request-response",
"metrics",
"tokio",
"macros",
"serde",
"secp256k1",
"plaintext",
"gossipsub",
"kad",
"identify",
"ping",
"noise",
"yamux",
"tcp",
"dns",
"request-response",
"metrics",
"tokio",
"macros",
"serde",
"secp256k1",
"plaintext",
] }
libp2p-mplex = { version = "0.41" }
# libp2p-bitswap = "0.25.1"
Expand All @@ -113,8 +113,8 @@ lru_time_cache = "0.11"
merkle-tree-rs = "0.1.0"
multiaddr = "0.18"
multihash = { version = "0.18.1", default-features = false, features = [
"sha2",
"sha3",
"sha2",
"sha3",
] }
num-bigint = "0.4"
num-derive = "0.3"
Expand Down Expand Up @@ -144,12 +144,12 @@ strum = { version = "0.26.1", features = ["derive"] }
tempfile = "3.7"
thiserror = "1"
tokio = { version = "1", features = [
"rt-multi-thread",
"macros",
"fs",
"io-util",
"io-std",
"sync",
"rt-multi-thread",
"macros",
"fs",
"io-util",
"io-std",
"sync",
] }
tokio-stream = "0.1.14"
tokio-util = { version = "0.7.8", features = ["compat"] }
Expand Down Expand Up @@ -208,8 +208,8 @@ fil_actors_runtime = { git = "https://github.com/filecoin-project/builtin-actors
fendermint_actor_eam = { path = "./fendermint/actors/eam" }

cid = { version = "0.10.1", default-features = false, features = [
"serde-codec",
"std",
"serde-codec",
"std",
] }

frc42_dispatch = "6.0.0"
Expand All @@ -219,15 +219,18 @@ tower-abci = { version = "0.7" }
tendermint = { version = "0.31", features = ["secp256k1"] }
tendermint-config = "0.33.0"
tendermint-rpc = { version = "0.31", features = [
"secp256k1",
"http-client",
"websocket-client",
"secp256k1",
"http-client",
"websocket-client",
] }
tendermint-proto = { version = "0.31" }

# Fluence
fluence-actor-sdk = { version = "0.1.2" }
fluence-fendermint-syscall = { version = "0.1.5" }
#fluence-actor-sdk = { version = "0.1.2" }
#fluence-fendermint-syscall = { version = "0.1.5" }

fluence-actor-sdk = { git = "https://github.com/fluencelabs/fluence-fendermint", branch = "misc/add-logs" }
fluence-fendermint-syscall = { git = "https://github.com/fluencelabs/fluence-fendermint", branch = "misc/add-logs" }

[patch.crates-io]
# Use stable-only features.
Expand Down
61 changes: 0 additions & 61 deletions contracts/binding/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,64 +1,3 @@
// DO NOT EDIT! This file was generated by build.rs
#[macro_use]
mod convert;
#[allow(clippy::all)]
pub mod checkpointing_facet;
#[allow(clippy::all)]
pub mod diamond_cut_facet;
#[allow(clippy::all)]
pub mod diamond_loupe_facet;
#[allow(clippy::all)]
pub mod gateway_diamond;
#[allow(clippy::all)]
pub mod gateway_getter_facet;
#[allow(clippy::all)]
pub mod gateway_manager_facet;
#[allow(clippy::all)]
pub mod gateway_messenger_facet;
#[allow(clippy::all)]
pub mod i_diamond;
#[allow(clippy::all)]
pub mod lib_gateway;
#[allow(clippy::all)]
pub mod lib_quorum;
#[allow(clippy::all)]
pub mod lib_staking;
#[allow(clippy::all)]
pub mod lib_staking_change_log;
#[allow(clippy::all)]
pub mod ownership_facet;
#[allow(clippy::all)]
pub mod register_subnet_facet;
#[allow(clippy::all)]
pub mod subnet_actor_checkpointing_facet;
#[allow(clippy::all)]
pub mod subnet_actor_diamond;
#[allow(clippy::all)]
pub mod subnet_actor_getter_facet;
#[allow(clippy::all)]
pub mod subnet_actor_manager_facet;
#[allow(clippy::all)]
pub mod subnet_actor_pause_facet;
#[allow(clippy::all)]
pub mod subnet_actor_reward_facet;
#[allow(clippy::all)]
pub mod subnet_getter_facet;
#[allow(clippy::all)]
pub mod subnet_registry_diamond;
#[allow(clippy::all)]
pub mod top_down_finality_facet;
#[allow(clippy::all)]
pub mod xnet_messaging_facet;

// The list of contracts need to convert FvmAddress to fvm_shared::Address
fvm_address_conversion!(gateway_manager_facet);
fvm_address_conversion!(gateway_getter_facet);
fvm_address_conversion!(xnet_messaging_facet);
fvm_address_conversion!(gateway_messenger_facet);
fvm_address_conversion!(subnet_actor_checkpointing_facet);
fvm_address_conversion!(subnet_actor_getter_facet);
fvm_address_conversion!(lib_gateway);

// The list of contracts that need to convert common types between each other
common_type_conversion!(subnet_actor_getter_facet, checkpointing_facet);
common_type_conversion!(subnet_actor_getter_facet, xnet_messaging_facet);
2 changes: 1 addition & 1 deletion contracts/lib/openzeppelin-contracts
55 changes: 46 additions & 9 deletions fendermint/actors/fluence/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use fvm_ipld_encoding::ipld_block::IpldBlock;
use fvm_shared::error::ExitCode;
use fvm_shared::error::{ErrorNumber, ExitCode};
use fvm_shared::MethodNum;
use num_derive::FromPrimitive;

use fil_actors_runtime::runtime::{ActorCode, Runtime};
use fil_actors_runtime::{actor_dispatch, FIRST_EXPORTED_METHOD_NUMBER};
use fil_actors_runtime::{actor_error, ActorError};

use crate::types::RandomXArguments;
use crate::types::RandomXResult;
use crate::types::{RandomXArguments, RandomXArgumentsBatched, RandomXResultBatched};

pub mod types;

Expand All @@ -23,6 +23,7 @@ fil_actors_runtime::wasm_trampoline!(FluenceActor);
#[repr(u64)]
pub enum Method {
RunRandomX = frc42_dispatch::method_hash!("RunRandomX"),
RunRandomXBatched = frc42_dispatch::method_hash!("RunRandomXBatched"),
}

/// Account Actor
Expand All @@ -37,19 +38,47 @@ impl FluenceActor {
log::info!("actor::run_randomx: start {params:?}");
rt.validate_immediate_caller_accept_any()?;

let result = fluence_actor_sdk::run_randomx(params.global_nonce, params.local_nonce)
.map_err(|error_num| {
log::error!("actor::run_randomx: run_randomx failed with {error_num}");

let err_msg = format!("run_randomx syscall failed with {error_num}");
ActorError::checked(ExitCode::new(SYSCALL_FAILED_EXIT_CODE), err_msg, None)
})?;
let result = fluence_actor_sdk::run_randomx(&params.global_nonce, &params.local_nonce)
.map_err(randomx_failed)?;
log::info!("actor::run_randomx: result is {result:?}");

let result = RandomXResult { result };
Ok(result)
}

/// Run RandomX with the provided parameters and returns its result hash.
pub fn run_randomx_batched(
rt: &impl Runtime,
params: RandomXArgumentsBatched,
) -> Result<RandomXResultBatched, ActorError> {
use fluence_actor_sdk::TARGET_HASH_SIZE;
use fvm_ipld_encoding::BytesDe;

log::info!("actor::run_randomx: start {params:?}");
rt.validate_immediate_caller_accept_any()?;
let result_len = params.global_nonce.len();

// The result is a vector of hashes, each hash is TARGET_HASH_SIZE bytes.
let result =
fluence_actor_sdk::run_randomx_batched(&params.global_nonce, &params.local_nonce)
.map_err(randomx_failed)?;

log::info!("actor::run_randomx: result is {result:?}");
log::info!("actor::run_randomx: result batch len is {result_len:?}");

let result = result[..result_len * TARGET_HASH_SIZE]
.chunks_exact(TARGET_HASH_SIZE)
.map(|chunk| {
let mut hash_arr: Vec<u8> = vec![0; TARGET_HASH_SIZE];
hash_arr.copy_from_slice(chunk);
BytesDe(hash_arr)
})
.collect();

let result = RandomXResultBatched { result };
Ok(result)
}

/// Fallback method for unimplemented method numbers.
pub fn fallback(
rt: &impl Runtime,
Expand All @@ -74,6 +103,14 @@ impl ActorCode for FluenceActor {

actor_dispatch! {
RunRandomX => run_randomx,
RunRandomXBatched => run_randomx_batched,
_ => fallback,
}
}

fn randomx_failed(error_num: ErrorNumber) -> ActorError {
log::error!("actor::run_randomx: run_randomx failed with {error_num}");

let err_msg = format!("run_randomx syscall failed with {error_num}");
ActorError::checked(ExitCode::new(SYSCALL_FAILED_EXIT_CODE), err_msg, None)
}
13 changes: 13 additions & 0 deletions fendermint/actors/fluence/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use fluence_actor_sdk::TARGET_HASH_SIZE;
use fvm_ipld_encoding::strict_bytes;
use fvm_ipld_encoding::tuple::*;
use fvm_ipld_encoding::BytesDe;

#[derive(Debug, Serialize_tuple, Deserialize_tuple)]
pub struct RandomXArguments {
Expand All @@ -15,3 +16,15 @@ pub struct RandomXResult {
#[serde(with = "strict_bytes")]
pub result: [u8; TARGET_HASH_SIZE],
}

#[derive(Debug, Serialize_tuple, Deserialize_tuple)]
pub struct RandomXArgumentsBatched {
// To be replaced with Vec<Nonce> once we have a proper type in shared for it
pub global_nonce: Vec<BytesDe>,
pub local_nonce: Vec<BytesDe>,
}

#[derive(Debug, Serialize_tuple, Deserialize_tuple)]
pub struct RandomXResultBatched {
pub result: Vec<BytesDe>,
}
1 change: 1 addition & 0 deletions fendermint/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ base64 = { workspace = true }
bytes = { workspace = true }
prost = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
tendermint = { workspace = true }
tendermint-rpc = { workspace = true }
tendermint-proto = { workspace = true }
Expand Down
Loading
Loading