Skip to content

Commit

Permalink
fix: protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell committed Oct 28, 2024
1 parent 85fcd9d commit 7f788bf
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions crates/protocol/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ pub const fn is_protected_v(tx: &TxEnvelope) -> bool {
#[cfg(test)]
mod tests {
use super::*;
use alloy_consensus::{
Signed, TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant, TxEip7702, TxLegacy,
};
use alloy_primitives::{b256, Signature};
use alloy_sol_types::{sol, SolCall};
use revm::{
db::BenchmarkDB,
Expand All @@ -316,6 +320,63 @@ mod tests {

use rstest::rstest;

#[test]
fn test_convert_v_to_y_parity() {
assert_eq!(convert_v_to_y_parity(27, TxType::Legacy), Ok(false));
assert_eq!(convert_v_to_y_parity(28, TxType::Legacy), Ok(true));
assert_eq!(convert_v_to_y_parity(36, TxType::Legacy), Ok(true));
assert_eq!(convert_v_to_y_parity(37, TxType::Legacy), Ok(false));
assert_eq!(convert_v_to_y_parity(1, TxType::Eip2930), Ok(true));
assert_eq!(convert_v_to_y_parity(1, TxType::Eip1559), Ok(true));
assert_eq!(
convert_v_to_y_parity(1, TxType::Eip4844),
Err(SpanBatchError::Decoding(SpanDecodingError::InvalidTransactionType))
);
assert_eq!(
convert_v_to_y_parity(0, TxType::Eip7702),
Err(SpanBatchError::Decoding(SpanDecodingError::InvalidTransactionType))
);
}

#[test]
fn test_is_protected_v() {
let sig = Signature::test_signature();
assert!(!is_protected_v(&TxEnvelope::Legacy(Signed::new_unchecked(
TxLegacy::default(),
sig,
Default::default(),
))));
let r = b256!("840cfc572845f5786e702984c2a582528cad4b49b2a10b9db1be7fca90058565");
let s = b256!("25e7109ceb98168d95b09b18bbf6b685130e0562f233877d492b94eee0c5b6d1");
let v = 27;
let valid_sig = Signature::from_scalars_and_parity(r, s, v).unwrap();
assert!(!is_protected_v(&TxEnvelope::Legacy(Signed::new_unchecked(
TxLegacy::default(),
valid_sig,
Default::default(),
))));
assert!(is_protected_v(&TxEnvelope::Eip2930(Signed::new_unchecked(
TxEip2930::default(),
sig,
Default::default(),
))));
assert!(is_protected_v(&TxEnvelope::Eip1559(Signed::new_unchecked(
TxEip1559::default(),
sig,
Default::default(),
))));
assert!(is_protected_v(&TxEnvelope::Eip4844(Signed::new_unchecked(
TxEip4844Variant::TxEip4844(TxEip4844::default()),
sig,
Default::default(),
))));
assert!(is_protected_v(&TxEnvelope::Eip7702(Signed::new_unchecked(
TxEip7702::default(),
sig,
Default::default(),
))));
}

#[rstest]
#[case::empty(&[], 0)]
#[case::thousand_zeros(&[0; 1000], 21)]
Expand Down

0 comments on commit 7f788bf

Please sign in to comment.