Skip to content

Commit

Permalink
Remove metrics feature (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
preston-evans98 authored May 22, 2023
1 parent 272990a commit 381781c
Show file tree
Hide file tree
Showing 6 changed files with 2 additions and 140 deletions.
5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ category = ["cryptography", "data-structures"]
publish = true

[features]
default = ["ics23", "std", "metrics"]
default = ["ics23", "std"]
mocks = ["dep:parking_lot"]
std = ["dep:thiserror"]
metrics = ["std", "dep:prometheus", "dep:once_cell"]

[dependencies]
anyhow = "1.0.38"
Expand All @@ -29,11 +28,9 @@ itertools = { version = "0.10.0", default-features = false }
mirai-annotations = "1.10.1"
num-derive = "0.3.3"
num-traits = "0.2.14"
once_cell = { version = "1.7.2", optional = true }
parking_lot = { version = "0.12.1", optional = true }
serde = { version = "1.0.124", features = ["derive"] }
thiserror = { version = "1.0.24", optional = true }
prometheus = { version = "0.13", optional = true }
sha2 = "0.10"
hex = "0.4"
tracing = "0.1"
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ use thiserror::Error;

mod bytes32ext;
mod iterator;
mod metrics;
mod node_type;
mod reader;
mod tree;
Expand Down
51 changes: 0 additions & 51 deletions src/metrics.rs

This file was deleted.

81 changes: 1 addition & 80 deletions src/node_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,6 @@ use proptest::prelude::*;
use proptest_derive::Arbitrary;
use serde::{Deserialize, Serialize};

#[cfg(feature = "metrics")]
use crate::metrics::{
inc_internal_encoded_bytes_metric_if_enabled, inc_leaf_encoded_bytes_metric_if_enabled,
};

/// The maximum size of a serialized internal node, in bytes.
///
/// A serialized internal node has 16 optional "child" nodes, plus 10 bytes of metadata (an optional usize and a bool)
/// Each child node is (if present) 32 bytes for the hash, 8 bytes for the optional version,
/// 9 bytes for the node type, and one byte for the outer option.
#[cfg(feature = "metrics")]
const MAX_SERIALIZED_INTERNAL_NODE_SIZE: usize = 16 * (32 + 8 + 9 + 1 + 1) + 10;

/// The size of a leaf serialized with borsh. 32 bytes each for the key hash and value hash.
#[cfg(feature = "metrics")]
const SERIALIZED_LEAF_SIZE: usize = 32 + 32;

use crate::{
types::{
nibble::{nibble_path::NibblePath, Nibble},
Expand Down Expand Up @@ -765,8 +748,7 @@ enum NodeTag {
}

/// The concrete node type of [`JellyfishMerkleTree`](crate::JellyfishMerkleTree).
#[derive(Clone, Debug, Eq, PartialEq, BorshDeserialize, Serialize, Deserialize)]
#[cfg_attr(not(feature = "metrics"), derive(BorshSerialize))]
#[derive(Clone, Debug, Eq, PartialEq, BorshSerialize, BorshDeserialize, Serialize, Deserialize)]
pub enum Node {
/// Represents `null`.
Null,
Expand All @@ -776,28 +758,6 @@ pub enum Node {
Leaf(LeafNode),
}

// Manually implement serialize to enable metrics
#[cfg(feature = "metrics")]
impl BorshSerialize for Node {
fn serialize<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
match self {
Node::Null => BorshSerialize::serialize(&NodeTag::Null, writer),
Node::Internal(internal) => {
let mut buffer = Vec::with_capacity(MAX_SERIALIZED_INTERNAL_NODE_SIZE);
BorshSerialize::serialize(&NodeTag::Internal, &mut buffer)?;
BorshSerialize::serialize(&internal, &mut buffer)?;
inc_internal_encoded_bytes_metric_if_enabled(buffer.len() as u64);
writer.write_all(&buffer)
}
Node::Leaf(leaf) => {
inc_leaf_encoded_bytes_metric_if_enabled(SERIALIZED_LEAF_SIZE as u64);
BorshSerialize::serialize(&NodeTag::Leaf, writer)?;
BorshSerialize::serialize(&leaf, writer)
}
}
}
}

impl From<InternalNode> for Node {
fn from(node: InternalNode) -> Self {
Node::Internal(node)
Expand Down Expand Up @@ -876,42 +836,3 @@ impl Node {
}
}
}

#[cfg(all(test, feature = "metrics"))]
mod tests {
use alloc::vec::Vec;
use borsh::BorshSerialize;

use super::{Children, MAX_SERIALIZED_INTERNAL_NODE_SIZE, SERIALIZED_LEAF_SIZE};

#[test]
fn serialized_leaf_size_is_correct() {
let leaf = crate::node_type::LeafNode::new(
crate::node_type::KeyHash([0; 32]),
crate::node_type::ValueHash([0; 32]),
);
let mut out = Vec::with_capacity(SERIALIZED_LEAF_SIZE);
leaf.serialize(&mut out).unwrap();
assert_eq!(out.len(), SERIALIZED_LEAF_SIZE)
}

#[test]
fn max_serialized_internal_node_size_is_correct() {
let mut children: Children = Default::default();
for nibble in 0u8..16 {
children.insert(
nibble.into(),
super::Child {
hash: [1u8; 32],
version: u64::MAX,
node_type: super::NodeType::Internal { leaf_count: 16 },
},
)
}

let internal = crate::node_type::InternalNode::new(children);
let mut out = Vec::with_capacity(MAX_SERIALIZED_INTERNAL_NODE_SIZE);
internal.serialize(&mut out).unwrap();
assert!(out.len() <= MAX_SERIALIZED_INTERNAL_NODE_SIZE);
}
}
3 changes: 0 additions & 3 deletions src/tree_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ use std::collections::{hash_map::Entry, HashMap, HashSet};
use anyhow::{bail, Result};

use crate::{
metrics::inc_storage_reads_metric_if_enabled,
node_type::{Node, NodeKey},
storage::{
NodeBatch, NodeStats, StaleNodeIndex, StaleNodeIndexBatch, TreeReader, TreeUpdateBatch,
Expand Down Expand Up @@ -196,7 +195,6 @@ where
} else if let Some(node) = self.frozen_cache.node_cache.nodes().get(node_key) {
node.clone()
} else {
inc_storage_reads_metric_if_enabled(1);
self.reader.get_node(node_key)?
})
}
Expand All @@ -209,7 +207,6 @@ where
} else if let Some(node) = self.frozen_cache.node_cache.nodes().get(node_key) {
Some(node.clone())
} else {
inc_storage_reads_metric_if_enabled(1);
self.reader.get_node_option(node_key)?
})
}
Expand Down
1 change: 0 additions & 1 deletion src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use anyhow::Result;
use borsh::{BorshDeserialize, BorshSerialize};
#[cfg(any(test, feature = "fuzzing"))]
use proptest_derive::Arbitrary;
use serde::{Deserialize, Serialize};

use crate::{
node_type::{Node, NodeKey},
Expand Down

0 comments on commit 381781c

Please sign in to comment.