What shape of merkle tree does the Bitcoin client build?

2

1

The Bitcoin client builds a merkle tree to represent the transactions, then includes the root of that tree in the block header.
merkle tree from bitcoin paper
But usually, the number of transactions is not a power of two. When that happens, how does the Bitcoin client represent the transactions in the tree?

If I wrote a custom mining client, which built a tree with a different "shape," (example below) what would happen?

custom merkle tree

Nick ODell

Posted 2014-09-01T03:08:16.340

Reputation: 26 536

Answers

3

When the number of transactions is not a power of two, how does the Bitcoin client represent the transactions?

Each time around, it hashes each transaction with the one next to it, until there's only one left.
2 transactions merkle tree example
If there's an odd number of transactions, the last one is hashed again. 3 transactions merkle tree example
In this example, we start out with 11, then 6, then 3, then 2, then 1. 11 transactions merkle tree example

What would happen if you made a differently-shaped merkle-tree?

When sending blocks around, there's no way to specify the shape of the merkle tree. If it's different, the standard client will just reject it.

Nick ODell

Posted 2014-09-01T03:08:16.340

Reputation: 26 536

3Small correction: when there are an odd number of elements in a level, the last one is hashed after appending it to itself. So usually hash6 of one level is H(hash12+hash13) of the level below, but it can be H(hash12+hash12) if hash12 is the last one.Pieter Wuille 2014-09-01T09:21:57.760

@PieterWuille Huh. I hadn't noticed that. +1Nick ODell 2014-09-01T15:15:46.273