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: Implement eth68 announcement metrics, track entries by TxType #6786

Merged
merged 8 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
60 changes: 60 additions & 0 deletions crates/net/network/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use metrics::Histogram;
use reth_eth_wire::DisconnectReason;
use reth_metrics::{
metrics::{Counter, Gauge},
Metrics,
};
use reth_primitives::TxType;

/// Scope for monitoring transactions sent from the manager to the tx manager
pub(crate) const NETWORK_POOL_TRANSACTIONS_SCOPE: &str = "network.pool.transactions";
Expand Down Expand Up @@ -244,3 +246,61 @@ pub struct EthRequestHandlerMetrics {
/// Number of received bodies requests
pub(crate) received_bodies_requests: Counter,
}

/// Eth67 announcement metrics, track entries by TxType
#[derive(Metrics)]
#[metrics(scope = "network.transaction_fetcher")]
pub struct AnnouncedTxTypesMetrics {
/// Histogram for tracking frequency of legacy transaction type
pub(crate) legacy: Histogram,

/// Histogram for tracking frequency of EIP-2930 transaction type
pub(crate) eip2930: Histogram,

/// Histogram for tracking frequency of EIP-1559 transaction type
pub(crate) eip1559: Histogram,

/// Histogram for tracking frequency of EIP-4844 transaction type
pub(crate) eip4844: Histogram,
}

#[derive(Debug, Default)]
pub struct TxTypesCounter {
pub(crate) legacy: usize,
pub(crate) eip2930: usize,
pub(crate) eip1559: usize,
pub(crate) eip4844: usize,
}

impl TxTypesCounter {
pub(crate) fn increase_by_tx_type(&mut self, tx_type: Option<TxType>) {
if let Some(tx_type) = tx_type {
match tx_type {
TxType::Legacy => {
self.legacy += 1;
}
TxType::EIP2930 => {
self.eip2930 += 1;
}
TxType::EIP1559 => {
self.eip1559 += 1;
}
TxType::EIP4844 => {
self.eip4844 += 1;
}
_ => {}
}
}
}
}

impl AnnouncedTxTypesMetrics {
/// Update metrics during announcement validation, by examining each announcement entry based on
/// TxType
pub(crate) fn update_eth68_announcement_metrics(&self, tx_types_counter: TxTypesCounter) {
self.legacy.record(tx_types_counter.legacy as f64);
self.eip2930.record(tx_types_counter.eip2930 as f64);
self.eip1559.record(tx_types_counter.eip1559 as f64);
self.eip4844.record(tx_types_counter.eip4844 as f64);
}
}
3 changes: 3 additions & 0 deletions crates/net/network/src/transactions/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
message::PeerRequest,
};

use crate::metrics::AnnouncedTxTypesMetrics;
use derive_more::Constructor;
use futures::{stream::FuturesUnordered, Future, FutureExt, Stream, StreamExt};
use pin_project::pin_project;
Expand Down Expand Up @@ -56,6 +57,7 @@ pub(crate) struct TransactionFetcher {
pub(super) filter_valid_hashes: AnnouncementFilter,
/// Info on capacity of the transaction fetcher.
pub(super) info: TransactionFetcherInfo,
pub(super) announced_tx_types_metrics: AnnouncedTxTypesMetrics,
}

// === impl TransactionFetcher ===
Expand Down Expand Up @@ -896,6 +898,7 @@ impl Default for TransactionFetcher {
hashes_fetch_inflight_and_pending_fetch: LruMap::new_unlimited(),
filter_valid_hashes: Default::default(),
info: TransactionFetcherInfo::default(),
announced_tx_types_metrics: AnnouncedTxTypesMetrics::default(),
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion crates/net/network/src/transactions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,12 +703,15 @@ where
let mut valid_announcement_data = match msg {
NewPooledTransactionHashes::Eth68(eth68_msg) => {
// validate eth68 announcement data
let (outcome, valid_data) =
let (outcome, valid_data, tx_types_counter) =
self.transaction_fetcher.filter_valid_hashes.filter_valid_entries_68(eth68_msg);

if let FilterOutcome::ReportPeer = outcome {
self.report_peer(peer_id, ReputationChangeKind::BadAnnouncement);
}
self.transaction_fetcher
.announced_tx_types_metrics
.update_eth68_announcement_metrics(tx_types_counter);

valid_data
}
Expand Down
51 changes: 37 additions & 14 deletions crates/net/network/src/transactions/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use std::{collections::HashMap, mem};

use crate::metrics::TxTypesCounter;
use derive_more::{Deref, DerefMut, Display};
use itertools::izip;
use reth_eth_wire::{
Expand All @@ -18,9 +19,14 @@ pub const SIGNATURE_DECODED_SIZE_BYTES: usize = mem::size_of::<Signature>();
/// Interface for validating a `(ty, size, hash)` tuple from a [`NewPooledTransactionHashes68`].
pub trait ValidateTx68 {
/// Validates a [`NewPooledTransactionHashes68`] entry. Returns [`ValidationOutcome`] which
/// signals to the caller wether to fetch the transaction or wether to drop it, and wether the
/// sender of the announcement should be penalized.
fn should_fetch(&self, ty: u8, hash: TxHash, size: usize) -> ValidationOutcome;
/// signals to the caller whether to fetch the transaction or whether to drop it, and weather
/// the sender of the announcement should be penalized.
fn should_fetch(
&self,
ty: u8,
hash: TxHash,
size: usize,
) -> (ValidationOutcome, Option<TxType>);

/// Returns the reasonable maximum encoded transaction length configured for this network, if
/// any. This property is not spec'ed out but can be inferred by looking how much data can be
Expand Down Expand Up @@ -65,7 +71,7 @@ pub trait FilterAnnouncement {
fn filter_valid_entries_68(
&self,
msg: NewPooledTransactionHashes68,
) -> (FilterOutcome, ValidAnnouncementData)
) -> (FilterOutcome, ValidAnnouncementData, TxTypesCounter)
where
Self: ValidateTx68;

Expand Down Expand Up @@ -101,7 +107,12 @@ pub struct AnnouncementFilter<N = EthAnnouncementFilter>(N);
pub struct EthAnnouncementFilter;

impl ValidateTx68 for EthAnnouncementFilter {
fn should_fetch(&self, ty: u8, hash: TxHash, size: usize) -> ValidationOutcome {
fn should_fetch(
&self,
ty: u8,
hash: TxHash,
size: usize,
) -> (ValidationOutcome, Option<TxType>) {
//
// 1. checks if tx type is valid value for this network
//
Expand All @@ -116,7 +127,7 @@ impl ValidateTx68 for EthAnnouncementFilter {
"invalid tx type in eth68 announcement"
);

return ValidationOutcome::ReportPeer
return (ValidationOutcome::ReportPeer, None)
}
};

Expand All @@ -137,7 +148,7 @@ impl ValidateTx68 for EthAnnouncementFilter {
"invalid tx size in eth68 announcement"
);

return ValidationOutcome::Ignore
return (ValidationOutcome::Ignore, Some(tx_type))
}
}
if let Some(reasonable_min_encoded_tx_length) = self.min_encoded_tx_length(tx_type) {
Expand Down Expand Up @@ -174,7 +185,7 @@ impl ValidateTx68 for EthAnnouncementFilter {
}
}

ValidationOutcome::Fetch
(ValidationOutcome::Fetch, Some(tx_type))
}

fn max_encoded_tx_length(&self, ty: TxType) -> Option<usize> {
Expand Down Expand Up @@ -208,7 +219,7 @@ impl FilterAnnouncement for EthAnnouncementFilter {
fn filter_valid_entries_68(
&self,
msg: NewPooledTransactionHashes68,
) -> (FilterOutcome, ValidAnnouncementData)
) -> (FilterOutcome, ValidAnnouncementData, TxTypesCounter)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@emhane Where would you like to put an instance of TxTypesCounter? I am a little confused about TxTypesCounter, let me know if this is not you want.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np. only the file with metrics and the validation file should have to change. create a new instance of TxTypesCounter here

pass it as parameter to should_fetch

fn should_fetch(&self, ty: u8, hash: &TxHash, size: usize, ty_counter: &mut TxTypesCounter) -> ValidationOutcome;

update it here

update metrics before this line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your detailed instructions. Based on your guidance, it seems I should incorporate AnnouncedTxTypesMetrics as a field within EthAnnouncementFilter, rather than placing it within TransactionFetcher. PTAL again.

where
Self: ValidateTx68,
{
Expand All @@ -229,6 +240,8 @@ impl FilterAnnouncement for EthAnnouncementFilter {
`%sizes: {sizes:?}`"
);

let mut tx_types_counter = TxTypesCounter::default();

//
// 1. checks if the announcement is empty
//
Expand All @@ -237,7 +250,11 @@ impl FilterAnnouncement for EthAnnouncementFilter {
network=%Self,
"empty eth68 announcement"
);
return (FilterOutcome::ReportPeer, ValidAnnouncementData::empty_eth68())
return (
FilterOutcome::ReportPeer,
ValidAnnouncementData::empty_eth68(),
tx_types_counter,
)
}

let mut should_report_peer = false;
Expand All @@ -251,9 +268,14 @@ impl FilterAnnouncement for EthAnnouncementFilter {
//
for (i, (&ty, &hash, &size)) in izip!(&types, &hashes, &sizes).enumerate() {
match self.should_fetch(ty, hash, size) {
ValidationOutcome::Fetch => (),
ValidationOutcome::Ignore => indices_to_remove.push(i),
ValidationOutcome::ReportPeer => {
(ValidationOutcome::Fetch, tx_type) => {
tx_types_counter.increase_by_tx_type(tx_type);
}
(ValidationOutcome::Ignore, tx_type) => {
indices_to_remove.push(i);
tx_types_counter.increase_by_tx_type(tx_type);
}
(ValidationOutcome::ReportPeer, _) => {
indices_to_remove.push(i);
should_report_peer = true;
}
Expand Down Expand Up @@ -285,6 +307,7 @@ impl FilterAnnouncement for EthAnnouncementFilter {
(
if should_report_peer { FilterOutcome::ReportPeer } else { FilterOutcome::Ok },
ValidAnnouncementData::new_eth68(deduped_data),
tx_types_counter,
)
}

Expand Down Expand Up @@ -349,7 +372,7 @@ mod test {

let filter = EthAnnouncementFilter;

let (outcome, _data) = filter.filter_valid_entries_68(announcement);
let (outcome, _data, _tx_types_counter) = filter.filter_valid_entries_68(announcement);

assert_eq!(outcome, FilterOutcome::ReportPeer);
}
Expand Down
Loading