mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
refactor: Reduce unnecessary LoC
By claude code
This commit is contained in:
@ -71,10 +71,10 @@ where
|
||||
let timestamp = evm_env.block_env.timestamp.saturating_to();
|
||||
|
||||
// Filter out system tx receipts
|
||||
let transactions_for_root: Vec<TransactionSigned> =
|
||||
transactions.iter().filter(|t| !is_system_transaction(t)).cloned().collect::<Vec<_>>();
|
||||
let receipts_for_root: Vec<Receipt> =
|
||||
receipts.iter().filter(|r| r.cumulative_gas_used() != 0).cloned().collect::<Vec<_>>();
|
||||
let transactions_for_root: Vec<_> =
|
||||
transactions.iter().filter(|t| !is_system_transaction(t)).cloned().collect();
|
||||
let receipts_for_root: Vec<_> =
|
||||
receipts.iter().filter(|r| r.cumulative_gas_used() != 0).cloned().collect();
|
||||
|
||||
let transactions_root = proofs::calculate_transaction_root(&transactions_for_root);
|
||||
let receipts_root = Receipt::calculate_receipt_root_no_memo(&receipts_for_root);
|
||||
@ -295,7 +295,6 @@ where
|
||||
// configure evm env based on parent block
|
||||
let mut cfg_env =
|
||||
CfgEnv::new().with_chain_id(self.chain_spec().chain().id()).with_spec(spec);
|
||||
|
||||
if let Some(blob_params) = &blob_params {
|
||||
cfg_env.set_max_blobs_per_tx(blob_params.max_blobs_per_tx);
|
||||
}
|
||||
@ -376,10 +375,6 @@ where
|
||||
block: &'a SealedBlock<BlockTy<Self::Primitives>>,
|
||||
) -> ExecutionCtxFor<'a, Self> {
|
||||
let block_body = block.body();
|
||||
let extras = HlExtras {
|
||||
read_precompile_calls: block_body.read_precompile_calls.clone(),
|
||||
highest_precompile_address: block_body.highest_precompile_address,
|
||||
};
|
||||
HlBlockExecutionCtx {
|
||||
ctx: EthBlockExecutionCtx {
|
||||
parent_hash: block.header().parent_hash,
|
||||
@ -387,7 +382,10 @@ where
|
||||
ommers: &block.body().ommers,
|
||||
withdrawals: block.body().withdrawals.as_ref().map(Cow::Borrowed),
|
||||
},
|
||||
extras,
|
||||
extras: HlExtras {
|
||||
read_precompile_calls: block_body.read_precompile_calls.clone(),
|
||||
highest_precompile_address: block_body.highest_precompile_address,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@ -403,8 +401,7 @@ where
|
||||
ommers: &[],
|
||||
withdrawals: attributes.withdrawals.map(Cow::Owned),
|
||||
},
|
||||
// TODO: hacky, double check if this is correct
|
||||
extras: HlExtras::default(),
|
||||
extras: HlExtras::default(), // TODO: hacky, double check if this is correct
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -416,10 +413,6 @@ impl ConfigureEngineEvm<HlExecutionData> for HlEvmConfig {
|
||||
|
||||
fn context_for_payload<'a>(&self, payload: &'a HlExecutionData) -> ExecutionCtxFor<'a, Self> {
|
||||
let block = &payload.0;
|
||||
let extras = HlExtras {
|
||||
read_precompile_calls: block.body.read_precompile_calls.clone(),
|
||||
highest_precompile_address: block.body.highest_precompile_address,
|
||||
};
|
||||
HlBlockExecutionCtx {
|
||||
ctx: EthBlockExecutionCtx {
|
||||
parent_hash: block.header.parent_hash,
|
||||
@ -427,7 +420,10 @@ impl ConfigureEngineEvm<HlExecutionData> for HlEvmConfig {
|
||||
ommers: &block.body.ommers,
|
||||
withdrawals: block.body.withdrawals.as_ref().map(Cow::Borrowed),
|
||||
},
|
||||
extras,
|
||||
extras: HlExtras {
|
||||
read_precompile_calls: block.body.read_precompile_calls.clone(),
|
||||
highest_precompile_address: block.body.highest_precompile_address,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -89,7 +89,6 @@ where
|
||||
/// Process a new payload and return the outcome
|
||||
fn new_payload(&self, block: BlockMsg, peer_id: PeerId) -> ImportFut {
|
||||
let engine = self.engine.clone();
|
||||
|
||||
Box::pin(async move {
|
||||
let sealed_block = block.block.0.block.clone().seal();
|
||||
let payload = HlPayloadTypes::block_to_payload(sealed_block);
|
||||
@ -107,7 +106,7 @@ where
|
||||
.into(),
|
||||
_ => None,
|
||||
},
|
||||
Err(err) => None,
|
||||
Err(_) => None,
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -117,15 +116,10 @@ where
|
||||
let engine = self.engine.clone();
|
||||
let consensus = self.consensus.clone();
|
||||
let sealed_block = block.block.0.block.clone().seal();
|
||||
let hash = sealed_block.hash();
|
||||
let number = sealed_block.number();
|
||||
let (hash, number) = (sealed_block.hash(), sealed_block.number());
|
||||
|
||||
Box::pin(async move {
|
||||
let (head_block_hash, current_hash) = match consensus.canonical_head(hash, number) {
|
||||
Ok(hash) => hash,
|
||||
Err(_) => return None,
|
||||
};
|
||||
|
||||
let (head_block_hash, _) = consensus.canonical_head(hash, number).ok()?;
|
||||
let state = ForkchoiceState {
|
||||
head_block_hash,
|
||||
safe_block_hash: head_block_hash,
|
||||
@ -146,18 +140,15 @@ where
|
||||
.into(),
|
||||
_ => None,
|
||||
},
|
||||
Err(err) => None,
|
||||
Err(_) => None,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Add a new block import task to the pending imports
|
||||
fn on_new_block(&mut self, block: BlockMsg, peer_id: PeerId) {
|
||||
let payload_fut = self.new_payload(block.clone(), peer_id);
|
||||
self.pending_imports.push(payload_fut);
|
||||
|
||||
let fcu_fut = self.update_fork_choice(block, peer_id);
|
||||
self.pending_imports.push(fcu_fut);
|
||||
self.pending_imports.push(self.new_payload(block.clone(), peer_id));
|
||||
self.pending_imports.push(self.update_fork_choice(block, peer_id));
|
||||
}
|
||||
}
|
||||
|
||||
@ -176,11 +167,9 @@ where
|
||||
}
|
||||
|
||||
// Process completed imports and send events to network
|
||||
while let Poll::Ready(Some(outcome)) = this.pending_imports.poll_next_unpin(cx) {
|
||||
if let Some(outcome) = outcome {
|
||||
if let Err(e) = this.to_network.send(BlockImportEvent::Outcome(outcome)) {
|
||||
return Poll::Ready(Err(Box::new(e)));
|
||||
}
|
||||
while let Poll::Ready(Some(Some(outcome))) = this.pending_imports.poll_next_unpin(cx) {
|
||||
if let Err(e) = this.to_network.send(BlockImportEvent::Outcome(outcome)) {
|
||||
return Poll::Ready(Err(Box::new(e)));
|
||||
}
|
||||
}
|
||||
|
||||
@ -261,15 +250,12 @@ mod tests {
|
||||
fn chain_info(&self) -> Result<ChainInfo, ProviderError> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn best_block_number(&self) -> Result<u64, ProviderError> {
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
fn last_block_number(&self) -> Result<u64, ProviderError> {
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
fn block_number(&self, _hash: B256) -> Result<Option<u64>, ProviderError> {
|
||||
Ok(None)
|
||||
}
|
||||
@ -279,7 +265,6 @@ mod tests {
|
||||
fn block_hash(&self, _number: u64) -> Result<Option<B256>, ProviderError> {
|
||||
Ok(Some(B256::ZERO))
|
||||
}
|
||||
|
||||
fn canonical_hashes_range(
|
||||
&self,
|
||||
_start: u64,
|
||||
@ -299,14 +284,12 @@ mod tests {
|
||||
fn both_valid() -> Self {
|
||||
Self { new_payload: PayloadStatusEnum::Valid, fcu: PayloadStatusEnum::Valid }
|
||||
}
|
||||
|
||||
fn invalid_new_payload() -> Self {
|
||||
Self {
|
||||
new_payload: PayloadStatusEnum::Invalid { validation_error: "test error".into() },
|
||||
fcu: PayloadStatusEnum::Valid,
|
||||
}
|
||||
}
|
||||
|
||||
fn invalid_fcu() -> Self {
|
||||
Self {
|
||||
new_payload: PayloadStatusEnum::Valid,
|
||||
@ -326,19 +309,15 @@ mod tests {
|
||||
let consensus = Arc::new(HlConsensus { provider: MockProvider });
|
||||
let (to_engine, from_engine) = mpsc::unbounded_channel();
|
||||
let engine_handle = ConsensusEngineHandle::new(to_engine);
|
||||
|
||||
handle_engine_msg(from_engine, responses).await;
|
||||
|
||||
let (to_import, from_network) = mpsc::unbounded_channel();
|
||||
let (to_network, import_outcome) = mpsc::unbounded_channel();
|
||||
|
||||
let handle = ImportHandle::new(to_import, import_outcome);
|
||||
|
||||
let service = ImportService::new(consensus, engine_handle, from_network, to_network);
|
||||
tokio::spawn(Box::pin(async move {
|
||||
service.await.unwrap();
|
||||
}));
|
||||
|
||||
Self { handle }
|
||||
}
|
||||
|
||||
|
||||
@ -69,32 +69,22 @@ mod rlp {
|
||||
|
||||
impl<'a> From<&'a HlNewBlock> for HlNewBlockHelper<'a> {
|
||||
fn from(value: &'a HlNewBlock) -> Self {
|
||||
let HlNewBlock(NewBlock {
|
||||
block:
|
||||
HlBlock {
|
||||
header,
|
||||
body:
|
||||
HlBlockBody {
|
||||
inner: BlockBody { transactions, ommers, withdrawals },
|
||||
sidecars,
|
||||
read_precompile_calls,
|
||||
highest_precompile_address,
|
||||
},
|
||||
},
|
||||
td,
|
||||
}) = value;
|
||||
|
||||
let b = &value.0.block;
|
||||
Self {
|
||||
block: BlockHelper {
|
||||
header: Cow::Borrowed(header),
|
||||
transactions: Cow::Borrowed(transactions),
|
||||
ommers: Cow::Borrowed(ommers),
|
||||
withdrawals: withdrawals.as_ref().map(Cow::Borrowed),
|
||||
header: Cow::Borrowed(&b.header),
|
||||
transactions: Cow::Borrowed(&b.body.inner.transactions),
|
||||
ommers: Cow::Borrowed(&b.body.inner.ommers),
|
||||
withdrawals: b.body.inner.withdrawals.as_ref().map(Cow::Borrowed),
|
||||
},
|
||||
td: *td,
|
||||
sidecars: sidecars.as_ref().map(Cow::Borrowed),
|
||||
read_precompile_calls: read_precompile_calls.as_ref().map(Cow::Borrowed),
|
||||
highest_precompile_address: highest_precompile_address.as_ref().map(Cow::Borrowed),
|
||||
td: value.0.td,
|
||||
sidecars: b.body.sidecars.as_ref().map(Cow::Borrowed),
|
||||
read_precompile_calls: b.body.read_precompile_calls.as_ref().map(Cow::Borrowed),
|
||||
highest_precompile_address: b
|
||||
.body
|
||||
.highest_precompile_address
|
||||
.as_ref()
|
||||
.map(Cow::Borrowed),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -111,30 +101,24 @@ mod rlp {
|
||||
|
||||
impl Decodable for HlNewBlock {
|
||||
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
|
||||
let HlNewBlockHelper {
|
||||
block: BlockHelper { header, transactions, ommers, withdrawals },
|
||||
td,
|
||||
sidecars,
|
||||
read_precompile_calls,
|
||||
highest_precompile_address,
|
||||
} = HlNewBlockHelper::decode(buf)?;
|
||||
|
||||
let h = HlNewBlockHelper::decode(buf)?;
|
||||
Ok(HlNewBlock(NewBlock {
|
||||
block: HlBlock {
|
||||
header: header.into_owned(),
|
||||
header: h.block.header.into_owned(),
|
||||
body: HlBlockBody {
|
||||
inner: BlockBody {
|
||||
transactions: transactions.into_owned(),
|
||||
ommers: ommers.into_owned(),
|
||||
withdrawals: withdrawals.map(|w| w.into_owned()),
|
||||
transactions: h.block.transactions.into_owned(),
|
||||
ommers: h.block.ommers.into_owned(),
|
||||
withdrawals: h.block.withdrawals.map(|w| w.into_owned()),
|
||||
},
|
||||
sidecars: sidecars.map(|s| s.into_owned()),
|
||||
read_precompile_calls: read_precompile_calls.map(|s| s.into_owned()),
|
||||
highest_precompile_address: highest_precompile_address
|
||||
sidecars: h.sidecars.map(|s| s.into_owned()),
|
||||
read_precompile_calls: h.read_precompile_calls.map(|s| s.into_owned()),
|
||||
highest_precompile_address: h
|
||||
.highest_precompile_address
|
||||
.map(|s| s.into_owned()),
|
||||
},
|
||||
},
|
||||
td,
|
||||
td: h.td,
|
||||
}))
|
||||
}
|
||||
}
|
||||
@ -172,41 +156,32 @@ impl HlNetworkBuilder {
|
||||
where
|
||||
Node: FullNodeTypes<Types = HlNode>,
|
||||
{
|
||||
let Self { engine_handle_rx, .. } = self;
|
||||
|
||||
let network_builder = ctx.network_config_builder()?;
|
||||
|
||||
let (to_import, from_network) = mpsc::unbounded_channel();
|
||||
let (to_network, import_outcome) = mpsc::unbounded_channel();
|
||||
|
||||
let handle = ImportHandle::new(to_import, import_outcome);
|
||||
let consensus = Arc::new(HlConsensus { provider: ctx.provider().clone() });
|
||||
|
||||
ctx.task_executor().spawn_critical("block import", async move {
|
||||
let handle = engine_handle_rx
|
||||
let handle = self
|
||||
.engine_handle_rx
|
||||
.lock()
|
||||
.await
|
||||
.take()
|
||||
.expect("node should only be launched once")
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
ImportService::new(consensus, handle, from_network, to_network).await.unwrap();
|
||||
});
|
||||
|
||||
let network_builder = network_builder
|
||||
.disable_dns_discovery()
|
||||
.disable_nat()
|
||||
.boot_nodes(boot_nodes())
|
||||
.set_head(ctx.head())
|
||||
.with_pow()
|
||||
.block_import(Box::new(HlBlockImport::new(handle)));
|
||||
// .discovery(discv4)
|
||||
// .eth_rlpx_handshake(Arc::new(HlHandshake::default()));
|
||||
|
||||
let network_config = ctx.build_network_config(network_builder);
|
||||
|
||||
Ok(network_config)
|
||||
Ok(ctx.build_network_config(
|
||||
ctx.network_config_builder()?
|
||||
.disable_dns_discovery()
|
||||
.disable_nat()
|
||||
.boot_nodes(boot_nodes())
|
||||
.set_head(ctx.head())
|
||||
.with_pow()
|
||||
.block_import(Box::new(HlBlockImport::new(handle))),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@ -229,11 +204,9 @@ where
|
||||
pool: Pool,
|
||||
) -> eyre::Result<Self::Network> {
|
||||
let block_source_config = self.block_source_config.clone();
|
||||
let network_config = self.network_config(ctx)?;
|
||||
let network = NetworkManager::builder(network_config).await?;
|
||||
let handle = ctx.start_network(network, pool);
|
||||
let handle =
|
||||
ctx.start_network(NetworkManager::builder(self.network_config(ctx)?).await?, pool);
|
||||
let local_node_record = handle.local_node_record();
|
||||
let chain_spec = ctx.chain_spec();
|
||||
info!(target: "reth::cli", enode=%local_node_record, "P2P networking initialized");
|
||||
|
||||
let next_block_number = ctx
|
||||
@ -243,12 +216,17 @@ where
|
||||
.block_number +
|
||||
1;
|
||||
|
||||
let chain_spec = ctx.chain_spec();
|
||||
ctx.task_executor().spawn_critical("pseudo peer", async move {
|
||||
let block_source =
|
||||
block_source_config.create_cached_block_source((&*chain_spec).clone(), next_block_number).await;
|
||||
start_pseudo_peer(chain_spec, local_node_record.to_string(), block_source)
|
||||
.await
|
||||
.unwrap();
|
||||
start_pseudo_peer(
|
||||
chain_spec.clone(),
|
||||
local_node_record.to_string(),
|
||||
block_source_config
|
||||
.create_cached_block_source((*chain_spec).clone(), next_block_number)
|
||||
.await,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
});
|
||||
|
||||
Ok(handle)
|
||||
|
||||
@ -68,19 +68,15 @@ impl BlockBodyTrait for HlBlockBody {
|
||||
fn transactions(&self) -> &[Self::Transaction] {
|
||||
BlockBodyTrait::transactions(&self.inner)
|
||||
}
|
||||
|
||||
fn into_ethereum_body(self) -> BlockBody {
|
||||
self.inner
|
||||
}
|
||||
|
||||
fn into_transactions(self) -> Vec<Self::Transaction> {
|
||||
self.inner.into_transactions()
|
||||
}
|
||||
|
||||
fn withdrawals(&self) -> Option<&alloy_rpc_types::Withdrawals> {
|
||||
self.inner.withdrawals()
|
||||
}
|
||||
|
||||
fn ommers(&self) -> Option<&[Self::OmmerHeader]> {
|
||||
self.inner.ommers()
|
||||
}
|
||||
@ -116,15 +112,12 @@ impl Block for HlBlock {
|
||||
fn new(header: Self::Header, body: Self::Body) -> Self {
|
||||
Self { header, body }
|
||||
}
|
||||
|
||||
fn header(&self) -> &Self::Header {
|
||||
&self.header
|
||||
}
|
||||
|
||||
fn body(&self) -> &Self::Body {
|
||||
&self.body
|
||||
}
|
||||
|
||||
fn split(self) -> (Self::Header, Self::Body) {
|
||||
(self.header, self.body)
|
||||
}
|
||||
@ -179,7 +172,6 @@ mod rlp {
|
||||
read_precompile_calls,
|
||||
highest_precompile_address,
|
||||
} = value;
|
||||
|
||||
Self {
|
||||
transactions: Cow::Borrowed(transactions),
|
||||
ommers: Cow::Borrowed(ommers),
|
||||
@ -203,7 +195,6 @@ mod rlp {
|
||||
highest_precompile_address,
|
||||
},
|
||||
} = value;
|
||||
|
||||
Self {
|
||||
header: Cow::Borrowed(header),
|
||||
transactions: Cow::Borrowed(transactions),
|
||||
@ -220,7 +211,6 @@ mod rlp {
|
||||
fn encode(&self, out: &mut dyn bytes::BufMut) {
|
||||
BlockBodyHelper::from(self).encode(out);
|
||||
}
|
||||
|
||||
fn length(&self) -> usize {
|
||||
BlockBodyHelper::from(self).length()
|
||||
}
|
||||
@ -253,7 +243,6 @@ mod rlp {
|
||||
fn encode(&self, out: &mut dyn bytes::BufMut) {
|
||||
BlockHelper::from(self).encode(out);
|
||||
}
|
||||
|
||||
fn length(&self) -> usize {
|
||||
BlockHelper::from(self).length()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user