Skip to content

Commit

Permalink
Merge pull request #271 from forta-network/caner/eth-logs-range-inspe…
Browse files Browse the repository at this point in the history
…ction

Add eth_logs range inspection
  • Loading branch information
canercidam authored Oct 20, 2023
2 parents 12e1829 + 5933055 commit ba5f780
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
18 changes: 16 additions & 2 deletions inspect/proxy_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const (
IndicatorProxyAPIIsETH2 = "proxy-api.is-eth2"
// IndicatorProxyAPIMethodEthCall indicates whether eth_call works or not.
IndicatorProxyAPIMethodEthCall = "proxy-api.method.eth-call"
// IndicatorProxyAPIMethodEthLogsRange indicates whether eth_logs range is wide or not.
IndicatorProxyAPIMethodEthLogsRange = "proxy-api.method.eth-logs-range"

// IndicatorProxyAPIOffsetScanMean offset information between scan and proxy
IndicatorProxyAPIOffsetScanMean = "proxy-api.offset.scan.mean"
Expand All @@ -47,8 +49,9 @@ var (
IndicatorProxyAPIAccessible, IndicatorProxyAPIChainID, IndicatorProxyAPIModuleWeb3, IndicatorProxyAPIModuleEth, IndicatorProxyAPIModuleNet,
IndicatorProxyAPIHistorySupport, IndicatorProxyAPIIsETH2,
}
ethCallCheckToAddr = common.HexToAddress(utils.ZeroAddress)
ethCallCheckData = hexutil.MustDecode("0xdeadbeef")
ethCallCheckToAddr = common.HexToAddress(utils.ZeroAddress)
ethCallCheckData = hexutil.MustDecode("0xdeadbeef")
inspectedBlockRange = 2000
)

const (
Expand Down Expand Up @@ -137,6 +140,17 @@ func (pai *ProxyAPIInspector) Inspect(ctx context.Context, inspectionCfg Inspect
results.Indicators[IndicatorProxyAPIMethodEthCall] = ResultSuccess
}

_, err = proxyClient.FilterLogs(ctx, geth.FilterQuery{
FromBlock: big.NewInt(0).SetUint64(currentHeight - uint64(inspectedBlockRange) - 1),
ToBlock: big.NewInt(0).SetUint64(currentHeight - 1),
})
if err != nil {
results.Indicators[IndicatorProxyAPIMethodEthLogsRange] = ResultFailure
resultErr = multierror.Append(resultErr, fmt.Errorf("eth_logs range check failed: %v", err))
} else {
results.Indicators[IndicatorProxyAPIMethodEthLogsRange] = ResultSuccess
}

// get configured block and include hash of the returned as metadata
hash, err := GetBlockResponseHash(ctx, proxyRPCClient, inspectionCfg.BlockNumber)
if err != nil {
Expand Down
28 changes: 19 additions & 9 deletions inspect/proxy_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func TestProxyAPIInspection(t *testing.T) {
ethClient := mock_ethereum.NewMockEthClient(ctrl)
regClient := mock_registry.NewMockClient(ctrl)

currentHeight := uint64(123)

RPCDialContext = func(ctx context.Context, rawurl string) (ethereum.RPCClient, error) {
return rpcClient, nil
}
Expand All @@ -48,7 +50,7 @@ func TestProxyAPIInspection(t *testing.T) {
return nil
}).AnyTimes()
rpcClient.EXPECT().CallContext(gomock.Any(), gomock.Any(), "web3_clientVersion").Return(nil)
ethClient.EXPECT().BlockNumber(gomock.Any()).Return(uint64(123), nil)
ethClient.EXPECT().BlockNumber(gomock.Any()).Return(currentHeight, nil)
rpcClient.EXPECT().CallContext(gomock.Any(), gomock.Any(), "eth_getBlockByNumber", gomock.Any()).
DoAndReturn(func(ctx interface{}, result interface{}, method interface{}, args ...interface{}) error {
_ = json.Unmarshal([]byte(`"{}"`), result)
Expand All @@ -58,11 +60,18 @@ func TestProxyAPIInspection(t *testing.T) {
// oldest supported block inspection calls
ethClient.EXPECT().BlockByNumber(gomock.Any(), big.NewInt(VeryOldBlockNumber)).Return(&types.Block{}, nil)

// eth_call inspection
ethClient.EXPECT().CallContract(gomock.Any(), geth.CallMsg{
To: &ethCallCheckToAddr,
Data: ethCallCheckData,
}, nil).Return(nil, errors.New("revert"))

// eth_logs range inspection
ethClient.EXPECT().FilterLogs(gomock.Any(), geth.FilterQuery{
FromBlock: big.NewInt(0).SetUint64(currentHeight - uint64(inspectedBlockRange) - 1),
ToBlock: big.NewInt(0).SetUint64(currentHeight - 1),
}).Return(nil, nil)

// eth2 support inspection calls
rpcClient.EXPECT().CallContext(gomock.Any(), gomock.Any(), "eth_getBlockByNumber", "latest", true).
DoAndReturn(func(ctx interface{}, result interface{}, method interface{}, args ...interface{}) error {
Expand All @@ -82,14 +91,15 @@ func TestProxyAPIInspection(t *testing.T) {

r.Equal(
map[string]float64{
IndicatorProxyAPIAccessible: ResultSuccess,
IndicatorProxyAPIChainID: float64(5),
IndicatorProxyAPIModuleWeb3: ResultSuccess,
IndicatorProxyAPIModuleEth: ResultSuccess,
IndicatorProxyAPIModuleNet: ResultSuccess,
IndicatorProxyAPIHistorySupport: VeryOldBlockNumber,
IndicatorProxyAPIIsETH2: ResultSuccess,
IndicatorProxyAPIMethodEthCall: ResultSuccess,
IndicatorProxyAPIAccessible: ResultSuccess,
IndicatorProxyAPIChainID: float64(5),
IndicatorProxyAPIModuleWeb3: ResultSuccess,
IndicatorProxyAPIModuleEth: ResultSuccess,
IndicatorProxyAPIModuleNet: ResultSuccess,
IndicatorProxyAPIHistorySupport: VeryOldBlockNumber,
IndicatorProxyAPIIsETH2: ResultSuccess,
IndicatorProxyAPIMethodEthCall: ResultSuccess,
IndicatorProxyAPIMethodEthLogsRange: ResultSuccess,
// trick to make test less flaky and ignore offset issues
IndicatorProxyAPIOffsetScanMax: results.Indicators[IndicatorProxyAPIOffsetScanMax],
IndicatorProxyAPIOffsetScanMean: results.Indicators[IndicatorProxyAPIOffsetScanMean],
Expand Down

0 comments on commit ba5f780

Please sign in to comment.