Skip to content

Commit

Permalink
fix execute encoding for transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
StanislavBreadless committed Jan 17, 2025
1 parent 9b121c9 commit 03b05ec
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
Binary file added core/genesis_export.bin
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use zksync_types::{
commitment::{L1BatchWithMetadata, PriorityOpsMerkleProof},
ethabi::{encode, Token},
ProtocolVersionId,
};

use crate::{
Expand All @@ -15,6 +16,49 @@ pub struct ExecuteBatches {
pub priority_ops_proofs: Vec<PriorityOpsMerkleProof>,
}

impl ExecuteBatches {
// The encodings of `ExecuteBatches` operations are different depending on the protocol version
// of the underlying chain.
// However, we can send batches with older protocol versions just by changing the encoding.
// This makes the migration simpler.
pub fn encode_for_eth_tx(self, chain_protocol_version: ProtocolVersionId) -> Vec<Token> {
let internal_protocol_version = self.l1_batches[0].header.protocol_version.unwrap();

if internal_protocol_version.is_pre_gateway() && chain_protocol_version.is_pre_gateway() {
vec![Token::Array(
self.l1_batches
.iter()
.map(|batch| StoredBatchInfo::from(batch).into_token())
.collect(),
)]
} else {
let encoded_data = encode(&[
Token::Array(
self.l1_batches
.iter()
.map(|batch| StoredBatchInfo::from(batch).into_token())
.collect(),
),
Token::Array(
self.priority_ops_proofs
.iter()
.map(|proof| proof.into_token())
.collect(),
),
]);
let execute_data = [[SUPPORTED_ENCODING_VERSION].to_vec(), encoded_data]
.concat()
.to_vec();

vec![
Token::Uint(self.l1_batches[0].header.number.0.into()),
Token::Uint(self.l1_batches.last().unwrap().header.number.0.into()),
Token::Bytes(execute_data),
]
}
}
}

impl Tokenize for &ExecuteBatches {
fn into_tokens(self) -> Vec<Token> {
let protocol_version = self.l1_batches[0].header.protocol_version.unwrap();
Expand Down
16 changes: 12 additions & 4 deletions core/node/eth_sender/src/eth_tx_aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ impl EthTxAggregator {
stm_protocol_version_id,
stm_validator_timelock_address,
),
chain_protocol_version_id,
is_gateway,
)
.await?;
Expand Down Expand Up @@ -642,7 +643,11 @@ impl EthTxAggregator {
.await;
}

fn encode_aggregated_op(&self, op: &AggregatedOperation) -> TxData {
fn encode_aggregated_op(
&self,
op: &AggregatedOperation,
chain_protocol_version_id: ProtocolVersionId,
) -> TxData {
let mut args = vec![Token::Uint(self.rollup_chain_id.as_u64().into())];
let is_op_pre_gateway = op.protocol_version().is_pre_gateway();

Expand Down Expand Up @@ -686,8 +691,9 @@ impl EthTxAggregator {
(calldata, None)
}
AggregatedOperation::Execute(op) => {
args.extend(op.into_tokens());
let encoding_fn = if is_op_pre_gateway {
args.extend(op.encode_for_eth_tx(chain_protocol_version_id));
let encoding_fn = if is_op_pre_gateway && chain_protocol_version_id.is_pre_gateway()
{
&self.functions.post_shared_bridge_execute
} else {
&self.functions.post_gateway_execute
Expand Down Expand Up @@ -743,6 +749,7 @@ impl EthTxAggregator {
storage: &mut Connection<'_, Core>,
aggregated_op: &AggregatedOperation,
timelock_contract_address: Address,
chain_protocol_version_id: ProtocolVersionId,
is_gateway: bool,
) -> Result<EthTx, EthSenderError> {
let mut transaction = storage.start_transaction().await.unwrap();
Expand All @@ -755,7 +762,8 @@ impl EthTxAggregator {
(_, _) => None,
};
let nonce = self.get_next_nonce(&mut transaction, sender_addr).await?;
let encoded_aggregated_op = self.encode_aggregated_op(aggregated_op);
let encoded_aggregated_op =
self.encode_aggregated_op(aggregated_op, chain_protocol_version_id);
let l1_batch_number_range = aggregated_op.l1_batch_range();

let eth_tx_predicted_gas = match (op_type, is_gateway, self.aggregator.mode()) {
Expand Down
3 changes: 2 additions & 1 deletion core/node/eth_sender/src/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use zksync_object_store::MockObjectStore;
use zksync_types::{
aggregated_operations::AggregatedActionType, block::L1BatchHeader,
commitment::L1BatchCommitmentMode, eth_sender::EthTx, pubdata_da::PubdataSendingMode,
settlement::SettlementMode, Address, L1BatchNumber, ProtocolVersion, H256,
settlement::SettlementMode, Address, L1BatchNumber, ProtocolVersion, ProtocolVersionId, H256,
};

use crate::{
Expand Down Expand Up @@ -525,6 +525,7 @@ impl EthSenderTester {
&mut self.conn.connection().await.unwrap(),
&aggregated_operation,
Address::random(),
ProtocolVersionId::latest(),
self.is_l2,
)
.await
Expand Down
1 change: 1 addition & 0 deletions core/node/eth_sender/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ async fn resend_each_block(commitment_mode: L1BatchCommitmentMode) -> anyhow::Re
&mut tester.conn.connection().await.unwrap(),
&get_dummy_operation(0),
Address::random(),
ProtocolVersionId::latest(),
false,
)
.await?;
Expand Down

0 comments on commit 03b05ec

Please sign in to comment.