This repository was archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 344
/
Copy pathexecution.go
515 lines (469 loc) · 16.3 KB
/
execution.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
// Copyright Monax Industries Limited
// SPDX-License-Identifier: Apache-2.0
package execution
import (
"context"
"fmt"
"runtime/debug"
"sync"
"github.com/hyperledger/burrow/execution/vms"
"github.com/hyperledger/burrow/acm"
"github.com/hyperledger/burrow/acm/acmstate"
"github.com/hyperledger/burrow/acm/validator"
"github.com/hyperledger/burrow/binary"
"github.com/hyperledger/burrow/crypto"
"github.com/hyperledger/burrow/event"
"github.com/hyperledger/burrow/execution/contexts"
"github.com/hyperledger/burrow/execution/engine"
"github.com/hyperledger/burrow/execution/errors"
"github.com/hyperledger/burrow/execution/exec"
"github.com/hyperledger/burrow/execution/names"
"github.com/hyperledger/burrow/execution/proposal"
"github.com/hyperledger/burrow/execution/registry"
"github.com/hyperledger/burrow/execution/state"
"github.com/hyperledger/burrow/genesis"
"github.com/hyperledger/burrow/logging"
"github.com/hyperledger/burrow/logging/structure"
"github.com/hyperledger/burrow/permission"
"github.com/hyperledger/burrow/txs"
"github.com/hyperledger/burrow/txs/payload"
"github.com/tendermint/tendermint/proto/tendermint/types"
)
type Executor interface {
Execute(txEnv *txs.Envelope) (*exec.TxExecution, error)
}
type ExecutorFunc func(txEnv *txs.Envelope) (*exec.TxExecution, error)
func (f ExecutorFunc) Execute(txEnv *txs.Envelope) (*exec.TxExecution, error) {
return f(txEnv)
}
type ExecutorState interface {
Update(updater func(ws state.Updatable) error) (hash []byte, version int64, err error)
LastStoredHeight() (uint64, error)
acmstate.IterableReader
acmstate.MetadataReader
names.Reader
registry.Reader
proposal.Reader
validator.IterableReader
}
type BatchExecutor interface {
// Provides access to write lock for a BatchExecutor so reads can be prevented for the duration of a commit
sync.Locker
// Used by execution.Accounts to implement memory pool signing
acmstate.Reader
// Execute transaction against block cache (i.e. block buffer)
Executor
// Reset executor to underlying State
Reset() error
}
// Executes transactions
type BatchCommitter interface {
BatchExecutor
// Commit execution results to underlying State and provide opportunity to mutate state before it is saved
Commit(header *types.Header) (stateHash []byte, err error)
}
type executor struct {
sync.RWMutex
runCall bool
params Params
state ExecutorState
stateCache *acmstate.Cache
metadataCache *acmstate.MetadataCache
nameRegCache *names.Cache
nodeRegCache *registry.Cache
proposalRegCache *proposal.Cache
validatorCache *validator.Cache
emitter *event.Emitter
block *exec.BlockExecution
logger *logging.Logger
vmOptions engine.Options
contexts map[payload.Type]contexts.Context
}
type Params struct {
ChainID string
ProposalThreshold uint64
}
func ParamsFromGenesis(genesisDoc *genesis.GenesisDoc) Params {
return Params{
ChainID: genesisDoc.GetChainID(),
ProposalThreshold: genesisDoc.Params.ProposalThreshold,
}
}
var _ BatchExecutor = (*executor)(nil)
// Wraps a cache of what is variously known as the 'check cache' and 'mempool'
func NewBatchChecker(backend ExecutorState, params Params, blockchain engine.Blockchain, logger *logging.Logger,
options ...Option) (BatchExecutor, error) {
return newExecutor("CheckCache", false, params, backend, blockchain, nil,
logger.WithScope("NewBatchExecutor"), options...)
}
func NewBatchCommitter(backend ExecutorState, params Params, blockchain engine.Blockchain, emitter *event.Emitter,
logger *logging.Logger, options ...Option) (BatchCommitter, error) {
return newExecutor("CommitCache", true, params, backend, blockchain, emitter,
logger.WithScope("NewBatchCommitter"), options...)
}
func newExecutor(name string, runCall bool, params Params, backend ExecutorState, blockchain engine.Blockchain,
emitter *event.Emitter, logger *logging.Logger, options ...Option) (*executor, error) {
// We need to track the last block stored in state
predecessor, err := backend.LastStoredHeight()
if err != nil {
return nil, err
}
exe := &executor{
runCall: runCall,
params: params,
state: backend,
stateCache: acmstate.NewCache(backend, acmstate.Named(name)),
metadataCache: acmstate.NewMetadataCache(backend),
nameRegCache: names.NewCache(backend),
nodeRegCache: registry.NewCache(backend),
proposalRegCache: proposal.NewCache(backend),
validatorCache: validator.NewCache(backend),
emitter: emitter,
block: &exec.BlockExecution{
Height: blockchain.LastBlockHeight() + 1,
PredecessorHeight: predecessor,
},
logger: logger.With(structure.ComponentKey, "Executor"),
}
for _, option := range options {
option(exe)
}
baseContexts := map[payload.Type]contexts.Context{
payload.TypeCall: &contexts.CallContext{
// TODO: expose WASM options to config
VMS: vms.NewConnectedVirtualMachines(exe.vmOptions),
Blockchain: blockchain,
State: exe.stateCache,
MetadataState: exe.metadataCache,
RunCall: runCall,
Logger: exe.logger,
},
payload.TypeSend: &contexts.SendContext{
State: exe.stateCache,
Logger: exe.logger,
},
payload.TypeName: &contexts.NameContext{
Blockchain: blockchain,
State: exe.stateCache,
NameReg: exe.nameRegCache,
Logger: exe.logger,
},
payload.TypePermissions: &contexts.PermissionsContext{
State: exe.stateCache,
Logger: exe.logger,
},
payload.TypeGovernance: &contexts.GovernanceContext{
ValidatorSet: exe.validatorCache,
State: exe.stateCache,
Logger: exe.logger,
},
payload.TypeBond: &contexts.BondContext{
ValidatorSet: exe.validatorCache,
State: exe.stateCache,
Logger: exe.logger,
},
payload.TypeUnbond: &contexts.UnbondContext{
ValidatorSet: exe.validatorCache,
State: exe.stateCache,
Logger: exe.logger,
},
payload.TypeIdentify: &contexts.IdentifyContext{
NodeWriter: exe.nodeRegCache,
StateReader: exe.stateCache,
Logger: exe.logger,
},
}
exe.contexts = map[payload.Type]contexts.Context{
payload.TypeProposal: &contexts.ProposalContext{
ChainID: params.ChainID,
ProposalThreshold: params.ProposalThreshold,
State: exe.stateCache,
ProposalReg: exe.proposalRegCache,
Logger: exe.logger,
Contexts: baseContexts,
},
}
// Copy over base contexts
for k, v := range baseContexts {
exe.contexts[k] = v
}
return exe, nil
}
func (exe *executor) AddContext(ty payload.Type, ctx contexts.Context) *executor {
exe.contexts[ty] = ctx
return exe
}
// If the tx is invalid, an error will be returned.
// Unlike ExecBlock(), state will not be altered.
func (exe *executor) Execute(txEnv *txs.Envelope) (txe *exec.TxExecution, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("recovered from panic in executor.Execute(%s): %v\n%s", txEnv.String(), r,
debug.Stack())
}
}()
logger := exe.logger.WithScope("executor.Execute(tx txs.Tx)").With(
"height", exe.block.Height,
"run_call", exe.runCall,
structure.TxHashKey, txEnv.Tx.Hash())
logger.InfoMsg("Executing transaction", "tx", txEnv.String())
// Verify transaction signature against inputs
err = txEnv.Verify(exe.params.ChainID)
if err != nil {
logger.InfoMsg("Transaction Verify failed", structure.ErrorKey, err)
return nil, err
}
if txExecutor, ok := exe.contexts[txEnv.Tx.Type()]; ok {
// Establish new TxExecution
txe := exe.block.Tx(txEnv)
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("recovered from panic in executor.Execute(%s): %v\n%s", txEnv.String(), r,
debug.Stack())
}
}()
err = exe.validateInputsAndStorePublicKeys(txEnv)
if err != nil {
logger.InfoMsg("Transaction validate failed", structure.ErrorKey, err)
txe.PushError(err)
return nil, err
}
err = txExecutor.Execute(txe, txe.Envelope.Tx.Payload)
if err != nil {
logger.InfoMsg("Transaction execution failed", structure.ErrorKey, err)
txe.PushError(err)
return nil, err
}
// Increment sequence numbers for Tx inputs
err = exe.updateSequenceNumbers(txEnv)
if err != nil {
logger.InfoMsg("Updating sequences failed", structure.ErrorKey, err)
txe.PushError(err)
return nil, err
}
// Return execution for this tx
return txe, nil
}
return nil, fmt.Errorf("unknown transaction type: %v", txEnv.Tx.Type())
}
// Validate inputs, check sequence numbers and capture public keys
func (exe *executor) validateInputsAndStorePublicKeys(txEnv *txs.Envelope) error {
for s, in := range txEnv.Tx.GetInputs() {
err := exe.updateSignatory(txEnv.Signatories[s])
if err != nil {
return fmt.Errorf("failed to update public key for input %v: %w", in.Address, err)
}
acc, err := exe.stateCache.GetAccount(in.Address)
if err != nil {
return err
}
if acc == nil {
return fmt.Errorf("validateInputs() expects to be able to retrieve account %v but it was not found",
in.Address)
}
if in.Address != acc.GetAddress() {
return fmt.Errorf("trying to validate input from address %v but passed account %v", in.Address,
acc.GetAddress())
}
// Check sequences
if acc.Sequence+1 != in.Sequence {
return errors.Errorf(errors.Codes.InvalidSequence, "Error invalid sequence in input %v: input has sequence %d, but account has sequence %d, "+
"so expected input to have sequence %d", in, in.Sequence, acc.Sequence, acc.Sequence+1)
}
// Check amount
if txEnv.Tx.Type() != payload.TypeUnbond && acc.Balance < in.Amount {
return errors.Codes.InsufficientFunds
}
// Check for Input permission
globalPerms, err := acmstate.GlobalAccountPermissions(exe.stateCache)
if err != nil {
return err
}
v, err := acc.Permissions.Base.Compose(globalPerms.Base).Get(permission.Input)
if err != nil {
return err
}
if !v {
return errors.Codes.NoInputPermission
}
}
return nil
}
func (exe *executor) updateSignatory(sig txs.Signatory) error {
// pointer dereferences are safe since txEnv.Validate() is run by
// txEnv.Verify() above which checks they are non-nil
acc, err := exe.stateCache.GetAccount(*sig.Address)
if err != nil {
return fmt.Errorf("error getting account on which to set public key: %v", *sig.Address)
} else if acc == nil {
return fmt.Errorf("account %s does not exist", sig.Address)
}
// Important that verify has been run against signatories at this point
if sig.PublicKey.GetAddress() != acc.Address {
return fmt.Errorf("unexpected mismatch between address %v and supplied public key %v",
acc.Address, sig.PublicKey)
}
acc.PublicKey = sig.PublicKey
return exe.stateCache.UpdateAccount(acc)
}
// Commit the current state - optionally pass in the tendermint ABCI header for that to be included with the BeginBlock
// StreamEvent
func (exe *executor) Commit(header *types.Header) (stateHash []byte, err error) {
// The write lock to the executor is controlled by the caller (e.g. abci.App) so we do not acquire it here to avoid
// deadlock
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("recovered from panic in executor.Commit(): %v\n%s", r, debug.Stack())
}
}()
// Capture height
height := exe.block.Height
exe.logger.InfoMsg("Executor committing", "height", exe.block.Height)
// Form BlockExecution for this block from TxExecutions and Tendermint block header
blockExecution, err := exe.finaliseBlockExecution(header)
if err != nil {
return nil, err
}
// First commit the app state, this app hash will not get checkpointed until the next block when we are sure
// that nothing in the downstream commit process could have failed. At worst we go back one block.
hash, version, err := exe.state.Update(func(ws state.Updatable) error {
// flush the caches
err := exe.stateCache.Sync(ws)
if err != nil {
return err
}
err = exe.metadataCache.Sync(ws)
if err != nil {
return err
}
err = exe.nameRegCache.Sync(ws)
if err != nil {
return err
}
err = exe.nodeRegCache.Sync(ws)
if err != nil {
return err
}
err = exe.proposalRegCache.Sync(ws)
if err != nil {
return err
}
err = exe.validatorCache.Sync(ws)
if err != nil {
return err
}
err = ws.AddBlock(blockExecution)
if err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
// Complete flushing of caches by resetting them to the state we have just committed
err = exe.Reset()
if err != nil {
return nil, err
}
expectedHeight := HeightAtVersion(version)
if expectedHeight != height {
return nil, fmt.Errorf("expected height at state tree version %d is %d but actual height is %d",
version, expectedHeight, height)
}
// Now state is fully committed publish events (this should be the last thing we do)
exe.publishBlock(blockExecution)
return hash, nil
}
func (exe *executor) Reset() error {
// As with Commit() we do not take the write lock here
exe.stateCache.Reset(exe.state)
exe.metadataCache.Reset(exe.state)
exe.nameRegCache.Reset(exe.state)
exe.nodeRegCache.Reset(exe.state)
exe.proposalRegCache.Reset(exe.state)
exe.validatorCache.Reset(exe.state)
return nil
}
// executor exposes access to the underlying state cache protected by a RWMutex that prevents access while locked
// (during an ABCI commit). while access can occur (and needs to continue for CheckTx/DeliverTx to make progress)
// through calls to Execute() external readers will be blocked until the executor is unlocked that allows the Transactor
// to always access the freshest mempool state as needed by accounts.SequentialSigningAccount
//
// Accounts
func (exe *executor) GetAccount(address crypto.Address) (*acm.Account, error) {
exe.RLock()
defer exe.RUnlock()
return exe.stateCache.GetAccount(address)
}
// Storage
func (exe *executor) GetStorage(address crypto.Address, key binary.Word256) ([]byte, error) {
exe.RLock()
defer exe.RUnlock()
return exe.stateCache.GetStorage(address, key)
}
func (exe *executor) PendingValidators() validator.IterableReader {
return exe.validatorCache.Delta
}
func (exe *executor) finaliseBlockExecution(header *types.Header) (*exec.BlockExecution, error) {
if header != nil && uint64(header.Height) != exe.block.Height {
return nil, fmt.Errorf("trying to finalise block execution with height %v but passed Tendermint"+
"block header at height %v", exe.block.Height, header.Height)
}
// Capture BlockExecution to return
be := exe.block
// Set the header when provided
be.Header = header
// My default the predecessor of the next block is the is the predecessor of the current block
// (in case the current block has no transactions - since we do not currently store empty blocks in state, see
// /execution/state/events.go)
predecessor := be.PredecessorHeight
if len(be.TxExecutions) > 0 {
// If the current block has transactions then it will be the predecessor of the next block
predecessor = be.Height
}
// Start new execution for the next height
exe.block = &exec.BlockExecution{
Height: exe.block.Height + 1,
PredecessorHeight: predecessor,
}
return be, nil
}
// update sequence numbers
func (exe *executor) updateSequenceNumbers(txEnv *txs.Envelope) error {
for _, sig := range txEnv.Signatories {
acc, err := exe.stateCache.GetAccount(*sig.Address)
if err != nil {
return fmt.Errorf("error getting account on which to set public key: %v", *sig.Address)
}
exe.logger.TraceMsg("Incrementing sequence number Tx signatory/input",
"height", exe.block.Height,
"tag", "sequence",
"account", acc.Address,
"old_sequence", acc.Sequence,
"new_sequence", acc.Sequence+1)
acc.Sequence++
err = exe.stateCache.UpdateAccount(acc)
if err != nil {
return fmt.Errorf("error updating account after incrementing sequence: %v", err)
}
}
return nil
}
func (exe *executor) publishBlock(blockExecution *exec.BlockExecution) {
for _, txe := range blockExecution.TxExecutions {
publishErr := exe.emitter.Publish(context.Background(), txe, txe)
if publishErr != nil {
exe.logger.InfoMsg("Error publishing TxExecution",
"height", blockExecution.Height,
structure.TxHashKey, txe.TxHash,
structure.ErrorKey, publishErr)
}
}
publishErr := exe.emitter.Publish(context.Background(), blockExecution, blockExecution)
if publishErr != nil {
exe.logger.InfoMsg("Error publishing BlockExecution",
"height", blockExecution.Height,
structure.ErrorKey, publishErr)
}
}