github.com/mit-dci/lit@v0.0.0-20221102210550-8c3d3b49f2ce/uspv/header_test.go (about)

     1  package uspv
     2  
     3  import (
     4  	"crypto/rand"
     5  	"encoding/hex"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/mit-dci/lit/btcutil/chaincfg/chainhash"
    10  	"github.com/mit-dci/lit/coinparam"
    11  	"github.com/mit-dci/lit/logging"
    12  	"github.com/mit-dci/lit/wire"
    13  )
    14  
    15  func randomHex(n int) string {
    16  	bytes := make([]byte, n)
    17  	if _, err := rand.Read(bytes); err != nil {
    18  		return ""
    19  	}
    20  	return hex.EncodeToString(bytes)
    21  }
    22  
    23  func TestMoreWork(t *testing.T) {
    24  	var a []*wire.BlockHeader
    25  	var b []*wire.BlockHeader
    26  	p := &coinparam.BitcoinParams
    27  	// 0000000000dd4dd73d78e8fd29ba2fd2ed618bd6fa2ee92559f5 42fdb26e7c1d
    28  	for j := 0; j < 2016; j++ {
    29  		header := "0000000000dd4dd73d78e8fd29ba2fd2ed618bd6fa2ee92559f5" + randomHex(6)
    30  		testHash, _ := chainhash.NewHashFromStr(header)
    31  		temp := wire.BlockHeader{
    32  			Version:    32,
    33  			PrevBlock:  *testHash,
    34  			MerkleRoot: *testHash,
    35  			Timestamp:  time.Unix(0x495fab29, 0),
    36  			Bits:       uint32(0x1d00ffff), // let just make up some stuff over here
    37  			Nonce:      uint32(0x1d00aaaa), // all invalid
    38  		}
    39  		a = append(a, &temp)
    40  		b = append(b, &temp)
    41  	}
    42  	header := "0000000000dd4dd73d78e8fd29ba2fd2ed618bd6fa2ee92559f5" + randomHex(6)
    43  	// Chain A -> 2016 -> header1 -> header3 -> header4
    44  	// Chain B -> 2016 -> header2
    45  	testHash, _ := chainhash.NewHashFromStr(header)
    46  
    47  	temp1 := wire.BlockHeader{
    48  		Version:    32,
    49  		PrevBlock:  *testHash,
    50  		MerkleRoot: *testHash,
    51  		Timestamp:  time.Unix(0x495fab29, 0),
    52  		Bits:       uint32(0x1d000ff1), // let just make up some stuff over here
    53  		Nonce:      uint32(0x1d00aaaa), // all invalid
    54  	}
    55  	a = append(a, &temp1)
    56  
    57  	temp2 := wire.BlockHeader{
    58  		Version:    32,
    59  		PrevBlock:  *testHash,
    60  		MerkleRoot: *testHash,
    61  		Timestamp:  time.Unix(0x495fab29, 0),
    62  		Bits:       uint32(0x1d000ff2), // let just make up some stuff over here
    63  		Nonce:      uint32(0x1d00aaaa), // all invalid
    64  	}
    65  	a = append(a, &temp2)
    66  
    67  	temp3 := wire.BlockHeader{
    68  		Version:    32,
    69  		PrevBlock:  *testHash,
    70  		MerkleRoot: *testHash,
    71  		Timestamp:  time.Unix(0x495fab29, 0),
    72  		Bits:       uint32(0x1d000ff3), // let just make up some stuff over here
    73  		Nonce:      uint32(0x1d00aaaa), // all invalid
    74  	}
    75  	a = append(a, &temp3)
    76  
    77  	temp4 := wire.BlockHeader{
    78  		Version:    32,
    79  		PrevBlock:  *testHash,
    80  		MerkleRoot: *testHash,
    81  		Timestamp:  time.Unix(0x495fab29, 0),
    82  		Bits:       uint32(0x1d0000f1), // let just make up some stuff over here
    83  		Nonce:      uint32(0x1d00aaaa), // all invalid
    84  	}
    85  
    86  	b = append(b, &temp4)
    87  	// Work of A: 206916179889
    88  	// WOrk of B: 1167945961455
    89  
    90  	if moreWork(a, b, p) {
    91  		logging.Error("Test failed!!")
    92  		t.Fatal()
    93  	} else {
    94  		logging.Info("Test Passed!")
    95  	}
    96  }