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

feat(transaction-pool): chaining & static txs for best transactions trait #12320

Merged
merged 22 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 20 additions & 7 deletions crates/optimism/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,23 @@ parking_lot.workspace = true
# rpc
serde_json.workspace = true

# test-utils dependencies
reth = { workspace = true, optional = true }
reth-e2e-test-utils = { workspace = true, optional = true }
alloy-genesis = { workspace = true, optional = true }
tokio = { workspace = true, optional = true }

[dev-dependencies]
reth.workspace = true
reth-optimism-node = { workspace = true, features = ["test-utils"] }
reth-db.workspace = true
reth-e2e-test-utils.workspace = true
reth-node-builder = { workspace = true, features = ["test-utils"] }
reth-provider = { workspace = true, features = ["test-utils"] }
reth-revm = { workspace = true, features = ["test-utils"] }
tokio.workspace = true
alloy-primitives.workspace = true
alloy-genesis.workspace = true
op-alloy-consensus.workspace = true
alloy-signer-local.workspace = true
alloy-network.workspace = true
alloy-consensus.workspace = true

[features]
optimism = [
Expand All @@ -78,15 +84,21 @@ optimism = [
"reth-optimism-rpc/optimism",
"reth-engine-local/optimism",
"reth-optimism-consensus/optimism",
"reth-db/optimism"
"reth-db/optimism",
"reth-optimism-node/optimism"
]
asm-keccak = [
"reth-primitives/asm-keccak",
"reth/asm-keccak",
"alloy-primitives/asm-keccak",
"revm/asm-keccak"
"revm/asm-keccak",
"reth-optimism-node/asm-keccak"
]
test-utils = [
"reth",
"reth-e2e-test-utils",
"alloy-genesis",
"tokio",
"reth-node-builder/test-utils",
"reth-chainspec/test-utils",
"reth-consensus/test-utils",
Expand All @@ -99,5 +111,6 @@ test-utils = [
"reth-provider/test-utils",
"reth-transaction-pool/test-utils",
"reth-trie-db/test-utils",
"revm/test-utils"
"revm/test-utils",
"reth-optimism-node/test-utils"
]
4 changes: 4 additions & 0 deletions crates/optimism/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ pub use node::OpNode;

pub mod txpool;

/// Helpers for running test node instances.
mattsse marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "test-utils")]
pub mod utils;

pub use reth_optimism_payload_builder::{
OpBuiltPayload, OpPayloadBuilder, OpPayloadBuilderAttributes,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
use crate::{node::OpAddOns, OpBuiltPayload, OpNode as OtherOpNode, OpPayloadBuilderAttributes};
use alloy_genesis::Genesis;
use alloy_primitives::{Address, B256};
use reth::{rpc::types::engine::PayloadAttributes, tasks::TaskManager};
use reth_e2e_test_utils::{
transaction::TransactionTestContext, wallet::Wallet, Adapter, NodeHelperType,
};
use reth_optimism_chainspec::OpChainSpecBuilder;
use reth_optimism_node::{
node::OpAddOns, OpBuiltPayload, OpNode as OtherOpNode, OpPayloadBuilderAttributes,
};
use reth_payload_builder::EthPayloadBuilderAttributes;
use std::sync::Arc;
use tokio::sync::Mutex;

/// Optimism Node Helper type
pub(crate) type OpNode = NodeHelperType<OtherOpNode, OpAddOns<Adapter<OtherOpNode>>>;

pub(crate) async fn setup(num_nodes: usize) -> eyre::Result<(Vec<OpNode>, TaskManager, Wallet)> {
let genesis: Genesis = serde_json::from_str(include_str!("../assets/genesis.json")).unwrap();
/// Creates the initial setup with `num_nodes` of the node config, started and connected.
pub async fn setup(num_nodes: usize) -> eyre::Result<(Vec<OpNode>, TaskManager, Wallet)> {
let genesis: Genesis =
serde_json::from_str(include_str!("../tests/assets/genesis.json")).unwrap();
reth_e2e_test_utils::setup(
num_nodes,
Arc::new(OpChainSpecBuilder::base_mainnet().genesis(genesis).ecotone_activated().build()),
Expand All @@ -27,7 +27,7 @@ pub(crate) async fn setup(num_nodes: usize) -> eyre::Result<(Vec<OpNode>, TaskMa
}

/// Advance the chain with sequential payloads returning them in the end.
pub(crate) async fn advance_chain(
pub async fn advance_chain(
length: usize,
node: &mut OpNode,
wallet: Arc<Mutex<Wallet>>,
Expand All @@ -49,7 +49,7 @@ pub(crate) async fn advance_chain(
}

/// Helper function to create a new eth payload attributes
pub(crate) fn optimism_payload_attributes(timestamp: u64) -> OpPayloadBuilderAttributes {
pub fn optimism_payload_attributes(timestamp: u64) -> OpPayloadBuilderAttributes {
let attributes = PayloadAttributes {
timestamp,
prev_randao: B256::ZERO,
Expand Down
3 changes: 0 additions & 3 deletions crates/optimism/node/tests/e2e/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,4 @@
#[cfg(feature = "optimism")]
mod p2p;

#[cfg(feature = "optimism")]
mod utils;

const fn main() {}
2 changes: 1 addition & 1 deletion crates/optimism/node/tests/e2e/p2p.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::utils::{advance_chain, setup};
use alloy_rpc_types_engine::PayloadStatusEnum;
use reth::blockchain_tree::error::BlockchainTreeError;
use reth_optimism_node::utils::{advance_chain, setup};
use std::sync::Arc;
use tokio::sync::Mutex;

Expand Down
3 changes: 3 additions & 0 deletions crates/optimism/node/tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
#[cfg(feature = "optimism")]
mod builder;

#[cfg(feature = "optimism")]
mod priority;

const fn main() {}
190 changes: 190 additions & 0 deletions crates/optimism/node/tests/it/priority.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
//! Node builder test that customizes priority of transactions in the block.

use alloy_consensus::TxEip1559;
use alloy_genesis::Genesis;
use alloy_network::TxSignerSync;
use alloy_primitives::{Address, ChainId, TxKind};
use reth::{args::DatadirArgs, tasks::TaskManager};
use reth_chainspec::EthChainSpec;
use reth_db::test_utils::create_test_rw_db_with_path;
use reth_e2e_test_utils::{
node::NodeTestContext, transaction::TransactionTestContext, wallet::Wallet,
};
use reth_node_api::{FullNodeTypes, NodeTypesWithEngine};
use reth_node_builder::{
components::ComponentsBuilder, EngineNodeLauncher, NodeBuilder, NodeConfig,
};
use reth_optimism_chainspec::{OpChainSpec, OpChainSpecBuilder};
use reth_optimism_node::{
args::RollupArgs,
node::{
OpAddOns, OpConsensusBuilder, OpExecutorBuilder, OpNetworkBuilder, OpPayloadBuilder,
OpPoolBuilder,
},
utils::optimism_payload_attributes,
OpEngineTypes, OpNode,
};
use reth_optimism_payload_builder::builder::OpPayloadTransactions;
use reth_primitives::{SealedBlock, Transaction, TransactionSigned, TransactionSignedEcRecovered};
use reth_provider::providers::BlockchainProvider2;
use reth_transaction_pool::{
pool::{BestPayloadTransactions, PayloadTransactionsChain, PayloadTransactionsFixed},
PayloadTransactions,
};
use std::sync::Arc;
use tokio::sync::Mutex;

#[derive(Clone, Debug)]
struct CustomTxPriority {
chain_id: ChainId,
}

impl OpPayloadTransactions for CustomTxPriority {
fn best_transactions<Pool>(
&self,
pool: Pool,
attr: reth_transaction_pool::BestTransactionsAttributes,
) -> impl PayloadTransactions
where
Pool: reth_transaction_pool::TransactionPool,
{
// Block composition:
// 1. Best transactions from the pool (up to 250k gas)
// 2. End-of-block transaction created by the node (up to 100k gas)

// End of block transaction should send a 0-value transfer to a random address.
let sender = Wallet::default().inner;
let mut end_of_block_tx = TxEip1559 {
chain_id: self.chain_id,
nonce: 1, // it will be 2nd tx after L1 block info tx that uses the same sender
gas_limit: 21000,
max_fee_per_gas: 20e9 as u128,
to: TxKind::Call(Address::random()),
value: 0.try_into().unwrap(),
..Default::default()
};
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,
),
sender.address(),
);

PayloadTransactionsChain::new(
BestPayloadTransactions::new(pool.best_transactions_with_attributes(attr)),
// Allow 250k gas for the transactions from the pool
Some(250_000),
PayloadTransactionsFixed::single(end_of_block_tx),
// Allow 100k gas for the end-of-block transaction
Some(100_000),
)
}
}

/// Builds the node with custom transaction priority service within default payload builder.
fn build_components<Node>(
chain_id: ChainId,
) -> ComponentsBuilder<
Node,
OpPoolBuilder,
OpPayloadBuilder<CustomTxPriority>,
OpNetworkBuilder,
OpExecutorBuilder,
OpConsensusBuilder,
>
where
Node:
FullNodeTypes<Types: NodeTypesWithEngine<Engine = OpEngineTypes, ChainSpec = OpChainSpec>>,
{
let RollupArgs { disable_txpool_gossip, compute_pending_block, discovery_v4, .. } =
RollupArgs::default();
ComponentsBuilder::default()
.node_types::<Node>()
.pool(OpPoolBuilder::default())
.payload(
OpPayloadBuilder::new(compute_pending_block)
.with_transactions(CustomTxPriority { chain_id }),
)
.network(OpNetworkBuilder { disable_txpool_gossip, disable_discovery_v4: !discovery_v4 })
.executor(OpExecutorBuilder::default())
.consensus(OpConsensusBuilder::default())
}

#[tokio::test]
async fn test_custom_block_priority_config() {
reth_tracing::init_test_tracing();

let genesis: Genesis = serde_json::from_str(include_str!("../assets/genesis.json")).unwrap();
let chain_spec =
Arc::new(OpChainSpecBuilder::base_mainnet().genesis(genesis).ecotone_activated().build());

// This wallet is going to send:
// 1. L1 block info tx
// 2. End-of-block custom tx
let wallet = Arc::new(Mutex::new(Wallet::default().with_chain_id(chain_spec.chain().into())));

// Configure and launch the node.
let config = NodeConfig::new(chain_spec).with_datadir_args(DatadirArgs {
datadir: reth_db::test_utils::tempdir_path().into(),
..Default::default()
});
let db = create_test_rw_db_with_path(
config
.datadir
.datadir
.unwrap_or_chain_default(config.chain.chain(), config.datadir.clone())
.db(),
);
let tasks = TaskManager::current();
let node_handle = NodeBuilder::new(config.clone())
.with_database(db)
.with_types_and_provider::<OpNode, BlockchainProvider2<_>>()
.with_components(build_components(config.chain.chain_id()))
.with_add_ons(OpAddOns::default())
.launch_with_fn(|builder| {
let launcher = EngineNodeLauncher::new(
tasks.executor(),
builder.config.datadir(),
Default::default(),
);
builder.launch_with(launcher)
})
.await
.expect("Failed to launch node");

// Advance the chain with a single block.
let block_payloads = NodeTestContext::new(node_handle.node, optimism_payload_attributes)
.await
.unwrap()
.advance(1, |_| {
let wallet = wallet.clone();
Box::pin(async move {
let mut wallet = wallet.lock().await;
let tx_fut = TransactionTestContext::optimism_l1_block_info_tx(
wallet.chain_id,
wallet.inner.clone(),
// This doesn't matter in the current test (because it's only one block),
// but make sure you're not reusing the nonce from end-of-block tx
// if they have the same signer.
wallet.inner_nonce * 2,
);
wallet.inner_nonce += 1;
tx_fut.await
})
})
.await
.unwrap();
assert_eq!(block_payloads.len(), 1);
let (block_payload, _) = block_payloads.first().unwrap();
let block_payload: SealedBlock = block_payload.block().clone();
assert_eq!(block_payload.body.transactions.len(), 2); // L1 block info tx + end-of-block custom tx

// Check that last transaction in the block looks like a transfer to a random address.
let end_of_block_tx = block_payload.body.transactions.last().unwrap();
let end_of_block_tx = end_of_block_tx.transaction.as_eip1559().unwrap();
assert_eq!(end_of_block_tx.nonce, 1);
assert_eq!(end_of_block_tx.gas_limit, 21_000);
assert!(end_of_block_tx.input.is_empty());
}
Loading
Loading