Skip to content

Commit

Permalink
chore: use normal LRU cache
Browse files Browse the repository at this point in the history
Signed-off-by: Jeremy Letang <me@jeremyletang.com>
  • Loading branch information
jeremyletang committed Nov 22, 2023
1 parent b5d9d15 commit a42b433
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
7 changes: 6 additions & 1 deletion core/client/eth/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ func Dial(ctx context.Context, cfg Config) (*Client, error) {
return nil, fmt.Errorf("couldn't instantiate Ethereum client: %w", err)
}

return &Client{ETHClient: newEthClientWrapper(ethClient), cfg: cfg}, nil
wrappedClient, err := newEthClientWrapper(ethClient)
if err != nil {
return nil, fmt.Errorf("could not instantiate lru cache: %w", err)
}

return &Client{ETHClient: wrappedClient, cfg: cfg}, nil
}

func (c *Client) UpdateEthereumConfig(ethConfig *types.EthereumConfig) error {
Expand Down
19 changes: 10 additions & 9 deletions core/client/eth/client_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package eth
import (
"context"
"math/big"
"time"

"code.vegaprotocol.io/vega/core/metrics"

"github.com/ethereum/go-ethereum"
ethcommon "github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/hashicorp/golang-lru/v2/expirable"
lru "github.com/hashicorp/golang-lru/v2"
)

// ETHClient ...
Expand Down Expand Up @@ -53,17 +52,19 @@ type ETHClient interface { //revive:disable:exported
type ethClientWrapper struct {
clt ETHClient

headerByNumberCache *expirable.LRU[string, *ethtypes.Header]
headerByNumberCache *lru.Cache[string, *ethtypes.Header]
}

func newEthClientWrapper(clt ETHClient) *ethClientWrapper {
return &ethClientWrapper{
clt: clt,
// arbitrary size of 100 blocks, kept for at most 10 minutes,
// let see later how to make this less hardcoded
headerByNumberCache: expirable.NewLRU[string, *ethtypes.Header](100, nil, 10*time.Minute),
func newEthClientWrapper(clt ETHClient) (*ethClientWrapper, error) {
cache, err := lru.New[string, *ethtypes.Header](1000)
if err != nil {
return nil, err
}

return &ethClientWrapper{
clt: clt,
headerByNumberCache: cache,
}, nil
}

func (c *ethClientWrapper) ChainID(ctx context.Context) (*big.Int, error) {
Expand Down

0 comments on commit a42b433

Please sign in to comment.