Skip to content

Commit c994a3a

Browse files
authored
fix: avoid nil pointer for tx evm raw cmd (#727)
* fix: avoid nil pointer for tx evm raw cmd * cleanup * cleanup
1 parent ff11da4 commit c994a3a

File tree

5 files changed

+107
-5
lines changed

5 files changed

+107
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- [\#687](https://github.com/cosmos/evm/pull/687) Avoid blocking node shutdown when evm indexer is enabled, log startup failures instead of using errgroup.
2323
- [\#689](https://github.com/cosmos/evm/pull/689) Align debug addr for hex address.
2424
- [\#668](https://github.com/cosmos/evm/pull/668) Fix panic in legacy mempool when Reset() was called with a skipped header between old and new block.
25+
- [\#727](https://github.com/cosmos/evm/pull/727) Avoid nil pointer for `tx evm raw` due to uninitialized EVM coin info.
2526
- [\#730](https://github.com/cosmos/evm/pull/730) Fix panic if evm mempool not used.
2627
- [\#733](https://github.com/cosmos/evm/pull/733) Avoid rejecting tx with unsupported extension option for ExtensionOptionDynamicFeeTx.
2728

x/vm/client/cli/tx.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,12 @@ func NewRawTxCmd() *cobra.Command {
7474
return err
7575
}
7676

77-
baseDenom := types.GetEVMCoinDenom()
78-
79-
tx, err := msg.BuildTx(clientCtx.TxConfig.NewTxBuilder(), baseDenom)
77+
queryClient := types.NewQueryClient(clientCtx)
78+
params, err := queryClient.Params(cmd.Context(), &types.QueryParamsRequest{})
79+
if err != nil {
80+
return err
81+
}
82+
tx, err := msg.BuildTxWithEvmParams(clientCtx.TxConfig.NewTxBuilder(), params.Params)
8083
if err != nil {
8184
return err
8285
}

x/vm/types/msg.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,15 @@ func (msg *MsgEthereumTx) Hash() common.Hash {
304304

305305
// BuildTx builds the canonical cosmos tx from ethereum msg
306306
func (msg *MsgEthereumTx) BuildTx(b client.TxBuilder, evmDenom string) (signing.Tx, error) {
307+
return msg.BuildTxWithEvmParams(b, Params{
308+
EvmDenom: evmDenom,
309+
ExtendedDenomOptions: &ExtendedDenomOptions{
310+
ExtendedDenom: GetEVMCoinExtendedDenom(),
311+
},
312+
})
313+
}
314+
315+
func (msg *MsgEthereumTx) BuildTxWithEvmParams(b client.TxBuilder, params Params) (signing.Tx, error) {
307316
builder, ok := b.(authtx.ExtensionOptionsTxBuilder)
308317
if !ok {
309318
return nil, errors.New("unsupported builder")
@@ -317,8 +326,8 @@ func (msg *MsgEthereumTx) BuildTx(b client.TxBuilder, evmDenom string) (signing.
317326
fees := make(sdk.Coins, 0, 1)
318327
feeAmt := sdkmath.NewIntFromBigInt(msg.GetFee())
319328
if feeAmt.Sign() > 0 {
320-
fees = append(fees, sdk.NewCoin(evmDenom, feeAmt))
321-
fees = ConvertCoinsDenomToExtendedDenom(fees)
329+
fees = append(fees, sdk.NewCoin(params.EvmDenom, feeAmt))
330+
fees = ConvertCoinsDenomToExtendedDenomWithEvmParams(fees, params)
322331
}
323332

324333
builder.SetExtensionOptions(option)

x/vm/types/scaling.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,16 @@ func ConvertCoinsDenomToExtendedDenom(coins sdk.Coins) sdk.Coins {
6666
}
6767
return convertedCoins.Sort()
6868
}
69+
70+
// ConvertCoinsDenomToExtendedDenomWithEvmParams returns the given coins with the Denom of the evm
71+
// coin converted to the extended denom using params.
72+
func ConvertCoinsDenomToExtendedDenomWithEvmParams(coins sdk.Coins, params Params) sdk.Coins {
73+
convertedCoins := make(sdk.Coins, len(coins))
74+
for i, coin := range coins {
75+
if coin.Denom == params.EvmDenom {
76+
coin = sdk.Coin{Denom: params.ExtendedDenomOptions.ExtendedDenom, Amount: coin.Amount}
77+
}
78+
convertedCoins[i] = coin
79+
}
80+
return convertedCoins.Sort()
81+
}

x/vm/types/scaling_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,79 @@ func TestConvertAmountTo18DecimalsBigInt(t *testing.T) {
239239
}
240240
}
241241
}
242+
243+
func TestConvertCoinsDenomToExtendedDenomWithEvmParams(t *testing.T) {
244+
eighteenDecimalsCoinInfo := testconstants.ExampleChainCoinInfo[testconstants.ExampleChainID]
245+
sixDecimalsCoinInfo := testconstants.ExampleChainCoinInfo[testconstants.SixDecimalsChainID]
246+
sixDecimalsParams := evmtypes.Params{
247+
EvmDenom: sixDecimalsCoinInfo.Denom,
248+
ExtendedDenomOptions: &evmtypes.ExtendedDenomOptions{
249+
ExtendedDenom: sixDecimalsCoinInfo.ExtendedDenom,
250+
},
251+
}
252+
nonBaseCoin := sdk.Coin{Denom: "btc", Amount: math.NewInt(100)}
253+
eighteenDecimalsBaseCoin := sdk.Coin{Denom: eighteenDecimalsCoinInfo.Denom, Amount: math.NewInt(1000000000000000000)}
254+
sixDecimalsBaseCoin := sdk.Coin{Denom: sixDecimalsCoinInfo.Denom, Amount: math.NewInt(1000000)}
255+
256+
tcs := []struct {
257+
name string
258+
coins sdk.Coins
259+
params evmtypes.Params
260+
expected sdk.Coins
261+
}{
262+
{
263+
name: "empty coins",
264+
coins: sdk.Coins{},
265+
params: sixDecimalsParams,
266+
expected: sdk.Coins{},
267+
},
268+
{
269+
name: "single coin - 18 decimals (no conversion)",
270+
coins: sdk.NewCoins(eighteenDecimalsBaseCoin),
271+
params: evmtypes.Params{
272+
EvmDenom: eighteenDecimalsCoinInfo.Denom,
273+
ExtendedDenomOptions: &evmtypes.ExtendedDenomOptions{
274+
ExtendedDenom: eighteenDecimalsCoinInfo.ExtendedDenom,
275+
},
276+
},
277+
expected: sdk.NewCoins(sdk.Coin{Denom: eighteenDecimalsCoinInfo.ExtendedDenom, Amount: math.NewInt(1000000000000000000)}),
278+
},
279+
{
280+
name: "single coin - 6 decimals conversion",
281+
coins: sdk.NewCoins(sixDecimalsBaseCoin),
282+
params: sixDecimalsParams,
283+
expected: sdk.NewCoins(sdk.Coin{Denom: sixDecimalsCoinInfo.ExtendedDenom, Amount: math.NewInt(1000000)}),
284+
},
285+
{
286+
name: "single coin - different denom (no conversion)",
287+
coins: sdk.NewCoins(nonBaseCoin),
288+
params: sixDecimalsParams,
289+
expected: sdk.NewCoins(nonBaseCoin),
290+
},
291+
{
292+
name: "multiple coins - mixed denominations",
293+
coins: sdk.NewCoins(
294+
sixDecimalsBaseCoin,
295+
nonBaseCoin,
296+
).Sort(),
297+
params: sixDecimalsParams,
298+
expected: sdk.NewCoins(
299+
nonBaseCoin,
300+
sdk.Coin{Denom: sixDecimalsCoinInfo.ExtendedDenom, Amount: math.NewInt(1000000)},
301+
).Sort(),
302+
},
303+
{
304+
name: "zero amount coin",
305+
coins: sdk.NewCoins(sdk.Coin{Denom: sixDecimalsCoinInfo.Denom, Amount: math.NewInt(0)}),
306+
params: sixDecimalsParams,
307+
expected: sdk.NewCoins(sdk.Coin{Denom: sixDecimalsCoinInfo.ExtendedDenom, Amount: math.NewInt(0)}),
308+
},
309+
}
310+
311+
for _, tc := range tcs {
312+
t.Run(tc.name, func(t *testing.T) {
313+
result := evmtypes.ConvertCoinsDenomToExtendedDenomWithEvmParams(tc.coins, tc.params)
314+
require.Equal(t, tc.expected, result)
315+
})
316+
}
317+
}

0 commit comments

Comments
 (0)