From a967e7960cd18c1b5b28fb63818731408001ec3c Mon Sep 17 00:00:00 2001 From: Svyatoslav Nikolsky Date: Tue, 29 Nov 2016 19:39:00 +0300 Subject: [PATCH] fixing sync TODOs --- db/src/lib.rs | 2 +- db/src/test_storage.rs | 10 ++++ sync/src/local_node.rs | 3 +- sync/src/synchronization_client.rs | 70 +++++++++++++++------------- sync/src/synchronization_executor.rs | 21 ++++----- sync/src/synchronization_server.rs | 18 +++---- 6 files changed, 67 insertions(+), 57 deletions(-) diff --git a/db/src/lib.rs b/db/src/lib.rs index e49d128b..517ed591 100644 --- a/db/src/lib.rs +++ b/db/src/lib.rs @@ -56,7 +56,7 @@ pub type SharedStore = std::sync::Arc; pub use best_block::BestBlock; pub use storage::{Storage, Store}; -pub use error::Error; +pub use error::{Error, ConsistencyError}; pub use kvdb::Database; pub use transaction_provider::{TransactionProvider, AsTransactionProvider, PreviousTransactionOutputProvider}; pub use transaction_meta_provider::TransactionMetaProvider; diff --git a/db/src/test_storage.rs b/db/src/test_storage.rs index 22750aad..5dbc60ba 100644 --- a/db/src/test_storage.rs +++ b/db/src/test_storage.rs @@ -26,6 +26,7 @@ struct TestData { blocks: HashMap, heights: HashMap, hashes: HashMap, + insert_errors: HashMap, } impl TestStorage { @@ -61,6 +62,10 @@ impl TestStorage { let genesis_block: Block = "0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000".into(); TestStorage::with_blocks(&vec![genesis_block]) } + + pub fn insert_error(&mut self, hash: H256, err: Error) { + self.data.write().insert_errors.insert(hash, err); + } } impl BlockProvider for TestStorage { @@ -120,6 +125,11 @@ impl BlockStapler for TestStorage { fn insert_block(&self, block: &chain::Block) -> Result { let hash = block.hash(); let mut data = self.data.write(); + + if let Some(err) = data.insert_errors.remove(&hash) { + return Err(err); + } + match data.blocks.entry(hash.clone()) { Entry::Occupied(mut entry) => { replace(entry.get_mut(), block.clone()); diff --git a/sync/src/local_node.rs b/sync/src/local_node.rs index e3c23e6d..97077c34 100644 --- a/sync/src/local_node.rs +++ b/sync/src/local_node.rs @@ -92,7 +92,8 @@ impl LocalNode where T: SynchronizationTaskExecutor + PeersCon self.client.lock().on_new_transactions_inventory(peer_index, transactions_inventory); } - // TODO: process other inventory types + // currently we do not setup connection filter => skip InventoryType::MessageFilteredBlock + // currently we do not send sendcmpct message => skip InventoryType::MessageCompactBlock } pub fn on_peer_getdata(&self, peer_index: usize, message: types::GetData) { diff --git a/sync/src/synchronization_client.rs b/sync/src/synchronization_client.rs index 4721fb06..017c3fa8 100644 --- a/sync/src/synchronization_client.rs +++ b/sync/src/synchronization_client.rs @@ -901,8 +901,6 @@ impl SynchronizationClientCore where T: TaskExecutor { } )); - // TODO: start management worker only when synchronization is started - // currently impossible because there is no way to call Interval::new with Remote && Handle is not-Send { let peers_config = ManagePeersConfig::default(); let unknown_config = ManageUnknownBlocksConfig::default(); @@ -999,7 +997,7 @@ impl SynchronizationClientCore where T: TaskExecutor { }) .collect(); - Some(Task::SendCompactBlocks(peer_index, block_header_and_ids, ServerTaskIndex::None)) + Some(Task::SendCompactBlocks(peer_index, block_header_and_ids)) }, BlockAnnouncementType::SendInventory => { let inventory: Vec<_> = new_blocks_hashes.iter() @@ -1010,7 +1008,7 @@ impl SynchronizationClientCore where T: TaskExecutor { }) .collect(); if !inventory.is_empty() { - Some(Task::SendInventory(peer_index, inventory, ServerTaskIndex::None)) + Some(Task::SendInventory(peer_index, inventory)) } else { None } @@ -1040,7 +1038,7 @@ impl SynchronizationClientCore where T: TaskExecutor { }) .collect(); if !inventory.is_empty() { - Some(Task::SendInventory(peer_index, inventory, ServerTaskIndex::None)) + Some(Task::SendInventory(peer_index, inventory)) } else { None } @@ -1191,17 +1189,12 @@ impl SynchronizationClientCore where T: TaskExecutor { } /// Process new peer transaction - fn process_peer_transaction(&mut self, peer_index: Option, hash: H256, transaction: Transaction) -> Option> { + fn process_peer_transaction(&mut self, _peer_index: Option, hash: H256, transaction: Transaction) -> Option> { // if we are in synchronization state, we will ignore this message if self.state.is_synchronizing() { return None; } - // mark peer as useful (TODO: remove after self.all_peers() would be all peers, not sync one) - if let Some(peer_index) = peer_index { - self.peers.useful_peer(peer_index); - } - // else => verify transaction + it's orphans and then add to the memory pool let mut chain = self.chain.write(); @@ -1548,7 +1541,7 @@ pub mod tests { assert!(tasks.iter().any(|t| t == &Task::RequestMemoryPool(2))); let inventory = vec![InventoryVector { inv_type: InventoryType::MessageBlock, hash: block.hash() }]; - assert!(tasks.iter().any(|t| t == &Task::SendInventory(1, inventory.clone(), ServerTaskIndex::None))); + assert!(tasks.iter().any(|t| t == &Task::SendInventory(1, inventory.clone()))); } #[test] @@ -1826,7 +1819,19 @@ pub mod tests { #[test] fn sync_after_db_insert_nonfatal_fail() { - // TODO: implement me + 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); } #[test] @@ -2093,7 +2098,7 @@ pub mod tests { { let tasks = executor.lock().take_tasks(); let inventory = vec![InventoryVector { inv_type: InventoryType::MessageBlock, hash: b2.hash() }]; - assert_eq!(tasks, vec![Task::RequestBlocksHeaders(2), Task::SendInventory(1, inventory, ServerTaskIndex::None)]); + assert_eq!(tasks, vec![Task::RequestBlocksHeaders(2), Task::SendInventory(1, inventory)]); } sync.on_new_blocks_headers(1, vec![b3.block_header.clone()]); @@ -2103,7 +2108,7 @@ pub mod tests { { let tasks = executor.lock().take_tasks(); let inventory = vec![InventoryVector { inv_type: InventoryType::MessageBlock, hash: b3.hash() }]; - assert!(tasks.iter().any(|t| t == &Task::SendInventory(2, inventory.clone(), ServerTaskIndex::None))); + assert!(tasks.iter().any(|t| t == &Task::SendInventory(2, inventory.clone()))); } } @@ -2111,17 +2116,16 @@ pub mod tests { fn relay_new_transaction_when_in_saturated_state() { 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 tx2_hash = tx2.hash(); + let tx: Transaction = test_data::TransactionBuilder::with_output(20).into(); + let tx_hash = tx.hash(); let mut sync = sync.lock(); - sync.on_peer_transaction(1, tx1); - sync.on_peer_transaction(2, tx2); + sync.on_peer_connected(1); + sync.on_peer_transaction(2, tx); let tasks = { executor.lock().take_tasks() }; - let inventory = vec![InventoryVector { inv_type: InventoryType::MessageTx, hash: tx2_hash }]; - assert_eq!(tasks, vec![Task::SendInventory(1, inventory, ServerTaskIndex::None)]); + let inventory = vec![InventoryVector { inv_type: InventoryType::MessageTx, hash: tx_hash }]; + assert_eq!(tasks, vec![Task::SendInventory(1, inventory)]); } #[test] @@ -2162,9 +2166,9 @@ pub mod tests { let tasks = { executor.lock().take_tasks() }; let inventory = vec![InventoryVector { inv_type: InventoryType::MessageTx, hash: tx1_hash }]; assert_eq!(tasks, vec![ - Task::SendInventory(1, inventory.clone(), ServerTaskIndex::None), - Task::SendInventory(3, inventory.clone(), ServerTaskIndex::None), - Task::SendInventory(4, inventory.clone(), ServerTaskIndex::None), + Task::SendInventory(1, inventory.clone()), + Task::SendInventory(3, inventory.clone()), + Task::SendInventory(4, inventory.clone()), ]); // tx2 is relayed to peers: 2, 3, 4 @@ -2173,9 +2177,9 @@ pub mod tests { let tasks = { executor.lock().take_tasks() }; let inventory = vec![InventoryVector { inv_type: InventoryType::MessageTx, hash: tx2_hash }]; assert_eq!(tasks, vec![ - Task::SendInventory(2, inventory.clone(), ServerTaskIndex::None), - Task::SendInventory(3, inventory.clone(), ServerTaskIndex::None), - Task::SendInventory(4, inventory.clone(), ServerTaskIndex::None), + Task::SendInventory(2, inventory.clone()), + Task::SendInventory(3, inventory.clone()), + Task::SendInventory(4, inventory.clone()), ]); } @@ -2201,7 +2205,7 @@ pub mod tests { let headers = vec![b0.block_header.clone()]; assert_eq!(tasks, vec![Task::RequestBlocksHeaders(1), Task::SendHeaders(2, headers, ServerTaskIndex::None), - Task::SendInventory(3, inventory, ServerTaskIndex::None), + Task::SendInventory(3, inventory), ]); } @@ -2245,13 +2249,13 @@ pub mod tests { inv_type: InventoryType::MessageTx, hash: tx1_hash.clone(), } - ], ServerTaskIndex::None), + ]), Task::SendInventory(4, vec![ InventoryVector { inv_type: InventoryType::MessageTx, hash: tx1_hash.clone(), } - ], ServerTaskIndex::None), + ]), ]); } @@ -2288,9 +2292,9 @@ pub mod tests { assert_eq!(tasks.len(), 3); assert_eq!(tasks[0], Task::RequestBlocksHeaders(1)); match tasks[1] { - Task::SendCompactBlocks(2, _, _) => (), + Task::SendCompactBlocks(2, _) => (), _ => panic!("unexpected task"), } - assert_eq!(tasks[2], Task::SendInventory(3, inventory, ServerTaskIndex::None)); + assert_eq!(tasks[2], Task::SendInventory(3, inventory)); } } diff --git a/sync/src/synchronization_executor.rs b/sync/src/synchronization_executor.rs index 54a2b3c7..d26c21a8 100644 --- a/sync/src/synchronization_executor.rs +++ b/sync/src/synchronization_executor.rs @@ -17,7 +17,6 @@ pub trait TaskExecutor : Send + 'static { fn execute(&mut self, task: Task); } -// TODO: get rid of unneeded ServerTaskIndex-es /// Synchronization task for the peer. #[derive(Debug, PartialEq)] pub enum Task { @@ -30,7 +29,7 @@ pub enum Task { /// Request memory pool contents RequestMemoryPool(usize), /// Send block. - SendBlock(usize, Block, ServerTaskIndex), + SendBlock(usize, Block), /// Send merkleblock SendMerkleBlock(usize, types::MerkleBlock), /// Send transaction @@ -38,13 +37,13 @@ pub enum Task { /// Send block transactions SendBlockTxn(usize, H256, Vec), /// Send notfound - SendNotFound(usize, Vec, ServerTaskIndex), + SendNotFound(usize, Vec), /// Send inventory - SendInventory(usize, Vec, ServerTaskIndex), + SendInventory(usize, Vec), /// Send headers SendHeaders(usize, Vec, ServerTaskIndex), /// Send compact blocks - SendCompactBlocks(usize, Vec, ServerTaskIndex), + SendCompactBlocks(usize, Vec), /// Notify io about ignored request Ignore(usize, u32), } @@ -130,13 +129,12 @@ impl TaskExecutor for LocalSynchronizationTaskExecutor { connection.send_getdata(&getdata); } }, - Task::SendBlock(peer_index, block, id) => { + Task::SendBlock(peer_index, block) => { let block_message = types::Block { block: block, }; if let Some(connection) = self.peers.get_mut(&peer_index) { - assert_eq!(id.raw(), None); trace!(target: "sync", "Sending block {:?} to peer#{}", block_message.block.hash().to_reversed_str(), peer_index); connection.send_block(&block_message); } @@ -170,24 +168,22 @@ impl TaskExecutor for LocalSynchronizationTaskExecutor { connection.send_block_txn(&transactions_message); } }, - Task::SendNotFound(peer_index, unknown_inventory, id) => { + Task::SendNotFound(peer_index, unknown_inventory) => { let notfound = types::NotFound { inventory: unknown_inventory, }; if let Some(connection) = self.peers.get_mut(&peer_index) { - assert_eq!(id.raw(), None); trace!(target: "sync", "Sending notfound to peer#{} with {} items", peer_index, notfound.inventory.len()); connection.send_notfound(¬found); } }, - Task::SendInventory(peer_index, inventory, id) => { + Task::SendInventory(peer_index, inventory) => { let inventory = types::Inv { inventory: inventory, }; if let Some(connection) = self.peers.get_mut(&peer_index) { - assert_eq!(id.raw(), None); trace!(target: "sync", "Sending inventory to peer#{} with {} items", peer_index, inventory.inventory.len()); connection.send_inventory(&inventory); } @@ -205,9 +201,8 @@ impl TaskExecutor for LocalSynchronizationTaskExecutor { } } }, - Task::SendCompactBlocks(peer_index, compact_blocks, id) => { + Task::SendCompactBlocks(peer_index, compact_blocks) => { if let Some(connection) = self.peers.get_mut(&peer_index) { - assert_eq!(id.raw(), None); for compact_block in compact_blocks { trace!(target: "sync", "Sending compact_block {:?} to peer#{}", compact_block.header.hash(), peer_index); connection.send_compact_block(&types::CompactBlock { diff --git a/sync/src/synchronization_server.rs b/sync/src/synchronization_server.rs index 3c24e981..f0692d4f 100644 --- a/sync/src/synchronization_server.rs +++ b/sync/src/synchronization_server.rs @@ -224,7 +224,7 @@ impl SynchronizationServer { inv_type: InventoryType::MessageBlock, hash: hash, }).collect(); - executor.lock().execute(Task::SendInventory(peer_index, inventory, indexed_task.id)); + executor.lock().execute(Task::SendInventory(peer_index, inventory)); } } else { @@ -309,7 +309,7 @@ impl SynchronizationServer { .collect(); if !inventory.is_empty() { trace!(target: "sync", "Going to respond with {} memory-pool transactions ids to peer#{}", inventory.len(), peer_index); - executor.lock().execute(Task::SendInventory(peer_index, inventory, indexed_task.id)); + executor.lock().execute(Task::SendInventory(peer_index, inventory)); } else { assert_eq!(indexed_task.id, ServerTaskIndex::None); } @@ -318,7 +318,7 @@ impl SynchronizationServer { }, // `notfound` ServerTask::ReturnNotFound(inventory) => { - executor.lock().execute(Task::SendNotFound(peer_index, inventory, indexed_task.id)); + executor.lock().execute(Task::SendNotFound(peer_index, inventory)); // inform that we have processed task for peer queue.lock().task_processed(peer_index); }, @@ -326,7 +326,7 @@ impl SynchronizationServer { ServerTask::ReturnBlock(block_hash) => { let block = chain.read().storage().block(db::BlockRef::Hash(block_hash)) .expect("we have checked that block exists in ServeGetData; db is append-only; qed"); - executor.lock().execute(Task::SendBlock(peer_index, block, indexed_task.id)); + executor.lock().execute(Task::SendBlock(peer_index, block)); // inform that we have processed task for peer queue.lock().task_processed(peer_index); }, @@ -609,7 +609,7 @@ pub mod tests { server.serve_getdata(0, FilteredInventory::with_unfiltered(inventory.clone())).map(|t| server.add_task(0, t)); // => respond with notfound let tasks = DummyTaskExecutor::wait_tasks(executor); - assert_eq!(tasks, vec![Task::SendNotFound(0, inventory, ServerTaskIndex::None)]); + assert_eq!(tasks, vec![Task::SendNotFound(0, inventory)]); } #[test] @@ -625,7 +625,7 @@ pub mod tests { server.serve_getdata(0, FilteredInventory::with_unfiltered(inventory)).map(|t| server.add_task(0, t)); // => respond with block let tasks = DummyTaskExecutor::wait_tasks(executor); - assert_eq!(tasks, vec![Task::SendBlock(0, test_data::genesis(), ServerTaskIndex::None)]); + assert_eq!(tasks, vec![Task::SendBlock(0, test_data::genesis())]); } #[test] @@ -659,7 +659,7 @@ pub mod tests { hash: test_data::block_h1().hash(), }]; let tasks = DummyTaskExecutor::wait_tasks(executor); - assert_eq!(tasks, vec![Task::SendInventory(0, inventory, ServerTaskIndex::None)]); + assert_eq!(tasks, vec![Task::SendInventory(0, inventory)]); } #[test] @@ -722,7 +722,7 @@ pub mod tests { hash: transaction_hash, }]; let tasks = DummyTaskExecutor::wait_tasks(executor); - assert_eq!(tasks, vec![Task::SendInventory(0, inventory, ServerTaskIndex::None)]); + assert_eq!(tasks, vec![Task::SendInventory(0, inventory)]); } #[test] @@ -764,7 +764,7 @@ pub mod tests { server.serve_getdata(0, FilteredInventory::with_unfiltered(inventory.clone())).map(|t| server.add_task(0, t)); // => respond with notfound let tasks = DummyTaskExecutor::wait_tasks(executor); - assert_eq!(tasks, vec![Task::SendNotFound(0, inventory, ServerTaskIndex::None)]); + assert_eq!(tasks, vec![Task::SendNotFound(0, inventory)]); } #[test]