Skip to content

Commit

Permalink
feat: emit more evm events via sol facade (#536)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderpick authored Feb 18, 2025
1 parent 0318b3b commit 571bca5
Show file tree
Hide file tree
Showing 24 changed files with 1,742 additions and 1,246 deletions.
675 changes: 565 additions & 110 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ fil_actor_eam = { path = "builtin-actors/actors/eam" }
fil_actor_adm = { path = "builtin-actors/actors/adm" }
fil_actors_runtime = { path = "builtin-actors/runtime" }

# Recall Solidity bindings
recall_sol_facade = { path = "recall-contracts/crates/facade" }

# Using the same tendermint-rs dependency as tower-abci. From both we are interested in v037 modules.
tower-abci = { version = "0.7" }
tower = { version = "0.4" }
Expand Down
2 changes: 1 addition & 1 deletion builtin-actors
7 changes: 4 additions & 3 deletions fendermint/actors/blob_reader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ version = "0.1.0"
crate-type = ["cdylib", "lib"]

[dependencies]
log = { workspace = true, features = ["std"] }
num-traits = { workspace = true }
fil_actors_runtime = { workspace = true }
fvm_ipld_encoding = { workspace = true }
fvm_shared = { workspace = true }
frc42_dispatch = { workspace = true }
log = { workspace = true, features = ["std"] }
num-traits = { workspace = true }
num-derive = { workspace = true }
sha2 = { workspace = true }
recall_sol_facade = { workspace = true, features = ["blob-reader"] }
serde = { workspace = true, features = ["derive"] }

fendermint_actor_blobs_shared = { path = "../blobs/shared" }
fendermint_actor_machine = { path = "../machine" }

[dev-dependencies]
fil_actors_evm_shared = { workspace = true }
Expand Down
71 changes: 62 additions & 9 deletions fendermint/actors/blob_reader/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
// SPDX-License-Identifier: Apache-2.0, MIT

use fendermint_actor_blobs_shared::state::Hash;
use fendermint_actor_machine::events::emit_evm_event;
use fil_actors_runtime::{
actor_dispatch, actor_error,
runtime::{ActorCode, Runtime},
ActorError, FIRST_EXPORTED_METHOD_NUMBER, SYSTEM_ACTOR_ADDR,
};
use fvm_ipld_encoding::ipld_block::IpldBlock;
use fvm_shared::address::Address;
use fvm_shared::MethodNum;
use fvm_shared::{address::Address, MethodNum};
use recall_sol_facade::blob_reader::{
read_request_closed, read_request_opened, read_request_pending,
};

use crate::shared::{
CloseReadRequestParams, GetOpenReadRequestsParams, GetReadRequestStatusParams, Method,
Expand All @@ -37,15 +40,30 @@ impl ReadReqActor {
params: OpenReadRequestParams,
) -> Result<Hash, ActorError> {
rt.validate_immediate_caller_accept_any()?;
rt.transaction(|st: &mut State, _rt| {

let id = rt.transaction(|st: &mut State, _rt| {
st.open_read_request(
params.hash,
params.offset,
params.len,
params.callback_addr,
params.callback_method,
)
})
})?;

emit_evm_event(
rt,
read_request_opened(
&id.0,
&params.hash.0,
params.offset as u64,
params.len as u64,
params.callback_addr,
params.callback_method,
),
)?;

Ok(id)
}

fn get_open_read_requests(
Expand All @@ -70,15 +88,21 @@ impl ReadReqActor {
params: CloseReadRequestParams,
) -> Result<(), ActorError> {
rt.validate_immediate_caller_is(std::iter::once(&SYSTEM_ACTOR_ADDR))?;
rt.transaction(|st: &mut State, _| st.close_read_request(params.0))

rt.transaction(|st: &mut State, _| st.close_read_request(params.0))?;

emit_evm_event(rt, read_request_closed(&params.0 .0))
}

fn set_read_request_pending(
rt: &impl Runtime,
params: SetReadRequestPendingParams,
) -> Result<(), ActorError> {
rt.validate_immediate_caller_is(std::iter::once(&SYSTEM_ACTOR_ADDR))?;
rt.transaction(|st: &mut State, _| st.set_read_request_pending(params.0))

rt.transaction(|st: &mut State, _| st.set_read_request_pending(params.0))?;

emit_evm_event(rt, read_request_pending(&params.0 .0))
}

/// Fallback method for unimplemented method numbers.
Expand Down Expand Up @@ -118,13 +142,12 @@ impl ActorCode for ReadReqActor {
mod tests {
use super::*;

use fendermint_actor_machine::events::to_actor_event;
use fil_actors_evm_shared::address::EthAddress;
use fil_actors_runtime::test_utils::{
expect_empty, MockRuntime, ETHACCOUNT_ACTOR_CODE_ID, SYSTEM_ACTOR_CODE_ID,
};
use fil_actors_runtime::SYSTEM_ACTOR_ADDR;
use fvm_ipld_encoding::ipld_block::IpldBlock;
use fvm_shared::address::Address;
use rand::RngCore;

pub fn new_hash(size: usize) -> (Hash, u64) {
Expand Down Expand Up @@ -153,6 +176,32 @@ mod tests {
rt
}

fn expect_emitted_open_event(rt: &MockRuntime, params: &OpenReadRequestParams, id: &Hash) {
let event = to_actor_event(
read_request_opened(
&id.0,
&params.hash.0,
params.offset as u64,
params.len as u64,
params.callback_addr,
params.callback_method,
)
.unwrap(),
)
.unwrap();
rt.expect_emitted_event(event);
}

fn expect_emitted_pending_event(rt: &MockRuntime, params: &SetReadRequestPendingParams) {
let event = to_actor_event(read_request_pending(&params.0 .0).unwrap()).unwrap();
rt.expect_emitted_event(event);
}

fn expect_emitted_closed_event(rt: &MockRuntime, params: &CloseReadRequestParams) {
let event = to_actor_event(read_request_closed(&params.0 .0).unwrap()).unwrap();
rt.expect_emitted_event(event);
}

#[test]
fn test_read_request_operations() {
let rt = construct_and_verify();
Expand Down Expand Up @@ -183,6 +232,8 @@ mod tests {
callback_addr: f4_eth_addr,
callback_method,
};
let expected_id = Hash::from(1);
expect_emitted_open_event(&rt, &open_params, &expected_id);
let request_id = rt
.call::<ReadReqActor>(
Method::OpenReadRequest as u64,
Expand Down Expand Up @@ -237,6 +288,7 @@ mod tests {
rt.set_caller(*SYSTEM_ACTOR_CODE_ID, SYSTEM_ACTOR_ADDR);
rt.expect_validate_caller_addr(vec![SYSTEM_ACTOR_ADDR]);
let pending_params = SetReadRequestPendingParams(request_id);
expect_emitted_pending_event(&rt, &pending_params);
let result = rt.call::<ReadReqActor>(
Method::SetReadRequestPending as u64,
IpldBlock::serialize_cbor(&pending_params).unwrap(),
Expand Down Expand Up @@ -264,6 +316,7 @@ mod tests {
rt.set_caller(*SYSTEM_ACTOR_CODE_ID, SYSTEM_ACTOR_ADDR);
rt.expect_validate_caller_addr(vec![SYSTEM_ACTOR_ADDR]);
let close_params = CloseReadRequestParams(request_id);
expect_emitted_closed_event(&rt, &close_params);
let result = rt.call::<ReadReqActor>(
Method::CloseReadRequest as u64,
IpldBlock::serialize_cbor(&close_params).unwrap(),
Expand Down Expand Up @@ -313,7 +366,7 @@ mod tests {
assert!(result.is_err());
rt.verify();

// Test closing request with with non system caller
// Test closing request with the non-system caller
rt.set_caller(*ETHACCOUNT_ACTOR_CODE_ID, id_addr);
rt.expect_validate_caller_addr(vec![SYSTEM_ACTOR_ADDR]);
let result = rt.call::<ReadReqActor>(
Expand Down
2 changes: 1 addition & 1 deletion fendermint/actors/blob_reader/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

use std::collections::HashMap;

use fendermint_actor_blobs_shared::state::Hash;
use fil_actors_runtime::ActorError;
use fvm_ipld_encoding::tuple::*;
use fvm_shared::{address::Address, MethodNum};
use log::info;

use crate::shared::{ReadRequest, ReadRequestStatus};
use fendermint_actor_blobs_shared::state::Hash;

const MAX_READ_REQUEST_LEN: u32 = 1024 * 1024; // 1MB

Expand Down
5 changes: 3 additions & 2 deletions fendermint/actors/blobs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ crate-type = ["cdylib", "lib"]

[dependencies]
anyhow = { workspace = true }
log = { workspace = true, features = ["std"] }
num-traits = { workspace = true }
fil_actors_runtime = { workspace = true }
fvm_ipld_blockstore = { workspace = true }
fvm_ipld_encoding = { workspace = true }
fvm_shared = { workspace = true }
log = { workspace = true, features = ["std"] }
num-traits = { workspace = true }
recall_sol_facade = { workspace = true, features = ["blobs", "credit", "gas"] }
serde = { workspace = true, features = ["derive"] }

fendermint_actor_recall_config_shared = { path = "../recall_config/shared" }
Expand Down
4 changes: 4 additions & 0 deletions fendermint/actors/blobs/shared/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ impl TokenCreditRate {
pub fn from(rate: impl Into<BigInt>) -> Self {
Self { rate: rate.into() }
}

pub fn rate(&self) -> &BigInt {
&self.rate
}
}

impl fmt::Display for TokenCreditRate {
Expand Down
Loading

0 comments on commit 571bca5

Please sign in to comment.