Skip to content

Commit

Permalink
feat: implement validator node lmdb store
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi committed Nov 16, 2022
1 parent 797f91a commit 161c17d
Show file tree
Hide file tree
Showing 23 changed files with 1,102 additions and 341 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ impl From<ConsensusConstants> for grpc::ConsensusConstants {
block_weight_inputs: weight_params.input_weight,
block_weight_outputs: weight_params.output_weight,
block_weight_kernels: weight_params.kernel_weight,
validator_node_timeout: cc.validator_node_timeout(),
max_script_byte_size: cc.get_max_script_byte_size() as u64,
faucet_value: cc.faucet_value().as_u64(),
effective_from_height: cc.effective_from_height(),
Expand All @@ -125,6 +124,9 @@ impl From<ConsensusConstants> for grpc::ConsensusConstants {
max_randomx_seed_height: cc.max_randomx_seed_height(),
output_version_range: Some(output_version_range),
permitted_output_types,

// TODO(vnreg)
validator_node_timeout: cc.validator_node_validity_period().as_u64(),
}
}
}
1 change: 1 addition & 0 deletions base_layer/common_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ tari_common = { version = "^0.39", path = "../../common" }
base64 = "0.13.0"
digest = "0.9.0"
lazy_static = "1.4.0"
newtype-ops = "0.1"
rand = "0.7.3"
serde = { version = "1.0.106", features = ["derive"] }
thiserror = "1.0.29"
Expand Down
44 changes: 44 additions & 0 deletions base_layer/common_types/src/epoch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2022. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
use newtype_ops::newtype_ops;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Default)]
pub struct VnEpoch(pub u64);

impl VnEpoch {
pub fn as_u64(&self) -> u64 {
self.0
}

pub fn to_be_bytes(&self) -> [u8; 8] {
self.0.to_be_bytes()
}

pub fn saturating_sub(self, other: VnEpoch) -> VnEpoch {
VnEpoch(self.0.saturating_sub(other.0))
}
}

newtype_ops! { [VnEpoch] {add sub mul div} {:=} Self Self }
newtype_ops! { [VnEpoch] {add sub mul div} {:=} &Self &Self }
newtype_ops! { [VnEpoch] {add sub mul div} {:=} Self &Self }
1 change: 1 addition & 0 deletions base_layer/common_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
pub mod chain_metadata;
pub mod dammsum;
pub mod emoji;
pub mod epoch;
pub mod grpc_authentication;
pub mod tari_address;
pub mod transaction;
Expand Down
9 changes: 7 additions & 2 deletions base_layer/core/src/blocks/genesis_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ fn get_esmeralda_genesis_block_raw() -> Block {
mod test {

use croaring::Bitmap;
use tari_common_types::types::Commitment;
use tari_common_types::{epoch::VnEpoch, types::Commitment};

use super::*;
use crate::{
Expand Down Expand Up @@ -434,7 +434,12 @@ mod test {
.as_ref()
.and_then(|f| f.validator_node_registration())
.unwrap();
vn_mmr.push(reg.derive_shard_key(block.hash()).to_vec()).unwrap();
vn_mmr
.push(
reg.derive_shard_key(None, VnEpoch(0), VnEpoch(0), block.hash())
.to_vec(),
)
.unwrap();
}
}

Expand Down
13 changes: 8 additions & 5 deletions base_layer/core/src/chain_storage/active_validator_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use serde::{Deserialize, Serialize};
use tari_common_types::types::{HashOutput, PublicKey};
use tari_common_types::{
epoch::VnEpoch,
types::{HashOutput, PublicKey},
};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ActiveValidatorNode {
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct ValidatorNodeEntry {
pub shard_key: [u8; 32],
pub from_height: u64,
pub to_height: u64,
pub start_epoch: VnEpoch,
pub end_epoch: VnEpoch,
pub public_key: PublicKey,
pub output_hash: HashOutput,
}
13 changes: 0 additions & 13 deletions base_layer/core/src/chain_storage/db_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use croaring::Bitmap;
use tari_common_types::types::{BlockHash, Commitment, HashOutput};
use tari_utilities::hex::Hex;

use super::{ActiveValidatorNode, TemplateRegistrationEntry};
use crate::{
blocks::{Block, BlockHeader, BlockHeaderAccumulatedData, ChainBlock, ChainHeader, UpdateBlockAccumulatedData},
chain_storage::{error::ChainStorageError, HorizonData, Reorg},
Expand Down Expand Up @@ -359,12 +358,6 @@ pub enum WriteOperation {
reorg: Reorg,
},
ClearAllReorgs,
InsertValidatorNode {
validator_node: ActiveValidatorNode,
},
InsertTemplateRegistration {
template_registration: TemplateRegistrationEntry,
},
}

impl fmt::Display for WriteOperation {
Expand Down Expand Up @@ -461,12 +454,6 @@ impl fmt::Display for WriteOperation {
SetHorizonData { .. } => write!(f, "Set horizon data"),
InsertReorg { .. } => write!(f, "Insert reorg"),
ClearAllReorgs => write!(f, "Clear all reorgs"),
InsertValidatorNode { validator_node } => {
write!(f, "Inserting VN {:?}", validator_node)
},
InsertTemplateRegistration { template_registration } => {
write!(f, "Inserting Template {:?}", template_registration)
},
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions base_layer/core/src/chain_storage/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ pub enum ChainStorageError {
FixedHashSizeError(#[from] FixedHashSizeError),
#[error("Composite key length was exceeded (THIS SHOULD NEVER HAPPEN)")]
CompositeKeyLengthExceeded,
#[error("Failed to decode key bytes: {0}")]
FromKeyBytesFailed(String),
}

impl ChainStorageError {
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/chain_storage/lmdb_db/composite_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl<const L: usize> CompositeKey<L> {
true
}

fn as_bytes(&self) -> &[u8] {
pub fn as_bytes(&self) -> &[u8] {
&self.bytes[..self.len]
}

Expand Down
Loading

0 comments on commit 161c17d

Please sign in to comment.