From 6cd867a8fbc461926568c3fb39784687afe86691 Mon Sep 17 00:00:00 2001 From: William Hua Date: Fri, 6 Sep 2024 13:47:22 -0400 Subject: [PATCH] ethrpc: add mutex to memoized chain id --- ethrpc/ethrpc.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ethrpc/ethrpc.go b/ethrpc/ethrpc.go index 00662b0..0028fd6 100644 --- a/ethrpc/ethrpc.go +++ b/ethrpc/ethrpc.go @@ -9,6 +9,7 @@ import ( "io" "math/big" "net/http" + "sync" "sync/atomic" "github.com/0xsequence/ethkit/ethrpc/jsonrpc" @@ -30,7 +31,9 @@ type Provider struct { br breaker.Breaker jwtToken string // optional - chainID *big.Int + chainID *big.Int + chainIDMu sync.Mutex + // cache cachestore.Store[[]byte] // NOTE: unused for now lastRequestID uint64 @@ -165,6 +168,8 @@ func (p *Provider) Do(ctx context.Context, calls ...Call) ([]byte, error) { } func (p *Provider) ChainID(ctx context.Context) (*big.Int, error) { + p.chainIDMu.Lock() + defer p.chainIDMu.Unlock() if p.chainID != nil { // chainID is memoized return p.chainID, nil