Skip to content

Commit

Permalink
fix(core)!: replace block and transaction hashes with FixedHash type (#…
Browse files Browse the repository at this point in the history
…4533)

Description
---
This removes the usage of Vec<u8> for all hashes except the mmr and RPC requests. 
All other internal hashing now uses the `FixedHash` type. 
We now know each hash will always be of the correct length when processing the hash.
This gets rid of a good amount of technical debt. 

How Has This Been Tested?
---
unit tests
  • Loading branch information
SWvheerden authored Aug 25, 2022
1 parent f970f81 commit 501f550
Show file tree
Hide file tree
Showing 121 changed files with 977 additions and 950 deletions.
8 changes: 4 additions & 4 deletions applications/tari_app_grpc/src/conversions/block_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::convert::TryFrom;

use tari_common_types::types::{BlindingFactor, FixedHash};
use tari_core::{blocks::BlockHeader, proof_of_work::ProofOfWork};
use tari_utilities::{ByteArray, Hashable};
use tari_utilities::ByteArray;

use crate::{
conversions::{datetime_to_timestamp, timestamp_to_datetime},
Expand All @@ -35,10 +35,10 @@ impl From<BlockHeader> for grpc::BlockHeader {
fn from(h: BlockHeader) -> Self {
let pow_algo = h.pow_algo();
Self {
hash: h.hash(),
hash: h.hash().to_vec(),
version: u32::from(h.version),
height: h.height,
prev_hash: h.prev_hash,
prev_hash: h.prev_hash.to_vec(),
timestamp: datetime_to_timestamp(h.timestamp),
input_mr: h.input_mr.to_vec(),
output_mr: h.output_mr.to_vec(),
Expand Down Expand Up @@ -79,7 +79,7 @@ impl TryFrom<grpc::BlockHeader> for BlockHeader {
Ok(Self {
version: u16::try_from(header.version).map_err(|_| "header version too large")?,
height: header.height,
prev_hash: header.prev_hash,
prev_hash: FixedHash::try_from(header.prev_hash).map_err(|err| err.to_string())?,
timestamp,
input_mr: FixedHash::try_from(header.input_mr).map_err(|err| err.to_string())?,
output_mr: FixedHash::try_from(header.output_mr).map_err(|err| err.to_string())?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl From<ChainMetadata> for grpc::MetaData {
let diff = meta.accumulated_difficulty();
Self {
height_of_longest_chain: meta.height_of_longest_chain(),
best_block: meta.best_block().clone(),
best_block: meta.best_block().to_vec(),
pruned_height: meta.pruned_height(),
accumulated_difficulty: diff.to_be_bytes().to_vec(),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

use std::convert::{TryFrom, TryInto};

use tari_common_types::types::BlindingFactor;
use tari_common_types::types::{BlindingFactor, FixedHash};
use tari_core::{
blocks::{NewBlockHeaderTemplate, NewBlockTemplate},
proof_of_work::ProofOfWork,
Expand All @@ -38,7 +38,7 @@ impl TryFrom<NewBlockTemplate> for grpc::NewBlockTemplate {
let header = grpc::NewBlockHeaderTemplate {
version: u32::from(block.header.version),
height: block.header.height,
prev_hash: block.header.prev_hash.clone(),
prev_hash: block.header.prev_hash.to_vec(),
total_kernel_offset: Vec::from(block.header.total_kernel_offset.as_bytes()),
total_script_offset: Vec::from(block.header.total_script_offset.as_bytes()),
pow: Some(grpc::ProofOfWork {
Expand Down Expand Up @@ -87,7 +87,7 @@ impl TryFrom<grpc::NewBlockTemplate> for NewBlockTemplate {
let header = NewBlockHeaderTemplate {
version: u16::try_from(header.version).map_err(|_| "header version too large")?,
height: header.height,
prev_hash: header.prev_hash,
prev_hash: FixedHash::try_from(header.prev_hash).map_err(|err| err.to_string())?,
total_kernel_offset,
total_script_offset,
pow,
Expand Down
11 changes: 8 additions & 3 deletions applications/tari_app_grpc/src/conversions/transaction_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ impl TryFrom<grpc::TransactionInput> for TransactionInput {
if input.output_hash.is_empty() {
return Err("Compact Transaction Input does not contain `output_hash`".to_string());
}
let input_hash = input
.output_hash
.try_into()
.map_err(|_| "Malformed input hash".to_string())?;
Ok(TransactionInput::new_with_output_hash(
input.output_hash,
input_hash,
ExecutionStack::from_bytes(input.input_data.as_slice()).map_err(|err| format!("{:?}", err))?,
script_signature,
))
Expand Down Expand Up @@ -93,7 +97,7 @@ impl TryFrom<TransactionInput> for grpc::TransactionInput {
signature_v: Vec::from(input.script_signature.v().as_bytes()),
});
if input.is_compact() {
let output_hash = input.output_hash();
let output_hash = input.output_hash().to_vec();
Ok(Self {
script_signature,
output_hash,
Expand All @@ -112,7 +116,8 @@ impl TryFrom<TransactionInput> for grpc::TransactionInput {
.to_vec(),
hash: input
.canonical_hash()
.map_err(|_| "Non-compact Transaction input should be able to be hashed".to_string())?,
.map_err(|_| "Non-compact Transaction input should be able to be hashed".to_string())?
.to_vec(),

script: input
.script()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use tari_core::transactions::{
tari_amount::MicroTari,
transaction_components::{KernelFeatures, TransactionKernel, TransactionKernelVersion},
};
use tari_utilities::{ByteArray, Hashable};
use tari_utilities::ByteArray;

use crate::tari_rpc as grpc;

Expand Down Expand Up @@ -71,7 +71,7 @@ impl TryFrom<grpc::TransactionKernel> for TransactionKernel {

impl From<TransactionKernel> for grpc::TransactionKernel {
fn from(kernel: TransactionKernel) -> Self {
let hash = kernel.hash();
let hash = kernel.hash().to_vec();
let commitment = match kernel.burn_commitment {
Some(c) => c.as_bytes().to_vec(),
None => vec![],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use tari_core::{
},
};
use tari_script::TariScript;
use tari_utilities::{ByteArray, Hashable};
use tari_utilities::ByteArray;

use crate::tari_rpc as grpc;

Expand Down Expand Up @@ -79,7 +79,7 @@ impl TryFrom<grpc::TransactionOutput> for TransactionOutput {

impl From<TransactionOutput> for grpc::TransactionOutput {
fn from(output: TransactionOutput) -> Self {
let hash = output.hash();
let hash = output.hash().to_vec();
grpc::TransactionOutput {
hash,
features: Some(output.features.into()),
Expand Down
12 changes: 7 additions & 5 deletions applications/tari_base_node/src/commands/command/get_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
// 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::TryInto;

use anyhow::Error;
use async_trait::async_trait;
use clap::Parser;
Expand Down Expand Up @@ -48,7 +50,10 @@ impl HandleCommand<Args> for CommandContext {
let format = args.format;
match args.value {
TypeOrHex::Type(value) => self.get_block(value, format).await,
TypeOrHex::Hex(hex) => self.get_block_by_hash(hex.0, format).await,
TypeOrHex::Hex(hex) => {
let hash = hex.0.try_into()?;
self.get_block_by_hash(hash, format).await
},
}
}
}
Expand All @@ -71,10 +76,7 @@ impl CommandContext {
.ok_or(ArgsError::NotFoundAt { height })?;
match format {
Format::Text => {
let block_data = self
.blockchain_db
.fetch_block_accumulated_data(block.hash().clone())
.await?;
let block_data = self.blockchain_db.fetch_block_accumulated_data(*block.hash()).await?;

println!("{}", block);
println!("-- Accumulated data --");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use async_trait::async_trait;
use chrono::{NaiveDateTime, Utc};
use clap::Parser;
use tari_core::proof_of_work::PowAlgorithm;
use tari_utilities::{hex::Hex, Hashable};
use tari_utilities::hex::Hex;
use tokio::{
fs::File,
io::{self, AsyncWriteExt},
Expand Down Expand Up @@ -100,7 +100,7 @@ impl CommandContext {

let target_diff = self
.blockchain_db
.fetch_target_difficulties_for_next_block(prev_header.hash().clone())
.fetch_target_difficulties_for_next_block(*prev_header.hash())
.await?;
let pow_algo = header.header().pow_algo();

Expand Down
17 changes: 11 additions & 6 deletions applications/tari_base_node/src/grpc/base_node_grpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use tari_core::{
transactions::{aggregated_body::AggregateBody, transaction_components::Transaction},
};
use tari_p2p::{auto_update::SoftwareUpdaterHandle, services::liveness::LivenessHandle};
use tari_utilities::{hex::Hex, message_format::MessageFormat, ByteArray, Hashable};
use tari_utilities::{hex::Hex, message_format::MessageFormat, ByteArray};
use tokio::task;
use tonic::{Request, Response, Status};

Expand Down Expand Up @@ -541,7 +541,7 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
Err(e) => return Err(report_error(report_error_flag, Status::internal(e.to_string()))),
};
// construct response
let block_hash = new_block.hash();
let block_hash = new_block.hash().to_vec();
let mining_hash = new_block.header.mining_hash().to_vec();
let block: Option<tari_rpc::Block> = Some(
new_block
Expand Down Expand Up @@ -586,7 +586,7 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
Err(e) => return Err(Status::internal(e.to_string())),
};
// construct response
let block_hash = new_block.hash();
let block_hash = new_block.hash().to_vec();
let mining_hash = new_block.header.mining_hash().to_vec();

let (header, block_body) = new_block.into_header_body();
Expand Down Expand Up @@ -628,7 +628,8 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
let block_hash = handler
.submit_block(block)
.await
.map_err(|e| report_error(report_error_flag, Status::internal(e.to_string())))?;
.map_err(|e| report_error(report_error_flag, Status::internal(e.to_string())))?
.to_vec();

debug!(
target: LOG_TARGET,
Expand Down Expand Up @@ -664,7 +665,8 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
let block_hash = handler
.submit_block(block)
.await
.map_err(|e| Status::internal(e.to_string()))?;
.map_err(|e| Status::internal(e.to_string()))?
.to_vec();

debug!(
target: LOG_TARGET,
Expand Down Expand Up @@ -1414,8 +1416,11 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
let tari_rpc::GetHeaderByHashRequest { hash } = request.into_inner();
let mut node_service = self.node_service.clone();
let hash_hex = hash.to_hex();
let block_hash = hash
.try_into()
.map_err(|_| report_error(report_error_flag, Status::internal("Malformed block hash".to_string())))?;
let block = node_service
.get_block_by_hash(hash)
.get_block_by_hash(block_hash)
.await
.map_err(|err| report_error(report_error_flag, Status::internal(err.to_string())))?;

Expand Down
15 changes: 9 additions & 6 deletions applications/tari_console_wallet/src/automation/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::{
convert::TryInto,
fs,
fs::File,
io,
Expand All @@ -39,7 +40,7 @@ use strum_macros::{Display, EnumIter, EnumString};
use tari_common_types::{
emoji::EmojiId,
transaction::TxId,
types::{CommitmentFactory, PublicKey},
types::{CommitmentFactory, FixedHash, PublicKey},
};
use tari_comms::{
connectivity::{ConnectivityEvent, ConnectivityRequester},
Expand All @@ -51,7 +52,7 @@ use tari_core::transactions::{
tari_amount::{uT, MicroTari, Tari},
transaction_components::{OutputFeatures, TransactionOutput, UnblindedOutput},
};
use tari_utilities::{hex::Hex, ByteArray, Hashable};
use tari_utilities::{hex::Hex, ByteArray};
use tari_wallet::{
connectivity_service::WalletConnectivityInterface,
error::WalletError,
Expand Down Expand Up @@ -143,7 +144,7 @@ pub async fn init_sha_atomic_swap(
pub async fn finalise_sha_atomic_swap(
mut output_service: OutputManagerHandle,
mut transaction_service: TransactionServiceHandle,
output_hash: Vec<u8>,
output_hash: FixedHash,
pre_image: PublicKey,
fee_per_gram: MicroTari,
message: String,
Expand All @@ -161,7 +162,7 @@ pub async fn finalise_sha_atomic_swap(
pub async fn claim_htlc_refund(
mut output_service: OutputManagerHandle,
mut transaction_service: TransactionServiceHandle,
output_hash: Vec<u8>,
output_hash: FixedHash,
fee_per_gram: MicroTari,
message: String,
) -> Result<TxId, CommandError> {
Expand Down Expand Up @@ -738,10 +739,11 @@ pub async fn command_runner(
tx_ids.push(tx_id);
},
FinaliseShaAtomicSwap(args) => {
let hash = args.output_hash[0].clone().try_into()?;
let tx_id = finalise_sha_atomic_swap(
output_service.clone(),
transaction_service.clone(),
args.output_hash[0].clone(),
hash,
args.pre_image.into(),
config.fee_per_gram.into(),
args.message,
Expand All @@ -751,10 +753,11 @@ pub async fn command_runner(
tx_ids.push(tx_id);
},
ClaimShaAtomicSwapRefund(args) => {
let hash = args.output_hash[0].clone().try_into()?;
let tx_id = claim_htlc_refund(
output_service.clone(),
transaction_service.clone(),
args.output_hash[0].clone(),
hash,
config.fee_per_gram.into(),
args.message,
)
Expand Down
3 changes: 3 additions & 0 deletions applications/tari_console_wallet/src/automation/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use std::{

use log::*;
use tari_common::exit_codes::{ExitCode, ExitError};
use tari_common_types::types::FixedHashSizeError;
use tari_core::transactions::{tari_amount::MicroTariError, transaction_components::TransactionError};
use tari_utilities::hex::HexError;
use tari_wallet::{
Expand Down Expand Up @@ -79,6 +80,8 @@ pub enum CommandError {
IoError(#[from] io::Error),
#[error("General error: {0}")]
General(String),
#[error("FixedHash size error `{0}`")]
FixedHashSizeError(#[from] FixedHashSizeError),
}

impl From<CommandError> for ExitError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ use tari_core::transactions::{
tari_amount::MicroTari,
transaction_components::{OutputFeatures, UnblindedOutput},
};
use tari_utilities::{hex::Hex, ByteArray, Hashable};
use tari_utilities::{hex::Hex, ByteArray};
use tari_wallet::{
connectivity_service::{OnlineStatus, WalletConnectivityInterface},
output_manager_service::handle::OutputManagerHandle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use futures::stream::StreamExt;
use log::*;
use tari_app_grpc::tari_rpc::BlockHeader;
use tari_core::consensus::ConsensusDecoding;
use tari_utilities::{hex::Hex, Hashable};
use tari_utilities::hex::Hex;

use crate::{
display_report,
Expand Down
43 changes: 0 additions & 43 deletions base_layer/common_types/src/array.rs

This file was deleted.

Loading

0 comments on commit 501f550

Please sign in to comment.