Skip to content

Commit

Permalink
Add configuration option for the snapshot sync target
Browse files Browse the repository at this point in the history
  • Loading branch information
remagpie authored and foriequal0 committed Dec 10, 2019
1 parent 1051665 commit f718e4a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
10 changes: 10 additions & 0 deletions codechain/codechain.yml
Expand Up @@ -260,6 +260,16 @@ args:
takes_value: true
conflicts_with:
- no-discovery
- snapshot-hash:
long: snapshot-hash
value_name: HASH
requires: snapshot-number
takes_value: true
- snapshot-number:
long: snapshot-number
value_name: NUM
requires: snapshot-hash
takes_value: true
- no-snapshot:
long: no-snapshot
help: Disable snapshots
Expand Down
15 changes: 15 additions & 0 deletions codechain/config/mod.rs
Expand Up @@ -25,6 +25,7 @@ use cidr::IpCidr;
use ckey::PlatformAddress;
use clap;
use cnetwork::{FilterEntry, NetworkConfig, SocketAddr};
use primitives::H256;
use toml;

pub use self::chain_type::ChainType;
Expand Down Expand Up @@ -274,6 +275,8 @@ pub struct Network {
pub min_peers: Option<usize>,
pub max_peers: Option<usize>,
pub sync: Option<bool>,
pub snapshot_hash: Option<H256>,
pub snapshot_number: Option<u64>,
pub transaction_relay: Option<bool>,
pub discovery: Option<bool>,
pub discovery_type: Option<String>,
Expand Down Expand Up @@ -575,6 +578,12 @@ impl Network {
if other.sync.is_some() {
self.sync = other.sync;
}
if other.snapshot_hash.is_some() {
self.snapshot_hash = other.snapshot_hash;
}
if other.snapshot_number.is_some() {
self.snapshot_number = other.snapshot_number;
}
if other.transaction_relay.is_some() {
self.transaction_relay = other.transaction_relay;
}
Expand Down Expand Up @@ -627,6 +636,12 @@ impl Network {
if matches.is_present("no-sync") {
self.sync = Some(false);
}
if let Some(snapshot_hash) = matches.value_of("snapshot-hash") {
self.snapshot_hash = Some(snapshot_hash.parse().map_err(|_| "Invalid snapshot-hash")?);
}
if let Some(snapshot_number) = matches.value_of("snapshot-number") {
self.snapshot_number = Some(snapshot_number.parse().map_err(|_| "Invalid snapshot-number")?);
}
if matches.is_present("no-tx-relay") {
self.transaction_relay = Some(false);
}
Expand Down
6 changes: 5 additions & 1 deletion codechain/run_node.rs
Expand Up @@ -296,7 +296,11 @@ pub fn run_node(matches: &ArgMatches) -> Result<(), String> {
if config.network.sync.unwrap() {
let sync_sender = {
let client = client.client();
service.register_extension(move |api| BlockSyncExtension::new(client, api))
let snapshot_target = match (config.network.snapshot_hash, config.network.snapshot_number) {
(Some(hash), Some(num)) => Some((hash, num)),
_ => None,
};
service.register_extension(move |api| BlockSyncExtension::new(client, api, snapshot_target))
};
let sync = Arc::new(BlockSyncSender::from(sync_sender.clone()));
client.client().add_notify(Arc::downgrade(&sync) as Weak<dyn ChainNotify>);
Expand Down
2 changes: 1 addition & 1 deletion sync/src/block/extension.rs
Expand Up @@ -69,7 +69,7 @@ pub struct Extension {
}

impl Extension {
pub fn new(client: Arc<Client>, api: Box<dyn Api>) -> Extension {
pub fn new(client: Arc<Client>, api: Box<dyn Api>, _snapshot_target: Option<(H256, u64)>) -> Extension {
api.set_timer(SYNC_TIMER_TOKEN, Duration::from_millis(SYNC_TIMER_INTERVAL)).expect("Timer set succeeds");

let mut header = client.best_header();
Expand Down

0 comments on commit f718e4a

Please sign in to comment.