github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/operations/attestations/kv/kv.go (about) 1 // Package kv includes a key-value store implementation 2 // of an attestation cache used to satisfy important use-cases 3 // such as aggregation in a beacon node runtime. 4 package kv 5 6 import ( 7 "sync" 8 "time" 9 10 "github.com/patrickmn/go-cache" 11 ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" 12 "github.com/prysmaticlabs/prysm/shared/hashutil" 13 "github.com/prysmaticlabs/prysm/shared/params" 14 ) 15 16 var hashFn = hashutil.HashProto 17 18 // AttCaches defines the caches used to satisfy attestation pool interface. 19 // These caches are KV store for various attestations 20 // such are unaggregated, aggregated or attestations within a block. 21 type AttCaches struct { 22 aggregatedAttLock sync.RWMutex 23 aggregatedAtt map[[32]byte][]*ethpb.Attestation 24 unAggregateAttLock sync.RWMutex 25 unAggregatedAtt map[[32]byte]*ethpb.Attestation 26 forkchoiceAttLock sync.RWMutex 27 forkchoiceAtt map[[32]byte]*ethpb.Attestation 28 blockAttLock sync.RWMutex 29 blockAtt map[[32]byte][]*ethpb.Attestation 30 seenAtt *cache.Cache 31 } 32 33 // NewAttCaches initializes a new attestation pool consists of multiple KV store in cache for 34 // various kind of attestations. 35 func NewAttCaches() *AttCaches { 36 secsInEpoch := time.Duration(params.BeaconConfig().SlotsPerEpoch.Mul(params.BeaconConfig().SecondsPerSlot)) 37 c := cache.New(secsInEpoch*time.Second, 2*secsInEpoch*time.Second) 38 pool := &AttCaches{ 39 unAggregatedAtt: make(map[[32]byte]*ethpb.Attestation), 40 aggregatedAtt: make(map[[32]byte][]*ethpb.Attestation), 41 forkchoiceAtt: make(map[[32]byte]*ethpb.Attestation), 42 blockAtt: make(map[[32]byte][]*ethpb.Attestation), 43 seenAtt: c, 44 } 45 46 return pool 47 }