-
Notifications
You must be signed in to change notification settings - Fork 257
/
Copy pathexecutor.go
executable file
·536 lines (509 loc) · 14.5 KB
/
executor.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
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package executor 实现执行器模块基类功能
package executor
//store package store the world - state data
import (
"fmt"
"runtime"
"strings"
"sync"
"github.com/33cn/chain33/client/api"
dbm "github.com/33cn/chain33/common/db"
clog "github.com/33cn/chain33/common/log"
log "github.com/33cn/chain33/common/log/log15"
"github.com/33cn/chain33/pluginmgr"
"github.com/33cn/chain33/rpc/grpcclient"
drivers "github.com/33cn/chain33/system/dapp"
// register drivers
"github.com/33cn/chain33/client"
"github.com/33cn/chain33/queue"
"github.com/33cn/chain33/types"
typ "github.com/33cn/chain33/types"
)
var elog = log.New("module", "execs")
// SetLogLevel set log level
func SetLogLevel(level string) {
clog.SetLogLevel(level)
}
// DisableLog disable log
func DisableLog() {
elog.SetHandler(log.DiscardHandler())
}
// Executor executor struct
type Executor struct {
disableLocal bool
client queue.Client
qclient client.QueueProtocolAPI
grpccli types.Chain33Client
pluginEnable map[string]bool
alias map[string]string
noneDriverPool *sync.Pool
}
func execInit(cfg *typ.Chain33Config) {
pluginmgr.InitExec(cfg)
}
var runonce sync.Once
// New new executor
func New(cfg *typ.Chain33Config) *Executor {
// init executor
runonce.Do(func() {
execInit(cfg)
})
mcfg := cfg.GetModuleConfig().Exec
exec := &Executor{}
exec.pluginEnable = make(map[string]bool)
exec.pluginEnable["stat"] = mcfg.EnableStat
exec.pluginEnable["mvcc"] = mcfg.EnableMVCC
exec.pluginEnable["addrindex"] = !mcfg.DisableAddrIndex
exec.pluginEnable["txindex"] = !mcfg.DisableTxIndex
exec.pluginEnable["fee"] = !mcfg.DisableFeeIndex
exec.pluginEnable[addrFeeIndex] = mcfg.EnableAddrFeeIndex
exec.noneDriverPool = &sync.Pool{
New: func() interface{} {
none, err := drivers.LoadDriver("none", 0)
if err != nil {
panic("load none driver err")
}
return none
},
}
exec.alias = make(map[string]string)
for _, v := range mcfg.Alias {
data := strings.Split(v, ":")
if len(data) != 2 {
panic("exec.alias config error: " + v)
}
if _, ok := exec.alias[data[0]]; ok {
panic("exec.alias repeat name: " + v)
}
if pluginmgr.HasExec(data[0]) {
panic("exec.alias repeat name with system Exec: " + v)
}
exec.alias[data[0]] = data[1]
}
return exec
}
//Wait Executor ready
func (exec *Executor) Wait() {}
// SetQueueClient set client queue, for recv msg
func (exec *Executor) SetQueueClient(qcli queue.Client) {
exec.client = qcli
exec.client.Sub("execs")
var err error
exec.qclient, err = client.New(qcli, nil)
if err != nil {
panic(err)
}
types.AssertConfig(exec.client)
cfg := exec.client.GetConfig()
if cfg.IsPara() {
exec.grpccli, err = grpcclient.NewMainChainClient(cfg, "")
if err != nil {
panic(err)
}
}
//recv 消息的处理
go func() {
for msg := range exec.client.Recv() {
elog.Debug("exec recv", "msg", msg)
if msg.Ty == types.EventExecTxList {
go exec.procExecTxList(msg)
} else if msg.Ty == types.EventAddBlock {
go exec.procExecAddBlock(msg)
} else if msg.Ty == types.EventDelBlock {
go exec.procExecDelBlock(msg)
} else if msg.Ty == types.EventCheckTx {
go exec.procExecCheckTx(msg)
} else if msg.Ty == types.EventBlockChainQuery {
go exec.procExecQuery(msg)
} else if msg.Ty == types.EventUpgrade {
//执行升级过程中不允许执行其他的事件,这个事件直接不采用异步执行
exec.procUpgrade(msg)
}
}
}()
}
func (exec *Executor) procUpgrade(msg *queue.Message) {
var kvset types.LocalDBSet
for _, plugin := range pluginmgr.GetExecList() {
elog.Info("begin upgrade plugin ", "name", plugin)
kvset1, err := exec.upgradePlugin(plugin)
if err != nil {
msg.Reply(exec.client.NewMessage("", types.EventUpgrade, err))
panic(err)
}
if kvset1 != nil && kvset1.KV != nil && len(kvset1.KV) > 0 {
kvset.KV = append(kvset.KV, kvset1.KV...)
}
}
elog.Info("upgrade plugin success")
msg.Reply(exec.client.NewMessage("", types.EventUpgrade, &kvset))
}
func (exec *Executor) upgradePlugin(plugin string) (*types.LocalDBSet, error) {
header, err := exec.qclient.GetLastHeader()
if err != nil {
return nil, err
}
driver, err := drivers.LoadDriverWithClient(exec.qclient, plugin, header.GetHeight())
if err == types.ErrUnknowDriver { //已经注册插件,但是没有启动
elog.Info("upgrade ignore ", "name", plugin)
return nil, nil
}
if err != nil {
return nil, err
}
var localdb dbm.KVDB
if !exec.disableLocal {
localdb = NewLocalDB(exec.client, false)
defer localdb.(*LocalDB).Close()
driver.SetLocalDB(localdb)
}
//目前升级不允许访问statedb
driver.SetStateDB(nil)
driver.SetAPI(exec.qclient)
driver.SetExecutorAPI(exec.qclient, exec.grpccli)
driver.SetEnv(header.GetHeight(), header.GetBlockTime(), uint64(header.GetDifficulty()))
localdb.Begin()
kvset, err := driver.Upgrade()
if err != nil {
localdb.Rollback()
return nil, err
}
localdb.Commit()
return kvset, nil
}
func (exec *Executor) procExecQuery(msg *queue.Message) {
//panic 处理
defer func() {
if r := recover(); r != nil {
elog.Error("query panic error", "err", r)
msg.Reply(exec.client.NewMessage("", types.EventReceipts, types.ErrExecPanic))
return
}
}()
header, err := exec.qclient.GetLastHeader()
if err != nil {
msg.Reply(exec.client.NewMessage("", types.EventBlockChainQuery, err))
return
}
data := msg.GetData().(*types.ChainExecutor)
driver, err := drivers.LoadDriverWithClient(exec.qclient, data.Driver, header.GetHeight())
if err != nil {
msg.Reply(exec.client.NewMessage("", types.EventBlockChainQuery, err))
return
}
if data.StateHash == nil {
data.StateHash = header.StateHash
}
var localdb dbm.KVDB
if !exec.disableLocal {
//query 只需要读取localdb
localdb = NewLocalDB(exec.client, true)
defer localdb.(*LocalDB).Close()
driver.SetLocalDB(localdb)
}
opt := &StateDBOption{EnableMVCC: exec.pluginEnable["mvcc"], Height: header.GetHeight()}
db := NewStateDB(exec.client, data.StateHash, localdb, opt)
db.(*StateDB).enableMVCC(nil)
driver.SetStateDB(db)
driver.SetAPI(exec.qclient)
driver.SetExecutorAPI(exec.qclient, exec.grpccli)
driver.SetEnv(header.GetHeight(), header.GetBlockTime(), uint64(header.GetDifficulty()))
//查询的情况下下,执行器不做严格校验,allow,尽可能的加载执行器,并且做查询
ret, err := driver.Query(data.FuncName, data.Param)
if err != nil {
msg.Reply(exec.client.NewMessage("", types.EventBlockChainQuery, err))
return
}
msg.Reply(exec.client.NewMessage("", types.EventBlockChainQuery, ret))
}
func (exec *Executor) procExecCheckTx(msg *queue.Message) {
//panic 处理
defer func() {
if r := recover(); r != nil {
elog.Error("check panic error", "err", r)
msg.Reply(exec.client.NewMessage("", types.EventReceipts, types.ErrExecPanic))
return
}
}()
datas := msg.GetData().(*types.ExecTxList)
ctx := &executorCtx{
stateHash: datas.StateHash,
height: datas.Height,
blocktime: datas.BlockTime,
difficulty: datas.Difficulty,
mainHash: datas.MainHash,
mainHeight: datas.MainHeight,
parentHash: datas.ParentHash,
}
var localdb dbm.KVDB
if !exec.disableLocal {
//交易检查只需要读取localdb,只读模式
localdb = NewLocalDB(exec.client, true)
defer localdb.(*LocalDB).Close()
}
execute := newExecutor(ctx, exec, localdb, datas.Txs, nil)
execute.enableMVCC(nil)
//返回一个列表表示成功还是失败
result := &types.ReceiptCheckTxList{}
for i := 0; i < len(datas.Txs); i++ {
tx := datas.Txs[i]
index := i
if datas.IsMempool {
index = -1
}
err := execute.execCheckTx(tx, index)
if err != nil {
result.Errs = append(result.Errs, err.Error())
} else {
result.Errs = append(result.Errs, "")
}
}
msg.Reply(exec.client.NewMessage("", types.EventReceiptCheckTx, result))
}
// GetStack ...
func GetStack() string {
var buf [4048]byte
n := runtime.Stack(buf[:], false)
return fmt.Sprintf("==> %s\n", string(buf[:n]))
}
func (exec *Executor) procExecTxList(msg *queue.Message) {
//panic 处理
defer func() {
if r := recover(); r != nil {
elog.Error("exec tx list panic error", "err", r, "stack", GetStack())
msg.Reply(exec.client.NewMessage("", types.EventReceipts, types.ErrExecPanic))
return
}
}()
datas := msg.GetData().(*types.ExecTxList)
ctx := &executorCtx{
stateHash: datas.StateHash,
height: datas.Height,
blocktime: datas.BlockTime,
difficulty: datas.Difficulty,
mainHash: datas.MainHash,
mainHeight: datas.MainHeight,
parentHash: datas.ParentHash,
}
var localdb dbm.KVDB
if !exec.disableLocal {
localdb = NewLocalDB(exec.client, false)
defer localdb.(*LocalDB).Close()
}
execute := newExecutor(ctx, exec, localdb, datas.Txs, nil)
execute.enableMVCC(nil)
var receipts []*types.Receipt
index := 0
for i := 0; i < len(datas.Txs); i++ {
tx := datas.Txs[i]
//检查groupcount
if tx.GroupCount < 0 || tx.GroupCount == 1 || tx.GroupCount > 20 {
receipts = append(receipts, types.NewErrReceipt(types.ErrTxGroupCount))
continue
}
if tx.GroupCount == 0 {
receipt, err := execute.execTx(exec, tx, index)
if api.IsAPIEnvError(err) {
msg.Reply(exec.client.NewMessage("", types.EventReceipts, err))
return
}
if err != nil {
receipts = append(receipts, types.NewErrReceipt(err))
continue
}
//update local
receipts = append(receipts, receipt)
index++
continue
}
//所有tx.GroupCount > 0 的交易都是错误的交易
if !execute.cfg.IsFork(datas.Height, "ForkTxGroup") {
receipts = append(receipts, types.NewErrReceipt(types.ErrTxGroupNotSupport))
continue
}
//判断GroupCount 是否会产生越界
if i+int(tx.GroupCount) > len(datas.Txs) {
receipts = append(receipts, types.NewErrReceipt(types.ErrTxGroupCount))
continue
}
receiptlist, err := execute.execTxGroup(datas.Txs[i:i+int(tx.GroupCount)], index)
i = i + int(tx.GroupCount) - 1
if len(receiptlist) > 0 && len(receiptlist) != int(tx.GroupCount) {
panic("len(receiptlist) must be equal tx.GroupCount")
}
if err != nil {
if api.IsAPIEnvError(err) {
msg.Reply(exec.client.NewMessage("", types.EventReceipts, err))
return
}
for n := 0; n < int(tx.GroupCount); n++ {
receipts = append(receipts, types.NewErrReceipt(err))
}
continue
}
receipts = append(receipts, receiptlist...)
index += int(tx.GroupCount)
}
msg.Reply(exec.client.NewMessage("", types.EventReceipts,
&types.Receipts{Receipts: receipts}))
}
func (exec *Executor) procExecAddBlock(msg *queue.Message) {
//panic 处理
defer func() {
if r := recover(); r != nil {
elog.Error("add blk panic error", "err", r)
msg.Reply(exec.client.NewMessage("", types.EventReceipts, types.ErrExecPanic))
return
}
}()
datas := msg.GetData().(*types.BlockDetail)
b := datas.Block
ctx := &executorCtx{
stateHash: b.StateHash,
height: b.Height,
blocktime: b.BlockTime,
difficulty: uint64(b.Difficulty),
mainHash: b.MainHash,
mainHeight: b.MainHeight,
parentHash: b.ParentHash,
}
var localdb dbm.KVDB
if !exec.disableLocal {
localdb = NewLocalDB(exec.client, false)
defer localdb.(*LocalDB).Close()
}
execute := newExecutor(ctx, exec, localdb, b.Txs, datas.Receipts)
//因为mvcc 还没有写入,所以目前的mvcc版本是前一个区块的版本
execute.enableMVCC(datas.PrevStatusHash)
var kvset types.LocalDBSet
for _, kv := range datas.KV {
err := execute.stateDB.Set(kv.Key, kv.Value)
if err != nil {
panic(err)
}
}
for name, plugin := range globalPlugins {
kvs, ok, err := plugin.CheckEnable(execute, exec.pluginEnable[name])
if err != nil {
panic(err)
}
if !ok {
continue
}
if len(kvs) > 0 {
kvset.KV = append(kvset.KV, kvs...)
}
kvs, err = plugin.ExecLocal(execute, datas)
if err != nil {
msg.Reply(exec.client.NewMessage("", types.EventAddBlock, err))
return
}
if len(kvs) > 0 {
kvset.KV = append(kvset.KV, kvs...)
for _, kv := range kvs {
err := execute.localDB.Set(kv.Key, kv.Value)
if err != nil {
panic(err)
}
}
}
}
for i := 0; i < len(b.Txs); i++ {
tx := b.Txs[i]
execute.localDB.(*LocalDB).StartTx()
kv, err := execute.execLocalTx(tx, datas.Receipts[i], i)
if err != nil {
msg.Reply(exec.client.NewMessage("", types.EventAddBlock, err))
return
}
if kv != nil && kv.KV != nil {
kvset.KV = append(kvset.KV, kv.KV...)
}
}
msg.Reply(exec.client.NewMessage("", types.EventAddBlock, &kvset))
}
func (exec *Executor) procExecDelBlock(msg *queue.Message) {
//panic 处理
defer func() {
if r := recover(); r != nil {
elog.Error("del blk panic error", "err", r)
msg.Reply(exec.client.NewMessage("", types.EventReceipts, types.ErrExecPanic))
return
}
}()
datas := msg.GetData().(*types.BlockDetail)
b := datas.Block
ctx := &executorCtx{
stateHash: b.StateHash,
height: b.Height,
blocktime: b.BlockTime,
difficulty: uint64(b.Difficulty),
mainHash: b.MainHash,
mainHeight: b.MainHeight,
parentHash: b.ParentHash,
}
var localdb dbm.KVDB
if !exec.disableLocal {
localdb = NewLocalDB(exec.client, false)
defer localdb.(*LocalDB).Close()
}
execute := newExecutor(ctx, exec, localdb, b.Txs, nil)
execute.enableMVCC(nil)
var kvset types.LocalDBSet
for _, kv := range datas.KV {
err := execute.stateDB.Set(kv.Key, kv.Value)
if err != nil {
panic(err)
}
}
for name, plugin := range globalPlugins {
kvs, ok, err := plugin.CheckEnable(execute, exec.pluginEnable[name])
if err != nil {
panic(err)
}
if !ok {
continue
}
if len(kvs) > 0 {
kvset.KV = append(kvset.KV, kvs...)
}
kvs, err = plugin.ExecDelLocal(execute, datas)
if err != nil {
msg.Reply(exec.client.NewMessage("", types.EventAddBlock, err))
return
}
if len(kvs) > 0 {
kvset.KV = append(kvset.KV, kvs...)
}
}
for i := len(b.Txs) - 1; i >= 0; i-- {
tx := b.Txs[i]
kv, err := execute.execDelLocal(tx, datas.Receipts[i], i)
if err == types.ErrActionNotSupport {
continue
}
if err != nil {
msg.Reply(exec.client.NewMessage("", types.EventDelBlock, err))
return
}
if kv != nil && kv.KV != nil {
err := execute.checkPrefix(tx.Execer, kv.KV)
if err != nil {
msg.Reply(exec.client.NewMessage("", types.EventDelBlock, err))
return
}
kvset.KV = append(kvset.KV, kv.KV...)
}
}
msg.Reply(exec.client.NewMessage("", types.EventDelBlock, &kvset))
}
// Close close executor
func (exec *Executor) Close() {
elog.Info("exec module closed")
if exec.client != nil {
exec.client.Close()
}
}