Skip to content

Commit

Permalink
Merge branch 'master' into e2e-devnet
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhailUshakoff committed Oct 2, 2024
2 parents 405b0a4 + 11fdb5b commit 5e0220e
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Node/src/ethereum_l1/execution_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ impl ExecutionLayer {
) -> Result<Self, Error> {
let signer = PrivateKeySigner::from_signing_key(private_key.into());
let wallet = EthereumWallet::from(signer.clone());
let clock = SlotClock::new(0u64, 12u64, 12u64, 32u64);
let clock = SlotClock::new(0u64, 12u64, 12u64, 32u64, 3u64);

let provider = ProviderBuilder::new().on_http(rpc_url.clone());
let l1_chain_id = provider.get_chain_id().await?;
Expand Down
2 changes: 2 additions & 0 deletions Node/src/ethereum_l1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl EthereumL1 {
preconf_registry_expiry_sec: u64,
bls_service: Arc<BLSService>,
l1_chain_id: u64,
l2_slot_duration_sec: u64,
) -> Result<Self, Error> {
let consensus_layer = ConsensusLayer::new(consensus_rpc_url)?;
let genesis_details = consensus_layer.get_genesis_details().await?;
Expand All @@ -46,6 +47,7 @@ impl EthereumL1 {
genesis_details.genesis_time,
slot_duration_sec,
slots_per_epoch,
l2_slot_duration_sec,
));

let execution_layer = ExecutionLayer::new(
Expand Down
66 changes: 59 additions & 7 deletions Node/src/ethereum_l1/slot_clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct SlotClock {
/// The length of each slot.
slot_duration: Duration,
slots_per_epoch: u64,
l2_slot_duration_sec: u64,
}

impl SlotClock {
Expand All @@ -19,6 +20,7 @@ impl SlotClock {
genesis_timestamp_sec: u64,
slot_duration_sec: u64,
slots_per_epoch: u64,
l2_slot_duration_sec: u64,
) -> Self {
tracing::info!(
"SlotClock: genesis_timestamp_sec: {}, genesis_slot: {}",
Expand All @@ -32,6 +34,7 @@ impl SlotClock {
genesis_duration: Duration::from_secs(genesis_timestamp_sec) - slot_duration,
slot_duration,
slots_per_epoch,
l2_slot_duration_sec,
}
}

Expand Down Expand Up @@ -150,6 +153,17 @@ impl SlotClock {
pub fn slot_of_epoch(&self, slot: Slot) -> Slot {
slot % self.slots_per_epoch
}

// 0 based L2 slot number within the current L1 slot
pub fn get_l2_slot_number_within_l1_slot(&self) -> Result<u64, Error> {
let now = SystemTime::now().duration_since(UNIX_EPOCH)?;
let slot_begin = self.start_of(self.get_current_slot()?)?;
Ok(self.which_l2_slot_is_it((now - slot_begin).as_secs()))
}

fn which_l2_slot_is_it(&self, secs_from_l1_slot_begin: u64) -> u64 {
secs_from_l1_slot_begin / self.l2_slot_duration_sec
}
}

#[cfg(test)]
Expand All @@ -159,11 +173,18 @@ mod tests {
use super::*;

const SLOT_DURATION: u64 = 12;
const L2_SLOT_DURATION: u64 = 3;

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

let now = Duration::from_secs(10);
let duration_to_next_slot = slot_clock.duration_to_next_slot_from(now).unwrap();
Expand All @@ -173,7 +194,13 @@ mod tests {
#[test]
fn test_slot_of() {
let genesis_slot = Slot::from(0u64);
let slot_clock = SlotClock::new(genesis_slot, SLOT_DURATION, SLOT_DURATION, 32);
let slot_clock = SlotClock::new(
genesis_slot,
SLOT_DURATION,
SLOT_DURATION,
32,
L2_SLOT_DURATION,
);

let now = Duration::from_secs(25);
let slot = slot_clock.slot_of(now).unwrap();
Expand All @@ -183,7 +210,13 @@ mod tests {
#[test]
fn test_duration_to_slot() {
let genesis_slot = Slot::from(0u64);
let slot_clock = SlotClock::new(genesis_slot, SLOT_DURATION, SLOT_DURATION, 32);
let slot_clock = SlotClock::new(
genesis_slot,
SLOT_DURATION,
SLOT_DURATION,
32,
L2_SLOT_DURATION,
);

let now = Duration::from_secs(10);
let slot = Slot::from(2u64);
Expand All @@ -194,7 +227,13 @@ mod tests {
#[test]
fn test_start_of() {
let genesis_slot = Slot::from(0u64);
let slot_clock = SlotClock::new(genesis_slot, SLOT_DURATION, SLOT_DURATION, 32);
let slot_clock = SlotClock::new(
genesis_slot,
SLOT_DURATION,
SLOT_DURATION,
32,
L2_SLOT_DURATION,
);

let start_of_slot = slot_clock.start_of(Slot::from(3u64)).unwrap();
assert_eq!(start_of_slot, Duration::from_secs(36));
Expand All @@ -203,7 +242,13 @@ mod tests {
#[test]
fn test_get_current_slot() {
let genesis_slot = Slot::from(0u64);
let slot_clock = SlotClock::new(genesis_slot, 1721387493, 12, 32);
let slot_clock = SlotClock::new(
genesis_slot,
1721387493,
SLOT_DURATION,
32,
L2_SLOT_DURATION,
);

let current_slot = slot_clock.get_current_slot().unwrap();
println!("current_slot: {}", current_slot);
Expand All @@ -213,7 +258,13 @@ mod tests {
#[test]
fn test_get_current_epoch() {
let genesis_slot = Slot::from(0u64);
let slot_clock = SlotClock::new(genesis_slot, 1721387493, 12, 32);
let slot_clock = SlotClock::new(
genesis_slot,
1721387493,
SLOT_DURATION,
32,
L2_SLOT_DURATION,
);

let current_epoch = slot_clock.get_current_epoch().unwrap();
assert!(current_epoch > 0);
Expand All @@ -230,6 +281,7 @@ mod tests {
genesis_timestamp,
slot_duration,
slot_per_epoch,
L2_SLOT_DURATION,
);

let epoch_begin_timestamp = slot_clock
Expand All @@ -244,7 +296,7 @@ mod tests {
#[test]
fn test_get_current_slot_of_epoch() {
let genesis_slot = Slot::from(0u64);
let slot_clock = SlotClock::new(genesis_slot, 100, SLOT_DURATION, 32);
let slot_clock = SlotClock::new(genesis_slot, 100, SLOT_DURATION, 32, L2_SLOT_DURATION);

assert_eq!(slot_clock.slot_of_epoch(1234), 18);
assert_eq!(slot_clock.slot_of_epoch(293482), 10);
Expand Down
5 changes: 5 additions & 0 deletions Node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ async fn main() -> Result<(), Error> {
config.preconf_registry_expiry_sec,
bls_service.clone(),
config.l1_chain_id,
config.l2_slot_duration_sec,
)
.await?;

Expand Down Expand Up @@ -133,6 +134,10 @@ fn init_logging() {
.add_directive("hyper=info".parse().unwrap())
.add_directive("alloy_transport=info".parse().unwrap())
.add_directive("alloy_rpc_client=info".parse().unwrap())
.add_directive("p2p_network=info".parse().unwrap())
.add_directive("libp2p_gossipsub=info".parse().unwrap())
.add_directive("discv5=info".parse().unwrap())
.add_directive("netlink_proto=info".parse().unwrap())
});

fmt().with_env_filter(filter).init();
Expand Down
34 changes: 24 additions & 10 deletions Node/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,15 @@ impl Node {
self.preconfirm_block(true).await?;
}
OperatorStatus::None => {
info!("Not my slot to preconfirm: {}", current_slot);
info!(
"Not my slot to preconfirm. Epoch {}, slot: {} ({}), L2 slot: {}",
self.epoch,
current_slot,
self.ethereum_l1.slot_clock.slot_of_epoch(current_slot),
self.ethereum_l1
.slot_clock
.get_l2_slot_number_within_l1_slot()?
);
}
}

Expand Down Expand Up @@ -375,10 +383,14 @@ impl Node {
async fn preconfirm_last_slot(&mut self) -> Result<(), Error> {
debug!("Preconfirming last slot");
self.preconfirm_block(false).await?;
const FINAL_L2_SLOT_PERCONFIRMATION: u64 = 3;
if self
.preconfirmation_helper
.is_last_final_slot_perconfirmation()
.ethereum_l1
.slot_clock
.get_l2_slot_number_within_l1_slot()?
== FINAL_L2_SLOT_PERCONFIRMATION
{
debug!("Last(4th) perconfirmation in the last L1 slot for the preconfer");
// Last(4th) perconfirmation when we are proposer and preconfer
self.is_preconfer_now.store(false, Ordering::Release);

Expand All @@ -399,10 +411,6 @@ impl Node {

preconfirmation_txs.clear();
}
} else {
// Increment perconfirmations count when we are proposer and preconfer
self.preconfirmation_helper
.increment_final_slot_perconfirmation();
}

Ok(())
Expand All @@ -423,10 +431,13 @@ impl Node {
async fn preconfirm_block(&mut self, send_to_contract: bool) -> Result<(), Error> {
let current_slot = self.ethereum_l1.slot_clock.get_current_slot()?;
info!(
"Preconfirming for the epoch: {} and the slot: {} ({})",
"Preconfirming for the epoch: {}, slot: {} ({}), L2 slot: {}",
self.epoch,
current_slot,
self.ethereum_l1.slot_clock.slot_of_epoch(current_slot)
self.ethereum_l1.slot_clock.slot_of_epoch(current_slot),
self.ethereum_l1
.slot_clock
.get_l2_slot_number_within_l1_slot()?
);

let lookahead_params = self.get_lookahead_params().await?;
Expand Down Expand Up @@ -530,7 +541,10 @@ impl Node {
&self,
message: PreconfirmationMessage,
) -> Result<(), Error> {
debug!("Send message to p2p : {:?}", message);
debug!(
"Send message to p2p, tx list hash: {}",
hex::encode(message.tx_list_hash)
);
self.node_to_p2p_tx
.send(message.into())
.await
Expand Down
2 changes: 1 addition & 1 deletion Node/src/node/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ mod tests {
.expect_get_preconfer_address()
.returning(|| PreconferAddress::from([1u8; 20]));
let ethereum_l1 = Arc::new(EthereumL1 {
slot_clock: Arc::new(SlotClock::new(0, 12, 12, 32)),
slot_clock: Arc::new(SlotClock::new(0, 12, 12, 32, 3)),
consensus_layer: ConsensusLayer::new("http://localhost:5052").unwrap(),
execution_layer,
});
Expand Down
15 changes: 1 addition & 14 deletions Node/src/node/preconfirmation_helper.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
pub struct PreconfirmationHelper {
nonce: u64,
final_slot_perconfirmation_count: u8,
}

impl PreconfirmationHelper {
pub fn new() -> Self {
Self {
nonce: 0,
final_slot_perconfirmation_count: 0,
}
Self { nonce: 0 }
}

pub fn init(&mut self, nonce: u64) {
self.nonce = nonce;
self.final_slot_perconfirmation_count = 0;
}

pub fn get_next_nonce(&mut self) -> u64 {
Expand All @@ -25,12 +20,4 @@ impl PreconfirmationHelper {
pub fn increment_nonce(&mut self) {
self.nonce += 1;
}

pub fn increment_final_slot_perconfirmation(&mut self) {
self.final_slot_perconfirmation_count += 1;
}

pub fn is_last_final_slot_perconfirmation(&self) -> bool {
self.final_slot_perconfirmation_count >= 3
}
}
1 change: 1 addition & 0 deletions Node/src/registration/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ mod tests {
60,
bls_service.clone(),
1,
3,
)
.await
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion Node/src/taiko/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Taiko {
}

pub async fn advance_head_to_new_l2_block(&self, tx_lists: Value) -> Result<Value, Error> {
tracing::debug!("Submitting new L2 blocks");
tracing::debug!("Submitting new L2 blocks to the Taiko driver");
let payload = serde_json::json!({
"TxLists": tx_lists,
"gasUsed": 0u64, //TODO remove here and in the driver
Expand Down
6 changes: 3 additions & 3 deletions p2pNode/p2pNetwork/src/peer_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,9 @@ impl PeerManager {

fn connected_and_dialling_peers(&self) -> HashSet<PeerId> {
let mut connected_and_dialling_peers = self.connected_peers.clone();
info!("Connected peers: {:?}", connected_and_dialling_peers.len());
debug!("Connected peers: {:?}", connected_and_dialling_peers.len());
connected_and_dialling_peers.extend(self.dialling_peers.clone());
info!(
debug!(
"Connected and dialling peers: {:?}",
connected_and_dialling_peers.len()
);
Expand Down Expand Up @@ -404,7 +404,7 @@ impl PeerManager {
.take(num_peers as usize)
.map(|(peer_id, _)| *peer_id)
.collect::<Vec<_>>();
info!("Found {:} peers for redialing", best_peers.len());
debug!("Found {:} peers for redialing", best_peers.len());
debug!("Best peers for redial: {:?}", best_peers);
best_peers
}
Expand Down

0 comments on commit 5e0220e

Please sign in to comment.