add unit tests for is_empty method for Account (#6265)

This commit is contained in:
Thomas Coratger
2024-01-29 23:21:53 +01:00
committed by GitHub
parent 3faf9d87a3
commit 7ad81d52e6
3 changed files with 33 additions and 13 deletions

View File

@@ -8,6 +8,7 @@ use bytes::Buf;
use reth_codecs::{main_codec, Compact};
use serde::{Deserialize, Serialize};
use std::ops::Deref;
/// An Ethereum account.
#[main_codec]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
@@ -27,7 +28,7 @@ impl Account {
}
/// After SpuriousDragon empty account is defined as account with nonce == 0 && balance == 0 &&
/// bytecode = None.
/// bytecode = None (or hash is [`KECCAK_EMPTY`]).
pub fn is_empty(&self) -> bool {
self.nonce == 0 &&
self.balance.is_zero() &&
@@ -145,6 +146,31 @@ mod tests {
assert_eq!(len, 4);
}
#[test]
fn test_empty_account() {
let mut acc = Account { nonce: 0, balance: U256::ZERO, bytecode_hash: None };
// Nonce 0, balance 0, and bytecode hash set to None is considered empty.
assert!(acc.is_empty());
acc.bytecode_hash = Some(KECCAK_EMPTY);
// Nonce 0, balance 0, and bytecode hash set to KECCAK_EMPTY is considered empty.
assert!(acc.is_empty());
acc.balance = U256::from(2);
// Non-zero balance makes it non-empty.
assert!(!acc.is_empty());
acc.balance = U256::ZERO;
acc.nonce = 10;
// Non-zero nonce makes it non-empty.
assert!(!acc.is_empty());
acc.nonce = 0;
acc.bytecode_hash = Some(B256::from(U256::ZERO));
// Non-empty bytecode hash makes it non-empty.
assert!(!acc.is_empty());
}
#[test]
fn test_bytecode() {
let mut buf = vec![];

View File

@@ -15,10 +15,7 @@ pub enum Priority<T: Ord + Clone> {
impl<T: Ord + Clone> From<Option<T>> for Priority<T> {
fn from(value: Option<T>) -> Self {
match value {
Some(val) => Priority::Value(val),
None => Priority::None,
}
value.map_or(Priority::None, Priority::Value)
}
}

View File

@@ -1,4 +1,5 @@
use crate::{
blobstore::BlobStoreError,
error::PoolResult,
pool::{state::SubPool, TransactionEvents},
validate::ValidPoolTransaction,
@@ -6,12 +7,14 @@ use crate::{
};
use futures_util::{ready, Stream};
use reth_primitives::{
AccessList, Address, BlobTransactionSidecar, BlobTransactionValidationError,
kzg::KzgSettings, AccessList, Address, BlobTransactionSidecar, BlobTransactionValidationError,
FromRecoveredPooledTransaction, FromRecoveredTransaction, IntoRecoveredTransaction, PeerId,
PooledTransactionsElement, PooledTransactionsElementEcRecovered, SealedBlock, Transaction,
TransactionKind, TransactionSignedEcRecovered, TxEip4844, TxHash, B256, EIP1559_TX_TYPE_ID,
EIP4844_TX_TYPE_ID, U256,
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::{
collections::{HashMap, HashSet},
fmt,
@@ -21,11 +24,6 @@ use std::{
};
use tokio::sync::mpsc::Receiver;
use crate::blobstore::BlobStoreError;
use reth_primitives::kzg::KzgSettings;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
/// General purpose abstraction of a transaction-pool.
///
/// This is intended to be used by API-consumers such as RPC that need inject new incoming,
@@ -967,8 +965,7 @@ impl PoolTransaction for EthPooledTransaction {
/// This will return `None` for non-EIP1559 transactions
fn max_priority_fee_per_gas(&self) -> Option<u128> {
match &self.transaction.transaction {
Transaction::Legacy(_) => None,
Transaction::Eip2930(_) => None,
Transaction::Legacy(_) | Transaction::Eip2930(_) => None,
Transaction::Eip1559(tx) => Some(tx.max_priority_fee_per_gas),
Transaction::Eip4844(tx) => Some(tx.max_priority_fee_per_gas),
#[cfg(feature = "optimism")]