|
| 1 | +// Copyright 2022. The Tari Project |
| 2 | +// |
| 3 | +// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the |
| 4 | +// following conditions are met: |
| 5 | +// |
| 6 | +// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following |
| 7 | +// disclaimer. |
| 8 | +// |
| 9 | +// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the |
| 10 | +// following disclaimer in the documentation and/or other materials provided with the distribution. |
| 11 | +// |
| 12 | +// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote |
| 13 | +// products derived from this software without specific prior written permission. |
| 14 | +// |
| 15 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 16 | +// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 17 | +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 18 | +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 19 | +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 20 | +// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 21 | +// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 22 | + |
| 23 | +use tari_core::base_node::state_machine_service::states::StateEvent; |
| 24 | + |
| 25 | +use crate::helpers::{sync, sync::WhatToDelete}; |
| 26 | + |
| 27 | +#[tokio::test(flavor = "multi_thread", worker_threads = 1)] |
| 28 | +async fn test_block_sync_happy_path() { |
| 29 | + // env_logger::init(); // Set `$env:RUST_LOG = "trace"` |
| 30 | + |
| 31 | + // Create the network with Alice node and Bob node |
| 32 | + let (mut alice_state_machine, alice_node, bob_node, initial_block, consensus_manager, key_manager) = |
| 33 | + sync::create_network_with_local_and_peer_nodes().await; |
| 34 | + |
| 35 | + // Add some block to Bob's chain |
| 36 | + let _bob_blocks = |
| 37 | + sync::create_and_add_some_blocks(&bob_node, &initial_block, 5, &consensus_manager, &key_manager, &[3; 5]).await; |
| 38 | + assert_eq!(bob_node.blockchain_db.get_height().unwrap(), 5); |
| 39 | + |
| 40 | + // Alice attempts header sync |
| 41 | + let mut header_sync = sync::initialize_sync_headers_with_ping_pong_data(&alice_node, &bob_node); |
| 42 | + let event = sync::sync_headers_execute(&mut alice_state_machine, &mut header_sync).await; |
| 43 | + match event.clone() { |
| 44 | + StateEvent::HeadersSynchronized(..) => { |
| 45 | + // Good, headers are synced |
| 46 | + }, |
| 47 | + _ => panic!("Expected HeadersSynchronized event"), |
| 48 | + } |
| 49 | + |
| 50 | + // Alice attempts block sync |
| 51 | + println!(); |
| 52 | + assert_eq!(alice_node.blockchain_db.get_height().unwrap(), 0); |
| 53 | + let mut block_sync = sync::initialize_sync_blocks(&bob_node); |
| 54 | + let event = sync::sync_blocks_execute(&mut alice_state_machine, &mut block_sync).await; |
| 55 | + match event { |
| 56 | + StateEvent::BlocksSynchronized => { |
| 57 | + // Good, blocks are synced |
| 58 | + }, |
| 59 | + _ => panic!("Expected BlocksSynchronized event"), |
| 60 | + } |
| 61 | + assert_eq!(alice_node.blockchain_db.get_height().unwrap(), 5); |
| 62 | + |
| 63 | + // Alice attempts block sync again |
| 64 | + println!(); |
| 65 | + let mut block_sync = sync::initialize_sync_blocks(&bob_node); |
| 66 | + let event = sync::sync_blocks_execute(&mut alice_state_machine, &mut block_sync).await; |
| 67 | + match event { |
| 68 | + StateEvent::BlocksSynchronized => { |
| 69 | + // Good, blocks are synced |
| 70 | + }, |
| 71 | + _ => panic!("Expected BlocksSynchronized event"), |
| 72 | + } |
| 73 | + assert_eq!(alice_node.blockchain_db.get_height().unwrap(), 5); |
| 74 | +} |
| 75 | + |
| 76 | +#[tokio::test(flavor = "multi_thread", worker_threads = 1)] |
| 77 | +async fn test_block_sync_peer_supplies_no_blocks_with_ban() { |
| 78 | + // env_logger::init(); // Set `$env:RUST_LOG = "trace"` |
| 79 | + |
| 80 | + // Create the network with Alice node and Bob node |
| 81 | + let (mut alice_state_machine, alice_node, bob_node, initial_block, consensus_manager, key_manager) = |
| 82 | + sync::create_network_with_local_and_peer_nodes().await; |
| 83 | + |
| 84 | + // Add some block to Bob's chain |
| 85 | + let blocks = sync::create_and_add_some_blocks( |
| 86 | + &bob_node, |
| 87 | + &initial_block, |
| 88 | + 10, |
| 89 | + &consensus_manager, |
| 90 | + &key_manager, |
| 91 | + &[3; 10], |
| 92 | + ) |
| 93 | + .await; |
| 94 | + assert_eq!(bob_node.blockchain_db.get_height().unwrap(), 10); |
| 95 | + // Add blocks to Alice's chain |
| 96 | + sync::add_some_existing_blocks(&blocks[1..=5], &alice_node); |
| 97 | + assert_eq!(alice_node.blockchain_db.get_height().unwrap(), 5); |
| 98 | + |
| 99 | + // Alice attempts header sync |
| 100 | + let mut header_sync = sync::initialize_sync_headers_with_ping_pong_data(&alice_node, &bob_node); |
| 101 | + let event = sync::sync_headers_execute(&mut alice_state_machine, &mut header_sync).await; |
| 102 | + match event.clone() { |
| 103 | + StateEvent::HeadersSynchronized(..) => { |
| 104 | + // Good, headers are synced |
| 105 | + }, |
| 106 | + _ => panic!("Expected HeadersSynchronized event"), |
| 107 | + } |
| 108 | + |
| 109 | + // Alice attempts block sync, Bob will not send any blocks and be banned |
| 110 | + println!(); |
| 111 | + let mut block_sync = sync::initialize_sync_blocks(&bob_node); |
| 112 | + sync::delete_some_blocks_and_headers(&blocks[5..=10], WhatToDelete::Blocks, &bob_node); |
| 113 | + assert_eq!(bob_node.blockchain_db.get_height().unwrap(), 5); |
| 114 | + let event = sync::sync_blocks_execute(&mut alice_state_machine, &mut block_sync).await; |
| 115 | + match event { |
| 116 | + StateEvent::BlockSyncFailed => { |
| 117 | + // Good, Bob is banned. |
| 118 | + }, |
| 119 | + _ => panic!("Expected BlockSyncFailed event"), |
| 120 | + } |
| 121 | + assert_eq!(alice_node.blockchain_db.get_height().unwrap(), 5); |
| 122 | + |
| 123 | + // Bob will be banned |
| 124 | + assert!(sync::wait_for_is_peer_banned(&alice_node, bob_node.node_identity.node_id(), 1).await); |
| 125 | +} |
| 126 | + |
| 127 | +#[tokio::test(flavor = "multi_thread", worker_threads = 1)] |
| 128 | +async fn test_block_sync_peer_supplies_not_all_blocks_with_ban() { |
| 129 | + // env_logger::init(); // Set `$env:RUST_LOG = "trace"` |
| 130 | + |
| 131 | + // Create the network with Alice node and Bob node |
| 132 | + let (mut alice_state_machine, alice_node, bob_node, initial_block, consensus_manager, key_manager) = |
| 133 | + sync::create_network_with_local_and_peer_nodes().await; |
| 134 | + |
| 135 | + // Add some block to Bob's chain |
| 136 | + let blocks = sync::create_and_add_some_blocks( |
| 137 | + &bob_node, |
| 138 | + &initial_block, |
| 139 | + 10, |
| 140 | + &consensus_manager, |
| 141 | + &key_manager, |
| 142 | + &[3; 10], |
| 143 | + ) |
| 144 | + .await; |
| 145 | + assert_eq!(bob_node.blockchain_db.get_height().unwrap(), 10); |
| 146 | + // Add blocks to Alice's chain |
| 147 | + sync::add_some_existing_blocks(&blocks[1..=5], &alice_node); |
| 148 | + assert_eq!(alice_node.blockchain_db.get_height().unwrap(), 5); |
| 149 | + |
| 150 | + // Alice attempts header sync |
| 151 | + let mut header_sync = sync::initialize_sync_headers_with_ping_pong_data(&alice_node, &bob_node); |
| 152 | + let event = sync::sync_headers_execute(&mut alice_state_machine, &mut header_sync).await; |
| 153 | + match event.clone() { |
| 154 | + StateEvent::HeadersSynchronized(..) => { |
| 155 | + // Good, headers are synced |
| 156 | + }, |
| 157 | + _ => panic!("Expected HeadersSynchronized event"), |
| 158 | + } |
| 159 | + |
| 160 | + // Alice attempts block sync, Bob will not send all blocks and be banned |
| 161 | + println!(); |
| 162 | + let mut block_sync = sync::initialize_sync_blocks(&bob_node); |
| 163 | + sync::delete_some_blocks_and_headers(&blocks[8..=10], WhatToDelete::Blocks, &bob_node); |
| 164 | + assert_eq!(bob_node.blockchain_db.get_height().unwrap(), 8); |
| 165 | + let event = sync::sync_blocks_execute(&mut alice_state_machine, &mut block_sync).await; |
| 166 | + match event { |
| 167 | + StateEvent::BlockSyncFailed => { |
| 168 | + // Good, Bob is banned. |
| 169 | + }, |
| 170 | + _ => panic!("Expected BlockSyncFailed event"), |
| 171 | + } |
| 172 | + assert_eq!(alice_node.blockchain_db.get_height().unwrap(), 5); |
| 173 | + |
| 174 | + // Bob will be banned |
| 175 | + assert!(sync::wait_for_is_peer_banned(&alice_node, bob_node.node_identity.node_id(), 1).await); |
| 176 | +} |
0 commit comments