-
Notifications
You must be signed in to change notification settings - Fork 179
/
handler.go
255 lines (222 loc) · 7.32 KB
/
handler.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
package evm
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ethereum/go-ethereum/common"
ethermint "github.com/okex/exchain/app/types"
"github.com/okex/exchain/x/common/perf"
"github.com/okex/exchain/x/evm/types"
"github.com/okex/exchain/x/evm/watcher"
tmtypes "github.com/tendermint/tendermint/types"
)
// NewHandler returns a handler for Ethermint type messages.
func NewHandler(k *Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) (result *sdk.Result, err error) {
ctx = ctx.WithEventManager(sdk.NewEventManager())
var handlerFun func() (*sdk.Result, error)
var name string
switch msg := msg.(type) {
case types.MsgEthereumTx:
name = "handleMsgEthereumTx"
handlerFun = func() (*sdk.Result, error) {
return handleMsgEthereumTx(ctx, k, msg)
}
case types.MsgEthermint:
name = "handleMsgEthermint"
handlerFun = func() (*sdk.Result, error) {
return handleMsgEthermint(ctx, k, msg)
}
default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized %s message type: %T", ModuleName, msg)
}
seq := perf.GetPerf().OnDeliverTxEnter(ctx, types.ModuleName, name)
defer perf.GetPerf().OnDeliverTxExit(ctx, types.ModuleName, name, seq)
result, err = handlerFun()
if err != nil {
err = sdkerrors.New(types.ModuleName, types.CodeSpaceEvmCallFailed, err.Error())
}
return result, err
}
}
// handleMsgEthereumTx handles an Ethereum specific tx
func handleMsgEthereumTx(ctx sdk.Context, k *Keeper, msg types.MsgEthereumTx) (*sdk.Result, error) {
// parse the chainID from a string to a base-10 integer
chainIDEpoch, err := ethermint.ParseChainID(ctx.ChainID())
if err != nil {
return nil, err
}
// Verify signature and retrieve sender address
sender, err := msg.VerifySig(chainIDEpoch)
if err != nil {
return nil, err
}
txHash := tmtypes.Tx(ctx.TxBytes()).Hash()
ethHash := common.BytesToHash(txHash)
st := types.StateTransition{
AccountNonce: msg.Data.AccountNonce,
Price: msg.Data.Price,
GasLimit: msg.Data.GasLimit,
Recipient: msg.Data.Recipient,
Amount: msg.Data.Amount,
Payload: msg.Data.Payload,
Csdb: types.CreateEmptyCommitStateDB(k.GenerateCSDBParams(), ctx),
ChainID: chainIDEpoch,
TxHash: ðHash,
Sender: sender,
Simulate: ctx.IsCheckTx(),
}
// since the txCount is used by the stateDB, and a simulated tx is run only on the node it's submitted to,
// then this will cause the txCount/stateDB of the node that ran the simulated tx to be different than the
// other nodes, causing a consensus error
if !st.Simulate {
k.Watcher.SaveEthereumTx(msg, common.BytesToHash(txHash), uint64(k.TxCount))
// Prepare db for logs
st.Csdb.Prepare(ethHash, k.Bhash, k.TxCount)
st.Csdb.SetLogSize(k.LogSize)
k.TxCount++
}
config, found := k.GetChainConfig(ctx)
if !found {
return nil, types.ErrChainConfigNotFound
}
defer func() {
if !st.Simulate && k.Watcher.Enabled() {
currentGasMeter := ctx.GasMeter()
pm := k.GenerateCSDBParams()
infCtx := ctx.WithGasMeter(sdk.NewInfiniteGasMeter())
sendAcc := pm.AccountKeeper.GetAccount(infCtx, sender.Bytes())
if sendAcc != nil {
pm.Watcher.SaveAccount(sendAcc, true)
}
ctx.WithGasMeter(currentGasMeter)
}
if e := recover(); e != nil {
k.Watcher.Reset()
panic(e)
}
if !st.Simulate {
if err != nil {
k.Watcher.Reset()
} else {
//save state and account data into batch
k.Watcher.Finalize()
}
}
}()
executionResult, resultData, err := st.TransitionDb(ctx, config)
if err != nil {
if !st.Simulate {
k.Watcher.SaveTransactionReceipt(watcher.TransactionFailed, msg, common.BytesToHash(txHash), uint64(k.TxCount-1), &types.ResultData{}, ctx.GasMeter().GasConsumed())
}
return nil, err
}
if !st.Simulate {
// update block bloom filter
k.Bloom.Or(k.Bloom, executionResult.Bloom)
k.LogSize = st.Csdb.GetLogSize()
k.Watcher.SaveTransactionReceipt(watcher.TransactionSuccess, msg, common.BytesToHash(txHash), uint64(k.TxCount-1), resultData, ctx.GasMeter().GasConsumed())
if msg.Data.Recipient == nil {
st.Csdb.IteratorCode(func(addr common.Address, c types.CacheCode) bool {
k.Watcher.SaveContractCode(addr, c.Code)
k.Watcher.SaveContractCodeByHash(c.CodeHash, c.Code)
return true
})
}
}
// log successful execution
k.Logger(ctx).Info(executionResult.Result.Log)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeEthereumTx,
sdk.NewAttribute(sdk.AttributeKeyAmount, msg.Data.Amount.String()),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, sender.String()),
),
})
if msg.Data.Recipient != nil {
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeEthereumTx,
sdk.NewAttribute(types.AttributeKeyRecipient, msg.Data.Recipient.String()),
),
)
}
// set the events to the result
executionResult.Result.Events = ctx.EventManager().Events()
return executionResult.Result, nil
}
// handleMsgEthermint handles an sdk.StdTx for an Ethereum state transition
func handleMsgEthermint(ctx sdk.Context, k *Keeper, msg types.MsgEthermint) (*sdk.Result, error) {
if !ctx.IsCheckTx() && !ctx.IsReCheckTx() {
return nil, sdkerrors.Wrap(ethermint.ErrInvalidMsgType, "Ethermint type message is not allowed.")
}
// parse the chainID from a string to a base-10 integer
chainIDEpoch, err := ethermint.ParseChainID(ctx.ChainID())
if err != nil {
return nil, err
}
txHash := tmtypes.Tx(ctx.TxBytes()).Hash()
ethHash := common.BytesToHash(txHash)
st := types.StateTransition{
AccountNonce: msg.AccountNonce,
Price: msg.Price.BigInt(),
GasLimit: msg.GasLimit,
Amount: msg.Amount.BigInt(),
Payload: msg.Payload,
Csdb: types.CreateEmptyCommitStateDB(k.GenerateCSDBParams(), ctx),
ChainID: chainIDEpoch,
TxHash: ðHash,
Sender: common.BytesToAddress(msg.From.Bytes()),
Simulate: ctx.IsCheckTx(),
}
if msg.Recipient != nil {
to := common.BytesToAddress(msg.Recipient.Bytes())
st.Recipient = &to
}
if !st.Simulate {
// Prepare db for logs
st.Csdb.Prepare(ethHash, k.Bhash, k.TxCount)
st.Csdb.SetLogSize(k.LogSize)
k.TxCount++
}
config, found := k.GetChainConfig(ctx)
if !found {
return nil, types.ErrChainConfigNotFound
}
executionResult, _, err := st.TransitionDb(ctx, config)
if err != nil {
return nil, err
}
// update block bloom filter
if !st.Simulate {
k.Bloom.Or(k.Bloom, executionResult.Bloom)
k.LogSize = st.Csdb.GetLogSize()
}
// log successful execution
k.Logger(ctx).Info(executionResult.Result.Log)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeEthermint,
sdk.NewAttribute(sdk.AttributeKeyAmount, msg.Amount.String()),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.From.String()),
),
})
if msg.Recipient != nil {
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeEthermint,
sdk.NewAttribute(types.AttributeKeyRecipient, msg.Recipient.String()),
),
)
}
// set the events to the result
executionResult.Result.Events = ctx.EventManager().Events()
return executionResult.Result, nil
}