Skip to content

Commit

Permalink
test: move network integration tests into testing
Browse files Browse the repository at this point in the history
  • Loading branch information
niklaslong committed Jan 14, 2021
1 parent 16555fb commit 841637e
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 185 deletions.
63 changes: 0 additions & 63 deletions network/tests/common/mod.rs

This file was deleted.

60 changes: 0 additions & 60 deletions network/tests/transaction_sync.rs

This file was deleted.

102 changes: 43 additions & 59 deletions testing/src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,74 +20,58 @@ pub use tcp::*;
pub mod blocks;
pub use blocks::*;

use crate::consensus::*;
use snarkos_consensus::{MemoryPool, MerkleTreeLedger};
use snarkos_network::{environment::Environment, external::Channel, Server};
use snarkvm_dpc::base_dpc::{instantiated::Components, parameters::PublicParameters};
pub mod sync;

use crate::{
consensus::{FIXTURE_VK, TEST_CONSENSUS},
dpc::load_verifying_parameters,
};

use snarkos_network::{external::Channel, Environment, Server};

use parking_lot::{Mutex, RwLock};
use std::{net::SocketAddr, sync::Arc, time::Duration};
use tokio::net::{tcp::OwnedReadHalf, TcpListener};

pub const CONNECTION_FREQUENCY_LONG: u64 = 100000; // 100 seconds
pub const CONNECTION_FREQUENCY_SHORT: u64 = 100; // .1 seconds
pub const CONNECTION_FREQUENCY_SHORT_TIMEOUT: u64 = 200; // .2 seconds

/// Puts the current tokio thread to sleep for given milliseconds
pub async fn sleep(time: u64) {
tokio::time::sleep(std::time::Duration::from_millis(time)).await;
}

/// Returns an `Environment` struct with given arguments
pub fn initialize_test_environment(
server_address: Option<SocketAddr>,
bootnodes: Vec<String>,
storage: Arc<RwLock<MerkleTreeLedger>>,
parameters: PublicParameters<Components>,
) -> anyhow::Result<Environment> {
let consensus = Arc::new(TEST_CONSENSUS.clone());
let memory_pool = Arc::new(Mutex::new(MemoryPool::new()));

Ok(Environment::new(
storage,
memory_pool,
consensus,
/// Starts a node with the specified bootnodes.
pub async fn start_node(bootnodes: Vec<String>) -> Server {
let storage = FIXTURE_VK.ledger();
let memory_pool = snarkos_consensus::MemoryPool::new();
let memory_pool_lock = Arc::new(Mutex::new(memory_pool));
let consensus = TEST_CONSENSUS.clone();
let parameters = load_verifying_parameters();
let socket_address = None;
let min_peers = 1;
let max_peers = 10;
let is_bootnode = false;
let is_miner = false;

let environment = Environment::new(
Arc::new(RwLock::new(storage)),
memory_pool_lock,
Arc::new(consensus),
Arc::new(parameters),
server_address,
1,
5,
socket_address,
min_peers,
max_peers,
bootnodes,
true,
false,
// Set all sync durations to a generic 10s duration
Duration::new(10, 0),
Duration::new(10, 0),
Duration::new(10, 0),
)?)
is_bootnode,
is_miner,
Duration::from_secs(2),
Duration::from_secs(2),
Duration::from_secs(2),
)
.unwrap();

let mut node = Server::new(environment).await.unwrap();
node.start().await.unwrap();

node
}

/// Returns a server struct with given arguments
pub async fn initialize_test_server(
server_address: Option<SocketAddr>,
bootnodes: Vec<String>,
storage: Arc<RwLock<MerkleTreeLedger>>,
parameters: PublicParameters<Components>,
) -> Server {
let environment = initialize_test_environment(server_address, bootnodes, storage, parameters).unwrap();

// let sync_handler = SyncManager::new(bootnode_address);
// let sync_handler_lock = Arc::new(Mutex::new(sync_handler));

Server::new(environment
// consensus,
// storage,
// parameters,
// memory_pool_lock,
// sync_handler_lock,
// connection_frequency,
)
.await
.unwrap()
/// Puts the current tokio thread to sleep for given milliseconds
pub async fn sleep(time: u64) {
tokio::time::sleep(std::time::Duration::from_millis(time)).await;
}

/// Starts a server on a new thread. Takes full ownership of server.
Expand Down
45 changes: 42 additions & 3 deletions network/tests/block_sync.rs → testing/src/network/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

// You should have received a copy of the GNU General Public License
// along with the snarkOS library. If not, see <https://www.gnu.org/licenses/>.
mod common;
use common::start_node;

use snarkos_testing::consensus::{BLOCK_1, BLOCK_2};
use crate::{
consensus::{BLOCK_1, BLOCK_2, TRANSACTION_1},
network::start_node,
};

use snarkvm_objects::block::Block;

use std::time::Duration;
Expand Down Expand Up @@ -79,3 +81,40 @@ async fn simple_block_sync() {
.block_hash_exists(&block_struct_2.header.get_hash())
);
}

#[tokio::test]
async fn simple_transaction_sync() {
use snarkos_consensus::memory_pool::Entry;
use snarkvm_dpc::instantiated::Tx;
use snarkvm_utilities::bytes::FromBytes;

let node_alice = start_node(vec![]).await;
let alice_address = node_alice.local_address().unwrap();

// insert transaction into node_alice
let mut memory_pool = node_alice.environment.memory_pool().lock();
let storage = node_alice.environment.storage().read();

let transaction = Tx::read(&TRANSACTION_1[..]).unwrap();
let size = TRANSACTION_1.len();
let entry = Entry {
size_in_bytes: size,
transaction: transaction.clone(),
};

memory_pool.insert(&storage, entry.clone()).unwrap().unwrap();

// drop the locks to avoid deadlocks
drop(memory_pool);
drop(storage);

let node_bob = start_node(vec![alice_address.to_string()]).await;

// T 0-2s: not much happens
// T 2s: first sync occures, a peer isn't yet connected to sync with
// T 4s: second sync occures, this time a peer is selected for the block sync
sleep(Duration::new(5, 0)).await;

// check transaction is present in bob's memory pool
assert!(node_bob.environment.memory_pool().lock().contains(&entry));
}

0 comments on commit 841637e

Please sign in to comment.