This switches the Merkle tree logic for blocks to one that runs in logspace (effectively constant space, with a limit of 2^32 leaves). The old code is moved to tests, and a new test is added that for various combinations of block sizes, transaction positions to compute a branch for, and mutations:
- Verifies that the old code and new code agree for the Merkle root.
- Verifies that the old code and new code agree for the Merkle branch.
- Verifies that the computed Merkle branch is valid.
- Verifies that mutations don’t change the Merkle root.
- Verifies that mutations are correctly detected.
Advantages:
- Removes the need for vMerkleTree (a validation-related data structure) from CBlock (a primitive data structure).
- Is slightly faster due to better memory locality and avoiding all heap overhead.
Disadvantages:
- Requires recomputing the Merkle tree any time a Merkle branch is needed. The wallet depends on this any time a transaction is added. This is not technically necessary, however.
- Longer and harder to read code.
The code remains in primitives/block for now. This is not optimal, as one of the purposes is disentangling the Merkle calculation logic from the primitive data structures. TBD.