From 99448b22a5e85b8f9dee2341697982a0b551d650 Mon Sep 17 00:00:00 2001 From: Matt Curtis Date: Tue, 9 Jan 2024 12:11:41 +0000 Subject: [PATCH 1/7] Small cache of blocks by hash --- go.mod | 1 + go.sum | 2 ++ go/ethadapter/geth_rpc_client.go | 41 +++++++++++++++++++++++--------- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index d988d82248..9987c10d67 100644 --- a/go.mod +++ b/go.mod @@ -20,6 +20,7 @@ require ( github.com/golang-jwt/jwt v3.2.2+incompatible github.com/google/uuid v1.3.0 github.com/gorilla/websocket v1.4.2 + github.com/hashicorp/golang-lru/v2 v2.0.7 github.com/mattn/go-sqlite3 v1.14.16 github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416 github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index 2a2657a0c5..a867591d1b 100644 --- a/go.sum +++ b/go.sum @@ -318,6 +318,8 @@ github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09 github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= +github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ= diff --git a/go/ethadapter/geth_rpc_client.go b/go/ethadapter/geth_rpc_client.go index 305bf83a68..60577f79e1 100644 --- a/go/ethadapter/geth_rpc_client.go +++ b/go/ethadapter/geth_rpc_client.go @@ -23,6 +23,7 @@ import ( gethcommon "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethclient" + "github.com/hashicorp/golang-lru/v2" ) const ( @@ -30,15 +31,17 @@ const ( connRetryInterval = 500 * time.Millisecond _maxRetryPriceIncreases = 5 _retryPriceMultiplier = 1.2 + _defaultBlockCacheSize = 51 // enough for 50 request batch size and one for previous block ) // gethRPCClient implements the EthClient interface and allows connection to a real ethereum node type gethRPCClient struct { - client *ethclient.Client // the underlying eth rpc client - l2ID gethcommon.Address // the address of the Obscuro node this client is dedicated to - timeout time.Duration // the timeout for connecting to, or communicating with, the L1 node - logger gethlog.Logger - rpcURL string + client *ethclient.Client // the underlying eth rpc client + l2ID gethcommon.Address // the address of the Obscuro node this client is dedicated to + timeout time.Duration // the timeout for connecting to, or communicating with, the L1 node + logger gethlog.Logger + rpcURL string + blockCache *lru.Cache[gethcommon.Hash, *types.Block] } // NewEthClientFromURL instantiates a new ethadapter.EthClient that connects to an ethereum node @@ -49,12 +52,17 @@ func NewEthClientFromURL(rpcURL string, timeout time.Duration, l2ID gethcommon.A } logger.Trace(fmt.Sprintf("Initialized eth node connection - addr: %s", rpcURL)) + + // cache recent blocks to avoid re-fetching them (they are often re-used for checking for forks etc.) + blkCache, _ := lru.New[gethcommon.Hash, *types.Block](_defaultBlockCacheSize) + return &gethRPCClient{ - client: client, - l2ID: l2ID, - timeout: timeout, - logger: logger, - rpcURL: rpcURL, + client: client, + l2ID: l2ID, + timeout: timeout, + logger: logger, + rpcURL: rpcURL, + blockCache: blkCache, }, nil } @@ -181,10 +189,21 @@ func (e *gethRPCClient) BlockByNumber(n *big.Int) (*types.Block, error) { } func (e *gethRPCClient) BlockByHash(hash gethcommon.Hash) (*types.Block, error) { + block, found := e.blockCache.Get(hash) + if found { + return block, nil + } + + // not in cache, fetch from RPC ctx, cancel := context.WithTimeout(context.Background(), e.timeout) defer cancel() - return e.client.BlockByHash(ctx, hash) + block, err := e.client.BlockByHash(ctx, hash) + if err != nil { + return nil, err + } + e.blockCache.Add(hash, block) + return block, nil } func (e *gethRPCClient) CallContract(msg ethereum.CallMsg) ([]byte, error) { From 609496e37e4f4af84dd817f5c7598c4544b44bb0 Mon Sep 17 00:00:00 2001 From: Matt Curtis Date: Tue, 9 Jan 2024 12:31:39 +0000 Subject: [PATCH 2/7] fix import formatting --- go/ethadapter/geth_rpc_client.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/go/ethadapter/geth_rpc_client.go b/go/ethadapter/geth_rpc_client.go index 60577f79e1..1efa20b762 100644 --- a/go/ethadapter/geth_rpc_client.go +++ b/go/ethadapter/geth_rpc_client.go @@ -8,6 +8,8 @@ import ( "math/big" "time" + "github.com/hashicorp/golang-lru/v2" + "github.com/ethereum/go-ethereum/accounts/abi/bind" gethlog "github.com/ethereum/go-ethereum/log" @@ -23,7 +25,6 @@ import ( gethcommon "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethclient" - "github.com/hashicorp/golang-lru/v2" ) const ( From 70dd30136f01d8f5b83e2e59b3312b752595c618 Mon Sep 17 00:00:00 2001 From: Matt Curtis Date: Tue, 9 Jan 2024 12:39:03 +0000 Subject: [PATCH 3/7] rabbit review --- go/ethadapter/geth_rpc_client.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/go/ethadapter/geth_rpc_client.go b/go/ethadapter/geth_rpc_client.go index 1efa20b762..2c272604de 100644 --- a/go/ethadapter/geth_rpc_client.go +++ b/go/ethadapter/geth_rpc_client.go @@ -55,7 +55,10 @@ func NewEthClientFromURL(rpcURL string, timeout time.Duration, l2ID gethcommon.A logger.Trace(fmt.Sprintf("Initialized eth node connection - addr: %s", rpcURL)) // cache recent blocks to avoid re-fetching them (they are often re-used for checking for forks etc.) - blkCache, _ := lru.New[gethcommon.Hash, *types.Block](_defaultBlockCacheSize) + blkCache, err := lru.New[gethcommon.Hash, *types.Block](_defaultBlockCacheSize) + if err != nil { + return nil, fmt.Errorf("unable to initialize block cache - %w", err) + } return &gethRPCClient{ client: client, From 7904ffd6c2fda0027b7abaa03381d3e849898af5 Mon Sep 17 00:00:00 2001 From: Matt Curtis Date: Tue, 9 Jan 2024 12:49:58 +0000 Subject: [PATCH 4/7] try again imports --- go/ethadapter/geth_rpc_client.go | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/go/ethadapter/geth_rpc_client.go b/go/ethadapter/geth_rpc_client.go index 2c272604de..d1181fa057 100644 --- a/go/ethadapter/geth_rpc_client.go +++ b/go/ethadapter/geth_rpc_client.go @@ -8,23 +8,18 @@ import ( "math/big" "time" - "github.com/hashicorp/golang-lru/v2" - + "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/accounts/abi/bind" + gethcommon "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/ethclient" gethlog "github.com/ethereum/go-ethereum/log" + "github.com/hashicorp/golang-lru/v2" "github.com/ten-protocol/go-ten/contracts/generated/ManagementContract" - "github.com/ten-protocol/go-ten/go/common/retry" - - "github.com/ten-protocol/go-ten/go/common/log" - "github.com/ten-protocol/go-ten/go/common" - - "github.com/ethereum/go-ethereum" - - gethcommon "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/ethclient" + "github.com/ten-protocol/go-ten/go/common/log" + "github.com/ten-protocol/go-ten/go/common/retry" ) const ( From f083c166a142bdae65a889a552bd060f37193a98 Mon Sep 17 00:00:00 2001 From: Matt Curtis Date: Tue, 9 Jan 2024 13:01:28 +0000 Subject: [PATCH 5/7] imports again... --- go/ethadapter/geth_rpc_client.go | 1 + 1 file changed, 1 insertion(+) diff --git a/go/ethadapter/geth_rpc_client.go b/go/ethadapter/geth_rpc_client.go index d1181fa057..b68f7203cc 100644 --- a/go/ethadapter/geth_rpc_client.go +++ b/go/ethadapter/geth_rpc_client.go @@ -14,6 +14,7 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethclient" gethlog "github.com/ethereum/go-ethereum/log" + "github.com/hashicorp/golang-lru/v2" "github.com/ten-protocol/go-ten/contracts/generated/ManagementContract" From 7778ef77b4f6da3ed6e7402d52b6f73e5ac2d27a Mon Sep 17 00:00:00 2001 From: Matt Curtis Date: Tue, 9 Jan 2024 14:23:35 +0000 Subject: [PATCH 6/7] sighduck --- go/ethadapter/geth_rpc_client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/ethadapter/geth_rpc_client.go b/go/ethadapter/geth_rpc_client.go index b68f7203cc..4e36ce3fc6 100644 --- a/go/ethadapter/geth_rpc_client.go +++ b/go/ethadapter/geth_rpc_client.go @@ -14,7 +14,7 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethclient" gethlog "github.com/ethereum/go-ethereum/log" - + "github.com/hashicorp/golang-lru/v2" "github.com/ten-protocol/go-ten/contracts/generated/ManagementContract" From 8f564c51943e08f4c4ce99ad2c6f861dea154584 Mon Sep 17 00:00:00 2001 From: Matt Curtis Date: Tue, 9 Jan 2024 14:26:10 +0000 Subject: [PATCH 7/7] that's the badger --- go/ethadapter/geth_rpc_client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/ethadapter/geth_rpc_client.go b/go/ethadapter/geth_rpc_client.go index 4e36ce3fc6..20c79c2cc1 100644 --- a/go/ethadapter/geth_rpc_client.go +++ b/go/ethadapter/geth_rpc_client.go @@ -15,7 +15,7 @@ import ( "github.com/ethereum/go-ethereum/ethclient" gethlog "github.com/ethereum/go-ethereum/log" - "github.com/hashicorp/golang-lru/v2" + lru "github.com/hashicorp/golang-lru/v2" "github.com/ten-protocol/go-ten/contracts/generated/ManagementContract" "github.com/ten-protocol/go-ten/go/common"