0

Is it possible to acquire the merkleroot per block with rpc command or core command?

Why is the merkleroot not included in the block header in the cns003.txt?

I see the transaction hashes are included in some rpc responses so must I calculate merkleroot manually?

Joss Bird
  • 561
  • 3
  • 21

1 Answers1

1

The merkle root hash is used when creating the block hash. This is in the block_header as returned from the daemon RPC method get_block.

jtgrassie
  • 19,111
  • 4
  • 14
  • 51
  • I thought the block hash was the block identifier: – Satoshi Nakamonero Oct 07 '18 at 10:00
  • Calculation of Block Identifier

    The identifier of a block is the result of hashing the following data with Keccak:

    • size of [block_header, Merkle root hash, and the number of transactions] in bytes (varint)

    • block_header,

    • Merkle root hash,

    • number of transactions (varint).

    The goal of the Merkle root hash is to "attach" the transactions referred to in the list to the block header: once the Merkle root hash is fixed, the transactions cannot be modified.

  • – Satoshi Nakamonero Oct 07 '18 at 10:00
  • For block with height of 912345 and block hash e22cf75f39ae720e8b71b3d120a5ac03f0db50bba6379e2850975b4859190bc6 I see only a coinbase transaction with hash c7da3965f25c19b8eb7dd8db48dcd4e7c885e2491db77e289f0609bf8e08ec30. In bitcoin if there is only a coinbase transaction then the coinbase transaction is used as merkleroot but this does not seem to be the case for Monero? When I take the Monero 912345 coinbase hash byteswap it, concatenate it with itself decode from hex, double hash it with sha256 and encode to hex doesn't result in the block hash? – Satoshi Nakamonero Oct 07 '18 at 10:08
  • Block hash is the block ID which also is derived from merkle root hash. – jtgrassie Oct 07 '18 at 13:15
  • Your example of trying to recreate block hash is flawed in a couple of ways. The merkle root hash is just the tx hash if there is only 1 tx, but to get to the block hash you need to follow the steps in the paper you linked. There is no mention of sha256 for starters. You need to use Keccak. – jtgrassie Oct 07 '18 at 13:32
  • ok, so is the only way to get the merkle root for block with more than one transaction(coinbase) to calculate it myself using keccak. The RPC method get_block doesn't contain the merkleroot from what i can tell unless it is in the blob? – Satoshi Nakamonero Oct 07 '18 at 15:49
  • Keccak won't calculate a merkle root, you have to generate/calculate the merkle root before hashing. get_block returns everything needed to calculate the merkle root hash (as outlined in the linked cns003 spec). – jtgrassie Oct 07 '18 at 16:52
  • h[i] = H(h[2*i] || h[2*i+1]) where 1 <= i <= m-1 or 3m-n <= i <= 2m-1. h[i] = H(tx[i-m]) where m <= i <= 3m-n-1 h[i] = H(tx[i-4m+n]) where 6m-2n <= i <= 4*m-1.

    Where H is the Keccak function that is used throughout CryptoNote, Why won't Keccak calculate the merkle root?

    – Satoshi Nakamonero Oct 08 '18 at 18:29
  • Seems to me the merkleroot is whole thing holding blockchain together why is is not in the block header and must be calculated manually using tx hashes. – Satoshi Nakamonero Oct 08 '18 at 18:36
  • "Why won't Keccak calculate the merkle root?" Because it's a hashing function, nothing more. – jtgrassie Oct 08 '18 at 18:42
  • "Seems to me the merkleroot is whole thing holding blockchain together why is is not in the block header and must be calculated manually using tx hashes". It is, it's hashed in the block ID (hash). If anything changes in the block (or it's txs), when validating a block ID it would not match. This is what makes the blocks immutable (tamperproof). – jtgrassie Oct 08 '18 at 18:45
  • ok that is what i am asking. So i use the keccak function to hash transactions hashes to get the merkle root. This is what I am asking. Say I pick any block, say block height 542343 is the only way to get the merkleroot from calculating it manually – Satoshi Nakamonero Oct 08 '18 at 19:03
  • "So i use the keccak function to hash transactions hashes to get the merkle root" No. The block hash (it's ID) is defined in the cns003 doc you linked to at section 5: "The identifier of a block is the result of hashing the following data with Keccak: - size of [block_header, Merkle root hash, and the number of transactions] in bytes (varint) - block_header, - Merkle root hash, - number of transactions (varint)."

    Keccak is a hashing function.

    – jtgrassie Oct 08 '18 at 19:44
  • An example of calculating the merkle root is defined here: https://github.com/monero-project/monero/blob/9f24e57dc225a26583571792caa0a948904ef21c/src/crypto/tree-hash.c#L70 – jtgrassie Oct 08 '18 at 19:45