Skip to content

Commit

Permalink
feat: detect & log signed queries via eth_call
Browse files Browse the repository at this point in the history
  • Loading branch information
CedarMist committed Sep 17, 2024
1 parent cf573dd commit 197d119
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions rpc/eth/metrics/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"

"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-core/go/common/logging"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/evm"

"github.com/oasisprotocol/oasis-web3-gateway/indexer"
"github.com/oasisprotocol/oasis-web3-gateway/rpc/eth"
Expand All @@ -36,6 +38,13 @@ type metricsWrapper struct {
backend indexer.Backend
}

var signedQueryCount = promauto.NewCounter(
prometheus.CounterOpts{
Name: "oasis_web3_gateway_signed_queries",
Help: "Number of eth_call signed queries",
},
)

func (m *metricsWrapper) latestAndRequestHeights(ctx context.Context, blockNum ethrpc.BlockNumber) (uint64, uint64, error) {
var height uint64

Expand Down Expand Up @@ -108,11 +117,37 @@ func (m *metricsWrapper) BlockNumber(ctx context.Context) (res hexutil.Uint64, e
return
}

/**
* Check if the transaction (presumably via `eth_call` RPC) is a signed query

Check failure on line 121 in rpc/eth/metrics/api.go

View workflow job for this annotation

GitHub Actions / lint-go

Comment should end in a period (godot)
*/
func isSignedQuery(args utils.TransactionArgs) bool {
var data *hexutil.Bytes = nil

Check failure on line 124 in rpc/eth/metrics/api.go

View workflow job for this annotation

GitHub Actions / lint-go

var-declaration: should drop = nil from declaration of var data; it is the zero value (revive)
if args.Data != nil {

Check failure on line 125 in rpc/eth/metrics/api.go

View workflow job for this annotation

GitHub Actions / lint-go

ifElseChain: rewrite if-else to switch statement (gocritic)
data = args.Data
} else if args.Input != nil {
data = args.Input
} else {
return false
}
if len(*data) > 1 && (*data)[0] == 0xa3 {
var packed evm.SignedCallDataPack
err := cbor.Unmarshal(*data, packed)
if err == nil {
return true
}
}
return false
}

// Call implements eth.API.
func (m *metricsWrapper) Call(ctx context.Context, args utils.TransactionArgs, blockNrOrHash ethrpc.BlockNumberOrHash, so *utils.StateOverride) (res hexutil.Bytes, err error) {
r, s, f, i, d := metrics.GetAPIMethodMetrics("eth_call")
defer metrics.InstrumentCaller(r, s, f, i, d, &err)()

if isSignedQuery(args) {
signedQueryCount.Inc()
}

res, err = m.api.Call(ctx, args, blockNrOrHash, so)
return
}
Expand Down

0 comments on commit 197d119

Please sign in to comment.