-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockchain_test.go
262 lines (218 loc) · 6.6 KB
/
blockchain_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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// for more information about testing in Go, see
// https://ieftimov.com/posts/testing-in-go-go-test/
package main
import ("os"
"errors"
"testing";
"fmt";
//"strconv"
)
var note float64 =.0
// tests if a []byte starts with a given number of zeros
func TestStartsWithXZeros(t *testing.T){
var test_note=1.5
b0 := []byte{0x0F}
if !StartsWithXZeros(b0,0){
t.Error("isValidHash has Problem with b0")
test_note=test_note-.25
}
if !StartsWithXZeros(b0,1){
t.Error("isValidHash has Problem with b0")
test_note=test_note-.25
}
if StartsWithXZeros(b0,7){
t.Error("isValidHash has Problem with b0")
test_note=test_note-.25
}
b1 := []byte{0x01, 0x00, 0x01}
if StartsWithXZeros(b1,8){
t.Error("isValidHash has Problem with b1")
test_note=test_note-.25
}
b2 := []byte{0x00, 0x00, 0x01}
if StartsWithXZeros(b2,24){
t.Error("isValidHash has Problem with b2")
test_note=test_note-.25
}
if !StartsWithXZeros(b2,23){
t.Error("isValidHash has Problem with b2")
test_note=test_note-.25
}
note=note+test_note
}
func TestEqualSlices(t *testing.T){
var test_note=0.75
s := []byte{0x1,0x1,0x2,0x1,0x1,0x1}
if !EqualSlices(s[0:2],s[3:5]){
t.Error("TestEqualSlices failed")
test_note=test_note-0.25
}
if EqualSlices(s[0:2],s[3:6]){
t.Error("TestEqualSlices failed")
test_note=test_note-0.25
}
if EqualSlices(s[1:3],s[3:5]){
t.Error("TestEqualSlices failed")
test_note=test_note-0.25
}
note=note+test_note
}
func TestIntToHex(t *testing.T){
var test_note=0.75
if !EqualSlices(IntToHex(0), []byte{0,0,0,0,0,0,0,0}){
t.Error("TestIntToHex failed")
test_note=test_note-0.25
}
var p int64= 16777215
want:= []byte{0,0,0,0,0,0xFF,0xFF,0xFF}
if !EqualSlices(IntToHex(p),want){
t.Error("TestIntToHex failed")
test_note=test_note-0.5
}
note=note+test_note
}
// Coinbase transacations are the genesis transactions, they don't
// need TXInputs, contrary to other transactions. Since mining is free
// in this Blockchain, and there is no inflation, the number of
// bitcoins in the blockchain remains constant.
func TestNewCoinbaseTX(t *testing.T){
var test_note=3.0
have1:=*NewCoinbaseTX("ammar","Reward for ammar 404")
have2:=*NewCoinbaseTX("ammar","")
txin1 := TXInput{[]byte{}, -1, "Reward for ammar 404"}
txin2 := TXInput{[]byte{}, -1, "Reward for ammar"}
txout := TXOutput{10, "ammar"}
want1 := Transaction{nil, []TXInput{txin1}, []TXOutput{txout}}
want2 := Transaction{nil, []TXInput{txin2}, []TXOutput{txout}}
if EqualTransactions(have1,want1){
t.Error("TestNewCoinbaseTX failed")
test_note=test_note-1
}
want1.Hash=want1.ComputeHash()
if !EqualTransactions(have1,want1){
t.Error("TestNewCoinbaseTX failed")
test_note=test_note-1
}
want2.Hash=want2.ComputeHash()
if !EqualTransactions(have2,want2){
t.Error("TestNewCoinbaseTX failed")
test_note=test_note-1
}
note=note+test_note
}
// creates a block with no transactions, nonce and timestamps are set
// to zero
func TestCreateEmptyBlock(t *testing.T){
var test_note=1.0
want := Block{nil, []byte{0x0}, nil, 0, 0}
got:=NewBlock(nil, []byte{0x0},false, 0)
got.Timestamp=0
if !EqualBlocks(*got,want){
fmt.Println(want)
fmt.Println(got)
t.Error("TestCreateEmptyBlock failed")
test_note=test_note-1
}
note=note+test_note
}
// Creates a Genesis Block and a blockchain that contains . The
// Genesis Block is the only entry point for bitcoins
func TestCreateGBlock(t *testing.T){
var test_note=3.0
difficulty:=16
cbtx:=NewCoinbaseTX("ammar","Reward for ammar 404")
genesisblock:=NewGBlock([]*Transaction{cbtx},difficulty);
bc:=NewBlockchainFromGB(genesisblock)
if !genesisblock.IsCorrectlyHashed(difficulty)||
bc.GetBalance("ammar")!=10{
fmt.Println("Coorectly hashed=",genesisblock.IsCorrectlyHashed(difficulty))
t.Error("TestCreateGBlock failed")
test_note=test_note-3
}
note=note+test_note
}
//
func TestCreateGBlock2(t *testing.T){
var test_note=1.0
difficulty:=18
cbtx1:=NewCoinbaseTX("ammar","Reward for ammar 404")
cbtx2:=NewCoinbaseTX("ammar","Reward for ammar 404")
genesisblock:=NewGBlock([]*Transaction{cbtx1,cbtx2},difficulty);
bc:=NewBlockchainFromGB(genesisblock)
if !genesisblock.IsCorrectlyHashed(difficulty)||
bc.GetBalance("ammar")!=20 {
t.Error("TestCreateGBlock failed")
test_note=0
}
note=note+test_note
}
func TestSerialization(t *testing.T){
var test_note=2.0
difficulty:=16
cbtx1:=NewCoinbaseTX("ammar","Reward for ammar 404")
cbtx2:=NewCoinbaseTX("ammar","Reward for ammar 404")
cbtx3:=NewCoinbaseTX("ali","Reward for ali 404")
genesisblock:=NewGBlock([]*Transaction{cbtx1,cbtx2,cbtx3},difficulty);
bc:=NewBlockchainFromGB(genesisblock)
err:=SaveBlockchain(bc,"bctest.json")
if err!=nil {
t.Error("SaveBlockchain Failed")
}
if _, err := os.Stat("bctest.json");errors.Is(err, os.ErrNotExist) {
t.Error("File was not written in the disk")
test_note=test_note-1
}
bc2,err:=LoadBlockchain("bctest.json")
if err!=nil||bc2.GetBalance("ammar")!=20 ||bc2.GetBalance("ali")!=10{
t.Error("TestSerialization failed")
test_note=test_note-1
}
err = os.Remove("bctest.json")
if err !=nil {
t.Error("Can't remove file from disk")
}
note=note+test_note
}
func TestSuccessfulTransfert(t *testing.T){
var test_note=2.0
bc:= NewBlockchain([]string{"ammar","ali"})
tx,err:=bc.NewTransfertTX("ali","ammar", 5)
bc.AddBlock([]*Transaction{tx})
if err !=nil || bc.GetBalance("ammar")!=15 || bc.GetBalance("ali")!=5{
t.Error("TestSuccTransfert failed")
test_note=test_note-2
}
note=note+test_note
}
/**
Testing a blockchain transfers with more than 2 actors*/
func TestSuccessfulTransfert2(t *testing.T){
bc:= NewBlockchain([]string{"ammar","ali", "james"})
tx,err:=bc.NewTransfertTX("ali","ammar", 5)
tx1,err1:=bc.NewTransfertTX("ali","james", 5)
tx2,err2:=bc.NewTransfertTX("ammar","ali", 10)
bc.AddBlock([]*Transaction{tx, tx1, tx2})
if err !=nil || err1 !=nil || err2 !=nil || bc.GetBalance("ammar")!=5 || bc.GetBalance("ali")!=10 || bc.GetBalance("james")!=15{
t.Error("TestSuccTransfert failed")
}
}
func TestImpossibleTransfert(t *testing.T){
var test_note=2.0
bc:= NewBlockchain([]string{"ammar","ali"})
tx,err:=bc.NewTransfertTX("ali","ammar", 15)
if tx!=nil {
bc.AddBlock([]*Transaction{tx})
}
if !errors.Is(err,ErrInsufficientFunds) || bc.GetBalance("ammar")!=10 || bc.GetBalance("ali")!=10{
t.Error("TestSuccTransfert failed")
test_note=test_note-2
}
note=note+test_note
}
func TestMain(m *testing.M) {
m.Run()
fmt.Println("\n===================================")
fmt.Println("======== Résumé ===============")
fmt.Printf(" Note actuelle : %2.2f/17\n",note)
fmt.Println("===================================")
}