Skip to content
This repository has been archived by the owner on Jun 9, 2024. It is now read-only.

feat(telemetry): add timing operations #1343

Merged
merged 10 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cosmos/runtime/miner/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var emptyHash = common.Hash{}
func (m *Miner) PrepareProposal(
ctx sdk.Context, req *abci.RequestPrepareProposal,
) (*abci.ResponsePrepareProposal, error) {

var (
payloadEnvelopeBz []byte
err error
Expand Down
23 changes: 21 additions & 2 deletions cosmos/runtime/miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,27 @@ package miner

import (
"context"
"time"

"github.com/cosmos/gogoproto/proto"

"github.com/berachain/polaris/eth"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/miner"
)

const (
MetricKeyBuildBlock = "polaris_miner_build_block"
MetricKeyBlockGasUsed = "polaris_miner_block_gas_used"
MetricKeyTransactions = "polaris_miner_transactions"
itsdevbear marked this conversation as resolved.
Show resolved Hide resolved
)

// Miner implements the baseapp.TxSelector interface.
type Miner struct {
eth.Miner
Expand Down Expand Up @@ -67,10 +75,15 @@ func (m *Miner) Init(serializer EnvelopeSerializer) {
// to resolve from the underying worker.
func (m *Miner) buildBlock(ctx sdk.Context) ([]byte, uint64, error) {
defer m.clearPayload()

// Record the time it takes to build a payload.
defer telemetry.MeasureSince(time.Now(), MetricKeyBuildBlock)

if err := m.submitPayloadForBuilding(ctx); err != nil {
return nil, 0, err
}
env, gasUsed := m.resolveEnvelope()

return env, gasUsed, nil
}

Expand Down Expand Up @@ -109,11 +122,17 @@ func (m *Miner) resolveEnvelope() ([]byte, uint64) {
return nil, 0
}
envelope := m.currentPayload.ResolveFull()
bz, err := m.serializer.ToSdkTxBytes(envelope, envelope.ExecutionPayload.GasLimit)
payload := envelope.ExecutionPayload

// Record metadata about the payload
defer telemetry.SetGauge(float32(payload.GasUsed), MetricKeyBlockGasUsed)
defer telemetry.SetGauge(float32(len(payload.Transactions)), MetricKeyTransactions)

bz, err := m.serializer.ToSdkTxBytes(envelope, payload.GasLimit)
if err != nil {
panic(err)
}
return bz, envelope.ExecutionPayload.GasUsed
return bz, payload.GasUsed
}

// clearPayload clears the payload.
Expand Down
8 changes: 8 additions & 0 deletions cosmos/runtime/txpool/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"cosmossdk.io/log"

"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

Expand All @@ -35,6 +36,11 @@ import (
"github.com/ethereum/go-ethereum/event"
)

const (
MetricKeyMempoolFull = "polaris_cometbft_mempool_full"
MetricKeyBroadcastFailure = "polaris_cometbft_broadcast_failure"
)

// txChanSize is the size of channel listening to NewTxsEvent. The number is referenced from the
// size of tx pool.
const (
Expand Down Expand Up @@ -229,12 +235,14 @@ func (h *handler) broadcastTransaction(tx *ethtypes.Transaction, retries int) {
switch rsp.Code {
case sdkerrors.ErrMempoolIsFull.ABCICode():
h.logger.Error("failed to broadcast: comet-bft mempool is full", "tx_hash", tx.Hash())
telemetry.IncrCounter(float32(1), MetricKeyMempoolFull)
case
sdkerrors.ErrTxInMempoolCache.ABCICode():
return
default:
h.logger.Error("failed to broadcast transaction",
"codespace", rsp.Codespace, "code", rsp.Code, "info", rsp.Info, "tx_hash", tx.Hash())
telemetry.IncrCounter(float32(1), MetricKeyBroadcastFailure)
}

h.failedTxs <- &failedTx{tx: tx, retries: retries}
Expand Down
21 changes: 21 additions & 0 deletions cosmos/store/snapmulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,33 @@
package snapmulti

import (
"time"

"cosmossdk.io/store/cachekv"
storetypes "cosmossdk.io/store/types"

polariscachekv "github.com/berachain/polaris/cosmos/store/cachekv"
"github.com/berachain/polaris/lib/ds"
"github.com/berachain/polaris/lib/ds/stack"
"github.com/berachain/polaris/lib/utils"
"github.com/cosmos/cosmos-sdk/telemetry"
)

const (
storeRegistryKey = `snapmultistore`
initJournalCapacity = 16
)

const (
MetricKeyBase = "polaris_snapmulti_"
MetricKeyFinalize = MetricKeyBase + "finalize"
MetricKeyFinalizeSize = MetricKeyFinalize + "_size"
MetricKeySnapshot = MetricKeyBase + "snapshot"
MetricKeySnapshotSize = MetricKeySnapshot + "_size"
MetricKeyRevertToSnapshot = MetricKeyBase + "revert_to_snapshot"
MetricKeyRevertToSnapshotSize = MetricKeyRevertToSnapshot + "_size"
)

// mapMultiStore represents a cached multistore, which is just a map of store keys to its
// corresponding cache kv store currently being used.
type mapMultiStore map[storetypes.StoreKey]storetypes.CacheKVStore
Expand Down Expand Up @@ -109,6 +122,9 @@ func (s *store) GetKVStore(key storetypes.StoreKey) storetypes.KVStore {

// Snapshot implements `libtypes.Snapshottable`.
func (s *store) Snapshot() int {
defer telemetry.MeasureSince(time.Now(), MetricKeySnapshot)
defer telemetry.SetGauge(float32(s.journal.Size()), MetricKeySnapshotSize)

var cms mapMultiStore
if cms = s.journal.Peek(); cms == nil {
// use root if the journal is empty
Expand All @@ -128,6 +144,8 @@ func (s *store) Snapshot() int {
// Revert implements `libtypes.Snapshottable`.
func (s *store) RevertToSnapshot(id int) {
// id is the new size of the journal we want to maintain.
defer telemetry.MeasureSince(time.Now(), MetricKeyRevertToSnapshot)
defer telemetry.SetGauge(float32(s.journal.Size()-id), MetricKeyRevertToSnapshotSize)
s.journal.PopToSize(id)
}

Expand All @@ -137,6 +155,9 @@ func (s *store) RevertToSnapshot(id int) {
//
// Finalize implements `libtypes.Controllable`.
func (s *store) Finalize() {
defer telemetry.MeasureSince(time.Now(), MetricKeyFinalize)
defer telemetry.SetGauge(float32(s.journal.Size()), MetricKeyFinalizeSize)

// Recursively pop the journal and write each cachekv store to its parent cachekv store.
for revision := s.journal.Pop(); revision != nil; revision = s.journal.Pop() {
for key, cacheKVStore := range revision {
Expand Down
6 changes: 6 additions & 0 deletions cosmos/x/evm/keeper/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,21 @@ package keeper
import (
"context"
"fmt"
"time"

storetypes "cosmossdk.io/store/types"

evmtypes "github.com/berachain/polaris/cosmos/x/evm/types"

"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/ethereum/go-ethereum/beacon/engine"
ethtypes "github.com/ethereum/go-ethereum/core/types"
)

const ()

func (k *Keeper) ProcessPayloadEnvelope(
ctx context.Context, msg *evmtypes.WrappedPayloadEnvelope,
) (*evmtypes.WrappedPayloadEnvelopeResponse, error) {
Expand Down Expand Up @@ -66,6 +70,8 @@ func (k *Keeper) ProcessPayloadEnvelope(
ctx = sCtx.WithKVGasConfig(storetypes.GasConfig{}).
itsdevbear marked this conversation as resolved.
Show resolved Hide resolved
WithTransientKVGasConfig(storetypes.GasConfig{})

// Record how long it takes to insert the new block into the chain.
defer telemetry.ModuleMeasureSince(evmtypes.ModuleName, time.Now(), evmtypes.MetricKeyInsertBlockAndSetHead)
if err = k.wrappedChain.InsertBlockAndSetHead(ctx, block); err != nil {
return nil, err
}
Expand Down
31 changes: 21 additions & 10 deletions cosmos/x/evm/plugins/precompile/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package precompile
import (
"fmt"
"math/big"
"time"

storetypes "cosmossdk.io/store/types"

Expand All @@ -35,13 +36,19 @@ import (
libtypes "github.com/berachain/polaris/lib/types"
"github.com/berachain/polaris/lib/utils"

"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/params"
)

const (
MetricKeyBase = "polaris_precompile"
MetricKeyTime = "polaris_precompile_time"
)

// Plugin is the interface that must be implemented by the plugin.
type Plugin interface {
core.PrecompilePlugin
Expand Down Expand Up @@ -131,6 +138,7 @@ func (p *plugin) Run(
evm vm.PrecompileEVM, pc vm.PrecompiledContract, input []byte,
caller common.Address, value *big.Int, suppliedGas uint64, readOnly bool,
) (ret []byte, gasRemaining uint64, err error) {

// get native Cosmos SDK context, MultiStore, and EventManager from the Polaris StateDB
sdb := utils.MustGetAs[pvm.PolarStateDB](evm.GetStateDB())
ctx := sdk.UnwrapSDKContext(sdb.GetContext())
Expand Down Expand Up @@ -166,16 +174,19 @@ func (p *plugin) Run(
gm.ConsumeGas(requiredGas, "precompile required gas")

// run the precompile container
ret, err = pc.Run(
ctx.WithGasMeter(gm).
WithKVGasConfig(p.kvGasConfig).
WithTransientKVGasConfig(p.transientKVGasConfig),
evm,
input,
caller,
value,
)
gasRemaining = gm.GasRemaining()
{
defer telemetry.MeasureSince(time.Now(), MetricKeyTime)
ret, err = pc.Run(
ctx.WithGasMeter(gm).
WithKVGasConfig(p.kvGasConfig).
WithTransientKVGasConfig(p.transientKVGasConfig),
evm,
input,
caller,
value,
)
gasRemaining = gm.GasRemaining()
}

return //nolint:nakedret // named returns.
}
Expand Down
5 changes: 3 additions & 2 deletions cosmos/x/evm/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
package types

const (
StoreKey = "evm"
ModuleName = "evm"
StoreKey = "evm"
ModuleName = "evm"
MetricKeyInsertBlockAndSetHead = "polaris_evm_insert_block_and_set_head"
)

const (
Expand Down
10 changes: 9 additions & 1 deletion e2e/localnet/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ require (
github.com/bits-and-blooms/bitset v1.11.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/containerd/continuity v0.3.0 // indirect
Expand All @@ -28,6 +30,7 @@ require (
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/ethereum/c-kzg-4844 v0.4.0 // indirect
github.com/fjl/memsize v0.0.1 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
Expand All @@ -39,16 +42,20 @@ require (
github.com/google/uuid v1.4.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/gotestyourself/gotestyourself v2.2.0+incompatible // indirect
github.com/hashicorp/go-bexpr v0.1.12 // indirect
github.com/holiman/uint256 v1.2.3 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc4 // indirect
github.com/opencontainers/runc v1.1.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rs/cors v1.9.0 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/supranational/blst v0.3.11 // indirect
Expand All @@ -63,7 +70,8 @@ require (
golang.org/x/sys v0.14.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.14.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools v2.2.0+incompatible // indirect
rsc.io/tmplfunc v0.0.3 // indirect
Expand Down
Loading
Loading