Skip to content

Commit

Permalink
fix mt bug
Browse files Browse the repository at this point in the history
  • Loading branch information
SWvheerden committed Nov 29, 2023
1 parent 8b7a0fd commit a47dd9c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 20 deletions.
2 changes: 1 addition & 1 deletion base_layer/core/src/proof_of_work/monero_rx/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ pub fn insert_merge_mining_tag_and_aux_chain_merkle_root_into_block<T: AsRef<[u8

block.miner_tx.prefix.extra = extra_field.into();

//lets test the block to ensure its serializes correctly
// lets test the block to ensure its serializes correctly
let blocktemplate_blob = serialize_monero_block_to_hex(block)?;
let bytes = hex::decode(blocktemplate_blob).map_err(|_| HexError::HexConversionError {})?;
let de_block = monero::consensus::deserialize::<monero::Block>(&bytes[..])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ use std::{cmp::min, convert::TryFrom};
use monero::VarInt;

// This is based on https://github.com/SChernykh/p2pool/blob/merge-mining/docs/MERGE_MINING.MD#merge-mining-tx_extra-tag-format
#[derive(Debug)]
#[derive(Debug, Clone, PartialEq)]
pub struct MerkleTreeParameters {
pub number_of_chains: u8,
pub aux_nonce: u32,
}

impl MerkleTreeParameters {
pub fn from_varint(merkle_tree_varint: VarInt) -> MerkleTreeParameters {
let bits = get_decode_bits(merkle_tree_varint.0);
let bits = get_decode_bits(merkle_tree_varint.0) + 1;
let number_of_chains = get_aux_chain_count(merkle_tree_varint.0, bits);
let aux_nonce = get_aux_nonce(merkle_tree_varint.0, bits);
MerkleTreeParameters {
Expand All @@ -45,16 +45,15 @@ impl MerkleTreeParameters {
pub fn to_varint(&self) -> VarInt {
let size = u8::try_from(self.number_of_chains.leading_zeros())
.expect("This cant fail, u8 can only have 8 leading 0's which will fit in 255");
let mut bits = encode_bits(8 - size);
let mut n = encode_aux_chain_count(self.number_of_chains, 8 - size);
let mut nonce = encode_aux_nonce(self.aux_nonce);
bits.append(&mut n);
bits.append(&mut nonce);
if bits.len() < 64 {
let mut missing_zeroes = vec![0; 64 - bits.len()];
bits.append(&mut missing_zeroes);
}
let num: u64 = bits.iter().fold(0, |result, &bit| (result << 1) ^ u64::from(bit));
let mut size_bits = encode_bits(7 - size);
let mut n_bits = encode_aux_chain_count(self.number_of_chains, 8 - size);
let mut nonce_bits = encode_aux_nonce(self.aux_nonce);
// this wont underflow as max size will be size_bits(3) + n_bits(8) + nonce_bits(32) = 43
let mut zero_bits = vec![0; 64 - size_bits.len() - n_bits.len() - nonce_bits.len()];
size_bits.append(&mut n_bits);
size_bits.append(&mut nonce_bits);
size_bits.append(&mut zero_bits);
let num: u64 = size_bits.iter().fold(0, |result, &bit| (result << 1) ^ u64::from(bit));
VarInt(num)
}
}
Expand Down Expand Up @@ -91,13 +90,16 @@ fn encode_aux_nonce(num: u32) -> Vec<u8> {

#[cfg(test)]
mod test {
use crate::proof_of_work::monero_rx::merkle_tree_parameters::{
encode_aux_chain_count,
encode_aux_nonce,
encode_bits,
get_aux_chain_count,
get_aux_nonce,
get_decode_bits,
use crate::proof_of_work::monero_rx::{
merkle_tree_parameters::{
encode_aux_chain_count,
encode_aux_nonce,
encode_bits,
get_aux_chain_count,
get_aux_nonce,
get_decode_bits,
},
MerkleTreeParameters,
};

#[test]
Expand Down Expand Up @@ -247,4 +249,33 @@ mod test {
];
assert_eq!(bits, array);
}

mod quicktest {
use quickcheck::{quickcheck, Arbitrary, Gen};

use crate::proof_of_work::monero_rx::MerkleTreeParameters;

impl Arbitrary for MerkleTreeParameters {
fn arbitrary(g: &mut Gen) -> MerkleTreeParameters {
let mut mt = MerkleTreeParameters {
number_of_chains: u8::arbitrary(g),
aux_nonce: u32::arbitrary(g),
};
if mt.number_of_chains == 0 {
mt.number_of_chains = 1;
};
mt
}
}

#[test]
fn test_ser_deserialize() {
fn varint_serialization(mt_params: MerkleTreeParameters) -> bool {
let varint = mt_params.to_varint();
let deserialize = MerkleTreeParameters::from_varint(varint);
mt_params == deserialize
}
quickcheck(varint_serialization as fn(MerkleTreeParameters) -> bool)
}
}
}

0 comments on commit a47dd9c

Please sign in to comment.