From 677cfcfa518fc3039302a4a162eac9f45b67e282 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Delabrouille?= <34384633+tdelabro@users.noreply.github.com> Date: Mon, 22 Jan 2024 17:26:01 +0100 Subject: [PATCH] feat: impl TransactionVersion for our tx types (#1391) --- CHANGELOG.md | 1 + crates/primitives/transactions/src/getters.rs | 138 ++++++++++++++---- 2 files changed, 112 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7583841237..9105da8485 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Next release +- feat: types in `mp-transactions` impl a method to get their version - fix: broken class hashes and contracts in genesis - refactor: rename LAST_SYNCED_L1_BLOCK to be more clear - chore: add headers to da calldata, fix eth da in sovereign mode diff --git a/crates/primitives/transactions/src/getters.rs b/crates/primitives/transactions/src/getters.rs index 9880f2aeb6..f2d965f153 100644 --- a/crates/primitives/transactions/src/getters.rs +++ b/crates/primitives/transactions/src/getters.rs @@ -3,6 +3,10 @@ use alloc::vec::Vec; use mp_felt::Felt252Wrapper; use super::{DeclareTransaction, DeployAccountTransaction, InvokeTransaction, Transaction, UserTransaction}; +use crate::{ + DeclareTransactionV0, DeclareTransactionV1, DeclareTransactionV2, HandleL1MessageTransaction, InvokeTransactionV0, + InvokeTransactionV1, UserAndL1HandlerTransaction, +}; impl Transaction { pub fn signature(&self) -> Vec { @@ -56,14 +60,6 @@ impl UserTransaction { } } - pub fn version(&self) -> u8 { - match self { - UserTransaction::Declare(tx, _) => tx.version(), - UserTransaction::DeployAccount(tx) => tx.version(), - UserTransaction::Invoke(tx) => tx.version(), - } - } - pub fn offset_version(&self) -> bool { match self { UserTransaction::Declare(tx, _) => tx.offset_version(), @@ -106,14 +102,6 @@ impl DeclareTransaction { } } - pub fn version(&self) -> u8 { - match self { - DeclareTransaction::V0(_) => 0, - DeclareTransaction::V1(_) => 1, - DeclareTransaction::V2(_) => 2, - } - } - pub fn class_hash(&self) -> &Felt252Wrapper { match self { DeclareTransaction::V0(tx) => &tx.class_hash, @@ -157,10 +145,6 @@ impl DeployAccountTransaction { &self.nonce } - pub fn version(&self) -> u8 { - 1 - } - pub fn account_address(&self) -> Felt252Wrapper { Felt252Wrapper(self.get_account_address()) } @@ -210,13 +194,6 @@ impl InvokeTransaction { } } - pub fn version(&self) -> u8 { - match self { - InvokeTransaction::V0(_) => 0, - InvokeTransaction::V1(_) => 1, - } - } - pub fn offset_version(&self) -> bool { match self { // we don't accept V0 txs from the RPC @@ -225,3 +202,110 @@ impl InvokeTransaction { } } } + +pub trait TransactionVersion { + fn version(&self) -> u8; +} + +impl TransactionVersion for UserTransaction { + #[inline(always)] + fn version(&self) -> u8 { + match self { + UserTransaction::Declare(tx, _) => tx.version(), + UserTransaction::DeployAccount(tx) => tx.version(), + UserTransaction::Invoke(tx) => tx.version(), + } + } +} + +impl TransactionVersion for Transaction { + #[inline(always)] + fn version(&self) -> u8 { + match self { + Transaction::Declare(tx) => tx.version(), + Transaction::DeployAccount(tx) => tx.version(), + Transaction::Invoke(tx) => tx.version(), + Transaction::L1Handler(tx) => tx.version(), + } + } +} + +impl TransactionVersion for UserAndL1HandlerTransaction { + #[inline(always)] + fn version(&self) -> u8 { + match self { + UserAndL1HandlerTransaction::User(tx) => tx.version(), + UserAndL1HandlerTransaction::L1Handler(tx, _) => tx.version(), + } + } +} + +impl TransactionVersion for InvokeTransaction { + #[inline(always)] + fn version(&self) -> u8 { + match self { + InvokeTransaction::V0(tx) => tx.version(), + InvokeTransaction::V1(tx) => tx.version(), + } + } +} + +impl TransactionVersion for InvokeTransactionV0 { + #[inline(always)] + fn version(&self) -> u8 { + 0 + } +} + +impl TransactionVersion for InvokeTransactionV1 { + #[inline(always)] + fn version(&self) -> u8 { + 1 + } +} + +impl TransactionVersion for DeclareTransaction { + #[inline(always)] + fn version(&self) -> u8 { + match self { + DeclareTransaction::V0(tx) => tx.version(), + DeclareTransaction::V1(tx) => tx.version(), + DeclareTransaction::V2(tx) => tx.version(), + } + } +} + +impl TransactionVersion for DeclareTransactionV0 { + #[inline(always)] + fn version(&self) -> u8 { + 0 + } +} + +impl TransactionVersion for DeclareTransactionV1 { + #[inline(always)] + fn version(&self) -> u8 { + 1 + } +} + +impl TransactionVersion for DeclareTransactionV2 { + #[inline(always)] + fn version(&self) -> u8 { + 2 + } +} + +impl TransactionVersion for DeployAccountTransaction { + #[inline(always)] + fn version(&self) -> u8 { + 1 + } +} + +impl TransactionVersion for HandleL1MessageTransaction { + #[inline(always)] + fn version(&self) -> u8 { + 0 + } +}