github.com/lbryio/lbcd@v0.22.119/chaincfg/params_test.go (about)

     1  // Copyright (c) 2016 The btcsuite developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package chaincfg
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/hex"
    10  	"math/big"
    11  	"testing"
    12  )
    13  
    14  // TestInvalidHashStr ensures the newShaHashFromStr function panics when used to
    15  // with an invalid hash string.
    16  func TestInvalidHashStr(t *testing.T) {
    17  	defer func() {
    18  		if r := recover(); r == nil {
    19  			t.Errorf("Expected panic for invalid hash, got nil")
    20  		}
    21  	}()
    22  	newHashFromStr("banana")
    23  }
    24  
    25  // TestMustRegisterPanic ensures the mustRegister function panics when used to
    26  // register an invalid network.
    27  func TestMustRegisterPanic(t *testing.T) {
    28  	t.Parallel()
    29  
    30  	// Setup a defer to catch the expected panic to ensure it actually
    31  	// paniced.
    32  	defer func() {
    33  		if err := recover(); err == nil {
    34  			t.Error("mustRegister did not panic as expected")
    35  		}
    36  	}()
    37  
    38  	// Intentionally try to register duplicate params to force a panic.
    39  	mustRegister(&MainNetParams)
    40  }
    41  
    42  func TestRegisterHDKeyID(t *testing.T) {
    43  	t.Parallel()
    44  
    45  	// Ref: https://github.com/satoshilabs/slips/blob/master/slip-0132.md
    46  	hdKeyIDZprv := []byte{0x02, 0xaa, 0x7a, 0x99}
    47  	hdKeyIDZpub := []byte{0x02, 0xaa, 0x7e, 0xd3}
    48  
    49  	if err := RegisterHDKeyID(hdKeyIDZpub, hdKeyIDZprv); err != nil {
    50  		t.Fatalf("RegisterHDKeyID: expected no error, got %v", err)
    51  	}
    52  
    53  	got, err := HDPrivateKeyToPublicKeyID(hdKeyIDZprv)
    54  	if err != nil {
    55  		t.Fatalf("HDPrivateKeyToPublicKeyID: expected no error, got %v", err)
    56  	}
    57  
    58  	if !bytes.Equal(got, hdKeyIDZpub) {
    59  		t.Fatalf("HDPrivateKeyToPublicKeyID: expected result %v, got %v",
    60  			hdKeyIDZpub, got)
    61  	}
    62  }
    63  
    64  func TestInvalidHDKeyID(t *testing.T) {
    65  	t.Parallel()
    66  
    67  	prvValid := []byte{0x02, 0xaa, 0x7a, 0x99}
    68  	pubValid := []byte{0x02, 0xaa, 0x7e, 0xd3}
    69  	prvInvalid := []byte{0x00}
    70  	pubInvalid := []byte{0x00}
    71  
    72  	if err := RegisterHDKeyID(pubInvalid, prvValid); err != ErrInvalidHDKeyID {
    73  		t.Fatalf("RegisterHDKeyID: want err ErrInvalidHDKeyID, got %v", err)
    74  	}
    75  
    76  	if err := RegisterHDKeyID(pubValid, prvInvalid); err != ErrInvalidHDKeyID {
    77  		t.Fatalf("RegisterHDKeyID: want err ErrInvalidHDKeyID, got %v", err)
    78  	}
    79  
    80  	if err := RegisterHDKeyID(pubInvalid, prvInvalid); err != ErrInvalidHDKeyID {
    81  		t.Fatalf("RegisterHDKeyID: want err ErrInvalidHDKeyID, got %v", err)
    82  	}
    83  
    84  	// FIXME: The error type should be changed to ErrInvalidHDKeyID.
    85  	if _, err := HDPrivateKeyToPublicKeyID(prvInvalid); err != ErrUnknownHDKeyID {
    86  		t.Fatalf("HDPrivateKeyToPublicKeyID: want err ErrUnknownHDKeyID, got %v", err)
    87  	}
    88  }
    89  
    90  func TestSigNetPowLimit(t *testing.T) {
    91  	sigNetPowLimitHex, _ := hex.DecodeString(
    92  		"00000377ae000000000000000000000000000000000000000000000000000000",
    93  	)
    94  	powLimit := new(big.Int).SetBytes(sigNetPowLimitHex)
    95  	if sigNetPowLimit.Cmp(powLimit) != 0 {
    96  		t.Fatalf("Signet PoW limit bits (%s) not equal to big int (%s)",
    97  			sigNetPowLimit.Text(16), powLimit.Text(16))
    98  	}
    99  
   100  	if compactToBig(sigNetGenesisBlock.Header.Bits).Cmp(powLimit) != 0 {
   101  		t.Fatalf("Signet PoW limit header bits (%d) not equal to big "+
   102  			"int (%s)", sigNetGenesisBlock.Header.Bits,
   103  			powLimit.Text(16))
   104  	}
   105  }
   106  
   107  // compactToBig is a copy of the blockchain.CompactToBig function. We copy it
   108  // here so we don't run into a circular dependency just because of a test.
   109  func compactToBig(compact uint32) *big.Int {
   110  	// Extract the mantissa, sign bit, and exponent.
   111  	mantissa := compact & 0x007fffff
   112  	isNegative := compact&0x00800000 != 0
   113  	exponent := uint(compact >> 24)
   114  
   115  	// Since the base for the exponent is 256, the exponent can be treated
   116  	// as the number of bytes to represent the full 256-bit number.  So,
   117  	// treat the exponent as the number of bytes and shift the mantissa
   118  	// right or left accordingly.  This is equivalent to:
   119  	// N = mantissa * 256^(exponent-3)
   120  	var bn *big.Int
   121  	if exponent <= 3 {
   122  		mantissa >>= 8 * (3 - exponent)
   123  		bn = big.NewInt(int64(mantissa))
   124  	} else {
   125  		bn = big.NewInt(int64(mantissa))
   126  		bn.Lsh(bn, 8*(exponent-3))
   127  	}
   128  
   129  	// Make it negative if the sign bit is set.
   130  	if isNegative {
   131  		bn = bn.Neg(bn)
   132  	}
   133  
   134  	return bn
   135  }