Skip to content
Closed
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
151 changes: 151 additions & 0 deletions rust/op-reth/crates/node/tests/it/engine_driver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
//! An engine API block builder for a launched node, and the timeouts that bound waits on it.

use alloy_primitives::{B256, Bytes};
use alloy_rpc_types_engine::{ForkchoiceState, PayloadId, PayloadStatus};
use alloy_signer_local::PrivateKeySigner;
use jsonrpsee::{core::client::ClientT, rpc_params};
use op_alloy_rpc_types_engine::OpExecutionPayloadEnvelopeV3;
use reth_e2e_test_utils::{transaction::TransactionTestContext, wallet::Wallet};
use reth_node_api::ConsensusEngineHandle;
use reth_optimism_node::{OpEngineTypes, utils::optimism_payload_attributes};
use std::time::{Duration, Instant};

/// Bounds every engine call and every wait on the node, so a stall fails the test instead of
/// hanging it.
const WAIT_TIMEOUT: Duration = Duration::from_secs(20);

/// A payload job the engine has started but not resolved.
pub(crate) struct StartedPayloadJob {
number: u64,
pub(crate) payload_id: PayloadId,
start_duration: Duration,
}

/// Builds blocks the way a consensus client does, through the engine API.
pub(crate) struct EngineDriver<C> {
pub(crate) client: C,
pub(crate) engine: ConsensusEngineHandle<OpEngineTypes>,
pub(crate) head: B256,
pub(crate) wallet: Wallet,
}

impl<C: ClientT + Sync> EngineDriver<C> {
/// A funded account the driver never signs with: the L1 info transactions it signs use up
/// the wallet's nonces.
pub(crate) fn spare_signer(&self) -> PrivateKeySigner {
Wallet::new(2).with_chain_id(self.wallet.chain_id).wallet_gen().remove(1)
}

pub(crate) async fn build_block(&mut self, number: u64) -> eyre::Result<Duration> {
self.build_block_with(number, Vec::new()).await
}

/// Builds a block that carries the given sequencer transactions after the block's L1 info
/// transaction.
pub(crate) async fn build_block_with(
&mut self,
number: u64,
sequencer_txs: Vec<Bytes>,
) -> eyre::Result<Duration> {
let job = self.start_payload_job_with(number, sequencer_txs).await?;
self.finish_payload_job(job).await
}

/// Starts a payload job on the head, with the block's L1 info transaction as its only
/// sequencer transaction.
pub(crate) async fn start_payload_job(&self, number: u64) -> eyre::Result<StartedPayloadJob> {
self.start_payload_job_with(number, Vec::new()).await
}

/// Starts a payload job on the head whose sequencer transactions are the block's L1 info
/// transaction followed by the given ones.
async fn start_payload_job_with(
&self,
number: u64,
sequencer_txs: Vec<Bytes>,
) -> eyre::Result<StartedPayloadJob> {
let mut attrs = optimism_payload_attributes(number);
let mut transactions = vec![
TransactionTestContext::optimism_l1_block_info_tx(
self.wallet.chain_id,
self.wallet.inner.clone(),
number - 1,
)
.await,
];
transactions.extend(sequencer_txs);
attrs.0.transactions = Some(transactions);
let started_at = Instant::now();
let fcu = within_timeout(
"forkchoice update with attributes stalled",
self.engine.fork_choice_updated(ForkchoiceState::same_hash(self.head), Some(attrs)),
)
.await?;
let start_duration = started_at.elapsed();
assert!(fcu.payload_status.is_valid(), "forkchoice update for block {number}: {fcu:?}");
let payload_id = fcu.payload_id.expect("a payload job must start");
Ok(StartedPayloadJob { number, payload_id, start_duration })
}

/// Fetches the job's payload and makes it canonical. Returns how long the engine took to
/// answer the forkchoice update that started the job and the getPayload that resolved it,
/// leaving out whatever the caller waited on in between.
pub(crate) async fn finish_payload_job(
&mut self,
job: StartedPayloadJob,
) -> eyre::Result<Duration> {
let resolve_started_at = Instant::now();
let payload: OpExecutionPayloadEnvelopeV3 = within_timeout(
"getPayload stalled",
self.client.request("engine_getPayloadV3", rpc_params![job.payload_id]),
)
.await?;
let job_duration = job.start_duration + resolve_started_at.elapsed();

let block = &payload.execution_payload.payload_inner.payload_inner;
assert_eq!(block.block_number, job.number);
assert!(
!block.transactions.is_empty(),
"block {} lacks its L1 info transaction",
job.number
);
self.head = block.block_hash;
let status: PayloadStatus = within_timeout(
"newPayload stalled",
self.client.request(
"engine_newPayloadV3",
rpc_params![
payload.execution_payload,
Vec::<B256>::new(),
payload.parent_beacon_block_root
],
),
)
.await?;
assert!(status.is_valid(), "newPayload for block {}: {status:?}", job.number);
let fcu = within_timeout(
"canonical forkchoice update stalled",
self.engine.fork_choice_updated(ForkchoiceState::same_hash(self.head), None),
)
.await?;
assert!(
fcu.payload_status.is_valid(),
"forkchoice update to block {}: {fcu:?}",
job.number
);
Ok(job_duration)
}
}

pub(crate) async fn within_timeout<T>(message: &str, future: impl Future<Output = T>) -> T {
tokio::time::timeout(WAIT_TIMEOUT, future).await.unwrap_or_else(|_| panic!("{message}"))
}

pub(crate) async fn wait_until(message: &str, mut ready: impl FnMut() -> bool) {
within_timeout(message, async {
while !ready() {
tokio::time::sleep(Duration::from_millis(10)).await;
}
})
.await;
}
228 changes: 228 additions & 0 deletions rust/op-reth/crates/node/tests/it/get_logs_errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
//! `eth_getLogs` must answer an error, not a silently empty or partial array, for a range the
//! node cannot serve in full: a range reaching past the head and a range or block whose receipts
//! the node pruned. An indexer that polls `lastSeen + 1`, or reads a range whose receipts are gone,
//! sees the error instead of mistaking the missing logs for an empty range.

use crate::engine_driver::{EngineDriver, wait_until};
use alloy_consensus::{SignableTransaction, TxEip1559};
use alloy_genesis::Genesis;
use alloy_network::{TxSignerSync, eip2718::Encodable2718};
use alloy_primitives::{Address, B256, Bytes, TxKind};
use alloy_rpc_types_eth::{Filter, Log};
use alloy_signer_local::PrivateKeySigner;
use jsonrpsee::{
core::client::{ClientT, Error as ClientError},
rpc_params,
types::error::INVALID_PARAMS_CODE,
};
use reth_chainspec::EthChainSpec;
use reth_e2e_test_utils::wallet::Wallet;
use reth_node_builder::{NodeBuilder, NodeConfig, NodeHandle, rpc::RethRpcServerHandles};
use reth_node_core::args::{PruningArgs, RpcServerArgs};
use reth_optimism_chainspec::OpChainSpecBuilder;
use reth_optimism_node::OpNode;
use reth_provider::{BlockHashReader, ChainSpecProvider, ReceiptProvider};
use reth_tasks::Runtime;
use std::sync::Arc;

/// The receipts of every block below this one are pruned; the blocks from it on keep theirs.
const RECEIPTS_PRUNED_BEFORE: u64 = 4;
/// The height the chain is built to. The head carries the one log a served range answers with.
const CHAIN_HEIGHT: u64 = 6;
/// Init code that emits an empty `LOG0` and deploys no runtime code: `PUSH1 0 PUSH1 0 LOG0 STOP`.
const LOG_EMITTING_INIT_CODE: [u8; 6] = [0x60, 0x00, 0x60, 0x00, 0xa0, 0x00];
/// EIP-4444's code for history the node no longer holds.
const PRUNED_HISTORY_UNAVAILABLE_CODE: i32 = 4444;

/// A node whose chain reaches `CHAIN_HEIGHT` with the receipts below `RECEIPTS_PRUNED_BEFORE`
/// pruned and one log, from a contract created at the head, in the receipts it kept.
struct PrunedReceiptsNode<N> {
handles: RethRpcServerHandles,
last_pruned_block_hash: B256,
log_address: Address,
/// Keeps the node behind the RPC handles running.
_node: N,
}

async fn launch_pruned_receipts_node() -> eyre::Result<PrunedReceiptsNode<impl Sized>> {
super::crash_backtrace::install();
reth_tracing::init_test_tracing();

let genesis: Genesis = serde_json::from_str(include_str!("../assets/genesis.json"))?;
let chain_spec = Arc::new(
OpChainSpecBuilder::optimism_sepolia().genesis(genesis).ecotone_activated().build(),
);
let chain_id = chain_spec.chain_id();
let mut config = NodeConfig::test()
.map_chain(chain_spec)
.with_unused_ports()
.with_rpc(RpcServerArgs::default().with_unused_ports().with_http().with_ws())
// The pruner runs after every persisted block, and a minimum distance of one lets it
// reach a receipts prune target this close to the head of a short chain.
.with_pruning(PruningArgs {
block_interval: Some(1),
minimum_distance: Some(1),
receipts_before: Some(RECEIPTS_PRUNED_BEFORE),
..Default::default()
});
config.network.discovery.discv5_port = Some(0);
config.network.discovery.discv5_port_ipv6 = Some(0);
// Every block is persisted as soon as it is canonical, so the pruner sees the chain's height.
config.engine.persistence_threshold = 0;
config.engine.memory_block_buffer_target = Some(0);
config.engine.num_state_masking_blocks = 0;
// Under the v2 layout, op-reth's default, the receipts of a short chain share one static file,
// which the pruner only drops whole, so the v2 pruned-receipts path is not covered here; the
// v1 layout holds them in the database, pruned block by block.
config.storage.v2 = false;
// The RPC cache would keep serving receipts the pruner removed; on a real chain the pruned
// range lies far outside its window.
config.rpc.rpc_state_cache.set_zero_lengths();

let NodeHandle { node, node_exit_future: _ } = NodeBuilder::new(config)
.testing_node(Runtime::test())
.node(OpNode::default())
.launch()
.await?;
let handles = node.add_ons_handle.rpc_server_handles().clone();
let mut driver = EngineDriver {
client: node.auth_server_handle().http_client(),
engine: node.add_ons_handle.beacon_engine_handle.clone(),
head: node.provider.chain_spec().genesis_hash(),
wallet: Wallet::default().with_chain_id(chain_id),
};

for number in 1..CHAIN_HEIGHT {
driver.build_block(number).await?;
}
let creator = driver.spare_signer();
let log_address = creator.address().create(0);
driver
.build_block_with(CHAIN_HEIGHT, vec![log_emitting_creation_tx(chain_id, &creator)])
.await?;

let last_pruned_block = RECEIPTS_PRUNED_BEFORE - 1;
wait_until("the receipts below the prune target were never pruned", || {
matches!(node.provider.receipts_by_block(last_pruned_block.into()), Ok(None))
})
.await;
assert!(
node.provider
.receipts_by_block(RECEIPTS_PRUNED_BEFORE.into())?
.is_some_and(|receipts| !receipts.is_empty()),
"the first block past the prune target must keep its receipts"
);
let last_pruned_block_hash =
node.provider.block_hash(last_pruned_block)?.expect("a pruned block keeps its header");

Ok(PrunedReceiptsNode { handles, last_pruned_block_hash, log_address, _node: node })
}

/// A contract creation whose init code emits one log and deploys nothing.
fn log_emitting_creation_tx(chain_id: u64, creator: &PrivateKeySigner) -> Bytes {
let mut creation_tx = TxEip1559 {
chain_id,
gas_limit: 100_000,
input: Bytes::from_static(&LOG_EMITTING_INIT_CODE),
max_fee_per_gas: 20_000_000_000,
max_priority_fee_per_gas: 1_000_000_000,
to: TxKind::Create,
..Default::default()
};
let signature = creator.sign_transaction_sync(&mut creation_tx).expect("the creation signs");
op_alloy_consensus::OpPooledTransaction::Eip1559(creation_tx.into_signed(signature))
.encoded_2718()
.into()
}

async fn get_logs(client: &impl ClientT, filter: Filter) -> Result<Vec<Log>, ClientError> {
client.request("eth_getLogs", rpc_params![filter]).await
}

/// Ranges the node cannot serve in full, each with the error code and exact message it answers.
fn unservable_ranges(last_pruned_block_hash: B256) -> [(&'static str, Filter, i32, String); 4] {
[
(
"fromBlock past the head",
Filter::new().from_block(CHAIN_HEIGHT + 1),
INVALID_PARAMS_CODE,
"invalid block range params".to_owned(),
),
(
"toBlock past the head",
Filter::new().from_block(1u64).to_block(CHAIN_HEIGHT + 1),
INVALID_PARAMS_CODE,
format!(
"block range extends beyond current head block: requested {}, head {CHAIN_HEIGHT}",
CHAIN_HEIGHT + 1
),
),
(
"range over pruned receipts",
Filter::new().from_block(1u64).to_block(CHAIN_HEIGHT),
PRUNED_HISTORY_UNAVAILABLE_CODE,
"pruned history unavailable".to_owned(),
),
(
"block hash with pruned receipts",
Filter::new().at_block_hash(last_pruned_block_hash),
PRUNED_HISTORY_UNAVAILABLE_CODE,
"pruned history unavailable".to_owned(),
),
]
}

#[track_caller]
fn assert_rpc_error(result: Result<Vec<Log>, ClientError>, case: &str, code: i32, message: &str) {
match result {
Err(ClientError::Call(err)) => {
assert_eq!(err.code(), code, "unexpected error code for {case}: {err:?}");
assert_eq!(err.message(), message, "unexpected error message for {case}: {err:?}");
}
other => panic!("expected error {code} `{message}` for {case}, got {other:?}"),
}
}

async fn assert_unservable_ranges_rejected(
client: &impl ClientT,
transport: &str,
last_pruned_block_hash: B256,
) {
for (range, filter, code, message) in unservable_ranges(last_pruned_block_hash) {
let result = get_logs(client, filter).await;

assert_rpc_error(result, &format!("{range} over {transport}"), code, &message);
}
}

async fn assert_kept_range_served(
client: &impl ClientT,
transport: &str,
log_address: Address,
) -> eyre::Result<()> {
let logs =
get_logs(client, Filter::new().from_block(RECEIPTS_PRUNED_BEFORE).to_block(CHAIN_HEIGHT))
.await?;

assert_eq!(
logs.len(),
1,
"the kept range over {transport} holds exactly the created contract's log: {logs:?}"
);
assert_eq!(logs[0].address(), log_address, "log address over {transport}");
assert_eq!(logs[0].block_number, Some(CHAIN_HEIGHT), "log block over {transport}");
Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn get_logs_rejects_only_ranges_it_cannot_serve_in_full() -> eyre::Result<()> {
let node = launch_pruned_receipts_node().await?;
let http_client = node.handles.rpc.http_client().expect("http is served");
let ws_client = node.handles.rpc.ws_client().await.expect("ws is served");

assert_unservable_ranges_rejected(&http_client, "http", node.last_pruned_block_hash).await;
assert_unservable_ranges_rejected(&ws_client, "ws", node.last_pruned_block_hash).await;
assert_kept_range_served(&http_client, "http", node.log_address).await?;
assert_kept_range_served(&ws_client, "ws", node.log_address).await?;
Ok(())
}
4 changes: 4 additions & 0 deletions rust/op-reth/crates/node/tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ mod derived_attributes;

mod payload_during_persistence;

mod engine_driver;

mod get_logs_errors;

const fn main() {}
Loading
Loading