Skip to content

Commit

Permalink
Merge pull request #1536 from mintlayer/fix/0x-prefix-in-id
Browse files Browse the repository at this point in the history
Remove 0x prefix in Display and Debug for Id
  • Loading branch information
TheQuantumPhysicist authored Feb 6, 2024
2 parents 0ab7904 + cdb8a7a commit d3ce09a
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 17 deletions.
10 changes: 2 additions & 8 deletions common/src/chain/transaction/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,11 @@ impl TextSummary for TxInput {
let n = utxo.output_index();
match source_id {
OutPointSourceId::Transaction(ref id) => {
let id_str = format!("{:?}", id.to_hash())
.strip_prefix("0x")
.expect("0x exists for some hellish reason")
.to_string();
let id_str = format!("{:?}", id.to_hash());
format!("Transaction({id_str}, {n})")
}
OutPointSourceId::BlockReward(id) => {
let id_str = format!("{:?}", id.to_hash())
.strip_prefix("0x")
.expect("0x exists for some hellish reason")
.to_string();
let id_str = format!("{:?}", id.to_hash()).to_string();
format!("BlockReward({id_str}, {n})")
}
}
Expand Down
66 changes: 63 additions & 3 deletions common/src/primitives/id/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

mod with_id;

use std::fmt::{Debug, Display};
use std::fmt::{Debug, Display, LowerHex, UpperHex};

use generic_array::{typenum, GenericArray};
use ref_cast::RefCast;
Expand Down Expand Up @@ -111,7 +111,7 @@ pub struct Id<T> {

impl<T: TypeName> Debug for Id<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Id<{}>{{{:?}}}", T::typename_str(), self.hash)
write!(f, "Id<{}>{{{:x}}}", T::typename_str(), self.hash)
}
}

Expand All @@ -126,7 +126,36 @@ impl<T> Copy for Id<T> {}

impl<T> Display for Id<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(&self.hash, f)
let s = self.hash.to_string();
write!(
f,
"{}",
self.hash.to_string().strip_prefix("0x").unwrap_or(&s)
)
}
}

impl<T> LowerHex for Id<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if f.alternate() {
write!(f, "0x")?;
}
for i in &self.hash.0[..] {
write!(f, "{:02x}", i)?;
}
Ok(())
}
}

impl<T> UpperHex for Id<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if f.alternate() {
write!(f, "0X")?;
}
for i in &self.hash.0[..] {
write!(f, "{:02X}", i)?;
}
Ok(())
}
}

Expand Down Expand Up @@ -253,6 +282,37 @@ mod tests {
}
}

#[test]
fn basic_str() {
let h1: Id<TestType1> =
H256::from_str("000000006a625f06636b8bb6ac7b960a8d03705d1ace08b1a19da3fdcc99ddbd")
.unwrap()
.into();

assert_eq!(
format!("{:x}", h1),
"000000006a625f06636b8bb6ac7b960a8d03705d1ace08b1a19da3fdcc99ddbd".to_string()
);
assert_eq!(
format!("{:#x}", h1),
"0x000000006a625f06636b8bb6ac7b960a8d03705d1ace08b1a19da3fdcc99ddbd".to_string()
);
assert_eq!(
format!("{:X}", h1),
"000000006A625F06636B8BB6AC7B960A8D03705D1ACE08B1A19DA3FDCC99DDBD".to_string()
);
assert_eq!(
format!("{:#X}", h1),
"0X000000006A625F06636B8BB6AC7B960A8D03705D1ACE08B1A19DA3FDCC99DDBD".to_string()
);
assert_eq!(format!("{}", h1), "0000…ddbd".to_string());
assert_eq!(
format!("{:?}", h1),
"Id<TestType1>{000000006a625f06636b8bb6ac7b960a8d03705d1ace08b1a19da3fdcc99ddbd}"
.to_string()
);
}

#[rstest]
#[trace]
#[case(Seed::from_entropy())]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use iced::{
Alignment, Element, Length,
};
use iced_aw::Grid;
use serialization::hex::HexEncode;

use crate::{
backend::messages::AccountInfo,
Expand Down Expand Up @@ -54,7 +53,7 @@ pub fn view_transactions(
|| "-".to_owned(),
|timestamp| print_block_timestamp(*timestamp),
);
let full_tx_id_str = format!("0x{}", tx.txid.hex_encode());
let full_tx_id_str = format!("{:x}", tx.txid);
transaction_list = transaction_list
.push(field(format!("{}", current_transaction_list.skip + index)))
.push(
Expand Down
4 changes: 2 additions & 2 deletions test/functional/test_framework/wallet_cli_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ async def new_address(self) -> str:
async def list_utxos(self, utxo_types: str = '', with_locked: str = '', utxo_states: List[str] = []) -> List[UtxoOutpoint]:
output = await self._write_command(f"account-utxos {utxo_types} {with_locked} {''.join(utxo_states)}\n")

pattern = r'UtxoOutPoint\s*{[^}]*Id<Transaction>\{0x([^}]*)\}[^}]*index:\s*(\d+)'
pattern = r'UtxoOutPoint\s*{[^}]*Id<Transaction>\{([^}]*)\}[^}]*index:\s*(\d+)'
matches = re.findall(pattern, output, re.DOTALL)
return [UtxoOutpoint(id=match[0].strip(), index=int(match[1].strip())) for match in matches]

Expand Down Expand Up @@ -364,7 +364,7 @@ async def get_balance(self, with_locked: str = 'unlocked', utxo_states: List[str

async def list_pending_transactions(self) -> List[str]:
output = await self._write_command(f"transaction-list-pending\n")
pattern = r'id: Id<Transaction>\{0x([^}]*)\}'
pattern = r'id: Id<Transaction>\{([^}]*)\}'
return re.findall(pattern, output)

async def list_transactions_by_address(self, address: Optional[str] = None, limit: int = 100) -> List[str]:
Expand Down
1 change: 0 additions & 1 deletion test/functional/wallet_conflict.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ async def async_test(self):
# try to send tokens again should fail as the tokens are already sent
assert_in("Success", await wallet.select_account(1))
assert_in("Coin selection error: No available UTXOs", await wallet.send_tokens_to_address(token_id, address, tokens_to_mint))

# check that the mempool still has the transfer tx
assert node.mempool_contains_tx(transfer_tx_id)
# abandon it from the wallet side so it is not rebroadcasted
Expand Down
2 changes: 1 addition & 1 deletion test/functional/wallet_submit_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ async def async_test(self):
timestamp = block['block']['V1']['header']['block_header']['timestamp']['timestamp']

output = await wallet.get_transaction(tx_id)
expected_tx_inputs = f"inputs: [Utxo(UtxoOutPoint {{ id: BlockReward(Id<GenBlock>{{0x{genesis_block_id}}}), index: 0 }})]"
expected_tx_inputs = f"inputs: [Utxo(UtxoOutPoint {{ id: BlockReward(Id<GenBlock>{{{genesis_block_id}}}), index: 0 }})]"
assert_in(expected_tx_inputs, output)
expected_tx_outputs = f"outputs: [Transfer(Coin(Amount {{ val: {coins_to_send * ATOMS_PER_COIN} }})"
assert_in(expected_tx_outputs, output)
Expand Down

0 comments on commit d3ce09a

Please sign in to comment.