2016-10-29 02:52:40 -07:00
use std ::sync ::Arc ;
2016-10-24 23:32:56 -07:00
use std ::cmp ::{ min , max } ;
2016-11-08 05:16:05 -08:00
use std ::collections ::{ HashMap , HashSet , VecDeque } ;
2016-10-24 23:32:56 -07:00
use std ::collections ::hash_map ::Entry ;
2016-11-21 03:10:04 -08:00
use parking_lot ::Mutex ;
2016-11-04 02:36:58 -07:00
use futures ::{ BoxFuture , Future , finished } ;
use futures ::stream ::Stream ;
use tokio_core ::reactor ::{ Handle , Interval } ;
use futures_cpupool ::CpuPool ;
2016-12-01 03:55:21 -08:00
use db ::{ self , IndexedBlock , BlockHeaderProvider , BlockRef } ;
2016-11-28 05:40:22 -08:00
use chain ::{ BlockHeader , Transaction } ;
2016-11-23 02:48:36 -08:00
use message ::types ;
2016-11-21 22:14:31 -08:00
use message ::common ::{ InventoryVector , InventoryType } ;
2016-10-24 23:32:56 -07:00
use primitives ::hash ::H256 ;
2016-12-01 03:55:21 -08:00
use primitives ::bytes ::Bytes ;
2016-10-29 02:52:40 -07:00
use synchronization_peers ::Peers ;
#[ cfg(test) ] use synchronization_peers ::{ Information as PeersInformation } ;
2016-12-01 03:55:21 -08:00
use synchronization_chain ::{ ChainRef , Chain , BlockState , TransactionState , HeadersIntersection , BlockInsertionResult } ;
2016-10-29 02:52:40 -07:00
#[ cfg(test) ]
use synchronization_chain ::{ Information as ChainInformation } ;
2016-11-02 03:15:37 -07:00
use synchronization_executor ::{ Task , TaskExecutor } ;
2016-11-16 05:46:21 -08:00
use orphan_blocks_pool ::OrphanBlocksPool ;
use orphan_transactions_pool ::OrphanTransactionsPool ;
2016-11-21 22:14:31 -08:00
use synchronization_server ::ServerTaskIndex ;
2016-11-14 11:24:45 -08:00
use synchronization_manager ::{ manage_synchronization_peers_blocks , manage_synchronization_peers_inventory ,
2016-11-16 05:46:21 -08:00
manage_unknown_orphaned_blocks , manage_orphaned_transactions , MANAGEMENT_INTERVAL_MS ,
ManagePeersConfig , ManageUnknownBlocksConfig , ManageOrphanTransactionsConfig } ;
2016-11-30 03:29:19 -08:00
use synchronization_verifier ::{ Verifier , VerificationSink , VerificationTask } ;
2016-11-25 04:09:08 -08:00
use compact_block_builder ::build_compact_block ;
2016-11-08 05:16:05 -08:00
use hash_queue ::HashPosition ;
2016-11-24 23:39:56 -08:00
use miner ::transaction_fee_rate ;
2016-12-01 03:55:21 -08:00
use verification ::ChainVerifier ;
2016-10-29 02:52:40 -07:00
use time ;
2016-11-04 02:36:58 -07:00
use std ::time ::Duration ;
2016-10-24 23:32:56 -07:00
2016-11-09 02:36:52 -08:00
#[ cfg_attr(feature= " cargo-clippy " , allow(doc_markdown)) ]
2016-11-10 04:16:16 -08:00
///! TODO: update with headers-first corrections
///!
2016-10-24 23:32:56 -07:00
///! Blocks synchronization process:
///!
///! When new peer is connected:
2016-11-08 05:16:05 -08:00
///! 1) send `getheaders` message with full block locator hashes (see `LocalNode`)
2016-10-24 23:32:56 -07:00
///!
2016-11-08 05:16:05 -08:00
///! on_new_blocks_headers: When `headers` message is received from peer:
2016-11-04 06:03:12 -07:00
///! 1) queue_intersection = intersect(queue, inventory)
///! 2) if !queue_intersection.is_empty(): ===> responded with blocks within sync window
///! 2.1) remember peer as useful
///! 2.2) inventory_rest = inventory - queue_intersection
///! 2.3) if inventory_rest.is_empty(): ===> no new unknown blocks in inventory
///! 2.3.1) stop (2.3)
///! 2.4) if !inventory_rest.is_empty(): ===> has new unknown blocks in inventory
///! 2.4.1) queue_rest = queue after intersection
///! 2.4.2) if queue_rest.is_empty(): ===> has new unknown blocks in inventory, no fork
2016-11-07 01:12:34 -08:00
///! 2.4.2.1) scheduled_blocks.append(inventory_rest)
///! 2.4.2.2) stop (2.4.2)
///! 2.4.3) if !queue_rest.is_empty(): ===> has new unknown blocks in inventory, fork
2016-11-04 06:03:12 -07:00
///! 2.4.3.1) scheduled_blocks.append(inventory_rest)
///! 2.4.3.2) stop (2.4.3)
2016-11-07 01:12:34 -08:00
///! 2.4.3) stop (2.4)
2016-11-04 06:03:12 -07:00
///! 2.5) stop (2)
///! 3) if queue_intersection.is_empty(): ===> responded with out-of-sync-window blocks
///! 3.1) last_known_block = inventory.last(b => b.is_known())
///! 3.2) if last_known_block == None: ===> we know nothing about these blocks & we haven't asked for these
2016-11-07 01:12:34 -08:00
///! 3.2.1) if !synchronizing => remember peer as useful + ask for blocks
///! 3.2.1) if synchronizing => peer will be excluded later by management thread
2016-11-04 06:03:12 -07:00
///! 3.2.2) stop (3.2)
///! 3.3) if last_known_block == last(inventory): ===> responded with all-known-blocks
2016-11-07 01:12:34 -08:00
///! 3.3.1) if syncing, remember peer as useful (possibly had failures before && have been excluded from sync)
2016-11-04 06:03:12 -07:00
///! 3.3.2) stop (3.3)
///! 3.4) if last_known_block in the middle of inventory: ===> responded with forked blocks
///! 3.4.1) remember peer as useful
///! 3.4.2) inventory_rest = inventory after last_known_block
///! 3.4.3) scheduled_blocks.append(inventory_rest)
///! 3.4.4) stop (3.4)
///! 3.5) stop (3)
2016-10-24 23:32:56 -07:00
///!
2016-11-04 06:03:12 -07:00
///! on_peer_block: After receiving `block` message:
2016-11-07 01:12:34 -08:00
///! 1) if block_state(block) in (Verifying, Stored): ===> late delivery
2016-11-04 06:03:12 -07:00
///! 1.1) remember peer as useful
///! 1.2) stop (1)
2016-11-07 01:12:34 -08:00
///! 2) if block_state(block) in (Scheduled, Requested): ===> future/on-time delivery
2016-11-04 06:03:12 -07:00
///! 2.1) remember peer as useful
2016-11-07 01:12:34 -08:00
///! 2.2) if block_state(block.parent) in (Verifying, Stored): ===> we can proceed with verification
///! 2.2.1) remove block from current queue (Verifying || Stored)
///! 2.2.2) append block to the verification queue
///! 2.2.3) queue verification().and_then(on_block_verification_success).or_else(on_block_verification_error)
///! 2.2.4) try to verify orphan blocks
///! 2.2.5) stop (2.2)
///! 2.3) if block_state(block.parent) in (Requested, Scheduled): ===> we have found an orphan block
///! 2.3.1) remove block from current queue (Verifying || Stored)
///! 2.3.2) append block to the orphans
///! 2.3.3) stop (2.3)
///! 2.4) if block_state(block.parent) == Unknown: ===> bad block found
///! 2.4.1) remove block from current queue (Verifying || Stored)
///! 2.4.2) stop (2.4)
///! 2.5) stop (2)
2016-11-04 06:03:12 -07:00
///! 3) if block_state(block) == Unknown: ===> maybe we are on-top of chain && new block is announced?
///! 3.1) if block_state(block.parent_hash) == Unknown: ===> we do not know parent
///! 3.1.1) ignore this block
///! 3.1.2) stop (3.1)
2016-11-07 01:12:34 -08:00
///! 3.2) if block_state(block.parent_hash) in (Verifying, Stored): ===> fork found, can verify
2016-11-04 06:03:12 -07:00
///! 3.2.1) ask peer for best inventory (after this block)
///! 3.2.2) append block to verifying queue
2016-11-07 01:12:34 -08:00
///! 3.2.3) queue verification().and_then(on_block_verification_success).or_else(on_block_verification_error)
2016-11-04 06:03:12 -07:00
///! 3.2.4) stop (3.2)
2016-11-07 01:12:34 -08:00
///! 3.3) if block_state(block.parent_hash) in (Requested, Scheduled): ===> fork found, add as orphan
///! 3.3.1) ask peer for best inventory (after this block)
///! 3.3.2) append block to orphan
///! 3.3.3) stop (3.3)
///! 3.4) stop (2)
///! + if no blocks left in scheduled + requested queue => we are saturated => ask all peers for inventory & forget
2016-10-24 23:32:56 -07:00
///!
2016-11-08 05:16:05 -08:00
///! execute_synchronization_tasks: After receiving `headers`/`inventory` message OR receiving `block` message OR when management thread schedules tasks:
2016-11-04 06:03:12 -07:00
///! 1) if there are blocks in `scheduled` queue AND we can fit more blocks into memory: ===> ask for blocks
2016-10-24 23:32:56 -07:00
///! 1.1) select idle peers
2016-11-04 06:03:12 -07:00
///! 1.2) for each idle peer: query chunk of blocks from `scheduled` queue
///! 1.3) move requested blocks from `scheduled` to `requested` queue
2016-10-24 23:32:56 -07:00
///! 1.4) mark idle peers as active
2016-11-04 06:03:12 -07:00
///! 1.5) stop (1)
///! 2) if `scheduled` queue is not yet saturated: ===> ask for new blocks hashes
2016-10-24 23:32:56 -07:00
///! 2.1) for each idle peer: send shortened `getblocks` message
2016-11-04 06:03:12 -07:00
///! 2.2) 'forget' idle peers => they will be added again if respond with inventory
///! 2.3) stop (2)
///!
///! manage_synchronization_peers: When management thread awakes:
///! 1) for peer in active_peers.where(p => now() - p.last_request_time() > failure_interval):
2016-11-08 05:16:05 -08:00
///! 1.1) return all peer' tasks to the tasks pool + TODO: filter tasks (if we have requested some hash several times from several peers && they haven't responded => drop this hash + reset sync???)
2016-11-04 06:03:12 -07:00
///! 1.2) increase # of failures for this peer
///! 1.3) if # of failures > max_failures: ===> super-bad peer
///! 1.3.1) forget peer
///! 1.3.3) stop (1.3)
///! 1.4) if # of failures <= max_failures: ===> bad peer
///! 1.4.1) move peer to idle pool
///! 1.4.2) stop (1.4)
///! 2) schedule tasks from pool (if any)
///!
///! on_block_verification_success: When verification completes scuccessfully:
///! 1) if block_state(block) != Verifying: ===> parent verification failed
///! 1.1) stop (1)
///! 2) remove from verifying queue
///! 3) insert to the db
///!
///! on_block_verification_error: When verification completes with an error:
///! 1) remove block from verification queue
2016-11-08 05:16:05 -08:00
///! 2) remove all known children from all queues [so that new `block` messages will be ignored in on_peer_block.3.1.1]
2016-10-24 23:32:56 -07:00
///!
2016-10-29 02:52:40 -07:00
/// Approximate maximal number of blocks hashes in scheduled queue.
2016-10-31 06:58:48 -07:00
const MAX_SCHEDULED_HASHES : u32 = 4 * 1024 ;
2016-10-29 02:52:40 -07:00
/// Approximate maximal number of blocks hashes in requested queue.
2016-11-04 02:36:58 -07:00
const MAX_REQUESTED_BLOCKS : u32 = 256 ;
2016-10-29 02:52:40 -07:00
/// Approximate maximal number of blocks in verifying queue.
2016-11-04 02:36:58 -07:00
const MAX_VERIFYING_BLOCKS : u32 = 256 ;
2016-10-24 23:32:56 -07:00
/// Minimum number of blocks to request from peer
2016-10-31 06:58:48 -07:00
const MIN_BLOCKS_IN_REQUEST : u32 = 32 ;
2016-10-24 23:32:56 -07:00
/// Maximum number of blocks to request from peer
2016-11-04 02:36:58 -07:00
const MAX_BLOCKS_IN_REQUEST : u32 = 128 ;
2016-12-01 08:40:23 -08:00
/// Number of blocks to receive since synchronization start to begin duplicating blocks requests
const NEAR_EMPTY_VERIFICATION_QUEUE_THRESHOLD_BLOCKS : usize = 20 ;
/// Number of seconds left before verification queue will be empty to count it as 'near empty queue'
const NEAR_EMPTY_VERIFICATION_QUEUE_THRESHOLD_S : f64 = 20_ f64 ;
/// Number of blocks to inspect when calculating average sync speed
const SYNC_SPEED_BLOCKS_TO_INSPECT : usize = 512 ;
/// Number of blocks to inspect when calculating average blocks speed
const BLOCKS_SPEED_BLOCKS_TO_INSPECT : usize = 512 ;
2016-10-24 23:32:56 -07:00
2016-11-02 03:15:37 -07:00
/// Synchronization state
2016-10-29 02:52:40 -07:00
#[ derive(Debug, Clone, Copy) ]
2016-10-24 23:32:56 -07:00
pub enum State {
2016-11-08 05:16:05 -08:00
/// We know that there are > 1 unknown blocks, unknown to us in the blockchain
2016-10-31 06:58:48 -07:00
Synchronizing ( f64 , u32 ) ,
2016-11-08 05:16:05 -08:00
/// There is only one unknown block in the blockchain
NearlySaturated ,
/// We have downloaded all blocks of the blockchain of which we have ever heard
2016-10-24 23:32:56 -07:00
Saturated ,
}
/// Information on current synchronization state.
2016-10-29 02:52:40 -07:00
#[ cfg(test) ]
2016-10-24 23:32:56 -07:00
#[ derive(Debug) ]
pub struct Information {
/// Current synchronization state.
pub state : State ,
2016-10-26 07:33:28 -07:00
/// Information on synchronization peers.
pub peers : PeersInformation ,
2016-10-26 05:56:52 -07:00
/// Current synchronization chain inormation.
pub chain : ChainInformation ,
2016-10-24 23:32:56 -07:00
/// Number of currently orphaned blocks.
2016-11-16 05:46:21 -08:00
pub orphaned_blocks : usize ,
/// Number of currently orphaned transactions.
pub orphaned_transactions : usize ,
2016-10-24 23:32:56 -07:00
}
2016-11-02 03:15:37 -07:00
/// Synchronization client trait
pub trait Client : Send + 'static {
fn best_block ( & self ) -> db ::BestBlock ;
2016-11-14 11:24:45 -08:00
fn state ( & self ) -> State ;
2016-12-01 00:00:09 -08:00
fn is_compact_block_sent_recently ( & mut self , peer_index : usize , hash : & H256 ) -> bool ;
2016-11-23 23:10:20 -08:00
fn filter_getdata_inventory ( & mut self , peer_index : usize , inventory : Vec < InventoryVector > ) -> FilteredInventory ;
2016-11-23 02:48:36 -08:00
fn on_peer_connected ( & mut self , peer_index : usize ) ;
2016-11-15 23:59:54 -08:00
fn on_new_blocks_inventory ( & mut self , peer_index : usize , blocks_hashes : Vec < H256 > ) ;
2016-11-16 05:46:21 -08:00
fn on_new_transactions_inventory ( & mut self , peer_index : usize , transactions_hashes : Vec < H256 > ) ;
2016-11-08 05:16:05 -08:00
fn on_new_blocks_headers ( & mut self , peer_index : usize , blocks_headers : Vec < BlockHeader > ) ;
2016-11-15 23:59:54 -08:00
fn on_peer_blocks_notfound ( & mut self , peer_index : usize , blocks_hashes : Vec < H256 > ) ;
2016-11-28 04:55:00 -08:00
fn on_peer_block ( & mut self , peer_index : usize , block : IndexedBlock ) ;
2016-11-16 05:46:21 -08:00
fn on_peer_transaction ( & mut self , peer_index : usize , transaction : Transaction ) ;
2016-11-23 02:48:36 -08:00
fn on_peer_filterload ( & mut self , peer_index : usize , message : & types ::FilterLoad ) ;
fn on_peer_filteradd ( & mut self , peer_index : usize , message : & types ::FilterAdd ) ;
fn on_peer_filterclear ( & mut self , peer_index : usize ) ;
2016-11-25 04:09:08 -08:00
fn on_peer_block_announcement_type ( & mut self , peer_index : usize , announcement_type : BlockAnnouncementType ) ;
2016-11-24 22:07:29 -08:00
fn on_peer_feefilter ( & mut self , peer_index : usize , message : & types ::FeeFilter ) ;
2016-11-02 03:15:37 -07:00
fn on_peer_disconnected ( & mut self , peer_index : usize ) ;
2016-11-21 03:10:04 -08:00
fn after_peer_nearly_blocks_verified ( & mut self , peer_index : usize , future : BoxFuture < ( ) , ( ) > ) ;
2016-11-18 00:52:45 -08:00
}
/// Synchronization client trait
pub trait ClientCore : VerificationSink {
fn best_block ( & self ) -> db ::BestBlock ;
fn state ( & self ) -> State ;
2016-12-01 00:00:09 -08:00
fn is_compact_block_sent_recently ( & mut self , peer_index : usize , hash : & H256 ) -> bool ;
2016-11-23 23:10:20 -08:00
fn filter_getdata_inventory ( & mut self , peer_index : usize , inventory : Vec < InventoryVector > ) -> FilteredInventory ;
2016-11-23 02:48:36 -08:00
fn on_peer_connected ( & mut self , peer_index : usize ) ;
2016-11-18 00:52:45 -08:00
fn on_new_blocks_inventory ( & mut self , peer_index : usize , blocks_hashes : Vec < H256 > ) ;
fn on_new_transactions_inventory ( & mut self , peer_index : usize , transactions_hashes : Vec < H256 > ) ;
fn on_new_blocks_headers ( & mut self , peer_index : usize , blocks_headers : Vec < BlockHeader > ) ;
fn on_peer_blocks_notfound ( & mut self , peer_index : usize , blocks_hashes : Vec < H256 > ) ;
2016-11-28 04:55:00 -08:00
fn on_peer_block ( & mut self , peer_index : usize , block : IndexedBlock ) -> Option < VecDeque < ( H256 , IndexedBlock ) > > ;
2016-11-18 00:52:45 -08:00
fn on_peer_transaction ( & mut self , peer_index : usize , transaction : Transaction ) -> Option < VecDeque < ( H256 , Transaction ) > > ;
2016-11-23 02:48:36 -08:00
fn on_peer_filterload ( & mut self , peer_index : usize , message : & types ::FilterLoad ) ;
fn on_peer_filteradd ( & mut self , peer_index : usize , message : & types ::FilterAdd ) ;
fn on_peer_filterclear ( & mut self , peer_index : usize ) ;
2016-11-25 04:09:08 -08:00
fn on_peer_block_announcement_type ( & mut self , peer_index : usize , announcement_type : BlockAnnouncementType ) ;
2016-11-24 22:07:29 -08:00
fn on_peer_feefilter ( & mut self , peer_index : usize , message : & types ::FeeFilter ) ;
2016-11-18 00:52:45 -08:00
fn on_peer_disconnected ( & mut self , peer_index : usize ) ;
2016-11-21 03:10:04 -08:00
fn after_peer_nearly_blocks_verified ( & mut self , peer_index : usize , future : BoxFuture < ( ) , ( ) > ) ;
2016-11-18 00:52:45 -08:00
fn execute_synchronization_tasks ( & mut self , forced_blocks_requests : Option < Vec < H256 > > ) ;
fn try_switch_to_saturated_state ( & mut self ) -> bool ;
2016-11-02 03:15:37 -07:00
}
2016-11-08 05:16:05 -08:00
2016-11-02 03:15:37 -07:00
/// Synchronization client configuration options.
2016-11-23 23:10:20 -08:00
#[ derive(Debug) ]
2016-10-29 02:52:40 -07:00
pub struct Config {
2016-12-04 23:13:22 -08:00
/// If true, connection to peer who has provided us with bad block is closed
pub close_connection_on_bad_block : bool ,
2016-11-04 02:36:58 -07:00
/// Number of threads to allocate in synchronization CpuPool.
pub threads_num : usize ,
2016-11-18 00:52:45 -08:00
}
2016-11-23 23:10:20 -08:00
/// Filtered `getdata` inventory.
#[ derive(Debug, PartialEq) ]
pub struct FilteredInventory {
/// Merkleblock messages + transactions to send after
pub filtered : Vec < ( types ::MerkleBlock , Vec < ( H256 , Transaction ) > ) > ,
2016-11-30 02:06:43 -08:00
/// Compactblock messages
pub compacted : Vec < types ::CompactBlock > ,
/// Rest of inventory with MessageTx, MessageBlock inventory types
2016-11-23 23:10:20 -08:00
pub unfiltered : Vec < InventoryVector > ,
/// Items that were supposed to be filtered, but we know nothing about these
pub notfound : Vec < InventoryVector > ,
}
2016-11-25 04:09:08 -08:00
#[ derive(Debug, Clone, Copy) ]
/// New block announcement type
pub enum BlockAnnouncementType {
/// Send inventory with block hash
SendInventory ,
/// Send block header
SendHeader ,
/// Send compact block
SendCompactBlock ,
}
2016-11-18 00:52:45 -08:00
/// Synchronization client facade
pub struct SynchronizationClient < T : TaskExecutor , U : Verifier > {
2016-11-18 03:39:34 -08:00
/// Client core
core : Arc < Mutex < SynchronizationClientCore < T > > > ,
2016-11-18 00:52:45 -08:00
/// Verifier
2016-11-18 03:39:34 -08:00
verifier : U ,
2016-11-02 03:15:37 -07:00
}
2016-10-29 02:52:40 -07:00
2016-11-02 03:15:37 -07:00
/// Synchronization client.
2016-11-18 00:52:45 -08:00
pub struct SynchronizationClientCore < T : TaskExecutor > {
2016-10-24 23:32:56 -07:00
/// Synchronization state.
state : State ,
2016-11-04 02:36:58 -07:00
/// Cpu pool.
pool : CpuPool ,
/// Sync management worker.
management_worker : Option < BoxFuture < ( ) , ( ) > > ,
2016-10-24 23:32:56 -07:00
/// Synchronization peers.
peers : Peers ,
2016-10-29 02:52:40 -07:00
/// Task executor.
executor : Arc < Mutex < T > > ,
2016-10-26 05:56:52 -07:00
/// Chain reference.
chain : ChainRef ,
2016-11-16 05:46:21 -08:00
/// Orphaned blocks pool.
orphaned_blocks_pool : OrphanBlocksPool ,
/// Orphaned transactions pool.
orphaned_transactions_pool : OrphanTransactionsPool ,
2016-12-05 06:15:28 -08:00
/// Chain verifier
chain_verifier : Arc < ChainVerifier > ,
2016-12-01 03:55:21 -08:00
/// Verify block headers?
verify_headers : bool ,
2016-11-08 05:16:05 -08:00
/// Verifying blocks by peer
verifying_blocks_by_peer : HashMap < H256 , usize > ,
2016-11-21 03:10:04 -08:00
/// Verifying blocks futures
verifying_blocks_futures : HashMap < usize , ( HashSet < H256 > , Vec < BoxFuture < ( ) , ( ) > > ) > ,
2016-11-30 04:15:48 -08:00
/// Hashes of items we do not want to relay after verification is completed
do_not_relay : HashSet < H256 > ,
2016-12-01 08:40:23 -08:00
/// Block processing speed meter
block_speed_meter : AverageSpeedMeter ,
/// Block synchronization speed meter
sync_speed_meter : AverageSpeedMeter ,
2016-12-04 23:13:22 -08:00
/// Configuration
config : Config ,
2016-10-24 23:32:56 -07:00
}
2016-12-01 03:55:21 -08:00
/// Block headers provider from `headers` message
2016-12-01 04:08:13 -08:00
pub struct MessageBlockHeadersProvider < ' a > {
2016-12-01 03:55:21 -08:00
/// sync chain
chain : & ' a Chain ,
/// headers offset
first_header_number : u32 ,
/// headers by hash
headers : HashMap < H256 , BlockHeader > ,
/// headers by order
headers_order : Vec < H256 > ,
}
2016-12-01 08:40:23 -08:00
/// Speed meter
struct AverageSpeedMeter {
/// Number of items to inspect
inspect_items : usize ,
/// Number of items currently inspected
inspected_items : VecDeque < f64 > ,
/// Current speed
speed : f64 ,
/// Last timestamp
last_timestamp : Option < f64 > ,
}
2016-11-23 23:10:20 -08:00
impl FilteredInventory {
#[ cfg(test) ]
pub fn with_unfiltered ( unfiltered : Vec < InventoryVector > ) -> Self {
FilteredInventory {
filtered : Vec ::new ( ) ,
2016-11-30 02:06:43 -08:00
compacted : Vec ::new ( ) ,
2016-11-23 23:10:20 -08:00
unfiltered : unfiltered ,
notfound : Vec ::new ( ) ,
}
}
#[ cfg(test) ]
pub fn with_notfound ( notfound : Vec < InventoryVector > ) -> Self {
FilteredInventory {
filtered : Vec ::new ( ) ,
2016-11-30 02:06:43 -08:00
compacted : Vec ::new ( ) ,
2016-11-23 23:10:20 -08:00
unfiltered : Vec ::new ( ) ,
notfound : notfound ,
}
}
}
2016-10-29 02:52:40 -07:00
impl State {
2016-11-08 05:16:05 -08:00
pub fn is_saturated ( & self ) -> bool {
2016-11-16 09:30:30 -08:00
match * self {
State ::Saturated = > true ,
2016-11-08 05:16:05 -08:00
_ = > false ,
}
}
2016-10-29 02:52:40 -07:00
pub fn is_synchronizing ( & self ) -> bool {
2016-11-09 02:36:52 -08:00
match * self {
State ::Synchronizing ( _ , _ ) = > true ,
2016-10-29 02:52:40 -07:00
_ = > false ,
}
}
2016-11-08 05:16:05 -08:00
pub fn is_nearly_saturated ( & self ) -> bool {
2016-11-16 09:30:30 -08:00
match * self {
State ::NearlySaturated = > true ,
2016-11-08 05:16:05 -08:00
_ = > false ,
}
}
2016-10-29 02:52:40 -07:00
}
2016-11-18 00:52:45 -08:00
impl < T , U > Client for SynchronizationClient < T , U > where T : TaskExecutor , U : Verifier {
fn best_block ( & self ) -> db ::BestBlock {
2016-11-18 03:39:34 -08:00
self . core . lock ( ) . best_block ( )
2016-11-18 00:52:45 -08:00
}
fn state ( & self ) -> State {
2016-11-18 03:39:34 -08:00
self . core . lock ( ) . state ( )
2016-11-18 00:52:45 -08:00
}
2016-12-01 00:00:09 -08:00
fn is_compact_block_sent_recently ( & mut self , peer_index : usize , hash : & H256 ) -> bool {
self . core . lock ( ) . is_compact_block_sent_recently ( peer_index , hash )
}
2016-11-23 23:10:20 -08:00
fn filter_getdata_inventory ( & mut self , peer_index : usize , inventory : Vec < InventoryVector > ) -> FilteredInventory {
self . core . lock ( ) . filter_getdata_inventory ( peer_index , inventory )
}
2016-11-23 02:48:36 -08:00
fn on_peer_connected ( & mut self , peer_index : usize ) {
self . core . lock ( ) . on_peer_connected ( peer_index ) ;
}
2016-11-18 00:52:45 -08:00
fn on_new_blocks_inventory ( & mut self , peer_index : usize , blocks_hashes : Vec < H256 > ) {
2016-11-18 03:39:34 -08:00
self . core . lock ( ) . on_new_blocks_inventory ( peer_index , blocks_hashes )
2016-11-18 00:52:45 -08:00
}
fn on_new_transactions_inventory ( & mut self , peer_index : usize , transactions_hashes : Vec < H256 > ) {
2016-11-18 03:39:34 -08:00
self . core . lock ( ) . on_new_transactions_inventory ( peer_index , transactions_hashes )
2016-11-18 00:52:45 -08:00
}
fn on_new_blocks_headers ( & mut self , peer_index : usize , blocks_headers : Vec < BlockHeader > ) {
2016-11-18 03:39:34 -08:00
self . core . lock ( ) . on_new_blocks_headers ( peer_index , blocks_headers ) ;
2016-11-18 00:52:45 -08:00
}
fn on_peer_blocks_notfound ( & mut self , peer_index : usize , blocks_hashes : Vec < H256 > ) {
2016-11-18 03:39:34 -08:00
self . core . lock ( ) . on_peer_blocks_notfound ( peer_index , blocks_hashes ) ;
2016-11-18 00:52:45 -08:00
}
2016-11-28 04:55:00 -08:00
fn on_peer_block ( & mut self , peer_index : usize , block : IndexedBlock ) {
let blocks_to_verify = self . core . lock ( ) . on_peer_block ( peer_index , block ) ;
2016-11-18 00:52:45 -08:00
// verify selected blocks
if let Some ( mut blocks_to_verify ) = blocks_to_verify {
while let Some ( ( _ , block ) ) = blocks_to_verify . pop_front ( ) {
2016-11-18 03:39:34 -08:00
self . verifier . verify_block ( block ) ;
2016-11-18 00:52:45 -08:00
}
2016-10-29 02:52:40 -07:00
}
2016-11-18 00:52:45 -08:00
// try to switch to saturated state OR execute sync tasks
{
2016-11-18 03:39:34 -08:00
let mut client = self . core . lock ( ) ;
2016-11-18 00:52:45 -08:00
if ! client . try_switch_to_saturated_state ( ) {
client . execute_synchronization_tasks ( None ) ;
}
}
}
fn on_peer_transaction ( & mut self , peer_index : usize , transaction : Transaction ) {
2016-11-18 03:39:34 -08:00
let transactions_to_verify = { self . core . lock ( ) . on_peer_transaction ( peer_index , transaction ) } ;
2016-11-18 00:52:45 -08:00
if let Some ( mut transactions_to_verify ) = transactions_to_verify {
2016-11-30 04:15:48 -08:00
// it is not actual height of block this transaction will be included to
// => it possibly will be invalid if included in later blocks
// => mined block can be rejected
// => we should verify blocks we mine
let next_block_height = self . best_block ( ) . number + 1 ;
2016-11-18 00:52:45 -08:00
while let Some ( ( _ , tx ) ) = transactions_to_verify . pop_front ( ) {
2016-11-30 04:15:48 -08:00
self . verifier . verify_transaction ( next_block_height , tx ) ;
2016-11-18 00:52:45 -08:00
}
}
}
2016-11-23 02:48:36 -08:00
fn on_peer_filterload ( & mut self , peer_index : usize , message : & types ::FilterLoad ) {
self . core . lock ( ) . on_peer_filterload ( peer_index , message ) ;
}
fn on_peer_filteradd ( & mut self , peer_index : usize , message : & types ::FilterAdd ) {
self . core . lock ( ) . on_peer_filteradd ( peer_index , message ) ;
}
fn on_peer_filterclear ( & mut self , peer_index : usize ) {
self . core . lock ( ) . on_peer_filterclear ( peer_index ) ;
}
2016-11-25 04:09:08 -08:00
fn on_peer_block_announcement_type ( & mut self , peer_index : usize , announcement_type : BlockAnnouncementType ) {
self . core . lock ( ) . on_peer_block_announcement_type ( peer_index , announcement_type ) ;
2016-11-24 04:18:18 -08:00
}
2016-11-24 22:07:29 -08:00
fn on_peer_feefilter ( & mut self , peer_index : usize , message : & types ::FeeFilter ) {
self . core . lock ( ) . on_peer_feefilter ( peer_index , message ) ;
2016-11-24 04:18:18 -08:00
}
2016-11-18 00:52:45 -08:00
fn on_peer_disconnected ( & mut self , peer_index : usize ) {
2016-11-18 03:39:34 -08:00
self . core . lock ( ) . on_peer_disconnected ( peer_index ) ;
2016-11-18 00:52:45 -08:00
}
2016-11-21 03:10:04 -08:00
fn after_peer_nearly_blocks_verified ( & mut self , peer_index : usize , future : BoxFuture < ( ) , ( ) > ) {
self . core . lock ( ) . after_peer_nearly_blocks_verified ( peer_index , future ) ;
2016-11-18 00:52:45 -08:00
}
}
impl < T , U > SynchronizationClient < T , U > where T : TaskExecutor , U : Verifier {
/// Create new synchronization client
2016-11-18 03:39:34 -08:00
pub fn new ( core : Arc < Mutex < SynchronizationClientCore < T > > > , verifier : U ) -> Arc < Mutex < Self > > {
2016-11-18 00:52:45 -08:00
Arc ::new ( Mutex ::new (
SynchronizationClient {
2016-11-18 03:39:34 -08:00
core : core ,
verifier : verifier ,
2016-11-18 00:52:45 -08:00
}
) )
}
/// Get information on current synchronization state.
#[ cfg(test) ]
pub fn information ( & self ) -> Information {
2016-11-18 03:39:34 -08:00
self . core . lock ( ) . information ( )
2016-10-29 02:52:40 -07:00
}
}
2016-11-18 00:52:45 -08:00
impl < T > ClientCore for SynchronizationClientCore < T > where T : TaskExecutor {
2016-11-02 03:15:37 -07:00
/// Get best known block
fn best_block ( & self ) -> db ::BestBlock {
self . chain . read ( ) . best_block ( )
2016-10-24 23:32:56 -07:00
}
2016-11-14 11:24:45 -08:00
/// Get synchronization state
fn state ( & self ) -> State {
2016-11-16 09:30:30 -08:00
self . state
2016-11-14 11:24:45 -08:00
}
2016-12-01 00:00:09 -08:00
/// Returns true if compactblock with this hash has been sent to this peer recently
fn is_compact_block_sent_recently ( & mut self , peer_index : usize , hash : & H256 ) -> bool {
self . peers . filter ( peer_index ) . is_known_compact_block ( hash )
}
2016-11-23 23:10:20 -08:00
/// Filter inventory from `getdata` message for given peer
fn filter_getdata_inventory ( & mut self , peer_index : usize , inventory : Vec < InventoryVector > ) -> FilteredInventory {
2016-11-30 02:06:43 -08:00
let storage = { self . chain . read ( ) . storage ( ) } ;
2016-11-23 23:10:20 -08:00
let mut filter = self . peers . filter_mut ( peer_index ) ;
let mut filtered : Vec < ( types ::MerkleBlock , Vec < ( H256 , Transaction ) > ) > = Vec ::new ( ) ;
2016-11-30 02:06:43 -08:00
let mut compacted : Vec < types ::CompactBlock > = Vec ::new ( ) ;
2016-11-23 23:10:20 -08:00
let mut unfiltered : Vec < InventoryVector > = Vec ::new ( ) ;
let mut notfound : Vec < InventoryVector > = Vec ::new ( ) ;
2016-11-24 02:04:58 -08:00
for item in inventory {
2016-11-23 23:10:20 -08:00
match item . inv_type {
// if peer asks for filtered block => we should:
// 1) check if block has any transactions, matching connection bloom filter
// 2) build && send `merkleblock` message for this block
// 3) send all matching transactions after this block
InventoryType ::MessageFilteredBlock = > {
2016-11-30 02:06:43 -08:00
match storage . block ( db ::BlockRef ::Hash ( item . hash . clone ( ) ) ) {
2016-11-23 23:10:20 -08:00
None = > notfound . push ( item ) ,
Some ( block ) = > match filter . build_merkle_block ( block ) {
None = > notfound . push ( item ) ,
2016-12-01 00:00:09 -08:00
Some ( merkleblock ) = > {
filtered . push ( ( merkleblock . merkleblock , merkleblock . matching_transactions ) ) ;
filter . known_block ( & item . hash , false ) ;
} ,
2016-11-30 02:06:43 -08:00
} ,
}
} ,
// peer asks for compact block:
InventoryType ::MessageCompactBlock = > {
match storage . block ( db ::BlockRef ::Hash ( item . hash . clone ( ) ) ) {
None = > notfound . push ( item ) ,
Some ( block ) = > {
let indexed_block : IndexedBlock = block . into ( ) ;
let prefilled_transactions_indexes = indexed_block . transactions ( ) . enumerate ( )
// we do not filter by fee rate here, because it only reasonable for non-mined transactions
. filter ( | & ( _ , ( h , t ) ) | filter . filter_transaction ( h , t , None ) )
. map ( | ( idx , _ ) | idx )
. collect ( ) ;
let compact_block = types ::CompactBlock {
header : build_compact_block ( indexed_block , prefilled_transactions_indexes ) ,
} ;
compacted . push ( compact_block ) ;
2016-12-01 00:00:09 -08:00
filter . known_block ( & item . hash , true ) ;
2016-11-30 02:06:43 -08:00
} ,
2016-11-23 23:10:20 -08:00
}
} ,
// these will be filtered (found/not found) in sync server
_ = > unfiltered . push ( item ) ,
}
}
FilteredInventory {
filtered : filtered ,
2016-11-30 02:06:43 -08:00
compacted : compacted ,
2016-11-23 23:10:20 -08:00
unfiltered : unfiltered ,
notfound : notfound ,
}
}
2016-11-23 02:48:36 -08:00
/// Called when new peer connection is established
fn on_peer_connected ( & mut self , peer_index : usize ) {
// unuseful until respond with headers message
self . peers . unuseful_peer ( peer_index ) ;
}
2016-10-24 23:32:56 -07:00
/// Try to queue synchronization of unknown blocks when new inventory is received.
2016-11-10 04:16:16 -08:00
fn on_new_blocks_inventory ( & mut self , peer_index : usize , blocks_hashes : Vec < H256 > ) {
// we use headers-first synchronization
// we know nothing about these blocks
// =>
// if we are in synchronization state, we will ignore this message
if self . state . is_synchronizing ( ) {
return ;
}
2016-11-10 05:01:03 -08:00
// else => request all unknown blocks
let unknown_blocks_hashes : Vec < _ > = {
let chain = self . chain . read ( ) ;
blocks_hashes . into_iter ( )
2016-12-04 23:13:22 -08:00
. filter ( | h | {
// if we haven't closed connection after receiving dead-end block
// => also process dead-end blocks
let block_state = chain . block_state ( h ) ;
block_state = = BlockState ::Unknown | | ( block_state = = BlockState ::DeadEnd & & ! self . config . close_connection_on_bad_block )
} )
2016-11-16 05:46:21 -08:00
. filter ( | h | ! self . orphaned_blocks_pool . contains_unknown_block ( h ) )
2016-11-10 05:01:03 -08:00
. collect ( )
} ;
2016-11-16 05:46:21 -08:00
if ! unknown_blocks_hashes . is_empty ( ) {
let mut executor = self . executor . lock ( ) ;
executor . execute ( Task ::RequestBlocks ( peer_index , unknown_blocks_hashes ) ) ;
}
}
/// Add new transactions to the memory pool
fn on_new_transactions_inventory ( & mut self , peer_index : usize , transactions_hashes : Vec < H256 > ) {
// if we are in synchronization state, we will ignore this message
if self . state . is_synchronizing ( ) {
return ;
}
// else => request all unknown transactions
let unknown_transactions_hashes : Vec < _ > = {
let chain = self . chain . read ( ) ;
transactions_hashes . into_iter ( )
2016-11-17 03:20:37 -08:00
. filter ( | h | chain . transaction_state ( h ) = = TransactionState ::Unknown )
2016-11-16 05:46:21 -08:00
. collect ( )
} ;
if ! unknown_transactions_hashes . is_empty ( ) {
let mut executor = self . executor . lock ( ) ;
executor . execute ( Task ::RequestTransactions ( peer_index , unknown_transactions_hashes ) ) ;
}
2016-11-08 05:16:05 -08:00
}
/// Try to queue synchronization of unknown blocks when blocks headers are received.
fn on_new_blocks_headers ( & mut self , peer_index : usize , blocks_headers : Vec < BlockHeader > ) {
let blocks_hashes = {
// we can't process headers message if it has no link to our headers
2016-11-16 09:30:30 -08:00
let header0 = & blocks_headers [ 0 ] ;
let unknown_state = self . chain . read ( ) . block_state ( & header0 . previous_header_hash ) = = BlockState ::Unknown ;
if unknown_state {
2016-11-16 06:54:43 -08:00
warn! (
target : " sync " ,
" Previous header of the first header from peer#{} `headers` message is unknown. First: {:?}. Previous: {:?} " ,
peer_index ,
header0 . hash ( ) . to_reversed_str ( ) ,
header0 . previous_header_hash . to_reversed_str ( )
) ;
2016-11-08 05:16:05 -08:00
return ;
}
// validate blocks headers before scheduling
2016-12-01 03:55:21 -08:00
let chain = self . chain . read ( ) ;
let mut block_header_provider = MessageBlockHeadersProvider ::new ( & * chain ) ;
2016-11-08 05:16:05 -08:00
let mut blocks_hashes : Vec < H256 > = Vec ::with_capacity ( blocks_headers . len ( ) ) ;
let mut prev_block_hash = header0 . previous_header_hash . clone ( ) ;
2016-11-16 09:30:30 -08:00
for block_header in & blocks_headers {
2016-11-08 05:16:05 -08:00
let block_header_hash = block_header . hash ( ) ;
2016-12-01 03:55:21 -08:00
// check that this header is direct child of previous header
2016-11-08 05:16:05 -08:00
if block_header . previous_header_hash ! = prev_block_hash {
2016-11-11 00:38:41 -08:00
warn! ( target : " sync " , " Neighbour headers in peer#{} `headers` message are unlinked: Prev: {:?}, PrevLink: {:?}, Curr: {:?} " , peer_index , prev_block_hash , block_header . previous_header_hash , block_header_hash ) ;
2016-11-08 05:16:05 -08:00
return ;
}
2016-12-01 03:55:21 -08:00
// verify header
if self . verify_headers {
2016-12-05 06:15:28 -08:00
if let Err ( error ) = self . chain_verifier . verify_block_header ( & block_header_provider , & block_header_hash , & block_header ) {
2016-12-01 03:55:21 -08:00
warn! ( target : " sync " , " Error verifying header {:?} from peer#{} `headers` message: {:?} " , block_header_hash . to_reversed_str ( ) , peer_index , error ) ;
return ;
}
}
block_header_provider . append_header ( block_header_hash . clone ( ) , block_header . clone ( ) ) ;
2016-11-08 05:16:05 -08:00
blocks_hashes . push ( block_header_hash . clone ( ) ) ;
prev_block_hash = block_header_hash ;
}
blocks_hashes
} ;
2016-11-14 11:24:45 -08:00
// update peers to select next tasks
self . peers . on_inventory_received ( peer_index ) ;
2016-11-08 05:16:05 -08:00
// now insert unknown blocks to the queue
self . process_new_blocks_headers ( peer_index , blocks_hashes , blocks_headers ) ;
self . execute_synchronization_tasks ( None ) ;
2016-10-29 02:52:40 -07:00
}
2016-11-15 23:59:54 -08:00
/// When peer has no blocks
fn on_peer_blocks_notfound ( & mut self , peer_index : usize , blocks_hashes : Vec < H256 > ) {
if let Some ( requested_blocks ) = self . peers . get_blocks_tasks ( peer_index ) {
// check if peer has responded with notfound to requested blocks
let notfound_blocks : HashSet < H256 > = blocks_hashes . into_iter ( ) . collect ( ) ;
if requested_blocks . intersection ( & notfound_blocks ) . nth ( 0 ) . is_none ( ) {
// if notfound some other blocks => just ignore the message
return ;
}
// for now, let's exclude peer from synchronization - we are relying on full nodes for synchronization
let removed_tasks = self . peers . reset_blocks_tasks ( peer_index ) ;
self . peers . unuseful_peer ( peer_index ) ;
// if peer has had some blocks tasks, rerequest these blocks
self . execute_synchronization_tasks ( Some ( removed_tasks ) ) ;
}
}
2016-10-29 02:52:40 -07:00
/// Process new block.
2016-11-28 04:55:00 -08:00
fn on_peer_block ( & mut self , peer_index : usize , block : IndexedBlock ) -> Option < VecDeque < ( H256 , IndexedBlock ) > > {
2016-11-28 05:07:33 -08:00
let block_hash = block . hash ( ) . clone ( ) ;
2016-10-29 02:52:40 -07:00
2016-12-01 08:40:23 -08:00
// update synchronization speed
self . sync_speed_meter . checkpoint ( ) ;
2016-10-29 02:52:40 -07:00
// update peers to select next tasks
self . peers . on_block_received ( peer_index , & block_hash ) ;
2016-11-18 00:52:45 -08:00
self . process_peer_block ( peer_index , block_hash , block )
2016-10-29 02:52:40 -07:00
}
2016-11-16 05:46:21 -08:00
/// Process new transaction.
2016-11-22 05:30:09 -08:00
fn on_peer_transaction ( & mut self , peer_index : usize , transaction : Transaction ) -> Option < VecDeque < ( H256 , Transaction ) > > {
let transaction_hash = transaction . hash ( ) ;
// remember that peer has this transaction
self . peers . on_transaction_received ( peer_index , & transaction_hash ) ;
2016-11-30 04:15:48 -08:00
self . process_peer_transaction ( Some ( peer_index ) , transaction_hash , transaction , true )
2016-11-16 05:46:21 -08:00
}
2016-11-23 02:48:36 -08:00
/// Peer wants to set bloom filter for the connection
fn on_peer_filterload ( & mut self , peer_index : usize , message : & types ::FilterLoad ) {
if self . peers . is_known_peer ( peer_index ) {
self . peers . filter_mut ( peer_index ) . load ( message ) ;
}
}
/// Peer wants to update bloom filter for the connection
fn on_peer_filteradd ( & mut self , peer_index : usize , message : & types ::FilterAdd ) {
if self . peers . is_known_peer ( peer_index ) {
self . peers . filter_mut ( peer_index ) . add ( message ) ;
}
}
/// Peer wants to remove bloom filter for the connection
fn on_peer_filterclear ( & mut self , peer_index : usize ) {
if self . peers . is_known_peer ( peer_index ) {
self . peers . filter_mut ( peer_index ) . clear ( ) ;
}
}
2016-11-25 04:09:08 -08:00
/// Change the way peer is informed about new blocks
fn on_peer_block_announcement_type ( & mut self , peer_index : usize , announcement_type : BlockAnnouncementType ) {
2016-11-24 04:18:18 -08:00
if self . peers . is_known_peer ( peer_index ) {
2016-11-25 04:09:08 -08:00
self . peers . set_block_announcement_type ( peer_index , announcement_type ) ;
2016-11-24 04:18:18 -08:00
}
}
2016-11-24 22:07:29 -08:00
/// Peer wants to limit transaction announcing by transaction fee
fn on_peer_feefilter ( & mut self , peer_index : usize , message : & types ::FeeFilter ) {
if self . peers . is_known_peer ( peer_index ) {
self . peers . on_peer_feefilter ( peer_index , message . fee_rate ) ;
2016-11-24 04:18:18 -08:00
}
}
2016-10-31 10:03:08 -07:00
/// Peer disconnected.
2016-11-02 03:15:37 -07:00
fn on_peer_disconnected ( & mut self , peer_index : usize ) {
2016-10-31 10:03:08 -07:00
// when last peer is disconnected, reset, but let verifying blocks be verified
2016-11-14 11:24:45 -08:00
let peer_tasks = self . peers . on_peer_disconnected ( peer_index ) ;
if ! self . peers . has_any_useful ( ) {
2016-11-14 02:02:25 -08:00
self . switch_to_saturated_state ( ) ;
2016-11-14 11:24:45 -08:00
} else if peer_tasks . is_some ( ) {
self . execute_synchronization_tasks ( peer_tasks ) ;
2016-10-31 10:03:08 -07:00
}
2016-10-29 02:52:40 -07:00
}
2016-11-21 03:10:04 -08:00
/// Execute after last block from this peer in NearlySaturated state is verified.
/// If there are no verifying blocks from this peer or we are not in the NearlySaturated state => execute immediately.
fn after_peer_nearly_blocks_verified ( & mut self , peer_index : usize , future : BoxFuture < ( ) , ( ) > ) {
2016-11-08 05:16:05 -08:00
// if we are currently synchronizing => no need to wait
if self . state . is_synchronizing ( ) {
2016-11-21 03:10:04 -08:00
future . wait ( ) . expect ( " no-error future " ) ;
return ;
2016-11-08 05:16:05 -08:00
}
// we have to wait until all previous peer requests are server
2016-11-21 03:10:04 -08:00
match self . verifying_blocks_futures . entry ( peer_index ) {
2016-11-08 05:16:05 -08:00
Entry ::Occupied ( mut entry ) = > {
2016-11-21 03:10:04 -08:00
entry . get_mut ( ) . 1. push ( future ) ;
2016-11-08 05:16:05 -08:00
} ,
2016-11-21 03:10:04 -08:00
_ = > future . wait ( ) . expect ( " no-error future " ) ,
2016-11-08 05:16:05 -08:00
}
}
2016-11-18 00:52:45 -08:00
/// Schedule new synchronization tasks, if any.
fn execute_synchronization_tasks ( & mut self , forced_blocks_requests : Option < Vec < H256 > > ) {
let mut tasks : Vec < Task > = Vec ::new ( ) ;
// display information if processed many blocks || enough time has passed since sync start
self . print_synchronization_information ( ) ;
// if some blocks requests are forced => we should ask peers even if there are no idle peers
if let Some ( forced_blocks_requests ) = forced_blocks_requests {
let useful_peers = self . peers . useful_peers ( ) ;
// if we have to request blocks && there are no useful peers at all => switch to saturated state
if useful_peers . is_empty ( ) {
warn! ( target : " sync " , " Last peer was marked as non-useful. Moving to saturated state. " ) ;
self . switch_to_saturated_state ( ) ;
return ;
}
let forced_tasks = self . prepare_blocks_requests_tasks ( useful_peers , forced_blocks_requests ) ;
tasks . extend ( forced_tasks ) ;
}
let mut blocks_requests : Option < Vec < H256 > > = None ;
let blocks_idle_peers = self . peers . idle_peers_for_blocks ( ) ;
{
// check if we can query some blocks hashes
let inventory_idle_peers = self . peers . idle_peers_for_inventory ( ) ;
if ! inventory_idle_peers . is_empty ( ) {
let scheduled_hashes_len = { self . chain . read ( ) . length_of_blocks_state ( BlockState ::Scheduled ) } ;
if scheduled_hashes_len < MAX_SCHEDULED_HASHES {
for inventory_peer in & inventory_idle_peers {
self . peers . on_inventory_requested ( * inventory_peer ) ;
}
let inventory_tasks = inventory_idle_peers . into_iter ( ) . map ( Task ::RequestBlocksHeaders ) ;
tasks . extend ( inventory_tasks ) ;
}
}
let blocks_idle_peers_len = blocks_idle_peers . len ( ) as u32 ;
if blocks_idle_peers_len ! = 0 {
let mut chain = self . chain . write ( ) ;
2016-12-01 08:40:23 -08:00
// check if verification queue is empty/almost empty
// && there are pending blocks requests
// && there are idle block peers
// => we may need to duplicate pending blocks requests to idle peers
// this will result in additional network load, but verification queue will be filled up earlier
// it is very useful when dealing with large blocks + some peer is responding, but with very low speed:
// requested: [B1, B2, B3, B4] from peer1
// orphans: [B5, B6, B7, B8, ... B1024] ===> 1GB of RAM
// verifying: None <=== we are waiting for B1 to come
// idle: [peer2]
// peer1 responds with single block in ~20 seconds
2016-12-02 01:21:01 -08:00
// => we could ask idle peer2 about [B1, B2, B3, B4]
2016-12-01 08:40:23 -08:00
// these requests has priority over new blocks requests below
2016-11-18 00:52:45 -08:00
let requested_hashes_len = chain . length_of_blocks_state ( BlockState ::Requested ) ;
let verifying_hashes_len = chain . length_of_blocks_state ( BlockState ::Verifying ) ;
2016-12-01 08:40:23 -08:00
if requested_hashes_len ! = 0 {
let verification_speed : f64 = self . block_speed_meter . speed ( ) ;
let synchronization_speed : f64 = self . sync_speed_meter . speed ( ) ;
// estimate time when verification queue will be empty
let verification_queue_will_be_empty_in = if verifying_hashes_len = = 0 {
// verification queue is already empty
if self . block_speed_meter . inspected_items_len ( ) < NEAR_EMPTY_VERIFICATION_QUEUE_THRESHOLD_BLOCKS {
// the very beginning of synchronization
// => peers have not yet responded with a single requested blocks
60_ f64
} else {
// blocks were are already received
// => bad situation
0_ f64
}
} else {
if verification_speed < 0.01_ f64 {
// verification speed is too slow
60_ f64
} else {
// blocks / (blocks / second) -> second
verifying_hashes_len as f64 / verification_speed
}
} ;
// estimate time when all synchronization requests will complete
let synchronization_queue_will_be_full_in = if synchronization_speed < 0.01_ f64 {
// synchronization speed is too slow
60_ f64
} else {
// blocks / (blocks / second) -> second
requested_hashes_len as f64 / synchronization_speed
} ;
// if verification queue will be empty before all synchronization requests will be completed
// + do not spam with duplicated blocks requests if blocks are too big && there are still blocks left for NEAR_EMPTY_VERIFICATION_QUEUE_THRESHOLD_S
// => duplicate blocks requests
if synchronization_queue_will_be_full_in > verification_queue_will_be_empty_in & &
verification_queue_will_be_empty_in < NEAR_EMPTY_VERIFICATION_QUEUE_THRESHOLD_S {
// blocks / second * second -> blocks
let hashes_requests_to_duplicate_len = synchronization_speed * ( synchronization_queue_will_be_full_in - verification_queue_will_be_empty_in ) ;
// do not ask for too many blocks
let hashes_requests_to_duplicate_len = min ( MAX_BLOCKS_IN_REQUEST , hashes_requests_to_duplicate_len as u32 ) ;
// ask for at least 1 block
let hashes_requests_to_duplicate_len = max ( 1 , min ( requested_hashes_len , hashes_requests_to_duplicate_len ) ) ;
blocks_requests = Some ( chain . best_n_of_blocks_state ( BlockState ::Requested , hashes_requests_to_duplicate_len ) ) ;
trace! ( target : " sync " , " Duplicating {} blocks requests. Sync speed: {} * {}, blocks speed: {} * {}. " , hashes_requests_to_duplicate_len , synchronization_speed , requested_hashes_len , verification_speed , verifying_hashes_len ) ;
}
}
// check if we can move some blocks from scheduled to requested queue
{
let scheduled_hashes_len = chain . length_of_blocks_state ( BlockState ::Scheduled ) ;
if requested_hashes_len + verifying_hashes_len < MAX_REQUESTED_BLOCKS + MAX_VERIFYING_BLOCKS & & scheduled_hashes_len ! = 0 {
let chunk_size = min ( MAX_BLOCKS_IN_REQUEST , max ( scheduled_hashes_len / blocks_idle_peers_len , MIN_BLOCKS_IN_REQUEST ) ) ;
let hashes_to_request_len = chunk_size * blocks_idle_peers_len ;
let hashes_to_request = chain . request_blocks_hashes ( hashes_to_request_len ) ;
match blocks_requests {
Some ( ref mut blocks_requests ) = > blocks_requests . extend ( hashes_to_request ) ,
None = > blocks_requests = Some ( hashes_to_request ) ,
}
}
2016-11-18 00:52:45 -08:00
}
}
}
// append blocks requests tasks
if let Some ( blocks_requests ) = blocks_requests {
tasks . extend ( self . prepare_blocks_requests_tasks ( blocks_idle_peers , blocks_requests ) ) ;
}
// execute synchronization tasks
for task in tasks {
self . executor . lock ( ) . execute ( task ) ;
}
}
fn try_switch_to_saturated_state ( & mut self ) -> bool {
let switch_to_saturated = {
let chain = self . chain . read ( ) ;
// requested block is received => move to saturated state if there are no more blocks
chain . length_of_blocks_state ( BlockState ::Scheduled ) = = 0
& & chain . length_of_blocks_state ( BlockState ::Requested ) = = 0
} ;
if switch_to_saturated {
self . switch_to_saturated_state ( ) ;
}
switch_to_saturated
}
}
impl < T > VerificationSink for SynchronizationClientCore < T > where T : TaskExecutor {
2016-11-02 03:15:37 -07:00
/// Process successful block verification
2016-11-30 03:29:19 -08:00
fn on_block_verification_success ( & mut self , block : IndexedBlock ) -> Option < Vec < VerificationTask > > {
2016-12-01 08:40:23 -08:00
// update block processing speed
self . block_speed_meter . checkpoint ( ) ;
// insert block to the storage
2016-11-08 05:16:05 -08:00
let hash = block . hash ( ) ;
2016-11-30 04:15:48 -08:00
let needs_relay = ! self . do_not_relay . remove ( hash ) ;
2016-11-15 07:01:33 -08:00
match {
2016-11-02 03:15:37 -07:00
let mut chain = self . chain . write ( ) ;
2016-11-07 01:12:34 -08:00
// remove block from verification queue
2016-11-10 05:01:03 -08:00
// header is removed in `insert_best_block` call
// or it is removed earlier, when block was removed from the verifying queue
2016-11-16 05:46:21 -08:00
if chain . forget_block_with_state_leave_header ( & hash , BlockState ::Verifying ) ! = HashPosition ::Missing {
2016-11-08 05:16:05 -08:00
// block was in verification queue => insert to storage
2016-11-16 05:46:21 -08:00
chain . insert_best_block ( hash . clone ( ) , & block )
2016-11-15 07:01:33 -08:00
} else {
2016-11-16 05:46:21 -08:00
Ok ( BlockInsertionResult ::default ( ) )
2016-11-08 05:16:05 -08:00
}
2016-11-15 07:01:33 -08:00
} {
2016-11-16 05:46:21 -08:00
Ok ( insert_result ) = > {
2016-11-15 07:01:33 -08:00
// awake threads, waiting for this block insertion
self . awake_waiting_threads ( & hash ) ;
2016-11-02 03:15:37 -07:00
2016-11-15 07:01:33 -08:00
// continue with synchronization
self . execute_synchronization_tasks ( None ) ;
2016-11-16 05:46:21 -08:00
// relay block to our peers
2016-11-24 02:11:10 -08:00
// TODO:
// SPV clients that wish to use Bloom filtering would normally set version.fRelay to false in the version message,
// then set a filter based on their wallet (or a subset of it, if they are overlapping different peers).
// Being able to opt-out of inv messages until the filter is set prevents a client being flooded with traffic in
// the brief window of time between finishing version handshaking and setting the filter.
2016-11-21 09:06:14 -08:00
if self . state . is_saturated ( ) | | self . state . is_nearly_saturated ( ) {
2016-11-30 04:15:48 -08:00
if needs_relay {
self . relay_new_blocks ( insert_result . canonized_blocks_hashes ) ;
}
2016-11-16 05:46:21 -08:00
}
// deal with block transactions
2016-11-30 03:29:19 -08:00
let mut verification_tasks : Vec < VerificationTask > = Vec ::with_capacity ( insert_result . transactions_to_reverify . len ( ) ) ;
2016-11-30 04:15:48 -08:00
let next_block_height = self . best_block ( ) . number + 1 ;
2016-11-22 05:30:09 -08:00
for ( hash , tx ) in insert_result . transactions_to_reverify {
2016-11-30 04:15:48 -08:00
// do not relay resurrected transactions again
if let Some ( tx_orphans ) = self . process_peer_transaction ( None , hash , tx , false ) {
let tx_tasks = tx_orphans . into_iter ( ) . map ( | ( _ , tx ) | VerificationTask ::VerifyTransaction ( next_block_height , tx ) ) ;
2016-11-30 03:29:19 -08:00
verification_tasks . extend ( tx_tasks ) ;
} ;
2016-11-16 05:46:21 -08:00
}
2016-11-30 03:29:19 -08:00
Some ( verification_tasks )
2016-11-15 07:01:33 -08:00
} ,
Err ( db ::Error ::Consistency ( e ) ) = > {
// process as verification error
self . on_block_verification_error ( & format! ( " {:?} " , db ::Error ::Consistency ( e ) ) , & hash ) ;
2016-11-30 03:29:19 -08:00
None
2016-11-15 07:01:33 -08:00
} ,
Err ( e ) = > {
// process as irrecoverable failure
panic! ( " Block {:?} insertion failed with error {:?} " , hash , e ) ;
}
}
2016-11-02 03:15:37 -07:00
}
/// Process failed block verification
2016-11-15 07:01:33 -08:00
fn on_block_verification_error ( & mut self , err : & str , hash : & H256 ) {
2016-11-16 06:54:43 -08:00
warn! ( target : " sync " , " Block {:?} verification failed with error {:?} " , hash . to_reversed_str ( ) , err ) ;
2016-11-02 03:15:37 -07:00
2016-11-30 04:15:48 -08:00
self . do_not_relay . remove ( hash ) ;
2016-12-02 07:12:20 -08:00
// close connection with this peer
if let Some ( peer_index ) = self . verifying_blocks_by_peer . get ( hash ) {
2016-12-04 23:13:22 -08:00
warn! ( target : " sync " , " Peer#{} has provided us with wrong block {:?} " , peer_index , hash . to_reversed_str ( ) ) ;
if self . config . close_connection_on_bad_block {
self . executor . lock ( ) . execute ( Task ::Close ( * peer_index ) ) ;
}
2016-12-02 07:12:20 -08:00
}
2016-11-07 01:12:34 -08:00
{
let mut chain = self . chain . write ( ) ;
2016-11-08 05:16:05 -08:00
// forget for this block and all its children
2016-11-10 05:01:03 -08:00
// headers are also removed as they all are invalid
2016-11-17 00:14:05 -08:00
chain . forget_block_with_children ( hash ) ;
2016-12-02 07:12:20 -08:00
// mark failed block as dead end (this branch won't be synchronized)
chain . mark_dead_end_block ( hash ) ;
2016-11-07 01:12:34 -08:00
}
2016-11-02 03:15:37 -07:00
2016-11-08 05:16:05 -08:00
// awake threads, waiting for this block insertion
2016-11-16 09:30:30 -08:00
self . awake_waiting_threads ( hash ) ;
2016-11-08 05:16:05 -08:00
2016-11-02 03:15:37 -07:00
// start new tasks
2016-11-08 05:16:05 -08:00
self . execute_synchronization_tasks ( None ) ;
2016-11-02 03:15:37 -07:00
}
2016-11-16 05:46:21 -08:00
/// Process successful transaction verification
fn on_transaction_verification_success ( & mut self , transaction : Transaction ) {
let hash = transaction . hash ( ) ;
2016-11-30 04:15:48 -08:00
let needs_relay = ! self . do_not_relay . remove ( & hash ) ;
2016-11-16 05:46:21 -08:00
2016-11-24 22:07:29 -08:00
let transaction_fee_rate = {
2016-11-21 07:42:18 -08:00
// insert transaction to the memory pool
let mut chain = self . chain . write ( ) ;
// remove transaction from verification queue
// if it is not in the queue => it was removed due to error or reorganization
if ! chain . forget_verifying_transaction ( & hash ) {
return ;
}
// transaction was in verification queue => insert to memory pool
2016-11-23 02:48:36 -08:00
chain . insert_verified_transaction ( transaction . clone ( ) ) ;
2016-11-24 22:07:29 -08:00
// calculate transaction fee rate
2016-11-24 23:39:56 -08:00
transaction_fee_rate ( & * chain , & transaction )
2016-11-24 22:07:29 -08:00
} ;
2016-11-16 05:46:21 -08:00
2016-11-21 07:42:18 -08:00
// relay transaction to peers
2016-11-30 04:15:48 -08:00
if needs_relay {
self . relay_new_transactions ( vec! [ ( hash , & transaction , transaction_fee_rate ) ] ) ;
}
2016-11-16 05:46:21 -08:00
}
/// Process failed transaction verification
fn on_transaction_verification_error ( & mut self , err : & str , hash : & H256 ) {
warn! ( target : " sync " , " Transaction {:?} verification failed with error {:?} " , hash . to_reversed_str ( ) , err ) ;
2016-11-30 04:15:48 -08:00
self . do_not_relay . remove ( hash ) ;
2016-11-16 05:46:21 -08:00
{
let mut chain = self . chain . write ( ) ;
// forget for this transaction and all its children
2016-11-17 03:20:37 -08:00
chain . forget_verifying_transaction_with_children ( hash ) ;
2016-11-16 05:46:21 -08:00
}
}
2016-11-02 03:15:37 -07:00
}
2016-11-18 00:52:45 -08:00
impl < T > SynchronizationClientCore < T > where T : TaskExecutor {
/// Create new synchronization client core
2016-12-05 06:15:28 -08:00
pub fn new ( config : Config , handle : & Handle , executor : Arc < Mutex < T > > , chain : ChainRef , chain_verifier : Arc < ChainVerifier > ) -> Arc < Mutex < Self > > {
2016-11-02 03:15:37 -07:00
let sync = Arc ::new ( Mutex ::new (
2016-11-18 00:52:45 -08:00
SynchronizationClientCore {
2016-11-02 03:15:37 -07:00
state : State ::Saturated ,
peers : Peers ::new ( ) ,
2016-11-04 02:36:58 -07:00
pool : CpuPool ::new ( config . threads_num ) ,
management_worker : None ,
2016-11-02 03:15:37 -07:00
executor : executor ,
chain : chain . clone ( ) ,
2016-11-16 05:46:21 -08:00
orphaned_blocks_pool : OrphanBlocksPool ::new ( ) ,
orphaned_transactions_pool : OrphanTransactionsPool ::new ( ) ,
2016-12-05 06:15:28 -08:00
chain_verifier : chain_verifier ,
2016-12-01 03:55:21 -08:00
verify_headers : true ,
2016-11-08 05:16:05 -08:00
verifying_blocks_by_peer : HashMap ::new ( ) ,
2016-11-21 03:10:04 -08:00
verifying_blocks_futures : HashMap ::new ( ) ,
2016-11-30 04:15:48 -08:00
do_not_relay : HashSet ::new ( ) ,
2016-12-01 08:40:23 -08:00
block_speed_meter : AverageSpeedMeter ::with_inspect_items ( SYNC_SPEED_BLOCKS_TO_INSPECT ) ,
sync_speed_meter : AverageSpeedMeter ::with_inspect_items ( BLOCKS_SPEED_BLOCKS_TO_INSPECT ) ,
2016-12-04 23:13:22 -08:00
config : config ,
2016-11-02 03:15:37 -07:00
}
) ) ;
2016-11-04 02:36:58 -07:00
{
2016-11-13 22:31:03 -08:00
let peers_config = ManagePeersConfig ::default ( ) ;
let unknown_config = ManageUnknownBlocksConfig ::default ( ) ;
2016-11-16 05:46:21 -08:00
let orphan_config = ManageOrphanTransactionsConfig ::default ( ) ;
2016-11-04 02:36:58 -07:00
let csync = Arc ::downgrade ( & sync ) ;
let mut sync = sync . lock ( ) ;
let management_worker = Interval ::new ( Duration ::from_millis ( MANAGEMENT_INTERVAL_MS ) , handle )
. expect ( " Failed to create interval " )
. and_then ( move | _ | {
let client = match csync . upgrade ( ) {
Some ( client ) = > client ,
None = > return Ok ( ( ) ) ,
} ;
let mut client = client . lock ( ) ;
2016-11-14 11:24:45 -08:00
client . print_synchronization_information ( ) ;
2016-11-08 05:16:05 -08:00
if client . state . is_synchronizing ( ) | | client . state . is_nearly_saturated ( ) {
2016-11-14 11:24:45 -08:00
let blocks_to_request = manage_synchronization_peers_blocks ( & peers_config , & mut client . peers ) ;
client . execute_synchronization_tasks ( blocks_to_request ) ;
2016-11-11 00:38:41 -08:00
2016-11-14 11:24:45 -08:00
manage_synchronization_peers_inventory ( & peers_config , & mut client . peers ) ;
2016-11-16 05:46:21 -08:00
manage_orphaned_transactions ( & orphan_config , & mut client . orphaned_transactions_pool ) ;
if let Some ( orphans_to_remove ) = manage_unknown_orphaned_blocks ( & unknown_config , & mut client . orphaned_blocks_pool ) {
let mut chain = client . chain . write ( ) ;
for orphan_to_remove in orphans_to_remove {
chain . forget_block ( & orphan_to_remove ) ;
}
2016-11-11 00:38:41 -08:00
}
2016-11-07 01:12:34 -08:00
}
2016-11-04 02:36:58 -07:00
Ok ( ( ) )
} )
. for_each ( | _ | Ok ( ( ) ) )
. then ( | _ | finished ::< ( ) , ( ) > ( ( ) ) )
. boxed ( ) ;
sync . management_worker = Some ( sync . pool . spawn ( management_worker ) . boxed ( ) ) ;
}
2016-11-02 03:15:37 -07:00
sync
}
/// Get information on current synchronization state.
#[ cfg(test) ]
pub fn information ( & self ) -> Information {
Information {
state : self . state ,
peers : self . peers . information ( ) ,
chain : self . chain . read ( ) . information ( ) ,
2016-11-16 05:46:21 -08:00
orphaned_blocks : self . orphaned_blocks_pool . len ( ) ,
orphaned_transactions : self . orphaned_transactions_pool . len ( ) ,
2016-11-02 03:15:37 -07:00
}
}
2016-12-01 03:55:21 -08:00
/// Verify block headers or not?
#[ cfg(test) ]
pub fn verify_headers ( & mut self , verify : bool ) {
self . verify_headers = verify ;
}
2016-11-21 22:14:31 -08:00
/// Relay new blocks
2016-11-23 02:48:36 -08:00
fn relay_new_blocks ( & mut self , new_blocks_hashes : Vec < H256 > ) {
2016-11-24 04:18:18 -08:00
let tasks : Vec < _ > = {
self . peers . all_peers ( ) . into_iter ( )
. filter_map ( | peer_index | {
2016-11-25 04:09:08 -08:00
let block_announcement_type = self . peers . block_announcement_type ( peer_index ) ;
match block_announcement_type {
BlockAnnouncementType ::SendHeader = > {
let filtered_blocks_hashes : Vec < _ > = new_blocks_hashes . iter ( )
. filter ( | h | self . peers . filter ( peer_index ) . filter_block ( h ) )
. collect ( ) ;
let chain = self . chain . read ( ) ;
let headers : Vec < _ > = filtered_blocks_hashes . into_iter ( )
. filter_map ( | h | chain . block_header_by_hash ( & h ) )
. collect ( ) ;
if ! headers . is_empty ( ) {
Some ( Task ::SendHeaders ( peer_index , headers , ServerTaskIndex ::None ) )
}
else {
None
}
} ,
BlockAnnouncementType ::SendCompactBlock = > {
let indexed_blocks : Vec < db ::IndexedBlock > = {
let chain = self . chain . read ( ) ;
new_blocks_hashes . iter ( )
. filter_map ( | h | chain . storage ( ) . block ( db ::BlockRef ::Hash ( h . clone ( ) ) ) )
. map ( | b | b . into ( ) )
. collect ( )
} ;
let block_header_and_ids : Vec < _ > = indexed_blocks . into_iter ( )
. filter_map ( | b | if self . peers . filter ( peer_index ) . filter_block ( & b . hash ( ) ) {
let prefilled_transactions_indexes = b . transactions ( ) . enumerate ( )
2016-11-27 07:02:21 -08:00
// we do not filter by fee rate here, because it only reasonable for non-mined transactions
. filter ( | & ( _ , ( h , t ) ) | self . peers . filter_mut ( peer_index ) . filter_transaction ( h , t , None ) )
2016-11-25 04:09:08 -08:00
. map ( | ( idx , _ ) | idx )
. collect ( ) ;
Some ( build_compact_block ( b , prefilled_transactions_indexes ) )
} else {
None
} )
. collect ( ) ;
2016-11-29 08:39:00 -08:00
Some ( Task ::SendCompactBlocks ( peer_index , block_header_and_ids ) )
2016-11-25 04:09:08 -08:00
} ,
BlockAnnouncementType ::SendInventory = > {
let inventory : Vec < _ > = new_blocks_hashes . iter ( )
. filter ( | h | self . peers . filter ( peer_index ) . filter_block ( h ) )
. map ( | h | InventoryVector {
inv_type : InventoryType ::MessageBlock ,
hash : h . clone ( ) ,
} )
. collect ( ) ;
if ! inventory . is_empty ( ) {
2016-11-29 08:39:00 -08:00
Some ( Task ::SendInventory ( peer_index , inventory ) )
2016-11-25 04:09:08 -08:00
} else {
None
}
} ,
2016-11-24 04:18:18 -08:00
}
} )
. collect ( )
} ;
2016-11-23 02:48:36 -08:00
2016-11-21 22:14:31 -08:00
let mut executor = self . executor . lock ( ) ;
2016-11-23 02:48:36 -08:00
for task in tasks {
executor . execute ( task ) ;
2016-11-21 22:14:31 -08:00
}
}
2016-11-22 05:30:09 -08:00
/// Relay new transactions
2016-11-24 22:07:29 -08:00
fn relay_new_transactions ( & mut self , new_transactions : Vec < ( H256 , & Transaction , u64 ) > ) {
2016-11-23 02:48:36 -08:00
let tasks : Vec < _ > = self . peers . all_peers ( ) . into_iter ( )
. filter_map ( | peer_index | {
let inventory : Vec < _ > = new_transactions . iter ( )
2016-11-24 22:07:29 -08:00
. filter ( | & & ( ref h , tx , tx_fee_rate ) | {
2016-11-27 07:02:21 -08:00
self . peers . filter_mut ( peer_index ) . filter_transaction ( h , tx , Some ( tx_fee_rate ) )
2016-11-24 22:07:29 -08:00
} )
. map ( | & ( ref h , _ , _ ) | InventoryVector {
2016-11-23 02:48:36 -08:00
inv_type : InventoryType ::MessageTx ,
hash : h . clone ( ) ,
} )
. collect ( ) ;
if ! inventory . is_empty ( ) {
2016-11-29 08:39:00 -08:00
Some ( Task ::SendInventory ( peer_index , inventory ) )
2016-11-23 02:48:36 -08:00
} else {
None
}
} )
. collect ( ) ;
2016-11-22 05:30:09 -08:00
let mut executor = self . executor . lock ( ) ;
2016-11-23 02:48:36 -08:00
for task in tasks {
executor . execute ( task ) ;
2016-11-22 05:30:09 -08:00
}
}
2016-10-31 04:26:39 -07:00
/// Process new blocks inventory
2016-11-08 05:16:05 -08:00
fn process_new_blocks_headers ( & mut self , peer_index : usize , mut hashes : Vec < H256 > , mut headers : Vec < BlockHeader > ) {
assert_eq! ( hashes . len ( ) , headers . len ( ) ) ;
2016-10-29 02:52:40 -07:00
let mut chain = self . chain . write ( ) ;
2016-12-04 23:13:22 -08:00
let intersection_result = chain . intersect_with_blocks_headers ( & hashes , & headers ) ;
match intersection_result {
2016-11-11 00:38:41 -08:00
HeadersIntersection ::NoKnownBlocks ( _ ) if self . state . is_synchronizing ( ) = > {
warn! ( target : " sync " , " Ignoring {} headers from peer#{}. Unknown and we are synchronizing. " , headers . len ( ) , peer_index ) ;
} ,
2016-11-08 05:16:05 -08:00
HeadersIntersection ::DbAllBlocksKnown = > {
2016-11-11 00:38:41 -08:00
trace! ( target : " sync " , " Ignoring {} headers from peer#{}. All blocks are known and in database. " , headers . len ( ) , peer_index ) ;
2016-11-07 01:12:34 -08:00
if self . state . is_synchronizing ( ) {
// remember peer as useful
2016-11-14 11:24:45 -08:00
self . peers . useful_peer ( peer_index ) ;
2016-11-02 03:15:37 -07:00
}
2016-11-07 01:12:34 -08:00
} ,
2016-11-08 05:16:05 -08:00
HeadersIntersection ::InMemoryNoNewBlocks = > {
2016-11-11 00:38:41 -08:00
trace! ( target : " sync " , " Ignoring {} headers from peer#{}. All blocks are known and in memory. " , headers . len ( ) , peer_index ) ;
2016-11-07 01:12:34 -08:00
// remember peer as useful
2016-11-14 11:24:45 -08:00
self . peers . useful_peer ( peer_index ) ;
2016-11-07 01:12:34 -08:00
} ,
2016-11-08 05:16:05 -08:00
HeadersIntersection ::InMemoryMainNewBlocks ( new_block_index )
| HeadersIntersection ::InMemoryForkNewBlocks ( new_block_index )
| HeadersIntersection ::DbForkNewBlocks ( new_block_index )
2016-12-04 23:13:22 -08:00
| HeadersIntersection ::NoKnownBlocks ( new_block_index )
| HeadersIntersection ::DeadEnd ( new_block_index ) = > {
if let HeadersIntersection ::DeadEnd ( dead_block_index ) = intersection_result {
warn! ( target : " sync " , " Peer#{} has provided us with dead-end block {:?} " , peer_index , hashes [ dead_block_index ] . to_reversed_str ( ) ) ;
if self . config . close_connection_on_bad_block {
self . executor . lock ( ) . execute ( Task ::Close ( peer_index ) ) ;
return ;
}
}
2016-11-18 03:53:59 -08:00
// check that we do not know all blocks in range [new_block_index..]
// if we know some block => there has been verification error => all headers should be ignored
// see when_previous_block_verification_failed_fork_is_not_requested for details
2016-12-02 07:12:20 -08:00
if hashes . iter ( ) . skip ( new_block_index ) . any ( | h | {
let block_state = chain . block_state ( h ) ;
match block_state {
BlockState ::Unknown = > false ,
BlockState ::DeadEnd = > {
2016-12-04 23:13:22 -08:00
warn! ( target : " sync " , " Peer#{} has provided us with blocks leading to dead-end block {:?} " , peer_index , h . to_reversed_str ( ) ) ;
if self . config . close_connection_on_bad_block {
self . executor . lock ( ) . execute ( Task ::Close ( peer_index ) ) ;
true
} else {
false
}
} ,
2016-12-02 07:12:20 -08:00
_ = > true ,
}
} ) {
2016-11-18 03:53:59 -08:00
return ;
}
2016-11-07 01:12:34 -08:00
// schedule new blocks
2016-11-08 05:16:05 -08:00
let new_blocks_hashes = hashes . split_off ( new_block_index ) ;
let new_blocks_headers = headers . split_off ( new_block_index ) ;
let new_blocks_hashes_len = new_blocks_hashes . len ( ) ;
2016-11-16 06:49:36 -08:00
trace! (
2016-11-17 05:28:42 -08:00
target : " sync " , " New {} headers from peer#{}. First {:?}, last: {:?} " ,
2016-11-16 06:49:36 -08:00
new_blocks_hashes_len ,
peer_index ,
new_blocks_hashes [ 0 ] . to_reversed_str ( ) ,
new_blocks_hashes [ new_blocks_hashes_len - 1 ] . to_reversed_str ( )
) ;
2016-11-08 05:16:05 -08:00
chain . schedule_blocks_headers ( new_blocks_hashes , new_blocks_headers ) ;
2016-11-07 01:12:34 -08:00
// remember peer as useful
2016-11-14 11:24:45 -08:00
self . peers . useful_peer ( peer_index ) ;
2016-11-07 01:12:34 -08:00
// switch to synchronization state
if ! self . state . is_synchronizing ( ) {
2016-11-16 05:46:21 -08:00
// TODO: NearlySaturated should start when we are in Saturated state && count(new_blocks_headers) is < LIMIT (LIMIT > 1)
2016-11-08 05:16:05 -08:00
if new_blocks_hashes_len = = 1 & & ! self . state . is_nearly_saturated ( ) {
self . state = State ::NearlySaturated ;
}
else {
self . state = State ::Synchronizing ( time ::precise_time_s ( ) , chain . best_storage_block ( ) . number ) ;
}
2016-11-02 03:15:37 -07:00
}
}
}
2016-10-24 23:32:56 -07:00
}
2016-10-29 02:52:40 -07:00
/// Process new peer block
2016-11-28 04:55:00 -08:00
fn process_peer_block ( & mut self , peer_index : usize , block_hash : H256 , block : IndexedBlock ) -> Option < VecDeque < ( H256 , IndexedBlock ) > > {
2016-11-18 00:52:45 -08:00
// prepare list of blocks to verify + make all required changes to the chain
2016-11-28 04:55:00 -08:00
let mut result : Option < VecDeque < ( H256 , IndexedBlock ) > > = None ;
2016-11-18 00:52:45 -08:00
let mut chain = self . chain . write ( ) ;
2016-12-04 23:13:22 -08:00
let block_state = chain . block_state ( & block_hash ) ;
match block_state {
2016-11-18 00:52:45 -08:00
BlockState ::Verifying | BlockState ::Stored = > {
// remember peer as useful
self . peers . useful_peer ( peer_index ) ;
} ,
2016-12-04 23:13:22 -08:00
BlockState ::Unknown | BlockState ::Scheduled | BlockState ::Requested | BlockState ::DeadEnd = > {
if block_state = = BlockState ::DeadEnd {
warn! ( target : " sync " , " Peer#{} has provided us with dead-end block {:?} " , peer_index , block_hash . to_reversed_str ( ) ) ;
if self . config . close_connection_on_bad_block {
2016-12-02 07:12:20 -08:00
self . executor . lock ( ) . execute ( Task ::Close ( peer_index ) ) ;
2016-12-04 23:13:22 -08:00
}
}
// check parent block state
let parent_block_state = chain . block_state ( & block . header ( ) . previous_header_hash ) ;
match parent_block_state {
BlockState ::Unknown | BlockState ::DeadEnd = > {
if parent_block_state = = BlockState ::DeadEnd {
warn! ( target : " sync " , " Peer#{} has provided us with dead-end block {:?} " , peer_index , block_hash . to_reversed_str ( ) ) ;
if self . config . close_connection_on_bad_block {
self . executor . lock ( ) . execute ( Task ::Close ( peer_index ) ) ;
return result ;
}
}
2016-11-18 00:52:45 -08:00
if self . state . is_synchronizing ( ) {
// when synchronizing, we tend to receive all blocks in-order
trace! (
target : " sync " ,
" Ignoring block {} from peer#{}, because its parent is unknown and we are synchronizing " ,
block_hash . to_reversed_str ( ) ,
peer_index
) ;
// remove block from current queue
chain . forget_block ( & block_hash ) ;
// remove orphaned blocks
let removed_blocks_hashes : Vec < _ > = self . orphaned_blocks_pool . remove_blocks_for_parent ( & block_hash ) . into_iter ( ) . map ( | t | t . 0 ) . collect ( ) ;
chain . forget_blocks_leave_header ( & removed_blocks_hashes ) ;
} else {
// remove this block from the queue
chain . forget_block_leave_header ( & block_hash ) ;
// remember this block as unknown
2016-11-25 07:57:56 -08:00
if ! self . orphaned_blocks_pool . contains_unknown_block ( & block_hash ) {
self . orphaned_blocks_pool . insert_unknown_block ( block_hash , block ) ;
}
2016-11-18 00:52:45 -08:00
}
} ,
BlockState ::Verifying | BlockState ::Stored = > {
// remember peer as useful
self . peers . useful_peer ( peer_index ) ;
// schedule verification
2016-11-28 04:55:00 -08:00
let mut blocks_to_verify : VecDeque < ( H256 , IndexedBlock ) > = VecDeque ::new ( ) ;
2016-11-18 00:52:45 -08:00
blocks_to_verify . push_back ( ( block_hash . clone ( ) , block ) ) ;
blocks_to_verify . extend ( self . orphaned_blocks_pool . remove_blocks_for_parent ( & block_hash ) ) ;
// forget blocks we are going to process
let blocks_hashes_to_forget : Vec < _ > = blocks_to_verify . iter ( ) . map ( | t | t . 0. clone ( ) ) . collect ( ) ;
chain . forget_blocks_leave_header ( & blocks_hashes_to_forget ) ;
// remember that we are verifying these blocks
2016-11-28 04:55:00 -08:00
let blocks_headers_to_verify : Vec < _ > = blocks_to_verify . iter ( ) . map ( | & ( ref h , ref b ) | ( h . clone ( ) , b . header ( ) . clone ( ) ) ) . collect ( ) ;
2016-11-18 00:52:45 -08:00
chain . verify_blocks ( blocks_headers_to_verify ) ;
// remember that we are verifying block from this peer
2016-11-29 04:40:26 -08:00
for verifying_block_hash in blocks_to_verify . iter ( ) . map ( | & ( ref h , _ ) | h . clone ( ) ) {
self . verifying_blocks_by_peer . insert ( verifying_block_hash , peer_index ) ;
2016-11-25 04:43:24 -08:00
}
2016-11-21 03:10:04 -08:00
match self . verifying_blocks_futures . entry ( peer_index ) {
2016-11-18 00:52:45 -08:00
Entry ::Occupied ( mut entry ) = > {
2016-11-29 04:40:26 -08:00
entry . get_mut ( ) . 0. extend ( blocks_to_verify . iter ( ) . map ( | & ( ref h , _ ) | h . clone ( ) ) ) ;
2016-11-18 00:52:45 -08:00
} ,
Entry ::Vacant ( entry ) = > {
2016-11-29 04:40:26 -08:00
let block_hashes : HashSet < _ > = blocks_to_verify . iter ( ) . map ( | & ( ref h , _ ) | h . clone ( ) ) . collect ( ) ;
2016-11-21 03:10:04 -08:00
entry . insert ( ( block_hashes , Vec ::new ( ) ) ) ;
2016-11-07 01:12:34 -08:00
}
}
2016-11-18 00:52:45 -08:00
result = Some ( blocks_to_verify ) ;
} ,
BlockState ::Requested | BlockState ::Scheduled = > {
// remember peer as useful
self . peers . useful_peer ( peer_index ) ;
// remember as orphan block
self . orphaned_blocks_pool . insert_orphaned_block ( block_hash , block ) ;
2016-10-29 02:52:40 -07:00
}
2016-11-18 00:52:45 -08:00
}
} ,
2016-11-07 01:12:34 -08:00
}
2016-11-18 00:52:45 -08:00
result
2016-10-24 23:32:56 -07:00
}
2016-11-16 05:46:21 -08:00
/// Process new peer transaction
2016-11-30 05:40:07 -08:00
fn process_peer_transaction ( & mut self , _peer_index : Option < usize > , hash : H256 , transaction : Transaction , relay : bool ) -> Option < VecDeque < ( H256 , Transaction ) > > {
2016-11-16 05:46:21 -08:00
// if we are in synchronization state, we will ignore this message
if self . state . is_synchronizing ( ) {
2016-11-18 00:52:45 -08:00
return None ;
2016-11-16 05:46:21 -08:00
}
// else => verify transaction + it's orphans and then add to the memory pool
let mut chain = self . chain . write ( ) ;
// if any parent transaction is unknown => we have orphan transaction => remember in orphan pool
let unknown_parents : HashSet < H256 > = transaction . inputs . iter ( )
. filter ( | input | chain . transaction_state ( & input . previous_output . hash ) = = TransactionState ::Unknown )
. map ( | input | input . previous_output . hash . clone ( ) )
. collect ( ) ;
if ! unknown_parents . is_empty ( ) {
self . orphaned_transactions_pool . insert ( hash , transaction , unknown_parents ) ;
2016-11-18 00:52:45 -08:00
return None ;
2016-11-16 05:46:21 -08:00
}
// else verify && insert this transaction && all dependent orphans
2016-11-30 04:15:48 -08:00
let mut transactions : VecDeque < ( H256 , Transaction ) > = VecDeque ::new ( ) ;
transactions . push_back ( ( hash . clone ( ) , transaction ) ) ;
transactions . extend ( self . orphaned_transactions_pool . remove_transactions_for_parent ( & hash ) ) ;
2016-11-18 00:52:45 -08:00
// remember that we are verifying these transactions
2016-11-30 04:15:48 -08:00
for & ( ref h , ref tx ) in & transactions {
2016-11-18 00:52:45 -08:00
chain . verify_transaction ( h . clone ( ) , tx . clone ( ) ) ;
2016-11-30 04:15:48 -08:00
if ! relay {
self . do_not_relay . insert ( h . clone ( ) ) ;
}
2016-11-14 11:24:45 -08:00
}
2016-11-30 04:15:48 -08:00
Some ( transactions )
2016-10-29 02:52:40 -07:00
}
2016-11-08 05:16:05 -08:00
fn prepare_blocks_requests_tasks ( & mut self , peers : Vec < usize > , mut hashes : Vec < H256 > ) -> Vec < Task > {
use std ::mem ::swap ;
2016-12-01 08:40:23 -08:00
// TODO: ask most fast peers for hashes at the beginning of `hashes`
2016-11-08 05:16:05 -08:00
let chunk_size = min ( MAX_BLOCKS_IN_REQUEST , max ( hashes . len ( ) as u32 , MIN_BLOCKS_IN_REQUEST ) ) ;
let last_peer_index = peers . len ( ) - 1 ;
let mut tasks : Vec < Task > = Vec ::new ( ) ;
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 ) } ;
if peer_chunk_size = = 0 {
break ;
}
let mut chunk_hashes = hashes . split_off ( peer_chunk_size ) ;
swap ( & mut chunk_hashes , & mut hashes ) ;
self . peers . on_blocks_requested ( peer , & chunk_hashes ) ;
tasks . push ( Task ::RequestBlocks ( peer , chunk_hashes ) ) ;
}
assert_eq! ( hashes . len ( ) , 0 ) ;
tasks
}
2016-11-07 01:12:34 -08:00
/// Switch to saturated state
2016-11-14 02:02:25 -08:00
fn switch_to_saturated_state ( & mut self ) {
2016-11-10 06:52:40 -08:00
if self . state . is_saturated ( ) {
return ;
}
2016-11-07 01:12:34 -08:00
self . state = State ::Saturated ;
self . peers . reset ( ) ;
2016-11-11 00:38:41 -08:00
// remove sync orphans, but leave unknown orphans until they'll be removed by management thread
2016-11-16 05:46:21 -08:00
let removed_orphans = self . orphaned_blocks_pool . remove_known_blocks ( ) ;
2016-11-11 00:38:41 -08:00
2016-11-08 05:16:05 -08:00
// leave currently verifying blocks
2016-11-07 01:12:34 -08:00
{
let mut chain = self . chain . write ( ) ;
2016-11-16 05:46:21 -08:00
chain . forget_blocks ( & removed_orphans ) ;
chain . forget_all_blocks_with_state ( BlockState ::Requested ) ;
chain . forget_all_blocks_with_state ( BlockState ::Scheduled ) ;
2016-11-10 06:52:40 -08:00
use time ;
info! ( target : " sync " , " {:?} @ Switched to saturated state. Chain information: {:?} " ,
time ::strftime ( " %H:%M:%S " , & time ::now ( ) ) . unwrap ( ) ,
chain . information ( ) ) ;
2016-11-07 01:12:34 -08:00
}
2016-11-16 05:46:21 -08:00
// finally - ask all known peers for their best blocks inventory, in case if some peer
2016-11-14 02:02:25 -08:00
// has lead us to the fork
2016-11-16 05:46:21 -08:00
// + ask all peers for their memory pool
2016-11-14 02:02:25 -08:00
{
2016-11-07 01:12:34 -08:00
let mut executor = self . executor . lock ( ) ;
2016-11-14 02:02:25 -08:00
for peer in self . peers . all_peers ( ) {
executor . execute ( Task ::RequestBlocksHeaders ( peer ) ) ;
2016-11-16 05:46:21 -08:00
executor . execute ( Task ::RequestMemoryPool ( peer ) ) ;
2016-11-08 05:16:05 -08:00
}
}
}
/// Awake threads, waiting for this block
fn awake_waiting_threads ( & mut self , hash : & H256 ) {
// find a peer, which has supplied us with this block
if let Entry ::Occupied ( block_entry ) = self . verifying_blocks_by_peer . entry ( hash . clone ( ) ) {
let peer_index = * block_entry . get ( ) ;
// find a # of blocks, which this thread has supplied
2016-11-21 03:10:04 -08:00
if let Entry ::Occupied ( mut entry ) = self . verifying_blocks_futures . entry ( peer_index ) {
2016-11-17 03:20:37 -08:00
let is_last_block = {
2016-11-21 03:10:04 -08:00
let & mut ( ref mut waiting , ref mut futures ) = entry . get_mut ( ) ;
2016-11-08 05:16:05 -08:00
waiting . remove ( hash ) ;
// if this is the last block => awake waiting threads
let is_last_block = waiting . is_empty ( ) ;
if is_last_block {
2016-11-21 03:10:04 -08:00
for future in futures . drain ( .. ) {
future . wait ( ) . expect ( " no-error future " ) ;
2016-11-08 05:16:05 -08:00
}
}
is_last_block
2016-11-17 03:20:37 -08:00
} ;
if is_last_block {
2016-11-08 05:16:05 -08:00
entry . remove_entry ( ) ;
}
2016-11-07 01:12:34 -08:00
}
2016-11-08 05:16:05 -08:00
block_entry . remove_entry ( ) ;
2016-11-07 01:12:34 -08:00
}
}
2016-11-14 11:24:45 -08:00
/// Print synchronization information
fn print_synchronization_information ( & mut self ) {
if let State ::Synchronizing ( timestamp , num_of_blocks ) = self . state {
let chain = self . chain . read ( ) ;
let new_timestamp = time ::precise_time_s ( ) ;
let timestamp_diff = new_timestamp - timestamp ;
let new_num_of_blocks = chain . best_storage_block ( ) . number ;
let blocks_diff = if new_num_of_blocks > num_of_blocks { new_num_of_blocks - num_of_blocks } else { 0 } ;
if timestamp_diff > = 60.0 | | blocks_diff > 1000 {
self . state = State ::Synchronizing ( time ::precise_time_s ( ) , new_num_of_blocks ) ;
use time ;
info! ( target : " sync " , " {:?} @ Processed {} blocks in {} seconds. Chain information: {:?} "
, time ::strftime ( " %H:%M:%S " , & time ::now ( ) ) . unwrap ( )
, blocks_diff , timestamp_diff
, chain . information ( ) ) ;
}
}
}
2016-10-24 23:32:56 -07:00
}
2016-12-01 03:55:21 -08:00
impl < ' a > MessageBlockHeadersProvider < ' a > {
pub fn new ( chain : & ' a Chain ) -> Self {
let first_header_number = chain . best_block_header ( ) . number + 1 ;
MessageBlockHeadersProvider {
chain : chain ,
first_header_number : first_header_number ,
headers : HashMap ::new ( ) ,
headers_order : Vec ::new ( ) ,
}
}
pub fn append_header ( & mut self , hash : H256 , header : BlockHeader ) {
self . headers . insert ( hash . clone ( ) , header ) ;
self . headers_order . push ( hash ) ;
}
}
impl < ' a > BlockHeaderProvider for MessageBlockHeadersProvider < ' a > {
fn block_header_bytes ( & self , block_ref : BlockRef ) -> Option < Bytes > {
use ser ::serialize ;
self . block_header ( block_ref ) . map ( | h | serialize ( & h ) )
}
fn block_header ( & self , block_ref : BlockRef ) -> Option < BlockHeader > {
match block_ref {
BlockRef ::Hash ( h ) = > self . chain . block_header_by_hash ( & h )
. or_else ( | | self . headers . get ( & h ) . cloned ( ) ) ,
BlockRef ::Number ( n ) = > self . chain . block_header_by_number ( n )
. or_else ( | | if n > = self . first_header_number & & n - self . first_header_number < self . headers_order . len ( ) as u32 {
let ref header_hash = self . headers_order [ ( n - self . first_header_number ) as usize ] ;
Some ( self . headers [ header_hash ] . clone ( ) )
} else {
None
} ) ,
}
}
}
2016-12-01 08:40:23 -08:00
impl AverageSpeedMeter {
pub fn with_inspect_items ( inspect_items : usize ) -> Self {
assert! ( inspect_items > 0 ) ;
AverageSpeedMeter {
inspect_items : inspect_items ,
inspected_items : VecDeque ::with_capacity ( inspect_items ) ,
speed : 0_ f64 ,
last_timestamp : None ,
}
}
pub fn speed ( & self ) -> f64 {
let items_per_second = 1_ f64 / self . speed ;
if items_per_second . is_normal ( ) { items_per_second } else { 0_ f64 }
}
pub fn inspected_items_len ( & self ) -> usize {
self . inspected_items . len ( )
}
pub fn checkpoint ( & mut self ) {
// if inspected_items is already full => remove oldest item from average
if self . inspected_items . len ( ) = = self . inspect_items {
let oldest = self . inspected_items . pop_front ( ) . expect ( " len() is not zero; qed " ) ;
self . speed = ( self . inspect_items as f64 * self . speed - oldest ) / ( self . inspect_items as f64 - 1_ f64 ) ;
}
// add new item
let now = time ::precise_time_s ( ) ;
if let Some ( last_timestamp ) = self . last_timestamp {
let newest = now - last_timestamp ;
self . speed = ( self . inspected_items . len ( ) as f64 * self . speed + newest ) / ( self . inspected_items . len ( ) as f64 + 1_ f64 ) ;
self . inspected_items . push_back ( newest ) ;
}
self . last_timestamp = Some ( now ) ;
}
}
2016-10-24 23:32:56 -07:00
#[ cfg(test) ]
2016-10-31 08:22:02 -07:00
pub mod tests {
2016-10-29 02:52:40 -07:00
use std ::sync ::Arc ;
use parking_lot ::{ Mutex , RwLock } ;
2016-11-04 02:36:58 -07:00
use tokio_core ::reactor ::{ Core , Handle } ;
2016-11-21 22:14:31 -08:00
use chain ::{ Block , Transaction } ;
use message ::common ::{ InventoryVector , InventoryType } ;
2016-11-25 00:09:18 -08:00
use message ::types ;
2016-12-01 04:08:13 -08:00
use super ::{ Client , Config , SynchronizationClient , SynchronizationClientCore , BlockAnnouncementType , MessageBlockHeadersProvider } ;
2016-11-23 02:48:36 -08:00
use connection_filter ::tests ::* ;
2016-11-02 04:20:39 -07:00
use synchronization_executor ::Task ;
2016-10-26 07:46:47 -07:00
use synchronization_chain ::{ Chain , ChainRef } ;
2016-11-02 04:20:39 -07:00
use synchronization_executor ::tests ::DummyTaskExecutor ;
2016-11-18 00:52:45 -08:00
use synchronization_verifier ::tests ::DummyVerifier ;
2016-11-21 22:14:31 -08:00
use synchronization_server ::ServerTaskIndex ;
2016-11-16 05:46:21 -08:00
use primitives ::hash ::H256 ;
2016-11-04 02:36:58 -07:00
use p2p ::event_loop ;
2016-11-02 03:15:37 -07:00
use test_data ;
2016-12-01 04:08:13 -08:00
use db ::{ self , BlockHeaderProvider } ;
2016-11-10 04:16:16 -08:00
use devtools ::RandomTempPath ;
2016-10-29 02:52:40 -07:00
2016-11-17 10:37:45 -08:00
fn create_disk_storage ( ) -> db ::SharedStore {
2016-11-10 04:16:16 -08:00
let path = RandomTempPath ::create_dir ( ) ;
Arc ::new ( db ::Storage ::new ( path . as_path ( ) ) . unwrap ( ) )
}
2016-11-18 03:00:14 -08:00
fn create_sync ( storage : Option < db ::SharedStore > , verifier : Option < DummyVerifier > ) -> ( Core , Handle , Arc < Mutex < DummyTaskExecutor > > , ChainRef , Arc < Mutex < SynchronizationClient < DummyTaskExecutor , DummyVerifier > > > ) {
2016-11-04 02:36:58 -07:00
let event_loop = event_loop ( ) ;
let handle = event_loop . handle ( ) ;
2016-11-10 04:16:16 -08:00
let storage = match storage {
Some ( storage ) = > storage ,
None = > Arc ::new ( db ::TestStorage ::with_genesis_block ( ) ) ,
} ;
2016-10-29 02:52:40 -07:00
let chain = ChainRef ::new ( RwLock ::new ( Chain ::new ( storage . clone ( ) ) ) ) ;
2016-11-02 04:20:39 -07:00
let executor = DummyTaskExecutor ::new ( ) ;
2016-12-05 00:00:27 -08:00
let config = Config { threads_num : 1 , close_connection_on_bad_block : true } ;
2016-11-10 03:56:58 -08:00
2016-12-01 03:55:21 -08:00
let client_core = SynchronizationClientCore ::new ( config , & handle , executor . clone ( ) , chain . clone ( ) , Magic ::Testnet ) ;
{
client_core . lock ( ) . verify_headers ( false ) ;
}
2016-11-18 06:10:57 -08:00
let mut verifier = verifier . unwrap_or_default ( ) ;
2016-11-18 03:39:34 -08:00
verifier . set_sink ( client_core . clone ( ) ) ;
let client = SynchronizationClient ::new ( client_core , verifier ) ;
2016-11-08 05:16:05 -08:00
( event_loop , handle , executor , chain , client )
2016-11-16 06:49:36 -08:00
}
2016-10-24 23:32:56 -07:00
#[ test ]
fn synchronization_saturated_on_start ( ) {
2016-11-18 03:00:14 -08:00
let ( _ , _ , _ , _ , sync ) = create_sync ( None , None ) ;
2016-10-29 02:52:40 -07:00
let sync = sync . lock ( ) ;
2016-10-24 23:32:56 -07:00
let info = sync . information ( ) ;
2016-10-29 02:52:40 -07:00
assert! ( ! info . state . is_synchronizing ( ) ) ;
2016-11-16 05:46:21 -08:00
assert_eq! ( info . orphaned_blocks , 0 ) ;
assert_eq! ( info . orphaned_transactions , 0 ) ;
2016-10-24 23:32:56 -07:00
}
#[ test ]
2016-11-08 05:16:05 -08:00
fn synchronization_in_order_block_path_nearly_saturated ( ) {
2016-11-18 03:00:14 -08:00
let ( _ , _ , executor , _ , sync ) = create_sync ( None , None ) ;
2016-10-24 23:32:56 -07:00
2016-10-29 02:52:40 -07:00
let mut sync = sync . lock ( ) ;
2016-11-07 01:12:34 -08:00
let block1 : Block = test_data ::block_h1 ( ) ;
let block2 : Block = test_data ::block_h2 ( ) ;
2016-10-24 23:32:56 -07:00
2016-11-08 05:16:05 -08:00
sync . on_new_blocks_headers ( 5 , vec! [ block1 . block_header . clone ( ) ] ) ;
2016-10-29 02:52:40 -07:00
let tasks = executor . lock ( ) . take_tasks ( ) ;
2016-11-14 11:24:45 -08:00
assert_eq! ( tasks , vec! [ Task ::RequestBlocksHeaders ( 5 ) , Task ::RequestBlocks ( 5 , vec! [ block1 . hash ( ) ] ) ] ) ;
2016-11-08 05:16:05 -08:00
assert! ( sync . information ( ) . state . is_nearly_saturated ( ) ) ;
2016-11-16 05:46:21 -08:00
assert_eq! ( sync . information ( ) . orphaned_blocks , 0 ) ;
2016-10-26 07:33:28 -07:00
assert_eq! ( sync . information ( ) . chain . scheduled , 0 ) ;
assert_eq! ( sync . information ( ) . chain . requested , 1 ) ;
assert_eq! ( sync . information ( ) . chain . stored , 1 ) ;
assert_eq! ( sync . information ( ) . peers . idle , 0 ) ;
assert_eq! ( sync . information ( ) . peers . active , 1 ) ;
2016-10-24 23:32:56 -07:00
2016-11-07 01:12:34 -08:00
// push unknown block => will be queued as orphan
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 5 , block2 . into ( ) ) ;
2016-11-08 05:16:05 -08:00
assert! ( sync . information ( ) . state . is_nearly_saturated ( ) ) ;
2016-11-16 05:46:21 -08:00
assert_eq! ( sync . information ( ) . orphaned_blocks , 1 ) ;
2016-10-26 07:33:28 -07:00
assert_eq! ( sync . information ( ) . chain . scheduled , 0 ) ;
assert_eq! ( sync . information ( ) . chain . requested , 1 ) ;
assert_eq! ( sync . information ( ) . chain . stored , 1 ) ;
assert_eq! ( sync . information ( ) . peers . idle , 0 ) ;
assert_eq! ( sync . information ( ) . peers . active , 1 ) ;
2016-10-24 23:32:56 -07:00
2016-11-07 01:12:34 -08:00
// push requested block => should be moved to the test storage && orphan should be moved
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 5 , block1 . into ( ) ) ;
2016-11-08 05:16:05 -08:00
assert! ( sync . information ( ) . state . is_saturated ( ) ) ;
2016-11-16 05:46:21 -08:00
assert_eq! ( sync . information ( ) . orphaned_blocks , 0 ) ;
2016-10-26 07:33:28 -07:00
assert_eq! ( sync . information ( ) . chain . scheduled , 0 ) ;
assert_eq! ( sync . information ( ) . chain . requested , 0 ) ;
2016-11-07 01:12:34 -08:00
assert_eq! ( sync . information ( ) . chain . stored , 3 ) ;
2016-10-31 04:46:11 -07:00
// we have just requested new `inventory` from the peer => peer is forgotten
assert_eq! ( sync . information ( ) . peers . idle , 0 ) ;
2016-10-26 07:33:28 -07:00
assert_eq! ( sync . information ( ) . peers . active , 0 ) ;
2016-10-24 23:32:56 -07:00
}
#[ test ]
fn synchronization_out_of_order_block_path ( ) {
2016-11-18 03:00:14 -08:00
let ( _ , _ , _ , _ , sync ) = create_sync ( None , None ) ;
2016-10-29 02:52:40 -07:00
let mut sync = sync . lock ( ) ;
2016-10-24 23:32:56 -07:00
2016-11-11 00:38:41 -08:00
sync . on_new_blocks_headers ( 5 , vec! [ test_data ::block_h1 ( ) . block_header . clone ( ) , test_data ::block_h2 ( ) . block_header . clone ( ) ] ) ;
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 5 , test_data ::block_h169 ( ) . into ( ) ) ;
2016-10-24 23:32:56 -07:00
// out-of-order block was presented by the peer
2016-11-11 00:38:41 -08:00
assert! ( sync . information ( ) . state . is_synchronizing ( ) ) ;
2016-11-16 05:46:21 -08:00
assert_eq! ( sync . information ( ) . orphaned_blocks , 0 ) ;
2016-10-26 07:33:28 -07:00
assert_eq! ( sync . information ( ) . chain . scheduled , 0 ) ;
2016-11-11 00:38:41 -08:00
assert_eq! ( sync . information ( ) . chain . requested , 2 ) ;
2016-10-26 07:33:28 -07:00
assert_eq! ( sync . information ( ) . chain . stored , 1 ) ;
2016-10-31 04:46:11 -07:00
// we have just requested new `inventory` from the peer => peer is forgotten
assert_eq! ( sync . information ( ) . peers . idle , 0 ) ;
2016-11-11 00:38:41 -08:00
assert_eq! ( sync . information ( ) . peers . active , 1 ) ;
2016-10-24 23:32:56 -07:00
// TODO: check that peer is penalized
}
2016-10-31 09:18:58 -07:00
#[ test ]
fn synchronization_parallel_peers ( ) {
2016-11-18 03:00:14 -08:00
let ( _ , _ , executor , _ , sync ) = create_sync ( None , None ) ;
2016-10-31 09:18:58 -07:00
2016-11-07 01:12:34 -08:00
let block1 : Block = test_data ::block_h1 ( ) ;
let block2 : Block = test_data ::block_h2 ( ) ;
2016-10-31 09:18:58 -07:00
{
let mut sync = sync . lock ( ) ;
// not synchronizing after start
2016-11-08 05:16:05 -08:00
assert! ( sync . information ( ) . state . is_saturated ( ) ) ;
2016-10-31 09:18:58 -07:00
// receive inventory from new peer#1
2016-11-08 05:16:05 -08:00
sync . on_new_blocks_headers ( 1 , vec! [ block1 . block_header . clone ( ) ] ) ;
2016-10-31 09:18:58 -07:00
assert_eq! ( sync . information ( ) . chain . requested , 1 ) ;
// synchronization has started && new blocks have been requested
let tasks = executor . lock ( ) . take_tasks ( ) ;
2016-11-08 05:16:05 -08:00
assert! ( sync . information ( ) . state . is_nearly_saturated ( ) ) ;
2016-11-14 11:24:45 -08:00
assert_eq! ( tasks , vec! [ Task ::RequestBlocksHeaders ( 1 ) , Task ::RequestBlocks ( 1 , vec! [ block1 . hash ( ) ] ) ] ) ;
2016-10-31 09:18:58 -07:00
}
{
let mut sync = sync . lock ( ) ;
// receive inventory from new peer#2
2016-11-08 05:16:05 -08:00
sync . on_new_blocks_headers ( 2 , vec! [ block1 . block_header . clone ( ) , block2 . block_header . clone ( ) ] ) ;
2016-10-31 09:18:58 -07:00
assert_eq! ( sync . information ( ) . chain . requested , 2 ) ;
// synchronization has started && new blocks have been requested
let tasks = executor . lock ( ) . take_tasks ( ) ;
assert! ( sync . information ( ) . state . is_synchronizing ( ) ) ;
2016-11-08 05:16:05 -08:00
assert_eq! ( tasks , vec! [ Task ::RequestBlocksHeaders ( 2 ) , Task ::RequestBlocks ( 2 , vec! [ block2 . hash ( ) ] ) ] ) ;
2016-10-31 09:18:58 -07:00
}
{
let mut sync = sync . lock ( ) ;
// receive block from peer#2
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 2 , block2 . into ( ) ) ;
2016-11-10 05:22:03 -08:00
assert! ( sync . information ( ) . chain . requested = = 2
2016-11-16 05:46:21 -08:00
& & sync . information ( ) . orphaned_blocks = = 1 ) ;
2016-10-31 09:18:58 -07:00
// receive block from peer#1
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 1 , block1 . into ( ) ) ;
2016-11-11 01:19:45 -08:00
2016-10-31 09:18:58 -07:00
assert! ( sync . information ( ) . chain . requested = = 0
2016-11-16 05:46:21 -08:00
& & sync . information ( ) . orphaned_blocks = = 0
2016-10-31 09:18:58 -07:00
& & sync . information ( ) . chain . stored = = 3 ) ;
}
}
2016-10-31 10:03:08 -07:00
#[ test ]
fn synchronization_reset_when_peer_is_disconnected ( ) {
2016-11-18 03:00:14 -08:00
let ( _ , _ , _ , _ , sync ) = create_sync ( None , None ) ;
2016-10-31 10:03:08 -07:00
// request new blocks
{
let mut sync = sync . lock ( ) ;
2016-11-08 05:16:05 -08:00
sync . on_new_blocks_headers ( 1 , vec! [ test_data ::block_h1 ( ) . block_header ] ) ;
assert! ( sync . information ( ) . state . is_nearly_saturated ( ) ) ;
2016-10-31 10:03:08 -07:00
}
// lost connection to peer => synchronization state lost
{
let mut sync = sync . lock ( ) ;
sync . on_peer_disconnected ( 1 ) ;
2016-11-08 05:16:05 -08:00
assert! ( sync . information ( ) . state . is_saturated ( ) ) ;
2016-10-31 10:03:08 -07:00
}
}
2016-11-02 03:15:37 -07:00
#[ test ]
fn synchronization_not_starting_when_receiving_known_blocks ( ) {
2016-11-18 03:00:14 -08:00
let ( _ , _ , executor , _ , sync ) = create_sync ( None , None ) ;
2016-11-02 03:15:37 -07:00
let mut sync = sync . lock ( ) ;
// saturated => receive inventory with known blocks only
2016-11-08 05:16:05 -08:00
sync . on_new_blocks_headers ( 1 , vec! [ test_data ::genesis ( ) . block_header ] ) ;
2016-11-02 03:15:37 -07:00
// => no need to start synchronization
2016-11-08 05:16:05 -08:00
assert! ( ! sync . information ( ) . state . is_nearly_saturated ( ) ) ;
2016-11-02 03:15:37 -07:00
// => no synchronization tasks are scheduled
let tasks = executor . lock ( ) . take_tasks ( ) ;
assert_eq! ( tasks , vec! [ ] ) ;
}
2016-11-07 01:12:34 -08:00
#[ test ]
fn synchronization_asks_for_inventory_after_saturating ( ) {
2016-11-18 03:00:14 -08:00
let ( _ , _ , executor , _ , sync ) = create_sync ( None , None ) ;
2016-11-07 01:12:34 -08:00
let mut sync = sync . lock ( ) ;
let block = test_data ::block_h1 ( ) ;
2016-11-08 05:16:05 -08:00
sync . on_new_blocks_headers ( 1 , vec! [ block . block_header . clone ( ) ] ) ;
sync . on_new_blocks_headers ( 2 , vec! [ block . block_header . clone ( ) ] ) ;
2016-11-07 01:12:34 -08:00
executor . lock ( ) . take_tasks ( ) ;
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 2 , block . clone ( ) . into ( ) ) ;
2016-11-07 01:12:34 -08:00
let tasks = executor . lock ( ) . take_tasks ( ) ;
2016-11-21 09:06:14 -08:00
assert_eq! ( tasks . len ( ) , 5 ) ;
2016-11-08 05:16:05 -08:00
assert! ( tasks . iter ( ) . any ( | t | t = = & Task ::RequestBlocksHeaders ( 1 ) ) ) ;
assert! ( tasks . iter ( ) . any ( | t | t = = & Task ::RequestBlocksHeaders ( 2 ) ) ) ;
2016-11-16 05:46:21 -08:00
assert! ( tasks . iter ( ) . any ( | t | t = = & Task ::RequestMemoryPool ( 1 ) ) ) ;
assert! ( tasks . iter ( ) . any ( | t | t = = & Task ::RequestMemoryPool ( 2 ) ) ) ;
2016-11-21 22:14:31 -08:00
let inventory = vec! [ InventoryVector { inv_type : InventoryType ::MessageBlock , hash : block . hash ( ) } ] ;
2016-11-29 08:39:00 -08:00
assert! ( tasks . iter ( ) . any ( | t | t = = & Task ::SendInventory ( 1 , inventory . clone ( ) ) ) ) ;
2016-11-08 05:16:05 -08:00
}
#[ test ]
fn synchronization_remembers_correct_block_headers_in_order ( ) {
2016-11-18 03:00:14 -08:00
let ( _ , _ , executor , chain , sync ) = create_sync ( None , None ) ;
2016-11-08 05:16:05 -08:00
let mut sync = sync . lock ( ) ;
let b1 = test_data ::block_h1 ( ) ;
let b2 = test_data ::block_h2 ( ) ;
sync . on_new_blocks_headers ( 1 , vec! [ b1 . block_header . clone ( ) , b2 . block_header . clone ( ) ] ) ;
let tasks = executor . lock ( ) . take_tasks ( ) ;
assert_eq! ( tasks . len ( ) , 2 ) ;
assert! ( tasks . iter ( ) . any ( | t | t = = & Task ::RequestBlocksHeaders ( 1 ) ) ) ;
assert! ( tasks . iter ( ) . any ( | t | t = = & Task ::RequestBlocks ( 1 , vec! [ b1 . hash ( ) , b2 . hash ( ) ] ) ) ) ;
{
let chain = chain . read ( ) ;
assert_eq! ( chain . information ( ) . headers . best , 2 ) ;
assert_eq! ( chain . information ( ) . headers . total , 2 ) ;
}
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 1 , b1 . into ( ) ) ;
2016-11-08 05:16:05 -08:00
let tasks = executor . lock ( ) . take_tasks ( ) ;
assert_eq! ( tasks , vec! [ ] ) ;
{
let chain = chain . read ( ) ;
assert_eq! ( chain . information ( ) . headers . best , 1 ) ;
assert_eq! ( chain . information ( ) . headers . total , 1 ) ;
}
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 1 , b2 . into ( ) ) ;
2016-11-08 05:16:05 -08:00
let tasks = executor . lock ( ) . take_tasks ( ) ;
2016-11-16 05:46:21 -08:00
assert_eq! ( tasks , vec! [ Task ::RequestBlocksHeaders ( 1 ) , Task ::RequestMemoryPool ( 1 ) ] ) ;
2016-11-08 05:16:05 -08:00
{
let chain = chain . read ( ) ;
assert_eq! ( chain . information ( ) . headers . best , 0 ) ;
assert_eq! ( chain . information ( ) . headers . total , 0 ) ;
}
}
#[ test ]
fn synchronization_remembers_correct_block_headers_out_of_order ( ) {
2016-11-18 03:00:14 -08:00
let ( _ , _ , executor , chain , sync ) = create_sync ( None , None ) ;
2016-11-08 05:16:05 -08:00
let mut sync = sync . lock ( ) ;
let b1 = test_data ::block_h1 ( ) ;
let b2 = test_data ::block_h2 ( ) ;
sync . on_new_blocks_headers ( 1 , vec! [ b1 . block_header . clone ( ) , b2 . block_header . clone ( ) ] ) ;
let tasks = executor . lock ( ) . take_tasks ( ) ;
assert_eq! ( tasks . len ( ) , 2 ) ;
assert! ( tasks . iter ( ) . any ( | t | t = = & Task ::RequestBlocksHeaders ( 1 ) ) ) ;
assert! ( tasks . iter ( ) . any ( | t | t = = & Task ::RequestBlocks ( 1 , vec! [ b1 . hash ( ) , b2 . hash ( ) ] ) ) ) ;
{
let chain = chain . read ( ) ;
assert_eq! ( chain . information ( ) . headers . best , 2 ) ;
assert_eq! ( chain . information ( ) . headers . total , 2 ) ;
}
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 1 , b2 . into ( ) ) ;
2016-11-08 05:16:05 -08:00
let tasks = executor . lock ( ) . take_tasks ( ) ;
assert_eq! ( tasks , vec! [ ] ) ;
{
let chain = chain . read ( ) ;
assert_eq! ( chain . information ( ) . headers . best , 2 ) ;
assert_eq! ( chain . information ( ) . headers . total , 2 ) ;
}
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 1 , b1 . into ( ) ) ;
2016-11-08 05:16:05 -08:00
let tasks = executor . lock ( ) . take_tasks ( ) ;
2016-11-16 05:46:21 -08:00
assert_eq! ( tasks , vec! [ Task ::RequestBlocksHeaders ( 1 ) , Task ::RequestMemoryPool ( 1 ) ] ) ;
2016-11-08 05:16:05 -08:00
{
let chain = chain . read ( ) ;
assert_eq! ( chain . information ( ) . headers . best , 0 ) ;
assert_eq! ( chain . information ( ) . headers . total , 0 ) ;
}
}
#[ test ]
fn synchronization_ignores_unknown_block_headers ( ) {
2016-11-18 03:00:14 -08:00
let ( _ , _ , executor , chain , sync ) = create_sync ( None , None ) ;
2016-11-08 05:16:05 -08:00
let mut sync = sync . lock ( ) ;
let b169 = test_data ::block_h169 ( ) ;
sync . on_new_blocks_headers ( 1 , vec! [ b169 . block_header ] ) ;
let tasks = executor . lock ( ) . take_tasks ( ) ;
assert_eq! ( tasks , vec! [ ] ) ;
let chain = chain . read ( ) ;
assert_eq! ( chain . information ( ) . headers . best , 0 ) ;
assert_eq! ( chain . information ( ) . headers . total , 0 ) ;
}
#[ test ]
fn synchronization_works_for_forks_from_db_best_block ( ) {
2016-11-10 04:16:16 -08:00
let storage = create_disk_storage ( ) ;
let genesis = test_data ::genesis ( ) ;
storage . insert_block ( & genesis ) . expect ( " no db error " ) ;
2016-11-18 03:00:14 -08:00
let ( _ , _ , executor , chain , sync ) = create_sync ( Some ( storage ) , None ) ;
2016-11-10 04:16:16 -08:00
let genesis_header = & genesis . block_header ;
2016-11-08 05:16:05 -08:00
let fork1 = test_data ::build_n_empty_blocks_from ( 2 , 100 , & genesis_header ) ;
let fork2 = test_data ::build_n_empty_blocks_from ( 3 , 200 , & genesis_header ) ;
let mut sync = sync . lock ( ) ;
sync . on_new_blocks_headers ( 1 , vec! [ fork1 [ 0 ] . block_header . clone ( ) , fork1 [ 1 ] . block_header . clone ( ) ] ) ;
sync . on_new_blocks_headers ( 2 , vec! [ fork2 [ 0 ] . block_header . clone ( ) , fork2 [ 1 ] . block_header . clone ( ) , fork2 [ 2 ] . block_header . clone ( ) ] ) ;
let tasks = { executor . lock ( ) . take_tasks ( ) } ;
assert_eq! ( tasks , vec! [ Task ::RequestBlocksHeaders ( 1 ) ,
Task ::RequestBlocks ( 1 , vec! [ fork1 [ 0 ] . hash ( ) , fork1 [ 1 ] . hash ( ) ] ) ,
Task ::RequestBlocksHeaders ( 2 ) ,
Task ::RequestBlocks ( 2 , vec! [ fork2 [ 0 ] . hash ( ) , fork2 [ 1 ] . hash ( ) , fork2 [ 2 ] . hash ( ) ] ) ,
] ) ;
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 2 , fork2 [ 0 ] . clone ( ) . into ( ) ) ;
2016-11-08 05:16:05 -08:00
{
let chain = chain . read ( ) ;
assert_eq! ( chain . best_storage_block ( ) . hash , fork2 [ 0 ] . hash ( ) ) ;
assert_eq! ( chain . best_storage_block ( ) . number , 1 ) ;
}
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 1 , fork1 [ 0 ] . clone ( ) . into ( ) ) ;
2016-11-10 04:16:16 -08:00
{
let chain = chain . read ( ) ;
assert_eq! ( chain . best_storage_block ( ) . hash , fork2 [ 0 ] . hash ( ) ) ;
assert_eq! ( chain . best_storage_block ( ) . number , 1 ) ;
}
2016-11-08 05:16:05 -08:00
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 1 , fork1 [ 1 ] . clone ( ) . into ( ) ) ;
2016-11-10 04:16:16 -08:00
{
let chain = chain . read ( ) ;
assert_eq! ( chain . best_storage_block ( ) . hash , fork1 [ 1 ] . hash ( ) ) ;
assert_eq! ( chain . best_storage_block ( ) . number , 2 ) ;
}
2016-11-08 05:16:05 -08:00
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 2 , fork2 [ 1 ] . clone ( ) . into ( ) ) ;
2016-11-10 04:16:16 -08:00
{
let chain = chain . read ( ) ;
assert_eq! ( chain . best_storage_block ( ) . hash , fork1 [ 1 ] . hash ( ) ) ;
assert_eq! ( chain . best_storage_block ( ) . number , 2 ) ;
}
2016-11-08 05:16:05 -08:00
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 2 , fork2 [ 2 ] . clone ( ) . into ( ) ) ;
2016-11-08 05:16:05 -08:00
{
let chain = chain . read ( ) ;
assert_eq! ( chain . best_storage_block ( ) . hash , fork2 [ 2 ] . hash ( ) ) ;
assert_eq! ( chain . best_storage_block ( ) . number , 3 ) ;
}
2016-11-07 01:12:34 -08:00
}
2016-11-10 05:22:03 -08:00
#[ test ]
fn synchronization_works_for_forks_long_after_short ( ) {
let storage = create_disk_storage ( ) ;
let genesis = test_data ::genesis ( ) ;
storage . insert_block ( & genesis ) . expect ( " no db error " ) ;
2016-11-18 03:00:14 -08:00
let ( _ , _ , executor , chain , sync ) = create_sync ( Some ( storage ) , None ) ;
2016-11-10 05:22:03 -08:00
let common_block = test_data ::block_builder ( ) . header ( ) . parent ( genesis . hash ( ) ) . build ( ) . build ( ) ;
let fork1 = test_data ::build_n_empty_blocks_from ( 2 , 100 , & common_block . block_header ) ;
let fork2 = test_data ::build_n_empty_blocks_from ( 3 , 200 , & common_block . block_header ) ;
let mut sync = sync . lock ( ) ;
sync . on_new_blocks_headers ( 1 , vec! [ common_block . block_header . clone ( ) , fork1 [ 0 ] . block_header . clone ( ) , fork1 [ 1 ] . block_header . clone ( ) ] ) ;
sync . on_new_blocks_headers ( 2 , vec! [ common_block . block_header . clone ( ) , fork2 [ 0 ] . block_header . clone ( ) , fork2 [ 1 ] . block_header . clone ( ) , fork2 [ 2 ] . block_header . clone ( ) ] ) ;
let tasks = { executor . lock ( ) . take_tasks ( ) } ;
assert_eq! ( tasks , vec! [ Task ::RequestBlocksHeaders ( 1 ) ,
Task ::RequestBlocks ( 1 , vec! [ common_block . hash ( ) , fork1 [ 0 ] . hash ( ) , fork1 [ 1 ] . hash ( ) ] ) ,
Task ::RequestBlocksHeaders ( 2 ) ,
Task ::RequestBlocks ( 2 , vec! [ fork2 [ 0 ] . hash ( ) , fork2 [ 1 ] . hash ( ) , fork2 [ 2 ] . hash ( ) ] ) ,
] ) ;
// TODO: this will change from 3 to 4 after longest fork will be stored in the BestHeadersChain
// however id doesn't affect sync process, as it is shown below
{
let chain = chain . read ( ) ;
assert_eq! ( chain . information ( ) . headers . best , 3 ) ;
assert_eq! ( chain . information ( ) . headers . total , 3 ) ;
}
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 1 , common_block . clone ( ) . into ( ) ) ;
sync . on_peer_block ( 1 , fork1 [ 0 ] . clone ( ) . into ( ) ) ;
sync . on_peer_block ( 1 , fork1 [ 1 ] . clone ( ) . into ( ) ) ;
sync . on_peer_block ( 2 , fork2 [ 0 ] . clone ( ) . into ( ) ) ;
sync . on_peer_block ( 2 , fork2 [ 1 ] . clone ( ) . into ( ) ) ;
sync . on_peer_block ( 2 , fork2 [ 2 ] . clone ( ) . into ( ) ) ;
2016-11-10 05:22:03 -08:00
{
let chain = chain . read ( ) ;
assert_eq! ( chain . best_storage_block ( ) . hash , fork2 [ 2 ] . hash ( ) ) ;
assert_eq! ( chain . best_storage_block ( ) . number , 4 ) ;
}
}
2016-11-11 00:38:41 -08:00
#[ test ]
fn accept_out_of_order_blocks_when_saturated ( ) {
2016-11-18 03:00:14 -08:00
let ( _ , _ , _ , chain , sync ) = create_sync ( None , None ) ;
2016-11-11 00:38:41 -08:00
let mut sync = sync . lock ( ) ;
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 1 , test_data ::block_h2 ( ) . into ( ) ) ;
2016-11-16 05:46:21 -08:00
assert_eq! ( sync . information ( ) . orphaned_blocks , 1 ) ;
2016-11-11 00:38:41 -08:00
{
let chain = chain . read ( ) ;
assert_eq! ( chain . best_storage_block ( ) . number , 0 ) ;
}
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 1 , test_data ::block_h1 ( ) . into ( ) ) ;
2016-11-16 05:46:21 -08:00
assert_eq! ( sync . information ( ) . orphaned_blocks , 0 ) ;
2016-11-11 00:38:41 -08:00
{
let chain = chain . read ( ) ;
assert_eq! ( chain . best_storage_block ( ) . number , 2 ) ;
}
}
#[ test ]
fn do_not_rerequest_unknown_block_in_inventory ( ) {
2016-11-18 03:00:14 -08:00
let ( _ , _ , executor , _ , sync ) = create_sync ( None , None ) ;
2016-11-11 00:38:41 -08:00
let mut sync = sync . lock ( ) ;
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 1 , test_data ::block_h2 ( ) . into ( ) ) ;
2016-11-11 00:38:41 -08:00
sync . on_new_blocks_inventory ( 1 , vec! [ test_data ::block_h1 ( ) . hash ( ) , test_data ::block_h2 ( ) . hash ( ) ] ) ;
let tasks = executor . lock ( ) . take_tasks ( ) ;
assert_eq! ( tasks , vec! [ Task ::RequestBlocks ( 1 , vec! [ test_data ::block_h1 ( ) . hash ( ) ] ) ] ) ;
}
2016-11-14 11:24:45 -08:00
#[ test ]
fn blocks_rerequested_on_peer_disconnect ( ) {
2016-11-18 03:00:14 -08:00
let ( _ , _ , executor , _ , sync ) = create_sync ( None , None ) ;
2016-11-14 11:24:45 -08:00
let block1 : Block = test_data ::block_h1 ( ) ;
let block2 : Block = test_data ::block_h2 ( ) ;
{
let mut sync = sync . lock ( ) ;
// receive inventory from new peer#1
sync . on_new_blocks_headers ( 1 , vec! [ block1 . block_header . clone ( ) ] ) ;
// synchronization has started && new blocks have been requested
let tasks = executor . lock ( ) . take_tasks ( ) ;
assert_eq! ( tasks , vec! [ Task ::RequestBlocksHeaders ( 1 ) , Task ::RequestBlocks ( 1 , vec! [ block1 . hash ( ) ] ) ] ) ;
}
{
let mut sync = sync . lock ( ) ;
// receive inventory from new peer#2
sync . on_new_blocks_headers ( 2 , vec! [ block1 . block_header . clone ( ) , block2 . block_header . clone ( ) ] ) ;
// synchronization has started && new blocks have been requested
let tasks = executor . lock ( ) . take_tasks ( ) ;
assert_eq! ( tasks , vec! [ Task ::RequestBlocksHeaders ( 2 ) , Task ::RequestBlocks ( 2 , vec! [ block2 . hash ( ) ] ) ] ) ;
}
{
let mut sync = sync . lock ( ) ;
// peer#1 is disconnected && it has pending blocks requests => ask peer#2
sync . on_peer_disconnected ( 1 ) ;
// blocks have been requested
let tasks = executor . lock ( ) . take_tasks ( ) ;
assert_eq! ( tasks , vec! [ Task ::RequestBlocks ( 2 , vec! [ block1 . hash ( ) ] ) ] ) ;
}
}
2016-11-15 07:03:14 -08:00
#[ test ]
fn sync_after_db_insert_nonfatal_fail ( ) {
2016-11-29 08:39:00 -08:00
use db ::Store ;
let mut storage = db ::TestStorage ::with_genesis_block ( ) ;
let block = test_data ::block_h1 ( ) ;
storage . insert_error ( block . hash ( ) , db ::Error ::Consistency ( db ::ConsistencyError ::NoBestBlock ) ) ;
let best_genesis = storage . best_block ( ) . unwrap ( ) ;
let ( _ , _ , _ , chain , sync ) = create_sync ( Some ( Arc ::new ( storage ) ) , None ) ;
let mut sync = sync . lock ( ) ;
sync . on_peer_block ( 1 , block . into ( ) ) ;
assert_eq! ( chain . read ( ) . best_block ( ) , best_genesis ) ;
2016-11-15 07:03:14 -08:00
}
2016-11-15 23:59:54 -08:00
#[ test ]
fn peer_removed_from_sync_after_responding_with_requested_block_notfound ( ) {
2016-11-18 03:00:14 -08:00
let ( _ , _ , executor , _ , sync ) = create_sync ( None , None ) ;
2016-11-15 23:59:54 -08:00
let mut sync = sync . lock ( ) ;
let b1 = test_data ::block_h1 ( ) ;
let b2 = test_data ::block_h2 ( ) ;
sync . on_new_blocks_headers ( 1 , vec! [ b1 . block_header . clone ( ) , b2 . block_header . clone ( ) ] ) ;
let tasks = executor . lock ( ) . take_tasks ( ) ;
assert_eq! ( tasks , vec! [ Task ::RequestBlocksHeaders ( 1 ) , Task ::RequestBlocks ( 1 , vec! [ b1 . hash ( ) , b2 . hash ( ) ] ) ] ) ;
assert_eq! ( sync . information ( ) . peers . idle , 0 ) ;
assert_eq! ( sync . information ( ) . peers . unuseful , 0 ) ;
assert_eq! ( sync . information ( ) . peers . active , 1 ) ;
sync . on_peer_blocks_notfound ( 1 , vec! [ b1 . hash ( ) ] ) ;
let tasks = executor . lock ( ) . take_tasks ( ) ;
2016-11-16 05:46:21 -08:00
assert_eq! ( tasks , vec! [ Task ::RequestBlocksHeaders ( 1 ) , Task ::RequestMemoryPool ( 1 ) ] ) ;
2016-11-15 23:59:54 -08:00
assert_eq! ( sync . information ( ) . peers . idle , 0 ) ;
assert_eq! ( sync . information ( ) . peers . unuseful , 1 ) ;
assert_eq! ( sync . information ( ) . peers . active , 0 ) ;
}
#[ test ]
fn peer_not_removed_from_sync_after_responding_with_requested_block_notfound ( ) {
2016-11-18 03:00:14 -08:00
let ( _ , _ , executor , _ , sync ) = create_sync ( None , None ) ;
2016-11-15 23:59:54 -08:00
let mut sync = sync . lock ( ) ;
let b1 = test_data ::block_h1 ( ) ;
let b2 = test_data ::block_h2 ( ) ;
sync . on_new_blocks_headers ( 1 , vec! [ b1 . block_header . clone ( ) , b2 . block_header . clone ( ) ] ) ;
let tasks = executor . lock ( ) . take_tasks ( ) ;
assert_eq! ( tasks , vec! [ Task ::RequestBlocksHeaders ( 1 ) , Task ::RequestBlocks ( 1 , vec! [ b1 . hash ( ) , b2 . hash ( ) ] ) ] ) ;
assert_eq! ( sync . information ( ) . peers . idle , 0 ) ;
assert_eq! ( sync . information ( ) . peers . unuseful , 0 ) ;
assert_eq! ( sync . information ( ) . peers . active , 1 ) ;
sync . on_peer_blocks_notfound ( 1 , vec! [ test_data ::block_h170 ( ) . hash ( ) ] ) ;
let tasks = executor . lock ( ) . take_tasks ( ) ;
assert_eq! ( tasks , vec! [ ] ) ;
assert_eq! ( sync . information ( ) . peers . idle , 0 ) ;
assert_eq! ( sync . information ( ) . peers . unuseful , 0 ) ;
assert_eq! ( sync . information ( ) . peers . active , 1 ) ;
}
2016-11-16 05:46:21 -08:00
#[ test ]
fn transaction_is_not_requested_when_synchronizing ( ) {
2016-11-18 03:00:14 -08:00
let ( _ , _ , executor , _ , sync ) = create_sync ( None , None ) ;
2016-11-16 05:46:21 -08:00
let mut sync = sync . lock ( ) ;
let b1 = test_data ::block_h1 ( ) ;
let b2 = test_data ::block_h2 ( ) ;
sync . on_new_blocks_headers ( 1 , vec! [ b1 . block_header . clone ( ) , b2 . block_header . clone ( ) ] ) ;
assert! ( sync . information ( ) . state . is_synchronizing ( ) ) ;
{ executor . lock ( ) . take_tasks ( ) ; } // forget tasks
sync . on_new_transactions_inventory ( 0 , vec! [ H256 ::from ( 0 ) ] ) ;
let tasks = executor . lock ( ) . take_tasks ( ) ;
assert_eq! ( tasks , vec! [ ] ) ;
}
#[ test ]
fn transaction_is_requested_when_not_synchronizing ( ) {
2016-11-18 03:00:14 -08:00
let ( _ , _ , executor , _ , sync ) = create_sync ( None , None ) ;
2016-11-16 05:46:21 -08:00
let mut sync = sync . lock ( ) ;
sync . on_new_transactions_inventory ( 0 , vec! [ H256 ::from ( 0 ) ] ) ;
{
let tasks = executor . lock ( ) . take_tasks ( ) ;
assert_eq! ( tasks , vec! [ Task ::RequestTransactions ( 0 , vec! [ H256 ::from ( 0 ) ] ) ] ) ;
}
let b1 = test_data ::block_h1 ( ) ;
sync . on_new_blocks_headers ( 1 , vec! [ b1 . block_header . clone ( ) ] ) ;
assert! ( sync . information ( ) . state . is_nearly_saturated ( ) ) ;
{ executor . lock ( ) . take_tasks ( ) ; } // forget tasks
sync . on_new_transactions_inventory ( 0 , vec! [ H256 ::from ( 1 ) ] ) ;
let tasks = executor . lock ( ) . take_tasks ( ) ;
assert_eq! ( tasks , vec! [ Task ::RequestTransactions ( 0 , vec! [ H256 ::from ( 1 ) ] ) ] ) ;
}
#[ test ]
fn same_transaction_can_be_requested_twice ( ) {
2016-11-18 03:00:14 -08:00
let ( _ , _ , executor , _ , sync ) = create_sync ( None , None ) ;
2016-11-16 05:46:21 -08:00
let mut sync = sync . lock ( ) ;
sync . on_new_transactions_inventory ( 0 , vec! [ H256 ::from ( 0 ) ] ) ;
{
let tasks = executor . lock ( ) . take_tasks ( ) ;
assert_eq! ( tasks , vec! [ Task ::RequestTransactions ( 0 , vec! [ H256 ::from ( 0 ) ] ) ] ) ;
}
sync . on_new_transactions_inventory ( 0 , vec! [ H256 ::from ( 0 ) ] ) ;
{
let tasks = executor . lock ( ) . take_tasks ( ) ;
assert_eq! ( tasks , vec! [ Task ::RequestTransactions ( 0 , vec! [ H256 ::from ( 0 ) ] ) ] ) ;
}
}
#[ test ]
fn known_transaction_is_not_requested ( ) {
2016-11-18 03:00:14 -08:00
let ( _ , _ , executor , _ , sync ) = create_sync ( None , None ) ;
2016-11-16 05:46:21 -08:00
let mut sync = sync . lock ( ) ;
sync . on_new_transactions_inventory ( 0 , vec! [ test_data ::genesis ( ) . transactions [ 0 ] . hash ( ) , H256 ::from ( 0 ) ] ) ;
assert_eq! ( executor . lock ( ) . take_tasks ( ) , vec! [ Task ::RequestTransactions ( 0 , vec! [ H256 ::from ( 0 ) ] ) ] ) ;
}
#[ test ]
fn transaction_is_not_accepted_when_synchronizing ( ) {
2016-11-18 03:00:14 -08:00
let ( _ , _ , _ , _ , sync ) = create_sync ( None , None ) ;
2016-11-16 05:46:21 -08:00
let mut sync = sync . lock ( ) ;
let b1 = test_data ::block_h1 ( ) ;
let b2 = test_data ::block_h2 ( ) ;
sync . on_new_blocks_headers ( 1 , vec! [ b1 . block_header . clone ( ) , b2 . block_header . clone ( ) ] ) ;
assert! ( sync . information ( ) . state . is_synchronizing ( ) ) ;
2016-11-18 00:52:45 -08:00
sync . on_peer_transaction ( 1 , Transaction ::default ( ) ) ;
2016-11-16 05:46:21 -08:00
assert_eq! ( sync . information ( ) . chain . transactions . transactions_count , 0 ) ;
}
#[ test ]
fn transaction_is_accepted_when_not_synchronizing ( ) {
2016-11-18 03:00:14 -08:00
let ( _ , _ , _ , _ , sync ) = create_sync ( None , None ) ;
2016-11-16 05:46:21 -08:00
let mut sync = sync . lock ( ) ;
2016-11-18 00:52:45 -08:00
sync . on_peer_transaction ( 1 , test_data ::TransactionBuilder ::with_version ( 1 ) . into ( ) ) ;
2016-11-16 05:46:21 -08:00
assert_eq! ( sync . information ( ) . chain . transactions . transactions_count , 1 ) ;
let b1 = test_data ::block_h1 ( ) ;
sync . on_new_blocks_headers ( 1 , vec! [ b1 . block_header . clone ( ) ] ) ;
assert! ( sync . information ( ) . state . is_nearly_saturated ( ) ) ;
2016-11-18 00:52:45 -08:00
sync . on_peer_transaction ( 1 , test_data ::TransactionBuilder ::with_version ( 2 ) . into ( ) ) ;
2016-11-16 05:46:21 -08:00
assert_eq! ( sync . information ( ) . chain . transactions . transactions_count , 2 ) ;
}
#[ test ]
fn transaction_is_orphaned_when_input_is_unknown ( ) {
2016-11-18 03:00:14 -08:00
let ( _ , _ , _ , _ , sync ) = create_sync ( None , None ) ;
2016-11-16 05:46:21 -08:00
let mut sync = sync . lock ( ) ;
2016-11-18 00:52:45 -08:00
sync . on_peer_transaction ( 1 , test_data ::TransactionBuilder ::with_default_input ( 0 ) . into ( ) ) ;
2016-11-16 05:46:21 -08:00
assert_eq! ( sync . information ( ) . chain . transactions . transactions_count , 0 ) ;
assert_eq! ( sync . information ( ) . orphaned_transactions , 1 ) ;
}
#[ test ]
fn orphaned_transaction_is_verified_when_input_is_received ( ) {
let chain = & mut test_data ::ChainBuilder ::new ( ) ;
test_data ::TransactionBuilder ::with_output ( 10 ) . store ( chain ) // t0
. set_input ( & chain . at ( 0 ) , 0 ) . set_output ( 20 ) . store ( chain ) ; // t0 -> t1
2016-11-18 03:00:14 -08:00
let ( _ , _ , _ , _ , sync ) = create_sync ( None , None ) ;
2016-11-16 05:46:21 -08:00
let mut sync = sync . lock ( ) ;
2016-11-18 00:52:45 -08:00
sync . on_peer_transaction ( 1 , chain . at ( 1 ) ) ;
2016-11-16 05:46:21 -08:00
assert_eq! ( sync . information ( ) . chain . transactions . transactions_count , 0 ) ;
assert_eq! ( sync . information ( ) . orphaned_transactions , 1 ) ;
2016-11-18 00:52:45 -08:00
sync . on_peer_transaction ( 1 , chain . at ( 0 ) ) ;
2016-11-16 05:46:21 -08:00
assert_eq! ( sync . information ( ) . chain . transactions . transactions_count , 2 ) ;
assert_eq! ( sync . information ( ) . orphaned_transactions , 0 ) ;
}
2016-11-18 03:00:14 -08:00
#[ test ]
// https://github.com/ethcore/parity-bitcoin/issues/121
fn when_previous_block_verification_failed_fork_is_not_requested ( ) {
// got headers [b10, b11, b12] - some fork
// got headers [b10, b21, b22] - main branch
// got b10, b11, b12, b21. b22 is requested
//
// verifying: [b10, b11, b12, b21]
// headers_chain: [b10, b11, b12]
//
// b21 verification failed => b22 is not removed (since it is not in headers_chain)
// got new headers [b10, b21, b22, b23] => intersection point is b10 => scheduling [b21, b22, b23]
//
// block queue is empty => new tasks => requesting [b21, b22] => panic in hash_queue
//
2016-11-18 03:53:59 -08:00
// => do not trust first intersection point - check each hash when scheduling hashes.
2016-11-18 03:00:14 -08:00
// If at least one hash is known => previous verification failed => drop all headers.
let genesis = test_data ::genesis ( ) ;
let b10 = test_data ::block_builder ( ) . header ( ) . parent ( genesis . hash ( ) ) . build ( ) . build ( ) ;
let b11 = test_data ::block_builder ( ) . header ( ) . nonce ( 1 ) . parent ( b10 . hash ( ) ) . build ( ) . build ( ) ;
let b12 = test_data ::block_builder ( ) . header ( ) . parent ( b11 . hash ( ) ) . build ( ) . build ( ) ;
let b21 = test_data ::block_builder ( ) . header ( ) . nonce ( 2 ) . parent ( b10 . hash ( ) ) . build ( ) . build ( ) ;
let b22 = test_data ::block_builder ( ) . header ( ) . parent ( b21 . hash ( ) ) . build ( ) . build ( ) ;
let b23 = test_data ::block_builder ( ) . header ( ) . parent ( b22 . hash ( ) ) . build ( ) . build ( ) ;
2016-11-18 03:53:59 -08:00
// simulate verification during b21 verification
2016-11-18 06:10:57 -08:00
let mut dummy_verifier = DummyVerifier ::default ( ) ;
2016-11-18 03:00:14 -08:00
dummy_verifier . error_when_verifying ( b21 . hash ( ) , " simulated " ) ;
let ( _ , _ , _ , _ , sync ) = create_sync ( None , Some ( dummy_verifier ) ) ;
let mut sync = sync . lock ( ) ;
sync . on_new_blocks_headers ( 1 , vec! [ b10 . block_header . clone ( ) , b11 . block_header . clone ( ) , b12 . block_header . clone ( ) ] ) ;
sync . on_new_blocks_headers ( 2 , vec! [ b10 . block_header . clone ( ) , b21 . block_header . clone ( ) , b22 . block_header . clone ( ) ] ) ;
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 1 , b10 . clone ( ) . into ( ) ) ;
sync . on_peer_block ( 1 , b11 . into ( ) ) ;
sync . on_peer_block ( 1 , b12 . into ( ) ) ;
2016-11-18 03:00:14 -08:00
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 2 , b21 . clone ( ) . into ( ) ) ;
2016-11-18 03:00:14 -08:00
// should not panic here
sync . on_new_blocks_headers ( 2 , vec! [ b10 . block_header . clone ( ) , b21 . block_header . clone ( ) ,
b22 . block_header . clone ( ) , b23 . block_header . clone ( ) ] ) ;
}
2016-11-21 07:42:18 -08:00
2016-11-21 09:06:14 -08:00
#[ test ]
fn relay_new_block_when_in_saturated_state ( ) {
let ( _ , _ , executor , _ , sync ) = create_sync ( None , None ) ;
let genesis = test_data ::genesis ( ) ;
let b0 = test_data ::block_builder ( ) . header ( ) . parent ( genesis . hash ( ) ) . build ( ) . build ( ) ;
let b1 = test_data ::block_builder ( ) . header ( ) . parent ( b0 . hash ( ) ) . build ( ) . build ( ) ;
let b2 = test_data ::block_builder ( ) . header ( ) . parent ( b1 . hash ( ) ) . build ( ) . build ( ) ;
let b3 = test_data ::block_builder ( ) . header ( ) . parent ( b2 . hash ( ) ) . build ( ) . build ( ) ;
let mut sync = sync . lock ( ) ;
sync . on_new_blocks_headers ( 1 , vec! [ b0 . block_header . clone ( ) , b1 . block_header . clone ( ) ] ) ;
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 1 , b0 . clone ( ) . into ( ) ) ;
sync . on_peer_block ( 1 , b1 . clone ( ) . into ( ) ) ;
2016-11-21 09:06:14 -08:00
// we were in synchronization state => block is not relayed
{
let tasks = executor . lock ( ) . take_tasks ( ) ;
assert_eq! ( tasks , vec! [ Task ::RequestBlocksHeaders ( 1 ) ,
Task ::RequestBlocks ( 1 , vec! [ b0 . hash ( ) , b1 . hash ( ) ] ) ,
Task ::RequestBlocksHeaders ( 1 ) ,
Task ::RequestMemoryPool ( 1 )
] ) ;
}
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 2 , b2 . clone ( ) . into ( ) ) ;
2016-11-21 09:06:14 -08:00
// we were in saturated state => block is relayed
{
let tasks = executor . lock ( ) . take_tasks ( ) ;
2016-11-21 22:14:31 -08:00
let inventory = vec! [ InventoryVector { inv_type : InventoryType ::MessageBlock , hash : b2 . hash ( ) } ] ;
2016-11-29 08:39:00 -08:00
assert_eq! ( tasks , vec! [ Task ::RequestBlocksHeaders ( 2 ) , Task ::SendInventory ( 1 , inventory ) ] ) ;
2016-11-21 09:06:14 -08:00
}
sync . on_new_blocks_headers ( 1 , vec! [ b3 . block_header . clone ( ) ] ) ;
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 1 , b3 . clone ( ) . into ( ) ) ;
2016-11-21 09:06:14 -08:00
// we were in nearly saturated state => block is relayed
{
let tasks = executor . lock ( ) . take_tasks ( ) ;
2016-11-21 22:14:31 -08:00
let inventory = vec! [ InventoryVector { inv_type : InventoryType ::MessageBlock , hash : b3 . hash ( ) } ] ;
2016-11-29 08:39:00 -08:00
assert! ( tasks . iter ( ) . any ( | t | t = = & Task ::SendInventory ( 2 , inventory . clone ( ) ) ) ) ;
2016-11-21 09:06:14 -08:00
}
}
2016-11-22 05:02:28 -08:00
2016-11-21 07:42:18 -08:00
#[ test ]
fn relay_new_transaction_when_in_saturated_state ( ) {
let ( _ , _ , executor , _ , sync ) = create_sync ( None , None ) ;
2016-11-29 08:39:00 -08:00
let tx : Transaction = test_data ::TransactionBuilder ::with_output ( 20 ) . into ( ) ;
let tx_hash = tx . hash ( ) ;
2016-11-21 07:42:18 -08:00
let mut sync = sync . lock ( ) ;
2016-11-29 08:39:00 -08:00
sync . on_peer_connected ( 1 ) ;
sync . on_peer_transaction ( 2 , tx ) ;
2016-11-21 07:42:18 -08:00
let tasks = { executor . lock ( ) . take_tasks ( ) } ;
2016-11-29 08:39:00 -08:00
let inventory = vec! [ InventoryVector { inv_type : InventoryType ::MessageTx , hash : tx_hash } ] ;
assert_eq! ( tasks , vec! [ Task ::SendInventory ( 1 , inventory ) ] ) ;
2016-11-21 07:42:18 -08:00
}
2016-11-23 02:48:36 -08:00
#[ test ]
fn relay_new_transaction_with_bloom_filter ( ) {
let ( _ , _ , executor , _ , sync ) = create_sync ( None , None ) ;
let tx1 : Transaction = test_data ::TransactionBuilder ::with_output ( 10 ) . into ( ) ;
let tx2 : Transaction = test_data ::TransactionBuilder ::with_output ( 20 ) . into ( ) ;
let tx3 : Transaction = test_data ::TransactionBuilder ::with_output ( 30 ) . into ( ) ;
let tx1_hash = tx1 . hash ( ) ;
let tx2_hash = tx2 . hash ( ) ;
let tx3_hash = tx3 . hash ( ) ;
let mut sync = sync . lock ( ) ;
// peer#1 wants tx1
sync . on_peer_connected ( 1 ) ;
sync . on_peer_filterload ( 1 , & default_filterload ( ) ) ;
sync . on_peer_filteradd ( 1 , & make_filteradd ( & * tx1_hash ) ) ;
// peer#2 wants tx2
sync . on_peer_connected ( 2 ) ;
sync . on_peer_filterload ( 2 , & default_filterload ( ) ) ;
sync . on_peer_filteradd ( 2 , & make_filteradd ( & * tx2_hash ) ) ;
// peer#3 wants tx1 + tx2 transactions
sync . on_peer_connected ( 3 ) ;
sync . on_peer_filterload ( 3 , & default_filterload ( ) ) ;
sync . on_peer_filteradd ( 3 , & make_filteradd ( & * tx1_hash ) ) ;
sync . on_peer_filteradd ( 3 , & make_filteradd ( & * tx2_hash ) ) ;
// peer#4 has default behaviour (no filter)
sync . on_peer_connected ( 4 ) ;
// peer#5 wants some other transactions
sync . on_peer_connected ( 5 ) ;
sync . on_peer_filterload ( 5 , & default_filterload ( ) ) ;
sync . on_peer_filteradd ( 5 , & make_filteradd ( & * tx3_hash ) ) ;
// tx1 is relayed to peers: 1, 3, 4
sync . on_peer_transaction ( 6 , tx1 ) ;
let tasks = { executor . lock ( ) . take_tasks ( ) } ;
let inventory = vec! [ InventoryVector { inv_type : InventoryType ::MessageTx , hash : tx1_hash } ] ;
assert_eq! ( tasks , vec! [
2016-11-29 08:39:00 -08:00
Task ::SendInventory ( 1 , inventory . clone ( ) ) ,
Task ::SendInventory ( 3 , inventory . clone ( ) ) ,
Task ::SendInventory ( 4 , inventory . clone ( ) ) ,
2016-11-23 02:48:36 -08:00
] ) ;
// tx2 is relayed to peers: 2, 3, 4
sync . on_peer_transaction ( 6 , tx2 ) ;
let tasks = { executor . lock ( ) . take_tasks ( ) } ;
let inventory = vec! [ InventoryVector { inv_type : InventoryType ::MessageTx , hash : tx2_hash } ] ;
assert_eq! ( tasks , vec! [
2016-11-29 08:39:00 -08:00
Task ::SendInventory ( 2 , inventory . clone ( ) ) ,
Task ::SendInventory ( 3 , inventory . clone ( ) ) ,
Task ::SendInventory ( 4 , inventory . clone ( ) ) ,
2016-11-23 02:48:36 -08:00
] ) ;
}
2016-11-24 04:18:18 -08:00
#[ test ]
fn relay_new_block_after_sendheaders ( ) {
let ( _ , _ , executor , _ , sync ) = create_sync ( None , None ) ;
let genesis = test_data ::genesis ( ) ;
let b0 = test_data ::block_builder ( ) . header ( ) . parent ( genesis . hash ( ) ) . build ( ) . build ( ) ;
let mut sync = sync . lock ( ) ;
sync . on_peer_connected ( 1 ) ;
sync . on_peer_connected ( 2 ) ;
2016-11-25 04:09:08 -08:00
sync . on_peer_block_announcement_type ( 2 , BlockAnnouncementType ::SendHeader ) ;
2016-11-24 04:18:18 -08:00
sync . on_peer_connected ( 3 ) ;
// igonore tasks
{ executor . lock ( ) . take_tasks ( ) ; }
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 1 , b0 . clone ( ) . into ( ) ) ;
2016-11-24 04:18:18 -08:00
let tasks = executor . lock ( ) . take_tasks ( ) ;
let inventory = vec! [ InventoryVector { inv_type : InventoryType ::MessageBlock , hash : b0 . hash ( ) } ] ;
let headers = vec! [ b0 . block_header . clone ( ) ] ;
assert_eq! ( tasks , vec! [ Task ::RequestBlocksHeaders ( 1 ) ,
Task ::SendHeaders ( 2 , headers , ServerTaskIndex ::None ) ,
2016-11-29 08:39:00 -08:00
Task ::SendInventory ( 3 , inventory ) ,
2016-11-24 04:18:18 -08:00
] ) ;
}
2016-11-25 07:35:35 -08:00
#[ test ]
2016-11-25 00:09:18 -08:00
fn relay_new_transaction_with_feefilter ( ) {
let ( _ , _ , executor , chain , sync ) = create_sync ( None , None ) ;
let b1 = test_data ::block_builder ( ) . header ( ) . parent ( test_data ::genesis ( ) . hash ( ) ) . build ( )
. transaction ( ) . output ( ) . value ( 1_000_000 ) . build ( ) . build ( )
. build ( ) ; // genesis -> b1
let tx0 = b1 . transactions [ 0 ] . clone ( ) ;
let tx1 : Transaction = test_data ::TransactionBuilder ::with_output ( 800_000 ) . add_input ( & tx0 , 0 ) . into ( ) ;
let tx1_hash = tx1 . hash ( ) ;
let mut sync = sync . lock ( ) ;
sync . on_peer_connected ( 1 ) ;
sync . on_peer_connected ( 2 ) ;
sync . on_peer_connected ( 3 ) ;
sync . on_peer_connected ( 4 ) ;
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 1 , b1 . into ( ) ) ;
2016-11-25 00:09:18 -08:00
{
use miner ::transaction_fee_rate ;
let chain = chain . read ( ) ;
assert_eq! ( transaction_fee_rate ( & * chain , & tx1 ) , 3333 ) ; // 200_000 / 60
}
sync . on_peer_feefilter ( 2 , & types ::FeeFilter { fee_rate : 3000 , } ) ;
sync . on_peer_feefilter ( 3 , & types ::FeeFilter { fee_rate : 4000 , } ) ;
// forget previous tasks
{ executor . lock ( ) . take_tasks ( ) ; }
sync . on_peer_transaction ( 1 , tx1 ) ;
let tasks = executor . lock ( ) . take_tasks ( ) ;
assert_eq! ( tasks , vec! [
Task ::SendInventory ( 2 , vec! [
InventoryVector {
inv_type : InventoryType ::MessageTx ,
hash : tx1_hash . clone ( ) ,
}
2016-11-29 08:39:00 -08:00
] ) ,
2016-11-25 00:09:18 -08:00
Task ::SendInventory ( 4 , vec! [
InventoryVector {
inv_type : InventoryType ::MessageTx ,
hash : tx1_hash . clone ( ) ,
}
2016-11-29 08:39:00 -08:00
] ) ,
2016-11-25 00:09:18 -08:00
] ) ;
}
2016-11-25 07:57:56 -08:00
#[ test ]
fn receive_same_unknown_block_twice ( ) {
let ( _ , _ , _ , _ , sync ) = create_sync ( None , None ) ;
let mut sync = sync . lock ( ) ;
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 1 , test_data ::block_h2 ( ) . into ( ) ) ;
2016-11-25 07:57:56 -08:00
// should not panic here
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 2 , test_data ::block_h2 ( ) . into ( ) ) ;
2016-11-25 07:57:56 -08:00
}
2016-11-27 06:53:34 -08:00
2016-11-27 07:02:21 -08:00
#[ test ]
2016-11-25 07:35:35 -08:00
fn relay_new_block_after_sendcmpct ( ) {
2016-11-27 08:09:47 -08:00
let ( _ , _ , executor , _ , sync ) = create_sync ( None , None ) ;
let genesis = test_data ::genesis ( ) ;
let b0 = test_data ::block_builder ( ) . header ( ) . parent ( genesis . hash ( ) ) . build ( ) . build ( ) ;
let mut sync = sync . lock ( ) ;
sync . on_peer_connected ( 1 ) ;
sync . on_peer_connected ( 2 ) ;
sync . on_peer_block_announcement_type ( 2 , BlockAnnouncementType ::SendCompactBlock ) ;
sync . on_peer_connected ( 3 ) ;
// igonore tasks
{ executor . lock ( ) . take_tasks ( ) ; }
2016-11-28 05:07:33 -08:00
sync . on_peer_block ( 1 , b0 . clone ( ) . into ( ) ) ;
2016-11-27 08:09:47 -08:00
let tasks = executor . lock ( ) . take_tasks ( ) ;
let inventory = vec! [ InventoryVector { inv_type : InventoryType ::MessageBlock , hash : b0 . hash ( ) } ] ;
assert_eq! ( tasks . len ( ) , 3 ) ;
assert_eq! ( tasks [ 0 ] , Task ::RequestBlocksHeaders ( 1 ) ) ;
match tasks [ 1 ] {
2016-11-29 08:39:00 -08:00
Task ::SendCompactBlocks ( 2 , _ ) = > ( ) ,
2016-11-27 08:09:47 -08:00
_ = > panic! ( " unexpected task " ) ,
}
2016-11-29 08:39:00 -08:00
assert_eq! ( tasks [ 2 ] , Task ::SendInventory ( 3 , inventory ) ) ;
2016-11-25 07:35:35 -08:00
}
2016-12-01 04:08:13 -08:00
#[ test ]
fn test_message_block_headers_provider ( ) {
let storage = Arc ::new ( db ::TestStorage ::with_genesis_block ( ) ) ;
let chain = ChainRef ::new ( RwLock ::new ( Chain ::new ( storage . clone ( ) ) ) ) ;
let chain = chain . read ( ) ;
let mut headers_provider = MessageBlockHeadersProvider ::new ( & * chain ) ;
assert_eq! ( headers_provider . block_header ( db ::BlockRef ::Hash ( test_data ::genesis ( ) . hash ( ) ) ) , Some ( test_data ::genesis ( ) . block_header ) ) ;
assert_eq! ( headers_provider . block_header ( db ::BlockRef ::Number ( 0 ) ) , Some ( test_data ::genesis ( ) . block_header ) ) ;
assert_eq! ( headers_provider . block_header ( db ::BlockRef ::Hash ( H256 ::from ( 1 ) ) ) , None ) ;
assert_eq! ( headers_provider . block_header ( db ::BlockRef ::Number ( 1 ) ) , None ) ;
headers_provider . append_header ( test_data ::block_h1 ( ) . hash ( ) , test_data ::block_h1 ( ) . block_header ) ;
assert_eq! ( headers_provider . block_header ( db ::BlockRef ::Hash ( test_data ::genesis ( ) . hash ( ) ) ) , Some ( test_data ::genesis ( ) . block_header ) ) ;
assert_eq! ( headers_provider . block_header ( db ::BlockRef ::Number ( 0 ) ) , Some ( test_data ::genesis ( ) . block_header ) ) ;
assert_eq! ( headers_provider . block_header ( db ::BlockRef ::Hash ( test_data ::block_h1 ( ) . hash ( ) ) ) , Some ( test_data ::block_h1 ( ) . block_header ) ) ;
assert_eq! ( headers_provider . block_header ( db ::BlockRef ::Number ( 1 ) ) , Some ( test_data ::block_h1 ( ) . block_header ) ) ;
assert_eq! ( headers_provider . block_header ( db ::BlockRef ::Hash ( H256 ::from ( 1 ) ) ) , None ) ;
assert_eq! ( headers_provider . block_header ( db ::BlockRef ::Number ( 2 ) ) , None ) ;
}
2016-12-02 07:12:20 -08:00
#[ test ]
fn collection_closed_on_block_verification_error ( ) {
let genesis = test_data ::genesis ( ) ;
let b0 = test_data ::block_builder ( ) . header ( ) . parent ( genesis . hash ( ) ) . build ( ) . build ( ) ;
// simulate verification error during b0 verification
let mut dummy_verifier = DummyVerifier ::default ( ) ;
dummy_verifier . error_when_verifying ( b0 . hash ( ) , " simulated " ) ;
let ( _ , _ , executor , _ , sync ) = create_sync ( None , Some ( dummy_verifier ) ) ;
sync . lock ( ) . on_peer_block ( 0 , b0 . into ( ) ) ;
let tasks = executor . lock ( ) . take_tasks ( ) ;
assert_eq! ( tasks , vec! [ Task ::Close ( 0 ) ] ) ;
}
#[ test ]
fn collection_closed_on_begin_dead_end_block_header ( ) {
let genesis = test_data ::genesis ( ) ;
let b0 = test_data ::block_builder ( ) . header ( ) . parent ( genesis . hash ( ) ) . build ( ) . build ( ) ;
let b1 = test_data ::block_builder ( ) . header ( ) . parent ( b0 . hash ( ) ) . build ( ) . build ( ) ;
let b2 = test_data ::block_builder ( ) . header ( ) . parent ( b1 . hash ( ) ) . build ( ) . build ( ) ;
let ( _ , _ , executor , chain , sync ) = create_sync ( None , None ) ;
{
chain . write ( ) . mark_dead_end_block ( & b0 . hash ( ) ) ;
}
sync . lock ( ) . on_new_blocks_headers ( 0 , vec! [ b0 . block_header . clone ( ) , b1 . block_header . clone ( ) , b2 . block_header . clone ( ) ] ) ;
let tasks = executor . lock ( ) . take_tasks ( ) ;
assert_eq! ( tasks , vec! [ Task ::Close ( 0 ) ] ) ;
}
#[ test ]
fn collection_closed_on_in_middle_dead_end_block_header ( ) {
let genesis = test_data ::genesis ( ) ;
let b0 = test_data ::block_builder ( ) . header ( ) . parent ( genesis . hash ( ) ) . build ( ) . build ( ) ;
let b1 = test_data ::block_builder ( ) . header ( ) . parent ( b0 . hash ( ) ) . build ( ) . build ( ) ;
let b2 = test_data ::block_builder ( ) . header ( ) . parent ( b1 . hash ( ) ) . build ( ) . build ( ) ;
let ( _ , _ , executor , chain , sync ) = create_sync ( None , None ) ;
{
chain . write ( ) . mark_dead_end_block ( & b1 . hash ( ) ) ;
}
sync . lock ( ) . on_new_blocks_headers ( 0 , vec! [ b0 . block_header . clone ( ) , b1 . block_header . clone ( ) , b2 . block_header . clone ( ) ] ) ;
let tasks = executor . lock ( ) . take_tasks ( ) ;
assert_eq! ( tasks , vec! [ Task ::Close ( 0 ) ] ) ;
}
#[ test ]
fn collection_closed_on_providing_dead_end_block ( ) {
let genesis = test_data ::genesis ( ) ;
let b0 = test_data ::block_builder ( ) . header ( ) . parent ( genesis . hash ( ) ) . build ( ) . build ( ) ;
let ( _ , _ , executor , chain , sync ) = create_sync ( None , None ) ;
{
chain . write ( ) . mark_dead_end_block ( & b0 . hash ( ) ) ;
}
sync . lock ( ) . on_peer_block ( 0 , b0 . into ( ) ) ;
let tasks = executor . lock ( ) . take_tasks ( ) ;
assert_eq! ( tasks , vec! [ Task ::Close ( 0 ) ] ) ;
}
#[ test ]
fn collection_closed_on_providing_child_dead_end_block ( ) {
let genesis = test_data ::genesis ( ) ;
let b0 = test_data ::block_builder ( ) . header ( ) . parent ( genesis . hash ( ) ) . build ( ) . build ( ) ;
let b1 = test_data ::block_builder ( ) . header ( ) . parent ( b0 . hash ( ) ) . build ( ) . build ( ) ;
let ( _ , _ , executor , chain , sync ) = create_sync ( None , None ) ;
{
chain . write ( ) . mark_dead_end_block ( & b0 . hash ( ) ) ;
}
sync . lock ( ) . on_peer_block ( 0 , b1 . into ( ) ) ;
let tasks = executor . lock ( ) . take_tasks ( ) ;
assert_eq! ( tasks , vec! [ Task ::Close ( 0 ) ] ) ;
}
2016-10-24 23:32:56 -07:00
}