forked from xuperchain/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execution.go
306 lines (275 loc) · 9.92 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
// Copyright 2017 Monax Industries Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package execution
import (
"context"
"fmt"
"runtime/debug"
"sync"
"time"
"github.com/hyperledger/burrow/acm"
"github.com/hyperledger/burrow/acm/state"
"github.com/hyperledger/burrow/bcm"
"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/evm"
"github.com/hyperledger/burrow/execution/exec"
"github.com/hyperledger/burrow/execution/names"
"github.com/hyperledger/burrow/logging"
"github.com/hyperledger/burrow/logging/structure"
"github.com/hyperledger/burrow/txs"
"github.com/hyperledger/burrow/txs/payload"
abciTypes "github.com/tendermint/tendermint/abci/types"
)
type Executor interface {
Execute(txEnv *txs.Envelope) (*exec.TxExecution, error)
}
type Context interface {
Execute(txe *exec.TxExecution) error
}
type ExecutorState interface {
Update(updater func(ws Updatable) error) (hash []byte, err error)
names.Reader
state.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
state.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(blockHash []byte, blockTime time.Time, header *abciTypes.Header) (stateHash []byte, err error)
}
type executor struct {
sync.RWMutex
runCall bool
blockchain *bcm.Blockchain
state ExecutorState
stateCache *state.Cache
nameRegCache *names.Cache
publisher event.Publisher
blockExecution *exec.BlockExecution
logger *logging.Logger
vmOptions []func(*evm.VM)
contexts map[payload.Type]Context
}
var _ BatchExecutor = (*executor)(nil)
// Wraps a cache of what is variously known as the 'check cache' and 'mempool'
func NewBatchChecker(backend ExecutorState, blockchain *bcm.Blockchain, logger *logging.Logger,
options ...ExecutionOption) BatchExecutor {
exe := newExecutor("CheckCache", false, backend, blockchain, event.NewNoOpPublisher(),
logger.WithScope("NewBatchExecutor"), options...)
return exe.AddContext(payload.TypeGovernance,
&contexts.GovernanceContext{
ValidatorSet: exe.blockchain.ValidatorChecker(),
StateWriter: exe.stateCache,
Logger: exe.logger,
},
)
}
func NewBatchCommitter(backend ExecutorState, blockchain *bcm.Blockchain, emitter event.Publisher, logger *logging.Logger,
options ...ExecutionOption) BatchCommitter {
exe := newExecutor("CommitCache", true, backend, blockchain, emitter,
logger.WithScope("NewBatchCommitter"), options...)
return exe.AddContext(payload.TypeGovernance,
&contexts.GovernanceContext{
ValidatorSet: exe.blockchain.ValidatorWriter(),
StateWriter: exe.stateCache,
Logger: exe.logger,
},
)
}
func newExecutor(name string, runCall bool, backend ExecutorState, blockchain *bcm.Blockchain, publisher event.Publisher,
logger *logging.Logger, options ...ExecutionOption) *executor {
exe := &executor{
runCall: runCall,
state: backend,
blockchain: blockchain,
stateCache: state.NewCache(backend, state.Name(name)),
nameRegCache: names.NewCache(backend),
publisher: publisher,
blockExecution: &exec.BlockExecution{
Height: blockchain.LastBlockHeight() + 1,
},
logger: logger.With(structure.ComponentKey, "Executor"),
}
for _, option := range options {
option(exe)
}
exe.contexts = map[payload.Type]Context{
payload.TypeSend: &contexts.SendContext{
Tip: blockchain,
StateWriter: exe.stateCache,
Logger: exe.logger,
},
payload.TypeCall: &contexts.CallContext{
Tip: blockchain,
StateWriter: exe.stateCache,
RunCall: runCall,
VMOptions: exe.vmOptions,
Logger: exe.logger,
},
payload.TypeName: &contexts.NameContext{
Tip: blockchain,
StateWriter: exe.stateCache,
NameReg: exe.nameRegCache,
Logger: exe.logger,
},
payload.TypePermissions: &contexts.PermissionsContext{
Tip: blockchain,
StateWriter: exe.stateCache,
Logger: exe.logger,
},
}
return exe
}
func (exe *executor) AddContext(ty payload.Type, ctx 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(
"run_call", exe.runCall,
"tx_hash", txEnv.Tx.Hash())
logger.TraceMsg("Executing transaction", "tx", txEnv.String())
// Verify transaction signature against inputs
err = txEnv.Verify(exe.stateCache, exe.blockchain.ChainID())
if err != nil {
return nil, err
}
if txExecutor, ok := exe.contexts[txEnv.Tx.Type()]; ok {
// Establish new TxExecution
txe := exe.blockExecution.Tx(txEnv)
err = txExecutor.Execute(txe)
if err != nil {
return nil, err
}
// Return execution for this tx
return txe, nil
}
return nil, fmt.Errorf("unknown transaction type: %v", txEnv.Tx.Type())
}
func (exe *executor) finaliseBlockExecution(header *abciTypes.Header) (*exec.BlockExecution, error) {
if header != nil && uint64(header.Height) != exe.blockExecution.Height {
return nil, fmt.Errorf("trying to finalise block execution with height %v but passed Tendermint"+
"block header at height %v", exe.blockExecution.Height, header.Height)
}
// Capture BlockExecution to return
be := exe.blockExecution
// Set the header when provided
be.BlockHeader = exec.BlockHeaderFromHeader(header)
// Start new execution for the next height
exe.blockExecution = &exec.BlockExecution{
Height: exe.blockExecution.Height + 1,
}
return be, nil
}
func (exe *executor) Commit(blockHash []byte, blockTime time.Time, header *abciTypes.Header) (_ []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())
}
}()
exe.logger.InfoMsg("Executor committing", "height", exe.blockExecution.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, err := exe.state.Update(func(ws Updatable) error {
// flush the caches
err := exe.stateCache.Flush(ws, exe.state)
if err != nil {
return err
}
err = exe.nameRegCache.Flush(ws, exe.state)
if err != nil {
return err
}
err = ws.AddBlock(blockExecution)
if err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
// Now state is committed publish events
for _, txe := range blockExecution.TxExecutions {
publishErr := exe.publisher.Publish(context.Background(), txe, txe.Tagged())
exe.logger.InfoMsg("Error publishing TxExecution",
"tx_hash", txe.TxHash,
structure.ErrorKey, publishErr)
}
publishErr := exe.publisher.Publish(context.Background(), blockExecution, blockExecution.Tagged())
exe.logger.InfoMsg("Error publishing TxExecution",
"height", blockExecution.Height, structure.ErrorKey, publishErr)
// Commit to our blockchain state which will checkpoint the previous app hash by saving it to the database
// (we know the previous app hash is safely committed because we are about to commit the next)
totalPowerChange, totalFlow, err := exe.blockchain.CommitBlock(blockTime, blockHash, hash)
if err != nil {
panic(fmt.Errorf("could not commit block to blockchain state: %v", err))
}
exe.logger.InfoMsg("Committed block",
"total_validator_power", exe.blockchain.CurrentValidators().TotalPower(),
"total_validator_power_change", totalPowerChange,
"total_validator_flow", totalFlow)
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.nameRegCache.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) (binary.Word256, error) {
exe.RLock()
defer exe.RUnlock()
return exe.stateCache.GetStorage(address, key)
}