-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
194 lines (175 loc) · 4.85 KB
/
tx.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
package watcher
import (
"fmt"
"github.com/ethereum/go-ethereum/common"
sdk "github.com/furyaxyz/fuxchain/libs/cosmos-sdk/types"
tm "github.com/furyaxyz/fuxchain/libs/tendermint/abci/types"
ctypes "github.com/furyaxyz/fuxchain/libs/tendermint/rpc/core/types"
"github.com/furyaxyz/fuxchain/x/evm/types"
)
type WatchTx interface {
GetTxWatchMessage() WatchMessage
GetTransaction() *Transaction
GetTxHash() common.Hash
GetFailedReceipts(cumulativeGas, gasUsed uint64) *TransactionReceipt
GetIndex() uint64
}
func (w *Watcher) RecordTxAndFailedReceipt(tx tm.TxEssentials, resp *tm.ResponseDeliverTx, txDecoder sdk.TxDecoder) {
if !w.Enabled() {
return
}
// record ResultTx always
if resp != nil {
txResult := &ctypes.ResultTx{
Hash: tx.TxHash(),
Height: int64(w.height),
TxResult: *resp,
Tx: tx.GetRaw(),
}
w.saveStdTxResponse(txResult)
}
realTx, err := w.getRealTx(tx, txDecoder)
if err != nil {
return
}
watchTx := w.createWatchTx(realTx)
switch realTx.GetType() {
case sdk.EvmTxType:
if watchTx == nil {
return
}
w.saveTx(watchTx)
if resp != nil && !resp.IsOK() {
w.saveFailedReceipts(watchTx, uint64(resp.GasUsed))
return
}
if resp != nil && resp.IsOK() && !w.IsRealEvmTx(resp) { // for evm2cm
msgs := realTx.GetMsgs()
if len(msgs) == 0 {
return
}
evmTx, ok := msgs[0].(*types.MsgEthereumTx)
if !ok {
return
}
w.SaveTransactionReceipt(TransactionSuccess, evmTx, watchTx.GetTxHash(), watchTx.GetIndex(), &types.ResultData{}, uint64(resp.GasUsed))
}
case sdk.StdTxType:
w.blockStdTxs = append(w.blockStdTxs, common.BytesToHash(realTx.TxHash()))
}
}
func (w *Watcher) IsRealEvmTx(resp *tm.ResponseDeliverTx) bool {
for _, ev := range resp.Events {
if ev.Type == sdk.EventTypeMessage {
for _, attr := range ev.Attributes {
if string(attr.Key) == sdk.AttributeKeyModule &&
string(attr.Value) == types.AttributeValueCategory {
return true
}
}
}
}
return false
}
func (w *Watcher) getRealTx(tx tm.TxEssentials, txDecoder sdk.TxDecoder) (sdk.Tx, error) {
var err error
realTx, _ := tx.(sdk.Tx)
if realTx == nil {
realTx, err = txDecoder(tx.GetRaw())
if err != nil {
return nil, err
}
}
return realTx, nil
}
func (w *Watcher) createWatchTx(realTx sdk.Tx) WatchTx {
var txMsg WatchTx
switch realTx.GetType() {
case sdk.EvmTxType:
evmTx, err := w.extractEvmTx(realTx)
if err != nil {
return nil
}
txMsg = NewEvmTx(evmTx, common.BytesToHash(evmTx.TxHash()), w.blockHash, w.height, w.evmTxIndex)
w.evmTxIndex++
}
return txMsg
}
func (w *Watcher) extractEvmTx(sdkTx sdk.Tx) (*types.MsgEthereumTx, error) {
var ok bool
var evmTx *types.MsgEthereumTx
// stdTx should only have one tx
msg := sdkTx.GetMsgs()
if len(msg) <= 0 {
return nil, fmt.Errorf("can not extract evm tx, len(msg) <= 0")
}
if evmTx, ok = msg[0].(*types.MsgEthereumTx); !ok {
return nil, fmt.Errorf("sdktx is not evm tx %v", sdkTx)
}
return evmTx, nil
}
func (w *Watcher) saveTx(tx WatchTx) {
if w == nil || tx == nil {
return
}
if w.InfuraKeeper != nil {
ethTx := tx.GetTransaction()
if ethTx != nil {
w.InfuraKeeper.OnSaveTransaction(*ethTx)
}
}
if txWatchMessage := tx.GetTxWatchMessage(); txWatchMessage != nil {
w.batch = append(w.batch, txWatchMessage)
}
w.blockTxs = append(w.blockTxs, tx.GetTxHash())
}
func (w *Watcher) saveFailedReceipts(watchTx WatchTx, gasUsed uint64) {
if w == nil || watchTx == nil {
return
}
w.UpdateCumulativeGas(watchTx.GetIndex(), gasUsed)
receipt := watchTx.GetFailedReceipts(w.cumulativeGas[watchTx.GetIndex()], gasUsed)
if w.InfuraKeeper != nil {
w.InfuraKeeper.OnSaveTransactionReceipt(*receipt)
}
wMsg := NewMsgTransactionReceipt(*receipt, watchTx.GetTxHash())
if wMsg != nil {
w.batch = append(w.batch, wMsg)
}
}
// SaveParallelTx saves parallel transactions and transactionReceipts to watcher
func (w *Watcher) SaveParallelTx(realTx sdk.Tx, resultData *types.ResultData, resp tm.ResponseDeliverTx) {
if !w.Enabled() {
return
}
// record ResultTx always
txResult := &ctypes.ResultTx{
Hash: realTx.TxHash(),
Height: int64(w.height),
TxResult: resp,
Tx: realTx.GetRaw(),
}
w.saveStdTxResponse(txResult)
switch realTx.GetType() {
case sdk.EvmTxType:
msgs := realTx.GetMsgs()
evmTx, ok := msgs[0].(*types.MsgEthereumTx)
if !ok {
return
}
watchTx := NewEvmTx(evmTx, common.BytesToHash(evmTx.TxHash()), w.blockHash, w.height, w.evmTxIndex)
w.evmTxIndex++
w.saveTx(watchTx)
// save transactionReceipts
if resp.IsOK() {
if resultData == nil {
resultData = &types.ResultData{}
}
w.SaveTransactionReceipt(TransactionSuccess, evmTx, watchTx.GetTxHash(), watchTx.GetIndex(), resultData, uint64(resp.GasUsed))
} else {
w.saveFailedReceipts(watchTx, uint64(resp.GasUsed))
}
case sdk.StdTxType:
w.blockStdTxs = append(w.blockStdTxs, common.BytesToHash(realTx.TxHash()))
}
}