block_v2_test.go raw

   1  package block
   2  
   3  import (
   4  	"encoding/binary"
   5  	"testing"
   6  
   7  	"git.smesh.lol/nostrino/pkg/btcwire"
   8  	"git.smesh.lol/nostrino/pkg/crypto/chainhash"
   9  )
  10  
  11  // TestForkBlockHashesWithItsOwnHeader: a forked block's identity is not the double SHA-256 of its
  12  // first 80 bytes. The parser must dispatch on the version flag, hash the whole 164-byte header
  13  // with the fork's construction, and start reading transactions after it.
  14  func TestForkBlockHashesWithItsOwnHeader(t *testing.T) {
  15  	tx := make([]byte, 0, 64)
  16  	tx = append(tx, 2, 0, 0, 0)                            // version
  17  	tx = append(tx, 1)                                     // one input
  18  	tx = append(tx, make([]byte, 32)...)                   // prevout hash
  19  	tx = append(tx, 0xff, 0xff, 0xff, 0xff)                // prevout index
  20  	tx = append(tx, 0)                                     // empty script
  21  	tx = append(tx, 0xff, 0xff, 0xff, 0xff)                // sequence
  22  	tx = append(tx, 1)                                     // one output
  23  	tx = append(tx, 0x00, 0xe4, 0x0b, 0x54, 0x02, 0, 0, 0) // 10 XBT? any value
  24  	tx = append(tx, 1, 0x51)                               // OP_TRUE
  25  	tx = append(tx, 0, 0, 0, 0)                            // locktime
  26  
  27  	txid := chainhash.DoubleHashH(tx)
  28  
  29  	var h btcwire.BlockHeader
  30  
  31  	version := uint32(btcwire.HeaderV2Flag) | 0x20000000
  32  	h.Version = int32(version)
  33  	h.MerkleRoot = txid
  34  	h.Timestamp = 1_700_000_000
  35  	h.Bits = 0x1d00ffff
  36  	h.V2 = &btcwire.HeaderV2{Height: 962_619, Flags: 4, TimeOffset: 30}
  37  
  38  	header := h.SerializeV2()
  39  
  40  	raw := make([]byte, 0, len(header)+len(tx)+3)
  41  	raw = append(raw, header...)
  42  	raw = append(raw, 1)
  43  	raw = append(raw, tx...)
  44  
  45  	parsed, err := Parse(raw)
  46  
  47  	if err != nil {
  48  		t.Fatal(err)
  49  	}
  50  
  51  	want := h.BlockHash()
  52  
  53  	if parsed.Hash != want {
  54  		t.Fatalf("block hash %x, want %x", parsed.Hash, want)
  55  	}
  56  
  57  	// The legacy 80-byte hash must not be what the parser returned.
  58  	legacy := chainhash.DoubleHashH(header[:btcwire.BlockHeaderSize])
  59  
  60  	if parsed.Hash == legacy {
  61  		t.Fatal("the block was hashed over its first 80 bytes")
  62  	}
  63  
  64  	if len(parsed.TxIDs) != 1 || parsed.TxIDs[0] != txid {
  65  		t.Fatalf("transactions %x, want the one built here", parsed.TxIDs)
  66  	}
  67  }
  68  
  69  // TestLegacyBlockStillHashesOverEightyBytes keeps the Bitcoin path pinned while the fork path is
  70  // added beside it.
  71  func TestLegacyBlockStillHashesOverEightyBytes(t *testing.T) {
  72  	tx := make([]byte, 0, 32)
  73  	tx = append(tx, 2, 0, 0, 0)
  74  	tx = append(tx, 1)
  75  	tx = append(tx, make([]byte, 32)...)
  76  	tx = append(tx, 0xff, 0xff, 0xff, 0xff)
  77  	tx = append(tx, 0)
  78  	tx = append(tx, 0xff, 0xff, 0xff, 0xff)
  79  	tx = append(tx, 1)
  80  	tx = append(tx, 0x00, 0xe4, 0x0b, 0x54, 0x02, 0, 0, 0)
  81  	tx = append(tx, 1, 0x51)
  82  	tx = append(tx, 0, 0, 0, 0)
  83  
  84  	txid := chainhash.DoubleHashH(tx)
  85  
  86  	var h btcwire.BlockHeader
  87  
  88  	h.Version = 0x20000000
  89  	h.MerkleRoot = txid
  90  	h.Timestamp = 1_600_000_000
  91  	h.Bits = 0x1d00ffff
  92  
  93  	header := h.Serialize()
  94  
  95  	raw := make([]byte, 0, len(header)+len(tx)+3)
  96  	raw = append(raw, header[:]...)
  97  	raw = append(raw, 1)
  98  	raw = append(raw, tx...)
  99  
 100  	parsed, err := Parse(raw)
 101  
 102  	if err != nil {
 103  		t.Fatal(err)
 104  	}
 105  
 106  	if parsed.Hash != h.BlockHash() {
 107  		t.Fatal("a Bitcoin block no longer hashes over its 80-byte header")
 108  	}
 109  
 110  	if parsed.Hash != chainhash.DoubleHashH(header[:]) {
 111  		t.Fatal("the legacy hash changed")
 112  	}
 113  
 114  	_ = binary.LittleEndian
 115  }
 116