mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat: add TransactionSigned::new fns (#12768)
This commit is contained in:
@ -1570,7 +1570,7 @@ mod tests {
|
||||
|
||||
let single_tx_cost = U256::from(INITIAL_BASE_FEE * MIN_TRANSACTION_GAS);
|
||||
let mock_tx = |nonce: u64| -> TransactionSignedEcRecovered {
|
||||
TransactionSigned::from_transaction_and_signature(
|
||||
TransactionSigned::new_unhashed(
|
||||
Transaction::Eip1559(TxEip1559 {
|
||||
chain_id: chain_spec.chain.id(),
|
||||
nonce,
|
||||
|
||||
@ -102,8 +102,7 @@ impl TestBlockBuilder {
|
||||
let signature_hash = tx.signature_hash();
|
||||
let signature = self.signer_pk.sign_hash_sync(&signature_hash).unwrap();
|
||||
|
||||
TransactionSigned::from_transaction_and_signature(tx, signature)
|
||||
.with_signer(self.signer)
|
||||
TransactionSigned::new_unhashed(tx, signature).with_signer(self.signer)
|
||||
};
|
||||
|
||||
let num_txs = rng.gen_range(0..5);
|
||||
|
||||
@ -450,7 +450,7 @@ mod tests {
|
||||
|
||||
let signature = Signature::new(U256::default(), U256::default(), true);
|
||||
|
||||
TransactionSigned::from_transaction_and_signature(request, signature)
|
||||
TransactionSigned::new_unhashed(request, signature)
|
||||
}
|
||||
|
||||
/// got test block
|
||||
|
||||
@ -342,7 +342,7 @@ mod tests {
|
||||
message: BlockBodies(vec![
|
||||
BlockBody {
|
||||
transactions: vec![
|
||||
TransactionSigned::from_transaction_and_signature(Transaction::Legacy(TxLegacy {
|
||||
TransactionSigned::new_unhashed(Transaction::Legacy(TxLegacy {
|
||||
chain_id: Some(1),
|
||||
nonce: 0x8u64,
|
||||
gas_price: 0x4a817c808,
|
||||
@ -356,7 +356,7 @@ mod tests {
|
||||
false,
|
||||
),
|
||||
),
|
||||
TransactionSigned::from_transaction_and_signature(Transaction::Legacy(TxLegacy {
|
||||
TransactionSigned::new_unhashed(Transaction::Legacy(TxLegacy {
|
||||
chain_id: Some(1),
|
||||
nonce: 0x9u64,
|
||||
gas_price: 0x4a817c809,
|
||||
@ -413,7 +413,7 @@ mod tests {
|
||||
message: BlockBodies(vec![
|
||||
BlockBody {
|
||||
transactions: vec![
|
||||
TransactionSigned::from_transaction_and_signature(Transaction::Legacy(
|
||||
TransactionSigned::new_unhashed(Transaction::Legacy(
|
||||
TxLegacy {
|
||||
chain_id: Some(1),
|
||||
nonce: 0x8u64,
|
||||
@ -423,13 +423,13 @@ mod tests {
|
||||
value: U256::from(0x200u64),
|
||||
input: Default::default(),
|
||||
}),
|
||||
Signature::new(
|
||||
Signature::new(
|
||||
U256::from_str("0x64b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c12").unwrap(),
|
||||
U256::from_str("0x64b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c10").unwrap(),
|
||||
false,
|
||||
),
|
||||
),
|
||||
TransactionSigned::from_transaction_and_signature(
|
||||
TransactionSigned::new_unhashed(
|
||||
Transaction::Legacy(TxLegacy {
|
||||
chain_id: Some(1),
|
||||
nonce: 0x9u64,
|
||||
|
||||
@ -130,7 +130,7 @@ mod tests {
|
||||
let expected = hex!("f8d7820457f8d2f867088504a817c8088302e2489435353535353535353535353535353535353535358202008025a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c12a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c10f867098504a817c809830334509435353535353535353535353535353535353535358202d98025a052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afba052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afb");
|
||||
let mut data = vec![];
|
||||
let txs = vec![
|
||||
TransactionSigned::from_transaction_and_signature(
|
||||
TransactionSigned::new_unhashed(
|
||||
Transaction::Legacy(TxLegacy {
|
||||
chain_id: Some(1),
|
||||
nonce: 0x8u64,
|
||||
@ -152,7 +152,7 @@ mod tests {
|
||||
false,
|
||||
),
|
||||
),
|
||||
TransactionSigned::from_transaction_and_signature(
|
||||
TransactionSigned::new_unhashed(
|
||||
Transaction::Legacy(TxLegacy {
|
||||
chain_id: Some(1),
|
||||
nonce: 0x09u64,
|
||||
@ -196,7 +196,7 @@ mod tests {
|
||||
fn decode_pooled_transactions() {
|
||||
let data = hex!("f8d7820457f8d2f867088504a817c8088302e2489435353535353535353535353535353535353535358202008025a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c12a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c10f867098504a817c809830334509435353535353535353535353535353535353535358202d98025a052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afba052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afb");
|
||||
let txs = vec![
|
||||
TransactionSigned::from_transaction_and_signature(
|
||||
TransactionSigned::new_unhashed(
|
||||
Transaction::Legacy(TxLegacy {
|
||||
chain_id: Some(1),
|
||||
nonce: 0x8u64,
|
||||
@ -218,7 +218,7 @@ mod tests {
|
||||
false,
|
||||
),
|
||||
),
|
||||
TransactionSigned::from_transaction_and_signature(
|
||||
TransactionSigned::new_unhashed(
|
||||
Transaction::Legacy(TxLegacy {
|
||||
chain_id: Some(1),
|
||||
nonce: 0x09u64,
|
||||
@ -260,7 +260,7 @@ mod tests {
|
||||
let decoded_transactions =
|
||||
RequestPair::<PooledTransactions>::decode(&mut &data[..]).unwrap();
|
||||
let txs = vec![
|
||||
TransactionSigned::from_transaction_and_signature(
|
||||
TransactionSigned::new_unhashed(
|
||||
Transaction::Legacy(TxLegacy {
|
||||
chain_id: Some(4),
|
||||
nonce: 15u64,
|
||||
@ -282,7 +282,7 @@ mod tests {
|
||||
true,
|
||||
),
|
||||
),
|
||||
TransactionSigned::from_transaction_and_signature(
|
||||
TransactionSigned::new_unhashed(
|
||||
Transaction::Eip1559(TxEip1559 {
|
||||
chain_id: 4,
|
||||
nonce: 26u64,
|
||||
@ -306,7 +306,7 @@ mod tests {
|
||||
true,
|
||||
),
|
||||
),
|
||||
TransactionSigned::from_transaction_and_signature(
|
||||
TransactionSigned::new_unhashed(
|
||||
Transaction::Legacy(TxLegacy {
|
||||
chain_id: Some(4),
|
||||
nonce: 3u64,
|
||||
@ -328,7 +328,7 @@ mod tests {
|
||||
false,
|
||||
),
|
||||
),
|
||||
TransactionSigned::from_transaction_and_signature(
|
||||
TransactionSigned::new_unhashed(
|
||||
Transaction::Legacy(TxLegacy {
|
||||
chain_id: Some(4),
|
||||
nonce: 1u64,
|
||||
@ -350,7 +350,7 @@ mod tests {
|
||||
false,
|
||||
),
|
||||
),
|
||||
TransactionSigned::from_transaction_and_signature(
|
||||
TransactionSigned::new_unhashed(
|
||||
Transaction::Legacy(TxLegacy {
|
||||
chain_id: Some(4),
|
||||
nonce: 2u64,
|
||||
@ -397,7 +397,7 @@ mod tests {
|
||||
fn encode_pooled_transactions_network() {
|
||||
let expected = hex!("f9022980f90225f8650f84832156008287fb94cf7f9e66af820a19257a2108375b180b0ec491678204d2802ca035b7bfeb9ad9ece2cbafaaf8e202e706b4cfaeb233f46198f00b44d4a566a981a0612638fb29427ca33b9a3be2a0a561beecfe0269655be160d35e72d366a6a860b87502f872041a8459682f008459682f0d8252089461815774383099e24810ab832a5b2a5425c154d58829a2241af62c000080c001a059e6b67f48fb32e7e570dfb11e042b5ad2e55e3ce3ce9cd989c7e06e07feeafda0016b83f4f980694ed2eee4d10667242b1f40dc406901b34125b008d334d47469f86b0384773594008398968094d3e8763675e4c425df46cc3b5c0f6cbdac39604687038d7ea4c68000802ba0ce6834447c0a4193c40382e6c57ae33b241379c5418caac9cdc18d786fd12071a03ca3ae86580e94550d7c071e3a02eadb5a77830947c9225165cf9100901bee88f86b01843b9aca00830186a094d3e8763675e4c425df46cc3b5c0f6cbdac3960468702769bb01b2a00802ba0e24d8bd32ad906d6f8b8d7741e08d1959df021698b19ee232feba15361587d0aa05406ad177223213df262cb66ccbb2f46bfdccfdfbbb5ffdda9e2c02d977631daf86b02843b9aca00830186a094d3e8763675e4c425df46cc3b5c0f6cbdac39604687038d7ea4c68000802ba00eb96ca19e8a77102767a41fc85a36afd5c61ccb09911cec5d3e86e193d9c5aea03a456401896b1b6055311536bf00a718568c744d8c1f9df59879e8350220ca18");
|
||||
let txs = vec![
|
||||
TransactionSigned::from_transaction_and_signature(
|
||||
TransactionSigned::new_unhashed(
|
||||
Transaction::Legacy(TxLegacy {
|
||||
chain_id: Some(4),
|
||||
nonce: 15u64,
|
||||
@ -419,7 +419,7 @@ mod tests {
|
||||
true,
|
||||
),
|
||||
),
|
||||
TransactionSigned::from_transaction_and_signature(
|
||||
TransactionSigned::new_unhashed(
|
||||
Transaction::Eip1559(TxEip1559 {
|
||||
chain_id: 4,
|
||||
nonce: 26u64,
|
||||
@ -443,7 +443,7 @@ mod tests {
|
||||
true,
|
||||
),
|
||||
),
|
||||
TransactionSigned::from_transaction_and_signature(
|
||||
TransactionSigned::new_unhashed(
|
||||
Transaction::Legacy(TxLegacy {
|
||||
chain_id: Some(4),
|
||||
nonce: 3u64,
|
||||
@ -465,7 +465,7 @@ mod tests {
|
||||
false,
|
||||
),
|
||||
),
|
||||
TransactionSigned::from_transaction_and_signature(
|
||||
TransactionSigned::new_unhashed(
|
||||
Transaction::Legacy(TxLegacy {
|
||||
chain_id: Some(4),
|
||||
nonce: 1u64,
|
||||
@ -487,7 +487,7 @@ mod tests {
|
||||
false,
|
||||
),
|
||||
),
|
||||
TransactionSigned::from_transaction_and_signature(
|
||||
TransactionSigned::new_unhashed(
|
||||
Transaction::Legacy(TxLegacy {
|
||||
chain_id: Some(4),
|
||||
nonce: 2u64,
|
||||
|
||||
@ -33,7 +33,7 @@ pub fn rng_transaction(rng: &mut impl rand::RngCore) -> TransactionSigned {
|
||||
});
|
||||
let signature = Signature::new(U256::default(), U256::default(), true);
|
||||
|
||||
TransactionSigned::from_transaction_and_signature(request, signature)
|
||||
TransactionSigned::new_unhashed(request, signature)
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
|
||||
@ -132,10 +132,7 @@ async fn test_sending_invalid_transactions() {
|
||||
value: Default::default(),
|
||||
input: Default::default(),
|
||||
};
|
||||
let tx = TransactionSigned::from_transaction_and_signature(
|
||||
tx.into(),
|
||||
Signature::test_signature(),
|
||||
);
|
||||
let tx = TransactionSigned::new_unhashed(tx.into(), Signature::test_signature());
|
||||
peer0.network().send_transactions(*peer1.peer_id(), vec![Arc::new(tx)]);
|
||||
}
|
||||
|
||||
|
||||
@ -377,7 +377,7 @@ mod tests {
|
||||
|
||||
let chain_spec = Arc::new(OpChainSpecBuilder::base_mainnet().regolith_activated().build());
|
||||
|
||||
let tx = TransactionSigned::from_transaction_and_signature(
|
||||
let tx = TransactionSigned::new_unhashed(
|
||||
Transaction::Eip1559(TxEip1559 {
|
||||
chain_id: chain_spec.chain.id(),
|
||||
nonce: 0,
|
||||
@ -388,7 +388,7 @@ mod tests {
|
||||
Signature::test_signature(),
|
||||
);
|
||||
|
||||
let tx_deposit = TransactionSigned::from_transaction_and_signature(
|
||||
let tx_deposit = TransactionSigned::new_unhashed(
|
||||
Transaction::Deposit(op_alloy_consensus::TxDeposit {
|
||||
from: addr,
|
||||
to: addr.into(),
|
||||
@ -461,7 +461,7 @@ mod tests {
|
||||
|
||||
let chain_spec = Arc::new(OpChainSpecBuilder::base_mainnet().canyon_activated().build());
|
||||
|
||||
let tx = TransactionSigned::from_transaction_and_signature(
|
||||
let tx = TransactionSigned::new_unhashed(
|
||||
Transaction::Eip1559(TxEip1559 {
|
||||
chain_id: chain_spec.chain.id(),
|
||||
nonce: 0,
|
||||
@ -472,7 +472,7 @@ mod tests {
|
||||
Signature::test_signature(),
|
||||
);
|
||||
|
||||
let tx_deposit = TransactionSigned::from_transaction_and_signature(
|
||||
let tx_deposit = TransactionSigned::new_unhashed(
|
||||
Transaction::Deposit(op_alloy_consensus::TxDeposit {
|
||||
from: addr,
|
||||
to: addr.into(),
|
||||
|
||||
@ -265,7 +265,7 @@ mod tests {
|
||||
input: Default::default(),
|
||||
});
|
||||
let signature = Signature::test_signature();
|
||||
let signed_tx = TransactionSigned::from_transaction_and_signature(deposit_tx, signature);
|
||||
let signed_tx = TransactionSigned::new_unhashed(deposit_tx, signature);
|
||||
let signed_recovered =
|
||||
TransactionSignedEcRecovered::from_signed_transaction(signed_tx, signer);
|
||||
let len = signed_recovered.encode_2718_len();
|
||||
|
||||
@ -63,10 +63,7 @@ impl OpPayloadTransactions for CustomTxPriority {
|
||||
};
|
||||
let signature = sender.sign_transaction_sync(&mut end_of_block_tx).unwrap();
|
||||
let end_of_block_tx = TransactionSignedEcRecovered::from_signed_transaction(
|
||||
TransactionSigned::from_transaction_and_signature(
|
||||
Transaction::Eip1559(end_of_block_tx),
|
||||
signature,
|
||||
),
|
||||
TransactionSigned::new_unhashed(Transaction::Eip1559(end_of_block_tx), signature),
|
||||
sender.address(),
|
||||
);
|
||||
|
||||
|
||||
@ -914,7 +914,7 @@ impl TransactionSignedNoHash {
|
||||
#[inline]
|
||||
pub fn with_hash(self) -> TransactionSigned {
|
||||
let Self { signature, transaction } = self;
|
||||
TransactionSigned::from_transaction_and_signature(transaction, signature)
|
||||
TransactionSigned::new_unhashed(transaction, signature)
|
||||
}
|
||||
|
||||
/// Recovers a list of signers from a transaction list iterator
|
||||
@ -1105,6 +1105,18 @@ impl PartialEq for TransactionSigned {
|
||||
// === impl TransactionSigned ===
|
||||
|
||||
impl TransactionSigned {
|
||||
/// Creates a new signed transaction from the given parts.
|
||||
pub fn new(transaction: Transaction, signature: Signature, hash: B256) -> Self {
|
||||
Self { hash: hash.into(), signature, transaction }
|
||||
}
|
||||
|
||||
/// Creates a new signed transaction from the given transaction and signature without the hash.
|
||||
///
|
||||
/// Note: this only calculates the hash on the first [`TransactionSigned::hash`] call.
|
||||
pub fn new_unhashed(transaction: Transaction, signature: Signature) -> Self {
|
||||
Self { hash: Default::default(), signature, transaction }
|
||||
}
|
||||
|
||||
/// Transaction signature.
|
||||
pub const fn signature(&self) -> &Signature {
|
||||
&self.signature
|
||||
@ -1251,13 +1263,6 @@ impl TransactionSigned {
|
||||
keccak256(self.encoded_2718())
|
||||
}
|
||||
|
||||
/// Create a new signed transaction from a transaction and its signature.
|
||||
///
|
||||
/// This will also calculate the transaction hash using its encoding.
|
||||
pub fn from_transaction_and_signature(transaction: Transaction, signature: Signature) -> Self {
|
||||
Self { transaction, signature, hash: Default::default() }
|
||||
}
|
||||
|
||||
/// Decodes legacy transaction from the data buffer into a tuple.
|
||||
///
|
||||
/// This expects `rlp(legacy_tx)`
|
||||
@ -1614,7 +1619,7 @@ impl Decodable2718 for TransactionSigned {
|
||||
Ok(Self { transaction: Transaction::Eip4844(tx), signature, hash: hash.into() })
|
||||
}
|
||||
#[cfg(feature = "optimism")]
|
||||
TxType::Deposit => Ok(Self::from_transaction_and_signature(
|
||||
TxType::Deposit => Ok(Self::new_unhashed(
|
||||
Transaction::Deposit(TxDeposit::rlp_decode(buf)?),
|
||||
TxDeposit::signature(),
|
||||
)),
|
||||
@ -1652,7 +1657,7 @@ impl<'a> arbitrary::Arbitrary<'a> for TransactionSigned {
|
||||
|
||||
#[cfg(feature = "optimism")]
|
||||
let signature = if transaction.is_deposit() { TxDeposit::signature() } else { signature };
|
||||
Ok(Self::from_transaction_and_signature(transaction, signature))
|
||||
Ok(Self::new_unhashed(transaction, signature))
|
||||
}
|
||||
}
|
||||
|
||||
@ -2191,7 +2196,7 @@ mod tests {
|
||||
signature: Signature,
|
||||
hash: Option<B256>,
|
||||
) {
|
||||
let expected = TransactionSigned::from_transaction_and_signature(transaction, signature);
|
||||
let expected = TransactionSigned::new_unhashed(transaction, signature);
|
||||
if let Some(hash) = hash {
|
||||
assert_eq!(hash, expected.hash());
|
||||
}
|
||||
@ -2288,7 +2293,7 @@ mod tests {
|
||||
let signature =
|
||||
crate::sign_message(B256::from_slice(&key_pair.secret_bytes()[..]), tx.signature_hash()).unwrap();
|
||||
|
||||
TransactionSigned::from_transaction_and_signature(tx, signature)
|
||||
TransactionSigned::new_unhashed(tx, signature)
|
||||
}).collect();
|
||||
|
||||
let parallel_senders = TransactionSigned::recover_signers(&txes, txes.len()).unwrap();
|
||||
|
||||
@ -199,7 +199,7 @@ impl TransactionBuilder {
|
||||
/// Signs the provided transaction using the specified signer and returns a signed transaction.
|
||||
fn signed(transaction: Transaction, signer: B256) -> TransactionSigned {
|
||||
let signature = sign_message(signer, transaction.signature_hash()).unwrap();
|
||||
TransactionSigned::from_transaction_and_signature(transaction, signature)
|
||||
TransactionSigned::new_unhashed(transaction, signature)
|
||||
}
|
||||
|
||||
/// Sets the signer for the transaction builder.
|
||||
|
||||
@ -1584,7 +1584,7 @@ mod tests {
|
||||
..Default::default()
|
||||
});
|
||||
let signature = Signature::test_signature();
|
||||
let signed_tx = TransactionSigned::from_transaction_and_signature(tx, signature);
|
||||
let signed_tx = TransactionSigned::new_unhashed(tx, signature);
|
||||
let transaction =
|
||||
TransactionSignedEcRecovered::from_signed_transaction(signed_tx, Default::default());
|
||||
let pooled_tx = EthPooledTransaction::new(transaction.clone(), 200);
|
||||
@ -1606,7 +1606,7 @@ mod tests {
|
||||
..Default::default()
|
||||
});
|
||||
let signature = Signature::test_signature();
|
||||
let signed_tx = TransactionSigned::from_transaction_and_signature(tx, signature);
|
||||
let signed_tx = TransactionSigned::new_unhashed(tx, signature);
|
||||
let transaction =
|
||||
TransactionSignedEcRecovered::from_signed_transaction(signed_tx, Default::default());
|
||||
let pooled_tx = EthPooledTransaction::new(transaction.clone(), 200);
|
||||
@ -1628,7 +1628,7 @@ mod tests {
|
||||
..Default::default()
|
||||
});
|
||||
let signature = Signature::test_signature();
|
||||
let signed_tx = TransactionSigned::from_transaction_and_signature(tx, signature);
|
||||
let signed_tx = TransactionSigned::new_unhashed(tx, signature);
|
||||
let transaction =
|
||||
TransactionSignedEcRecovered::from_signed_transaction(signed_tx, Default::default());
|
||||
let pooled_tx = EthPooledTransaction::new(transaction.clone(), 200);
|
||||
@ -1652,7 +1652,7 @@ mod tests {
|
||||
..Default::default()
|
||||
});
|
||||
let signature = Signature::test_signature();
|
||||
let signed_tx = TransactionSigned::from_transaction_and_signature(tx, signature);
|
||||
let signed_tx = TransactionSigned::new_unhashed(tx, signature);
|
||||
let transaction =
|
||||
TransactionSignedEcRecovered::from_signed_transaction(signed_tx, Default::default());
|
||||
let pooled_tx = EthPooledTransaction::new(transaction.clone(), 300);
|
||||
@ -1676,7 +1676,7 @@ mod tests {
|
||||
..Default::default()
|
||||
});
|
||||
let signature = Signature::test_signature();
|
||||
let signed_tx = TransactionSigned::from_transaction_and_signature(tx, signature);
|
||||
let signed_tx = TransactionSigned::new_unhashed(tx, signature);
|
||||
let transaction =
|
||||
TransactionSignedEcRecovered::from_signed_transaction(signed_tx, Default::default());
|
||||
let pooled_tx = EthPooledTransaction::new(transaction.clone(), 200);
|
||||
|
||||
@ -149,7 +149,7 @@ pub fn sign_tx_with_key_pair(key_pair: Keypair, tx: Transaction) -> TransactionS
|
||||
let signature =
|
||||
sign_message(B256::from_slice(&key_pair.secret_bytes()[..]), tx.signature_hash()).unwrap();
|
||||
|
||||
TransactionSigned::from_transaction_and_signature(tx, signature)
|
||||
TransactionSigned::new_unhashed(tx, signature)
|
||||
}
|
||||
|
||||
/// Generates a set of [Keypair]s based on the desired count.
|
||||
@ -479,7 +479,7 @@ mod tests {
|
||||
sign_message(B256::from_slice(&key_pair.secret_bytes()[..]), signature_hash)
|
||||
.unwrap();
|
||||
|
||||
let signed = TransactionSigned::from_transaction_and_signature(tx.clone(), signature);
|
||||
let signed = TransactionSigned::new_unhashed(tx.clone(), signature);
|
||||
let recovered = signed.recover_signer().unwrap();
|
||||
|
||||
let expected = public_key_to_address(key_pair.public_key());
|
||||
|
||||
Reference in New Issue
Block a user