From b3066f26318007e4c1b23e07198f7abb913a31bb Mon Sep 17 00:00:00 2001 From: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Date: Fri, 14 May 2021 17:44:58 -0400 Subject: [PATCH] evm: SDK metrics (#24) * evm: SDK metrics * update BeginBlock * changelog --- CHANGELOG.md | 2 ++ go.mod | 2 ++ go.sum | 6 ++++-- x/evm/keeper/abci.go | 13 ++++++++++++- x/evm/keeper/msg_server.go | 33 +++++++++++++++++++++++++++++++++ x/evm/types/events.go | 3 +++ x/evm/types/msg.go | 2 +- x/evm/types/state_transition.go | 6 ++++++ 8 files changed, 63 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b4e0b56..87a3fca2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,10 +38,12 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## Unreleased ### API Breaking + * (eth) [\#845](https://github.com/cosmos/ethermint/pull/845) The `eth` namespace must be included in the list of API's as default to run the rpc server without error. ### Improvements +* (evm) [tharsis#24](https://github.com/tharsis/ethermint/pull/24)Implement metrics for `MsgEthereumTx`, state transtitions, `BeginBlock` and `EndBlock`. * (deps) [\#602](https://github.com/cosmos/ethermint/pull/856) Bump tendermint version to [v0.39.3](https://github.com/tendermint/tendermint/releases/tag/v0.39.3) ## [v0.4.1] - 2021-03-01 diff --git a/go.mod b/go.mod index 04ff6d9c..a15f8d92 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/cosmos/ethermint go 1.15 require ( + github.com/armon/go-metrics v0.3.8 github.com/aws/aws-sdk-go v1.38.21 // indirect github.com/bitly/go-simplejson v0.5.0 // indirect github.com/btcsuite/btcd v0.21.0-beta @@ -22,6 +23,7 @@ require ( github.com/gorilla/mux v1.8.0 github.com/gorilla/websocket v1.4.2 github.com/grpc-ecosystem/grpc-gateway v1.16.0 + github.com/hashicorp/go-immutable-radix v1.3.0 // indirect github.com/hashicorp/go-uuid v1.0.2 // indirect github.com/improbable-eng/grpc-web v0.14.0 github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect diff --git a/go.sum b/go.sum index 2aabe6f4..a0de0f0a 100644 --- a/go.sum +++ b/go.sum @@ -82,8 +82,9 @@ github.com/aristanetworks/splunk-hec-go v0.3.3/go.mod h1:1VHO9r17b0K7WmOlLb9nTk/ github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= -github.com/armon/go-metrics v0.3.6 h1:x/tmtOF9cDBoXH7XoAGOz2qqm1DknFD1590XmD/DUJ8= github.com/armon/go-metrics v0.3.6/go.mod h1:4O98XIr/9W0sxpJ8UaYkvjk10Iff7SnFrb4QAOwNTFc= +github.com/armon/go-metrics v0.3.8 h1:oOxq3KPj0WhCuy50EhzwiyMyG2ovRQZpZLXQuOh2a/M= +github.com/armon/go-metrics v0.3.8/go.mod h1:4O98XIr/9W0sxpJ8UaYkvjk10Iff7SnFrb4QAOwNTFc= github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a/go.mod h1:DAHtR1m6lCRdSC2Tm3DSWRPvIPr6xNKyeHdqDQSQT+A= github.com/aws/aws-lambda-go v1.13.3/go.mod h1:4UKl9IzQMoD+QF79YdCuzCwp8VbmG4VAQwij/eHl5CU= @@ -419,8 +420,9 @@ github.com/hashicorp/consul/sdk v0.3.0/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyN github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= -github.com/hashicorp/go-immutable-radix v1.0.0 h1:AKDB1HM5PWEA7i4nhcpwOrO2byshxBjXVn/J/3+z5/0= github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= +github.com/hashicorp/go-immutable-radix v1.3.0 h1:8exGP7ego3OmkfksihtSouGMZ+hQrhxx+FVELeXpVPE= +github.com/hashicorp/go-immutable-radix v1.3.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM= github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk= github.com/hashicorp/go-retryablehttp v0.5.3/go.mod h1:9B5zBasrRhHXnJnui7y6sL7es7NDiJgTc6Er0maI1Xs= diff --git a/x/evm/keeper/abci.go b/x/evm/keeper/abci.go index 054e52df..43ca1839 100644 --- a/x/evm/keeper/abci.go +++ b/x/evm/keeper/abci.go @@ -2,16 +2,24 @@ package keeper import ( "math/big" + "time" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/ethereum/go-ethereum/common" ethtypes "github.com/ethereum/go-ethereum/core/types" + abci "github.com/tendermint/tendermint/abci/types" + + "github.com/cosmos/cosmos-sdk/telemetry" + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/cosmos/ethermint/x/evm/types" ) // BeginBlock sets the block hash -> block height map for the previous block height // and resets the Bloom filter and the transaction count to 0. func (k *Keeper) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { + defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker) + if req.Header.Height < 1 { return } @@ -19,6 +27,7 @@ func (k *Keeper) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { // Gas costs are handled within msg handler so costs should be ignored ctx = ctx.WithGasMeter(sdk.NewInfiniteGasMeter()) + // TODO: why do we have so many hash -> height mappings k.SetBlockHash(ctx, req.Hash, req.Header.Height) k.SetBlockHeightToHash(ctx, req.Hash, req.Header.Height) @@ -35,6 +44,8 @@ func (k *Keeper) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { // the store. The EVM end block logic doesn't update the validator set, thus it returns // an empty slice. func (k Keeper) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.ValidatorUpdate { + defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker) + // Gas costs are handled within msg handler so costs should be ignored ctx = ctx.WithGasMeter(sdk.NewInfiniteGasMeter()) diff --git a/x/evm/keeper/msg_server.go b/x/evm/keeper/msg_server.go index 87da20e8..4e845ea4 100644 --- a/x/evm/keeper/msg_server.go +++ b/x/evm/keeper/msg_server.go @@ -3,12 +3,15 @@ package keeper import ( "context" "errors" + "time" + "github.com/armon/go-metrics" ethcmn "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/vm" tmtypes "github.com/tendermint/tendermint/types" + "github.com/cosmos/cosmos-sdk/telemetry" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" @@ -18,6 +21,8 @@ import ( var _ types.MsgServer = &Keeper{} func (k *Keeper) EthereumTx(goCtx context.Context, msg *types.MsgEthereumTx) (*types.MsgEthereumTxResponse, error) { + defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), types.TypeMsgEthereumTx) + ctx := sdk.UnwrapSDKContext(goCtx) ethMsg, err := msg.AsMessage() @@ -30,6 +35,19 @@ func (k *Keeper) EthereumTx(goCtx context.Context, msg *types.MsgEthereumTx) (*t return nil, types.ErrChainConfigNotFound } + var labels []metrics.Label + if msg.To() == nil { + labels = []metrics.Label{ + telemetry.NewLabel("execution", "create"), + } + } else { + labels = []metrics.Label{ + telemetry.NewLabel("execution", "call"), + // add label to the called recipient address (contract or account) + telemetry.NewLabel("to", msg.Data.To), + } + } + sender := ethMsg.From() txHash := tmtypes.Tx(ctx.TxBytes()).Hash() @@ -118,6 +136,21 @@ func (k *Keeper) EthereumTx(goCtx context.Context, msg *types.MsgEthereumTx) (*t } } + defer func() { + if st.Message.Value().IsInt64() { + telemetry.SetGauge( + float32(st.Message.Value().Int64()), + "tx", "msg", "ethereum_tx", + ) + } + + telemetry.IncrCounterWithLabels( + []string{types.ModuleName, "ethereum_tx"}, + 1, + labels, + ) + }() + // emit events ctx.EventManager().EmitEvents(sdk.Events{ sdk.NewEvent( diff --git a/x/evm/types/events.go b/x/evm/types/events.go index 982a7c2a..d3cf33fd 100644 --- a/x/evm/types/events.go +++ b/x/evm/types/events.go @@ -8,4 +8,7 @@ const ( AttributeKeyRecipient = "recipient" AttributeKeyTxHash = "txHash" AttributeValueCategory = ModuleName + + MetricKeyTransitionDB = "transition_db" + MetricKeyStaticCall = "static_call" ) diff --git a/x/evm/types/msg.go b/x/evm/types/msg.go index 887f55b5..56993b3e 100644 --- a/x/evm/types/msg.go +++ b/x/evm/types/msg.go @@ -25,7 +25,7 @@ var ( // message type and route constants const ( // TypeMsgEthereumTx defines the type string of an Ethereum transaction - TypeMsgEthereumTx = "ethereum" + TypeMsgEthereumTx = "ethereum_tx" ) // NewMsgEthereumTx returns a reference to a new Ethereum transaction message. diff --git a/x/evm/types/state_transition.go b/x/evm/types/state_transition.go index 1674cef3..5079e22d 100644 --- a/x/evm/types/state_transition.go +++ b/x/evm/types/state_transition.go @@ -3,6 +3,7 @@ package types import ( "math/big" "os" + "time" "github.com/pkg/errors" log "github.com/xlab/suplog" @@ -13,6 +14,7 @@ import ( "github.com/ethereum/go-ethereum/core/vm" tmtypes "github.com/tendermint/tendermint/types" + "github.com/cosmos/cosmos-sdk/telemetry" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) @@ -114,6 +116,8 @@ func (st *StateTransition) newEVM( // returning the evm execution result. // NOTE: State transition checks are run during AnteHandler execution. func (st *StateTransition) TransitionDb(ctx sdk.Context, config ChainConfig) (resp *ExecutionResult, err error) { + defer telemetry.ModuleMeasureSince(ModuleName, time.Now(), MetricKeyTransitionDB) + contractCreation := st.Message.To() == nil cost, err := core.IntrinsicGas(st.Message.Data(), st.Message.AccessList(), true, false, true) @@ -294,6 +298,8 @@ func (st *StateTransition) TransitionDb(ctx sdk.Context, config ChainConfig) (re // Opcodes that attempt to perform such modifications will result in exceptions // instead of performing the modifications. func (st *StateTransition) StaticCall(ctx sdk.Context, config ChainConfig) ([]byte, error) { + defer telemetry.ModuleMeasureSince(ModuleName, time.Now(), MetricKeyStaticCall) + // This gas limit the the transaction gas limit with intrinsic gas subtracted gasLimit := st.Message.Gas() - ctx.GasMeter().GasConsumed() csdb := st.Csdb.WithContext(ctx)