Skip to content

Commit

Permalink
feat: trickle validators (#6660)
Browse files Browse the repository at this point in the history
Description
---
We allow here only a configured (in Consensus constants) number of
validator nodes to be active in an epoch, so this way we avoid having
way too much validators appear at the same time.
All the validator nodes are saved to the blockchain database, when there
is a new validator node registration UTXO, but with a different `start
epoch` that is calculated from the already saved validator nodes.
There is a new gRPC method called `GetValidatorNodeChanges`, where we
can ask base node to give back all the validator node additions and
deletions in the given start/end height range.
When Layer-2 scans new blocks and sees that there is a change in the
validator node set, simply calls this new grpc method and adds/removes
validator nodes. This way base node controls fully how many nodes are
registered at which epoch.

Motivation and Context
---
Layer-2 validator nodes are registered all at once when they were added
to the chain with a special UTXO, but it allows to register too many
nodes at the same time, so consensus can catch up much slower.

How Has This Been Tested?
---

What process can a PR reviewer use to test or verify this change?
---

<!-- Checklist -->
<!-- 1. Is the title of your PR in the form that would make nice release
notes? The title, excluding the conventional commit
tag, will be included exactly as is in the CHANGELOG, so please think
about it carefully. -->


Breaking Changes
---

- [x] None
- [ ] Requires data directory on base node to be deleted
- [ ] Requires hard fork
- [ ] Other - Please specify

<!-- Does this include a breaking change? If so, include this line as a
footer -->
<!-- BREAKING CHANGE: Description what the user should do, e.g. delete a
database, resync the chain -->

---------

Co-authored-by: Stan Bondi <[email protected]>
  • Loading branch information
ksrichard and sdbondi authored Nov 11, 2024
1 parent 9012114 commit 9005c11
Show file tree
Hide file tree
Showing 27 changed files with 754 additions and 385 deletions.
574 changes: 299 additions & 275 deletions applications/minotari_app_grpc/proto/base_node.proto

Large diffs are not rendered by default.

131 changes: 66 additions & 65 deletions applications/minotari_app_grpc/proto/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,122 +25,123 @@ package tari.rpc;

/// An unsigned range interface to more accurately represent Rust native Range's
message Range {
uint64 min = 1;
uint64 max = 2;
uint64 min = 1;
uint64 max = 2;
}

/// An Empty placeholder for endpoints without request parameters
message Empty {}

/// Define an interface for block height
message BlockHeight {
uint64 block_height = 1;
uint64 block_height = 1;
}

// Define the explicit Signature implementation for the Minotari base layer. A different signature scheme can be
// employed by redefining this type.
message Signature {
bytes public_nonce = 1;
bytes signature = 2;
bytes public_nonce = 1;
bytes signature = 2;
}

// Define the explicit ComAndPubSignature implementation for the Minotari base layer. A different signature scheme can be
// employed by redefining this type.
message ComAndPubSignature {
bytes ephemeral_commitment = 1;
bytes ephemeral_pubkey = 2;
bytes u_a = 3;
bytes u_x = 4;
bytes u_y = 5;
bytes ephemeral_commitment = 1;
bytes ephemeral_pubkey = 2;
bytes u_a = 3;
bytes u_x = 4;
bytes u_y = 5;
}

// Define the explicit CommitmentSignature implementation for the Minotari base layer. A different signature scheme can be
// employed by redefining this type
message CommitmentSignature {
bytes public_nonce = 1;
bytes u = 2;
bytes v = 3;
bytes public_nonce = 1;
bytes u = 2;
bytes v = 3;
}

/// PoW Algorithm constants
message PowAlgorithmConstants {
uint64 min_difficulty = 2;
uint64 max_difficulty = 3;
uint64 target_time = 4;
uint64 min_difficulty = 2;
uint64 max_difficulty = 3;
uint64 target_time = 4;
}

/// Weight params
message WeightParams {
uint64 kernel_weight = 1;
uint64 input_weight = 2;
uint64 output_weight = 3;
uint64 features_and_scripts_bytes_per_gram = 4;
uint64 kernel_weight = 1;
uint64 input_weight = 2;
uint64 output_weight = 3;
uint64 features_and_scripts_bytes_per_gram = 4;
}

/// Output version
message OutputsVersion {
Range outputs = 1;
Range features = 2;
Range outputs = 1;
Range features = 2;
}

/// Output types
enum OutputType {
STANDARD = 0;
COINBASE = 1;
BURN = 2;
VALIDATOR_NODE_REGISTRATION = 3;
CODE_TEMPLATE_REGISTRATION = 4;
STANDARD = 0;
COINBASE = 1;
BURN = 2;
VALIDATOR_NODE_REGISTRATION = 3;
CODE_TEMPLATE_REGISTRATION = 4;
}

/// Range proof types
enum RangeProofType {
BULLETPROOF_PLUS = 0;
REVEALED_VALUE = 1;
BULLETPROOF_PLUS = 0;
REVEALED_VALUE = 1;
}

message PermittedRangeProofs {
OutputType output_type = 1;
repeated RangeProofType range_proof_types = 2;
OutputType output_type = 1;
repeated RangeProofType range_proof_types = 2;
}

/// Range proof
message RangeProof {
bytes proof_bytes = 1;
bytes proof_bytes = 1;
}

/// Consensus Constants response
message ConsensusConstants {
uint64 coinbase_min_maturity = 1;
uint32 blockchain_version = 2;
uint64 future_time_limit = 3;
uint64 difficulty_block_window = 5;
uint64 max_block_transaction_weight = 7;
uint64 pow_algo_count = 8;
uint64 median_timestamp_count = 9;
uint64 emission_initial = 10;
repeated uint64 emission_decay = 11;
uint64 emission_tail = 12 [deprecated=true];
uint64 min_sha3x_pow_difficulty = 13;
uint64 block_weight_inputs = 14;
uint64 block_weight_outputs = 15;
uint64 block_weight_kernels = 16;
uint64 pre_mine_value = 17;
uint64 max_script_byte_size = 18;
uint64 validator_node_validity_period = 19;
uint64 effective_from_height = 20;
Range valid_blockchain_version_range = 21;
uint64 max_randomx_seed_height = 22;
map<uint32, PowAlgorithmConstants> proof_of_work = 23;
WeightParams transaction_weight = 24;
Range input_version_range = 26;
OutputsVersion output_version_range = 27;
Range kernel_version_range = 28;
repeated OutputType permitted_output_types = 29;
uint64 epoch_length = 30;
uint64 validator_node_registration_min_deposit_amount = 31;
uint64 validator_node_registration_min_lock_height = 32;
uint64 validator_node_registration_shuffle_interval_epoch = 33;
repeated PermittedRangeProofs permitted_range_proof_types = 34;
uint64 inflation_bips = 35;
uint64 tail_epoch_length = 36;
uint64 coinbase_min_maturity = 1;
uint32 blockchain_version = 2;
uint64 future_time_limit = 3;
uint64 difficulty_block_window = 5;
uint64 max_block_transaction_weight = 7;
uint64 pow_algo_count = 8;
uint64 median_timestamp_count = 9;
uint64 emission_initial = 10;
repeated uint64 emission_decay = 11;
uint64 emission_tail = 12 [deprecated = true];
uint64 min_sha3x_pow_difficulty = 13;
uint64 block_weight_inputs = 14;
uint64 block_weight_outputs = 15;
uint64 block_weight_kernels = 16;
uint64 pre_mine_value = 17;
uint64 max_script_byte_size = 18;
uint64 vn_registration_max_vns_initial_epoch = 19;
uint64 vn_registration_max_vns_per_epoch = 20;
uint64 effective_from_height = 21;
Range valid_blockchain_version_range = 22;
uint64 max_randomx_seed_height = 23;
map<uint32, PowAlgorithmConstants> proof_of_work = 24;
WeightParams transaction_weight = 25;
Range input_version_range = 26;
OutputsVersion output_version_range = 27;
Range kernel_version_range = 28;
repeated OutputType permitted_output_types = 29;
uint64 epoch_length = 30;
uint64 validator_node_registration_min_deposit_amount = 31;
uint64 validator_node_registration_min_lock_height = 32;
uint64 validator_node_registration_shuffle_interval_epoch = 33;
repeated PermittedRangeProofs permitted_range_proof_types = 34;
uint64 inflation_bips = 35;
uint64 tail_epoch_length = 36;
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ impl From<ConsensusConstants> for grpc::ConsensusConstants {
output_version_range: Some(output_version_range),
permitted_output_types,
permitted_range_proof_types,
validator_node_validity_period: cc.validator_node_validity_period_epochs().as_u64(),
epoch_length: cc.epoch_length(),
validator_node_registration_min_deposit_amount: cc
.validator_node_registration_min_deposit_amount()
Expand All @@ -139,6 +138,8 @@ impl From<ConsensusConstants> for grpc::ConsensusConstants {
validator_node_registration_shuffle_interval_epoch: cc
.validator_node_registration_shuffle_interval()
.as_u64(),
vn_registration_max_vns_initial_epoch: cc.vn_registration_max_vns_initial_epoch(),
vn_registration_max_vns_per_epoch: cc.vn_registration_max_vns_per_epoch(),
}
}
}
2 changes: 2 additions & 0 deletions applications/minotari_app_grpc/src/conversions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub mod transaction_input;
pub mod transaction_kernel;
pub mod transaction_output;
pub mod unblinded_output;
pub mod validator_node_change;
pub mod validator_node_registration;

use prost_types::Timestamp;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2024 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

use tari_core::base_node::comms_interface::{ValidatorNodeChange, ValidatorNodeChangeState};
use tari_utilities::ByteArray;

impl From<&ValidatorNodeChange> for crate::tari_rpc::ValidatorNodeChange {
fn from(node_change: &ValidatorNodeChange) -> Self {
Self {
public_key: node_change.public_key.to_vec(),
state: match node_change.state {
ValidatorNodeChangeState::ADD => crate::tari_rpc::ValidatorNodeChangeState::Add.into(),
ValidatorNodeChangeState::REMOVE => crate::tari_rpc::ValidatorNodeChangeState::Remove.into(),
},
start_height: node_change.height,
registration: Some((&node_change.registration).into()),
minimum_value_promise: node_change.minimum_value_promise.into(),
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2024 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

use tari_core::transactions::transaction_components::ValidatorNodeRegistration;
use tari_utilities::ByteArray;

impl From<&ValidatorNodeRegistration> for crate::tari_rpc::ValidatorNodeRegistration {
fn from(registration: &ValidatorNodeRegistration) -> Self {
Self {
public_key: registration.public_key().to_vec(),
signature: Some(crate::tari_rpc::Signature {
public_nonce: registration.signature().get_public_nonce().to_vec(),
signature: registration.signature().get_signature().to_vec(),
}),
claim_public_key: registration.claim_public_key().to_vec(),
sidechain_id: match registration.sidechain_id() {
None => vec![],
Some(id) => id.to_vec(),
},
sidechain_id_knowledge_proof: registration.sidechain_id_knowledge_proof().map(|signature| {
crate::tari_rpc::Signature {
public_nonce: signature.get_public_nonce().to_vec(),
signature: signature.get_signature().to_vec(),
}
}),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ impl CommandContext {
public_key,
sidechain_id: validator_network,
shard_key,
..
} in vns
{
table.add_row(row![
Expand Down
37 changes: 36 additions & 1 deletion applications/minotari_node/src/grpc/base_node_grpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use futures::{channel::mpsc, SinkExt};
use log::*;
use minotari_app_grpc::{
tari_rpc,
tari_rpc::{CalcType, Sorting},
tari_rpc::{CalcType, GetValidatorNodeChangesRequest, GetValidatorNodeChangesResponse, Sorting},
};
use tari_common_types::{
key_branches::TransactionKeyManagerBranch,
Expand Down Expand Up @@ -159,6 +159,7 @@ impl BaseNodeGrpcServer {
GrpcMethod::GetMempoolStats,
GrpcMethod::GetTipInfo,
GrpcMethod::GetActiveValidatorNodes,
GrpcMethod::GetValidatorNodeChanges,
GrpcMethod::GetShardKey,
GrpcMethod::GetTemplateRegistrations,
GrpcMethod::GetHeaderByHash,
Expand Down Expand Up @@ -2315,6 +2316,7 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
public_key,
sidechain_id,
shard_key,
..
} in active_validator_nodes
{
let active_validator_node = tari_rpc::GetActiveValidatorNodesResponse {
Expand Down Expand Up @@ -2550,6 +2552,39 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
);
Ok(Response::new(rx))
}

async fn get_validator_node_changes(
&self,
request: Request<GetValidatorNodeChangesRequest>,
) -> Result<Response<GetValidatorNodeChangesResponse>, Status> {
self.check_method_enabled(GrpcMethod::GetValidatorNodeChanges)?;
let request = request.into_inner();
trace!(target: LOG_TARGET, "Incoming GRPC request for GetValidatorNodeChanges");

let mut handler = self.node_service.clone();

let sidechain_id = if request.sidechain_id.is_empty() {
None
} else {
Some(
PublicKey::from_canonical_bytes(&request.sidechain_id)
.map_err(|e| Status::invalid_argument(format!("Invalid sidechain_id '{}'", e)))?,
)
};

let changes = handler
.get_validator_node_changes(request.start_height, request.end_height, sidechain_id)
.await
.map_err(|error| {
warn!(target: LOG_TARGET, "Base node service error: {}", error);
Status::internal("Internal error!")
})?
.iter()
.map(|node_change| node_change.into())
.collect();

Ok(Response::new(GetValidatorNodeChangesResponse { changes }))
}
}

enum BlockGroupType {
Expand Down
8 changes: 6 additions & 2 deletions applications/minotari_node/src/grpc_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ pub enum GrpcMethod {
ListConnectedPeers,
GetMempoolStats,
GetActiveValidatorNodes,
GetValidatorNodeChanges,
GetShardKey,
GetTemplateRegistrations,
GetSideChainUtxos,
}

impl GrpcMethod {
/// All the GRPC methods as a fixed array
pub const ALL_VARIANTS: [GrpcMethod; 36] = [
pub const ALL_VARIANTS: [GrpcMethod; 37] = [
GrpcMethod::ListHeaders,
GrpcMethod::GetHeaderByHash,
GrpcMethod::GetBlocks,
Expand Down Expand Up @@ -103,14 +104,15 @@ impl GrpcMethod {
GrpcMethod::ListConnectedPeers,
GrpcMethod::GetMempoolStats,
GrpcMethod::GetActiveValidatorNodes,
GrpcMethod::GetValidatorNodeChanges,
GrpcMethod::GetShardKey,
GrpcMethod::GetTemplateRegistrations,
GrpcMethod::GetSideChainUtxos,
];
}

impl IntoIterator for GrpcMethod {
type IntoIter = std::array::IntoIter<GrpcMethod, 36>;
type IntoIter = std::array::IntoIter<GrpcMethod, 37>;
type Item = GrpcMethod;

fn into_iter(self) -> Self::IntoIter {
Expand Down Expand Up @@ -158,6 +160,7 @@ impl FromStr for GrpcMethod {
"list_connected_peers" => Ok(GrpcMethod::ListConnectedPeers),
"get_mempool_stats" => Ok(GrpcMethod::GetMempoolStats),
"get_active_validator_nodes" => Ok(GrpcMethod::GetActiveValidatorNodes),
"get_validator_node_changes" => Ok(GrpcMethod::GetValidatorNodeChanges),
"get_shard_key" => Ok(GrpcMethod::GetShardKey),
"get_template_registrations" => Ok(GrpcMethod::GetTemplateRegistrations),
"get_side_chain_utxos" => Ok(GrpcMethod::GetSideChainUtxos),
Expand Down Expand Up @@ -253,6 +256,7 @@ mod tests {
GrpcMethod::ListConnectedPeers => count += 1,
GrpcMethod::GetMempoolStats => count += 1,
GrpcMethod::GetActiveValidatorNodes => count += 1,
GrpcMethod::GetValidatorNodeChanges => count += 1,
GrpcMethod::GetShardKey => count += 1,
GrpcMethod::GetTemplateRegistrations => count += 1,
GrpcMethod::GetSideChainUtxos => count += 1,
Expand Down
Loading

0 comments on commit 9005c11

Please sign in to comment.