Skip to content

Commit 470a3ac

Browse files
authored
refactor(l2): use deposit hash as the tx hash for l2 txs (#2562)
**Motivation** Here we want to not process the same deposit to the L2 as two different transactions. **Description** * Change the transaction hash of the `PrivilegedL2Transaction` to the deposit hash (instead of the hash of the entire tx) . The one that is emitted when the deposit is done * In the `l1_watcher` skip transactions that are already on the store. Closes #2552
1 parent 0739937 commit 470a3ac

File tree

3 files changed

+16
-36
lines changed

3 files changed

+16
-36
lines changed

crates/common/types/transaction.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,9 @@ impl Transaction {
12651265
}
12661266

12671267
pub fn compute_hash(&self) -> H256 {
1268+
if let Transaction::PrivilegedL2Transaction(tx) = self {
1269+
return tx.get_deposit_hash().unwrap_or_default();
1270+
}
12681271
keccak_hash::keccak(self.encode_canonical_to_vec())
12691272
}
12701273

crates/l2/sequencer/errors.rs

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ pub enum L1WatcherError {
2525
FailedToRetrieveChainConfig(String),
2626
#[error("L1Watcher failed to get config: {0}")]
2727
FailedToGetConfig(#[from] ConfigError),
28+
#[error("L1Watcher failed to access Store: {0}")]
29+
FailedAccessingStore(#[from] StoreError),
2830
#[error("{0}")]
2931
Custom(String),
3032
}

crates/l2/sequencer/l1_watcher.rs

+11-36
Original file line numberDiff line numberDiff line change
@@ -83,42 +83,10 @@ impl L1Watcher {
8383
continue;
8484
}
8585

86-
let pending_deposit_logs = self.get_pending_deposit_logs().await?;
87-
let _deposit_txs = self
88-
.process_logs(logs, &pending_deposit_logs, store, blockchain)
89-
.await?;
86+
let _deposit_txs = self.process_logs(logs, store, blockchain).await?;
9087
}
9188
}
9289

93-
pub async fn get_pending_deposit_logs(&self) -> Result<Vec<H256>, L1WatcherError> {
94-
let selector = keccak(b"getPendingDepositLogs()")
95-
.as_bytes()
96-
.get(..4)
97-
.ok_or(EthClientError::Custom("Failed to get selector.".to_owned()))?
98-
.to_vec();
99-
100-
Ok(hex::decode(
101-
self.eth_client
102-
.call(
103-
self.address,
104-
Bytes::copy_from_slice(&selector),
105-
Overrides::default(),
106-
)
107-
.await?
108-
.get(2..)
109-
.ok_or(L1WatcherError::FailedToDeserializeLog(
110-
"Not a valid hex string".to_string(),
111-
))?,
112-
)
113-
.map_err(|_| L1WatcherError::FailedToDeserializeLog("Not a valid hex string".to_string()))?
114-
.chunks(32)
115-
.map(H256::from_slice)
116-
.collect::<Vec<H256>>()
117-
.split_at(2) // Two first words are index and length abi encode
118-
.1
119-
.to_vec())
120-
}
121-
12290
pub async fn get_logs(&mut self) -> Result<Vec<RpcLog>, L1WatcherError> {
12391
if self.last_block_fetched.is_zero() {
12492
self.last_block_fetched =
@@ -176,7 +144,6 @@ impl L1Watcher {
176144
pub async fn process_logs(
177145
&self,
178146
logs: Vec<RpcLog>,
179-
pending_deposit_logs: &[H256],
180147
store: &Store,
181148
blockchain: &Blockchain,
182149
) -> Result<Vec<H256>, L1WatcherError> {
@@ -189,7 +156,7 @@ impl L1Watcher {
189156
let value_bytes = mint_value.to_big_endian();
190157
let id_bytes = deposit_id.to_big_endian();
191158
let gas_limit_bytes = gas_limit.to_big_endian();
192-
if !pending_deposit_logs.contains(&keccak(
159+
let deposit_hash = keccak(
193160
[
194161
to_address.as_bytes(),
195162
&value_bytes,
@@ -200,7 +167,15 @@ impl L1Watcher {
200167
keccak(&calldata).as_bytes(),
201168
]
202169
.concat(),
203-
)) {
170+
);
171+
172+
let deposit_already_processed = store
173+
.get_transaction_by_hash(deposit_hash)
174+
.await
175+
.map_err(L1WatcherError::FailedAccessingStore)?
176+
.is_some();
177+
178+
if deposit_already_processed {
204179
warn!("Deposit already processed (to: {recipient:#x}, value: {mint_value}, depositId: {deposit_id}), skipping.");
205180
continue;
206181
}

0 commit comments

Comments
 (0)