Skip to content

Commit

Permalink
Merge branch 'testnet2' into update
Browse files Browse the repository at this point in the history
  • Loading branch information
howardwu authored Jan 12, 2022
2 parents 08a2727 + d43601d commit a464b67
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 89 deletions.
26 changes: 13 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ default = []
test = []

[dependencies]
snarkvm = { git = "https://github.com/AleoHQ/snarkVM.git", rev = "459ea96" }
snarkvm = { git = "https://github.com/AleoHQ/snarkVM.git", rev = "2af7756" }
#snarkvm = { path = "../snarkVM" }

bytes = "1.0.0"
Expand Down
13 changes: 13 additions & 0 deletions src/environment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::helpers::{NodeType, Status, Tasks};
use snarkvm::dpc::Network;

use once_cell::sync::OnceCell;
use rayon::{ThreadPool, ThreadPoolBuilder};
use std::{
collections::HashSet,
fmt::Debug,
Expand Down Expand Up @@ -104,6 +105,18 @@ pub trait Environment: 'static + Clone + Debug + Default + Send + Sync {
static TERMINATOR: OnceCell<Arc<AtomicBool>> = OnceCell::new();
TERMINATOR.get_or_init(|| Arc::new(AtomicBool::new(false)))
}

/// Returns a thread pool for the node to perform intensive operations.
fn thread_pool() -> &'static Arc<ThreadPool> {
static POOL: OnceCell<Arc<ThreadPool>> = OnceCell::new();
POOL.get_or_init(|| {
Arc::new(ThreadPoolBuilder::new()
.stack_size(8 * 1024 * 1024)
.num_threads((num_cpus::get() * 7 / 8).max(2))
.build()
.expect("Failed to initialize a thread pool for the node"))
})
}
}

#[derive(Clone, Debug, Default)]
Expand Down
122 changes: 122 additions & 0 deletions src/helpers/block_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Copyright (C) 2019-2021 Aleo Systems Inc.
// This file is part of the snarkOS library.

// The snarkOS library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The snarkOS library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// 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/>.

use snarkvm::dpc::prelude::*;

use std::hash::{Hash, Hasher};

///
/// A request for a block with the specified height and an optional hash.
///
#[derive(Clone, Debug)]
pub struct BlockRequest<N: Network> {
block_height: u32,
block_hash: Option<N::BlockHash>,
}

impl<N: Network> BlockRequest<N> {
/// Returns the block height stored in the request.
pub fn block_height(&self) -> u32 {
self.block_height
}

/// Returns the block hash stored in the request, if it exists.
pub fn block_hash(&self) -> Option<N::BlockHash> {
self.block_hash
}
}

impl<N: Network> From<u32> for BlockRequest<N> {
fn from(height: u32) -> Self {
Self {
block_height: height,
block_hash: None,
}
}
}

impl<N: Network> From<(u32, Option<N::BlockHash>)> for BlockRequest<N> {
fn from((height, hash): (u32, Option<N::BlockHash>)) -> Self {
Self {
block_height: height,
block_hash: hash,
}
}
}

// The height is the primary key, so use only it for hashing purposes.
impl<N: Network> PartialEq for BlockRequest<N> {
fn eq(&self, other: &Self) -> bool {
self.block_height == other.block_height
}
}

impl<N: Network> Eq for BlockRequest<N> {}

// The k1 == k2 -> hash(k1) == hash(k2) rule must hold.
impl<N: Network> Hash for BlockRequest<N> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.block_height.hash(state);
}
}

#[cfg(test)]
mod tests {
use super::*;
use snarkvm::{dpc::testnet2::Testnet2, prelude::UniformRand};

use rand::{thread_rng, Rng};

#[test]
fn test_block_request() {
let rng = &mut thread_rng();

for _ in 0..5 {
let block_height: u32 = rng.gen();
let block_hash = <Testnet2 as Network>::BlockHash::rand(rng);

let request = BlockRequest::<Testnet2>::from(block_height);
assert_eq!(block_height, request.block_height());
assert_eq!(None, request.block_hash());

let request = BlockRequest::<Testnet2>::from((block_height, None));
assert_eq!(block_height, request.block_height());
assert_eq!(None, request.block_hash());

let request = BlockRequest::<Testnet2>::from((block_height, Some(block_hash)));
assert_eq!(block_height, request.block_height());
assert_eq!(Some(block_hash), request.block_hash());
}
}

#[test]
fn test_block_request_eq() {
let rng = &mut thread_rng();

for _ in 0..5 {
let block_height: u32 = rng.gen();
let block_hash = <Testnet2 as Network>::BlockHash::rand(rng);

let a = BlockRequest::<Testnet2>::from(block_height);
let b = BlockRequest::<Testnet2>::from((block_height, None));
assert_eq!(a, b);

let a = BlockRequest::<Testnet2>::from(block_height);
let b = BlockRequest::<Testnet2>::from((block_height, Some(block_hash)));
assert_eq!(a, b);
}
}
}
3 changes: 3 additions & 0 deletions src/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
// 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/>.

pub mod block_request;
pub use block_request::*;

pub mod block_requests;
pub use block_requests::*;

Expand Down
50 changes: 3 additions & 47 deletions src/network/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with the snarkOS library. If not, see <https://www.gnu.org/licenses/>.

use crate::{
helpers::{block_requests::*, CircularMap, NodeType, State},
helpers::{block_requests::*, BlockRequest, CircularMap, NodeType, State},
Data,
Environment,
LedgerReader,
Expand All @@ -32,7 +32,6 @@ use anyhow::Result;
use chrono::Utc;
use std::{
collections::HashMap,
hash::{Hash, Hasher},
net::SocketAddr,
path::Path,
sync::{atomic::Ordering, Arc},
Expand Down Expand Up @@ -71,49 +70,6 @@ pub enum LedgerRequest<N: Network> {
UnconfirmedBlock(SocketAddr, Block<N>, ProverRouter<N>),
}

///
/// A request for a block with the specified height and possibly a hash.
///
#[derive(Clone, Debug)]
pub struct BlockRequest<N: Network> {
block_height: u32,
block_hash: Option<N::BlockHash>,
}

// The height is the primary key, so use only it for hashing purposes.
impl<N: Network> PartialEq for BlockRequest<N> {
fn eq(&self, other: &Self) -> bool {
self.block_height == other.block_height
}
}

impl<N: Network> Eq for BlockRequest<N> {}

// The k1 == k2 -> hash(k1) == hash(k2) rule must hold.
impl<N: Network> Hash for BlockRequest<N> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.block_height.hash(state);
}
}

impl<N: Network> From<u32> for BlockRequest<N> {
fn from(height: u32) -> Self {
Self {
block_height: height,
block_hash: None,
}
}
}

impl<N: Network> From<(u32, Option<N::BlockHash>)> for BlockRequest<N> {
fn from((height, hash): (u32, Option<N::BlockHash>)) -> Self {
Self {
block_height: height,
block_hash: hash,
}
}
}

pub type PeersState<N> = HashMap<SocketAddr, Option<(NodeType, State, Option<bool>, u32, BlockLocators<N>)>>;

///
Expand Down Expand Up @@ -537,8 +493,8 @@ impl<N: Network, E: Environment> Ledger<N, E> {
'outer: for requests in self.block_requests.read().await.values() {
for request in requests.keys() {
// If the unconfirmed block conflicts with a requested block on a fork, skip.
if request.block_height == unconfirmed_block_height {
if let Some(requested_block_hash) = request.block_hash {
if request.block_height() == unconfirmed_block_height {
if let Some(requested_block_hash) = request.block_hash() {
if unconfirmed_block.hash() != requested_block_hash {
is_block_on_fork = true;
break 'outer;
Expand Down
Loading

0 comments on commit a464b67

Please sign in to comment.