Skip to content

Commit

Permalink
fix: display (#5981)
Browse files Browse the repository at this point in the history
Description
---
Fixes display removing panics

Motivation and Context
---
display should not panic
  • Loading branch information
SWvheerden committed Dec 1, 2023
1 parent 14e334a commit a172b38
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 68 deletions.
3 changes: 1 addition & 2 deletions base_layer/common_types/src/chain_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use std::fmt::{Display, Error, Formatter};

use primitive_types::U256;
use serde::{Deserialize, Serialize};
use tari_utilities::hex::Hex;

use crate::types::{BlockHash, FixedHash};

Expand Down Expand Up @@ -145,7 +144,7 @@ impl ChainMetadata {
impl Display for ChainMetadata {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
let height = self.height_of_longest_chain;
let best_block = self.best_block.to_hex();
let best_block = self.best_block;
let accumulated_difficulty = self.accumulated_difficulty;
writeln!(f, "Height of longest chain: {}", height)?;
writeln!(f, "Total accumulated difficulty: {}", accumulated_difficulty)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ impl Display for NodeCommsRequest {
},
FetchBlocksByKernelExcessSigs(v) => write!(f, "FetchBlocksByKernelExcessSigs (n={})", v.len()),
FetchBlocksByUtxos(v) => write!(f, "FetchBlocksByUtxos (n={})", v.len()),
GetHeaderByHash(v) => write!(f, "GetHeaderByHash({})", v.to_hex()),
GetBlockByHash(v) => write!(f, "GetBlockByHash({})", v.to_hex()),
GetHeaderByHash(v) => write!(f, "GetHeaderByHash({})", v),
GetBlockByHash(v) => write!(f, "GetBlockByHash({})", v),
GetNewBlockTemplate(v) => write!(f, "GetNewBlockTemplate ({}) with weight {}", v.algo, v.max_weight),
GetNewBlock(b) => write!(f, "GetNewBlock (Block Height={})", b.header.height),
GetBlockFromAllChains(v) => write!(f, "GetBlockFromAllChains({})", v.to_hex()),
GetBlockFromAllChains(v) => write!(f, "GetBlockFromAllChains({})", v),
FetchKernelByExcessSig(s) => write!(
f,
"FetchKernelByExcessSig (signature=({}, {}))",
Expand Down
3 changes: 1 addition & 2 deletions base_layer/core/src/blocks/accumulated_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use primitive_types::U256;
use serde::{Deserialize, Serialize};
use tari_common_types::types::{Commitment, HashOutput, PrivateKey};
use tari_mmr::{pruned_hashset::PrunedHashSet, ArrayLike};
use tari_utilities::hex::Hex;

use crate::{
blocks::{error::BlockError, Block, BlockHeader},
Expand Down Expand Up @@ -198,7 +197,7 @@ impl BlockHeaderAccumulatedData {

impl Display for BlockHeaderAccumulatedData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "Hash: {}", self.hash.to_hex())?;
writeln!(f, "Hash: {}", self.hash)?;
writeln!(f, "Achieved difficulty: {}", self.achieved_difficulty)?;
writeln!(f, "Total accumulated difficulty: {}", self.total_accumulated_difficulty)?;
writeln!(
Expand Down
3 changes: 1 addition & 2 deletions base_layer/core/src/blocks/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use borsh::{BorshDeserialize, BorshSerialize};
use log::*;
use serde::{Deserialize, Serialize};
use tari_common_types::types::{FixedHash, PrivateKey};
use tari_utilities::hex::Hex;
use thiserror::Error;

use crate::{
Expand Down Expand Up @@ -150,7 +149,7 @@ impl Display for Block {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
writeln!(f, "----------------- Block -----------------")?;
writeln!(f, "--- Header ---")?;
writeln!(f, "Hash: {}", self.header.hash().to_hex())?;
writeln!(f, "Hash: {}", self.header.hash())?;
writeln!(f, "{}", self.header)?;
writeln!(f, "--- Body ---")?;
writeln!(f, "{}", self.body)
Expand Down
8 changes: 2 additions & 6 deletions base_layer/core/src/blocks/block_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,17 +302,13 @@ impl Display for BlockHeader {
"Version: {}\nBlock height: {}\nPrevious block hash: {}\nTimestamp: {}",
self.version,
self.height,
self.prev_hash.to_hex(),
self.prev_hash,
self.to_chrono_datetime().to_rfc2822()
)?;
writeln!(
fmt,
"Merkle roots:\nInputs: {},\nOutputs: {} ({})\n\nKernels: {} ({})",
self.input_mr.to_hex(),
self.output_mr.to_hex(),
self.output_smt_size,
self.kernel_mr.to_hex(),
self.kernel_mmr_size
self.input_mr, self.output_mr, self.output_smt_size, self.kernel_mr, self.kernel_mmr_size
)?;
writeln!(fmt, "ValidatorNode: {}\n", self.validator_node_mr.to_hex())?;
writeln!(
Expand Down
4 changes: 1 addition & 3 deletions base_layer/core/src/blocks/new_blockheader_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ impl Display for NewBlockHeaderTemplate {
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
let msg = format!(
"Version: {}\nBlock height: {}\nPrevious block hash: {}\n",
self.version,
self.height,
self.prev_hash.to_hex(),
self.version, self.height, self.prev_hash,
);
fmt.write_str(&msg)?;
fmt.write_str(&format!(
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/chain_storage/block_add_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl fmt::Display for BlockAddResult {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
BlockAddResult::Ok(block) => {
write!(f, "Block {} at height {} added", block.hash().to_hex(), block.height())
write!(f, "Block {} at height {} added", block.hash(), block.height())
},
BlockAddResult::BlockExists => write!(f, "Block already exists"),
BlockAddResult::OrphanBlock => write!(f, "Block added as orphan"),
Expand Down
48 changes: 21 additions & 27 deletions base_layer/core/src/chain_storage/db_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,16 +339,16 @@ impl fmt::Display for WriteOperation {
InsertOrphanBlock(block) => write!(
f,
"InsertOrphanBlock({}, {})",
block.hash().to_hex(),
block.hash(),
block.body.to_counts_string()
),
InsertChainHeader { header } => {
write!(f, "InsertChainHeader(#{} {})", header.height(), header.hash().to_hex())
write!(f, "InsertChainHeader(#{} {})", header.height(), header.hash())
},
InsertTipBlockBody { block } => write!(
f,
"InsertTipBlockBody({}, {})",
block.accumulated_data().hash.to_hex(),
block.accumulated_data().hash,
block.block().body.to_counts_string(),
),
InsertKernel {
Expand All @@ -358,8 +358,8 @@ impl fmt::Display for WriteOperation {
} => write!(
f,
"Insert kernel {} in block:{} position: {}",
kernel.hash().to_hex(),
header_hash.to_hex(),
kernel.hash(),
header_hash,
mmr_position
),
InsertOutput {
Expand All @@ -370,27 +370,24 @@ impl fmt::Display for WriteOperation {
} => write!(
f,
"Insert output {} in block({}):{},",
output.hash().to_hex(),
output.hash(),
header_height,
header_hash.to_hex(),
),
DeleteOrphanChainTip(hash) => write!(f, "DeleteOrphanChainTip({})", hash.to_hex()),
InsertOrphanChainTip(hash, total_accumulated_difficulty) => write!(
f,
"InsertOrphanChainTip({}, {})",
hash.to_hex(),
total_accumulated_difficulty
header_hash,
),
DeleteTipBlock(hash) => write!(f, "DeleteTipBlock({})", hash.to_hex()),
DeleteOrphanChainTip(hash) => write!(f, "DeleteOrphanChainTip({})", hash),
InsertOrphanChainTip(hash, total_accumulated_difficulty) => {
write!(f, "InsertOrphanChainTip({}, {})", hash, total_accumulated_difficulty)
},
DeleteTipBlock(hash) => write!(f, "DeleteTipBlock({})", hash),
InsertMoneroSeedHeight(data, height) => {
write!(f, "Insert Monero seed string {} for height: {}", data.to_hex(), height)
},
InsertChainOrphanBlock(block) => write!(f, "InsertChainOrphanBlock({})", block.hash().to_hex()),
InsertChainOrphanBlock(block) => write!(f, "InsertChainOrphanBlock({})", block.hash()),
UpdateBlockAccumulatedData { header_hash, .. } => {
write!(f, "Update Block data for block {}", header_hash.to_hex())
write!(f, "Update Block data for block {}", header_hash)
},
PruneOutputsSpentAtHash { block_hash } => write!(f, "Prune output(s) at hash: {}", block_hash.to_hex()),
DeleteAllInputsInBlock { block_hash } => write!(f, "Delete outputs in block {}", block_hash.to_hex()),
PruneOutputsSpentAtHash { block_hash } => write!(f, "Prune output(s) at hash: {}", block_hash),
DeleteAllInputsInBlock { block_hash } => write!(f, "Delete outputs in block {}", block_hash),
SetAccumulatedDataForOrphan(accumulated_data) => {
write!(f, "Set accumulated data for orphan {}", accumulated_data)
},
Expand All @@ -403,16 +400,13 @@ impl fmt::Display for WriteOperation {
} => write!(
f,
"Update best block to height:{} ({}) with difficulty: {} and timestamp: {}",
height,
hash.to_hex(),
accumulated_difficulty,
timestamp
height, hash, accumulated_difficulty, timestamp
),
SetPruningHorizonConfig(pruning_horizon) => write!(f, "Set config: pruning horizon to {}", pruning_horizon),
SetPrunedHeight { height, .. } => write!(f, "Set pruned height to {}", height),
DeleteHeader(height) => write!(f, "Delete header at height: {}", height),
DeleteOrphan(hash) => write!(f, "Delete orphan with hash: {}", hash.to_hex()),
InsertBadBlock { hash, height } => write!(f, "Insert bad block #{} {}", height, hash.to_hex()),
DeleteOrphan(hash) => write!(f, "Delete orphan with hash: {}", hash),
InsertBadBlock { hash, height } => write!(f, "Insert bad block #{} {}", height, hash),
SetHorizonData { .. } => write!(f, "Set horizon data"),
InsertReorg { .. } => write!(f, "Insert reorg"),
ClearAllReorgs => write!(f, "Clear all reorgs"),
Expand Down Expand Up @@ -466,8 +460,8 @@ impl Display for DbKey {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
match self {
DbKey::HeaderHeight(v) => f.write_str(&format!("Header height (#{})", v)),
DbKey::HeaderHash(v) => f.write_str(&format!("Header hash (#{})", v.to_hex())),
DbKey::OrphanBlock(v) => f.write_str(&format!("Orphan block hash ({})", v.to_hex())),
DbKey::HeaderHash(v) => f.write_str(&format!("Header hash (#{})", v)),
DbKey::OrphanBlock(v) => f.write_str(&format!("Orphan block hash ({})", v)),
}
}
}
2 changes: 1 addition & 1 deletion base_layer/core/src/chain_storage/lmdb_db/lmdb_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2477,7 +2477,7 @@ impl fmt::Display for MetadataValue {
MetadataValue::AccumulatedWork(d) => write!(f, "Total accumulated work is {}", d),
MetadataValue::PruningHorizon(h) => write!(f, "Pruning horizon is {}", h),
MetadataValue::PrunedHeight(height) => write!(f, "Effective pruned height is {}", height),
MetadataValue::BestBlock(hash) => write!(f, "Chain tip block hash is {}", hash.to_hex()),
MetadataValue::BestBlock(hash) => write!(f, "Chain tip block hash is {}", hash),
MetadataValue::HorizonData(_) => write!(f, "Horizon data"),
MetadataValue::BestBlockTimestamp(timestamp) => write!(f, "Chain tip block timestamp is {}", timestamp),
MetadataValue::MigrationVersion(n) => write!(f, "Migration version {}", n),
Expand Down
7 changes: 2 additions & 5 deletions base_layer/core/src/covenants/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ use borsh::{BorshDeserialize, BorshSerialize};
use integer_encoding::VarIntWriter;
use tari_common_types::types::{Commitment, FixedHash, PublicKey};
use tari_script::TariScript;
use tari_utilities::{
hex::{to_hex, Hex},
ByteArray,
};
use tari_utilities::{hex::Hex, ByteArray};

use super::decoder::CovenantDecodeError;
use crate::{
Expand Down Expand Up @@ -237,7 +234,7 @@ impl Display for CovenantArg {
#[allow(clippy::enum_glob_use)]
use CovenantArg::*;
match self {
Hash(hash) => write!(f, "Hash({})", to_hex(&hash[..])),
Hash(hash) => write!(f, "Hash({})", hash),
PublicKey(public_key) => write!(f, "PublicKey({})", public_key.to_hex()),
Commitment(commitment) => write!(f, "Commitment({})", commitment.to_hex()),
TariScript(_) => write!(f, "TariScript(...)"),
Expand Down
12 changes: 7 additions & 5 deletions base_layer/core/src/mempool/service/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ impl Display for MempoolRequest {
MempoolRequest::GetTxStateByExcessSig(sig) => {
write!(f, "GetTxStateByExcessSig ({})", sig.get_signature().to_hex())
},
MempoolRequest::SubmitTransaction(tx) => write!(
f,
"SubmitTransaction ({})",
tx.body.kernels()[0].excess_sig.get_signature().to_hex()
),
MempoolRequest::SubmitTransaction(tx) => {
let sig_hex = tx
.first_kernel_excess_sig()
.map(|sig| sig.get_signature().to_hex())
.unwrap_or_else(|| "No kernels!".to_string());
write!(f, "SubmitTransaction ({})", sig_hex)
},
MempoolRequest::GetFeePerGramStats { count, tip_height } => {
write!(f, "GetFeePerGramStats(count: {}, tip_height: {})", *count, *tip_height)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ impl TransactionInput {
impl Display for TransactionInput {
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
match self.spent_output {
SpentOutput::OutputHash(ref h) => write!(fmt, "Input spending Output hash: {}", h.to_hex()),
SpentOutput::OutputHash(ref h) => write!(fmt, "Input spending Output hash: {}", h),
SpentOutput::OutputData {
ref commitment,
ref script,
Expand All @@ -530,12 +530,12 @@ impl Display for TransactionInput {
fmt,
"({}, {}) [{:?}], Script: ({}), Input_data : ({}), Offset_Pubkey: ({}), Input Hash: {}",
commitment.to_hex(),
self.output_hash().to_hex(),
self.output_hash(),
features,
script,
self.input_data.to_hex(),
sender_offset_public_key.to_hex(),
self.canonical_hash().to_hex(),
self.canonical_hash(),
),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ impl Display for TransactionOutput {
"({}, {}) [{:?}], Script: ({}), Offset Pubkey: ({}), Metadata Signature: ({}, {}, {}, {}, {}), Encrypted \
data ({}), Proof: {}",
self.commitment.to_hex(),
self.hash().to_hex(),
self.hash(),
self.features,
self.script,
self.sender_offset_public_key.to_hex(),
Expand Down
3 changes: 1 addition & 2 deletions base_layer/wallet/src/base_node_service/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use std::{fmt, fmt::Formatter, sync::Arc, time::Duration};

use tari_common_types::{chain_metadata::ChainMetadata, types::BlockHash};
use tari_service_framework::reply_channel::SenderService;
use tari_utilities::hex::Hex;
use tokio::sync::broadcast;
use tower::Service;

Expand Down Expand Up @@ -57,7 +56,7 @@ impl fmt::Display for BaseNodeEvent {
write!(f, "BaseNodeStateChanged: Synced:{:?}", state.is_synced)
},
BaseNodeEvent::NewBlockDetected(hash, height) => {
write!(f, "NewBlockDetected: {} ({})", height, hash.to_hex())
write!(f, "NewBlockDetected: {} ({})", height, hash)
},
}
}
Expand Down
7 changes: 2 additions & 5 deletions base_layer/wallet/src/output_manager_service/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,12 @@ impl fmt::Display for OutputManagerRequest {
CreateClaimShaAtomicSwapTransaction(output, pre_image, fee_per_gram) => write!(
f,
"ClaimShaAtomicSwap(output hash: {}, pre_image: {}, fee_per_gram: {} )",
output.to_hex(),
pre_image,
fee_per_gram,
output, pre_image, fee_per_gram,
),
CreateHtlcRefundTransaction(output, fee_per_gram) => write!(
f,
"CreateHtlcRefundTransaction(output hash: {}, , fee_per_gram: {} )",
output.to_hex(),
fee_per_gram,
output, fee_per_gram,
),

GetOutputStatusesByTxId(t) => write!(f, "GetOutputStatusesByTxId: {}", t),
Expand Down

0 comments on commit a172b38

Please sign in to comment.