Skip to content
Merged
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
2 changes: 2 additions & 0 deletions Cargo.lock

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

22 changes: 17 additions & 5 deletions ethexe/common/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ impl Mock<H256> for SimpleBlockData {
fn mock(parent_hash: H256) -> Self {
SimpleBlockData {
hash: H256::random(),
header: BlockHeader {
height: 43,
timestamp: 120,
parent_hash,
},
header: BlockHeader::mock(parent_hash),
}
}
}
Expand All @@ -60,6 +56,22 @@ impl Mock<()> for SimpleBlockData {
}
}

impl Mock<H256> for BlockHeader {
fn mock(parent_hash: H256) -> Self {
Self {
height: 43,
timestamp: 120,
parent_hash,
}
}
}

impl Mock<()> for BlockHeader {
fn mock(_args: ()) -> Self {
Self::mock(H256::random())
}
}

impl Mock<()> for ProtocolTimelines {
fn mock(_args: ()) -> Self {
Self {
Expand Down
2 changes: 2 additions & 0 deletions ethexe/consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ rand_chacha = "0.3"
roast-secp256k1-evm.workspace = true
hashbrown.workspace = true
lru.workspace = true
metrics.workspace = true
metrics-derive.workspace = true
gear-workspace-hack.workspace = true

[dev-dependencies]
Expand Down
39 changes: 29 additions & 10 deletions ethexe/consensus/src/validator/coordinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
use anyhow::{Result, anyhow, ensure};
use derive_more::Display;
use ethexe_common::{
Address, ToDigest, ValidatorsVec, consensus::BatchCommitmentValidationRequest,
Address, SimpleBlockData, ToDigest, ValidatorsVec, consensus::BatchCommitmentValidationRequest,
gear::BatchCommitment, network::ValidatorMessage,
};
use futures::FutureExt;
Expand Down Expand Up @@ -84,7 +84,9 @@ impl Coordinator {
mut ctx: ValidatorContext,
validators: ValidatorsVec,
batch: BatchCommitment,
block: SimpleBlockData,
) -> Result<ValidatorState> {
debug_assert_eq!(batch.block_hash, block.hash, "Block hash mismatch");
ensure!(
validators.len() as u64 >= ctx.core.signatures_threshold,
"Number of validators is less than threshold"
Expand All @@ -102,6 +104,11 @@ impl Coordinator {
ctx.core.pub_key,
)?;

ctx.core
.metrics
.last_signed_commitment_block_number
.set(block.header.height);

if multisigned_batch.signatures().len() as u64 >= ctx.core.signatures_threshold {
return Self::submission(ctx, multisigned_batch);
}
Expand Down Expand Up @@ -160,7 +167,7 @@ impl Coordinator {
mod tests {
use super::*;
use crate::{mock::*, validator::mock::*};
use ethexe_common::{ToDigest, ValidatorsVec};
use ethexe_common::{ToDigest, ValidatorsVec, mock::*};
use gprimitives::H256;
use nonempty::NonEmpty;

Expand All @@ -175,9 +182,13 @@ mod tests {
.collect::<Vec<_>>()
.try_into()
.unwrap();
let batch = BatchCommitment::default();
let block = SimpleBlockData::mock(());
let batch = BatchCommitment {
block_hash: block.hash,
..Default::default()
};

let coordinator = Coordinator::create(ctx, validators, batch).unwrap();
let coordinator = Coordinator::create(ctx, validators, batch, block).unwrap();
assert!(coordinator.is_coordinator());
coordinator.context().output[0]
.clone()
Expand All @@ -191,10 +202,12 @@ mod tests {
ctx.core.signatures_threshold = 3;
let validators =
NonEmpty::from_vec(keys.iter().take(2).map(|k| k.to_address()).collect()).unwrap();
let batch = BatchCommitment::default();
let mut batch = BatchCommitment::default();
let block = SimpleBlockData::mock(());
batch.block_hash = block.hash;

assert!(
Coordinator::create(ctx, validators.into(), batch).is_err(),
Coordinator::create(ctx, validators.into(), batch, block).is_err(),
"Expected an error, but got Ok"
);
}
Expand All @@ -205,10 +218,12 @@ mod tests {
ctx.core.signatures_threshold = 0;
let validators =
NonEmpty::from_vec(keys.iter().take(1).map(|k| k.to_address()).collect()).unwrap();
let batch = BatchCommitment::default();
let mut batch = BatchCommitment::default();
let block = SimpleBlockData::mock(());
batch.block_hash = block.hash;

assert!(
Coordinator::create(ctx, validators.into(), batch).is_err(),
Coordinator::create(ctx, validators.into(), batch, block).is_err(),
"Expected an error due to zero threshold, but got Ok"
);
}
Expand All @@ -220,7 +235,11 @@ mod tests {
let validators =
NonEmpty::from_vec(keys.iter().take(3).map(|k| k.to_address()).collect()).unwrap();

let batch = BatchCommitment::default();
let block = SimpleBlockData::mock(());
let batch = BatchCommitment {
block_hash: block.hash,
..Default::default()
};
let digest = batch.to_digest();

let reply1 = ctx
Expand All @@ -244,7 +263,7 @@ mod tests {
.signer
.validation_reply(keys[2], ctx.core.router_address, digest);

let mut coordinator = Coordinator::create(ctx, validators.into(), batch).unwrap();
let mut coordinator = Coordinator::create(ctx, validators.into(), batch, block).unwrap();
assert!(coordinator.is_coordinator());
coordinator.context().output[0]
.clone()
Expand Down
5 changes: 4 additions & 1 deletion ethexe/consensus/src/validator/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use crate::{
announces,
utils::{self, CodeNotValidatedError},
validator::tx_pool::InjectedTxPool,
validator::{ValidatorMetrics, tx_pool::InjectedTxPool},
};
use anyhow::{Context as _, Result, anyhow};
use async_trait::async_trait;
Expand Down Expand Up @@ -61,6 +61,8 @@ pub struct ValidatorCore {
pub middleware: MiddlewareWrapper,
#[debug(skip)]
pub injected_pool: InjectedTxPool,
#[debug(skip)]
pub metrics: ValidatorMetrics,

/// Minimum deepness threshold to create chain commitment even if there are no transitions.
pub chain_deepness_threshold: u32,
Expand All @@ -85,6 +87,7 @@ impl Clone for ValidatorCore {
committer: self.committer.clone_boxed(),
middleware: self.middleware.clone(),
injected_pool: self.injected_pool.clone(),
metrics: self.metrics.clone(),
chain_deepness_threshold: self.chain_deepness_threshold,
block_gas_limit: self.block_gas_limit,
commitment_delay_limit: self.commitment_delay_limit,
Expand Down
1 change: 1 addition & 0 deletions ethexe/consensus/src/validator/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ pub fn mock_validator_context() -> (ValidatorContext, Vec<PublicKey>, MockEthere
committer: Box::new(ethereum.clone()),
middleware: MiddlewareWrapper::from_inner(ethereum.clone()),
injected_pool: InjectedTxPool::new(db.clone()),
metrics: ValidatorMetrics::default(),
chain_deepness_threshold: DEFAULT_CHAIN_DEEPNESS_THRESHOLD,
commitment_delay_limit: COMMITMENT_DELAY_LIMIT,
producer_delay: Duration::from_millis(1),
Expand Down
8 changes: 8 additions & 0 deletions ethexe/consensus/src/validator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ impl ValidatorService {
committer: committer.into(),
middleware: MiddlewareWrapper::from_inner(election_provider),
injected_pool: InjectedTxPool::new(db),
metrics: ValidatorMetrics::default(),
chain_deepness_threshold: config.chain_deepness_threshold,
block_gas_limit: config.block_gas_limit,
commitment_delay_limit: config.commitment_delay_limit,
Expand Down Expand Up @@ -579,3 +580,10 @@ impl ValidatorContext {
self.pending_events.push_front(event.into());
}
}

#[derive(Clone, metrics_derive::Metrics)]
#[metrics(scope = "ethexe_consensus")]
struct ValidatorMetrics {
/// The last block number validator signed batch commitment for.
pub last_signed_commitment_block_number: metrics::Gauge,
}
6 changes: 6 additions & 0 deletions ethexe/consensus/src/validator/participant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ impl StateHandler for Participant {
digest,
None,
)?;
self.ctx
.core
.metrics
.last_signed_commitment_block_number
.set(self.block.header.height);

let reply = BatchCommitmentValidationReply { digest, signature };

let era_index = self
Expand Down
2 changes: 1 addition & 1 deletion ethexe/consensus/src/validator/producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl StateHandler for Producer {
State::AggregateBatchCommitment { future } => match future.poll_unpin(cx) {
Poll::Ready(Ok(Some(batch))) => {
tracing::debug!(batch.block_hash = %batch.block_hash, "Batch commitment aggregated, switch to Coordinator");
return Coordinator::create(self.ctx, self.validators, batch)
return Coordinator::create(self.ctx, self.validators, batch, self.block)
.map(|s| (Poll::Ready(()), s));
}
Poll::Ready(Ok(None)) => {
Expand Down
Loading