From c4b24af69ac7940ca9c0fe09a12b55e52564367b Mon Sep 17 00:00:00 2001 From: mralj Date: Fri, 27 Sep 2024 13:32:52 +0200 Subject: [PATCH] added rpc call timeout strategy --- core/vm/contracts_rollup.go | 13 +++++++++++-- params/protocol_params_rollup.go | 2 ++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/core/vm/contracts_rollup.go b/core/vm/contracts_rollup.go index 2b6140423ebc..8dee19ed6f4f 100644 --- a/core/vm/contracts_rollup.go +++ b/core/vm/contracts_rollup.go @@ -7,6 +7,7 @@ import ( "context" "errors" "math/big" + "time" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/log" @@ -90,8 +91,16 @@ func (c *L1SLoad) Run(input []byte) ([]byte, error) { // TODO: // 1. Batch multiple storage slots - // 2. What about timeout strategy here? - res, err := c.L1RpcClient.StorageAt(context.Background(), contractAddress, contractStorageKeys[0], c.GetLatestL1BlockNumber()) + var ctx context.Context + if params.L1SLoadRPCTimeoutInSec > 0 { + c, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(params.L1SLoadRPCTimeoutInSec)) + ctx = c + defer cancel() + } else { + ctx = context.Background() + } + + res, err := c.L1RpcClient.StorageAt(ctx, contractAddress, contractStorageKeys[0], c.GetLatestL1BlockNumber()) if err != nil { return nil, err } diff --git a/params/protocol_params_rollup.go b/params/protocol_params_rollup.go index 3ac56c6f63d2..46a0c9f570a9 100644 --- a/params/protocol_params_rollup.go +++ b/params/protocol_params_rollup.go @@ -5,3 +5,5 @@ const ( L1SLoadPerLoadGas uint64 = 2000 // Per-load price for loading one storage slot L1SLoadMaxNumStorageSlots = 5 // Max number of storage slots requested in L1Sload precompile ) + +var L1SLoadRPCTimeoutInSec = MainnetChainConfig.Clique.Period // After how many ms will RPC call timeout