forked from 33cn/chain33
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.go
155 lines (141 loc) · 4.74 KB
/
exec.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
// 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 util
import (
"errors"
"github.com/33cn/chain33/common"
log "github.com/33cn/chain33/common/log/log15"
"github.com/33cn/chain33/queue"
"github.com/33cn/chain33/types"
)
//CheckBlock : To check the block's validaty
func CheckBlock(client queue.Client, block *types.BlockDetail) error {
req := block
msg := client.NewMessage("consensus", types.EventCheckBlock, req)
client.Send(msg, true)
resp, err := client.Wait(msg)
if err != nil {
return err
}
reply := resp.GetData().(*types.Reply)
if reply.IsOk {
return nil
}
return errors.New(string(reply.GetMsg()))
}
//ExecTx : To send lists of txs within a block to exector for exection
func ExecTx(client queue.Client, prevStateRoot []byte, block *types.Block) *types.Receipts {
list := &types.ExecTxList{
StateHash: prevStateRoot,
Txs: block.Txs,
BlockTime: block.BlockTime,
Height: block.Height,
Difficulty: uint64(block.Difficulty),
IsMempool: false,
}
msg := client.NewMessage("execs", types.EventExecTxList, list)
client.Send(msg, true)
resp, err := client.Wait(msg)
if err != nil {
panic(err)
}
receipts := resp.GetData().(*types.Receipts)
return receipts
}
//ExecKVMemSet : send kv values to memory store and set it in db
func ExecKVMemSet(client queue.Client, prevStateRoot []byte, height int64, kvset []*types.KeyValue, sync bool) []byte {
set := &types.StoreSet{StateHash: prevStateRoot, KV: kvset, Height: height}
setwithsync := &types.StoreSetWithSync{Storeset: set, Sync: sync}
msg := client.NewMessage("store", types.EventStoreMemSet, setwithsync)
client.Send(msg, true)
resp, err := client.Wait(msg)
if err != nil {
panic(err)
}
hash := resp.GetData().(*types.ReplyHash)
return hash.GetHash()
}
//ExecKVSetCommit : commit the data set opetation to db
func ExecKVSetCommit(client queue.Client, hash []byte) error {
req := &types.ReqHash{Hash: hash}
msg := client.NewMessage("store", types.EventStoreCommit, req)
client.Send(msg, true)
msg, err := client.Wait(msg)
if err != nil {
return err
}
hash = msg.GetData().(*types.ReplyHash).GetHash()
return nil
}
//ExecKVSetRollback : do the db's roll back operation
func ExecKVSetRollback(client queue.Client, hash []byte) error {
req := &types.ReqHash{Hash: hash}
msg := client.NewMessage("store", types.EventStoreRollback, req)
client.Send(msg, true)
msg, err := client.Wait(msg)
if err != nil {
return err
}
hash = msg.GetData().(*types.ReplyHash).GetHash()
return nil
}
func checkTxDupInner(txs []*types.TransactionCache) (ret []*types.TransactionCache) {
dupMap := make(map[string]bool)
for _, tx := range txs {
hash := string(tx.Hash())
if _, ok := dupMap[hash]; ok {
continue
}
dupMap[hash] = true
ret = append(ret, tx)
}
return ret
}
//CheckTxDup : check whether the tx is duplicated within the while chain
func CheckTxDup(client queue.Client, txs []*types.TransactionCache, height int64) (transactions []*types.TransactionCache, err error) {
var checkHashList types.TxHashList
if types.IsFork(height, "ForkCheckTxDup") {
txs = checkTxDupInner(txs)
}
for _, tx := range txs {
checkHashList.Hashes = append(checkHashList.Hashes, tx.Hash())
checkHashList.Expire = append(checkHashList.Expire, tx.GetExpire())
}
checkHashList.Count = height
hashList := client.NewMessage("blockchain", types.EventTxHashList, &checkHashList)
client.Send(hashList, true)
dupTxList, err := client.Wait(hashList)
if err != nil {
return nil, err
}
dupTxs := dupTxList.GetData().(*types.TxHashList).Hashes
dupMap := make(map[string]bool)
for _, hash := range dupTxs {
dupMap[string(hash)] = true
log.Debug("CheckTxDup", "TxDuphash", common.ToHex(hash))
}
for _, tx := range txs {
hash := tx.Hash()
if dupMap[string(hash)] {
continue
}
transactions = append(transactions, tx)
}
return transactions, nil
}
//ReportErrEventToFront 上报指定错误信息到指定模块,目前只支持从store,blockchain,wallet写数据库失败时上报错误信息到wallet模块,
//然后由钱包模块前端定时调用显示给客户
func ReportErrEventToFront(logger log.Logger, client queue.Client, frommodule string, tomodule string, err error) {
if client == nil || len(tomodule) == 0 || len(frommodule) == 0 || err == nil {
logger.Error("SendErrEventToFront input para err .")
return
}
logger.Debug("SendErrEventToFront", "frommodule", frommodule, "tomodule", tomodule, "err", err)
var reportErrEvent types.ReportErrEvent
reportErrEvent.Frommodule = frommodule
reportErrEvent.Tomodule = tomodule
reportErrEvent.Error = err.Error()
msg := client.NewMessage(tomodule, types.EventErrToFront, &reportErrEvent)
client.Send(msg, false)
}