Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

feat: impl TransactionVersion for our tx types #1391

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
138 changes: 111 additions & 27 deletions crates/primitives/transactions/src/getters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Felt252Wrapper> {
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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
Expand All @@ -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
}
}