Skip to content

Commit

Permalink
Fix CI + improve error handling in actor
Browse files Browse the repository at this point in the history
  • Loading branch information
fridrik01 committed Jan 23, 2024
1 parent b37a78e commit 18de5a0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
5 changes: 5 additions & 0 deletions fendermint/actors/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ fn main() -> Result<(), Box<dyn Error>> {
if !result.success() {
return Err("actor build failed".into());
}

// make sure the output dir exists
std::fs::create_dir_all("output")
.expect("failed to create output dir for the actors_bundle.car file");

let dst = Path::new("output/actors_bundle.car");
let mut bundler = Bundler::new(dst);
for (&pkg, id) in ACTORS.iter().zip(1u32..) {
Expand Down
34 changes: 20 additions & 14 deletions fendermint/actors/chainmetadata/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ use fil_actors_runtime::runtime::{ActorCode, Runtime};
use fil_actors_runtime::ActorDowncast;
use fil_actors_runtime::ActorError;
use fil_actors_runtime::Array;
use fvm_shared::clock::ChainEpoch;
use fvm_shared::error::ExitCode;

use crate::shared::BLOCKHASHES_AMT_BITWIDTH;
use crate::{ConstructorParams, Method, State};
use crate::{ConstructorParams, Method, PushBlockParams, State};

#[cfg(feature = "fil-actor")]
fil_actors_runtime::wasm_trampoline!(Actor);
Expand Down Expand Up @@ -42,7 +43,7 @@ impl Actor {
Ok(())
}

fn push_block(rt: &impl Runtime, block: Cid) -> Result<(), ActorError> {
fn push_block(rt: &impl Runtime, params: PushBlockParams) -> Result<(), ActorError> {
rt.validate_immediate_caller_is(std::iter::once(&SYSTEM_ACTOR_ADDR))?;

rt.transaction(|st: &mut State, rt| {
Expand All @@ -56,7 +57,7 @@ impl Actor {

// push the block to the AMT
blockhashes
.set(rt.curr_epoch().try_into().unwrap(), block.to_string())
.set(params.epoch as u64, params.block.to_string())
.unwrap();

// remove the oldest block if the AMT is full
Expand Down Expand Up @@ -87,7 +88,7 @@ impl Actor {
Ok(state.lookback_len)
}

fn block_cid(rt: &impl Runtime, rewind: u64) -> Result<Cid, ActorError> {
fn block_cid(rt: &impl Runtime, epoch: ChainEpoch) -> Result<Option<Cid>, ActorError> {
let st: State = rt.state()?;

// load the blockhashes AMT
Expand All @@ -98,20 +99,25 @@ impl Actor {
)
})?;

let blockhash: &String = blockhashes
.get(blockhashes.count() - rewind - 1)
.unwrap()
.unwrap();
// get the block cid from the AMT, if it does not exist return None
let blockhash: &String = match blockhashes.get(epoch as u64).unwrap() {
Some(v) => v,
None => {
return Ok(None);
}
};

Cid::from_str(blockhash.as_str()).map_err(|_| {
ActorError::unchecked(
// return the blockhash as a cid, or an error if the cid is invalid
match Cid::from_str(blockhash.as_str()) {
Ok(cid) => Ok(Some(cid)),
Err(_) => Err(ActorError::unchecked(
ExitCode::USR_ILLEGAL_STATE,
format!(
"failed to parse cid, hash: {}, rewind: {}",
blockhash, rewind
"failed to parse cid, blockhash: {}, epoch: {}",
blockhash, epoch
),
)
})
)),
}
}
}

Expand Down
8 changes: 7 additions & 1 deletion fendermint/actors/chainmetadata/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use cid::Cid;
use fvm_ipld_encoding::tuple::{Deserialize_tuple, Serialize_tuple};
use fvm_shared::METHOD_CONSTRUCTOR;
use fvm_shared::{clock::ChainEpoch, METHOD_CONSTRUCTOR};
use num_derive::FromPrimitive;

// The state is a stores `blockhashes` in an AMT containing the blockhashes of the
Expand All @@ -28,6 +28,12 @@ pub struct ConstructorParams {
pub lookback_len: u64,
}

#[derive(Default, Debug, Serialize_tuple, Deserialize_tuple)]
pub struct PushBlockParams {
pub epoch: ChainEpoch,
pub block: Cid,
}

#[derive(FromPrimitive)]
#[repr(u64)]
pub enum Method {
Expand Down
8 changes: 6 additions & 2 deletions fendermint/vm/interpreter/src/fvm/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,12 @@ where

// Push the current block hash to the chainmetadata actor
//
let block_cid = fendermint_vm_message::cid(&state.block_hash().unwrap()).unwrap();
let params = fvm_ipld_encoding::RawBytes::serialize(block_cid)?;
let params = fvm_ipld_encoding::RawBytes::serialize(
fendermint_actor_chainmetadata::PushBlockParams {
epoch: height,
block: fendermint_vm_message::cid(&state.block_hash().unwrap()).unwrap(),
},
)?;
let msg = FvmMessage {
from: system::SYSTEM_ACTOR_ADDR,
to: fvm_shared::address::Address::new_id(fendermint_actors::CHAINMETADATA_ACTOR_ID),
Expand Down

0 comments on commit 18de5a0

Please sign in to comment.