Skip to content

Commit

Permalink
bor: generic lru v2 (#7011)
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov authored Mar 3, 2023
1 parent a5144e0 commit b4e24bf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
8 changes: 4 additions & 4 deletions consensus/bor/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strconv"
"sync"

lru "github.com/hashicorp/golang-lru"
lru2 "github.com/hashicorp/golang-lru/v2"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/xsleonard/go-merkle"
"golang.org/x/crypto/sha3"
Expand All @@ -30,7 +30,7 @@ var (
type API struct {
chain consensus.ChainHeaderReader
bor *Bor
rootHashCache *lru.ARCCache
rootHashCache *lru2.ARCCache[string, string]
}

// GetSnapshot retrieves the state snapshot at a given block.
Expand Down Expand Up @@ -225,7 +225,7 @@ func (api *API) GetRootHash(start uint64, end uint64) (string, error) {
key := getRootHashKey(start, end)

if root, known := api.rootHashCache.Get(key); known {
return root.(string), nil
return root, nil
}

length := end - start + 1
Expand Down Expand Up @@ -289,7 +289,7 @@ func (api *API) GetRootHash(start uint64, end uint64) (string, error) {
func (api *API) initializeRootHashCache() error {
var err error
if api.rootHashCache == nil {
api.rootHashCache, err = lru.NewARC(10)
api.rootHashCache, err = lru2.NewARC[string, string](10)
}

return err
Expand Down
16 changes: 8 additions & 8 deletions consensus/bor/bor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"time"

"github.com/google/btree"
lru "github.com/hashicorp/golang-lru"
"github.com/hashicorp/golang-lru/v2"
"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/length"
Expand Down Expand Up @@ -127,11 +127,11 @@ var (
type SignerFn func(signer libcommon.Address, mimeType string, message []byte) ([]byte, error)

// ecrecover extracts the Ethereum account address from a signed header.
func ecrecover(header *types.Header, sigcache *lru.ARCCache, c *chain.BorConfig) (libcommon.Address, error) {
func ecrecover(header *types.Header, sigcache *lru.ARCCache[libcommon.Hash, libcommon.Address], c *chain.BorConfig) (libcommon.Address, error) {
// If the signature's already cached, return that
hash := header.Hash()
if address, known := sigcache.Get(hash); known {
return address.(libcommon.Address), nil
return address, nil
}
// Retrieve the signature from the header extra-data
if len(header.Extra) < extraSeal {
Expand Down Expand Up @@ -230,8 +230,8 @@ type Bor struct {
config *chain.BorConfig // Consensus engine configuration parameters for bor consensus
DB kv.RwDB // Database to store and retrieve snapshot checkpoints

recents *lru.ARCCache // Snapshots for recent block to speed up reorgs
signatures *lru.ARCCache // Signatures of recent blocks to speed up mining
recents *lru.ARCCache[libcommon.Hash, *Snapshot] // Snapshots for recent block to speed up reorgs
signatures *lru.ARCCache[libcommon.Hash, libcommon.Address] // Signatures of recent blocks to speed up mining

authorizedSigner atomic.Pointer[signer] // Ethereum address and sign function of the signing key

Expand Down Expand Up @@ -271,8 +271,8 @@ func New(
}

// Allocate the snapshot caches and create the engine
recents, _ := lru.NewARC(inmemorySnapshots)
signatures, _ := lru.NewARC(inmemorySignatures)
recents, _ := lru.NewARC[libcommon.Hash, *Snapshot](inmemorySnapshots)
signatures, _ := lru.NewARC[libcommon.Hash, libcommon.Address](inmemorySignatures)
c := &Bor{
chainConfig: chainConfig,
config: borConfig,
Expand Down Expand Up @@ -514,7 +514,7 @@ func (c *Bor) snapshot(chain consensus.ChainHeaderReader, number uint64, hash li
for snap == nil {
// If an in-memory snapshot was found, use that
if s, ok := c.recents.Get(hash); ok {
snap = s.(*Snapshot)
snap = s

break
}
Expand Down
10 changes: 5 additions & 5 deletions consensus/bor/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"context"
"encoding/json"

lru "github.com/hashicorp/golang-lru"
lru2 "github.com/hashicorp/golang-lru/v2"
"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/kv"
Expand All @@ -15,8 +15,8 @@ import (

// Snapshot is the state of the authorization voting at a given point in time.
type Snapshot struct {
config *chain.BorConfig // Consensus engine parameters to fine tune behavior
sigcache *lru.ARCCache // Cache of recent block signatures to speed up ecrecover
config *chain.BorConfig // Consensus engine parameters to fine tune behavior
sigcache *lru2.ARCCache[libcommon.Hash, libcommon.Address] // Cache of recent block signatures to speed up ecrecover

Number uint64 `json:"number"` // Block number where the snapshot was created
Hash libcommon.Hash `json:"hash"` // Block hash where the snapshot was created
Expand All @@ -38,7 +38,7 @@ const BorSeparate = "BorSeparate"
// the genesis block.
func newSnapshot(
config *chain.BorConfig,
sigcache *lru.ARCCache,
sigcache *lru2.ARCCache[libcommon.Hash, libcommon.Address],
number uint64,
hash libcommon.Hash,
validators []*valset.Validator,
Expand All @@ -55,7 +55,7 @@ func newSnapshot(
}

// loadSnapshot loads an existing snapshot from the database.
func loadSnapshot(config *chain.BorConfig, sigcache *lru.ARCCache, db kv.RwDB, hash libcommon.Hash) (*Snapshot, error) {
func loadSnapshot(config *chain.BorConfig, sigcache *lru2.ARCCache[libcommon.Hash, libcommon.Address], db kv.RwDB, hash libcommon.Hash) (*Snapshot, error) {
tx, err := db.BeginRo(context.Background())
if err != nil {
return nil, err
Expand Down

0 comments on commit b4e24bf

Please sign in to comment.