Skip to content

Commit

Permalink
Merge pull request #4965 from sdbondi/merge-feature-dan
Browse files Browse the repository at this point in the history
feat: merge feature-dan into development
  • Loading branch information
stringhandler authored Nov 28, 2022
2 parents 54efe18 + f04d79c commit de54416
Show file tree
Hide file tree
Showing 45 changed files with 1,200 additions and 308 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.

4 changes: 3 additions & 1 deletion applications/tari_app_grpc/proto/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ message ConsensusConstants {
/// Maximum byte size of TariScript
uint64 max_script_byte_size = 18;
/// How long does it take to timeout validator node registration
uint64 validator_node_timeout = 19;
uint64 validator_node_validity_period = 19;
/// The height at which these constants become effective
uint64 effective_from_height = 20;
/// Current version of the blockchain
Expand All @@ -143,4 +143,6 @@ message ConsensusConstants {
Range kernel_version_range = 28;
/// An allowlist of output types
repeated OutputType permitted_output_types = 29;
/// The length of an epoch
uint64 epoch_length = 30;
}
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,8 @@ 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,
validator_node_validity_period: cc.validator_node_validity_period().as_u64(),
epoch_length: cc.epoch_length(),
}
}
}
13 changes: 7 additions & 6 deletions applications/tari_app_grpc/src/conversions/sidechain_feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use tari_core::{
SideChainFeature,
TemplateType,
ValidatorNodeRegistration,
ValidatorNodeSignature,
},
};
use tari_utilities::ByteArray;
Expand Down Expand Up @@ -79,21 +80,21 @@ impl TryFrom<grpc::ValidatorNodeRegistration> for ValidatorNodeRegistration {
type Error = String;

fn try_from(value: grpc::ValidatorNodeRegistration) -> Result<Self, Self::Error> {
Ok(Self {
public_key: PublicKey::from_bytes(&value.public_key).map_err(|e| e.to_string())?,
signature: value
Ok(ValidatorNodeRegistration::new(ValidatorNodeSignature::new(
PublicKey::from_bytes(&value.public_key).map_err(|e| e.to_string())?,
value
.signature
.map(Signature::try_from)
.ok_or("signature not provided")??,
})
)))
}
}

impl From<ValidatorNodeRegistration> for grpc::ValidatorNodeRegistration {
fn from(value: ValidatorNodeRegistration) -> Self {
Self {
public_key: value.public_key.to_vec(),
signature: Some(value.signature.into()),
public_key: value.public_key().to_vec(),
signature: Some(value.signature().into()),
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions applications/tari_console_wallet/src/automation/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ pub async fn claim_htlc_refund(
}

pub async fn register_validator_node(
amount: MicroTari,
mut wallet_transaction_service: TransactionServiceHandle,
validator_node_public_key: PublicKey,
validator_node_signature: Signature,
Expand All @@ -202,6 +203,7 @@ pub async fn register_validator_node(
) -> Result<TxId, CommandError> {
wallet_transaction_service
.register_validator_node(
amount,
validator_node_public_key,
validator_node_signature,
selection_criteria,
Expand Down Expand Up @@ -971,6 +973,7 @@ pub async fn command_runner(
},
RegisterValidatorNode(args) => {
let tx_id = register_validator_node(
args.amount,
transaction_service.clone(),
args.validator_node_public_key.into(),
Signature::new(
Expand Down
1 change: 1 addition & 0 deletions applications/tari_console_wallet/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ pub struct HashPasswordArgs {

#[derive(Debug, Args, Clone)]
pub struct RegisterValidatorNodeArgs {
pub amount: MicroTari,
pub validator_node_public_key: UniPublicKey,
pub validator_node_public_nonce: UniPublicKey,
pub validator_node_signature: Vec<u8>,
Expand Down
41 changes: 36 additions & 5 deletions applications/tari_console_wallet/src/grpc/wallet_grpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,23 @@ use tari_common_types::{
types::{BlockHash, PublicKey, Signature},
};
use tari_comms::{multiaddr::Multiaddr, types::CommsPublicKey, CommsNode};
use tari_core::transactions::{
tari_amount::{MicroTari, T},
transaction_components::{CodeTemplateRegistration, OutputFeatures, OutputType, SideChainFeature, UnblindedOutput},
use tari_core::{
consensus::{ConsensusConstants, ConsensusManager},
transactions::{
tari_amount::{MicroTari, T},
transaction_components::{
CodeTemplateRegistration,
OutputFeatures,
OutputType,
SideChainFeature,
UnblindedOutput,
},
},
};
use tari_utilities::{hex::Hex, ByteArray};
use tari_wallet::{
connectivity_service::{OnlineStatus, WalletConnectivityInterface},
error::WalletStorageError,
output_manager_service::{handle::OutputManagerHandle, UtxoSelectionCriteria},
transaction_service::{
handle::TransactionServiceHandle,
Expand Down Expand Up @@ -127,11 +137,13 @@ async fn send_transaction_event(

pub struct WalletGrpcServer {
wallet: WalletSqlite,
rules: ConsensusManager,
}

impl WalletGrpcServer {
pub fn new(wallet: WalletSqlite) -> Self {
Self { wallet }
let rules = ConsensusManager::builder(wallet.network.as_network()).build();
Self { wallet, rules }
}

fn get_transaction_service(&self) -> TransactionServiceHandle {
Expand All @@ -145,6 +157,18 @@ impl WalletGrpcServer {
fn comms(&self) -> &CommsNode {
&self.wallet.comms
}

fn get_consensus_constants(&self) -> Result<&ConsensusConstants, WalletStorageError> {
// If we don't have the chain metadata, we hope that VNReg consensus constants did not change - worst case, we
// spend more than we need to or the the transaction is rejected.
let height = self
.wallet
.db
.get_chain_metadata()?
.map(|m| m.height_of_longest_chain())
.unwrap_or_default();
Ok(self.rules.consensus_constants(height))
}
}

#[tonic::async_trait]
Expand Down Expand Up @@ -955,10 +979,17 @@ impl wallet_server::Wallet for WalletGrpcServer {
.validator_node_signature
.ok_or_else(|| Status::invalid_argument("Validator node signature is missing!"))?
.try_into()
.unwrap();
.map_err(|_| Status::invalid_argument("Validator node signature is malformed!"))?;

let constants = self.get_consensus_constants().map_err(|e| {
error!(target: LOG_TARGET, "Failed to get consensus constants: {}", e);
Status::internal("failed to fetch consensus constants")
})?;

// TODO: we need to set the output maturity
let response = match transaction_service
.register_validator_node(
constants.validator_node_registration_min_deposit_amount(),
validator_node_public_key,
validator_node_signature,
UtxoSelectionCriteria::default(),
Expand Down
2 changes: 2 additions & 0 deletions base_layer/common_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ edition = "2018"
[dependencies]
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.16.5" }
tari_utilities = { git = "https://github.com/tari-project/tari_utilities.git", tag="v0.4.10" }
# TODO: remove this dependency and move Network into tari_common_types
tari_common = { version = "^0.41", path = "../../common" }

base64 = "0.13.0"
borsh = "0.9.3"
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
34 changes: 14 additions & 20 deletions base_layer/core/src/blocks/genesis_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ pub fn get_esmeralda_genesis_block() -> ChainBlock {
//
// use croaring::Bitmap;
// use std::convert::TryFrom;
// use crate::{KernelMmr, MutableOutputMmr, WitnessMmr};
// use crate::{chain_storage::calculate_validator_node_mr, KernelMmr, MutableOutputMmr, WitnessMmr};

// let mut kernel_mmr = KernelMmr::new(Vec::new());
// for k in block.body.kernels() {
Expand All @@ -247,7 +247,7 @@ pub fn get_esmeralda_genesis_block() -> ChainBlock {
// }
// }

// let vn_mmr = ValidatorNodeMmr::new(Vec::new());
// let vn_mmr = calculate_validator_node_mr(&[]).unwrap();

// block.header.kernel_mr = FixedHash::try_from(kernel_mmr.get_merkle_root().unwrap()).unwrap();
// block.header.witness_mr = FixedHash::try_from(witness_mmr.get_merkle_root().unwrap()).unwrap();
Expand Down Expand Up @@ -363,17 +363,17 @@ 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::{
chain_storage::calculate_validator_node_mr,
consensus::ConsensusManager,
test_helpers::blockchain::create_new_blockchain_with_network,
transactions::{transaction_components::transaction_output::batch_verify_range_proofs, CryptoFactories},
validation::{ChainBalanceValidator, FinalHorizonStateValidation},
KernelMmr,
MutableOutputMmr,
ValidatorNodeMmr,
WitnessMmr,
};

Expand Down Expand Up @@ -428,7 +428,7 @@ mod test {

let mut witness_mmr = WitnessMmr::new(Vec::new());
let mut output_mmr = MutableOutputMmr::new(Vec::new(), Bitmap::create()).unwrap();
let mut vn_mmr = ValidatorNodeMmr::new(Vec::new());
let mut vn_nodes = Vec::new();
for o in block.block().body.outputs() {
witness_mmr.push(o.witness_hash().to_vec()).unwrap();
output_mmr.push(o.hash().to_vec()).unwrap();
Expand All @@ -439,25 +439,19 @@ 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_nodes.push((
reg.public_key().clone(),
reg.derive_shard_key(None, VnEpoch(0), VnEpoch(0), block.hash()),
));
}
}

assert_eq!(kernel_mmr.get_merkle_root().unwrap(), block.header().kernel_mr,);
assert_eq!(witness_mmr.get_merkle_root().unwrap(), block.header().witness_mr,);
assert_eq!(output_mmr.get_merkle_root().unwrap(), block.header().output_mr,);
assert_eq!(
kernel_mmr.get_merkle_root().unwrap().as_slice(),
block.header().kernel_mr.as_slice()
);
assert_eq!(
witness_mmr.get_merkle_root().unwrap().as_slice(),
block.header().witness_mr.as_slice()
);
assert_eq!(
output_mmr.get_merkle_root().unwrap().as_slice(),
block.header().output_mr.as_slice()
);
assert_eq!(
vn_mmr.get_merkle_root().unwrap().as_slice(),
block.header().validator_node_mr.as_slice()
calculate_validator_node_mr(&vn_nodes).unwrap(),
block.header().validator_node_mr,
);

// Check that the faucet UTXOs balance (the faucet_value consensus constant is set correctly and faucet kernel
Expand Down
15 changes: 9 additions & 6 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::{Commitment, 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,
pub commitment: Commitment,
}
4 changes: 4 additions & 0 deletions base_layer/core/src/chain_storage/blockchain_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,12 @@ pub trait BlockchainBackend: Send + Sync {
/// Fetches all tracked reorgs
fn fetch_all_reorgs(&self) -> Result<Vec<Reorg>, ChainStorageError>;

/// Fetches the validator node set for the given height ordered according to height of registration and canonical
/// block body ordering.
fn fetch_active_validator_nodes(&self, height: u64) -> Result<Vec<(PublicKey, [u8; 32])>, ChainStorageError>;
/// Returns the shard key for the validator node if valid at the given height.
fn get_shard_key(&self, height: u64, public_key: PublicKey) -> Result<Option<[u8; 32]>, ChainStorageError>;
/// Returns all template registrations within (inclusive) the given height range.
fn fetch_template_registrations(
&self,
start_height: u64,
Expand Down
Loading

0 comments on commit de54416

Please sign in to comment.