Skip to content

Commit

Permalink
Add lighthouse db command (#3129)
Browse files Browse the repository at this point in the history
## Proposed Changes

Add a `lighthouse db` command with three initial subcommands:

- `lighthouse db version`: print the database schema version.
- `lighthouse db migrate --to N`: manually upgrade (or downgrade!) the database to a different version.
- `lighthouse db inspect --column C`: log the key and size in bytes of every value in a given `DBColumn`.

This PR lays the groundwork for other changes, namely:

- Mark's fast-deposit sync (#2915), for which I think we should implement a database downgrade (from v9 to v8).
- My `tree-states` work, which already implements a downgrade (v10 to v8).
- Standalone purge commands like `lighthouse db purge-dht` per #2824.

## Additional Info

I updated the `strum` crate to 0.24.0, which necessitated some changes in the network code to remove calls to deprecated methods.

Thanks to @winksaville for the motivation, and implementation work that I used as a source of inspiration (#2685).
  • Loading branch information
michaelsproul committed Apr 1, 2022
1 parent ea78336 commit 06b6466
Show file tree
Hide file tree
Showing 25 changed files with 446 additions and 86 deletions.
31 changes: 26 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ members = [
"beacon_node/store",
"beacon_node/timer",

"boot_node",
"boot_node",

"common/account_utils",
"common/clap_utils",
Expand Down Expand Up @@ -45,6 +45,8 @@ members = [
"common/fallback",
"common/monitoring_api",

"database_manager",

"consensus/cached_tree_hash",
"consensus/int_to_bytes",
"consensus/fork_choice",
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ derivative = "2.1.1"
itertools = "0.10.0"
slasher = { path = "../../slasher" }
eth2 = { path = "../../common/eth2" }
strum = { version = "0.21.0", features = ["derive"] }
strum = { version = "0.24.0", features = ["derive"] }
logging = { path = "../../common/logging" }
execution_layer = { path = "../execution_layer" }
sensitive_url = { path = "../../common/sensitive_url" }
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/client/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl Config {
/// For more information, see:
///
/// https://github.com/sigp/lighthouse/pull/2843
fn get_data_dir(&self) -> PathBuf {
pub fn get_data_dir(&self) -> PathBuf {
let existing_legacy_dir = self.get_existing_legacy_data_dir();

if let Some(legacy_dir) = existing_legacy_dir {
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/execution_layer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ tempfile = "3.1.0"
rand = "0.7.3"
zeroize = { version = "1.4.2", features = ["zeroize_derive"] }
lighthouse_metrics = { path = "../../common/lighthouse_metrics" }
lazy_static = "1.4.0"
lazy_static = "1.4.0"
2 changes: 1 addition & 1 deletion beacon_node/lighthouse_network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ task_executor = { path = "../../common/task_executor" }
rand = "0.7.3"
directory = { path = "../../common/directory" }
regex = "1.5.5"
strum = { version = "0.21.0", features = ["derive"] }
strum = { version = "0.24.0", features = ["derive"] }
superstruct = "0.4.1"
prometheus-client = "0.15.0"
unused_port = { path = "../../common/unused_port" }
Expand Down
11 changes: 4 additions & 7 deletions beacon_node/lighthouse_network/src/peer_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ impl<TSpec: EthSpec> PeerManager<TSpec> {
/// Updates `PeerInfo` with `identify` information.
pub fn identify(&mut self, peer_id: &PeerId, info: &IdentifyInfo) {
if let Some(peer_info) = self.network_globals.peers.write().peer_info_mut(peer_id) {
let previous_kind = peer_info.client().kind.clone();
let previous_kind = peer_info.client().kind;
let previous_listening_addresses =
peer_info.set_listening_addresses(info.listen_addrs.clone());
peer_info.set_client(peerdb::client::Client::from_identify_info(info));
Expand All @@ -412,12 +412,9 @@ impl<TSpec: EthSpec> PeerManager<TSpec> {
) {
metrics::inc_gauge_vec(
&metrics::PEERS_PER_CLIENT,
&[&peer_info.client().kind.to_string()],
);
metrics::dec_gauge_vec(
&metrics::PEERS_PER_CLIENT,
&[&previous_kind.to_string()],
&[peer_info.client().kind.as_ref()],
);
metrics::dec_gauge_vec(&metrics::PEERS_PER_CLIENT, &[previous_kind.as_ref()]);
}
}
} else {
Expand Down Expand Up @@ -674,7 +671,7 @@ impl<TSpec: EthSpec> PeerManager<TSpec> {
let value = clients_per_peer.get(&client_kind.to_string()).unwrap_or(&0);
metrics::set_gauge_vec(
&metrics::PEERS_PER_CLIENT,
&[&client_kind.to_string()],
&[client_kind.as_ref()],
*value as i64,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use libp2p::identify::IdentifyInfo;
use serde::Serialize;
use strum::{AsRefStr, AsStaticStr, EnumIter};
use strum::{AsRefStr, EnumIter, IntoStaticStr};

/// Various client and protocol information related to a node.
#[derive(Clone, Debug, Serialize)]
Expand All @@ -21,7 +21,7 @@ pub struct Client {
pub agent_string: Option<String>,
}

#[derive(Clone, Debug, Serialize, PartialEq, AsRefStr, AsStaticStr, EnumIter)]
#[derive(Clone, Copy, Debug, Serialize, PartialEq, AsRefStr, IntoStaticStr, EnumIter)]
pub enum ClientKind {
/// A lighthouse node (the best kind).
Lighthouse,
Expand Down
4 changes: 2 additions & 2 deletions beacon_node/lighthouse_network/src/rpc/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use ssz_types::{
VariableList,
};
use std::ops::Deref;
use strum::AsStaticStr;
use strum::IntoStaticStr;
use superstruct::superstruct;
use types::{Epoch, EthSpec, Hash256, SignedBeaconBlock, Slot};

Expand Down Expand Up @@ -263,7 +263,7 @@ pub enum RPCCodedResponse<T: EthSpec> {
}

/// The code assigned to an erroneous `RPCResponse`.
#[derive(Debug, Clone, Copy, PartialEq, AsStaticStr)]
#[derive(Debug, Clone, Copy, PartialEq, IntoStaticStr)]
#[strum(serialize_all = "snake_case")]
pub enum RPCResponseErrorCode {
RateLimited,
Expand Down
8 changes: 4 additions & 4 deletions beacon_node/lighthouse_network/src/rpc/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::io;
use std::marker::PhantomData;
use std::sync::Arc;
use std::time::Duration;
use strum::{AsStaticRef, AsStaticStr};
use strum::IntoStaticStr;
use tokio_io_timeout::TimeoutStream;
use tokio_util::{
codec::Framed,
Expand Down Expand Up @@ -510,7 +510,7 @@ impl<TSpec: EthSpec> InboundRequest<TSpec> {
}

/// Error in RPC Encoding/Decoding.
#[derive(Debug, Clone, PartialEq, AsStaticStr)]
#[derive(Debug, Clone, PartialEq, IntoStaticStr)]
#[strum(serialize_all = "snake_case")]
pub enum RPCError {
/// Error when decoding the raw buffer from ssz.
Expand Down Expand Up @@ -617,8 +617,8 @@ impl RPCError {
/// Used for metrics.
pub fn as_static_str(&self) -> &'static str {
match self {
RPCError::ErrorResponse(ref code, ..) => code.as_static(),
e => e.as_static(),
RPCError::ErrorResponse(ref code, ..) => code.into(),
e => e.into(),
}
}
}
2 changes: 1 addition & 1 deletion beacon_node/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ itertools = "0.10.0"
num_cpus = "1.13.0"
lru_cache = { path = "../../common/lru_cache" }
if-addrs = "0.6.4"
strum = "0.21.0"
strum = "0.24.0"
tokio-util = { version = "0.6.3", features = ["time"] }
9 changes: 4 additions & 5 deletions beacon_node/network/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use lighthouse_network::{
Gossipsub, NetworkGlobals,
};
use std::sync::Arc;
use strum::AsStaticRef;
use strum::IntoEnumIterator;
use types::EthSpec;

Expand Down Expand Up @@ -357,12 +356,12 @@ pub fn update_gossip_metrics<T: EthSpec>(
for client_kind in ClientKind::iter() {
set_gauge_vec(
&BEACON_BLOCK_MESH_PEERS_PER_CLIENT,
&[&client_kind.to_string()],
&[client_kind.as_ref()],
0_i64,
);
set_gauge_vec(
&BEACON_AGGREGATE_AND_PROOF_MESH_PEERS_PER_CLIENT,
&[&client_kind.to_string()],
&[client_kind.as_ref()],
0_i64,
);
}
Expand All @@ -377,7 +376,7 @@ pub fn update_gossip_metrics<T: EthSpec>(
.peers
.read()
.peer_info(peer_id)
.map(|peer_info| peer_info.client().kind.as_static())
.map(|peer_info| peer_info.client().kind.into())
.unwrap_or_else(|| "Unknown");
if let Some(v) =
get_int_gauge(&BEACON_BLOCK_MESH_PEERS_PER_CLIENT, &[client])
Expand All @@ -392,7 +391,7 @@ pub fn update_gossip_metrics<T: EthSpec>(
.peers
.read()
.peer_info(peer_id)
.map(|peer_info| peer_info.client().kind.as_static())
.map(|peer_info| peer_info.client().kind.into())
.unwrap_or_else(|| "Unknown");
if let Some(v) = get_int_gauge(
&BEACON_AGGREGATE_AND_PROOF_MESH_PEERS_PER_CLIENT,
Expand Down
14 changes: 9 additions & 5 deletions beacon_node/network/src/sync/block_lookups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use lru_cache::LRUCache;
use slog::{crit, debug, error, trace, warn, Logger};
use smallvec::SmallVec;
use store::{Hash256, SignedBeaconBlock};
use strum::AsStaticRef;
use tokio::sync::mpsc;

use crate::beacon_processor::{ChainSegmentProcessId, WorkEvent};
Expand Down Expand Up @@ -176,7 +175,7 @@ impl<T: BeaconChainTypes> BlockLookups<T> {
// request finished correctly, it will be removed after the block is processed.
}
Err(error) => {
let msg: &str = error.as_static();
let msg: &str = error.into();
cx.report_peer(peer_id, PeerAction::LowToleranceError, msg);
// Remove the request, if it can be retried it will be added with a new id.
let mut req = request.remove();
Expand Down Expand Up @@ -243,7 +242,7 @@ impl<T: BeaconChainTypes> BlockLookups<T> {
VerifyError::RootMismatch
| VerifyError::NoBlockReturned
| VerifyError::ExtraBlocksReturned => {
let e = e.as_static();
let e = e.into();
warn!(self.log, "Peer sent invalid response to parent request.";
"peer_id" => %peer_id, "reason" => e);

Expand Down Expand Up @@ -310,8 +309,13 @@ impl<T: BeaconChainTypes> BlockLookups<T> {
}
}
Err(e) => {
trace!(self.log, "Single block request failed on peer disconnection";
"block_root" => %req.hash, "peer_id" => %peer_id, "reason" => e.as_static());
trace!(
self.log,
"Single block request failed on peer disconnection";
"block_root" => %req.hash,
"peer_id" => %peer_id,
"reason" => <&str>::from(e),
);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions beacon_node/network/src/sync/block_lookups/parent_lookup.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use lighthouse_network::PeerId;
use store::{EthSpec, Hash256, SignedBeaconBlock};
use strum::AsStaticStr;
use strum::IntoStaticStr;

use crate::sync::{
manager::{Id, SLOT_IMPORT_TOLERANCE},
Expand Down Expand Up @@ -28,7 +28,7 @@ pub(crate) struct ParentLookup<T: EthSpec> {
current_parent_request_id: Option<Id>,
}

#[derive(Debug, PartialEq, Eq, AsStaticStr)]
#[derive(Debug, PartialEq, Eq, IntoStaticStr)]
pub enum VerifyError {
RootMismatch,
NoBlockReturned,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use lighthouse_network::{rpc::BlocksByRootRequest, PeerId};
use rand::seq::IteratorRandom;
use ssz_types::VariableList;
use store::{EthSpec, Hash256, SignedBeaconBlock};
use strum::AsStaticStr;
use strum::IntoStaticStr;

/// Object representing a single block lookup request.
#[derive(PartialEq, Eq)]
Expand All @@ -28,14 +28,14 @@ pub enum State {
Processing { peer_id: PeerId },
}

#[derive(Debug, PartialEq, Eq, AsStaticStr)]
#[derive(Debug, PartialEq, Eq, IntoStaticStr)]
pub enum VerifyError {
RootMismatch,
NoBlockReturned,
ExtraBlocksReturned,
}

#[derive(Debug, PartialEq, Eq, AsStaticStr)]
#[derive(Debug, PartialEq, Eq, IntoStaticStr)]
pub enum LookupRequestError {
TooManyAttempts,
NoPeers,
Expand Down
25 changes: 15 additions & 10 deletions beacon_node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,16 +284,7 @@ pub fn get_config<E: EthSpec>(
client_config.freezer_db_path = Some(PathBuf::from(freezer_dir));
}

if let Some(slots_per_restore_point) = cli_args.value_of("slots-per-restore-point") {
client_config.store.slots_per_restore_point = slots_per_restore_point
.parse()
.map_err(|_| "slots-per-restore-point is not a valid integer".to_string())?;
} else {
client_config.store.slots_per_restore_point = std::cmp::min(
E::slots_per_historical_root() as u64,
store::config::DEFAULT_SLOTS_PER_RESTORE_POINT,
);
}
client_config.store.slots_per_restore_point = get_slots_per_restore_point::<E>(cli_args)?;

if let Some(block_cache_size) = cli_args.value_of("block-cache-size") {
client_config.store.block_cache_size = block_cache_size
Expand Down Expand Up @@ -820,3 +811,17 @@ pub fn get_data_dir(cli_args: &ArgMatches) -> PathBuf {
})
.unwrap_or_else(|| PathBuf::from("."))
}

/// Get the `slots_per_restore_point` value to use for the database.
pub fn get_slots_per_restore_point<E: EthSpec>(cli_args: &ArgMatches) -> Result<u64, String> {
if let Some(slots_per_restore_point) =
clap_utils::parse_optional(cli_args, "slots-per-restore-point")?
{
Ok(slots_per_restore_point)
} else {
Ok(std::cmp::min(
E::slots_per_historical_root() as u64,
store::config::DEFAULT_SLOTS_PER_RESTORE_POINT,
))
}
}
2 changes: 1 addition & 1 deletion beacon_node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use beacon_chain::{
use clap::ArgMatches;
pub use cli::cli_app;
pub use client::{Client, ClientBuilder, ClientConfig, ClientGenesis};
pub use config::{get_config, get_data_dir, set_network_config};
pub use config::{get_config, get_data_dir, get_slots_per_restore_point, set_network_config};
use environment::RuntimeContext;
pub use eth2_config::Eth2Config;
use slasher::Slasher;
Expand Down
Loading

0 comments on commit 06b6466

Please sign in to comment.