Skip to content

Commit

Permalink
feat: delta hardfork (#198)
Browse files Browse the repository at this point in the history
* basic span batch decoding

* refactor: move span batch type into separate module

* span batch status checks

* output BlockInput in batch stage

* take BlockInput in attributes stage

* fix tests

* fix tests

* remove unstable code

* filter overlapping block inputs

* clippy

* cleanup

* fix clippy in service

* fix transaction decoding

* remove empty block creation for testing

* revert changes

* debug

* fix batch origin calculation

* better logging

* logging

* logging

* show batch future or undecided

* confrm error

* cleanup for testing

* fix batch origin checks

* fix prev l2 block calculation

* fix prev block fetch

* fix l2_info tracking

* fix epoch checks

* fix

* fix overlapped checks

* fix origin bit handling in block input derivation

* better logging

* start sync earlier

* reorder span batch status checks for better logging

* prefetch l2 refs

* add overlap epoch check

* fmt

* fix batch status check ordering
  • Loading branch information
ncitron authored Jan 14, 2024
1 parent 2665fe7 commit d25b253
Show file tree
Hide file tree
Showing 12 changed files with 938 additions and 110 deletions.
29 changes: 24 additions & 5 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl FromStr for SyncMode {
}

/// A system configuration
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Clone, Deserialize, Default)]
pub struct Config {
/// The base chain RPC URL
pub l1_rpc_url: String,
Expand Down Expand Up @@ -143,13 +143,21 @@ pub struct ChainConfig {
pub regolith_time: u64,
/// Timestamp of the canyon hardfork
pub canyon_time: u64,
/// Timestamp of the delta hardfork
pub delta_time: u64,
/// Network blocktime
#[serde(default = "default_blocktime")]
pub blocktime: u64,
/// L2 To L1 Message passer address
pub l2_to_l1_message_passer: Address,
}

impl Default for ChainConfig {
fn default() -> Self {
ChainConfig::optimism()
}
}

/// Optimism system config contract values
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct SystemConfig {
Expand Down Expand Up @@ -255,6 +263,7 @@ impl ChainConfig {
blocktime: 2,
regolith_time: 0,
canyon_time: 170499240,
delta_time: u64::MAX,
}
}

Expand Down Expand Up @@ -293,6 +302,7 @@ impl ChainConfig {
max_seq_drift: 600,
regolith_time: 1679079600,
canyon_time: 1699981200,
delta_time: 1703116800,
blocktime: 2,
}
}
Expand Down Expand Up @@ -331,6 +341,7 @@ impl ChainConfig {
max_seq_drift: 600,
regolith_time: 0,
canyon_time: 1699981200,
delta_time: 1703203200,
blocktime: 2,
}
}
Expand Down Expand Up @@ -369,6 +380,7 @@ impl ChainConfig {
blocktime: 2,
regolith_time: 0,
canyon_time: 1704992401,
delta_time: u64::MAX,
}
}

Expand Down Expand Up @@ -405,6 +417,7 @@ impl ChainConfig {
max_seq_drift: 600,
regolith_time: 1683219600,
canyon_time: 1699981200,
delta_time: 1703116800,
blocktime: 2,
}
}
Expand Down Expand Up @@ -442,6 +455,7 @@ impl ChainConfig {
max_seq_drift: 600,
regolith_time: 0,
canyon_time: 1699981200,
delta_time: 1703203200,
blocktime: 2,
}
}
Expand Down Expand Up @@ -485,6 +499,7 @@ pub struct ExternalChainConfig {
l2_chain_id: u64,
regolith_time: u64,
canyon_time: u64,
delta_time: u64,
batch_inbox_address: Address,
deposit_contract_address: Address,
l1_system_config_address: Address,
Expand Down Expand Up @@ -547,6 +562,7 @@ impl From<ExternalChainConfig> for ChainConfig {
max_seq_drift: external.max_sequencer_drift,
regolith_time: external.regolith_time,
canyon_time: external.canyon_time,
delta_time: external.delta_time,
blocktime: external.block_time,
l2_to_l1_message_passer: addr("0x4200000000000000000000000000000000000016"),
}
Expand Down Expand Up @@ -593,6 +609,7 @@ impl From<ChainConfig> for ExternalChainConfig {
l2_chain_id: chain_config.l2_chain_id,
regolith_time: chain_config.regolith_time,
canyon_time: chain_config.canyon_time,
delta_time: chain_config.delta_time,
batch_inbox_address: chain_config.batch_inbox,
deposit_contract_address: chain_config.deposit_contract,
l1_system_config_address: chain_config.system_config_contract,
Expand Down Expand Up @@ -718,8 +735,9 @@ mod test {
"channel_timeout": 120,
"l1_chain_id": 900,
"l2_chain_id": 901,
"regolith_time": 0,
"canyon_time": 0,
"regolith_time": 1,
"canyon_time": 2,
"delta_time": 3,
"batch_inbox_address": "0xff00000000000000000000000000000000000000",
"deposit_contract_address": "0x6900000000000000000000000000000000000001",
"l1_system_config_address": "0x6900000000000000000000000000000000000009"
Expand Down Expand Up @@ -765,8 +783,9 @@ mod test {
assert_eq!(chain.channel_timeout, 120);
assert_eq!(chain.seq_window_size, 200);
assert_eq!(chain.max_seq_drift, 300);
assert_eq!(chain.regolith_time, 0);
assert_eq!(chain.canyon_time, 0);
assert_eq!(chain.regolith_time, 1);
assert_eq!(chain.canyon_time, 2);
assert_eq!(chain.delta_time, 3);
assert_eq!(chain.blocktime, 2);
assert_eq!(
chain.l2_to_l1_message_passer,
Expand Down
15 changes: 10 additions & 5 deletions src/derive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,16 @@ mod tests {

chain_watcher.start().unwrap();

let state = Arc::new(RwLock::new(State::new(
config.chain.l2_genesis,
config.chain.l1_start_epoch,
config.clone(),
)));
let provider = Provider::try_from(env::var("L2_TEST_RPC_URL").unwrap()).unwrap();
let state = Arc::new(RwLock::new(
State::new(
config.chain.l2_genesis,
config.chain.l1_start_epoch,
&provider,
config.clone(),
)
.await,
));

let mut pipeline = Pipeline::new(state.clone(), config.clone(), 0).unwrap();

Expand Down
48 changes: 24 additions & 24 deletions src/derive/stages/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use crate::derive::PurgeableIterator;
use crate::engine::PayloadAttributes;
use crate::l1::L1Info;

use super::batches::Batch;
use super::block_input::BlockInput;

pub struct Attributes {
batch_iter: Box<dyn PurgeableIterator<Item = Batch>>,
block_input_iter: Box<dyn PurgeableIterator<Item = BlockInput<u64>>>,
state: Arc<RwLock<State>>,
sequence_number: u64,
epoch_hash: H256,
Expand All @@ -27,64 +27,60 @@ impl Iterator for Attributes {
type Item = PayloadAttributes;

fn next(&mut self) -> Option<Self::Item> {
self.batch_iter
self.block_input_iter
.next()
.map(|input| input.with_full_epoch(&self.state).unwrap())
.map(|batch| self.derive_attributes(batch))
}
}

impl PurgeableIterator for Attributes {
fn purge(&mut self) {
self.batch_iter.purge();
self.block_input_iter.purge();
self.sequence_number = 0;
self.epoch_hash = self.state.read().unwrap().safe_epoch.hash;
}
}

impl Attributes {
pub fn new(
batch_iter: Box<dyn PurgeableIterator<Item = Batch>>,
block_input_iter: Box<dyn PurgeableIterator<Item = BlockInput<u64>>>,
state: Arc<RwLock<State>>,
config: Arc<Config>,
seq: u64,
) -> Self {
let epoch_hash = state.read().unwrap().safe_epoch.hash;

Self {
batch_iter,
block_input_iter,
state,
sequence_number: seq,
epoch_hash,
config,
}
}

fn derive_attributes(&mut self, batch: Batch) -> PayloadAttributes {
tracing::debug!("attributes derived from block {}", batch.epoch_num);
tracing::debug!("batch epoch hash {:?}", batch.epoch_hash);
fn derive_attributes(&mut self, input: BlockInput<Epoch>) -> PayloadAttributes {
tracing::debug!("attributes derived from block {}", input.epoch.number);
tracing::debug!("batch epoch hash {:?}", input.epoch.hash);

self.update_sequence_number(batch.epoch_hash);
self.update_sequence_number(input.epoch.hash);

let state = self.state.read().unwrap();
let l1_info = state.l1_info_by_hash(batch.epoch_hash).unwrap();
let l1_info = state.l1_info_by_hash(input.epoch.hash).unwrap();

let epoch = Some(Epoch {
number: batch.epoch_num,
hash: batch.epoch_hash,
timestamp: l1_info.block_info.timestamp,
});

let withdrawals = if batch.timestamp >= self.config.chain.canyon_time {
let withdrawals = if input.timestamp >= self.config.chain.canyon_time {
Some(Vec::new())
} else {
None
};

let timestamp = U64([batch.timestamp]);
let l1_inclusion_block = Some(batch.l1_inclusion_block);
let timestamp = U64([input.timestamp]);
let l1_inclusion_block = Some(input.l1_inclusion_block);
let seq_number = Some(self.sequence_number);
let prev_randao = l1_info.block_info.mix_hash;
let transactions = Some(self.derive_transactions(batch, l1_info));
let epoch = Some(input.epoch);
let transactions = Some(self.derive_transactions(input, l1_info));
let suggested_fee_recipient = SystemAccounts::default().fee_vault;

PayloadAttributes {
Expand All @@ -101,18 +97,22 @@ impl Attributes {
}
}

fn derive_transactions(&self, batch: Batch, l1_info: &L1Info) -> Vec<RawTransaction> {
fn derive_transactions(
&self,
input: BlockInput<Epoch>,
l1_info: &L1Info,
) -> Vec<RawTransaction> {
let mut transactions = Vec::new();

let attributes_tx = self.derive_attributes_deposited(l1_info, batch.timestamp);
let attributes_tx = self.derive_attributes_deposited(l1_info, input.timestamp);
transactions.push(attributes_tx);

if self.sequence_number == 0 {
let mut user_deposited_txs = self.derive_user_deposited();
transactions.append(&mut user_deposited_txs);
}

let mut rest = batch.transactions;
let mut rest = input.transactions;
transactions.append(&mut rest);

transactions
Expand Down
Loading

0 comments on commit d25b253

Please sign in to comment.