electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

test_verifier.py (2209B)


      1 # -*- coding: utf-8 -*-
      2 
      3 from electrum.bitcoin import hash_encode
      4 from electrum.transaction import Transaction
      5 from electrum.util import bfh
      6 from electrum.verifier import SPV, InnerNodeOfSpvProofIsValidTx
      7 
      8 from . import TestCaseForTestnet
      9 
     10 
     11 MERKLE_BRANCH = [
     12     'f2994fd4546086b21b4916b76cf901afb5c4db1c3ecbfc91d6f4cae1186dfe12',
     13     '6b65935528311901c7acda7db817bd6e3ce2f05d1c62c385b7caadb65fac7520']
     14 
     15 MERKLE_ROOT = '11dbac015b6969ea75509dd1250f33c04ec4d562c2d895de139a65f62f808254'
     16 
     17 VALID_64_BYTE_TX = ('0200000001cb659c5528311901a7aada7db817bd6e3ce2f05d1c62c385b7caad'
     18                     'b65fac75201234000000fabcdefa01abcd1234010000000405060708fabcdefa')
     19 assert len(VALID_64_BYTE_TX) == 128
     20 
     21 
     22 class VerifierTestCase(TestCaseForTestnet):
     23     # these tests are regarding the attack described in
     24     # https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2018-June/016105.html
     25 
     26     def test_verify_ok_t_tx(self):
     27         """Actually mined 64 byte tx should not raise."""
     28         t_tx = Transaction(VALID_64_BYTE_TX)
     29         t_tx_hash = t_tx.txid()
     30         self.assertEqual(MERKLE_ROOT, SPV.hash_merkle_root(MERKLE_BRANCH, t_tx_hash, 3))
     31 
     32     def test_verify_fail_f_tx_odd(self):
     33         """Raise if inner node of merkle branch is valid tx. ('odd' fake leaf position)"""
     34         # first 32 bytes of T encoded as hash
     35         fake_branch_node = hash_encode(bfh(VALID_64_BYTE_TX[:64]))
     36         fake_mbranch = [fake_branch_node] + MERKLE_BRANCH
     37         # last 32 bytes of T encoded as hash
     38         f_tx_hash = hash_encode(bfh(VALID_64_BYTE_TX[64:]))
     39         with self.assertRaises(InnerNodeOfSpvProofIsValidTx):
     40             SPV.hash_merkle_root(fake_mbranch, f_tx_hash, 7)
     41 
     42     def test_verify_fail_f_tx_even(self):
     43         """Raise if inner node of merkle branch is valid tx. ('even' fake leaf position)"""
     44         # last 32 bytes of T encoded as hash
     45         fake_branch_node = hash_encode(bfh(VALID_64_BYTE_TX[64:]))
     46         fake_mbranch = [fake_branch_node] + MERKLE_BRANCH
     47         # first 32 bytes of T encoded as hash
     48         f_tx_hash = hash_encode(bfh(VALID_64_BYTE_TX[:64]))
     49         with self.assertRaises(InnerNodeOfSpvProofIsValidTx):
     50             SPV.hash_merkle_root(fake_mbranch, f_tx_hash, 6)