[T4] change compaction check trigger to 1 day and cut_through_horizon to 1 week (#1721)

* change chain compaction trigger from 2000 to 10080
* change CUT_THROUGH_HORIZON from 2 days to 1 week
* roll the dice to trigger the compaction
This commit is contained in:
Gary Yu 2018-10-14 00:34:16 +08:00 committed by Ignotus Peverell
parent 9a716aea72
commit e9f62b74d5
3 changed files with 24 additions and 19 deletions

View file

@ -72,9 +72,9 @@ pub const EASINESS: u32 = 50;
/// happening. Needs to be long enough to not overlap with a long reorg. /// happening. Needs to be long enough to not overlap with a long reorg.
/// Rational /// Rational
/// behind the value is the longest bitcoin fork was about 30 blocks, so 5h. We /// behind the value is the longest bitcoin fork was about 30 blocks, so 5h. We
/// add an order of magnitude to be safe and round to 48h of blocks to make it /// add an order of magnitude to be safe and round to 7x24h of blocks to make it
/// easier to reason about. /// easier to reason about.
pub const CUT_THROUGH_HORIZON: u32 = 48 * 3600 / (BLOCK_TIME_SEC as u32); pub const CUT_THROUGH_HORIZON: u32 = 7 * 24 * 3600 / (BLOCK_TIME_SEC as u32);
/// Weight of an input when counted against the max block weight capacity /// Weight of an input when counted against the max block weight capacity
pub const BLOCK_INPUT_WEIGHT: usize = 1; pub const BLOCK_INPUT_WEIGHT: usize = 1;

View file

@ -72,6 +72,11 @@ pub const TESTNET3_INITIAL_DIFFICULTY: u64 = 30000;
/// Testnet 4 initial block difficulty /// Testnet 4 initial block difficulty
pub const TESTNET4_INITIAL_DIFFICULTY: u64 = 1; pub const TESTNET4_INITIAL_DIFFICULTY: u64 = 1;
/// Trigger compaction check on average every 1440 blocks (i.e. one day) for FAST_SYNC_NODE,
/// roll the dice on every block to decide,
/// all blocks lower than (BodyHead.height - CUT_THROUGH_HORIZON) will be removed.
pub const COMPACTION_CHECK: u64 = 1440;
/// Types of chain a server can run with, dictates the genesis block and /// Types of chain a server can run with, dictates the genesis block and
/// and mining parameters used. /// and mining parameters used.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]

View file

@ -21,7 +21,7 @@ use std::sync::{Arc, RwLock, Weak};
use std::thread; use std::thread;
use std::time::Instant; use std::time::Instant;
use chain::{self, ChainAdapter, Options, Tip}; use chain::{self, ChainAdapter, Options};
use chrono::prelude::{DateTime, Utc}; use chrono::prelude::{DateTime, Utc};
use common::types::{self, ChainValidationMode, ServerConfig, SyncState, SyncStatus}; use common::types::{self, ChainValidationMode, ServerConfig, SyncState, SyncStatus};
use core::core::hash::{Hash, Hashed}; use core::core::hash::{Hash, Hashed};
@ -32,6 +32,7 @@ use core::pow::Difficulty;
use core::{core, global}; use core::{core, global};
use p2p; use p2p;
use pool; use pool;
use rand::prelude::*;
use store; use store;
use util::{OneTime, LOGGER}; use util::{OneTime, LOGGER};
@ -470,9 +471,9 @@ impl NetToChainAdapter {
let prev_hash = b.header.previous; let prev_hash = b.header.previous;
let bhash = b.hash(); let bhash = b.hash();
match self.chain().process_block(b, self.chain_opts()) { match self.chain().process_block(b, self.chain_opts()) {
Ok(tip) => { Ok(_) => {
self.validate_chain(bhash); self.validate_chain(bhash);
self.check_compact(tip); self.check_compact();
true true
} }
Err(ref e) if e.is_bad_data() => { Err(ref e) if e.is_bad_data() => {
@ -541,16 +542,16 @@ impl NetToChainAdapter {
} }
} }
fn check_compact(&self, tip: Option<Tip>) { fn check_compact(&self) {
// no compaction during sync or if we're in historical mode // no compaction during sync or if we're in historical mode
if self.archive_mode || self.sync_state.is_syncing() { if self.archive_mode || self.sync_state.is_syncing() {
return; return;
} }
if let Some(tip) = tip { // Roll the dice to trigger compaction at 1/COMPACTION_CHECK chance per block,
// trigger compaction every 2000 blocks, uses a different thread to avoid // uses a different thread to avoid blocking the caller thread (likely a peer)
// blocking the caller thread (likely a peer) let mut rng = thread_rng();
if tip.height % 2000 == 0 { if 0 == rng.gen_range(0, global::COMPACTION_CHECK) {
let chain = self.chain().clone(); let chain = self.chain().clone();
let _ = thread::Builder::new() let _ = thread::Builder::new()
.name("compactor".to_string()) .name("compactor".to_string())
@ -561,7 +562,6 @@ impl NetToChainAdapter {
}); });
} }
} }
}
// After receiving a compact block if we cannot successfully hydrate // After receiving a compact block if we cannot successfully hydrate
// it into a full block then fallback to requesting the full block // it into a full block then fallback to requesting the full block