hashorder_test.go raw

   1  package v2peer
   2  
   3  import (
   4  	"testing"
   5  	"time"
   6  
   7  	btcdwire "github.com/btcsuite/btcd/wire/v2"
   8  
   9  	"git.smesh.lol/nostrino/pkg/btcwire"
  10  )
  11  
  12  // TestTheTwoHashTypesAgree: btcd's hash and this project's hash of one header
  13  // are the same 32 bytes in every field that matters, with no conversion. The
  14  // zero header looked like they disagreed because it reverses onto itself: it
  15  // carries no information about layout. A header with distinct fields shows
  16  // what holds for every header, and it is the rule the rest of the code uses.
  17  func TestTheTwoHashTypesAgree(t *testing.T) {
  18  	theirs := &btcdwire.BlockHeader{Version: 1, Timestamp: time.Unix(1234567890, 0), Bits: 0x1d00ffff, Nonce: 42}
  19  	ours := &btcwire.BlockHeader{Version: 1, Timestamp: 1234567890, Bits: 0x1d00ffff, Nonce: 42}
  20  	for i := 0; i < 32; i++ {
  21  		theirs.PrevBlock[i] = byte(i)
  22  		theirs.MerkleRoot[i] = byte(0xff - i)
  23  		ours.PrevBlock[i] = byte(i)
  24  		ours.MerkleRoot[i] = byte(0xff - i)
  25  	}
  26  	v := [32]byte(theirs.BlockHash())
  27  	o := [32]byte(ours.BlockHash())
  28  	if v != o {
  29  		t.Fatalf("this project hashes the header %x, btcd hashes it %x", o, v)
  30  	}
  31  	if toVendor(o) != theirs.BlockHash() {
  32  		t.Fatal("toVendor is not the identity on a hash the two types already agree on")
  33  	}
  34  	if fromVendor(v) != o {
  35  		t.Fatal("fromVendor is not the identity on a hash the two types already agree on")
  36  	}
  37  	t.Logf("both hash it %x", o)
  38  }
  39