Skip to content

Commit

Permalink
chore: Add flate2 dependency for compression
Browse files Browse the repository at this point in the history
  • Loading branch information
johntaiko committed Aug 12, 2024
1 parent 77d64ae commit 9122142
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 178 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ secp256k1 = { version = "0.29", default-features = false, features = [
"recovery",
] }
enr = { version = "0.12.1", default-features = false }
flate2 = "1.0"

# for eip-4844
c-kzg = "1.0.0"
Expand Down
11 changes: 11 additions & 0 deletions crates/consensus/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,17 @@ pub enum ConsensusError {
child_gas_limit: u64,
},

/// Error indicating that the child gas limit is over the maximum allowed limit.
///
/// This error occurs when the child gas limit is more than the specified maximum gas limit.
#[error(
"child gas limit {child_gas_limit} is over the maximum allowed limit ({MINIMUM_GAS_LIMIT})"
)]
GasLimitInvalidMaximum {
/// The child gas limit.
child_gas_limit: u64,
},

/// Error when the child gas limit exceeds the maximum allowed decrease.
#[error("child gas_limit {child_gas_limit} max decrease is {parent_gas_limit}/1024")]
GasLimitInvalidDecrease {
Expand Down
3 changes: 3 additions & 0 deletions crates/primitives-traits/src/constants/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ pub const EIP1559_DEFAULT_ELASTICITY_MULTIPLIER: u64 = 2;
/// Minimum gas limit allowed for transactions.
pub const MINIMUM_GAS_LIMIT: u64 = 5000;

/// Maximum gas limit allowed for transactions.
pub const MAXIMUM_GAS_LIMIT: u64 = 0x7fffffffffffffff;

/// Base fee max change denominator for Optimism Mainnet as defined in the Optimism
/// [transaction costs](https://community.optimism.io/docs/developers/build/differences/#transaction-costs) doc.
pub const OP_MAINNET_EIP1559_DEFAULT_BASE_FEE_MAX_CHANGE_DENOMINATOR: u128 = 50;
Expand Down
219 changes: 52 additions & 167 deletions crates/taiko/consensus/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use reth_consensus_common::validation::{
validate_header_gas,
};
use reth_primitives::{
constants::MINIMUM_GAS_LIMIT, BlockWithSenders, Header, SealedBlock, SealedHeader,
constants::MAXIMUM_GAS_LIMIT, BlockWithSenders, Header, SealedBlock, SealedHeader,
EMPTY_OMMER_ROOT_HASH, U256,
};
use std::{sync::Arc, time::SystemTime};
Expand Down Expand Up @@ -52,9 +52,10 @@ impl TaikoBeaconConsensus {
header: &SealedHeader,
_parent: &SealedHeader,
) -> Result<(), ConsensusError> {
// Check if the self gas limit is below the minimum required limit.
if header.gas_limit < MINIMUM_GAS_LIMIT {
return Err(ConsensusError::GasLimitInvalidMinimum { child_gas_limit: header.gas_limit })
if header.gas_limit > MAXIMUM_GAS_LIMIT {
return Err(ConsensusError::GasLimitInvalidMaximum {
child_gas_limit: header.gas_limit,
});
}

Ok(())
Expand All @@ -63,37 +64,70 @@ impl TaikoBeaconConsensus {

impl Consensus for TaikoBeaconConsensus {
fn validate_header(&self, header: &SealedHeader) -> Result<(), ConsensusError> {
// Check if timestamp is in the future. Clock can drift but this can be consensus issue.
let present_timestamp =
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();

if header.timestamp > present_timestamp {
return Err(ConsensusError::TimestampIsInFuture {
timestamp: header.timestamp,
present_timestamp,
});
}
validate_header_gas(header)?;
validate_header_base_fee(header, &self.chain_spec)?;

if !header.is_zero_difficulty() {
return Err(ConsensusError::TheMergeDifficultyIsNotZero);
}

if header.nonce != 0 {
return Err(ConsensusError::TheMergeNonceIsNotZero);
}

if header.ommers_hash != EMPTY_OMMER_ROOT_HASH {
return Err(ConsensusError::TheMergeOmmerRootIsNotEmpty);
}

// Post-merge, the consensus layer is expected to perform checks such that the block
// timestamp is a function of the slot. This is different from pre-merge, where blocks
// are only allowed to be in the future (compared to the system's clock) by a certain
// threshold.
//
// Block validation with respect to the parent should ensure that the block timestamp
// is greater than its parent timestamp.

// validate header extradata for all networks post merge
validate_header_extradata(header)?;

// EIP-4895: Beacon chain push withdrawals as operations
if self.chain_spec.is_shanghai_active_at_timestamp(header.timestamp) &&
header.withdrawals_root.is_none()
if self.chain_spec.is_shanghai_active_at_timestamp(header.timestamp)
&& header.withdrawals_root.is_none()
{
return Err(ConsensusError::WithdrawalsRootMissing)
} else if !self.chain_spec.is_shanghai_active_at_timestamp(header.timestamp) &&
header.withdrawals_root.is_some()
return Err(ConsensusError::WithdrawalsRootMissing);
} else if !self.chain_spec.is_shanghai_active_at_timestamp(header.timestamp)
&& header.withdrawals_root.is_some()
{
return Err(ConsensusError::WithdrawalsRootUnexpected)
return Err(ConsensusError::WithdrawalsRootUnexpected);
}

// Ensures that EIP-4844 fields are valid once cancun is active.
if self.chain_spec.is_cancun_active_at_timestamp(header.timestamp) {
validate_4844_header_standalone(header)?;
} else if header.blob_gas_used.is_some() {
return Err(ConsensusError::BlobGasUsedUnexpected)
return Err(ConsensusError::BlobGasUsedUnexpected);
} else if header.excess_blob_gas.is_some() {
return Err(ConsensusError::ExcessBlobGasUnexpected)
return Err(ConsensusError::ExcessBlobGasUnexpected);
} else if header.parent_beacon_block_root.is_some() {
return Err(ConsensusError::ParentBeaconBlockRootUnexpected)
return Err(ConsensusError::ParentBeaconBlockRootUnexpected);
}

if self.chain_spec.is_prague_active_at_timestamp(header.timestamp) {
if header.requests_root.is_none() {
return Err(ConsensusError::RequestsRootMissing)
return Err(ConsensusError::RequestsRootMissing);
}
} else if header.requests_root.is_some() {
return Err(ConsensusError::RequestsRootUnexpected)
return Err(ConsensusError::RequestsRootUnexpected);
}

Ok(())
Expand Down Expand Up @@ -124,64 +158,9 @@ impl Consensus for TaikoBeaconConsensus {

fn validate_header_with_total_difficulty(
&self,
header: &Header,
total_difficulty: U256,
_header: &Header,
_total_difficulty: U256,
) -> Result<(), ConsensusError> {
let is_post_merge = self
.chain_spec
.fork(Hardfork::Paris)
.active_at_ttd(total_difficulty, header.difficulty);

if is_post_merge {
if !header.is_zero_difficulty() {
return Err(ConsensusError::TheMergeDifficultyIsNotZero)
}

if header.nonce != 0 {
return Err(ConsensusError::TheMergeNonceIsNotZero)
}

if header.ommers_hash != EMPTY_OMMER_ROOT_HASH {
return Err(ConsensusError::TheMergeOmmerRootIsNotEmpty)
}

// Post-merge, the consensus layer is expected to perform checks such that the block
// timestamp is a function of the slot. This is different from pre-merge, where blocks
// are only allowed to be in the future (compared to the system's clock) by a certain
// threshold.
//
// Block validation with respect to the parent should ensure that the block timestamp
// is greater than its parent timestamp.

// validate header extradata for all networks post merge
validate_header_extradata(header)?;

// mixHash is used instead of difficulty inside EVM
// https://eips.ethereum.org/EIPS/eip-4399#using-mixhash-field-instead-of-difficulty
} else {
// TODO Consensus checks for old blocks:
// * difficulty, mix_hash & nonce aka PoW stuff
// low priority as syncing is done in reverse order

// Check if timestamp is in the future. Clock can drift but this can be consensus issue.
let present_timestamp =
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();

if header.exceeds_allowed_future_timestamp(present_timestamp) {
return Err(ConsensusError::TimestampIsInFuture {
timestamp: header.timestamp,
present_timestamp,
})
}

// Goerli and early OP exception:
// * If the network is goerli pre-merge, ignore the extradata check, since we do not
// support clique. Same goes for OP blocks below Bedrock.
if self.chain_spec.chain != Chain::goerli() && !self.chain_spec.is_optimism() {
validate_header_extradata(header)?;
}
}

Ok(())
}

Expand All @@ -208,7 +187,7 @@ fn validate_against_parent_timestamp(
return Err(ConsensusError::TimestampIsInPast {
parent_timestamp: parent.timestamp,
timestamp: header.timestamp,
})
});
}
Ok(())
}
Expand All @@ -221,97 +200,3 @@ fn validate_against_parent_timestamp(
// Ok(())

// }

#[cfg(test)]
mod tests {
use super::*;
use reth_chainspec::ChainSpecBuilder;
use reth_primitives::{proofs, B256};

fn header_with_gas_limit(gas_limit: u64) -> SealedHeader {
let header = Header { gas_limit, ..Default::default() };
header.seal(B256::ZERO)
}

#[test]
fn test_valid_gas_limit_increase() {
let parent = header_with_gas_limit(1024 * 10);
let child = header_with_gas_limit(parent.gas_limit + 5);

assert_eq!(
TaikoBeaconConsensus::new(Arc::new(ChainSpec::default()))
.validate_against_parent_gas_limit(&child, &parent),
Ok(())
);
}

#[test]
fn test_gas_limit_below_minimum() {
let parent = header_with_gas_limit(MINIMUM_GAS_LIMIT);
let child = header_with_gas_limit(MINIMUM_GAS_LIMIT - 1);

assert_eq!(
TaikoBeaconConsensus::new(Arc::new(ChainSpec::default()))
.validate_against_parent_gas_limit(&child, &parent),
Err(ConsensusError::GasLimitInvalidMinimum { child_gas_limit: child.gas_limit })
);
}

#[test]
fn test_invalid_gas_limit_increase_exceeding_limit() {
let parent = header_with_gas_limit(1024 * 10);
let child = header_with_gas_limit(parent.gas_limit + parent.gas_limit / 1024 + 1);

assert_eq!(
TaikoBeaconConsensus::new(Arc::new(ChainSpec::default()))
.validate_against_parent_gas_limit(&child, &parent),
Err(ConsensusError::GasLimitInvalidIncrease {
parent_gas_limit: parent.gas_limit,
child_gas_limit: child.gas_limit,
})
);
}

#[test]
fn test_valid_gas_limit_decrease_within_limit() {
let parent = header_with_gas_limit(1024 * 10);
let child = header_with_gas_limit(parent.gas_limit - 5);

assert_eq!(
TaikoBeaconConsensus::new(Arc::new(ChainSpec::default()))
.validate_against_parent_gas_limit(&child, &parent),
Ok(())
);
}

#[test]
fn test_invalid_gas_limit_decrease_exceeding_limit() {
let parent = header_with_gas_limit(1024 * 10);
let child = header_with_gas_limit(parent.gas_limit - parent.gas_limit / 1024 - 1);

assert_eq!(
TaikoBeaconConsensus::new(Arc::new(ChainSpec::default()))
.validate_against_parent_gas_limit(&child, &parent),
Err(ConsensusError::GasLimitInvalidDecrease {
parent_gas_limit: parent.gas_limit,
child_gas_limit: child.gas_limit,
})
);
}

#[test]
fn shanghai_block_zero_withdrawals() {
// ensures that if shanghai is activated, and we include a block with a withdrawals root,
// that the header is valid
let chain_spec = Arc::new(ChainSpecBuilder::mainnet().shanghai_activated().build());

let header = Header {
base_fee_per_gas: Some(1337u64),
withdrawals_root: Some(proofs::calculate_withdrawals_root(&[])),
..Default::default()
}
.seal_slow();

assert_eq!(TaikoBeaconConsensus::new(chain_spec).validate_header(&header), Ok(()));
}
}
3 changes: 3 additions & 0 deletions crates/taiko/consensus/proposer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ futures-util.workspace = true
tokio = { workspace = true, features = ["sync", "time"] }
tokio-stream.workspace = true
tracing.workspace = true

# misc
flate2.workspace = true
Loading

0 comments on commit 9122142

Please sign in to comment.