-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathboot.go
331 lines (286 loc) · 8.36 KB
/
boot.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
package boot
import (
"context"
"crypto/sha256"
"fmt"
"os"
"strings"
"time"
"github.com/dfuse-io/eosio-boot/config"
"github.com/dfuse-io/eosio-boot/content"
"github.com/dfuse-io/eosio-boot/ops"
"github.com/dfuse-io/eosio-boot/snapshot"
"github.com/eoscanada/eos-go"
"github.com/eoscanada/eos-go/ecc"
"github.com/eoscanada/eos-go/system"
"go.uber.org/zap"
)
type Option interface {
apply(b *Boot)
}
type funcOption func(b *Boot)
func (f funcOption) apply(b *Boot) {
f(b)
}
var traceEnable = false
func init() {
traceEnable = os.Getenv("TRACE") == "true"
}
func WithMaxActionCountPerTrx(max int) Option {
return funcOption(func(b *Boot) {
b.maxActionCountPerTrx = max
})
}
func WithKeyBag(keyBag *eos.KeyBag) Option {
return funcOption(func(b *Boot) {
b.keyBag = keyBag
})
}
func WithLogger(logger *zap.Logger) Option {
return funcOption(func(b *Boot) {
b.logger = logger
b.contentManager.SetLogger(logger)
})
}
func WithHackVotingActions() Option {
return funcOption(func(b *Boot) {
b.hackVotingAccounts = true
})
}
type Boot struct {
Snapshot snapshot.Snapshot
bootSequencePath string
targetNetAPI *eos.API
bootstrappingEnabled bool
genesisPath string
bootSequence *BootSeq
contentManager *content.Manager
keyBag *eos.KeyBag
bootseqKeys map[string]*ecc.PrivateKey
maxActionCountPerTrx int
hackVotingAccounts bool
logger *zap.Logger
}
func New(bootSequencePath string, targetAPI *eos.API, cachePath string, opts ...Option) (b *Boot, err error) {
b = &Boot{
targetNetAPI: targetAPI,
bootSequencePath: bootSequencePath,
contentManager: content.NewManager(cachePath),
maxActionCountPerTrx: 500,
bootseqKeys: map[string]*ecc.PrivateKey{},
logger: zap.NewNop(),
}
for _, opt := range opts {
opt.apply(b)
}
b.bootSequence, err = readBootSeq(b.bootSequencePath)
if err != nil {
return nil, err
}
return b, nil
}
func (b *Boot) Revision() string {
return b.bootSequence.Checksum
}
func (b *Boot) getBootseqKey(label string) (*ecc.PrivateKey, error) {
if _, found := b.bootseqKeys[label]; found {
return b.bootseqKeys[label], nil
}
return nil, fmt.Errorf("bootseq does not contain key with label %q", label)
}
func (b *Boot) Run() (checksums string, err error) {
ctx := context.Background()
b.logger.Debug("parsing boot sequence keys")
if err := b.parseBootseqKeys(); err != nil {
return "", fmt.Errorf("unable to parse boot sequence: %w", err)
}
b.logger.Debug("downloading references")
if err := b.contentManager.Download(b.bootSequence.Contents); err != nil {
return "", fmt.Errorf("unable to download content references: %w", err)
}
b.logger.Debug("setting boot keys")
if err := b.setKeys(); err != nil {
return "", fmt.Errorf("unable to set boot keys: %w", err)
}
if err := b.attachKeysOnTargetNode(ctx); err != nil {
return "", fmt.Errorf("unable to attach keys on target node: %w", err)
}
// We need to wait for target node to be up prior calling the get producer protocol features below
b.waitTargetNodeToBeUp()
features, err := b.targetNetAPI.GetProducerProtocolFeatures(ctx)
if err != nil {
return "", fmt.Errorf("unable to get producer protocol features: %w", err)
}
opConfig := config.NewOpConfig(
b.bootSequence.Contents,
b.contentManager,
b.bootseqKeys,
b.targetNetAPI,
features,
b.logger,
)
trxEventCh := make(chan interface{}, 500)
go func() {
defer close(trxEventCh)
for _, step := range b.bootSequence.BootSequence {
b.logger.Info("executing bootseq op",
zap.String("label", step.Label),
zap.String("op", step.Op),
zap.String("signer", step.Signer),
zap.Bool("skip_validation", step.SkipValidation),
)
pubkey, err := b.getOpPubkey(step)
if err != nil {
b.logger.Error("unable to get public key for operation", zap.Error(err))
return
}
err = step.Data.Actions(pubkey, opConfig, trxEventCh)
if err != nil {
b.logger.Error("unable to get actions for step", zap.String("ops", step.Op), zap.Error(err))
return
}
}
}()
index := 0
for {
index++
trxBundle := b.chunkifyActionChan(trxEventCh)
if trxBundle == nil {
// chunkify exited without given any chunks, channel must be closed
break
}
if len(trxBundle.actions) == 0 {
// nothing to execute skip
continue
}
str := []string{}
for _, t := range trxBundle.actions {
str = append(str, fmt.Sprintf("%s:%s", t.Account, t.Name))
}
b.logger.Debug("pushing transaction",
zap.Int("index", index),
zap.Int("action_count", len(trxBundle.actions)),
zap.String("actions", strings.Join(str, ", ")),
)
b.targetNetAPI.SetCustomGetRequiredKeys(func(ctx context.Context, tx *eos.Transaction) (out []ecc.PublicKey, err error) {
out = append(out, trxBundle.signer)
return out, nil
})
err := Retry(25, time.Second, func() error {
_, err := b.targetNetAPI.SignPushActions(ctx, trxBundle.actions...)
if err != nil {
b.logger.Error("error pushing transaction bundle",
zap.Error(err),
zap.Int("index", index),
)
if traceEnable {
trxBundle.debugPrint(b.logger)
}
return fmt.Errorf("push actions of transaciton bundle: %w", err)
}
return nil
})
if err != nil {
b.logger.Error("failed to push transaction bundle", zap.Error(err))
return "", err
}
}
b.logger.Info("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(opConfig)
if err != nil {
return "", fmt.Errorf("chain validation: %s", err)
}
if !isValid {
b.logger.Info("WARNING: chain invalid, destroying network if possible")
os.Exit(0)
}
return b.bootSequence.Checksum, nil
}
type transactionBundle struct {
actions []*eos.Action
signer ecc.PublicKey
}
// helpful for debug puropses
func (t *transactionBundle) debugPrint(logger *zap.Logger) {
acts := []string{}
logger.Debug("transaction bundle dump start", zap.Int("action_count", len(t.actions)))
for _, action := range t.actions {
actionKey := fmt.Sprintf("%s:%s", action.Account, action.Name)
var str string
switch actionKey {
case "eosio:newaccount":
logger.Debug("action: new account",
zap.Reflect("account", (action.ActionData.Data).(system.NewAccount)),
)
case "eosio:setabi":
setABIAction := (action.ActionData.Data).(system.SetABI)
h := sha256.New()
h.Write(setABIAction.ABI)
logger.Debug("action: set abi",
zap.String("account", string(setABIAction.Account)),
zap.ByteString("abi_sha256", h.Sum(nil)),
)
case "eosio:setcode":
setCodeAction := (action.ActionData.Data).(system.SetCode)
h := sha256.New()
h.Write(setCodeAction.Code)
logger.Debug("action: set code",
zap.String("account", string(setCodeAction.Account)),
zap.ByteString("code_sha256", h.Sum(nil)),
)
case "eosio:linkauth":
logger.Debug("action: link auth code",
zap.Reflect("link_auth", (action.ActionData.Data).(system.LinkAuth)),
)
case "eosio:updateauth":
logger.Debug("action: update auth", zap.Reflect("update_auth", (action.ActionData.Data).(system.UpdateAuth)))
case "eosio:init":
initAction := (action.ActionData.Data).(system.Init)
logger.Debug("action: eosio init",
zap.Uint32("version", uint32(initAction.Version)),
zap.String("core", initAction.Core.String()),
)
}
acts = append(acts, str)
}
logger.Debug("transaction bundle dump end")
}
func (b *Boot) chunkifyActionChan(trxEventCh chan interface{}) *transactionBundle {
out := &transactionBundle{
actions: []*eos.Action{},
}
for {
if len(out.actions) > b.maxActionCountPerTrx {
return out
}
act, ok := <-trxEventCh
if !ok {
// channel is closed, there is not transaction to process
return nil
}
switch v := act.(type) {
case ops.TransactionBoundary:
out.signer = v.Signer
return out
case *ops.TransactionAction:
out.actions = append(out.actions, (*eos.Action)(v))
default:
panic(fmt.Sprintf("chunkify: unexpected type in action chan"))
}
}
}
func (b *Boot) getOpPubkey(op *ops.OperationType) (ecc.PublicKey, error) {
if op.Signer != "" {
if privKey, found := b.bootseqKeys[op.Signer]; found {
return privKey.PublicKey(), nil
}
return ecc.PublicKey{}, fmt.Errorf("cannot find private key in boot sequence with label %q", op.Signer)
}
pubKey, err := b.getBootKey()
if err != nil {
return ecc.PublicKey{}, err
}
return pubKey, nil
}