forked from asonnino/fraudproofs-prototype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fraudproof_test.go
291 lines (246 loc) · 7.3 KB
/
fraudproof_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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package fraudproofs
import (
"bytes"
//"crypto/sha256"
//"github.com/minio/sha256-simd"
"crypto/sha512"
"fmt"
"github.com/NebulousLabs/merkletree"
"github.com/jinzhu/copier"
"github.com/musalbas/smt"
"math/rand"
"testing"
"time"
)
func TestTransaction(test *testing.T) {
// create good transaction
_, err := NewTransaction(generateCorruptedTransactionInput())
if err == nil {
test.Error("should return an error")
}
// create bad transaction
goodT, err := NewTransaction(generateTransactionInput())
if err != nil {
test.Error(err)
}
// serialize and deserialize
buff := goodT.Serialize()
t, err := Deserialize(buff)
if err != nil {
test.Error(err)
} else if bytes.Compare(t.Serialize(), buff) != 0 {
test.Error("transaction not serialized and deserialize correctly")
}
}
func TestBlock(test *testing.T) {
// create bad block (corrupted transactions)
_, err := NewBlock(generateCorruptedBlockInput())
if err == nil {
test.Error("should return an error")
}
// create good block
goodTransaction, stateTree := generateBlockInput()
goodBlock, err := NewBlock(goodTransaction, stateTree)
if err != nil {
test.Error(err)
}
// check good block
_, err = goodBlock.CheckBlock(stateTree)
if err != nil {
test.Error(err)
}
// check a bad block (corrupted transactions)
badBlock := generateBlockWithCorruptedTransactions()
_, err = badBlock.CheckBlock(stateTree)
if err == nil {
test.Error("should return an error")
}
// check bad block (corrupted intermediate state)
badBlock = corruptBlockInterStates(goodBlock)
goodFp, err := badBlock.CheckBlock(stateTree)
if err != nil {
test.Error(err)
} else if goodFp == nil {
test.Error("should return a fraud proof")
}
// verify fraud proof of bad block
ret := badBlock.VerifyFraudProof(*goodFp)
if ret != true {
test.Error("fraud proof does not check")
}
// verify corrupted fraud proof (corrupted chunks proof)
corruptedFp := corruptFraudproofChunks(goodFp)
ret = badBlock.VerifyFraudProof(*corruptedFp)
if ret != false {
test.Error("invalid fraud proof should not check")
}
// verify corrupted fraud proof (corrupted state proof)
corruptedFp = corruptFraudproofState(goodFp)
ret = badBlock.VerifyFraudProof(*corruptedFp)
if ret != false {
test.Error("invalid fraud proof should not check")
}
}
func TestBlockchain(test *testing.T) {
// add good blocks to blockchain
blockchain := NewBlockchain()
goodBlock, _ := NewBlock(generateBlockInput())
blockchain.Append(goodBlock) // add a first block
fp, err := blockchain.Append(goodBlock) // add a second block
if err != nil {
test.Error(err)
} else if fp != nil {
test.Error("should not return a fraud proof")
}
// add bad block to blockchain (corrupted intermediate state)
fp, err = blockchain.Append(corruptBlockInterStates(goodBlock))
if err != nil {
test.Error(err)
} else if fp == nil {
test.Error("should return a fraud proof")
}
// add bad block to blockchain (corrupted transactions)
_, err = blockchain.Append(generateBlockWithCorruptedTransactions())
if err == nil {
test.Error("should return an error")
}
}
func TestTiming(test *testing.T) {
// create good block
goodTransaction, stateTree := generateBlockInput()
goodBlock, err := NewBlock(goodTransaction, stateTree)
if err != nil {
test.Error(err)
}
// check bad block (corrupted intermediate state)
goodBlock = corruptBlockInterStates(goodBlock)
start := time.Now()
goodFp, err := goodBlock.CheckBlock(stateTree)
t := time.Now()
elapsed := t.Sub(start)
fmt.Println("generate proof: ", elapsed)
if err != nil {
test.Error(err)
} else if goodFp == nil {
test.Error("should return a fraud proof")
}
// verify fraud proof of bad block
start = time.Now()
ret := goodBlock.VerifyFraudProof(*goodFp)
t = time.Now()
elapsed = t.Sub(start)
fmt.Println("verify proof: ", elapsed)
if ret != true {
test.Error("fraud proof does not check")
}
}
// ------------------ helpers ------------------ //
func generateTransactionInput() ([][]byte, [][]byte, [][]byte, [][]byte, [][]byte, []byte) {
var writeKeys, newData, oldData, readKeys, readData [][]byte
// average Ethereum transaction size (225B)
const numWriteKeys = 1
const numReadKeys = numWriteKeys
const sizeKeys = 32
const sizeData = 49
for i := 0; i < numWriteKeys; i++ {
token := make([]byte, sizeKeys)
//rand.Read(token)
for j := 0; j < len(token); j++ {
token[j] = 1
}
writeKeys = append(writeKeys, token)
token = make([]byte, sizeData)
//rand.Read(token)
for j := 0; j < len(token); j++ {
token[j] = 2
}
newData = append(newData, token)
token = make([]byte, sizeData)
//rand.Read(token)
for j := 0; j < len(token); j++ {
token[j] = 3
}
oldData = append(oldData, token)
}
for i := 0; i < numReadKeys; i++ {
token := make([]byte, sizeKeys)
rand.Read(token)
//fmt.Println(len(token), token)
//for j := 0; j < len(token); j++ {
// token[j] = byte(i)
//}
//fmt.Println(len(token), token)
readKeys = append(readKeys, token)
token = make([]byte, sizeData)
//rand.Read(token)
for j := 0; j < len(token); j++ {
token[j] = 5
}
//fmt.Println(token)
readData = append(readData, token)
}
return writeKeys, newData, oldData, readKeys, readData, []byte{}
}
func generateCorruptedTransactionInput() ([][]byte, [][]byte, [][]byte, [][]byte, [][]byte, []byte) {
writeKeys, newData, oldData, readKeys, readData, arbitrary := generateTransactionInput()
writeKeys = writeKeys[1:]
return writeKeys, newData, oldData, readKeys, readData, arbitrary
}
func corruptTransaction(t *Transaction) (*Transaction) {
t.writeKeys = t.writeKeys[1:]
return t
}
func generateBlockInput() ([]Transaction, *smt.SparseMerkleTree) {
// average Ethereum transactions per block (if block of 1MB)
const numTransactions = 4444 // 4444
t := make([]Transaction, numTransactions)
for i := 0; i < len(t); i++ {
tmp, _ := NewTransaction(generateTransactionInput())
t[i] = *tmp
}
stateTree := smt.NewSparseMerkleTree(smt.NewSimpleMap(), sha512.New512_256())
return t, stateTree
}
func generateCorruptedBlockInput() ([]Transaction, *smt.SparseMerkleTree) {
t1, _ := NewTransaction(generateTransactionInput())
t2, _ := NewTransaction(generateTransactionInput())
t1 = corruptTransaction(t1)
stateTree := smt.NewSparseMerkleTree(smt.NewSimpleMap(), sha512.New512_256())
return []Transaction{*t1,*t2}, stateTree
}
func generateBlockWithCorruptedTransactions() (*Block) {
block, _ := NewBlock(generateBlockInput())
t := block.transactions[0]
block.transactions[0] = *corruptTransaction(&t)
return block
}
func corruptBlockInterStates(b *Block) (*Block) {
h := sha512.New512_256()
h.Write([]byte("random"))
b.interStateRoots[0] = h.Sum(nil)
dataTree := merkletree.New(sha512.New512_256())
dataRoot, _ := fillDataTree(b.transactions, b.interStateRoots, dataTree)
return &Block{
dataRoot,
b.stateRoot,
b.transactions,
nil,
dataTree,
b.interStateRoots}
}
func corruptFraudproofChunks(fp *FraudProof) (*FraudProof) {
copyFp := &FraudProof{}
copier.Copy(copyFp, fp)
h := sha512.New512_256()
h.Write([]byte("random"))
copyFp.proofChunks[0] = [][]byte{h.Sum(nil), h.Sum(nil)}
return copyFp
}
func corruptFraudproofState(fp *FraudProof) (*FraudProof) {
copyFp := &FraudProof{}
copier.Copy(copyFp, fp)
h := sha512.New512_256()
h.Write([]byte("random"))
copyFp.proofState[0] = [][]byte{h.Sum(nil), h.Sum(nil)}
return copyFp
}