prysm-pulse/beacon-chain/operations/synccommittee/message.go
Raul Jordan d077483577
Add V3 Suffix to All Prysm Packages (#11083)
* v3 import renamings

* tidy

* fmt

* rev

* Update beacon-chain/core/epoch/precompute/reward_penalty_test.go

* Update beacon-chain/core/helpers/validators_test.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/iface/BUILD.bazel

* Update beacon-chain/db/kv/kv.go

* Update beacon-chain/db/kv/state.go

* Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go

* Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go

* Update beacon-chain/sync/initial-sync/service.go

* fix deps

* fix bad replacements

* fix bad replacements

* change back

* gohashtree version

* fix deps

Co-authored-by: Nishant Das <nishdas93@gmail.com>
Co-authored-by: Potuz <potuz@prysmaticlabs.com>
2022-08-16 12:20:13 +00:00

80 lines
2.2 KiB
Go

package synccommittee
import (
"github.com/pkg/errors"
types "github.com/prysmaticlabs/prysm/v3/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/v3/container/queue"
ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1"
)
// SaveSyncCommitteeMessage saves a sync committee message in to a priority queue.
// The priority queue capped at syncCommitteeMaxQueueSize contributions.
func (s *Store) SaveSyncCommitteeMessage(msg *ethpb.SyncCommitteeMessage) error {
if msg == nil {
return errNilMessage
}
s.messageLock.Lock()
defer s.messageLock.Unlock()
item, err := s.messageCache.PopByKey(syncCommitteeKey(msg.Slot))
if err != nil {
return err
}
copied := ethpb.CopySyncCommitteeMessage(msg)
// Messages exist in the queue. Append instead of insert new.
if item != nil {
messages, ok := item.Value.([]*ethpb.SyncCommitteeMessage)
if !ok {
return errors.New("not typed []ethpb.SyncCommitteeMessage")
}
messages = append(messages, copied)
savedSyncCommitteeMessageTotal.Inc()
return s.messageCache.Push(&queue.Item{
Key: syncCommitteeKey(msg.Slot),
Value: messages,
Priority: int64(msg.Slot),
})
}
// Message does not exist. Insert new.
if err := s.messageCache.Push(&queue.Item{
Key: syncCommitteeKey(msg.Slot),
Value: []*ethpb.SyncCommitteeMessage{copied},
Priority: int64(msg.Slot),
}); err != nil {
return err
}
savedSyncCommitteeMessageTotal.Inc()
// Trim messages in queue down to syncCommitteeMaxQueueSize.
if s.messageCache.Len() > syncCommitteeMaxQueueSize {
if _, err := s.messageCache.Pop(); err != nil {
return err
}
}
return nil
}
// SyncCommitteeMessages returns sync committee messages by slot from the priority queue.
// Upon retrieval, the message is removed from the queue.
func (s *Store) SyncCommitteeMessages(slot types.Slot) ([]*ethpb.SyncCommitteeMessage, error) {
s.messageLock.RLock()
defer s.messageLock.RUnlock()
item := s.messageCache.RetrieveByKey(syncCommitteeKey(slot))
if item == nil {
return nil, nil
}
messages, ok := item.Value.([]*ethpb.SyncCommitteeMessage)
if !ok {
return nil, errors.New("not typed []ethpb.SyncCommitteeMessage")
}
return messages, nil
}