This repository has been archived by the owner on Mar 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathbios.go
602 lines (491 loc) · 14.5 KB
/
bios.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
package bios
import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
"github.com/eoscanada/eos-go"
"github.com/eoscanada/eos-go/ecc"
)
type BIOS struct {
Log *Logger
CachePath string
TargetNetAPI *eos.API
Snapshot Snapshot
BootSequenceFile string
BootSequence *BootSeq
WriteActions bool
HackVotingAccounts bool
ReuseGenesis bool
Genesis *GenesisJSON
EphemeralPrivateKey *ecc.PrivateKey
EphemeralPublicKey ecc.PublicKey
}
func NewBIOS(logger *Logger, cachePath string, targetAPI *eos.API) *BIOS {
b := &BIOS{
CachePath: cachePath,
TargetNetAPI: targetAPI,
Log: logger,
}
return b
}
func (b *BIOS) Boot() error {
bootSeq, err := ReadBootSeq(b.BootSequenceFile)
if err != nil {
return err
}
b.BootSequence = bootSeq
if err := b.DownloadReferences(); err != nil {
return err
}
b.Log.Println("START BOOT SEQUENCE...")
var genesisData string
var pubKey ecc.PublicKey
var privKey string
err = b.setEphemeralKeypair()
if err != nil {
return err
}
pubKey = b.EphemeralPublicKey
privKey = b.EphemeralPrivateKey.String()
if b.ReuseGenesis {
genesisData, err = b.LoadGenesisFromFile(pubKey.String())
if err != nil {
return err
}
} else {
genesisData = b.GenerateGenesisJSON(pubKey.String())
b.writeToFile("genesis.pub", pubKey.String())
b.writeToFile("genesis.key", privKey)
}
// Don't get `get_required_keys` from the blockchain, this adds
// latency.. and we KNOW the key you're going to ask :) It's the
// only key we're going to sign with anyway..
b.TargetNetAPI.SetCustomGetRequiredKeys(func(tx *eos.Transaction) (out []ecc.PublicKey, err error) {
return append(out, pubKey), nil
})
// Store keys in wallet, to sign `SetCode` and friends..
if err := b.TargetNetAPI.Signer.ImportPrivateKey(privKey); err != nil {
return fmt.Errorf("ImportWIF: %s", err)
}
if err := b.writeAllActionsToDisk(); err != nil {
return fmt.Errorf("writing actions to disk: %s", err)
}
if err := b.DispatchBootNode(genesisData, pubKey.String(), privKey); err != nil {
return fmt.Errorf("dispatch boot_node hook: %s", err)
}
b.pingTargetNetwork()
b.Log.Println("In-memory keys:")
memkeys, _ := b.TargetNetAPI.Signer.AvailableKeys()
for _, key := range memkeys {
b.Log.Printf("- %s\n", key.String())
}
b.Log.Println("")
//eos.Debug = true
for _, step := range b.BootSequence.BootSequence {
b.Log.Printf("%s [%s] ", step.Label, step.Op)
acts, err := step.Data.Actions(b)
if err != nil {
return fmt.Errorf("getting actions for step %q: %s", step.Op, err)
}
if len(acts) != 0 {
for idx, chunk := range ChunkifyActions(acts) {
err := Retry(25, time.Second, func() error {
_, err := b.TargetNetAPI.SignPushActions(chunk...)
if err != nil {
b.Log.Printf("r")
b.Log.Debugf("error pushing transaction for step %q, chunk %d: %s\n", step.Op, idx, err)
return fmt.Errorf("push actions for step %q, chunk %d: %s", step.Op, idx, err)
}
return nil
})
if err != nil {
b.Log.Printf(" failed\n")
return err
}
b.Log.Printf(".")
}
b.Log.Printf(" done\n")
}
}
b.Log.Println("Waiting 2 seconds for transactions to flush to blocks")
time.Sleep(2 * time.Second)
// FIXME: don't do chain validation here..
isValid, err := b.RunChainValidation()
if err != nil {
return fmt.Errorf("chain validation: %s", err)
}
if !isValid {
b.Log.Println("WARNING: chain invalid, destroying network if possible")
os.Exit(0)
}
return nil
}
func (b *BIOS) setEphemeralKeypair() error {
if _, ok := b.BootSequence.Keys["ephemeral"]; ok {
cnt := b.BootSequence.Keys["ephemeral"]
privKey, err := ecc.NewPrivateKey(strings.TrimSpace(string(cnt)))
if err != nil {
return fmt.Errorf("unable to correctly decode ephemeral private key %q: %s", cnt, err)
}
b.EphemeralPrivateKey = privKey
b.EphemeralPublicKey = privKey.PublicKey()
b.logEphemeralKey("Using user provider custom ephemeral keys from boot sequence")
} else if b.ReuseGenesis {
genesisPrivateKey, err := readPrivKeyFromFile("genesis.key")
if err != nil {
return err
}
b.EphemeralPrivateKey = genesisPrivateKey
b.EphemeralPublicKey = genesisPrivateKey.PublicKey()
b.logEphemeralKey("REUSING previously generated ephemeral keys from genesis")
} else {
ephemeralPrivateKey, err := b.GenerateEphemeralPrivKey()
if err != nil {
return err
}
b.EphemeralPrivateKey = ephemeralPrivateKey
b.EphemeralPublicKey = ephemeralPrivateKey.PublicKey()
b.logEphemeralKey("Generated ephemeral keys")
}
return nil
}
func (b *BIOS) logEphemeralKey(tag string) {
pubKey := b.EphemeralPublicKey.String()
privKey := b.EphemeralPrivateKey.String()
b.Log.Printf("%s:\n\n\tPublic key: %s\n\tPrivate key: %s..%s\n\n", tag, pubKey, privKey[:4], privKey[len(privKey)-4:])
}
func (b *BIOS) RunChainValidation() (bool, error) {
bootSeqMap := ActionMap{}
bootSeq := []*eos.Action{}
for _, step := range b.BootSequence.BootSequence {
acts, err := step.Data.Actions(b)
if err != nil {
return false, fmt.Errorf("validating: getting actions for step %q: %s", step.Op, err)
}
for _, stepAction := range acts {
if stepAction == nil {
continue
}
stepAction.SetToServer(true)
data, err := eos.MarshalBinary(stepAction)
if err != nil {
return false, fmt.Errorf("validating: binary marshalling: %s", err)
}
key := sha2(data)
// if _, ok := bootSeqMap[key]; ok {
// // TODO: don't fatal here plz :)
// log.Fatalf("Same action detected twice [%s] with key [%s]\n", stepAction.Name, key)
// }
bootSeqMap[key] = stepAction
bootSeq = append(bootSeq, stepAction)
}
}
err := b.validateTargetNetwork(bootSeqMap, bootSeq)
if err != nil {
b.Log.Printf("BOOT SEQUENCE VALIDATION FAILED:\n%s", err)
return false, nil
}
b.Log.Println("")
b.Log.Println("All good! Chain validation succeeded!")
b.Log.Println("")
return true, nil
}
func (b *BIOS) writeAllActionsToDisk() error {
if !b.WriteActions {
b.Log.Println("Not writing actions to 'actions.jsonl'. Activate with --write-actions")
return nil
}
b.Log.Println("Writing all actions to 'actions.jsonl'...")
fl, err := os.Create("actions.jsonl")
if err != nil {
return err
}
defer fl.Close()
for _, step := range b.BootSequence.BootSequence {
acts, err := step.Data.Actions(b)
if err != nil {
return fmt.Errorf("fetch step %q: %s", step.Op, err)
}
for _, stepAction := range acts {
if stepAction == nil {
continue
}
stepAction.SetToServer(false)
data, err := json.Marshal(stepAction)
if err != nil {
return fmt.Errorf("binary marshalling: %s", err)
}
_, err = fl.Write(data)
if err != nil {
return err
}
_, _ = fl.Write([]byte("\n"))
}
}
return nil
}
type ActionMap map[string]*eos.Action
type ValidationError struct {
Err error
BlockNumber int
Action *eos.Action
RawAction []byte
Index int
ActionHexData string
PackedTransaction *eos.PackedTransaction
}
func (e ValidationError) Error() string {
s := fmt.Sprintf("Action [%d][%s::%s] absent from blocks\n", e.Index, e.Action.Account, e.Action.Name)
data, err := json.Marshal(e.Action)
if err != nil {
s += fmt.Sprintf(" json generation err: %s\n", err)
} else {
s += fmt.Sprintf(" json data: %s\n", string(data))
}
s += fmt.Sprintf(" hex data: %s\n", hex.EncodeToString(e.RawAction))
s += fmt.Sprintf(" error: %s\n", e.Err.Error())
return s
}
type ValidationErrors struct {
Errors []error
}
func (v ValidationErrors) Error() string {
s := ""
for _, err := range v.Errors {
s += ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"
s += err.Error()
s += "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"
}
return s
}
func (b *BIOS) pingTargetNetwork() {
b.Log.Printf("Pinging target node at %q...", b.TargetNetAPI.BaseURL)
for {
info, err := b.TargetNetAPI.GetInfo()
if err != nil {
b.Log.Debugf("target node error: %s\n", err)
b.Log.Printf("e")
time.Sleep(1 * time.Second)
continue
}
if info.HeadBlockNum < 2 {
b.Log.Debugln("target node: still no blocks in")
b.Log.Printf(".")
time.Sleep(1 * time.Second)
continue
}
break
}
b.Log.Println(" touchdown!")
}
func (b *BIOS) validateTargetNetwork(bootSeqMap ActionMap, bootSeq []*eos.Action) (err error) {
expectedActionCount := len(bootSeq)
validationErrors := make([]error, 0)
b.pingTargetNetwork()
// TODO: wait for target network to be up, and responding...
b.Log.Println("Pulling blocks from chain until we gathered all actions to validate:")
blockHeight := 1
actionsRead := 0
seenMap := map[string]bool{}
gotSomeTx := false
backOff := false
timeBetweenFetch := time.Duration(0)
var timeLastNotFound time.Time
for {
time.Sleep(timeBetweenFetch)
m, err := b.TargetNetAPI.GetBlockByNum(uint32(blockHeight))
if err != nil {
if gotSomeTx && !backOff {
backOff = true
timeBetweenFetch = 500 * time.Millisecond
timeLastNotFound = time.Now()
time.Sleep(2000 * time.Millisecond)
continue
}
b.Log.Debugln("Failed getting block num from target api:", err)
b.Log.Printf("e")
time.Sleep(1 * time.Second)
continue
} else {
b.Log.Printf(".\n")
}
blockHeight++
b.Log.Printf("Receiving block height=%d producer=%s transactions=%d\n", m.BlockNumber(), m.Producer, len(m.Transactions))
if !gotSomeTx && len(m.Transactions) > 2 {
gotSomeTx = true
}
if !timeLastNotFound.IsZero() && timeLastNotFound.Before(time.Now().Add(-10*time.Second)) {
b.flushMissingActions(seenMap, bootSeq)
}
for _, receipt := range m.Transactions {
unpacked, err := receipt.Transaction.Packed.Unpack()
if err != nil {
b.Log.Println("WARNING: Unable to unpack transaction, won't be able to fully validate:", err)
return fmt.Errorf("unpack transaction failed")
}
for _, act := range unpacked.Actions {
act.SetToServer(false)
data, err := eos.MarshalBinary(act)
if err != nil {
b.Log.Printf("Error marshalling an action: %s\n", err)
validationErrors = append(validationErrors, ValidationError{
Err: err,
BlockNumber: 1, // extract from the block transactionmroot
PackedTransaction: receipt.Transaction.Packed,
Action: act,
RawAction: data,
ActionHexData: hex.EncodeToString(act.HexData),
Index: actionsRead,
})
return err
}
key := sha2(data) // TODO: compute a hash here..
b.Log.Printf("- Validating action %d/%d [%s::%s]", actionsRead+1, expectedActionCount, act.Account, act.Name)
if _, ok := bootSeqMap[key]; !ok {
validationErrors = append(validationErrors, ValidationError{
Err: errors.New("not found"),
BlockNumber: 1, // extract from the block transactionmroot
PackedTransaction: receipt.Transaction.Packed,
Action: act,
RawAction: data,
ActionHexData: hex.EncodeToString(act.HexData),
Index: actionsRead,
})
b.Log.Printf(" INVALID ***************************** INVALID *************.\n")
} else {
seenMap[key] = true
b.Log.Printf(" valid.\n")
}
actionsRead++
}
}
if actionsRead == len(bootSeq) {
break
}
}
if len(validationErrors) > 0 {
return ValidationErrors{Errors: validationErrors}
}
return nil
}
func (b *BIOS) flushMissingActions(seenMap map[string]bool, bootSeq []*eos.Action) {
fl, err := os.Create("missing_actions.jsonl")
if err != nil {
fmt.Println("Couldn't write to `missing_actions.jsonl`:", err)
return
}
defer fl.Close()
// TODO: print all actions that are still MISSING to `missing_actions.jsonl`.
b.Log.Println("Flushing unseen transactions to `missing_actions.jsonl` up until this point.")
for _, act := range bootSeq {
act.SetToServer(true)
data, _ := eos.MarshalBinary(act)
key := sha2(data)
if !seenMap[key] {
act.SetToServer(false)
data, _ := json.Marshal(act)
fl.Write(data)
fl.Write([]byte("\n"))
}
}
}
func (b *BIOS) inputGenesisData() (genesis *GenesisJSON) {
b.Log.Println("")
for {
b.Log.Printf("Please input the genesis data of the network you want to join: ")
genesisData, err := ScanSingleLine()
if err != nil {
b.Log.Println("error reading:", err)
continue
}
err = json.Unmarshal([]byte(genesisData), &genesis)
if err != nil {
b.Log.Printf("Invalid genesis data: %s\n", err)
continue
}
return
}
}
func (b *BIOS) GenerateEphemeralPrivKey() (*ecc.PrivateKey, error) {
return ecc.NewRandomPrivateKey()
}
func (b *BIOS) GenerateGenesisJSON(pubKey string) string {
// known not to fail
cnt, _ := json.Marshal(&GenesisJSON{
InitialTimestamp: time.Now().UTC().Format("2006-01-02T15:04:05"),
InitialKey: pubKey,
})
return string(cnt)
}
func (b *BIOS) LoadGenesisFromFile(pubkey string) (string, error) {
cnt, err := ioutil.ReadFile("genesis.json")
if err != nil {
return "", err
}
var gendata *GenesisJSON
err = json.Unmarshal(cnt, &gendata)
if err != nil {
return "", err
}
if pubkey != gendata.InitialKey {
return "", fmt.Errorf("attempting to reuse genesis.json: genesis.key doesn't match genesis.json")
}
out, _ := json.Marshal(gendata)
return string(out), nil
}
func (b *BIOS) GetContentsCacheRef(filename string) (string, error) {
for _, fl := range b.BootSequence.Contents {
if fl.Name == filename {
return fl.URL, nil
}
}
return "", fmt.Errorf("%q not found in target contents", filename)
}
func ChunkifyActions(actions []*eos.Action) (out [][]*eos.Action) {
currentChunk := []*eos.Action{}
for _, act := range actions {
if act == nil {
if len(currentChunk) != 0 {
out = append(out, currentChunk)
}
currentChunk = []*eos.Action{}
} else {
currentChunk = append(currentChunk, act)
}
}
if len(currentChunk) > 0 {
out = append(out, currentChunk)
}
return
}
func accountVariation(acct eos.AccountName, variation int) eos.AccountName {
name := string(acct)
if len(name) > 11 {
name = name[:11]
}
variedName := name + string([]byte{'a' + byte(variation-1)})
return eos.AccountName(variedName)
}
func readPrivKeyFromFile(filename string) (*ecc.PrivateKey, error) {
cnt, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
strCnt := strings.TrimSpace(string(cnt))
return ecc.NewPrivateKey(strCnt)
}
func (b *BIOS) writeToFile(filename, content string) {
fl, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
b.Log.Println("Unable to write to file", filename, err)
return
}
defer fl.Close()
fl.Write([]byte(content))
b.Log.Printf("Wrote file %q\n", filename)
}