Skip to content

Commit 5d21b42

Browse files
authored
Merge branch 'main' into new-approach3
2 parents 5c60cb4 + 7f40013 commit 5d21b42

File tree

102 files changed

+2567
-348
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+2567
-348
lines changed

Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cli/commands/src/db/get.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use clap::Parser;
33
use reth_db::{
44
static_file::{
55
ColumnSelectorOne, ColumnSelectorTwo, HeaderWithHashMask, ReceiptMask, TransactionMask,
6+
TransactionSenderMask,
67
},
78
RawDupSort,
89
};
@@ -75,6 +76,10 @@ impl Command {
7576
StaticFileSegment::Receipts => {
7677
(table_key::<tables::Receipts>(&key)?, <ReceiptMask<ReceiptTy<N>>>::MASK)
7778
}
79+
StaticFileSegment::TransactionSenders => (
80+
table_key::<tables::TransactionSenders>(&key)?,
81+
<TransactionSenderMask>::MASK,
82+
),
7883
};
7984

8085
let content = tool
@@ -114,6 +119,13 @@ impl Command {
114119
)?;
115120
println!("{}", serde_json::to_string_pretty(&receipt)?);
116121
}
122+
StaticFileSegment::TransactionSenders => {
123+
let sender =
124+
<<tables::TransactionSenders as Table>::Value>::decompress(
125+
content[0].as_slice(),
126+
)?;
127+
println!("{}", serde_json::to_string_pretty(&sender)?);
128+
}
117129
}
118130
}
119131
}

crates/cli/commands/src/db/settings.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ pub enum SetCommand {
4444
#[clap(action(ArgAction::Set))]
4545
value: bool,
4646
},
47+
/// Store transaction senders in static files instead of the database
48+
TransactionSendersInStaticFiles {
49+
#[clap(action(ArgAction::Set))]
50+
value: bool,
51+
},
4752
}
4853

4954
impl Command {
@@ -83,8 +88,10 @@ impl Command {
8388
println!("No storage settings found, creating new settings.");
8489
}
8590

86-
let mut settings @ StorageSettings { receipts_in_static_files: _ } =
87-
settings.unwrap_or_default();
91+
let mut settings @ StorageSettings {
92+
receipts_in_static_files: _,
93+
transaction_senders_in_static_files: _,
94+
} = settings.unwrap_or_else(StorageSettings::legacy);
8895

8996
// Update the setting based on the key
9097
match cmd {
@@ -96,6 +103,14 @@ impl Command {
96103
settings.receipts_in_static_files = value;
97104
println!("Set receipts_in_static_files = {}", value);
98105
}
106+
SetCommand::TransactionSendersInStaticFiles { value } => {
107+
if settings.transaction_senders_in_static_files == value {
108+
println!("transaction_senders_in_static_files is already set to {}", value);
109+
return Ok(());
110+
}
111+
settings.transaction_senders_in_static_files = value;
112+
println!("Set transaction_senders_in_static_files = {}", value);
113+
}
99114
}
100115

101116
// Write updated settings

crates/cli/commands/src/stage/drop.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ impl<C: ChainSpecParser> Command<C> {
4545
StageEnum::Headers => Some(StaticFileSegment::Headers),
4646
StageEnum::Bodies => Some(StaticFileSegment::Transactions),
4747
StageEnum::Execution => Some(StaticFileSegment::Receipts),
48+
StageEnum::Senders => Some(StaticFileSegment::TransactionSenders),
4849
_ => None,
4950
};
5051

@@ -68,17 +69,24 @@ impl<C: ChainSpecParser> Command<C> {
6869
StaticFileSegment::Transactions => {
6970
let to_delete = static_file_provider
7071
.get_highest_static_file_tx(static_file_segment)
71-
.map(|tx| tx + 1)
72+
.map(|tx_num| tx_num + 1)
7273
.unwrap_or_default();
7374
writer.prune_transactions(to_delete, 0)?;
7475
}
7576
StaticFileSegment::Receipts => {
7677
let to_delete = static_file_provider
7778
.get_highest_static_file_tx(static_file_segment)
78-
.map(|receipt| receipt + 1)
79+
.map(|tx_num| tx_num + 1)
7980
.unwrap_or_default();
8081
writer.prune_receipts(to_delete, 0)?;
8182
}
83+
StaticFileSegment::TransactionSenders => {
84+
let to_delete = static_file_provider
85+
.get_highest_static_file_tx(static_file_segment)
86+
.map(|tx_num| tx_num + 1)
87+
.unwrap_or_default();
88+
writer.prune_transaction_senders(to_delete, 0)?;
89+
}
8290
}
8391
}
8492
}

crates/config/src/config.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,14 +436,17 @@ pub struct BlocksPerFileConfig {
436436
pub transactions: Option<u64>,
437437
/// Number of blocks per file for the receipts segment.
438438
pub receipts: Option<u64>,
439+
/// Number of blocks per file for the transaction senders segment.
440+
pub transaction_senders: Option<u64>,
439441
}
440442

441443
impl StaticFilesConfig {
442444
/// Validates the static files configuration.
443445
///
444446
/// Returns an error if any blocks per file value is zero.
445447
pub fn validate(&self) -> eyre::Result<()> {
446-
let BlocksPerFileConfig { headers, transactions, receipts } = self.blocks_per_file;
448+
let BlocksPerFileConfig { headers, transactions, receipts, transaction_senders } =
449+
self.blocks_per_file;
447450
eyre::ensure!(headers != Some(0), "Headers segment blocks per file must be greater than 0");
448451
eyre::ensure!(
449452
transactions != Some(0),
@@ -453,12 +456,17 @@ impl StaticFilesConfig {
453456
receipts != Some(0),
454457
"Receipts segment blocks per file must be greater than 0"
455458
);
459+
eyre::ensure!(
460+
transaction_senders != Some(0),
461+
"Transaction senders segment blocks per file must be greater than 0"
462+
);
456463
Ok(())
457464
}
458465

459466
/// Converts the blocks per file configuration into a [`HashMap`] per segment.
460467
pub fn as_blocks_per_file_map(&self) -> HashMap<StaticFileSegment, u64> {
461-
let BlocksPerFileConfig { headers, transactions, receipts } = self.blocks_per_file;
468+
let BlocksPerFileConfig { headers, transactions, receipts, transaction_senders } =
469+
self.blocks_per_file;
462470

463471
let mut map = HashMap::new();
464472
// Iterating over all possible segments allows us to do an exhaustive match here,
@@ -468,6 +476,7 @@ impl StaticFilesConfig {
468476
StaticFileSegment::Headers => headers,
469477
StaticFileSegment::Transactions => transactions,
470478
StaticFileSegment::Receipts => receipts,
479+
StaticFileSegment::TransactionSenders => transaction_senders,
471480
};
472481

473482
if let Some(blocks_per_file) = blocks_per_file {

crates/engine/local/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ reth-engine-primitives = { workspace = true, features = ["std"] }
1515
reth-ethereum-engine-primitives.workspace = true
1616
reth-payload-builder.workspace = true
1717
reth-payload-primitives.workspace = true
18+
reth-primitives-traits.workspace = true
1819
reth-storage-api.workspace = true
1920
reth-transaction-pool.workspace = true
2021

@@ -43,4 +44,5 @@ op = [
4344
"dep:op-alloy-rpc-types-engine",
4445
"dep:reth-optimism-chainspec",
4546
"reth-payload-primitives/op",
47+
"reth-primitives-traits/op",
4648
]

crates/engine/local/src/miner.rs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Contains the implementation of the mining mode for the local engine.
22
3-
use alloy_consensus::BlockHeader;
43
use alloy_primitives::{TxHash, B256};
54
use alloy_rpc_types_engine::ForkchoiceState;
65
use eyre::OptionExt;
@@ -10,14 +9,15 @@ use reth_payload_builder::PayloadBuilderHandle;
109
use reth_payload_primitives::{
1110
BuiltPayload, EngineApiMessageVersion, PayloadAttributesBuilder, PayloadKind, PayloadTypes,
1211
};
12+
use reth_primitives_traits::{HeaderTy, SealedHeaderFor};
1313
use reth_storage_api::BlockReader;
1414
use reth_transaction_pool::TransactionPool;
1515
use std::{
1616
collections::VecDeque,
1717
future::Future,
1818
pin::Pin,
1919
task::{Context, Poll},
20-
time::{Duration, UNIX_EPOCH},
20+
time::Duration,
2121
};
2222
use tokio::time::Interval;
2323
use tokio_stream::wrappers::ReceiverStream;
@@ -106,36 +106,39 @@ pub struct LocalMiner<T: PayloadTypes, B, Pool: TransactionPool + Unpin> {
106106
mode: MiningMode<Pool>,
107107
/// The payload builder for the engine
108108
payload_builder: PayloadBuilderHandle<T>,
109-
/// Timestamp for the next block.
110-
last_timestamp: u64,
109+
/// Latest block in the chain so far.
110+
last_header: SealedHeaderFor<<T::BuiltPayload as BuiltPayload>::Primitives>,
111111
/// Stores latest mined blocks.
112112
last_block_hashes: VecDeque<B256>,
113113
}
114114

115115
impl<T, B, Pool> LocalMiner<T, B, Pool>
116116
where
117117
T: PayloadTypes,
118-
B: PayloadAttributesBuilder<<T as PayloadTypes>::PayloadAttributes>,
118+
B: PayloadAttributesBuilder<
119+
T::PayloadAttributes,
120+
HeaderTy<<T::BuiltPayload as BuiltPayload>::Primitives>,
121+
>,
119122
Pool: TransactionPool + Unpin,
120123
{
121124
/// Spawns a new [`LocalMiner`] with the given parameters.
122125
pub fn new(
123-
provider: impl BlockReader,
126+
provider: impl BlockReader<Header = HeaderTy<<T::BuiltPayload as BuiltPayload>::Primitives>>,
124127
payload_attributes_builder: B,
125128
to_engine: ConsensusEngineHandle<T>,
126129
mode: MiningMode<Pool>,
127130
payload_builder: PayloadBuilderHandle<T>,
128131
) -> Self {
129-
let latest_header =
132+
let last_header =
130133
provider.sealed_header(provider.best_block_number().unwrap()).unwrap().unwrap();
131134

132135
Self {
133136
payload_attributes_builder,
134137
to_engine,
135138
mode,
136139
payload_builder,
137-
last_timestamp: latest_header.timestamp(),
138-
last_block_hashes: VecDeque::from([latest_header.hash()]),
140+
last_block_hashes: VecDeque::from([last_header.hash()]),
141+
last_header,
139142
}
140143
}
141144

@@ -193,19 +196,11 @@ where
193196
/// Generates payload attributes for a new block, passes them to FCU and inserts built payload
194197
/// through newPayload.
195198
async fn advance(&mut self) -> eyre::Result<()> {
196-
let timestamp = std::cmp::max(
197-
self.last_timestamp.saturating_add(1),
198-
std::time::SystemTime::now()
199-
.duration_since(UNIX_EPOCH)
200-
.expect("cannot be earlier than UNIX_EPOCH")
201-
.as_secs(),
202-
);
203-
204199
let res = self
205200
.to_engine
206201
.fork_choice_updated(
207202
self.forkchoice_state(),
208-
Some(self.payload_attributes_builder.build(timestamp)),
203+
Some(self.payload_attributes_builder.build(&self.last_header)),
209204
EngineApiMessageVersion::default(),
210205
)
211206
.await?;
@@ -222,16 +217,16 @@ where
222217
eyre::bail!("No payload")
223218
};
224219

225-
let block = payload.block();
220+
let header = payload.block().sealed_header().clone();
226221
let payload = T::block_to_payload(payload.block().clone());
227222
let res = self.to_engine.new_payload(payload).await?;
228223

229224
if !res.is_valid() {
230225
eyre::bail!("Invalid payload")
231226
}
232227

233-
self.last_timestamp = timestamp;
234-
self.last_block_hashes.push_back(block.hash());
228+
self.last_block_hashes.push_back(header.hash());
229+
self.last_header = header;
235230
// ensure we keep at most 64 blocks
236231
if self.last_block_hashes.len() > 64 {
237232
self.last_block_hashes.pop_front();

crates/engine/local/src/payload.rs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
//! The implementation of the [`PayloadAttributesBuilder`] for the
22
//! [`LocalMiner`](super::LocalMiner).
33
4+
use alloy_consensus::BlockHeader;
45
use alloy_primitives::{Address, B256};
5-
use reth_chainspec::EthereumHardforks;
6+
use reth_chainspec::{EthChainSpec, EthereumHardforks};
67
use reth_ethereum_engine_primitives::EthPayloadAttributes;
78
use reth_payload_primitives::PayloadAttributesBuilder;
9+
use reth_primitives_traits::SealedHeader;
810
use std::sync::Arc;
911

1012
/// The attributes builder for local Ethereum payload.
@@ -13,21 +15,36 @@ use std::sync::Arc;
1315
pub struct LocalPayloadAttributesBuilder<ChainSpec> {
1416
/// The chainspec
1517
pub chain_spec: Arc<ChainSpec>,
18+
19+
/// Whether to enforce increasing timestamp.
20+
pub enforce_increasing_timestamp: bool,
1621
}
1722

1823
impl<ChainSpec> LocalPayloadAttributesBuilder<ChainSpec> {
1924
/// Creates a new instance of the builder.
2025
pub const fn new(chain_spec: Arc<ChainSpec>) -> Self {
21-
Self { chain_spec }
26+
Self { chain_spec, enforce_increasing_timestamp: true }
27+
}
28+
29+
/// Creates a new instance of the builder without enforcing increasing timestamps.
30+
pub fn without_increasing_timestamp(self) -> Self {
31+
Self { enforce_increasing_timestamp: false, ..self }
2232
}
2333
}
2434

25-
impl<ChainSpec> PayloadAttributesBuilder<EthPayloadAttributes>
35+
impl<ChainSpec> PayloadAttributesBuilder<EthPayloadAttributes, ChainSpec::Header>
2636
for LocalPayloadAttributesBuilder<ChainSpec>
2737
where
28-
ChainSpec: Send + Sync + EthereumHardforks + 'static,
38+
ChainSpec: EthChainSpec + EthereumHardforks + 'static,
2939
{
30-
fn build(&self, timestamp: u64) -> EthPayloadAttributes {
40+
fn build(&self, parent: &SealedHeader<ChainSpec::Header>) -> EthPayloadAttributes {
41+
let mut timestamp =
42+
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs();
43+
44+
if self.enforce_increasing_timestamp {
45+
timestamp = std::cmp::max(parent.timestamp().saturating_add(1), timestamp);
46+
}
47+
3148
EthPayloadAttributes {
3249
timestamp,
3350
prev_randao: B256::random(),
@@ -45,14 +62,18 @@ where
4562
}
4663

4764
#[cfg(feature = "op")]
48-
impl<ChainSpec> PayloadAttributesBuilder<op_alloy_rpc_types_engine::OpPayloadAttributes>
65+
impl<ChainSpec>
66+
PayloadAttributesBuilder<op_alloy_rpc_types_engine::OpPayloadAttributes, ChainSpec::Header>
4967
for LocalPayloadAttributesBuilder<ChainSpec>
5068
where
51-
ChainSpec: Send + Sync + EthereumHardforks + 'static,
69+
ChainSpec: EthChainSpec + EthereumHardforks + 'static,
5270
{
53-
fn build(&self, timestamp: u64) -> op_alloy_rpc_types_engine::OpPayloadAttributes {
71+
fn build(
72+
&self,
73+
parent: &SealedHeader<ChainSpec::Header>,
74+
) -> op_alloy_rpc_types_engine::OpPayloadAttributes {
5475
op_alloy_rpc_types_engine::OpPayloadAttributes {
55-
payload_attributes: self.build(timestamp),
76+
payload_attributes: self.build(parent),
5677
// Add dummy system transaction
5778
transactions: Some(vec![
5879
reth_optimism_chainspec::constants::TX_SET_L1_BLOCK_OP_MAINNET_BLOCK_124665056

crates/era-utils/src/history.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use reth_db_api::{
99
RawKey, RawTable, RawValue,
1010
};
1111
use reth_era::{
12-
common::{decode::DecodeCompressed, file_ops::StreamReader},
12+
common::{decode::DecodeCompressedRlp, file_ops::StreamReader},
1313
e2s::error::E2sError,
1414
era1::{
1515
file::{BlockTupleIterator, Era1Reader},

crates/era/src/common/decode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use alloy_rlp::Decodable;
55
use ssz::Decode;
66

77
/// Extension trait for generic decoding from compressed data
8-
pub trait DecodeCompressed {
8+
pub trait DecodeCompressedRlp {
99
/// Decompress and decode the data into the given type
1010
fn decode<T: Decodable>(&self) -> Result<T, E2sError>;
1111
}

0 commit comments

Comments
 (0)