From 9e07969d44a4a077be2c16cdeea7abef9b855a24 Mon Sep 17 00:00:00 2001 From: Svyatoslav Nikolsky Date: Wed, 7 Nov 2018 09:34:30 +0300 Subject: [PATCH] always ask for witness when requesting b/tx on core chain --- network/src/consensus.rs | 8 ++++++++ sync/src/lib.rs | 2 +- sync/src/synchronization_chain.rs | 23 +++++++---------------- sync/src/synchronization_client_core.rs | 21 +++++---------------- 4 files changed, 21 insertions(+), 33 deletions(-) diff --git a/network/src/consensus.rs b/network/src/consensus.rs index bfc460ec..0811d559 100644 --- a/network/src/consensus.rs +++ b/network/src/consensus.rs @@ -170,6 +170,14 @@ impl ConsensusFork { 4 } + /// Returns true if SegWit is possible on this chain. + pub fn is_segwit_possible(&self) -> bool { + match *self { + ConsensusFork::BitcoinCore => true, + ConsensusFork::BitcoinCash(_) => false, + } + } + pub fn activation_height(&self) -> u32 { match *self { ConsensusFork::BitcoinCore => 0, diff --git a/sync/src/lib.rs b/sync/src/lib.rs index f41c10c5..76dc3f5d 100644 --- a/sync/src/lib.rs +++ b/sync/src/lib.rs @@ -110,7 +110,7 @@ pub fn create_local_sync_node(consensus: ConsensusParams, db: storage::SharedSto let memory_pool = Arc::new(RwLock::new(MemoryPool::new())); let sync_state = SynchronizationStateRef::new(SynchronizationState::with_storage(db.clone())); let sync_chain = SyncChain::new(db.clone(), consensus.clone(), memory_pool.clone()); - if sync_chain.is_segwit_active() { + if sync_chain.is_segwit_possible() { peers.require_peer_services(Services::default().with_witness(true)); } diff --git a/sync/src/synchronization_chain.rs b/sync/src/synchronization_chain.rs index f9639262..dca616a0 100644 --- a/sync/src/synchronization_chain.rs +++ b/sync/src/synchronization_chain.rs @@ -9,7 +9,6 @@ use primitives::bytes::Bytes; use primitives::hash::H256; use utils::{BestHeadersChain, BestHeadersChainInformation, HashQueueChain, HashPosition}; use types::{BlockHeight, StorageRef, MemoryPoolRef}; -use verification::Deployments; /// Index of 'verifying' queue const VERIFYING_QUEUE: usize = 0; @@ -106,8 +105,6 @@ pub struct Chain { best_storage_block: storage::BestBlock, /// Local blocks storage storage: StorageRef, - /// Consensus params. - consensus: ConsensusParams, /// In-memory queue of blocks hashes hash_chain: HashQueueChain, /// In-memory queue of blocks headers @@ -118,10 +115,9 @@ pub struct Chain { memory_pool: MemoryPoolRef, /// Blocks that have been marked as dead-ends dead_end_blocks: HashSet, - /// Deployments cache - deployments: Deployments, - /// Is SegWit active? - is_segwit_active: bool, + /// Is SegWit is possible on this chain? SegWit inventory types are used when block/tx-es are + /// requested and this flag is true. + is_segwit_possible: bool, } impl BlockState { @@ -152,21 +148,18 @@ impl Chain { .expect("storage with genesis block is required"); let best_storage_block = storage.best_block(); let best_storage_block_hash = best_storage_block.hash.clone(); - let deployments = Deployments::new(); - let is_segwit_active = deployments.segwit(best_storage_block.number, storage.as_block_header_provider(), &consensus); + let is_segwit_possible = consensus.fork.is_segwit_possible(); Chain { genesis_block_hash: genesis_block_hash, best_storage_block: best_storage_block, storage: storage, - consensus: consensus, hash_chain: HashQueueChain::with_number_of_queues(NUMBER_OF_QUEUES), headers_chain: BestHeadersChain::new(best_storage_block_hash), verifying_transactions: LinkedHashMap::new(), memory_pool: memory_pool, dead_end_blocks: HashSet::new(), - deployments: deployments, - is_segwit_active: is_segwit_active, + is_segwit_possible, } } @@ -193,8 +186,8 @@ impl Chain { } /// Is segwit active - pub fn is_segwit_active(&self) -> bool { - self.is_segwit_active + pub fn is_segwit_possible(&self) -> bool { + self.is_segwit_possible } /// Get number of blocks in given state @@ -367,7 +360,6 @@ impl Chain { // remember new best block hash self.best_storage_block = self.storage.as_store().best_block(); - self.is_segwit_active = self.deployments.segwit(self.best_storage_block.number, self.storage.as_block_header_provider(), &self.consensus); // remove inserted block + handle possible reorganization in headers chain // TODO: mk, not sure if we need both of those params @@ -403,7 +395,6 @@ impl Chain { // remember new best block hash self.best_storage_block = self.storage.best_block(); - self.is_segwit_active = self.deployments.segwit(self.best_storage_block.number, self.storage.as_block_header_provider(), &self.consensus); // remove inserted block + handle possible reorganization in headers chain // TODO: mk, not sure if we need both of those params diff --git a/sync/src/synchronization_client_core.rs b/sync/src/synchronization_client_core.rs index 62282458..58a7879b 100644 --- a/sync/src/synchronization_client_core.rs +++ b/sync/src/synchronization_client_core.rs @@ -6,7 +6,7 @@ use futures::Future; use parking_lot::Mutex; use time::precise_time_s; use chain::{IndexedBlockHeader, IndexedTransaction, Transaction, IndexedBlock}; -use message::{types, Services}; +use message::types; use message::common::{InventoryType, InventoryVector}; use miner::transaction_fee_rate; use primitives::hash::H256; @@ -226,8 +226,7 @@ impl ClientCore for SynchronizationClientCore where T: TaskExecutor { } // else ask for all unknown transactions and blocks - let is_segwit_active = self.chain.is_segwit_active(); - let ask_for_witness = is_segwit_active && self.peers.is_segwit_enabled(peer_index); + let is_segwit_possible = self.chain.is_segwit_possible(); let unknown_inventory: Vec<_> = message.inventory.into_iter() .filter(|item| { match item.inv_type { @@ -258,7 +257,7 @@ impl ClientCore for SynchronizationClientCore where T: TaskExecutor { // we are not synchronizing => // 1) either segwit is active and we are connected to segwit-enabled nodes => we could ask for witness // 2) or segwit is inactive => we shall not ask for witness - .map(|item| if !ask_for_witness { + .map(|item| if !is_segwit_possible { item } else { match item.inv_type { @@ -973,8 +972,8 @@ impl SynchronizationClientCore where T: TaskExecutor { let chunk_size = min(limits.max_blocks_in_request, max(hashes.len() as BlockHeight, limits.min_blocks_in_request)); let last_peer_index = peers.len() - 1; let mut tasks: Vec = Vec::new(); - let is_segwit_active = self.chain.is_segwit_active(); - let inv_type = if is_segwit_active { InventoryType::MessageWitnessBlock } else { InventoryType::MessageBlock }; + let is_segwit_possible = self.chain.is_segwit_possible(); + let inv_type = if is_segwit_possible { InventoryType::MessageWitnessBlock } else { InventoryType::MessageBlock }; for (peer_index, peer) in peers.into_iter().enumerate() { // we have to request all blocks => we will request last peer for all remaining blocks let peer_chunk_size = if peer_index == last_peer_index { hashes.len() } else { min(hashes.len(), chunk_size as usize) }; @@ -1073,9 +1072,6 @@ impl SynchronizationClientCore where T: TaskExecutor { // update block processing speed self.block_speed_meter.checkpoint(); - // remember if SegWit was active before this block - let segwit_was_active = self.chain.is_segwit_active(); - // remove flags let needs_relay = !self.do_not_relay.remove(block.hash()); @@ -1096,13 +1092,6 @@ impl SynchronizationClientCore where T: TaskExecutor { // update shared state self.shared_state.update_best_storage_block_height(self.chain.best_storage_block().number); - // if SegWit activated after this block insertion: - // 1) no more connections to !NODE_WITNESS nodes - // 2) disconnect from all nodes without NODE_WITNESS support - if !segwit_was_active && self.chain.is_segwit_active() { - self.peers.require_peer_services(Services::default().with_witness(true)); - } - // notify listener if let Some(best_block_hash) = insert_result.canonized_blocks_hashes.last() { if let Some(ref listener) = self.listener {