Skip to content

Commit

Permalink
feat: add missing fields to grpc consensus constants interface (#4845)
Browse files Browse the repository at this point in the history
* add remaining logic to build on ConsensusConstants grpc interface

* address some PR comments

* correct grammar error

* cargo clippy

* further refactoring

* add constructor to TransactionOutput type

* cargo fmt
  • Loading branch information
jorgeantonio21 authored Oct 25, 2022
1 parent ce35b65 commit ce6c22f
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 3 deletions.
60 changes: 60 additions & 0 deletions applications/tari_app_grpc/proto/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ syntax = "proto3";

package tari.rpc;

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

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

Expand All @@ -41,6 +47,36 @@ message ComSignature {
bytes signature_v = 3;
}

/// PoW Algorithm constants
message PowAlgorithmConstants {
uint64 max_target_time = 1;
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 metadata_bytes_per_gram = 4;
}

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

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

/// Consensus Constants response
message ConsensusConstants {
Expand Down Expand Up @@ -76,4 +112,28 @@ message ConsensusConstants {
uint64 block_weight_outputs = 15;
/// Block weight for kernels
uint64 block_weight_kernels = 16;
/// This is to keep track of the value inside of the genesis block
uint64 faucet_value = 17;
/// 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;
/// The height at which these constants become effective
uint64 effective_from_height = 20;
/// Current version of the blockchain
Range valid_blockchain_version_range = 21;
/// This is the maximum age a monero merge mined seed can be reused
uint64 max_randomx_seed_height = 22;
/// This keeps track of the block split targets and which algo is accepted
map<uint32, PowAlgorithmConstants> proof_of_work = 23;
/// Transaction Weight params
WeightParams transaction_weight = 24;
/// Range of valid transaction input versions
Range input_version_range = 26;
/// Range of valid transaction output (and features) versions
OutputsVersion output_version_range = 27;
/// Range of valid transaction kernel versions
Range kernel_version_range = 28;
/// An allowlist of output types
repeated OutputType permitted_output_types = 29;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// 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 std::convert::TryFrom;
use std::{collections::HashMap, convert::TryFrom, iter::FromIterator};

use tari_core::{consensus::ConsensusConstants, proof_of_work::PowAlgorithm};

Expand All @@ -30,6 +30,73 @@ impl From<ConsensusConstants> for grpc::ConsensusConstants {
fn from(cc: ConsensusConstants) -> Self {
let (emission_initial, emission_decay, emission_tail) = cc.emission_amounts();
let weight_params = cc.transaction_weight().params();
let input_version_range = cc.input_version_range().clone().into_inner();
let input_version_range = grpc::Range {
min: u64::from(input_version_range.0.as_u8()),
max: u64::from(input_version_range.1.as_u8()),
};
let kernel_version_range = cc.kernel_version_range().clone().into_inner();
let kernel_version_range = grpc::Range {
min: u64::from(kernel_version_range.0.as_u8()),
max: u64::from(kernel_version_range.1.as_u8()),
};
let valid_blockchain_version_range = cc.valid_blockchain_version_range().clone().into_inner();
let valid_blockchain_version_range = grpc::Range {
min: u64::from(valid_blockchain_version_range.0),
max: u64::from(valid_blockchain_version_range.1),
};
let transaction_weight = cc.transaction_weight();
let metadata_bytes_per_gram = if let Some(val) = transaction_weight.params().metadata_bytes_per_gram {
u64::from(val)
} else {
0u64
};
let transaction_weight = grpc::WeightParams {
kernel_weight: cc.transaction_weight().params().kernel_weight,
input_weight: cc.transaction_weight().params().input_weight,
output_weight: cc.transaction_weight().params().output_weight,
metadata_bytes_per_gram,
};
let output_version_range = cc.output_version_range();
let outputs = grpc::Range {
min: u64::from(output_version_range.outputs.start().as_u8()),
max: u64::from(output_version_range.outputs.end().as_u8()),
};
let features = grpc::Range {
min: u64::from(output_version_range.features.start().as_u8()),
max: u64::from(output_version_range.features.end().as_u8()),
};

let output_version_range = grpc::OutputsVersion {
outputs: Some(outputs),
features: Some(features),
};

let permitted_output_types = cc.permitted_output_types();
let permitted_output_types = permitted_output_types
.iter()
.map(|ot| i32::from(ot.as_byte()))
.collect::<Vec<i32>>();

let monero_pow = PowAlgorithm::Monero;
let sha3_pow = PowAlgorithm::Sha3;

let monero_pow = grpc::PowAlgorithmConstants {
max_target_time: cc.get_difficulty_max_block_interval(monero_pow),
max_difficulty: cc.max_pow_difficulty(monero_pow).as_u64(),
min_difficulty: cc.min_pow_difficulty(monero_pow).as_u64(),
target_time: cc.get_diff_target_block_interval(monero_pow),
};

let sha3_pow = grpc::PowAlgorithmConstants {
max_target_time: cc.get_difficulty_max_block_interval(sha3_pow),
max_difficulty: cc.max_pow_difficulty(sha3_pow).as_u64(),
min_difficulty: cc.min_pow_difficulty(sha3_pow).as_u64(),
target_time: cc.get_diff_target_block_interval(sha3_pow),
};

let proof_of_work = HashMap::from_iter([(0u32, monero_pow), (1u32, sha3_pow)]);

Self {
coinbase_lock_height: cc.coinbase_lock_height(),
blockchain_version: cc.blockchain_version().into(),
Expand All @@ -46,6 +113,18 @@ 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(),
input_version_range: Some(input_version_range),
kernel_version_range: Some(kernel_version_range),
valid_blockchain_version_range: Some(valid_blockchain_version_range),
proof_of_work,
transaction_weight: Some(transaction_weight),
max_randomx_seed_height: cc.max_randomx_seed_height(),
output_version_range: Some(output_version_range),
permitted_output_types,
}
}
}
5 changes: 3 additions & 2 deletions applications/tari_base_node/src/grpc/base_node_grpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1675,9 +1675,10 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
let sidechain_outputs = utxos
.into_iter()
.filter(|u| u.features.output_type.is_sidechain_type())
.map(TryInto::try_into);
.map(TryInto::try_into)
.collect::<Result<Vec<_>, _>>();

match sidechain_outputs.collect() {
match sidechain_outputs {
Ok(outputs) => {
let resp = tari_rpc::GetSideChainUtxosResponse {
block_info: Some(tari_rpc::BlockInfo {
Expand Down
5 changes: 5 additions & 0 deletions base_layer/core/src/transactions/weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ impl WeightParams {
pub struct TransactionWeight(WeightParams);

impl TransactionWeight {
/// Constructor
pub fn new(weight_params: WeightParams) -> Self {
Self(weight_params)
}

/// Creates a new `TransactionWeight` with latest weight params
pub fn latest() -> Self {
Self(WeightParams::v1())
Expand Down

0 comments on commit ce6c22f

Please sign in to comment.