Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add unit tests for is_empty method for Account #6265

Merged
merged 2 commits into from
Jan 29, 2024
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
28 changes: 27 additions & 1 deletion crates/primitives/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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() &&
Expand Down Expand Up @@ -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![];
Expand Down
5 changes: 1 addition & 4 deletions crates/transaction-pool/src/ordering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
13 changes: 5 additions & 8 deletions crates/transaction-pool/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
use crate::{
blobstore::BlobStoreError,
error::PoolResult,
pool::{state::SubPool, TransactionEvents},
validate::ValidPoolTransaction,
AllTransactionsEvents,
};
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,
Expand All @@ -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,
Expand Down Expand Up @@ -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")]
Expand Down
Loading