-
Notifications
You must be signed in to change notification settings - Fork 99
feat(l2): add validium mode #2365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
4c526a0
64965fe
bf26fdf
f3c2e0f
0e60588
b32f1f7
85fd749
37e106d
a9b8a9e
63be86c
a1cfadd
1fad6a9
e5d06c3
fefaf04
5718c72
698e31d
06a31c1
73d044d
501d964
c686fd1
ddddca3
4a1fede
62df9bc
232c71d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,16 @@ contract OnChainProposer is IOnChainProposer, ReentrancyGuard { | |
/// @dev Used only in dev mode. | ||
address public constant DEV_MODE = address(0xAA); | ||
|
||
/// @notice Indicates whether the contract operates in validium mode. | ||
/// @dev This value is immutable and can only be set during contract deployment. | ||
bool public immutable VALIDIUM; | ||
|
||
/// @notice Constructor to initialize the immutable validium value. | ||
/// @param _validium A boolean indicating if the contract operates in validium mode. | ||
constructor(bool _validium) { | ||
VALIDIUM = _validium; | ||
} | ||
|
||
modifier onlySequencer() { | ||
require( | ||
authorizedSequencerAddresses[msg.sender], | ||
|
@@ -145,10 +155,12 @@ contract OnChainProposer is IOnChainProposer, ReentrancyGuard { | |
blockNumber == lastCommittedBlock + 1, | ||
"OnChainProposer: blockNumber is not the immediate successor of lastCommittedBlock" | ||
); | ||
require( | ||
blockCommitments[blockNumber].commitmentHash == bytes32(0), | ||
"OnChainProposer: block already committed" | ||
); | ||
if (!VALIDIUM) { | ||
require( | ||
blockCommitments[blockNumber].commitmentHash == bytes32(0), | ||
"OnChainProposer: block already committed" | ||
); | ||
} | ||
// Check if commitment is equivalent to blob's KZG commitment. | ||
|
||
if (depositLogs != bytes32(0)) { | ||
|
@@ -165,12 +177,15 @@ contract OnChainProposer is IOnChainProposer, ReentrancyGuard { | |
withdrawalsLogsMerkleRoot | ||
); | ||
} | ||
blockCommitments[blockNumber] = BlockCommitmentInfo( | ||
if (!VALIDIUM) { | ||
blockCommitments[blockNumber] = BlockCommitmentInfo( | ||
commitment, | ||
depositLogs | ||
); | ||
); | ||
emit BlockCommitted(commitment); | ||
} | ||
|
||
lastCommittedBlock = blockNumber; | ||
emit BlockCommitted(commitment); | ||
} | ||
|
||
/// @inheritdoc IOnChainProposer | ||
|
@@ -201,10 +216,12 @@ contract OnChainProposer is IOnChainProposer, ReentrancyGuard { | |
"OnChainProposer: block already verified" | ||
); | ||
|
||
require( | ||
blockCommitments[blockNumber].commitmentHash != bytes32(0), | ||
"OnChainProposer: block not committed" | ||
); | ||
if (!VALIDIUM) { | ||
require( | ||
blockCommitments[blockNumber].commitmentHash != bytes32(0), | ||
"OnChainProposer: block not committed" | ||
); | ||
} | ||
|
||
if (PICOVERIFIER != DEV_MODE) { | ||
// If the verification fails, it will revert. | ||
|
@@ -234,16 +251,18 @@ contract OnChainProposer is IOnChainProposer, ReentrancyGuard { | |
} | ||
|
||
lastVerifiedBlock = blockNumber; | ||
// The first 2 bytes are the number of deposits. | ||
uint16 deposits_amount = uint16( | ||
bytes2(blockCommitments[blockNumber].depositLogs) | ||
); | ||
if (deposits_amount > 0) { | ||
ICommonBridge(BRIDGE).removeDepositLogs(deposits_amount); | ||
} | ||
|
||
// Remove previous block commitment as it is no longer needed. | ||
delete blockCommitments[blockNumber - 1]; | ||
if (!VALIDIUM) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also here, this should still happen in validium mode There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok! I wasn't sure of that if statement but at moment I thought about not doing it in validium because the commitment that we have is zero. Thanks for pointing it out. |
||
// The first 2 bytes are the number of deposits. | ||
uint16 deposits_amount = uint16( | ||
bytes2(blockCommitments[blockNumber].depositLogs) | ||
); | ||
if (deposits_amount > 0) { | ||
ICommonBridge(BRIDGE).removeDepositLogs(deposits_amount); | ||
} | ||
// Remove previous block commitment as it is no longer needed. | ||
delete blockCommitments[blockNumber - 1]; | ||
} | ||
|
||
emit BlockVerified(blockNumber); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ use crate::{ | |
utils::config::{committer::CommitterConfig, errors::ConfigError, eth::EthConfig}, | ||
}; | ||
|
||
use bytes::Bytes; | ||
use ethrex_common::{ | ||
types::{ | ||
blobs_bundle, fake_exponential_checked, BlobsBundle, BlobsBundleError, Block, | ||
|
@@ -39,6 +40,7 @@ pub struct Committer { | |
interval_ms: u64, | ||
arbitrary_base_blob_gas_price: u64, | ||
execution_cache: Arc<ExecutionCache>, | ||
validium: bool, | ||
} | ||
|
||
pub async fn start_l1_committer( | ||
|
@@ -70,6 +72,7 @@ impl Committer { | |
interval_ms: committer_config.interval_ms, | ||
arbitrary_base_blob_gas_price: committer_config.arbitrary_base_blob_gas_price, | ||
execution_cache, | ||
validium: committer_config.validium, | ||
} | ||
} | ||
|
||
|
@@ -159,7 +162,11 @@ impl Committer { | |
&account_updates, | ||
)?; | ||
|
||
let blobs_bundle = self.generate_blobs_bundle(&state_diff)?; | ||
let blobs_bundle = if !self.validium { | ||
self.generate_blobs_bundle(&state_diff)? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The call to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's right, I missed that one. |
||
} else { | ||
BlobsBundle::default() | ||
}; | ||
|
||
let head_block_hash = block_to_commit.hash(); | ||
match self | ||
|
@@ -356,33 +363,28 @@ impl Committer { | |
) -> Result<H256, CommitterError> { | ||
info!("Sending commitment for block {block_number}"); | ||
|
||
let blob_versioned_hashes = blobs_bundle.generate_versioned_hashes(); | ||
let commitment_bytes: Bytes = if !self.validium { | ||
let blob_versioned_hashes = blobs_bundle.generate_versioned_hashes(); | ||
blob_versioned_hashes | ||
.first() | ||
.ok_or(BlobsBundleError::BlobBundleEmptyError) | ||
.map_err(CommitterError::from)? | ||
.as_fixed_bytes() | ||
.to_vec() | ||
.into() | ||
} else { | ||
Bytes::from(vec![0; 32]) // Validium doesn't need to send commitment. | ||
}; | ||
|
||
let calldata_values = vec![ | ||
Value::Uint(U256::from(block_number)), | ||
Value::FixedBytes( | ||
blob_versioned_hashes | ||
.first() | ||
.ok_or(BlobsBundleError::BlobBundleEmptyError) | ||
.map_err(CommitterError::from)? | ||
.as_fixed_bytes() | ||
.to_vec() | ||
.into(), | ||
), | ||
Value::FixedBytes(commitment_bytes), | ||
Value::FixedBytes(withdrawal_logs_merkle_root.0.to_vec().into()), | ||
Value::FixedBytes(deposit_logs_hash.0.to_vec().into()), | ||
]; | ||
|
||
let calldata = encode_calldata(COMMIT_FUNCTION_SIGNATURE, &calldata_values)?; | ||
|
||
let le_bytes = estimate_blob_gas( | ||
&self.eth_client, | ||
self.arbitrary_base_blob_gas_price, | ||
20, // 20% of headroom | ||
) | ||
.await? | ||
.to_le_bytes(); | ||
|
||
let gas_price_per_blob = U256::from_little_endian(&le_bytes); | ||
let gas_price = self | ||
.eth_client | ||
.get_gas_price_with_extra(20) | ||
|
@@ -392,25 +394,58 @@ impl Committer { | |
CommitterError::InternalError("Failed to convert gas_price to a u64".to_owned()) | ||
})?; | ||
|
||
let wrapped_tx = self | ||
.eth_client | ||
.build_eip4844_transaction( | ||
self.on_chain_proposer_address, | ||
self.l1_address, | ||
calldata.into(), | ||
Overrides { | ||
from: Some(self.l1_address), | ||
gas_price_per_blob: Some(gas_price_per_blob), | ||
max_fee_per_gas: Some(gas_price), | ||
max_priority_fee_per_gas: Some(gas_price), | ||
..Default::default() | ||
}, | ||
blobs_bundle, | ||
// Validium: EIP1559 Transaction. | ||
// Rollup: EIP4844 Transaction -> For on-chain Data Availability. | ||
let mut tx = if !self.validium { | ||
let le_bytes = estimate_blob_gas( | ||
&self.eth_client, | ||
self.arbitrary_base_blob_gas_price, | ||
20, // 20% of headroom | ||
) | ||
.await | ||
.map_err(CommitterError::from)?; | ||
.await? | ||
.to_le_bytes(); | ||
|
||
let gas_price_per_blob = U256::from_little_endian(&le_bytes); | ||
|
||
let wrapped_tx = self | ||
.eth_client | ||
.build_eip4844_transaction( | ||
self.on_chain_proposer_address, | ||
self.l1_address, | ||
calldata.into(), | ||
Overrides { | ||
from: Some(self.l1_address), | ||
gas_price_per_blob: Some(gas_price_per_blob), | ||
max_fee_per_gas: Some(gas_price), | ||
max_priority_fee_per_gas: Some(gas_price), | ||
..Default::default() | ||
}, | ||
blobs_bundle, | ||
) | ||
.await | ||
.map_err(CommitterError::from)?; | ||
|
||
WrappedTransaction::EIP4844(wrapped_tx) | ||
} else { | ||
let wrapped_tx = self | ||
.eth_client | ||
.build_eip1559_transaction( | ||
self.on_chain_proposer_address, | ||
self.l1_address, | ||
calldata.into(), | ||
Overrides { | ||
from: Some(self.l1_address), | ||
max_fee_per_gas: Some(gas_price), | ||
max_priority_fee_per_gas: Some(gas_price), | ||
..Default::default() | ||
}, | ||
) | ||
.await | ||
.map_err(CommitterError::from)?; | ||
|
||
WrappedTransaction::EIP1559(wrapped_tx) | ||
}; | ||
|
||
let mut tx = WrappedTransaction::EIP4844(wrapped_tx); | ||
self.eth_client | ||
.set_gas_for_wrapped_tx(&mut tx, self.l1_address) | ||
.await?; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, this event should still be emitted in valiidum mode
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
37e106d