forked from febe19/bazo-miner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlongestchain_test.go
178 lines (146 loc) · 4.8 KB
/
longestchain_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package miner
import (
"github.com/julwil/bazo-miner/crypto"
"github.com/julwil/bazo-miner/storage"
"testing"
)
//Tests cases 1) when new block is received that belongs to a longer chain and 2) when new block is received
//that is shorter than the current chain
func TestGetBlockSequences(t *testing.T) {
cleanAndPrepare()
b := newBlock([32]byte{}, [crypto.COMM_PROOF_LENGTH]byte{}, 1)
createBlockWithTxs(b)
finalizeBlock(b)
validate(b, false)
b2 := newBlock(b.Hash, [crypto.COMM_PROOF_LENGTH]byte{}, b.Height+1)
createBlockWithTxs(b2)
finalizeBlock(b2)
validate(b2, false)
b3 := newBlock(b2.Hash, [crypto.COMM_PROOF_LENGTH]byte{}, b2.Height+1)
createBlockWithTxs(b3)
if err := finalizeBlock(b3); err != nil {
t.Error(err)
return
}
logger.Printf("b3: %v", b3)
rollback, blocksToValidate, _ := getBlockSequences(b3)
if len(rollback) != 0 {
t.Error("Rollback shouldn't execute here\n")
}
if len(blocksToValidate) != 1 || blocksToValidate[0].Hash != b3.Hash {
t.Error("Wrong validation sequence\n")
}
//PoW needs lastBlock, have to set it manually
lastBlock = storage.ReadClosedBlock([32]byte{})
c := newBlock([32]byte{}, [crypto.COMM_PROOF_LENGTH]byte{}, 1)
createBlockWithTxs(c)
if err := finalizeBlock(c); err != nil {
t.Error(err)
return
}
storage.WriteOpenBlock(c)
//PoW needs lastBlock, have to set it manually
lastBlock = c
c2 := newBlock(c.Hash, [crypto.COMM_PROOF_LENGTH]byte{}, c.Height+1)
createBlockWithTxs(c2)
if err := finalizeBlock(c2); err != nil {
t.Error(err)
return
}
storage.WriteOpenBlock(c2)
//PoW needs lastBlock, have to set it manually
lastBlock = c2
c3 := newBlock(c2.Hash, [crypto.COMM_PROOF_LENGTH]byte{}, c.Height+1)
createBlockWithTxs(c3)
finalizeBlock(c3)
lastBlock = b2
//Blockchain now: genesis <- b <- b2
//New Blockchain of longer size: genesis <- c <- c2 <- c3
rollback, blocksToValidate, _ = getBlockSequences(c3)
//Rollback slice needs to include b2 and b (in that order)
if len(rollback) != 2 ||
rollback[0].Hash != b2.Hash ||
rollback[1].Hash != b.Hash {
t.Error("Rollback of current chain failed\n")
}
if len(blocksToValidate) != 3 ||
blocksToValidate[0].Hash != c.Hash ||
blocksToValidate[1].Hash != c2.Hash ||
blocksToValidate[2].Hash != c3.Hash {
t.Error("Validation failed\n")
}
cleanAndPrepare()
//Make sure that another chain of equal length does not get activated
b = newBlock([32]byte{}, [crypto.COMM_PROOF_LENGTH]byte{}, 1)
createBlockWithTxs(b)
finalizeBlock(b)
validate(b, false)
b2 = newBlock(b.Hash, [crypto.COMM_PROOF_LENGTH]byte{}, b.Height+1)
createBlockWithTxs(b2)
finalizeBlock(b2)
validate(b2, false)
b3 = newBlock(b2.Hash, [crypto.COMM_PROOF_LENGTH]byte{}, b2.Height+1)
createBlockWithTxs(b3)
finalizeBlock(b3)
validate(b3, false)
//Blockchain now: genesis <- b <- b2 <- b3
//Competing chain: genesis <- c <- c2 <- c3
lastBlock = storage.ReadClosedBlock([32]byte{})
c = newBlock([32]byte{}, [crypto.COMM_PROOF_LENGTH]byte{}, 1)
createBlockWithTxs(c)
finalizeBlock(c)
storage.WriteOpenBlock(c)
lastBlock = c
c2 = newBlock(c.Hash, [crypto.COMM_PROOF_LENGTH]byte{}, c.Height+1)
createBlockWithTxs(c2)
finalizeBlock(c2)
storage.WriteOpenBlock(c2)
lastBlock = c2
c3 = newBlock(c2.Hash, [crypto.COMM_PROOF_LENGTH]byte{}, c2.Height+1)
createBlockWithTxs(c3)
finalizeBlock(c3)
//Make sure that the new blockchain of equal length does not get activated
lastBlock = b3
rollback, blocksToValidate, _ = getBlockSequences(c3)
if rollback != nil || blocksToValidate != nil {
t.Error("Did not properly detect longest chain\n")
}
}
//Test whether we get the new proper chain (we leverage the fact that open storage is checked so we don't need
//to need network functionality for that test
func TestGetNewChain(t *testing.T) {
cleanAndPrepare()
b := newBlock([32]byte{}, [crypto.COMM_PROOF_LENGTH]byte{}, 1)
createBlockWithTxs(b)
finalizeBlock(b)
validate(b, false)
b2 := newBlock(b.Hash, [crypto.COMM_PROOF_LENGTH]byte{}, b.Height+1)
createBlockWithTxs(b2)
finalizeBlock(b2)
ancestor, newChain := getNewChain(b2)
if ancestor.Hash != b.Hash {
t.Errorf("Hash mismatch: %x vs. %x\n", ancestor.Hash, b.Hash)
}
if len(newChain) != 1 || newChain[0].Hash != b2.Hash {
t.Error("Wrong new chain\n")
}
//Blockchain now: genesis <- b
//New chain: genesis <- c <- c2
lastBlock = storage.ReadClosedBlock([32]byte{})
c := newBlock([32]byte{}, [crypto.COMM_PROOF_LENGTH]byte{}, 1)
createBlockWithTxs(c)
finalizeBlock(c)
storage.WriteOpenBlock(c)
lastBlock = c
c2 := newBlock(c.Hash, [crypto.COMM_PROOF_LENGTH]byte{}, c.Height+1)
createBlockWithTxs(c2)
finalizeBlock(c2)
lastBlock = b
ancestor, newChain = getNewChain(c2)
if ancestor.Hash != [32]byte{} {
t.Errorf("Hash mismatch")
}
if len(newChain) != 2 || newChain[0].Hash != c.Hash || newChain[1].Hash != c2.Hash {
t.Error("Wrong new chain\n")
}
}