-
Notifications
You must be signed in to change notification settings - Fork 178
/
bootstrap.go
121 lines (94 loc) · 3 KB
/
bootstrap.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
package bootstrap
import (
"errors"
"fmt"
"github.com/dgraph-io/badger/v2"
"github.com/rs/zerolog"
"github.com/onflow/flow-go/engine/execution/state"
"github.com/onflow/flow-go/engine/execution/state/delta"
"github.com/onflow/flow-go/fvm"
"github.com/onflow/flow-go/fvm/programs"
"github.com/onflow/flow-go/ledger"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/storage"
"github.com/onflow/flow-go/storage/badger/operation"
)
type Bootstrapper struct {
logger zerolog.Logger
}
func NewBootstrapper(logger zerolog.Logger) *Bootstrapper {
return &Bootstrapper{
logger: logger,
}
}
// BootstrapLedger adds the above root account to the ledger and initializes execution node-only data
func (b *Bootstrapper) BootstrapLedger(
ledger ledger.Ledger,
servicePublicKey flow.AccountPublicKey,
chain flow.Chain,
opts ...fvm.BootstrapProcedureOption,
) (flow.StateCommitment, error) {
view := delta.NewView(state.LedgerGetRegister(ledger, ledger.InitialState()))
programs := programs.NewEmptyPrograms()
rt := fvm.NewInterpreterRuntime()
vm := fvm.NewVirtualMachine(rt)
ctx := fvm.NewContext(b.logger, fvm.WithChain(chain))
bootstrap := fvm.Bootstrap(
servicePublicKey,
opts...,
)
err := vm.Run(ctx, bootstrap, view, programs)
if err != nil {
return nil, err
}
newStateCommitment, err := state.CommitDelta(ledger, view.Delta(), ledger.InitialState())
if err != nil {
return nil, err
}
return newStateCommitment, nil
}
// IsBootstrapped returns whether the execution database has been bootstrapped, if yes, returns the
// root statecommitment
func (b *Bootstrapper) IsBootstrapped(db *badger.DB) (flow.StateCommitment, bool, error) {
var commit flow.StateCommitment
err := db.View(func(txn *badger.Txn) error {
err := operation.LookupStateCommitment(flow.ZeroID, &commit)(txn)
if err != nil {
return fmt.Errorf("could not lookup state commitment: %w", err)
}
return nil
})
if errors.Is(err, storage.ErrNotFound) {
return nil, false, nil
}
if err != nil {
return nil, false, err
}
return commit, true, nil
}
func (b *Bootstrapper) BootstrapExecutionDatabase(db *badger.DB, commit flow.StateCommitment, genesis *flow.Header) error {
err := operation.RetryOnConflict(db.Update, func(txn *badger.Txn) error {
err := operation.InsertExecutedBlock(genesis.ID())(txn)
if err != nil {
return fmt.Errorf("could not index initial genesis execution block: %w", err)
}
err = operation.IndexStateCommitment(flow.ZeroID, commit)(txn)
if err != nil {
return fmt.Errorf("could not index void state commitment: %w", err)
}
err = operation.IndexStateCommitment(genesis.ID(), commit)(txn)
if err != nil {
return fmt.Errorf("could not index genesis state commitment: %w", err)
}
views := make([]*delta.Snapshot, 0)
err = operation.InsertExecutionStateInteractions(genesis.ID(), views)(txn)
if err != nil {
return fmt.Errorf("could not bootstrap execution state interactions: %w", err)
}
return nil
})
if err != nil {
return err
}
return nil
}