-
Notifications
You must be signed in to change notification settings - Fork 12
/
keeper.go
201 lines (175 loc) · 5.18 KB
/
keeper.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
package keeper
import (
"encoding/json"
"fmt"
"math/big"
"strconv"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
evmkeeper "github.com/evmos/ethermint/x/evm/keeper"
"github.com/evmos/ethermint/x/evm/types"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
tmtypes "github.com/tendermint/tendermint/types"
fxserverconfig "github.com/functionx/fx-core/v7/server/config"
fxtypes "github.com/functionx/fx-core/v7/types"
fxevmtypes "github.com/functionx/fx-core/v7/x/evm/types"
)
type Keeper struct {
*evmkeeper.Keeper
// access to account state
accountKeeper fxevmtypes.AccountKeeper
module common.Address
}
func NewKeeper(ek *evmkeeper.Keeper, ak fxevmtypes.AccountKeeper) *Keeper {
ctx := sdk.Context{}.WithChainID(fxtypes.ChainIdWithEIP155())
ek.WithChainID(ctx)
addr := ak.GetModuleAddress(types.ModuleName)
if addr == nil {
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
}
return &Keeper{
Keeper: ek,
accountKeeper: ak,
module: common.BytesToAddress(addr),
}
}
// CallEVMWithoutGas performs a smart contract method call using contract data without gas
func (k *Keeper) CallEVMWithoutGas(
ctx sdk.Context,
from common.Address,
contract *common.Address,
value *big.Int,
data []byte,
commit bool,
) (*types.MsgEthereumTxResponse, error) {
gasMeter := ctx.GasMeter()
ctx = ctx.WithGasMeter(sdk.NewInfiniteGasMeter())
nonce, err := k.accountKeeper.GetSequence(ctx, from.Bytes())
if err != nil {
return nil, err
}
gasLimit := fxserverconfig.DefaultGasCap
params := ctx.ConsensusParams()
if params != nil && params.Block != nil && params.Block.MaxGas > 0 {
gasLimit = uint64(params.Block.MaxGas)
}
if value == nil {
value = big.NewInt(0)
}
msg := ethtypes.NewMessage(
from,
contract,
nonce,
value, // amount
gasLimit, // gasLimit
big.NewInt(0), // gasFeeCap
big.NewInt(0), // gasTipCap
big.NewInt(0), // gasPrice
data,
ethtypes.AccessList{}, // AccessList
!commit, // isFake
)
res, err := k.ApplyMessage(ctx, msg, types.NewNoOpTracer(), commit)
if err != nil {
return nil, err
}
if res.Failed() {
errStr := res.VmError
if res.VmError == vm.ErrExecutionReverted.Error() {
if cause, err := abi.UnpackRevert(common.CopyBytes(res.Ret)); err == nil {
errStr = cause
}
}
return res, errorsmod.Wrap(types.ErrVMExecution, errStr)
}
ctx.WithGasMeter(gasMeter)
return res, nil
}
func (k *Keeper) CallEVM(
ctx sdk.Context,
from common.Address,
contract *common.Address,
value *big.Int,
gasLimit uint64,
data []byte,
commit bool,
) (*types.MsgEthereumTxResponse, error) {
gasMeter := ctx.GasMeter()
ctx = ctx.WithGasMeter(sdk.NewInfiniteGasMeter())
nonce, err := k.accountKeeper.GetSequence(ctx, from.Bytes())
if err != nil {
return nil, err
}
params := ctx.ConsensusParams()
if params != nil && params.Block != nil && params.Block.MaxGas > 0 {
gasLimit = uint64(params.Block.MaxGas)
}
if value == nil {
value = big.NewInt(0)
}
msg := ethtypes.NewMessage(
from,
contract,
nonce,
value, // amount
gasLimit, // gasLimit
big.NewInt(0), // gasFeeCap
big.NewInt(0), // gasTipCap
big.NewInt(0), // gasPrice
data,
ethtypes.AccessList{}, // AccessList
!commit, // isFake
)
res, err := k.ApplyMessage(ctx, msg, types.NewNoOpTracer(), commit)
if err != nil {
return nil, err
}
attrs := []sdk.Attribute{
sdk.NewAttribute(sdk.AttributeKeyAmount, value.String()),
// add event for ethereum transaction hash format
sdk.NewAttribute(types.AttributeKeyEthereumTxHash, res.Hash),
// add event for index of valid ethereum tx
sdk.NewAttribute(types.AttributeKeyTxIndex, strconv.FormatUint(0, 10)),
// add event for eth tx gas used, we can't get it from cosmos tx result when it contains multiple eth tx msgs.
sdk.NewAttribute(types.AttributeKeyTxGasUsed, strconv.FormatUint(res.GasUsed, 10)),
}
if len(ctx.TxBytes()) > 0 {
// add event for tendermint transaction hash format
hash := tmbytes.HexBytes(tmtypes.Tx(ctx.TxBytes()).Hash())
attrs = append(attrs, sdk.NewAttribute(types.AttributeKeyTxHash, hash.String()))
}
attrs = append(attrs, sdk.NewAttribute(types.AttributeKeyRecipient, contract.Hex()))
if res.Failed() {
attrs = append(attrs, sdk.NewAttribute(types.AttributeKeyEthereumTxFailed, res.VmError))
}
txLogAttrs := make([]sdk.Attribute, len(res.Logs))
for i, log := range res.Logs {
value, err := json.Marshal(log)
if err != nil {
return nil, errorsmod.Wrap(err, "failed to encode log")
}
txLogAttrs[i] = sdk.NewAttribute(types.AttributeKeyTxLog, string(value))
}
// emit events
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeEthereumTx,
attrs...,
),
sdk.NewEvent(
types.EventTypeTxLog,
txLogAttrs...,
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, from.String()),
),
})
ctx.WithGasMeter(gasMeter)
return res, nil
}