2019-05-09 00:27:29 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
|
2024-02-15 05:46:47 +00:00
|
|
|
forkchoicetypes "github.com/prysmaticlabs/prysm/v5/beacon-chain/forkchoice/types"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
|
2019-05-09 00:27:29 +00:00
|
|
|
)
|
|
|
|
|
2023-12-18 16:12:43 +00:00
|
|
|
type AttestationConsensusData struct {
|
|
|
|
Slot primitives.Slot
|
|
|
|
HeadRoot []byte
|
|
|
|
Target forkchoicetypes.Checkpoint
|
|
|
|
Source forkchoicetypes.Checkpoint
|
|
|
|
}
|
2019-05-13 18:42:57 +00:00
|
|
|
|
2023-12-18 16:12:43 +00:00
|
|
|
// AttestationCache stores cached results of AttestationData requests.
|
2019-05-09 00:27:29 +00:00
|
|
|
type AttestationCache struct {
|
2023-12-18 16:12:43 +00:00
|
|
|
a *AttestationConsensusData
|
|
|
|
sync.RWMutex
|
2019-05-09 00:27:29 +00:00
|
|
|
}
|
|
|
|
|
2023-12-18 16:12:43 +00:00
|
|
|
// NewAttestationCache creates a new instance of AttestationCache.
|
2019-05-09 00:27:29 +00:00
|
|
|
func NewAttestationCache() *AttestationCache {
|
2023-12-18 16:12:43 +00:00
|
|
|
return &AttestationCache{}
|
2019-05-09 00:27:29 +00:00
|
|
|
}
|
|
|
|
|
2023-12-18 16:12:43 +00:00
|
|
|
// Get retrieves cached attestation data, recording a cache hit or miss. This method is lock free.
|
|
|
|
func (c *AttestationCache) Get() *AttestationConsensusData {
|
|
|
|
return c.a
|
2019-05-09 00:27:29 +00:00
|
|
|
}
|
|
|
|
|
2023-12-18 16:12:43 +00:00
|
|
|
// Put adds a response to the cache. This method is lock free.
|
|
|
|
func (c *AttestationCache) Put(a *AttestationConsensusData) error {
|
|
|
|
if a == nil {
|
|
|
|
return errors.New("attestation cannot be nil")
|
2019-05-09 00:27:29 +00:00
|
|
|
}
|
2023-12-18 16:12:43 +00:00
|
|
|
c.a = a
|
2019-05-09 00:27:29 +00:00
|
|
|
return nil
|
|
|
|
}
|