-
Notifications
You must be signed in to change notification settings - Fork 42
/
genesis.go
700 lines (553 loc) · 17.3 KB
/
genesis.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
// Copyright (c) 2019 Perlin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package wavelet
import (
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
wasm "github.com/perlin-network/life/wasm-validation"
"github.com/perlin-network/wavelet/avl"
"github.com/perlin-network/wavelet/log"
"github.com/perlin-network/wavelet/sys"
"github.com/pkg/errors"
"github.com/valyala/bytebufferpool"
"github.com/valyala/fastjson"
)
const testingGenesis = `
{
"400056ee68a7cc2695222df05ea76875bc27ec6e61e8e62317c336157019c405": {
"balance": 10000000000000000000,
"reward": 5000000
},
"696937c2c8df35dba0169de72990b80761e51dd9e2411fa1fce147f68ade830a": {
"balance": 10000000000000000000,
"reward": 5000000
},
"f03bb6f98c4dfd31f3d448c7ec79fa3eaa92250112ada43471812f4b1ace6467": {
"balance": 10000000000000000000
}
}
`
const testnetGenesis = `
{
"0f569c84d434fb0ca682c733176f7c0c2d853fce04d95ae131d2f9b4124d93d8": {
"balance": 10000000000000000000
}
}
`
var defaultGenesis = testingGenesis
func SetGenesisByNetwork(name string) error {
switch name {
case "testnet":
defaultGenesis = testnetGenesis
case "testing":
defaultGenesis = testingGenesis
default:
return fmt.Errorf("invalid network: %s", name)
}
return nil
}
// performInception restore genesis data from the a directory or JSON contents expected to exist at the birth of any
// node in this ledgers network.
//
// An account state may be specified within the genesis directory in the form of a [account address].json file with key
// value pairs.
//
// A smart contract may be specified within the genesis directory in the form of a [contract address].wasm file with
// accompanying [contract address].[page index].dmp files representing the contracts memory pages.
//
// The AccountsLen in the restored tree may not match with the original tree.
//
// If the genesis is nil, restore from the hardcoded default genesis.
func performInception(tree *avl.Tree, genesis *string) Block {
logger := log.Node()
var err error
if genesis != nil {
isJSON := func(str string) bool {
var js json.RawMessage
return json.Unmarshal([]byte(str), &js) == nil
}
if isJSON(*genesis) {
err = restoreFromJSON(tree, []byte(*genesis))
} else {
err = restoreFromDir(tree, *genesis)
}
} else {
err = restoreFromJSON(tree, []byte(defaultGenesis))
}
if err != nil {
logger.Fatal().Err(err).Msg("genesis")
}
return NewBlock(0, tree.Checksum())
}
func restoreFromJSON(tree *avl.Tree, json []byte) error {
var p fastjson.Parser
parsed, err := p.ParseBytes(json)
if err != nil {
return errors.Wrapf(err, "failed to parse JSON")
}
accounts, err := parsed.Object()
if err != nil {
return errors.Wrapf(err, "failed to parse JSON Object")
}
set := make(map[AccountID]struct{}) // Ensure that there are no duplicate account entries in the JSON.
accounts.Visit(func(key []byte, val *fastjson.Value) {
if err != nil {
return
}
var id AccountID
var n int
n, err = hex.Decode(id[:], key)
if n != cap(id) && err == nil {
err = errors.Errorf("got an invalid account ID: %x", key)
return
}
if err != nil {
err = errors.Wrapf(err, "got an invalid account ID: %x", key)
return
}
if _, exists := set[id]; exists {
err = errors.Errorf("found duplicate entries for account ID %x in genesis file", id)
return
}
set[id] = struct{}{}
err = restoreAccount(tree, id, val)
})
return nil
}
func restoreContractGlobals(tree *avl.Tree, id TransactionID, path string) error {
globalsBuf := make([]byte, sys.ContractMaxGlobals)
f, err := os.Open(path)
if err != nil {
return errors.Wrapf(err, "failed to open contract globals file %s", path)
}
n, err := f.Read(globalsBuf)
if err != nil && err != io.EOF {
return errors.Wrapf(err, "failed to read contract globals %s", path)
}
if err == nil && n == cap(globalsBuf) {
return errors.Wrapf(err, "failed to read contract globals %s, buffer is not enough", path)
}
// This assumes WriteAccountContractGlobals will make a copy of the bytes.
WriteAccountContractGlobals(tree, id, globalsBuf[:n])
return nil
}
func restoreFromDir(tree *avl.Tree, dir string) error {
if _, err := os.Stat(dir); os.IsNotExist(err) {
return errors.Wrapf(err, "directory %s does not exist", dir)
}
accounts := make(map[AccountID]struct{})
contractPageFiles := make(map[TransactionID][]string)
// This is should only used to check for duplicates.
contractsExist := make(map[TransactionID]struct{})
pool := &bytebufferpool.Pool{}
var (
p fastjson.Parser
walletBuf [512]byte
contracts []TransactionID
)
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
// Nothing to do for directories.
return nil
}
ext := filepath.Ext(path)
switch ext {
case ".json":
return restoreFromDirJSON(tree, walletBuf, p, accounts, path)
case ".wasm":
return restoreFromDirWASM(tree, pool, &contracts, contractsExist, path)
case ".dmp":
return restoreFromDirDMP(tree, contractPageFiles, path)
}
return nil
})
if err != nil {
return err
}
// Each contract must have Code.
for _, id := range contracts {
if _, exist := ReadAccountContractCode(tree, id); !exist {
return errors.Errorf("contract %x is missing code", id)
}
}
return restoreContractPages(tree, contracts, contractPageFiles)
}
func restoreFromDirJSON(
tree *avl.Tree, walletBuf [512]byte, p fastjson.Parser, accounts map[AccountID]struct{}, path string,
) error {
filename := strings.TrimSuffix(filepath.Base(path), ".json")
var id AccountID
// filename is the id
if n, err := hex.Decode(id[:], []byte(filename)); n != cap(id) || err != nil {
if err != nil {
return errors.Wrapf(err, "filename of %s has invalid account ID", path)
}
return errors.Errorf("filename of %s has invalid account ID", path)
}
// Check for duplicate
if _, exist := accounts[id]; exist {
return nil
}
accounts[id] = struct{}{}
f, err := os.Open(path)
if err != nil {
return errors.Wrapf(err, "failed to open file %s", path)
}
n, err := f.Read(walletBuf[:])
if err != nil && err != io.EOF {
return errors.Wrapf(err, "failed to read wallet %s", path)
}
if err == nil && n == cap(walletBuf) {
return errors.Wrapf(err, "failed to read wallet %s, buffer is not enough", path)
}
val, err := p.ParseBytes(walletBuf[:n])
if err != nil {
return errors.Wrapf(err, "failed to parse file %s", path)
}
return restoreAccount(tree, id, val)
}
func restoreFromDirWASM(
tree *avl.Tree,
pool *bytebufferpool.Pool,
contracts *[]TransactionID,
contractsExist map[AccountID]struct{},
path string,
) error {
filename := strings.TrimSuffix(filepath.Base(path), ".wasm")
var id TransactionID
// filename is the id
if n, err := hex.Decode(id[:], []byte(filename)); n != cap(id) || err != nil {
if err != nil {
return errors.Wrapf(err, "filename of %s has invalid contract ID", path)
}
return errors.Errorf("filename of %s has invalid contract ID", path)
}
// Check for duplicate
if _, exist := contractsExist[id]; exist {
return nil
}
contractsExist[id] = struct{}{}
// Maybe instead of doing this, the function should return the contract id
// and have the caller append it to the contracts list
*contracts = append(*contracts, id)
f, err := os.Open(path)
if err != nil {
return errors.Wrapf(err, "failed to open file %s", path)
}
buf := pool.Get()
defer pool.Put(buf)
_, err = buf.ReadFrom(f)
if err != nil {
return errors.Wrapf(err, "failed to open file %s", path)
}
if err := wasm.GetValidator().ValidateWasm(buf.Bytes()); err != nil {
return errors.Wrapf(err, "file %s has invalid wasm", path)
}
WriteAccountContractCode(tree, id, buf.Bytes())
return nil
}
func restoreFromDirDMP(tree *avl.Tree, contractPageFiles map[TransactionID][]string, path string) error {
filename := strings.TrimSuffix(filepath.Base(path), ".dmp")
secondExt := filepath.Ext(filename)
var id TransactionID
// filename is the id
if n, err := hex.Decode(id[:], []byte(strings.TrimSuffix(filename, secondExt))); n != cap(id) || err != nil {
if err != nil {
return errors.Wrapf(err, "filename of %s has invalid contract ID", path)
}
return errors.Errorf("filename of %s has invalid contract ID", path)
}
// Restore contract globals.
if secondExt == ".globals" {
return restoreContractGlobals(tree, id, path)
}
// For contract pages, get all the contract pages first and group them by contract ID.
// This is to make sure, for each contract the pages are complete and there are no missing pages.
// Must at least contain a character. e.g. .1
if len(secondExt) < 2 {
return errors.Errorf("filename of %s has invalid name, expected index after contract address", path)
}
idx, err := strconv.Atoi(secondExt[1:])
if err != nil {
return errors.Wrapf(err, "filename of %s has invalid index, expected an unsigned integer", path)
}
if idx < 0 {
return errors.Errorf("filename of %s has invalid index, expected an unsigned integer", path)
}
files := contractPageFiles[id]
// Grow the slice according to the page idx
// The key of the slice is the page idx.
if i := idx - cap(files) + 1; i > 0 {
newFiles := make([]string, len(files)+i)
copy(newFiles, files)
contractPageFiles[id] = newFiles
files = newFiles
}
// Check if the page already exist
if len(files[idx]) > 0 {
return nil
}
files[idx] = path
return nil
}
func restoreAccount(tree *avl.Tree, id AccountID, val *fastjson.Value) error {
fields, err := val.Object()
if err != nil {
return err
}
var (
balance, stake, reward, gasBalance uint64
isContract bool
)
fields.Visit(func(key []byte, v *fastjson.Value) {
switch string(key) {
case "balance":
balance, err = v.Uint64()
if err != nil {
err = errors.Wrapf(err, "failed to cast type for key %q", key)
return
}
WriteAccountBalance(tree, id, balance)
case "stake":
stake, err = v.Uint64()
if err != nil {
err = errors.Wrapf(err, "failed to cast type for key %q", key)
return
}
WriteAccountStake(tree, id, stake)
case "reward":
reward, err = v.Uint64()
if err != nil {
err = errors.Wrapf(err, "failed to cast type for key %q", key)
return
}
WriteAccountReward(tree, id, reward)
case "gas_balance":
gasBalance, err = v.Uint64()
if err != nil {
err = errors.Wrapf(err, "failed to cast type for key %q", key)
return
}
WriteAccountContractGasBalance(tree, id, gasBalance)
case "is_contract":
isContract, err = v.Bool()
if err != nil {
err = errors.Wrapf(err, "failed to cast type for key %q", key)
return
}
}
})
if err != nil {
return err
}
if !isContract {
WriteAccountsLen(tree, ReadAccountsLen(tree)+1)
}
return nil
}
// For each contract, read all the pages into buffers first, and check for following conditions:
// 1. the file is successfully read into buffer,
// 2. the file size is either 0 or 65536,
//
// Considered success only if all the conditions are true, otherwise returns an error.
func restoreContractPages(
tree *avl.Tree, contracts []TransactionID, contractPageFiles map[TransactionID][]string,
) error {
pool := bytebufferpool.Pool{}
for _, id := range contracts {
files := contractPageFiles[id]
WriteAccountContractNumPages(tree, id, uint64(len(files)))
for i := range files {
file := files[i]
if len(file) == 0 {
return errors.Errorf("contract %x is missing page index %d", id, i)
}
buf := pool.Get()
f, err := os.Open(file)
if err != nil {
return errors.Wrapf(err, "failed to open contract page file %s", file)
}
n, err := buf.ReadFrom(f)
if err != nil {
return errors.Wrapf(err, "failed to read contract page file %s", file)
}
// Check page size
if n != 0 && n != PageSize {
return errors.Errorf(
"contract page file %s has invalid page size %d. must be 0 or %d", file, n, PageSize,
)
}
if buf.Len() == 0 {
continue
}
// This assumes WriteAccountContractPage will make a copy of the bytes.
WriteAccountContractPage(tree, id, uint64(i), buf.Bytes())
pool.Put(buf)
}
}
return nil
}
// Dump the ledger states from the tree into a directory.
// Existing files will be truncate.
//
// dir is the directory path which will be used to dump the states.
//
// isDumpContract is a flag which if true, the dump will include all the contracts
//
// useContractFolder is a flag which if true, each contract dump has it's own folder.
// The folder name will be hex-encoded of the contract ID.
func Dump(tree *avl.Tree, dir string, isDumpContract bool, useContractFolder bool) error {
// Make sure the directory exists
if err := os.MkdirAll(dir, 0700); err != nil {
return errors.Wrapf(err, "failed to create directory %s", dir)
}
type account struct {
balance *uint64
stake *uint64
reward *uint64
isContract bool
gasBalance *uint64
}
var (
filePerm os.FileMode = 0644
err error
)
accounts := make(map[AccountID]*account)
tree.IteratePrefix(keyAccounts[:], func(key, value []byte) bool {
var prefix [1]byte
copy(prefix[:], key)
// Filter by prefixes relevant to wallet and contract code.
if prefix != keyAccountBalance &&
prefix != keyAccountStake &&
prefix != keyAccountReward &&
prefix != keyAccountContractCode {
return true
}
var id AccountID
copy(id[:], key[1:])
if _, exist := accounts[id]; exist {
return true
}
_, isContract := ReadAccountContractCode(tree, id)
acc := &account{
isContract: isContract,
}
accounts[id] = acc
if !isDumpContract && acc.isContract {
return true
}
if balance, exist := ReadAccountBalance(tree, id); exist {
acc.balance = &balance
}
if stake, exist := ReadAccountStake(tree, id); exist {
acc.stake = &stake
}
if reward, exist := ReadAccountReward(tree, id); exist {
acc.reward = &reward
}
if gasBalance, exist := ReadAccountContractGasBalance(tree, id); exist {
acc.gasBalance = &gasBalance
}
var folder = dir
if useContractFolder {
folder = filepath.Join(dir, fmt.Sprintf("%x", id))
// Make sure the folder exists
if fErr := os.MkdirAll(folder, 0700); fErr != nil {
err = errors.Wrapf(fErr, "failed to create directory %s", dir)
return true
}
}
err = dumpContract(tree, id, folder)
return true
})
if err != nil {
return err
}
arena := &fastjson.Arena{}
data := make([]byte, 0, 512)
for id, v := range accounts {
if !isDumpContract && v.isContract {
continue
}
o := arena.NewObject()
if v.isContract {
o.Set("is_contract", arena.NewTrue())
} else {
o.Set("is_contract", arena.NewFalse())
}
if v.gasBalance != nil {
o.Set("gas_balance", arena.NewNumberString(strconv.FormatUint(*v.gasBalance, 10)))
}
if v.balance != nil {
o.Set("balance", arena.NewNumberString(strconv.FormatUint(*v.balance, 10)))
}
if v.stake != nil {
o.Set("stake", arena.NewNumberString(strconv.FormatUint(*v.stake, 10)))
}
if v.reward != nil {
o.Set("reward", arena.NewNumberString(strconv.FormatUint(*v.reward, 10)))
}
data = o.MarshalTo(data)
filename := fmt.Sprintf("%x.json", id)
err := ioutil.WriteFile(filepath.Join(dir, filename), data, filePerm)
if err != nil {
return errors.Wrapf(err, "failed to write %s", filename)
}
data = data[:0]
arena.Reset()
}
return nil
}
func dumpContract(tree *avl.Tree, id AccountID, dir string) error {
// Write contract globals.
if globals, ok := ReadAccountContractGlobals(tree, id); ok {
globalsFilename := fmt.Sprintf("%x.globals.dmp", id)
err := ioutil.WriteFile(filepath.Join(dir, globalsFilename), globals, 0644)
if err != nil {
return errors.Wrapf(err, "failed to write globals %s", globalsFilename)
}
}
// Write contract code.
if code, ok := ReadAccountContractCode(tree, id); ok {
wasmFilename := fmt.Sprintf("%x.wasm", id)
err := ioutil.WriteFile(filepath.Join(dir, wasmFilename), code, 0644)
if err != nil {
return errors.Wrapf(err, "failed to write wasm %s", wasmFilename)
}
}
// Write contract pages.
if numPages, ok := ReadAccountContractNumPages(tree, id); ok && numPages > 0 {
for i := uint64(0); i < numPages; i++ {
pageFilename := fmt.Sprintf("%x.%d.dmp", id, i)
page, _ := ReadAccountContractPage(tree, id, i)
err := ioutil.WriteFile(filepath.Join(dir, pageFilename), page, 0644)
if err != nil {
return errors.Wrapf(err, "failed to write page %s", pageFilename)
}
}
}
return nil
}