2020-04-29 17:40:33 +00:00
|
|
|
// Package kv includes a key-value store implementation
|
|
|
|
// of an attestation cache used to satisfy important use-cases
|
|
|
|
// such as aggregation in a beacon node runtime.
|
2019-12-04 18:30:45 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
2020-03-13 17:35:28 +00:00
|
|
|
"sync"
|
2020-08-21 23:27:51 +00:00
|
|
|
"time"
|
2019-12-04 18:30:45 +00:00
|
|
|
|
2020-08-21 23:27:51 +00:00
|
|
|
"github.com/patrickmn/go-cache"
|
2021-09-21 19:59:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
2021-09-15 22:55:11 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/crypto/hash"
|
2021-07-21 21:34:07 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2019-12-04 18:30:45 +00:00
|
|
|
)
|
|
|
|
|
2021-09-15 22:55:11 +00:00
|
|
|
var hashFn = hash.HashProto
|
2020-03-26 23:55:25 +00:00
|
|
|
|
2019-12-04 18:30:45 +00:00
|
|
|
// AttCaches defines the caches used to satisfy attestation pool interface.
|
|
|
|
// These caches are KV store for various attestations
|
|
|
|
// such are unaggregated, aggregated or attestations within a block.
|
|
|
|
type AttCaches struct {
|
2020-08-21 23:27:51 +00:00
|
|
|
aggregatedAttLock sync.RWMutex
|
|
|
|
aggregatedAtt map[[32]byte][]*ethpb.Attestation
|
|
|
|
unAggregateAttLock sync.RWMutex
|
|
|
|
unAggregatedAtt map[[32]byte]*ethpb.Attestation
|
|
|
|
forkchoiceAttLock sync.RWMutex
|
|
|
|
forkchoiceAtt map[[32]byte]*ethpb.Attestation
|
|
|
|
blockAttLock sync.RWMutex
|
|
|
|
blockAtt map[[32]byte][]*ethpb.Attestation
|
|
|
|
seenAtt *cache.Cache
|
2019-12-04 18:30:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAttCaches initializes a new attestation pool consists of multiple KV store in cache for
|
|
|
|
// various kind of attestations.
|
|
|
|
func NewAttCaches() *AttCaches {
|
2021-02-16 07:45:34 +00:00
|
|
|
secsInEpoch := time.Duration(params.BeaconConfig().SlotsPerEpoch.Mul(params.BeaconConfig().SecondsPerSlot))
|
2020-08-21 23:27:51 +00:00
|
|
|
c := cache.New(secsInEpoch*time.Second, 2*secsInEpoch*time.Second)
|
2019-12-04 18:30:45 +00:00
|
|
|
pool := &AttCaches{
|
2020-08-21 23:27:51 +00:00
|
|
|
unAggregatedAtt: make(map[[32]byte]*ethpb.Attestation),
|
|
|
|
aggregatedAtt: make(map[[32]byte][]*ethpb.Attestation),
|
|
|
|
forkchoiceAtt: make(map[[32]byte]*ethpb.Attestation),
|
|
|
|
blockAtt: make(map[[32]byte][]*ethpb.Attestation),
|
|
|
|
seenAtt: c,
|
2019-12-04 18:30:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pool
|
|
|
|
}
|