vbuterin / pybitcointools

SImple, common-sense Bitcoin-themed Python ECC library
1.28k stars 857 forks source link

merkle_prove index out of range #73

Closed ethers closed 9 years ago

ethers commented 9 years ago

https://github.com/vbuterin/pybitcointools/blob/master/bitcoin/blocks.py#L43 is hitting index out of range

tx = 'fda204502a3345e08afd6af27377c052e77f1fefeaeb31bdd45f1e1237ca5470'
>>> merkle_prove(tx)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "bitcoin/composite.py", line 128, in merkle_prove
  File "bitcoin/blocks.py", line 43, in mk_merkle_proof
IndexError: list index out of range

fda... is the 8th indexed transaction of block 100001

ethers commented 9 years ago

this exact issue is fixed with https://github.com/vbuterin/pybitcointools/commit/189fc6628aa8a795d04fa4b8fbfefc3e9bdae207

(but found another bug which will be opened next)

ethers commented 9 years ago

189fc66 probably caused #74

Keeping this seems clearer

        if len(nodes) % 2:
            newnodes.append(bin_sha256(bin_sha256(nodes[-1] + nodes[-1])))

So instead of

merkle_siblings = \
    [layers[i][(index >> i) ^ 1] for i in range(len(layers)-1)]

something like this fixes issue #73

    merkle_siblings = []
    for i in range(len(layers)-1):
        j = (index >> i) ^ 1
        if j == len(layers[i]):
            j -= 1
        merkle_siblings.append(layers[i][j])

What do you think?