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: Expand metrics in HTTP debug page #205

Merged
merged 8 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
106 changes: 45 additions & 61 deletions node/actors/network/src/debug_page/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::{
collections::{HashMap, HashSet},
net::SocketAddr,
sync::{atomic::Ordering, Arc},
time::{Duration, SystemTime},
time::SystemTime,
};
use tls_listener::TlsListener;
use tokio::net::TcpListener;
Expand Down Expand Up @@ -236,7 +236,7 @@ impl Server {
))
.with_paragraph(format!(
"Server address: {}",
self.network.gossip.cfg.server_addr.to_string()
self.network.gossip.cfg.server_addr
))
.with_paragraph(format!(
"Public address: {}",
Expand Down Expand Up @@ -389,16 +389,7 @@ impl Server {
.with_header(2, "Fetch queue")
.with_paragraph(format!(
"Blocks: {:?}",
self.network
.gossip
.fetch_queue
.blocks
.subscribe()
.borrow()
.keys()
.map(|x| x.0)
.collect::<Vec<_>>()
.sort()
self.network.gossip.fetch_queue.current_blocks()
));

// Attester network
Expand All @@ -409,36 +400,36 @@ impl Server {
self.network
.gossip
.attestation
.key
.clone()
.key()
.map_or("None".to_string(), |k| k.public().encode())
));

if let Some(state) = self
.network
.gossip
.attestation
.state
.state()
.subscribe()
.borrow()
.clone()
{
html = html.with_paragraph(format!(
"Batch to attest - Number: {}, Hash: {}, Genesis hash: {}",
state.info().batch_to_attest.number,
state.info().batch_to_attest.hash.encode(),
state.info().batch_to_attest.genesis.encode(),
));

html = html
.with_paragraph(format!(
"Batch to attest:\nNumber: {}, Hash: {}, Genesis hash: {}",
state.info.batch_to_attest.number,
state.info.batch_to_attest.hash.encode(),
state.info.batch_to_attest.genesis.encode(),
.with_header(2, "Attester committee")
.with_paragraph(Self::attester_table(
state.info().committee.iter(),
state.votes(),
))
.with_header(2, "Committee")
.with_paragraph(Self::attester_committee_table(state.info.committee.iter()))
.with_paragraph(format!(
"Total weight: {}",
state.info.committee.total_weight()
))
.with_header(2, "Votes")
.with_paragraph(Self::attester_votes_table(state.votes.iter()))
.with_paragraph(format!("Total weight: {}", state.total_weight));
state.info().committee.total_weight()
));
}

// Validator network
Expand Down Expand Up @@ -512,45 +503,25 @@ impl Server {
table.to_html_string()
}

fn attester_committee_table<'a>(
fn attester_table<'a>(
attesters: impl Iterator<Item = &'a attester::WeightedAttester>,
votes: &im::HashMap<attester::PublicKey, Arc<attester::Signed<attester::Batch>>>,
) -> String {
let mut table = Table::new().with_header_row(vec!["Public key", "Weight"]);
let mut table = Table::new().with_header_row(vec!["Public key", "Weight", "Voted"]);

for attester in attesters {
table.add_body_row(vec![attester.key.encode(), attester.weight.to_string()]);
}

table.to_html_string()
}

fn attester_votes_table<'a>(
votes: impl Iterator<
Item = (
&'a attester::PublicKey,
&'a Arc<attester::Signed<attester::Batch>>,
),
>,
) -> String {
let mut table = Table::new()
.with_custom_header_row(
TableRow::new()
.with_cell(TableCell::new(TableCellType::Header).with_raw("Public key"))
.with_cell(
TableCell::new(TableCellType::Header)
.with_attributes([("colspan", "3")])
.with_raw("Batch"),
),
)
.with_header_row(vec!["", "Number", "Hash", "Genesis hash"]);
let voted = if votes.contains_key(&attester.key) {
"Yes"
} else {
"No"
}
.to_string();

for (key, batch) in votes {
table.add_body_row(vec![
key.encode(),
batch.msg.number.to_string(),
batch.msg.hash.encode(),
batch.msg.genesis.encode(),
])
attester.key.encode(),
attester.weight.to_string(),
voted,
]);
}

table.to_html_string()
Expand Down Expand Up @@ -662,6 +633,19 @@ impl Server {
let minutes = (seconds % 3600) / 60;
let seconds = seconds % 60;

format!("{}d {}h {}m {}s", days, hours, minutes, seconds)
let mut components = Vec::new();

if days > 0 {
components.push(format!("{}d", days));
}
if hours > 0 {
components.push(format!("{}h", hours));
}
if minutes > 0 {
components.push(format!("{}m", minutes));
}

components.push(format!("{}s", seconds));
components.join(" ")
}
}
61 changes: 44 additions & 17 deletions node/actors/network/src/gossip/attestation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,31 @@ pub struct Info {
#[derive(Clone)]
pub struct State {
/// Info about the batch to attest and the committee.
pub info: Arc<Info>,
info: Arc<Info>,
/// Votes collected so far.
pub votes: im::HashMap<attester::PublicKey, Arc<attester::Signed<attester::Batch>>>,
votes: im::HashMap<attester::PublicKey, Arc<attester::Signed<attester::Batch>>>,
/// Total weight of the votes collected.
pub total_weight: attester::Weight,
total_weight: attester::Weight,
}

/// Diff between 2 states.
pub(crate) struct Diff {
/// New votes.
pub(crate) votes: Vec<Arc<attester::Signed<attester::Batch>>>,
/// New info, if changed.
pub(crate) info: Option<Arc<Info>>,
}
impl State {
/// Returns a reference to the `info` field.
pub fn info(&self) -> &Arc<Info> {
&self.info
}

impl Diff {
fn is_empty(&self) -> bool {
self.votes.is_empty() && self.info.is_none()
/// Returns a reference to the `votes` field.
pub fn votes(
&self,
) -> &im::HashMap<attester::PublicKey, Arc<attester::Signed<attester::Batch>>> {
&self.votes
}

/// Returns a reference to the `total_weight` field.
pub fn total_weight(&self) -> &attester::Weight {
&self.total_weight
}
}

impl State {
/// Returns a diff between `self` state and `old` state.
/// Diff contains votes which are present is `self`, but not in `old`.
fn diff(&self, old: &Option<Self>) -> Diff {
Expand Down Expand Up @@ -141,6 +144,20 @@ impl State {
}
}

/// Diff between 2 states.
pub(crate) struct Diff {
/// New votes.
pub(crate) votes: Vec<Arc<attester::Signed<attester::Batch>>>,
/// New info, if changed.
pub(crate) info: Option<Arc<Info>>,
}

impl Diff {
fn is_empty(&self) -> bool {
self.votes.is_empty() && self.info.is_none()
}
}

/// Receiver of state diffs.
pub(crate) struct DiffReceiver {
prev: Option<State>,
Expand Down Expand Up @@ -224,9 +241,9 @@ impl DiffReceiver {
pub struct Controller {
/// Key to automatically vote for batches.
/// None, if the current node is not an attester.
pub(crate) key: Option<attester::SecretKey>,
key: Option<attester::SecretKey>,
/// Internal state of the controller.
pub(crate) state: Watch<Option<State>>,
state: Watch<Option<State>>,
}

impl fmt::Debug for Controller {
Expand All @@ -247,6 +264,16 @@ impl Controller {
}
}

/// Returns a reference to the key, if it exists.
pub fn key(&self) -> Option<&attester::SecretKey> {
self.key.as_ref()
}

/// Returns a reference to the state.
pub(crate) fn state(&self) -> &Watch<Option<State>> {
&self.state
}

/// Registers metrics for this controller.
pub(crate) fn register_metrics(self: &Arc<Self>) {
metrics::Metrics::register(Arc::downgrade(self));
Expand Down
15 changes: 14 additions & 1 deletion node/actors/network/src/gossip/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub(crate) enum RequestItem {

/// Queue of block fetch request.
pub(crate) struct Queue {
pub(crate) blocks: sync::watch::Sender<BlockInner>,
blocks: sync::watch::Sender<BlockInner>,
batches: sync::watch::Sender<BatchInner>,
}

Expand All @@ -37,6 +37,19 @@ impl Default for Queue {
}

impl Queue {
/// Returns the sorted list of currently requested blocks.
pub(crate) fn current_blocks(&self) -> Vec<u64> {
let mut blocks = self
.blocks
.subscribe()
.borrow()
.keys()
.map(|x| x.0)
.collect::<Vec<_>>();
blocks.sort();
blocks
}

/// Requests a resource from peers and waits until it is stored.
/// Note: in the current implementation concurrent calls for the same resource number are
/// unsupported - second call will override the first call.
Expand Down
Loading
Loading