Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions cmd/faucet/rate_limiter.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
package main

import (
lru "github.com/hashicorp/golang-lru"
"github.com/ethereum/go-ethereum/common/lru"
"golang.org/x/time/rate"
)

type IPRateLimiter struct {
ips *lru.Cache // LRU cache to store IP addresses and their associated rate limiters
r rate.Limit // the rate limit, e.g., 5 requests per second
b int // the burst size, e.g., allowing a burst of 10 requests at once. The rate limiter gets into action
ips *lru.Cache[string, *rate.Limiter] // LRU cache to store IP addresses and their associated rate limiters
r rate.Limit // the rate limit, e.g., 5 requests per second
b int // the burst size, e.g., allowing a burst of 10 requests at once. The rate limiter gets into action
// only after this number exceeds
}

func NewIPRateLimiter(r rate.Limit, b int, size int) (*IPRateLimiter, error) {
cache, err := lru.New(size)
if err != nil {
return nil, err
}

i := &IPRateLimiter{
ips: cache,
ips: lru.NewCache[string, *rate.Limiter](size),
r: r,
b: b,
}
Expand All @@ -37,7 +32,7 @@ func (i *IPRateLimiter) addIP(ip string) *rate.Limiter {

func (i *IPRateLimiter) GetLimiter(ip string) *rate.Limiter {
if limiter, exists := i.ips.Get(ip); exists {
return limiter.(*rate.Limiter)
return limiter
}

return i.addIP(ip)
Expand Down
35 changes: 11 additions & 24 deletions consensus/parlia/parlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"sync"
"time"

lru "github.com/hashicorp/golang-lru"
"github.com/ethereum/go-ethereum/common/lru"
"github.com/holiman/uint256"
"github.com/prysmaticlabs/prysm/v5/crypto/bls"
"github.com/willf/bitset"
Expand Down Expand Up @@ -196,11 +196,11 @@ func isToSystemContract(to common.Address) bool {
}

// ecrecover extracts the Ethereum account address from a signed header.
func ecrecover(header *types.Header, sigCache *lru.ARCCache, chainId *big.Int) (common.Address, error) {
func ecrecover(header *types.Header, sigCache *lru.Cache[common.Hash, common.Address], chainId *big.Int) (common.Address, error) {
// If the signature's already cached, return that
hash := header.Hash()
if address, known := sigCache.Get(hash); known {
return address.(common.Address), nil
return address, nil
}
// Retrieve the signature from the header extra-data
if len(header.Extra) < extraSeal {
Expand Down Expand Up @@ -240,9 +240,9 @@ type Parlia struct {
genesisHash common.Hash
db ethdb.Database // Database to store and retrieve snapshot checkpoints

recentSnaps *lru.ARCCache // Snapshots for recent block to speed up
signatures *lru.ARCCache // Signatures of recent blocks to speed up mining
recentHeaders *lru.ARCCache //
recentSnaps *lru.Cache[common.Hash, *Snapshot] // Snapshots for recent block to speed up
signatures *lru.Cache[common.Hash, common.Address] // Signatures of recent blocks to speed up mining
recentHeaders *lru.Cache[string, common.Hash]
// Recent headers to check for double signing: key includes block number and miner. value is the block header
// If same key's value already exists for different block header roots then double sign is detected

Expand Down Expand Up @@ -276,19 +276,6 @@ func New(
parliaConfig := chainConfig.Parlia
log.Info("Parlia", "chainConfig", chainConfig)

// Allocate the snapshot caches and create the engine
recentSnaps, err := lru.NewARC(inMemorySnapshots)
if err != nil {
panic(err)
}
signatures, err := lru.NewARC(inMemorySignatures)
if err != nil {
panic(err)
}
recentHeaders, err := lru.NewARC(inMemoryHeaders)
if err != nil {
panic(err)
}
vABIBeforeLuban, err := abi.JSON(strings.NewReader(validatorSetABIBeforeLuban))
if err != nil {
panic(err)
Expand All @@ -311,9 +298,9 @@ func New(
genesisHash: genesisHash,
db: db,
ethAPI: ethAPI,
recentSnaps: recentSnaps,
recentHeaders: recentHeaders,
signatures: signatures,
recentSnaps: lru.NewCache[common.Hash, *Snapshot](inMemorySnapshots),
recentHeaders: lru.NewCache[string, common.Hash](inMemoryHeaders),
signatures: lru.NewCache[common.Hash, common.Address](inMemorySignatures),
validatorSetABIBeforeLuban: vABIBeforeLuban,
validatorSetABI: vABI,
slashABI: sABI,
Expand Down Expand Up @@ -760,7 +747,7 @@ func (p *Parlia) snapshot(chain consensus.ChainHeaderReader, number uint64, hash
for snap == nil {
// If an in-memory snapshot was found, use that
if s, ok := p.recentSnaps.Get(hash); ok {
snap = s.(*Snapshot)
snap = s
break
}

Expand Down Expand Up @@ -950,7 +937,7 @@ func (p *Parlia) verifySeal(chain consensus.ChainHeaderReader, header *types.Hea
if ok && preHash != header.Hash() {
doubleSignCounter.Inc(1)
log.Warn("DoubleSign detected", " block", header.Number, " miner", header.Coinbase,
"hash1", preHash.(common.Hash), "hash2", header.Hash())
"hash1", preHash, "hash2", header.Hash())
} else {
p.recentHeaders.Add(key, header.Hash())
}
Expand Down
9 changes: 4 additions & 5 deletions consensus/parlia/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ import (
"math"
"sort"

lru "github.com/hashicorp/golang-lru"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/lru"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethdb"
Expand All @@ -40,7 +39,7 @@ import (
type Snapshot struct {
config *params.ParliaConfig // Consensus engine parameters to fine tune behavior
ethAPI *ethapi.BlockChainAPI
sigCache *lru.ARCCache // Cache of recent block signatures to speed up ecrecover
sigCache *lru.Cache[common.Hash, common.Address] // Cache of recent block signatures to speed up ecrecover

Number uint64 `json:"number"` // Block number where the snapshot was created
Hash common.Hash `json:"hash"` // Block hash where the snapshot was created
Expand All @@ -63,7 +62,7 @@ type ValidatorInfo struct {
// the genesis block.
func newSnapshot(
config *params.ParliaConfig,
sigCache *lru.ARCCache,
sigCache *lru.Cache[common.Hash, common.Address],
number uint64,
hash common.Hash,
validators []common.Address,
Expand Down Expand Up @@ -112,7 +111,7 @@ func (s validatorsAscending) Less(i, j int) bool { return bytes.Compare(s[i][:],
func (s validatorsAscending) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

// loadSnapshot loads an existing snapshot from the database.
func loadSnapshot(config *params.ParliaConfig, sigCache *lru.ARCCache, db ethdb.Database, hash common.Hash, ethAPI *ethapi.BlockChainAPI) (*Snapshot, error) {
func loadSnapshot(config *params.ParliaConfig, sigCache *lru.Cache[common.Hash, common.Address], db ethdb.Database, hash common.Hash, ethAPI *ethapi.BlockChainAPI) (*Snapshot, error) {
blob, err := db.Get(append([]byte("parlia-"), hash[:]...))
if err != nil {
return nil, err
Expand Down
26 changes: 7 additions & 19 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import (
"github.com/ethereum/go-ethereum/triedb"
"github.com/ethereum/go-ethereum/triedb/hashdb"
"github.com/ethereum/go-ethereum/triedb/pathdb"
exlru "github.com/hashicorp/golang-lru"
"golang.org/x/crypto/sha3"
)

Expand Down Expand Up @@ -319,9 +318,9 @@ type BlockChain struct {
futureBlocks *lru.Cache[common.Hash, *types.Block]

// trusted diff layers
diffLayerCache *exlru.Cache // Cache for the diffLayers
diffLayerChanCache *exlru.Cache // Cache for the difflayer channel
diffQueue *prque.Prque[int64, *types.DiffLayer] // A Priority queue to store recent diff layer
diffLayerCache *lru.Cache[common.Hash, *types.DiffLayer] // Cache for the diffLayers
diffLayerChanCache *lru.Cache[common.Hash, chan struct{}] // Cache for the difflayer channel
diffQueue *prque.Prque[int64, *types.DiffLayer] // A Priority queue to store recent diff layer
diffQueueBuffer chan *types.DiffLayer
diffLayerFreezerBlockLimit uint64

Expand Down Expand Up @@ -357,9 +356,6 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
"triesInMemory", cacheConfig.TriesInMemory, "scheme", cacheConfig.StateScheme)
}

diffLayerCache, _ := exlru.New(diffLayerCacheLimit)
diffLayerChanCache, _ := exlru.New(diffLayerCacheLimit)

// Open trie database with provided config
enableVerkle, err := EnableVerkleAtGenesis(db, genesis)
if err != nil {
Expand Down Expand Up @@ -404,8 +400,8 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
blockStatsCache: lru.NewCache[common.Hash, *BlockStats](blockCacheLimit),
txLookupCache: lru.NewCache[common.Hash, txLookup](txLookupCacheLimit),
futureBlocks: lru.NewCache[common.Hash, *types.Block](maxFutureBlocks),
diffLayerCache: diffLayerCache,
diffLayerChanCache: diffLayerChanCache,
diffLayerCache: lru.NewCache[common.Hash, *types.DiffLayer](diffLayerCacheLimit),
diffLayerChanCache: lru.NewCache[common.Hash, chan struct{}](diffLayerCacheLimit),
engine: engine,
vmConfig: vmConfig,
diffQueue: prque.New[int64, *types.DiffLayer](nil),
Expand Down Expand Up @@ -674,10 +670,6 @@ func (bc *BlockChain) cacheDiffLayer(diffLayer *types.DiffLayer, diffLayerCh cha
sort.Sort(&diffLayer.Storages[index])
}

if bc.diffLayerCache.Len() >= diffLayerCacheLimit {
bc.diffLayerCache.RemoveOldest()
}

bc.diffLayerCache.Add(diffLayer.BlockHash, diffLayer)
close(diffLayerCh)

Expand Down Expand Up @@ -1836,9 +1828,6 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
diffLayer.Number = block.NumberU64()

diffLayerCh := make(chan struct{})
if bc.diffLayerChanCache.Len() >= diffLayerCacheLimit {
bc.diffLayerChanCache.RemoveOldest()
}
bc.diffLayerChanCache.Add(diffLayer.BlockHash, diffLayerCh)

go bc.cacheDiffLayer(diffLayer, diffLayerCh)
Expand Down Expand Up @@ -3275,12 +3264,11 @@ func (bc *BlockChain) GetVerifyResult(blockNumber uint64, blockHash common.Hash,
}

func (bc *BlockChain) GetTrustedDiffLayer(blockHash common.Hash) *types.DiffLayer {
var diff *types.DiffLayer
if cached, ok := bc.diffLayerCache.Get(blockHash); ok {
diff = cached.(*types.DiffLayer)
return diff
return cached
}

var diff *types.DiffLayer
diffStore := bc.db.DiffStore()
if diffStore != nil {
diff = rawdb.ReadDiffLayer(diffStore, blockHash)
Expand Down
21 changes: 8 additions & 13 deletions core/monitor/malicious_vote_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package monitor
import (
"encoding/json"

"github.com/ethereum/go-ethereum/common/lru"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
lru "github.com/hashicorp/golang-lru"
)

// follow define in core/vote
Expand All @@ -25,24 +25,19 @@ var (
// 1. monitor whether there are bugs in the voting mechanism, so add metrics to observe it.
// 2. do malicious vote slashing. TODO
type MaliciousVoteMonitor struct {
curVotes map[types.BLSPublicKey]*lru.Cache
curVotes map[types.BLSPublicKey]*lru.Cache[uint64, *types.VoteEnvelope]
}

func NewMaliciousVoteMonitor() *MaliciousVoteMonitor {
return &MaliciousVoteMonitor{
curVotes: make(map[types.BLSPublicKey]*lru.Cache, 21), // mainnet config
curVotes: make(map[types.BLSPublicKey]*lru.Cache[uint64, *types.VoteEnvelope], 21), // mainnet config
}
}

func (m *MaliciousVoteMonitor) ConflictDetect(newVote *types.VoteEnvelope, pendingBlockNumber uint64) bool {
// get votes for specified VoteAddress
if _, ok := m.curVotes[newVote.VoteAddress]; !ok {
voteDataBuffer, err := lru.New(maxSizeOfRecentEntry)
if err != nil {
log.Error("MaliciousVoteMonitor new lru failed", "err", err)
return false
}
m.curVotes[newVote.VoteAddress] = voteDataBuffer
m.curVotes[newVote.VoteAddress] = lru.NewCache[uint64, *types.VoteEnvelope](maxSizeOfRecentEntry)
}
voteDataBuffer := m.curVotes[newVote.VoteAddress]
sourceNumber, targetNumber := newVote.Data.SourceNumber, newVote.Data.TargetNumber
Expand All @@ -67,16 +62,16 @@ func (m *MaliciousVoteMonitor) ConflictDetect(newVote *types.VoteEnvelope, pendi
continue
}
maliciousVote := false
if blockNumber == targetNumber && voteEnvelope.(*types.VoteEnvelope).Data.Hash() != newVoteHash {
if blockNumber == targetNumber && voteEnvelope.Data.Hash() != newVoteHash {
violateRule1Counter.Inc(1)
maliciousVote = true
} else if (blockNumber < targetNumber && voteEnvelope.(*types.VoteEnvelope).Data.SourceNumber > sourceNumber) ||
(blockNumber > targetNumber && voteEnvelope.(*types.VoteEnvelope).Data.SourceNumber < sourceNumber) {
} else if (blockNumber < targetNumber && voteEnvelope.Data.SourceNumber > sourceNumber) ||
(blockNumber > targetNumber && voteEnvelope.Data.SourceNumber < sourceNumber) {
violateRule2Counter.Inc(1)
maliciousVote = true
}
if maliciousVote {
evidence := types.NewSlashIndicatorFinalityEvidenceWrapper(voteEnvelope.(*types.VoteEnvelope), newVote)
evidence := types.NewSlashIndicatorFinalityEvidenceWrapper(voteEnvelope, newVote)
if evidence != nil {
if evidenceJson, err := json.Marshal(evidence); err == nil {
log.Warn("MaliciousVote", "evidence", string(evidenceJson))
Expand Down
12 changes: 4 additions & 8 deletions core/remote_state_verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import (
"sync"
"time"

lru "github.com/hashicorp/golang-lru"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/lru"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -48,7 +47,7 @@ type remoteVerifyManager struct {
taskLock sync.RWMutex
tasks map[common.Hash]*verifyTask
peers verifyPeers
verifiedCache *lru.Cache
verifiedCache *lru.Cache[common.Hash, bool]
allowInsecure bool

// Subscription
Expand All @@ -61,7 +60,7 @@ type remoteVerifyManager struct {
}

func NewVerifyManager(blockchain *BlockChain, peers verifyPeers, allowInsecure bool) (*remoteVerifyManager, error) {
verifiedCache, _ := lru.New(verifiedCacheSize)
verifiedCache := lru.NewCache[common.Hash, bool](verifiedCacheSize)
block := blockchain.CurrentBlock()
if block == nil {
return nil, ErrCurrentBlockNotFound
Expand Down Expand Up @@ -178,7 +177,7 @@ func (vm *remoteVerifyManager) NewBlockVerifyTask(header *types.Header) {

var diffLayer *types.DiffLayer
if cached, ok := vm.bc.diffLayerChanCache.Get(hash); ok {
diffLayerCh := cached.(chan struct{})
diffLayerCh := cached
<-diffLayerCh
diffLayer = vm.bc.GetTrustedDiffLayer(hash)
}
Expand All @@ -203,9 +202,6 @@ func (vm *remoteVerifyManager) NewBlockVerifyTask(header *types.Header) {
}

func (vm *remoteVerifyManager) cacheBlockVerified(hash common.Hash) {
if vm.verifiedCache.Len() >= verifiedCacheSize {
vm.verifiedCache.RemoveOldest()
}
vm.verifiedCache.Add(hash, true)
}

Expand Down
6 changes: 3 additions & 3 deletions core/vm/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ package vm

import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/lru"
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/metrics"
lru "github.com/hashicorp/golang-lru"
"github.com/holiman/uint256"
)

const codeBitmapCacheSize = 2000

var (
codeBitmapCache, _ = lru.New(codeBitmapCacheSize)
codeBitmapCache = lru.NewCache[common.Hash, bitvec](codeBitmapCacheSize)

contractCodeBitmapHitMeter = metrics.NewRegisteredMeter("vm/contract/code/bitmap/hit", nil)
contractCodeBitmapMissMeter = metrics.NewRegisteredMeter("vm/contract/code/bitmap/miss", nil)
Expand Down Expand Up @@ -106,7 +106,7 @@ func (c *Contract) isCode(udest uint64) bool {
if !exist {
if cached, ok := codeBitmapCache.Get(c.CodeHash); ok {
contractCodeBitmapHitMeter.Mark(1)
analysis = cached.(bitvec)
analysis = cached
} else {
// Do the analysis and save in parent context
// We do not need to store it in c.analysis
Expand Down
Loading