Skip to content

Commit

Permalink
Merge #2173: [Merge Mining] Fixed missing Merge Mining Tag
Browse files Browse the repository at this point in the history
The function `append_merge_mining_tag`, the `block` parameter needed to be
mutable.  Ensured miner_tx hash is first hash added to hashes when calculating
root.
  • Loading branch information
stringhandler committed Aug 27, 2020
2 parents c372c0d + c5542a3 commit d2ddd71
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
25 changes: 17 additions & 8 deletions applications/tari_merge_mining_proxy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,20 @@ fn get_monero_data(data: &[u8], seed: String) -> Option<MoneroData> {
let block = deserialize::<Block>(&hex);
match block {
Ok(block) => {
let mut hashes = block.clone().tx_hashes;
let count = 1 + (block.tx_hashes.len() as u16);
let mut hashes = Vec::with_capacity(count as usize);
let mut proof = Vec::with_capacity(count as usize);
hashes.push(block.miner_tx.hash());
let root = tree_hash(hashes);
let mut proof = block.clone().tx_hashes;
proof.push(block.miner_tx.hash());
for item in block.clone().tx_hashes {
hashes.push(item);
proof.push(item);
}
let root = tree_hash(hashes);
Some(MoneroData {
header: block.header.clone(),
key: seed,
count: (block.tx_hashes.len() as u16) + 1,
count,
transaction_root: from_slice(root.as_slice()),
transaction_hashes: from_hashes(&proof),
coinbase_tx: block.miner_tx,
Expand Down Expand Up @@ -393,8 +398,8 @@ fn add_merge_mining_tag(data: &[u8], hash: &[u8]) -> Vec<u8> {
Ok(hex) => {
let block = deserialize::<Block>(&hex[..]);
match block {
Ok(block) => {
let mm_tag = append_merge_mining_tag(&block, Hash(from_slice(hash)));
Ok(mut block) => {
let mm_tag = append_merge_mining_tag(&mut block, Hash(from_slice(hash)));
match mm_tag {
Ok(mm_tagged_template) => {
let count = 1 + block.tx_hashes.len() as u16;
Expand Down Expand Up @@ -969,9 +974,13 @@ mod test {
let bytes = hex::decode(hex).unwrap();
let block = deserialize::<Block>(&bytes[..]).unwrap();
let header = serialize::<BlockHeader>(&block.header);
let mut count = serialize::<VarInt>(&VarInt(1 + block.tx_hashes.len() as u64));
let mut hashes = block.clone().tx_hashes;
let tx_count = 1 + block.tx_hashes.len() as u64;
let mut count = serialize::<VarInt>(&VarInt(tx_count));
let mut hashes = Vec::with_capacity(tx_count as usize);
hashes.push(block.miner_tx.hash());
for item in block.clone().tx_hashes {
hashes.push(item);
}
let mut root = tree_hash(hashes); // tree_hash.c used by monero
let mut encode2 = header;
encode2.append(&mut root);
Expand Down
17 changes: 8 additions & 9 deletions base_layer/core/src/proof_of_work/monero_rx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub struct MoneroData {

// Hash algorithm in monero
pub fn cn_fast_hash(data: &[u8]) -> Vec<u8> {
Hash::hash(data).0.as_bytes().to_vec()
Hash::hash(data).0.to_vec()
}

// Tree hash count in monero
Expand Down Expand Up @@ -170,16 +170,15 @@ fn monero_difficulty_calculation(header: &BlockHeader) -> Result<Difficulty, Mer
Ok(difficulty)
}

/// Appends merge mining hash to a Monero block and returns the Monero blocktemplate_blob
pub fn append_merge_mining_tag(block: &MoneroBlock, hash: Hash) -> Result<String, MergeMineError> {
let mut monero_block = block.clone();
/// Appends merge mining hash to a Monero block and returns the encoded Monero blocktemplate_blob
pub fn append_merge_mining_tag(block: &mut MoneroBlock, hash: Hash) -> Result<String, MergeMineError> {
let mm_tag = SubField::MergeMining(VarInt(0), hash);
monero_block.miner_tx.prefix.extra.0.push(mm_tag);
block.miner_tx.prefix.extra.0.push(mm_tag);
let serialized = serialize::<MoneroBlock>(&block);
Ok(hex::encode(&serialized).into())
}

/// Calculates the Monero blockhashing_blob
/// Calculates the encoded Monero blockhashing_blob
pub fn create_input_blob(
header: &MoneroBlockHeader,
tx_count: &u16,
Expand All @@ -191,7 +190,7 @@ pub fn create_input_blob(
let mut count = serialize::<VarInt>(&VarInt(tx_count.clone() as u64));
let mut hashes = Vec::new();
for item in tx_hashes {
hashes.push(Hash(from_slice(item.clone().as_bytes())));
hashes.push(Hash::from(item.clone()));
}
let mut root = tree_hash(hashes);
let mut encode = header;
Expand Down Expand Up @@ -220,7 +219,7 @@ pub fn from_hashes(hashes: &[Hash]) -> Vec<[u8; 32]> {
fn verify_root(monero_data: &MoneroData) -> Result<(), MergeMineError> {
let mut hashes = Vec::new();
for item in &monero_data.transaction_hashes {
hashes.push(Hash(from_slice(item.to_vec().as_slice())));
hashes.push(Hash::from(item));
}
let root = tree_hash(hashes);

Expand All @@ -235,7 +234,7 @@ fn verify_root(monero_data: &MoneroData) -> Result<(), MergeMineError> {
fn merged_mining_subfield(header: &BlockHeader) -> SubField {
let hash = header.merged_mining_hash();
let depth = 0;
SubField::MergeMining(VarInt(depth), Hash::hash(&hash[..32]))
SubField::MergeMining(VarInt(depth), Hash::from(from_slice(&hash)))
}

fn verify_header(header: &BlockHeader, monero_data: &MoneroData) -> Result<(), MergeMineError> {
Expand Down

0 comments on commit d2ddd71

Please sign in to comment.