Skip to content

Commit 6636167

Browse files
authored
Log block import source (#5738)
* the default target peers is 100 * add some comments * Merge branch 'unstable' of https://github.com/sigp/lighthouse into track-block-import-source * add block import source * revert * update logging text * fix tests * lint * use % instaed of to_string
1 parent 6f45ad4 commit 6636167

File tree

12 files changed

+74
-15
lines changed

12 files changed

+74
-15
lines changed

beacon_node/beacon_chain/src/beacon_chain.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2774,6 +2774,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
27742774
signature_verified_block.block_root(),
27752775
signature_verified_block,
27762776
notify_execution_layer,
2777+
BlockImportSource::RangeSync,
27772778
|| Ok(()),
27782779
)
27792780
.await
@@ -2956,16 +2957,21 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
29562957
self: &Arc<Self>,
29572958
block_root: Hash256,
29582959
unverified_block: B,
2960+
block_source: BlockImportSource,
29592961
notify_execution_layer: NotifyExecutionLayer,
29602962
) -> Result<AvailabilityProcessingStatus, BlockError<T::EthSpec>> {
29612963
self.reqresp_pre_import_cache
29622964
.write()
29632965
.insert(block_root, unverified_block.block_cloned());
29642966

29652967
let r = self
2966-
.process_block(block_root, unverified_block, notify_execution_layer, || {
2967-
Ok(())
2968-
})
2968+
.process_block(
2969+
block_root,
2970+
unverified_block,
2971+
notify_execution_layer,
2972+
block_source,
2973+
|| Ok(()),
2974+
)
29692975
.await;
29702976
self.remove_notified(&block_root, r)
29712977
}
@@ -2988,6 +2994,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
29882994
block_root: Hash256,
29892995
unverified_block: B,
29902996
notify_execution_layer: NotifyExecutionLayer,
2997+
block_source: BlockImportSource,
29912998
publish_fn: impl FnOnce() -> Result<(), BlockError<T::EthSpec>> + Send + 'static,
29922999
) -> Result<AvailabilityProcessingStatus, BlockError<T::EthSpec>> {
29933000
// Start the Prometheus timer.
@@ -3048,6 +3055,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
30483055
"Beacon block imported";
30493056
"block_root" => ?block_root,
30503057
"block_slot" => block_slot,
3058+
"source" => %block_source,
30513059
);
30523060

30533061
// Increment the Prometheus counter for block processing successes.

beacon_node/beacon_chain/src/test_utils.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,6 +1881,7 @@ where
18811881
block_root,
18821882
RpcBlock::new(Some(block_root), block, sidecars).unwrap(),
18831883
NotifyExecutionLayer::Yes,
1884+
BlockImportSource::RangeSync,
18841885
|| Ok(()),
18851886
)
18861887
.await?
@@ -1907,6 +1908,7 @@ where
19071908
block_root,
19081909
RpcBlock::new(Some(block_root), block, sidecars).unwrap(),
19091910
NotifyExecutionLayer::Yes,
1911+
BlockImportSource::RangeSync,
19101912
|| Ok(()),
19111913
)
19121914
.await?

beacon_node/beacon_chain/tests/block_verification.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ async fn assert_invalid_signature(
473473
)
474474
.unwrap(),
475475
NotifyExecutionLayer::Yes,
476+
BlockImportSource::Lookup,
476477
|| Ok(()),
477478
)
478479
.await;
@@ -541,6 +542,7 @@ async fn invalid_signature_gossip_block() {
541542
signed_block.canonical_root(),
542543
Arc::new(signed_block),
543544
NotifyExecutionLayer::Yes,
545+
BlockImportSource::Lookup,
544546
|| Ok(()),
545547
)
546548
.await,
@@ -875,6 +877,7 @@ async fn block_gossip_verification() {
875877
gossip_verified.block_root,
876878
gossip_verified,
877879
NotifyExecutionLayer::Yes,
880+
BlockImportSource::Lookup,
878881
|| Ok(()),
879882
)
880883
.await
@@ -1165,6 +1168,7 @@ async fn verify_block_for_gossip_slashing_detection() {
11651168
verified_block.block_root,
11661169
verified_block,
11671170
NotifyExecutionLayer::Yes,
1171+
BlockImportSource::Lookup,
11681172
|| Ok(()),
11691173
)
11701174
.await
@@ -1196,6 +1200,7 @@ async fn verify_block_for_gossip_doppelganger_detection() {
11961200
verified_block.block_root,
11971201
verified_block,
11981202
NotifyExecutionLayer::Yes,
1203+
BlockImportSource::Lookup,
11991204
|| Ok(()),
12001205
)
12011206
.await
@@ -1342,6 +1347,7 @@ async fn add_base_block_to_altair_chain() {
13421347
base_block.canonical_root(),
13431348
Arc::new(base_block.clone()),
13441349
NotifyExecutionLayer::Yes,
1350+
BlockImportSource::Lookup,
13451351
|| Ok(()),
13461352
)
13471353
.await
@@ -1477,6 +1483,7 @@ async fn add_altair_block_to_base_chain() {
14771483
altair_block.canonical_root(),
14781484
Arc::new(altair_block.clone()),
14791485
NotifyExecutionLayer::Yes,
1486+
BlockImportSource::Lookup,
14801487
|| Ok(()),
14811488
)
14821489
.await

beacon_node/beacon_chain/tests/payload_invalidation.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ async fn invalidates_all_descendants() {
702702
fork_block.canonical_root(),
703703
fork_block,
704704
NotifyExecutionLayer::Yes,
705+
BlockImportSource::Lookup,
705706
|| Ok(()),
706707
)
707708
.await
@@ -802,6 +803,7 @@ async fn switches_heads() {
802803
fork_block.canonical_root(),
803804
fork_block,
804805
NotifyExecutionLayer::Yes,
806+
BlockImportSource::Lookup,
805807
|| Ok(()),
806808
)
807809
.await
@@ -1061,7 +1063,7 @@ async fn invalid_parent() {
10611063

10621064
// Ensure the block built atop an invalid payload is invalid for import.
10631065
assert!(matches!(
1064-
rig.harness.chain.process_block(block.canonical_root(), block.clone(), NotifyExecutionLayer::Yes,
1066+
rig.harness.chain.process_block(block.canonical_root(), block.clone(), NotifyExecutionLayer::Yes, BlockImportSource::Lookup,
10651067
|| Ok(()),
10661068
).await,
10671069
Err(BlockError::ParentExecutionPayloadInvalid { parent_root: invalid_root })
@@ -1352,6 +1354,7 @@ async fn build_optimistic_chain(
13521354
block.canonical_root(),
13531355
block,
13541356
NotifyExecutionLayer::Yes,
1357+
BlockImportSource::Lookup,
13551358
|| Ok(()),
13561359
)
13571360
.await
@@ -1926,6 +1929,7 @@ async fn recover_from_invalid_head_by_importing_blocks() {
19261929
fork_block.canonical_root(),
19271930
fork_block.clone(),
19281931
NotifyExecutionLayer::Yes,
1932+
BlockImportSource::Lookup,
19291933
|| Ok(()),
19301934
)
19311935
.await

beacon_node/beacon_chain/tests/store_tests.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2458,6 +2458,7 @@ async fn weak_subjectivity_sync_test(slots: Vec<Slot>, checkpoint_slot: Slot) {
24582458
full_block.canonical_root(),
24592459
RpcBlock::new(Some(block_root), Arc::new(full_block), Some(blobs)).unwrap(),
24602460
NotifyExecutionLayer::Yes,
2461+
BlockImportSource::Lookup,
24612462
|| Ok(()),
24622463
)
24632464
.await
@@ -2676,6 +2677,7 @@ async fn process_blocks_and_attestations_for_unaligned_checkpoint() {
26762677
invalid_fork_block.canonical_root(),
26772678
invalid_fork_block.clone(),
26782679
NotifyExecutionLayer::Yes,
2680+
BlockImportSource::Lookup,
26792681
|| Ok(()),
26802682
)
26812683
.await
@@ -2689,6 +2691,7 @@ async fn process_blocks_and_attestations_for_unaligned_checkpoint() {
26892691
valid_fork_block.canonical_root(),
26902692
valid_fork_block.clone(),
26912693
NotifyExecutionLayer::Yes,
2694+
BlockImportSource::Lookup,
26922695
|| Ok(()),
26932696
)
26942697
.await

beacon_node/beacon_chain/tests/tests.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use lazy_static::lazy_static;
1212
use operation_pool::PersistedOperationPool;
1313
use state_processing::{per_slot_processing, per_slot_processing::Error as SlotProcessingError};
1414
use types::{
15-
BeaconState, BeaconStateError, EthSpec, Hash256, Keypair, MinimalEthSpec, RelativeEpoch, Slot,
15+
BeaconState, BeaconStateError, BlockImportSource, EthSpec, Hash256, Keypair, MinimalEthSpec,
16+
RelativeEpoch, Slot,
1617
};
1718

1819
// Should ideally be divisible by 3.
@@ -686,6 +687,7 @@ async fn run_skip_slot_test(skip_slots: u64) {
686687
harness_a.chain.head_snapshot().beacon_block_root,
687688
harness_a.get_head_block(),
688689
NotifyExecutionLayer::Yes,
690+
BlockImportSource::Lookup,
689691
|| Ok(()),
690692
)
691693
.await

beacon_node/http_api/src/publish_blocks.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use std::time::Duration;
1919
use tokio::sync::mpsc::UnboundedSender;
2020
use tree_hash::TreeHash;
2121
use types::{
22-
AbstractExecPayload, BeaconBlockRef, BlobSidecarList, EthSpec, ExecPayload, ExecutionBlockHash,
23-
ForkName, FullPayload, FullPayloadBellatrix, Hash256, SignedBeaconBlock,
22+
AbstractExecPayload, BeaconBlockRef, BlobSidecarList, BlockImportSource, EthSpec, ExecPayload,
23+
ExecutionBlockHash, ForkName, FullPayload, FullPayloadBellatrix, Hash256, SignedBeaconBlock,
2424
SignedBlindedBeaconBlock, VariableList,
2525
};
2626
use warp::http::StatusCode;
@@ -230,6 +230,7 @@ pub async fn publish_block<T: BeaconChainTypes, B: IntoGossipVerifiedBlockConten
230230
block_root,
231231
gossip_verified_block,
232232
NotifyExecutionLayer::Yes,
233+
BlockImportSource::HttpApi,
233234
publish_fn,
234235
))
235236
.await

beacon_node/network/src/network_beacon_processor/gossip_methods.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
3131
use store::hot_cold_store::HotColdDBError;
3232
use tokio::sync::mpsc;
3333
use types::{
34-
Attestation, AttesterSlashing, BlobSidecar, EthSpec, Hash256, IndexedAttestation,
35-
LightClientFinalityUpdate, LightClientOptimisticUpdate, ProposerSlashing,
34+
beacon_block::BlockImportSource, Attestation, AttesterSlashing, BlobSidecar, EthSpec, Hash256,
35+
IndexedAttestation, LightClientFinalityUpdate, LightClientOptimisticUpdate, ProposerSlashing,
3636
SignedAggregateAndProof, SignedBeaconBlock, SignedBlsToExecutionChange,
3737
SignedContributionAndProof, SignedVoluntaryExit, Slot, SubnetId, SyncCommitteeMessage,
3838
SyncSubnetId,
@@ -1141,9 +1141,16 @@ impl<T: BeaconChainTypes> NetworkBeaconProcessor<T> {
11411141
let block = verified_block.block.block_cloned();
11421142
let block_root = verified_block.block_root;
11431143

1144+
// TODO(block source)
1145+
11441146
let result = self
11451147
.chain
1146-
.process_block_with_early_caching(block_root, verified_block, NotifyExecutionLayer::Yes)
1148+
.process_block_with_early_caching(
1149+
block_root,
1150+
verified_block,
1151+
BlockImportSource::Gossip,
1152+
NotifyExecutionLayer::Yes,
1153+
)
11471154
.await;
11481155

11491156
match &result {

beacon_node/network/src/network_beacon_processor/sync_methods.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use store::KzgCommitment;
2424
use tokio::sync::mpsc;
2525
use types::beacon_block_body::format_kzg_commitments;
2626
use types::blob_sidecar::FixedBlobSidecarList;
27+
use types::BlockImportSource;
2728
use types::{Epoch, Hash256};
2829

2930
/// Id associated to a batch processing request, either a sync batch or a parent lookup.
@@ -153,7 +154,12 @@ impl<T: BeaconChainTypes> NetworkBeaconProcessor<T> {
153154

154155
let result = self
155156
.chain
156-
.process_block_with_early_caching(block_root, block, NotifyExecutionLayer::Yes)
157+
.process_block_with_early_caching(
158+
block_root,
159+
block,
160+
BlockImportSource::Lookup,
161+
NotifyExecutionLayer::Yes,
162+
)
157163
.await;
158164

159165
metrics::inc_counter(&metrics::BEACON_PROCESSOR_RPC_BLOCK_IMPORTED_TOTAL);

consensus/types/src/beacon_block.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use derivative::Derivative;
44
use serde::{Deserialize, Serialize};
55
use ssz::{Decode, DecodeError};
66
use ssz_derive::{Decode, Encode};
7+
use std::fmt;
78
use std::marker::PhantomData;
89
use superstruct::superstruct;
910
use test_random_derive::TestRandom;
@@ -836,6 +837,23 @@ impl<E: EthSpec, Payload: AbstractExecPayload<E>> ForkVersionDeserialize
836837
))
837838
}
838839
}
840+
pub enum BlockImportSource {
841+
Gossip,
842+
Lookup,
843+
RangeSync,
844+
HttpApi,
845+
}
846+
847+
impl fmt::Display for BlockImportSource {
848+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
849+
match self {
850+
BlockImportSource::Gossip => write!(f, "gossip"),
851+
BlockImportSource::Lookup => write!(f, "lookup"),
852+
BlockImportSource::RangeSync => write!(f, "range_sync"),
853+
BlockImportSource::HttpApi => write!(f, "http_api"),
854+
}
855+
}
856+
}
839857

840858
#[cfg(test)]
841859
mod tests {

0 commit comments

Comments
 (0)