Skip to content

Commit

Permalink
fix parity on legacy txs (#1016)
Browse files Browse the repository at this point in the history
* fix parity on legacy txs

* fix tests

* comment
  • Loading branch information
greged93 authored Apr 25, 2024
1 parent 117199d commit acf1fda
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
6 changes: 3 additions & 3 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ cli:
plugins:
sources:
- id: trunk
ref: v1.4.5
ref: v1.5.0
uri: https://github.com/trunk-io/plugins
runtimes:
enabled:
Expand All @@ -30,8 +30,8 @@ lint:
- shfmt@3.6.0
- taplo@0.8.1
- terrascan@1.19.1
- trivy@0.50.2
- trufflehog@3.71.0
- trivy@0.50.4
- trufflehog@3.74.0
- yamllint@1.35.1
ignore:
- linters: [ALL]
Expand Down
3 changes: 3 additions & 0 deletions src/eth_provider/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ pub enum SignatureError {
/// Thrown when signature is missing.
#[error("missing signature")]
MissingSignature,
/// Thrown when parity is invalid.
#[error("invalid parity")]
InvalidParity,
}

/// Error related to Ethereum data format.
Expand Down
35 changes: 27 additions & 8 deletions src/models/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use reth_primitives::{
AccessList, AccessListItem, Signature, TransactionKind, TransactionSigned, TxEip1559, TxEip2930, TxLegacy, TxType,
U256,
};

use crate::eth_provider::error::{EthApiError, EthereumDataFormatError, SignatureError};
use crate::eth_provider::error::{EthApiError, EthereumDataFormatError, SignatureError, TransactionError};

pub fn rpc_to_primitive_transaction(
rpc_transaction: reth_rpc_types::Transaction,
Expand Down Expand Up @@ -77,12 +78,22 @@ pub fn rpc_to_ec_recovered_transaction(
let signature = transaction.signature.ok_or(SignatureError::MissingSignature)?;
let transaction = rpc_to_primitive_transaction(transaction)?;

let parity = match transaction.tx_type() {
TxType::Legacy => {
// EIP-155: v = {0, 1} + CHAIN_ID * 2 + 35
let chain_id = transaction.chain_id().ok_or(TransactionError::InvalidChainId)?;
let recovery: U256 = U256::from(2) * U256::from(chain_id) + U256::from(35);
signature.v - recovery
}
TxType::Eip1559 | TxType::Eip2930 | TxType::Eip4844 => signature.v,
};

let tx_signed = TransactionSigned::from_transaction_and_signature(
transaction,
Signature {
r: signature.r,
s: signature.s,
odd_y_parity: signature.y_parity.ok_or(SignatureError::RecoveryError)?.0,
odd_y_parity: parity.try_into().map_err(|_| SignatureError::InvalidParity)?,
},
);

Expand Down Expand Up @@ -115,8 +126,8 @@ mod tests {
signature: Some(reth_rpc_types::Signature {
r: U256::from(1),
s: U256::from(2),
v: U256::from(37),
y_parity: Some(reth_rpc_types::Parity(true)),
v: U256::from(38),
y_parity: None,
}),
chain_id: Some(1),
transaction_type: Some(0),
Expand All @@ -126,6 +137,15 @@ mod tests {
}

fn with_transaction_type(mut self, tx_type: TxType) -> Self {
match tx_type {
TxType::Eip2930 | TxType::Eip1559 => {
if let Some(sig) = self.tx.signature.as_mut() {
sig.v = U256::from(1);
sig.y_parity = Some(reth_rpc_types::Parity(true));
}
}
_ => {}
}
self.tx.transaction_type = Some(tx_type as u8);
self
}
Expand Down Expand Up @@ -226,12 +246,11 @@ mod tests {

// Then
assert_common_fields!(tx, rpc_tx, $gas_price_field, $has_access_list);
let mut v = rpc_tx.signature.unwrap().v.to::<u64>();
v = if v > 1 { v - tx.chain_id().unwrap() * 2 - 35 } else { v };
assert_eq!(
tx.signature,
rpc_tx
.signature
.map(|sig| Signature { r: sig.r, s: sig.s, odd_y_parity: sig.y_parity.unwrap().0 })
.unwrap()
rpc_tx.signature.map(|sig| Signature { r: sig.r, s: sig.s, odd_y_parity: v != 0 }).unwrap()
);
}
};
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/trace_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub async fn tracing(katana: &Katana, plain_opcodes: &KakarotEvmContract) {
signature: Some(reth_rpc_types::Signature {
r: tx_signed.signature().r,
s: tx_signed.signature().s,
v: U256::from(tx_signed.signature().v(Some(chain_id))),
v: U256::from(tx_signed.signature().odd_y_parity),
y_parity: Some(reth_rpc_types::Parity(tx_signed.signature().odd_y_parity)),
}),
max_fee_per_gas: Some(max_fee_per_gas),
Expand Down

0 comments on commit acf1fda

Please sign in to comment.