Skip to content

Commit

Permalink
Adding initial events from fendermint (#723)
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptoAtwill authored Feb 21, 2024
1 parent ab5b29f commit 4d3f890
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions fendermint/app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ fendermint_rpc = { path = "../rpc" }
fendermint_eth_api = { path = "../eth/api" }
fendermint_vm_actor_interface = { path = "../vm/actor_interface" }
fendermint_vm_core = { path = "../vm/core" }
fendermint_vm_event = { path = "../vm/event" }
fendermint_vm_encoding = { path = "../vm/encoding" }
fendermint_vm_genesis = { path = "../vm/genesis" }
fendermint_vm_interpreter = { path = "../vm/interpreter", features = ["bundle"] }
Expand Down
7 changes: 6 additions & 1 deletion fendermint/app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use fendermint_storage::{
Codec, Encode, KVCollection, KVRead, KVReadable, KVStore, KVWritable, KVWrite,
};
use fendermint_vm_core::Timestamp;
use fendermint_vm_event::{emit, EventType};
use fendermint_vm_interpreter::bytes::{
BytesMessageApplyRes, BytesMessageCheckRes, BytesMessageQuery, BytesMessageQueryRes,
};
Expand Down Expand Up @@ -752,7 +753,11 @@ where
.await
.context("end failed")?;

Ok(to_end_block(ret)?)
let r = to_end_block(ret)?;

emit!(EventType::NewBlock, height = request.height);

Ok(r)
}

/// Commit the current state at the current height.
Expand Down
13 changes: 13 additions & 0 deletions fendermint/vm/event/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "fendermint_vm_event"
description = "Defines the event type emitted in fendermint"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
license.workspace = true
license-file.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
strum = { worspace = true }
21 changes: 21 additions & 0 deletions fendermint/vm/event/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2022-2024 Protocol Labs
// SPDX-License-Identifier: Apache-2.0, MIT

#[derive(strum::Display)]
pub enum EventType {
NewParentView,
ParentFinalityCommitted,
NewBottomUpCheckpoint,
/// A new block is produced in fendermint
NewBlock,
}

#[macro_export]
macro_rules! emit {
($event:expr, $($arg:tt)*) => {
tracing::info!(event = tracing::field::display($event), $($arg)+)
};
($event:expr) => {
tracing::info!(event = tracing::field::display($event))
};
}
1 change: 1 addition & 0 deletions fendermint/vm/interpreter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ license.workspace = true
[dependencies]
fendermint_vm_actor_interface = { path = "../actor_interface" }
fendermint_vm_core = { path = "../core" }
fendermint_vm_event = { path = "../event" }
fendermint_vm_encoding = { path = "../encoding" }
fendermint_vm_genesis = { path = "../genesis" }
fendermint_vm_message = { path = "../message" }
Expand Down
12 changes: 12 additions & 0 deletions fendermint/vm/interpreter/src/fvm/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ use super::{
ValidatorContext,
};

use fendermint_vm_event::{emit, EventType};

/// Validator voting power snapshot.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PowerTable(pub Vec<Validator<Power>>);
Expand Down Expand Up @@ -89,6 +91,8 @@ where
*circ_supply -= burnt_tokens;
});

let num_msgs = msgs.len();

// Construct checkpoint.
let checkpoint = BottomUpCheckpoint {
subnet_id,
Expand Down Expand Up @@ -116,6 +120,14 @@ where
power_diff(curr_power_table, next_power_table)
};

emit!(
EventType::NewBottomUpCheckpoint,
height = height.value(),
block_hash = hex::encode(block_hash),
msgs = num_msgs,
next_configuration_number,
);

Ok(Some((checkpoint, power_updates)))
}

Expand Down
1 change: 1 addition & 0 deletions fendermint/vm/topdown/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ tokio = { workspace = true }
tracing = { workspace = true }

fendermint_vm_genesis = { path = "../genesis" }
fendermint_vm_event = { path = "../event" }

[dev-dependencies]
arbitrary = { workspace = true }
Expand Down
21 changes: 20 additions & 1 deletion fendermint/vm/topdown/src/finality/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::finality::{
};
use crate::{BlockHash, BlockHeight, Config, Error, IPCParentFinality, SequentialKeyCache};
use async_stm::{abort, atomically, Stm, StmResult, TVar};
use fendermint_vm_event::{emit, EventType};
use ipc_api::cross::IpcEnvelope;
use ipc_api::staking::StakingChangeRequest;
use std::cmp::min;
Expand Down Expand Up @@ -72,8 +73,15 @@ impl FinalityWithNull {
maybe_payload: Option<ParentViewPayload>,
) -> StmResult<(), Error> {
if let Some((block_hash, validator_changes, top_down_msgs)) = maybe_payload {
emit!(
EventType::NewParentView,
is_null = false,
height,
block_hash = hex::encode(&block_hash)
);
self.parent_block_filled(height, block_hash, validator_changes, top_down_msgs)
} else {
emit!(EventType::NewParentView, is_null = true, height);
self.parent_null_round(height)
}
}
Expand Down Expand Up @@ -116,7 +124,18 @@ impl FinalityWithNull {
cache
})?;

self.last_committed_finality.write(Some(finality))
let hash = hex::encode(&finality.block_hash);

self.last_committed_finality.write(Some(finality))?;

// emit event only after successful write
emit!(
EventType::ParentFinalityCommitted,
height,
block_hash = hash
);

Ok(())
}
}

Expand Down

0 comments on commit 4d3f890

Please sign in to comment.