Skip to content

Commit

Permalink
Detection of the slot to preconfirm
Browse files Browse the repository at this point in the history
  • Loading branch information
mskrzypkows committed Jul 19, 2024
1 parent 108fff6 commit 3da691a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
11 changes: 10 additions & 1 deletion Node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ async fn main() -> Result<(), Error> {
)
.await?;
let mev_boost = mev_boost::MevBoost::new(&config.mev_boost_url);
let node = node::Node::new(node_rx, avs_p2p_tx, taiko, ethereum_l1, mev_boost).await?;
let node = node::Node::new(
node_rx,
avs_p2p_tx,
taiko,
ethereum_l1,
mev_boost,
config.l2_slot_duration_sec,
config.validator_pubkey,
)
.await?;
node.entrypoint().await?;
Ok(())
}
Expand Down
51 changes: 48 additions & 3 deletions Node/src/node/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use crate::{
ethereum_l1::slot_clock::Epoch, ethereum_l1::EthereumL1, mev_boost::MevBoost, taiko::Taiko,
ethereum_l1::{
slot_clock::{Epoch, Slot},
EthereumL1,
},
mev_boost::MevBoost,
taiko::Taiko,
};
use anyhow::{anyhow as any_err, Error};
use beacon_api_client::ProposerDuty;
Expand All @@ -14,6 +19,10 @@ pub struct Node {
_mev_boost: MevBoost, // temporary unused
epoch: Epoch,
lookahead: Vec<ProposerDuty>,
l2_slot_duration_sec: u64,
validator_pubkey: String,
current_slot_to_preconf: Option<Slot>,
next_slot_to_preconf: Option<Slot>,
}

impl Node {
Expand All @@ -23,6 +32,8 @@ impl Node {
taiko: Taiko,
ethereum_l1: EthereumL1,
mev_boost: MevBoost,
l2_slot_duration_sec: u64,
validator_pubkey: String,
) -> Result<Self, Error> {
Ok(Self {
taiko,
Expand All @@ -33,6 +44,10 @@ impl Node {
_mev_boost: mev_boost,
epoch: Epoch::MAX, // it'll be updated in the first preconfirmation loop
lookahead: vec![],
l2_slot_duration_sec,
validator_pubkey,
current_slot_to_preconf: None,
next_slot_to_preconf: None,
})
}

Expand Down Expand Up @@ -72,7 +87,8 @@ impl Node {
tracing::error!("Failed to execute main block preconfirmation step: {}", err);
}
let elapsed = start_time.elapsed();
let sleep_duration = std::time::Duration::from_secs(4).saturating_sub(elapsed);
let sleep_duration =
std::time::Duration::from_secs(self.l2_slot_duration_sec).saturating_sub(elapsed);
tokio::time::sleep(sleep_duration).await;
}
}
Expand All @@ -86,13 +102,35 @@ impl Node {
current_epoch
);
self.epoch = current_epoch;
self.current_slot_to_preconf = self.next_slot_to_preconf;
self.lookahead = self
.ethereum_l1
.consensus_layer
.get_lookahead(self.epoch + 1)
.await?
.await?;
self.next_slot_to_preconf = self.check_for_the_slot_to_preconf(&self.lookahead);
}

if let Some(slot) = self.current_slot_to_preconf {
if slot == self.ethereum_l1.slot_clock.get_current_slot()? {
self.preconfirm_block().await?;
}
} else {
tracing::debug!(
"Not my slot to preconfirm: {}",
self.ethereum_l1.slot_clock.get_current_slot()?
);
}

Ok(())
}

async fn preconfirm_block(&mut self) -> Result<(), Error> {
tracing::debug!(
"Preconfirming for the slot: {:?}",
self.current_slot_to_preconf
);

let pending_tx_lists = self.taiko.get_pending_l2_tx_lists().await?;
if pending_tx_lists.tx_list_bytes.is_empty() {
return Ok(());
Expand All @@ -115,6 +153,13 @@ impl Node {
Ok(())
}

fn check_for_the_slot_to_preconf(&self, lookahead: &Vec<ProposerDuty>) -> Option<Slot> {
lookahead
.iter()
.find(|duty| duty.public_key.to_string() == self.validator_pubkey)
.map(|duty| duty.slot)
}

fn commit_to_the_tx_lists(&self) {
//TODO: implement
}
Expand Down
30 changes: 29 additions & 1 deletion Node/src/utils/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub struct Config {
pub l1_beacon_url: String,
pub l1_slot_duration_sec: u64,
pub l1_slots_per_epoch: u64,
pub l2_slot_duration_sec: u64,
pub validator_pubkey: String,
}

impl Config {
Expand Down Expand Up @@ -41,6 +43,26 @@ impl Config {
})
.expect("L1_SLOTS_PER_EPOCH must be a number");

let l2_slot_duration_sec = std::env::var("L2_SLOT_DURATION_SEC")
.unwrap_or_else(|_| "3".to_string())
.parse::<u64>()
.map(|val| {
if val == 0 {
panic!("L2_SLOT_DURATION_SEC must be a positive number");
}
val
})
.expect("L2_SLOT_DURATION_SEC must be a number");

const VALIDATOR_PUBKEY: &str = "VALIDATOR_PUBKEY";
let validator_pubkey = std::env::var(VALIDATOR_PUBKEY).unwrap_or_else(|_| {
warn!(
"No validator pubkey found in {} env var, using default",
VALIDATOR_PUBKEY
);
"0x0".to_string()
});

let config = Self {
taiko_proposer_url: std::env::var("TAIKO_PROPOSER_URL")
.unwrap_or_else(|_| "http://127.0.0.1:1234".to_string()),
Expand Down Expand Up @@ -68,6 +90,8 @@ impl Config {
.unwrap_or_else(|_| "http://127.0.0.1:4000".to_string()),
l1_slot_duration_sec,
l1_slots_per_epoch,
l2_slot_duration_sec,
validator_pubkey,
};

info!(
Expand All @@ -80,14 +104,18 @@ New block proposal contract address: {}
Consensus layer URL: {}
L1 slot duration: {}
L1 slots per epoch: {}
L2 slot duration: {}
Validator pubkey: {}
"#,
config.taiko_proposer_url,
config.taiko_driver_url,
config.mev_boost_url,
config.taiko_preconfirming_address,
config.l1_beacon_url,
config.l1_slot_duration_sec,
config.l1_slots_per_epoch
config.l1_slots_per_epoch,
config.l2_slot_duration_sec,
config.validator_pubkey
);

config
Expand Down

0 comments on commit 3da691a

Please sign in to comment.