Skip to content

Commit

Permalink
ethrpc: add mutex to memoized chain id
Browse files Browse the repository at this point in the history
  • Loading branch information
attente committed Sep 6, 2024
1 parent ad7994c commit b008ea9
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion ethrpc/ethrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"math/big"
"net/http"
"sync"
"sync/atomic"

"github.com/0xsequence/ethkit/ethrpc/jsonrpc"
Expand All @@ -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

Expand Down Expand Up @@ -165,15 +168,20 @@ 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
}

var ret *big.Int
_, err := p.Do(ctx, ChainID().Into(&ret))
if err != nil {
return nil, err
}

p.chainID = ret
return ret, nil
}
Expand Down

0 comments on commit b008ea9

Please sign in to comment.