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