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

add a node cmd to reset node startup info #2792

Merged
merged 1 commit into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions cmd/starcoin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub fn add_command(
.subcommand(node::InfoCommand)
.subcommand(node::PeersCommand)
.subcommand(node::MetricsCommand)
.subcommand(node::ResetCommand)
.subcommand(
Command::with_name("service")
.subcommand(node::service::ListCommand)
Expand Down
3 changes: 2 additions & 1 deletion cmd/starcoin/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ mod metrics_cmd;
mod peers_cmd;

pub mod network;
pub mod reset_cmd;
pub mod service;
pub mod sync;

pub use info_cmd::*;
pub use metrics_cmd::*;
pub use peers_cmd::*;
pub use reset_cmd::*;
34 changes: 34 additions & 0 deletions cmd/starcoin/src/node/reset_cmd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) The Starcoin Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::cli_state::CliState;
use crate::StarcoinOpt;
use anyhow::Result;
use scmd::{CommandAction, ExecContext};
use starcoin_crypto::HashValue;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
#[structopt(name = "reset")]
pub struct ResetOpt {
#[structopt(name = "block-hash")]
block_hash: HashValue,
}

pub struct ResetCommand;

impl CommandAction for ResetCommand {
type State = CliState;
type GlobalOpt = StarcoinOpt;
type Opt = ResetOpt;
type ReturnItem = ();

fn run(
&self,
ctx: &ExecContext<Self::State, Self::GlobalOpt, Self::Opt>,
) -> Result<Self::ReturnItem> {
let client = ctx.state().client();
client.reset_node(ctx.opt().block_hash)?;
Ok(())
}
}
2 changes: 2 additions & 0 deletions node/api/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use anyhow::Result;
use starcoin_crypto::HashValue;
use starcoin_service_registry::{ServiceInfo, ServiceRequest, ServiceStatus};

#[derive(Debug, Clone)]
Expand All @@ -13,6 +14,7 @@ pub enum NodeRequest {
StopPacemaker,
StartPacemaker,
ShutdownSystem,
ResetNode(HashValue),
}

#[derive(Debug)]
Expand Down
6 changes: 6 additions & 0 deletions node/api/src/node_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use crate::message::{NodeRequest, NodeResponse};
use anyhow::Result;
use starcoin_crypto::HashValue;
use starcoin_service_registry::{
ActorService, ServiceHandler, ServiceInfo, ServiceRef, ServiceStatus,
};
Expand All @@ -24,6 +25,7 @@ pub trait NodeAsyncService:
async fn stop_pacemaker(&self) -> Result<()>;

async fn shutdown_system(&self) -> Result<()>;
async fn reset_node(&self, block_hash: HashValue) -> Result<()>;
}

#[async_trait::async_trait]
Expand Down Expand Up @@ -91,4 +93,8 @@ where
self.try_send(NodeRequest::ShutdownSystem)?;
Ok(())
}
async fn reset_node(&self, block_hash: HashValue) -> Result<()> {
self.try_send(NodeRequest::ResetNode(block_hash))?;
Ok(())
}
}
11 changes: 10 additions & 1 deletion node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ use starcoin_storage::cache_storage::CacheStorage;
use starcoin_storage::db_storage::DBStorage;
use starcoin_storage::errors::StorageInitError;
use starcoin_storage::storage::StorageInstance;
use starcoin_storage::Storage;
use starcoin_storage::{BlockStore, Storage};
use starcoin_stratum::service::{StratumService, StratumServiceFactory};
use starcoin_stratum::stratum::{Stratum, StratumFactory};
use starcoin_sync::announcement::AnnouncementService;
use starcoin_sync::block_connector::BlockConnectorService;
use starcoin_sync::sync::SyncService;
use starcoin_sync::txn_sync::TxnSyncService;
use starcoin_txpool::TxPoolActorService;
use starcoin_types::startup_info::StartupInfo;
use starcoin_types::system_events::SystemStarted;
use std::sync::Arc;
use std::time::Duration;
Expand Down Expand Up @@ -116,6 +117,14 @@ impl ServiceHandler<Self, NodeRequest> for NodeService {
self.registry
.start_service_sync(GenerateBlockEventPacemaker::service_name()),
),
NodeRequest::ResetNode(block_hash) => {
let storage = self
.registry
.get_shared_sync::<Arc<Storage>>()
.expect("Storage must exist.");
info!("Prepare to reset node startup info to {}", block_hash);
NodeResponse::Result(storage.save_startup_info(StartupInfo { main: block_hash }))
}
})
}
}
Expand Down
3 changes: 3 additions & 0 deletions rpc/api/src/node_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
pub use self::gen_client::Client as NodeManagerClient;
use crate::FutureResult;
use jsonrpc_derive::rpc;
use starcoin_crypto::HashValue;
use starcoin_service_registry::{ServiceInfo, ServiceStatus};

#[rpc]
Expand All @@ -22,4 +23,6 @@ pub trait NodeManagerApi {

#[rpc(name = "node_manager.shutdown_system")]
fn shutdown_system(&self) -> FutureResult<()>;
#[rpc(name = "node_manager.reset_to_block")]
fn reset_to_block(&self, block_number: HashValue) -> FutureResult<()>;
}
5 changes: 4 additions & 1 deletion rpc/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,10 @@ impl RpcClient {
self.call_rpc_blocking(|inner| inner.node_manager_client.shutdown_system())
.map_err(map_err)
}

pub fn reset_node(&self, block_hash: HashValue) -> anyhow::Result<()> {
self.call_rpc_blocking(|inner| inner.node_manager_client.reset_to_block(block_hash))
.map_err(map_err)
}
pub fn next_sequence_number_in_txpool(
&self,
address: AccountAddress,
Expand Down
10 changes: 10 additions & 0 deletions rpc/server/src/module/node_manager_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use crate::module::map_err;
use futures::future::TryFutureExt;
use futures::FutureExt;
use starcoin_crypto::HashValue;
use starcoin_node_api::node_service::NodeAsyncService;
use starcoin_rpc_api::node_manager::NodeManagerApi;
use starcoin_rpc_api::FutureResult;
Expand Down Expand Up @@ -74,4 +75,13 @@ where
.map_err(map_err);
Box::pin(fut.boxed())
}
fn reset_to_block(&self, block_hash: HashValue) -> FutureResult<()> {
let service = self.service.clone();
let fut = async move {
service.reset_node(block_hash).await?;
Ok(())
}
.map_err(map_err);
Box::pin(fut.boxed())
}
}