-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
163 lines (146 loc) · 4.11 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
package watcher
import (
"fmt"
"github.com/ethereum/go-ethereum/common"
sdk "github.com/evoblockchain/evochain/libs/cosmos-sdk/types"
tm "github.com/evoblockchain/evochain/libs/tendermint/abci/types"
ctypes "github.com/evoblockchain/evochain/libs/tendermint/rpc/core/types"
"github.com/evoblockchain/evochain/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
}
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))
}
case sdk.StdTxType:
w.blockStdTxs = append(w.blockStdTxs, common.BytesToHash(realTx.TxHash()))
txResult := &ctypes.ResultTx{
Hash: tx.TxHash(),
Height: int64(w.height),
TxResult: *resp,
Tx: tx.GetRaw(),
}
w.saveStdTxResponse(txResult)
}
}
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
}
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()))
txResult := &ctypes.ResultTx{
Hash: realTx.TxHash(),
Height: int64(w.height),
TxResult: resp,
Tx: realTx.GetRaw(),
}
w.saveStdTxResponse(txResult)
}
}