laconicd-deprecated/x/evm/keeper/msg_server.go

162 lines
5.3 KiB
Go
Raw Normal View History

// Copyright 2021 Evmos Foundation
// This file is part of Evmos' Ethermint library.
//
// The Ethermint library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Ethermint library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE
package keeper
import (
2021-04-17 10:00:07 +00:00
"context"
"encoding/json"
"fmt"
"strconv"
2021-04-17 10:00:07 +00:00
chore(evm): Deprecate x/params usage in x/evm (#1472) * imp(evm): Migrate from old Cosmos SDK params module to new way of keeping params in module Keeper * Updated changelog * Apply changes from code review * (impv): Added Shanghai and Cancun blocks to current types and latest migration * (tests): Update unit tests to include Shanghai and Cancun blocks * (fix) - ran golangci-lint on the entire project * (fix) - remove deprecated params method * (impv): added marshaling of booleans per individual param key * (impv): added individual param getting and setting * (impv): replaced getters with individual param * (impv): added amino codec for MsgEthereumTx * Added changes suggested in code review * (fix): updated the migration files for v4 * (fix): fixed unit tests panic for incorrect interface * (fix): updated module msg handler * (fix): rename to original params getter method * (refactor): registered implementation for the new msg * (refactor): added correct amino codec for MsgUpdateParams and removed for MsgEthTx * Applied changes from code review * (fix): removed unnecessary duplicate * (fix): removed params_legacy from the types and moved logic to migration * (fix): Added v4 mocks to the migrations_test * (fix): undo all the non related work regarding the Cancun and Shanghai blocks * (fix): reverted linting the entire project - will make a separate PR for it * Applied changes from review * Applied changes from code review * (fix): removed comments * (fix): Ran formatter and fixed linting issues on unsed functions * (fix): Linting issues resolved * (fix): refactor migrations and added default EIPs * (fix): Combined into one call * (fix): Added more straightforward way to handle migration * (fix): corrected migration test * Applied changes from code review * (fix): Linter fix * (fix): Linter * (fix): Lint proto files * Apply suggestions from code review Co-authored-by: MalteHerrmann <42640438+MalteHerrmann@users.noreply.github.com> * (fix): Added new block to migration * (fix): Added additional comments and formatted proto files * (fix): Added name to unit test cases * (fix): removed unused import * Apply changes from code review * (fix): typo * (fix): remove HTTP endpoint exposure * Apply suggestions from code review Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * applied changes from code review * fix: extra line added in merge removed * fix: applied changes from code review Co-authored-by: MalteHerrmann <42640438+MalteHerrmann@users.noreply.github.com> Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
2023-01-04 14:28:45 +00:00
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
2021-04-18 15:54:18 +00:00
tmtypes "github.com/tendermint/tendermint/types"
errorsmod "cosmossdk.io/errors"
"github.com/armon/go-metrics"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/evmos/ethermint/x/evm/types"
)
2021-04-18 15:54:18 +00:00
var _ types.MsgServer = &Keeper{}
// EthereumTx implements the gRPC MsgServer interface. It receives a transaction which is then
// executed (i.e applied) against the go-ethereum EVM. The provided SDK Context is set to the Keeper
// so that it can implements and call the StateDB methods without receiving it as a function
// parameter.
2021-04-18 15:54:18 +00:00
func (k *Keeper) EthereumTx(goCtx context.Context, msg *types.MsgEthereumTx) (*types.MsgEthereumTxResponse, error) {
2021-04-17 10:00:07 +00:00
ctx := sdk.UnwrapSDKContext(goCtx)
sender := msg.From
tx := msg.AsTransaction()
txIndex := k.GetTxIndexTransient(ctx)
labels := []metrics.Label{
telemetry.NewLabel("tx_type", fmt.Sprintf("%d", tx.Type())),
}
if tx.To() == nil {
labels = append(labels, telemetry.NewLabel("execution", "create"))
} else {
labels = append(labels, telemetry.NewLabel("execution", "call"))
}
response, err := k.ApplyTransaction(ctx, tx)
if err != nil {
return nil, errorsmod.Wrap(err, "failed to apply transaction")
}
defer func() {
telemetry.IncrCounterWithLabels(
[]string{"tx", "msg", "ethereum_tx", "total"},
1,
labels,
)
if response.GasUsed != 0 {
telemetry.IncrCounterWithLabels(
[]string{"tx", "msg", "ethereum_tx", "gas_used", "total"},
float32(response.GasUsed),
labels,
)
// Observe which users define a gas limit >> gas used. Note, that
// gas_limit and gas_used are always > 0
gasLimit := sdk.NewDec(int64(tx.Gas()))
gasRatio, err := gasLimit.QuoInt64(int64(response.GasUsed)).Float64()
if err == nil {
telemetry.SetGaugeWithLabels(
[]string{"tx", "msg", "ethereum_tx", "gas_limit", "per", "gas_used"},
float32(gasRatio),
labels,
)
}
}
}()
attrs := []sdk.Attribute{
sdk.NewAttribute(sdk.AttributeKeyAmount, tx.Value().String()),
// add event for ethereum transaction hash format
sdk.NewAttribute(types.AttributeKeyEthereumTxHash, response.Hash),
// add event for index of valid ethereum tx
sdk.NewAttribute(types.AttributeKeyTxIndex, strconv.FormatUint(txIndex, 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(response.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()))
}
if to := tx.To(); to != nil {
attrs = append(attrs, sdk.NewAttribute(types.AttributeKeyRecipient, to.Hex()))
}
if response.Failed() {
attrs = append(attrs, sdk.NewAttribute(types.AttributeKeyEthereumTxFailed, response.VmError))
}
txLogAttrs := make([]sdk.Attribute, len(response.Logs))
for i, log := range response.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, sender),
sdk.NewAttribute(types.AttributeKeyTxType, fmt.Sprintf("%d", tx.Type())),
),
})
2021-04-18 15:54:18 +00:00
return response, nil
}
chore(evm): Deprecate x/params usage in x/evm (#1472) * imp(evm): Migrate from old Cosmos SDK params module to new way of keeping params in module Keeper * Updated changelog * Apply changes from code review * (impv): Added Shanghai and Cancun blocks to current types and latest migration * (tests): Update unit tests to include Shanghai and Cancun blocks * (fix) - ran golangci-lint on the entire project * (fix) - remove deprecated params method * (impv): added marshaling of booleans per individual param key * (impv): added individual param getting and setting * (impv): replaced getters with individual param * (impv): added amino codec for MsgEthereumTx * Added changes suggested in code review * (fix): updated the migration files for v4 * (fix): fixed unit tests panic for incorrect interface * (fix): updated module msg handler * (fix): rename to original params getter method * (refactor): registered implementation for the new msg * (refactor): added correct amino codec for MsgUpdateParams and removed for MsgEthTx * Applied changes from code review * (fix): removed unnecessary duplicate * (fix): removed params_legacy from the types and moved logic to migration * (fix): Added v4 mocks to the migrations_test * (fix): undo all the non related work regarding the Cancun and Shanghai blocks * (fix): reverted linting the entire project - will make a separate PR for it * Applied changes from review * Applied changes from code review * (fix): removed comments * (fix): Ran formatter and fixed linting issues on unsed functions * (fix): Linting issues resolved * (fix): refactor migrations and added default EIPs * (fix): Combined into one call * (fix): Added more straightforward way to handle migration * (fix): corrected migration test * Applied changes from code review * (fix): Linter fix * (fix): Linter * (fix): Lint proto files * Apply suggestions from code review Co-authored-by: MalteHerrmann <42640438+MalteHerrmann@users.noreply.github.com> * (fix): Added new block to migration * (fix): Added additional comments and formatted proto files * (fix): Added name to unit test cases * (fix): removed unused import * Apply changes from code review * (fix): typo * (fix): remove HTTP endpoint exposure * Apply suggestions from code review Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * applied changes from code review * fix: extra line added in merge removed * fix: applied changes from code review Co-authored-by: MalteHerrmann <42640438+MalteHerrmann@users.noreply.github.com> Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
2023-01-04 14:28:45 +00:00
// UpdateParams implements the gRPC MsgServer interface. When an UpdateParams
// proposal passes, it updates the module parameters. The update can only be
// performed if the requested authority is the Cosmos SDK governance module
// account.
func (k *Keeper) UpdateParams(goCtx context.Context, req *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
if k.authority.String() != req.Authority {
return nil, errorsmod.Wrapf(govtypes.ErrInvalidSigner, "invalid authority, expected %s, got %s", k.authority.String(), req.Authority)
}
ctx := sdk.UnwrapSDKContext(goCtx)
if err := k.SetParams(ctx, req.Params); err != nil {
return nil, err
}
return &types.MsgUpdateParamsResponse{}, nil
}