fork_pow_test.go raw

   1  package neutrino
   2  
   3  import (
   4  	"math/big"
   5  	"testing"
   6  
   7  	"git.smesh.lol/nostrino/pkg/btcwire"
   8  )
   9  
  10  // TestShiftedBitsAppliesTheForkShiftOnce: the fork relaxes the target by 2^shift at the boundary
  11  // and nowhere else, capped at the chain's proof-of-work limit.
  12  func TestShiftedBitsAppliesTheForkShiftOnce(t *testing.T) {
  13  	const (
  14  		bits  = 0x1703a30c
  15  		limit = 0x1d00ffff
  16  	)
  17  
  18  	if got := shiftedBits(bits, 0, limit); got != bits {
  19  		t.Fatalf("a zero shift changed the bits to %08x", got)
  20  	}
  21  
  22  	target, err := btcwire.CompactToBig(bits)
  23  
  24  	if err != nil {
  25  		t.Fatal(err)
  26  	}
  27  
  28  	want := BigToCompact(new(big.Int).Lsh(target, 22))
  29  
  30  	if got := shiftedBits(bits, 22, limit); got != want {
  31  		t.Fatalf("shifted bits %08x, want %08x", got, want)
  32  	}
  33  
  34  	// The limit itself, shifted, is above the limit, so it must clamp rather than overflow.
  35  	if got := shiftedBits(limit, 22, limit); got != limit {
  36  		t.Fatalf("a shift above the limit gave %08x, want the limit %08x", got, limit)
  37  	}
  38  }
  39