From 593305557b019fc4954ca0d96a6125e224a3db73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Caner=20=C3=87=C4=B1dam?= Date: Fri, 20 Oct 2023 14:25:03 +0300 Subject: [PATCH] add eth_logs range inspection --- inspect/proxy_api.go | 18 ++++++++++++++++-- inspect/proxy_api_test.go | 28 +++++++++++++++++++--------- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/inspect/proxy_api.go b/inspect/proxy_api.go index 8771b117..462bab45 100644 --- a/inspect/proxy_api.go +++ b/inspect/proxy_api.go @@ -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" @@ -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 ( @@ -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 { diff --git a/inspect/proxy_api_test.go b/inspect/proxy_api_test.go index afcd5fee..a7f823c0 100644 --- a/inspect/proxy_api_test.go +++ b/inspect/proxy_api_test.go @@ -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 } @@ -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) @@ -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: ðCallCheckToAddr, 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 { @@ -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],