Skip to content

Commit

Permalink
Checking of the next preconfer when we're in last slot of the epoch (#…
Browse files Browse the repository at this point in the history
…109)

* Checking of the next preconfer when we're in last slot of the epoch

* unit tests for the operator module
  • Loading branch information
mskrzypkows authored Sep 10, 2024
1 parent 22a555f commit 7c437b3
Show file tree
Hide file tree
Showing 7 changed files with 288 additions and 48 deletions.
84 changes: 84 additions & 0 deletions Node/Cargo.lock

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

2 changes: 2 additions & 0 deletions Node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ tree_hash = "0.8"
blst = "0.3"

[dev-dependencies]
mockall_double = "0.3"
mockall = "0.13"
mockito = "1.4"
36 changes: 36 additions & 0 deletions Node/src/ethereum_l1/el_with_cl_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#[cfg(test)]
mod tests {
use super::super::{
consensus_layer::tests::setup_server, consensus_layer::ConsensusLayer,
execution_layer::ExecutionLayer, execution_layer::PreconfTaskManager,
};
use alloy::node_bindings::Anvil;

#[tokio::test]
async fn test_propose_new_block_with_lookahead() {
let server = setup_server().await;
let cl = ConsensusLayer::new(server.url().as_str()).unwrap();
let _duties = cl.get_lookahead(1).await.unwrap();

let anvil = Anvil::new().try_spawn().unwrap();
let rpc_url: reqwest::Url = anvil.endpoint().parse().unwrap();
let private_key = anvil.keys()[0].clone();
let el = ExecutionLayer::new_from_pk(rpc_url, private_key)
.await
.unwrap();

// TODO:
// There is a bug in the Anvil (anvil 0.2.0) library:
// `Result::unwrap()` on an `Err` value: buffer overrun while deserializing
// check if it's fixed in next version
// let lookahead_params = el
// .get_lookahead_params_for_epoch_using_cl_lookahead(1, &duties)
// .await
// .unwrap();
let lookahead_params = Vec::<PreconfTaskManager::LookaheadSetParam>::new();

el.propose_new_block(0, vec![0; 32], [0; 32], 0, lookahead_params, true)
.await
.unwrap();
}
}
4 changes: 4 additions & 0 deletions Node/src/ethereum_l1/execution_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use beacon_api_client::ProposerDuty;
use ecdsa::SigningKey;
use futures_util::StreamExt;
use k256::Secp256k1;
#[cfg(test)]
use mockall::automock;
use rand_core::{OsRng, RngCore};
use std::str::FromStr;
use std::sync::Arc;
Expand Down Expand Up @@ -116,6 +118,8 @@ pub struct EventPollerLookaheadUpdated(
>,
);

#[cfg_attr(test, allow(dead_code))]
#[cfg_attr(test, automock)]
impl ExecutionLayer {
pub async fn new(
rpc_url: &str,
Expand Down
40 changes: 4 additions & 36 deletions Node/src/ethereum_l1/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
pub mod consensus_layer;
mod el_with_cl_tests;
pub mod execution_layer;
pub mod merkle_proofs;
pub mod slot_clock;

use crate::utils::config::ContractAddresses;
use consensus_layer::ConsensusLayer;
#[cfg_attr(test, double)]
use execution_layer::ExecutionLayer;
#[cfg(test)]
use mockall_double::double;
use slot_clock::SlotClock;
use std::sync::Arc;

Expand Down Expand Up @@ -50,39 +54,3 @@ impl EthereumL1 {
})
}
}

#[cfg(test)]
mod tests {
use super::*;
use alloy::node_bindings::Anvil;
use consensus_layer::tests::setup_server;
use execution_layer::PreconfTaskManager;

#[tokio::test]
async fn test_propose_new_block_with_lookahead() {
let server = setup_server().await;
let cl = ConsensusLayer::new(server.url().as_str()).unwrap();
let _duties = cl.get_lookahead(1).await.unwrap();

let anvil = Anvil::new().try_spawn().unwrap();
let rpc_url: reqwest::Url = anvil.endpoint().parse().unwrap();
let private_key = anvil.keys()[0].clone();
let el = ExecutionLayer::new_from_pk(rpc_url, private_key)
.await
.unwrap();

// TODO:
// There is a bug in the Anvil (anvil 0.2.0) library:
// `Result::unwrap()` on an `Err` value: buffer overrun while deserializing
// check if it's fixed in next version
// let lookahead_params = el
// .get_lookahead_params_for_epoch_using_beacon_lookahead(1, &duties)
// .await
// .unwrap();
let lookahead_params = Vec::<PreconfTaskManager::LookaheadSetParam>::new();

el.propose_new_block(0, vec![0; 32], [0; 32], 0, lookahead_params, true)
.await
.unwrap();
}
}
13 changes: 13 additions & 0 deletions Node/src/ethereum_l1/slot_clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ impl SlotClock {
Ok(slot / self.slots_per_epoch)
}

pub fn get_epoch_duration_secs(&self) -> u64 {
self.slot_duration.as_secs() * self.slots_per_epoch
}

pub fn get_current_slot_of_epoch(&self) -> Result<Slot, Error> {
let now = SystemTime::now().duration_since(UNIX_EPOCH)?;
let cur_slot = self.slot_of(now)?;
Expand Down Expand Up @@ -191,4 +195,13 @@ mod tests {
let current_epoch = slot_clock.get_current_epoch().unwrap();
assert!(current_epoch > 0);
}

#[test]
fn test_get_epoch_duration_secs() {
let genesis_slot = Slot::from(0u64);
let slot_clock = SlotClock::new(genesis_slot, 0, 12, 32);

let epoch_duration = slot_clock.get_epoch_duration_secs();
assert_eq!(epoch_duration, 384); // 12 slots per epoch * 32 seconds per slot
}
}
Loading

0 comments on commit 7c437b3

Please sign in to comment.