Better receiver names in beacon chain module (#8286)

This commit is contained in:
Radosław Kapka
2021-01-19 14:21:32 +01:00
committed by GitHub
parent 20b836d038
commit f89fd67952
12 changed files with 331 additions and 331 deletions

View File

@@ -51,19 +51,19 @@ type ChainService struct {
}
// StateNotifier mocks the same method in the chain service.
func (ms *ChainService) StateNotifier() statefeed.Notifier {
if ms.stateNotifier == nil {
ms.stateNotifier = &MockStateNotifier{}
func (s *ChainService) StateNotifier() statefeed.Notifier {
if s.stateNotifier == nil {
s.stateNotifier = &MockStateNotifier{}
}
return ms.stateNotifier
return s.stateNotifier
}
// BlockNotifier mocks the same method in the chain service.
func (ms *ChainService) BlockNotifier() blockfeed.Notifier {
if ms.blockNotifier == nil {
ms.blockNotifier = &MockBlockNotifier{}
func (s *ChainService) BlockNotifier() blockfeed.Notifier {
if s.blockNotifier == nil {
s.blockNotifier = &MockBlockNotifier{}
}
return ms.blockNotifier
return s.blockNotifier
}
// MockBlockNotifier mocks the block notifier.
@@ -72,11 +72,11 @@ type MockBlockNotifier struct {
}
// BlockFeed returns a block feed.
func (msn *MockBlockNotifier) BlockFeed() *event.Feed {
if msn.feed == nil {
msn.feed = new(event.Feed)
func (mbn *MockBlockNotifier) BlockFeed() *event.Feed {
if mbn.feed == nil {
mbn.feed = new(event.Feed)
}
return msn.feed
return mbn.feed
}
// MockStateNotifier mocks the state notifier.
@@ -125,11 +125,11 @@ func (msn *MockStateNotifier) StateFeed() *event.Feed {
}
// OperationNotifier mocks the same method in the chain service.
func (ms *ChainService) OperationNotifier() opfeed.Notifier {
if ms.opNotifier == nil {
ms.opNotifier = &MockOperationNotifier{}
func (s *ChainService) OperationNotifier() opfeed.Notifier {
if s.opNotifier == nil {
s.opNotifier = &MockOperationNotifier{}
}
return ms.opNotifier
return s.opNotifier
}
// MockOperationNotifier mocks the operation notifier.
@@ -146,227 +146,227 @@ func (mon *MockOperationNotifier) OperationFeed() *event.Feed {
}
// ReceiveBlockInitialSync mocks ReceiveBlockInitialSync method in chain service.
func (ms *ChainService) ReceiveBlockInitialSync(ctx context.Context, block *ethpb.SignedBeaconBlock, _ [32]byte) error {
if ms.State == nil {
ms.State = &stateTrie.BeaconState{}
func (s *ChainService) ReceiveBlockInitialSync(ctx context.Context, block *ethpb.SignedBeaconBlock, _ [32]byte) error {
if s.State == nil {
s.State = &stateTrie.BeaconState{}
}
if !bytes.Equal(ms.Root, block.Block.ParentRoot) {
return errors.Errorf("wanted %#x but got %#x", ms.Root, block.Block.ParentRoot)
if !bytes.Equal(s.Root, block.Block.ParentRoot) {
return errors.Errorf("wanted %#x but got %#x", s.Root, block.Block.ParentRoot)
}
if err := ms.State.SetSlot(block.Block.Slot); err != nil {
if err := s.State.SetSlot(block.Block.Slot); err != nil {
return err
}
ms.BlocksReceived = append(ms.BlocksReceived, block)
s.BlocksReceived = append(s.BlocksReceived, block)
signingRoot, err := block.Block.HashTreeRoot()
if err != nil {
return err
}
if ms.DB != nil {
if err := ms.DB.SaveBlock(ctx, block); err != nil {
if s.DB != nil {
if err := s.DB.SaveBlock(ctx, block); err != nil {
return err
}
logrus.Infof("Saved block with root: %#x at slot %d", signingRoot, block.Block.Slot)
}
ms.Root = signingRoot[:]
ms.Block = block
s.Root = signingRoot[:]
s.Block = block
return nil
}
// ReceiveBlockBatch processes blocks in batches from initial-sync.
func (ms *ChainService) ReceiveBlockBatch(ctx context.Context, blks []*ethpb.SignedBeaconBlock, _ [][32]byte) error {
if ms.State == nil {
ms.State = &stateTrie.BeaconState{}
func (s *ChainService) ReceiveBlockBatch(ctx context.Context, blks []*ethpb.SignedBeaconBlock, _ [][32]byte) error {
if s.State == nil {
s.State = &stateTrie.BeaconState{}
}
for _, block := range blks {
if !bytes.Equal(ms.Root, block.Block.ParentRoot) {
return errors.Errorf("wanted %#x but got %#x", ms.Root, block.Block.ParentRoot)
if !bytes.Equal(s.Root, block.Block.ParentRoot) {
return errors.Errorf("wanted %#x but got %#x", s.Root, block.Block.ParentRoot)
}
if err := ms.State.SetSlot(block.Block.Slot); err != nil {
if err := s.State.SetSlot(block.Block.Slot); err != nil {
return err
}
ms.BlocksReceived = append(ms.BlocksReceived, block)
s.BlocksReceived = append(s.BlocksReceived, block)
signingRoot, err := block.Block.HashTreeRoot()
if err != nil {
return err
}
if ms.DB != nil {
if err := ms.DB.SaveBlock(ctx, block); err != nil {
if s.DB != nil {
if err := s.DB.SaveBlock(ctx, block); err != nil {
return err
}
logrus.Infof("Saved block with root: %#x at slot %d", signingRoot, block.Block.Slot)
}
ms.Root = signingRoot[:]
ms.Block = block
s.Root = signingRoot[:]
s.Block = block
}
return nil
}
// ReceiveBlock mocks ReceiveBlock method in chain service.
func (ms *ChainService) ReceiveBlock(ctx context.Context, block *ethpb.SignedBeaconBlock, _ [32]byte) error {
if ms.State == nil {
ms.State = &stateTrie.BeaconState{}
func (s *ChainService) ReceiveBlock(ctx context.Context, block *ethpb.SignedBeaconBlock, _ [32]byte) error {
if s.State == nil {
s.State = &stateTrie.BeaconState{}
}
if !bytes.Equal(ms.Root, block.Block.ParentRoot) {
return errors.Errorf("wanted %#x but got %#x", ms.Root, block.Block.ParentRoot)
if !bytes.Equal(s.Root, block.Block.ParentRoot) {
return errors.Errorf("wanted %#x but got %#x", s.Root, block.Block.ParentRoot)
}
if err := ms.State.SetSlot(block.Block.Slot); err != nil {
if err := s.State.SetSlot(block.Block.Slot); err != nil {
return err
}
ms.BlocksReceived = append(ms.BlocksReceived, block)
s.BlocksReceived = append(s.BlocksReceived, block)
signingRoot, err := block.Block.HashTreeRoot()
if err != nil {
return err
}
if ms.DB != nil {
if err := ms.DB.SaveBlock(ctx, block); err != nil {
if s.DB != nil {
if err := s.DB.SaveBlock(ctx, block); err != nil {
return err
}
logrus.Infof("Saved block with root: %#x at slot %d", signingRoot, block.Block.Slot)
}
ms.Root = signingRoot[:]
ms.Block = block
s.Root = signingRoot[:]
s.Block = block
return nil
}
// HeadSlot mocks HeadSlot method in chain service.
func (ms *ChainService) HeadSlot() uint64 {
if ms.State == nil {
func (s *ChainService) HeadSlot() uint64 {
if s.State == nil {
return 0
}
return ms.State.Slot()
return s.State.Slot()
}
// HeadRoot mocks HeadRoot method in chain service.
func (ms *ChainService) HeadRoot(_ context.Context) ([]byte, error) {
if len(ms.Root) > 0 {
return ms.Root, nil
func (s *ChainService) HeadRoot(_ context.Context) ([]byte, error) {
if len(s.Root) > 0 {
return s.Root, nil
}
return make([]byte, 32), nil
}
// HeadBlock mocks HeadBlock method in chain service.
func (ms *ChainService) HeadBlock(context.Context) (*ethpb.SignedBeaconBlock, error) {
return ms.Block, nil
func (s *ChainService) HeadBlock(context.Context) (*ethpb.SignedBeaconBlock, error) {
return s.Block, nil
}
// HeadState mocks HeadState method in chain service.
func (ms *ChainService) HeadState(context.Context) (*stateTrie.BeaconState, error) {
return ms.State, nil
func (s *ChainService) HeadState(context.Context) (*stateTrie.BeaconState, error) {
return s.State, nil
}
// CurrentFork mocks HeadState method in chain service.
func (ms *ChainService) CurrentFork() *pb.Fork {
return ms.Fork
func (s *ChainService) CurrentFork() *pb.Fork {
return s.Fork
}
// FinalizedCheckpt mocks FinalizedCheckpt method in chain service.
func (ms *ChainService) FinalizedCheckpt() *ethpb.Checkpoint {
return ms.FinalizedCheckPoint
func (s *ChainService) FinalizedCheckpt() *ethpb.Checkpoint {
return s.FinalizedCheckPoint
}
// CurrentJustifiedCheckpt mocks CurrentJustifiedCheckpt method in chain service.
func (ms *ChainService) CurrentJustifiedCheckpt() *ethpb.Checkpoint {
return ms.CurrentJustifiedCheckPoint
func (s *ChainService) CurrentJustifiedCheckpt() *ethpb.Checkpoint {
return s.CurrentJustifiedCheckPoint
}
// PreviousJustifiedCheckpt mocks PreviousJustifiedCheckpt method in chain service.
func (ms *ChainService) PreviousJustifiedCheckpt() *ethpb.Checkpoint {
return ms.PreviousJustifiedCheckPoint
func (s *ChainService) PreviousJustifiedCheckpt() *ethpb.Checkpoint {
return s.PreviousJustifiedCheckPoint
}
// ReceiveAttestation mocks ReceiveAttestation method in chain service.
func (ms *ChainService) ReceiveAttestation(_ context.Context, _ *ethpb.Attestation) error {
func (s *ChainService) ReceiveAttestation(_ context.Context, _ *ethpb.Attestation) error {
return nil
}
// ReceiveAttestationNoPubsub mocks ReceiveAttestationNoPubsub method in chain service.
func (ms *ChainService) ReceiveAttestationNoPubsub(context.Context, *ethpb.Attestation) error {
func (s *ChainService) ReceiveAttestationNoPubsub(context.Context, *ethpb.Attestation) error {
return nil
}
// AttestationPreState mocks AttestationPreState method in chain service.
func (ms *ChainService) AttestationPreState(_ context.Context, _ *ethpb.Attestation) (*stateTrie.BeaconState, error) {
return ms.State, nil
func (s *ChainService) AttestationPreState(_ context.Context, _ *ethpb.Attestation) (*stateTrie.BeaconState, error) {
return s.State, nil
}
// HeadValidatorsIndices mocks the same method in the chain service.
func (ms *ChainService) HeadValidatorsIndices(_ context.Context, epoch uint64) ([]uint64, error) {
if ms.State == nil {
func (s *ChainService) HeadValidatorsIndices(_ context.Context, epoch uint64) ([]uint64, error) {
if s.State == nil {
return []uint64{}, nil
}
return helpers.ActiveValidatorIndices(ms.State, epoch)
return helpers.ActiveValidatorIndices(s.State, epoch)
}
// HeadSeed mocks the same method in the chain service.
func (ms *ChainService) HeadSeed(_ context.Context, epoch uint64) ([32]byte, error) {
return helpers.Seed(ms.State, epoch, params.BeaconConfig().DomainBeaconAttester)
func (s *ChainService) HeadSeed(_ context.Context, epoch uint64) ([32]byte, error) {
return helpers.Seed(s.State, epoch, params.BeaconConfig().DomainBeaconAttester)
}
// HeadETH1Data provides the current ETH1Data of the head state.
func (ms *ChainService) HeadETH1Data() *ethpb.Eth1Data {
return ms.ETH1Data
func (s *ChainService) HeadETH1Data() *ethpb.Eth1Data {
return s.ETH1Data
}
// ProtoArrayStore mocks the same method in the chain service.
func (ms *ChainService) ProtoArrayStore() *protoarray.Store {
return ms.ForkChoiceStore
func (s *ChainService) ProtoArrayStore() *protoarray.Store {
return s.ForkChoiceStore
}
// GenesisTime mocks the same method in the chain service.
func (ms *ChainService) GenesisTime() time.Time {
return ms.Genesis
func (s *ChainService) GenesisTime() time.Time {
return s.Genesis
}
// GenesisValidatorRoot mocks the same method in the chain service.
func (ms *ChainService) GenesisValidatorRoot() [32]byte {
return ms.ValidatorsRoot
func (s *ChainService) GenesisValidatorRoot() [32]byte {
return s.ValidatorsRoot
}
// CurrentSlot mocks the same method in the chain service.
func (ms *ChainService) CurrentSlot() uint64 {
if ms.Slot != nil {
return *ms.Slot
func (s *ChainService) CurrentSlot() uint64 {
if s.Slot != nil {
return *s.Slot
}
return uint64(time.Now().Unix()-ms.Genesis.Unix()) / params.BeaconConfig().SecondsPerSlot
return uint64(time.Now().Unix()-s.Genesis.Unix()) / params.BeaconConfig().SecondsPerSlot
}
// Participation mocks the same method in the chain service.
func (ms *ChainService) Participation(_ uint64) *precompute.Balance {
return ms.Balance
func (s *ChainService) Participation(_ uint64) *precompute.Balance {
return s.Balance
}
// IsValidAttestation always returns true.
func (ms *ChainService) IsValidAttestation(_ context.Context, _ *ethpb.Attestation) bool {
return ms.ValidAttestation
func (s *ChainService) IsValidAttestation(_ context.Context, _ *ethpb.Attestation) bool {
return s.ValidAttestation
}
// IsCanonical returns and determines whether a block with the provided root is part of
// the canonical chain.
func (ms *ChainService) IsCanonical(_ context.Context, r [32]byte) (bool, error) {
if ms.CanonicalRoots != nil {
_, ok := ms.CanonicalRoots[r]
func (s *ChainService) IsCanonical(_ context.Context, r [32]byte) (bool, error) {
if s.CanonicalRoots != nil {
_, ok := s.CanonicalRoots[r]
return ok, nil
}
return true, nil
}
// HasInitSyncBlock mocks the same method in the chain service.
func (ms *ChainService) HasInitSyncBlock(_ [32]byte) bool {
func (s *ChainService) HasInitSyncBlock(_ [32]byte) bool {
return false
}
// HeadGenesisValidatorRoot mocks HeadGenesisValidatorRoot method in chain service.
func (ms *ChainService) HeadGenesisValidatorRoot() [32]byte {
func (s *ChainService) HeadGenesisValidatorRoot() [32]byte {
return [32]byte{}
}
// VerifyBlkDescendant mocks VerifyBlkDescendant and always returns nil.
func (ms *ChainService) VerifyBlkDescendant(_ context.Context, _ [32]byte) error {
return ms.VerifyBlkDescendantErr
func (s *ChainService) VerifyBlkDescendant(_ context.Context, _ [32]byte) error {
return s.VerifyBlkDescendantErr
}
// VerifyLmdFfgConsistency mocks VerifyLmdFfgConsistency and always returns nil.
func (ms *ChainService) VerifyLmdFfgConsistency(_ context.Context, a *ethpb.Attestation) error {
func (s *ChainService) VerifyLmdFfgConsistency(_ context.Context, a *ethpb.Attestation) error {
if !bytes.Equal(a.Data.BeaconBlockRoot, a.Data.Target.Root) {
return errors.New("LMD and FFG miss matched")
}
@@ -374,8 +374,8 @@ func (ms *ChainService) VerifyLmdFfgConsistency(_ context.Context, a *ethpb.Atte
}
// VerifyFinalizedConsistency mocks VerifyFinalizedConsistency and always returns nil.
func (ms *ChainService) VerifyFinalizedConsistency(_ context.Context, r []byte) error {
if !bytes.Equal(r, ms.FinalizedCheckPoint.Root) {
func (s *ChainService) VerifyFinalizedConsistency(_ context.Context, r []byte) error {
if !bytes.Equal(r, s.FinalizedCheckPoint.Root) {
return errors.New("Root and finalized store are not consistent")
}
return nil

View File

@@ -41,24 +41,24 @@ func newSubnetIDs() *subnetIDs {
}
// AddAttesterSubnetID adds the subnet index for subscribing subnet for the attester of a given slot.
func (c *subnetIDs) AddAttesterSubnetID(slot, subnetID uint64) {
c.attesterLock.Lock()
defer c.attesterLock.Unlock()
func (s *subnetIDs) AddAttesterSubnetID(slot, subnetID uint64) {
s.attesterLock.Lock()
defer s.attesterLock.Unlock()
ids := []uint64{subnetID}
val, exists := c.attester.Get(slot)
val, exists := s.attester.Get(slot)
if exists {
ids = sliceutil.UnionUint64(append(val.([]uint64), ids...))
}
c.attester.Add(slot, ids)
s.attester.Add(slot, ids)
}
// GetAttesterSubnetIDs gets the subnet IDs for subscribed subnets for attesters of the slot.
func (c *subnetIDs) GetAttesterSubnetIDs(slot uint64) []uint64 {
c.attesterLock.RLock()
defer c.attesterLock.RUnlock()
func (s *subnetIDs) GetAttesterSubnetIDs(slot uint64) []uint64 {
s.attesterLock.RLock()
defer s.attesterLock.RUnlock()
val, exists := c.attester.Get(slot)
val, exists := s.attester.Get(slot)
if !exists {
return nil
}
@@ -69,24 +69,24 @@ func (c *subnetIDs) GetAttesterSubnetIDs(slot uint64) []uint64 {
}
// AddAggregatorSubnetID adds the subnet ID for subscribing subnet for the aggregator of a given slot.
func (c *subnetIDs) AddAggregatorSubnetID(slot, subnetID uint64) {
c.aggregatorLock.Lock()
defer c.aggregatorLock.Unlock()
func (s *subnetIDs) AddAggregatorSubnetID(slot, subnetID uint64) {
s.aggregatorLock.Lock()
defer s.aggregatorLock.Unlock()
ids := []uint64{subnetID}
val, exists := c.aggregator.Get(slot)
val, exists := s.aggregator.Get(slot)
if exists {
ids = sliceutil.UnionUint64(append(val.([]uint64), ids...))
}
c.aggregator.Add(slot, ids)
s.aggregator.Add(slot, ids)
}
// GetAggregatorSubnetIDs gets the subnet IDs for subscribing subnet for aggregator of the slot.
func (c *subnetIDs) GetAggregatorSubnetIDs(slot uint64) []uint64 {
c.aggregatorLock.RLock()
defer c.aggregatorLock.RUnlock()
func (s *subnetIDs) GetAggregatorSubnetIDs(slot uint64) []uint64 {
s.aggregatorLock.RLock()
defer s.aggregatorLock.RUnlock()
val, exists := c.aggregator.Get(slot)
val, exists := s.aggregator.Get(slot)
if !exists {
return []uint64{}
}
@@ -95,11 +95,11 @@ func (c *subnetIDs) GetAggregatorSubnetIDs(slot uint64) []uint64 {
// GetPersistentSubnets retrieves the persistent subnet and expiration time of that validator's
// subscription.
func (c *subnetIDs) GetPersistentSubnets(pubkey []byte) ([]uint64, bool, time.Time) {
c.subnetsLock.RLock()
defer c.subnetsLock.RUnlock()
func (s *subnetIDs) GetPersistentSubnets(pubkey []byte) ([]uint64, bool, time.Time) {
s.subnetsLock.RLock()
defer s.subnetsLock.RUnlock()
id, duration, ok := c.persistentSubnets.GetWithExpiration(string(pubkey))
id, duration, ok := s.persistentSubnets.GetWithExpiration(string(pubkey))
if !ok {
return []uint64{}, ok, time.Time{}
}
@@ -108,11 +108,11 @@ func (c *subnetIDs) GetPersistentSubnets(pubkey []byte) ([]uint64, bool, time.Ti
// GetAllSubnets retrieves all the non-expired subscribed subnets of all the validators
// in the cache.
func (c *subnetIDs) GetAllSubnets() []uint64 {
c.subnetsLock.RLock()
defer c.subnetsLock.RUnlock()
func (s *subnetIDs) GetAllSubnets() []uint64 {
s.subnetsLock.RLock()
defer s.subnetsLock.RUnlock()
itemsMap := c.persistentSubnets.Items()
itemsMap := s.persistentSubnets.Items()
var committees []uint64
for _, v := range itemsMap {
@@ -126,9 +126,9 @@ func (c *subnetIDs) GetAllSubnets() []uint64 {
// AddPersistentCommittee adds the relevant committee for that particular validator along with its
// expiration period.
func (c *subnetIDs) AddPersistentCommittee(pubkey []byte, comIndex []uint64, duration time.Duration) {
c.subnetsLock.Lock()
defer c.subnetsLock.Unlock()
func (s *subnetIDs) AddPersistentCommittee(pubkey []byte, comIndex []uint64, duration time.Duration) {
s.subnetsLock.Lock()
defer s.subnetsLock.Unlock()
c.persistentSubnets.Set(string(pubkey), comIndex, duration)
s.persistentSubnets.Set(string(pubkey), comIndex, duration)
}

View File

@@ -22,53 +22,53 @@ func newStateSummaryCache() *stateSummaryCache {
}
// put saves a state summary to the initial sync state summaries cache.
func (s *stateSummaryCache) put(r [32]byte, b *pb.StateSummary) {
s.initSyncStateSummariesLock.Lock()
defer s.initSyncStateSummariesLock.Unlock()
s.initSyncStateSummaries[r] = b
func (c *stateSummaryCache) put(r [32]byte, b *pb.StateSummary) {
c.initSyncStateSummariesLock.Lock()
defer c.initSyncStateSummariesLock.Unlock()
c.initSyncStateSummaries[r] = b
}
// has checks if a state summary exists in the initial sync state summaries cache using the root
// of the block.
func (s *stateSummaryCache) has(r [32]byte) bool {
s.initSyncStateSummariesLock.RLock()
defer s.initSyncStateSummariesLock.RUnlock()
_, ok := s.initSyncStateSummaries[r]
func (c *stateSummaryCache) has(r [32]byte) bool {
c.initSyncStateSummariesLock.RLock()
defer c.initSyncStateSummariesLock.RUnlock()
_, ok := c.initSyncStateSummaries[r]
return ok
}
// get retrieves a state summary from the initial sync state summaries cache using the root of
// the block.
func (s *stateSummaryCache) get(r [32]byte) *pb.StateSummary {
s.initSyncStateSummariesLock.RLock()
defer s.initSyncStateSummariesLock.RUnlock()
b := s.initSyncStateSummaries[r]
func (c *stateSummaryCache) get(r [32]byte) *pb.StateSummary {
c.initSyncStateSummariesLock.RLock()
defer c.initSyncStateSummariesLock.RUnlock()
b := c.initSyncStateSummaries[r]
return b
}
// len retrieves the state summary count from the state summaries cache.
func (s *stateSummaryCache) len() int {
s.initSyncStateSummariesLock.RLock()
defer s.initSyncStateSummariesLock.RUnlock()
return len(s.initSyncStateSummaries)
func (c *stateSummaryCache) len() int {
c.initSyncStateSummariesLock.RLock()
defer c.initSyncStateSummariesLock.RUnlock()
return len(c.initSyncStateSummaries)
}
// GetAll retrieves all the beacon state summaries from the initial sync state summaries cache, the returned
// state summaries are unordered.
func (s *stateSummaryCache) getAll() []*pb.StateSummary {
s.initSyncStateSummariesLock.RLock()
defer s.initSyncStateSummariesLock.RUnlock()
func (c *stateSummaryCache) getAll() []*pb.StateSummary {
c.initSyncStateSummariesLock.RLock()
defer c.initSyncStateSummariesLock.RUnlock()
summaries := make([]*pb.StateSummary, 0, len(s.initSyncStateSummaries))
for _, b := range s.initSyncStateSummaries {
summaries := make([]*pb.StateSummary, 0, len(c.initSyncStateSummaries))
for _, b := range c.initSyncStateSummaries {
summaries = append(summaries, b)
}
return summaries
}
// Clear clears out the initial sync state summaries cache.
func (s *stateSummaryCache) clear() {
s.initSyncStateSummariesLock.Lock()
defer s.initSyncStateSummariesLock.Unlock()
s.initSyncStateSummaries = make(map[[32]byte]*pb.StateSummary)
func (c *stateSummaryCache) clear() {
c.initSyncStateSummariesLock.Lock()
defer c.initSyncStateSummariesLock.Unlock()
c.initSyncStateSummaries = make(map[[32]byte]*pb.StateSummary)
}

View File

@@ -12,23 +12,23 @@ import (
// newly aggregated attestations in the pool.
// It tracks the unaggregated attestations that weren't able to aggregate to prevent
// the deletion of unaggregated attestations in the pool.
func (p *AttCaches) AggregateUnaggregatedAttestations() error {
unaggregatedAtts, err := p.UnaggregatedAttestations()
func (c *AttCaches) AggregateUnaggregatedAttestations() error {
unaggregatedAtts, err := c.UnaggregatedAttestations()
if err != nil {
return err
}
return p.aggregateUnaggregatedAttestations(unaggregatedAtts)
return c.aggregateUnaggregatedAttestations(unaggregatedAtts)
}
// AggregateUnaggregatedAttestationsBySlotIndex aggregates the unaggregated attestations and saves
// newly aggregated attestations in the pool. Unaggregated attestations are filtered by slot and
// committee index.
func (p *AttCaches) AggregateUnaggregatedAttestationsBySlotIndex(slot, committeeIndex uint64) error {
unaggregatedAtts := p.UnaggregatedAttestationsBySlotIndex(slot, committeeIndex)
return p.aggregateUnaggregatedAttestations(unaggregatedAtts)
func (c *AttCaches) AggregateUnaggregatedAttestationsBySlotIndex(slot, committeeIndex uint64) error {
unaggregatedAtts := c.UnaggregatedAttestationsBySlotIndex(slot, committeeIndex)
return c.aggregateUnaggregatedAttestations(unaggregatedAtts)
}
func (p *AttCaches) aggregateUnaggregatedAttestations(unaggregatedAtts []*ethpb.Attestation) error {
func (c *AttCaches) aggregateUnaggregatedAttestations(unaggregatedAtts []*ethpb.Attestation) error {
attsByDataRoot := make(map[[32]byte][]*ethpb.Attestation, len(unaggregatedAtts))
for _, att := range unaggregatedAtts {
attDataRoot, err := att.Data.HashTreeRoot()
@@ -58,7 +58,7 @@ func (p *AttCaches) aggregateUnaggregatedAttestations(unaggregatedAtts []*ethpb.
leftOverUnaggregatedAtt[h] = true
}
}
if err := p.SaveAggregatedAttestations(aggregatedAtts); err != nil {
if err := c.SaveAggregatedAttestations(aggregatedAtts); err != nil {
return err
}
}
@@ -72,7 +72,7 @@ func (p *AttCaches) aggregateUnaggregatedAttestations(unaggregatedAtts []*ethpb.
if leftOverUnaggregatedAtt[h] {
continue
}
if err := p.DeleteUnaggregatedAttestation(att); err != nil {
if err := c.DeleteUnaggregatedAttestation(att); err != nil {
return err
}
}
@@ -81,14 +81,14 @@ func (p *AttCaches) aggregateUnaggregatedAttestations(unaggregatedAtts []*ethpb.
}
// SaveAggregatedAttestation saves an aggregated attestation in cache.
func (p *AttCaches) SaveAggregatedAttestation(att *ethpb.Attestation) error {
func (c *AttCaches) SaveAggregatedAttestation(att *ethpb.Attestation) error {
if err := helpers.ValidateNilAttestation(att); err != nil {
return err
}
if !helpers.IsAggregated(att) {
return errors.New("attestation is not aggregated")
}
has, err := p.HasAggregatedAttestation(att)
has, err := c.HasAggregatedAttestation(att)
if err != nil {
return err
}
@@ -96,7 +96,7 @@ func (p *AttCaches) SaveAggregatedAttestation(att *ethpb.Attestation) error {
return nil
}
seen, err := p.hasSeenBit(att)
seen, err := c.hasSeenBit(att)
if err != nil {
return err
}
@@ -109,12 +109,12 @@ func (p *AttCaches) SaveAggregatedAttestation(att *ethpb.Attestation) error {
return errors.Wrap(err, "could not tree hash attestation")
}
copiedAtt := stateTrie.CopyAttestation(att)
p.aggregatedAttLock.Lock()
defer p.aggregatedAttLock.Unlock()
atts, ok := p.aggregatedAtt[r]
c.aggregatedAttLock.Lock()
defer c.aggregatedAttLock.Unlock()
atts, ok := c.aggregatedAtt[r]
if !ok {
atts := []*ethpb.Attestation{copiedAtt}
p.aggregatedAtt[r] = atts
c.aggregatedAtt[r] = atts
return nil
}
@@ -122,15 +122,15 @@ func (p *AttCaches) SaveAggregatedAttestation(att *ethpb.Attestation) error {
if err != nil {
return err
}
p.aggregatedAtt[r] = atts
c.aggregatedAtt[r] = atts
return nil
}
// SaveAggregatedAttestations saves a list of aggregated attestations in cache.
func (p *AttCaches) SaveAggregatedAttestations(atts []*ethpb.Attestation) error {
func (c *AttCaches) SaveAggregatedAttestations(atts []*ethpb.Attestation) error {
for _, att := range atts {
if err := p.SaveAggregatedAttestation(att); err != nil {
if err := c.SaveAggregatedAttestation(att); err != nil {
return err
}
}
@@ -138,13 +138,13 @@ func (p *AttCaches) SaveAggregatedAttestations(atts []*ethpb.Attestation) error
}
// AggregatedAttestations returns the aggregated attestations in cache.
func (p *AttCaches) AggregatedAttestations() []*ethpb.Attestation {
p.aggregatedAttLock.RLock()
defer p.aggregatedAttLock.RUnlock()
func (c *AttCaches) AggregatedAttestations() []*ethpb.Attestation {
c.aggregatedAttLock.RLock()
defer c.aggregatedAttLock.RUnlock()
atts := make([]*ethpb.Attestation, 0)
for _, a := range p.aggregatedAtt {
for _, a := range c.aggregatedAtt {
atts = append(atts, a...)
}
@@ -153,12 +153,12 @@ func (p *AttCaches) AggregatedAttestations() []*ethpb.Attestation {
// AggregatedAttestationsBySlotIndex returns the aggregated attestations in cache,
// filtered by committee index and slot.
func (p *AttCaches) AggregatedAttestationsBySlotIndex(slot, committeeIndex uint64) []*ethpb.Attestation {
func (c *AttCaches) AggregatedAttestationsBySlotIndex(slot, committeeIndex uint64) []*ethpb.Attestation {
atts := make([]*ethpb.Attestation, 0)
p.aggregatedAttLock.RLock()
defer p.aggregatedAttLock.RUnlock()
for _, a := range p.aggregatedAtt {
c.aggregatedAttLock.RLock()
defer c.aggregatedAttLock.RUnlock()
for _, a := range c.aggregatedAtt {
if slot == a[0].Data.Slot && committeeIndex == a[0].Data.CommitteeIndex {
atts = append(atts, a...)
}
@@ -168,7 +168,7 @@ func (p *AttCaches) AggregatedAttestationsBySlotIndex(slot, committeeIndex uint6
}
// DeleteAggregatedAttestation deletes the aggregated attestations in cache.
func (p *AttCaches) DeleteAggregatedAttestation(att *ethpb.Attestation) error {
func (c *AttCaches) DeleteAggregatedAttestation(att *ethpb.Attestation) error {
if err := helpers.ValidateNilAttestation(att); err != nil {
return err
}
@@ -180,13 +180,13 @@ func (p *AttCaches) DeleteAggregatedAttestation(att *ethpb.Attestation) error {
return errors.Wrap(err, "could not tree hash attestation data")
}
if err := p.insertSeenBit(att); err != nil {
if err := c.insertSeenBit(att); err != nil {
return err
}
p.aggregatedAttLock.Lock()
defer p.aggregatedAttLock.Unlock()
attList, ok := p.aggregatedAtt[r]
c.aggregatedAttLock.Lock()
defer c.aggregatedAttLock.Unlock()
attList, ok := c.aggregatedAtt[r]
if !ok {
return nil
}
@@ -198,16 +198,16 @@ func (p *AttCaches) DeleteAggregatedAttestation(att *ethpb.Attestation) error {
}
}
if len(filtered) == 0 {
delete(p.aggregatedAtt, r)
delete(c.aggregatedAtt, r)
} else {
p.aggregatedAtt[r] = filtered
c.aggregatedAtt[r] = filtered
}
return nil
}
// HasAggregatedAttestation checks if the input attestations has already existed in cache.
func (p *AttCaches) HasAggregatedAttestation(att *ethpb.Attestation) (bool, error) {
func (c *AttCaches) HasAggregatedAttestation(att *ethpb.Attestation) (bool, error) {
if err := helpers.ValidateNilAttestation(att); err != nil {
return false, err
}
@@ -216,9 +216,9 @@ func (p *AttCaches) HasAggregatedAttestation(att *ethpb.Attestation) (bool, erro
return false, errors.Wrap(err, "could not tree hash attestation")
}
p.aggregatedAttLock.RLock()
defer p.aggregatedAttLock.RUnlock()
if atts, ok := p.aggregatedAtt[r]; ok {
c.aggregatedAttLock.RLock()
defer c.aggregatedAttLock.RUnlock()
if atts, ok := c.aggregatedAtt[r]; ok {
for _, a := range atts {
if a.AggregationBits.Len() == att.AggregationBits.Len() && a.AggregationBits.Contains(att.AggregationBits) {
return true, nil
@@ -226,9 +226,9 @@ func (p *AttCaches) HasAggregatedAttestation(att *ethpb.Attestation) (bool, erro
}
}
p.blockAttLock.RLock()
defer p.blockAttLock.RUnlock()
if atts, ok := p.blockAtt[r]; ok {
c.blockAttLock.RLock()
defer c.blockAttLock.RUnlock()
if atts, ok := c.blockAtt[r]; ok {
for _, a := range atts {
if a.AggregationBits.Len() == att.AggregationBits.Len() && a.AggregationBits.Contains(att.AggregationBits) {
return true, nil
@@ -240,8 +240,8 @@ func (p *AttCaches) HasAggregatedAttestation(att *ethpb.Attestation) (bool, erro
}
// AggregatedAttestationCount returns the number of aggregated attestations key in the pool.
func (p *AttCaches) AggregatedAttestationCount() int {
p.aggregatedAttLock.RLock()
defer p.aggregatedAttLock.RUnlock()
return len(p.aggregatedAtt)
func (c *AttCaches) AggregatedAttestationCount() int {
c.aggregatedAttLock.RLock()
defer c.aggregatedAttLock.RUnlock()
return len(c.aggregatedAtt)
}

View File

@@ -7,7 +7,7 @@ import (
)
// SaveBlockAttestation saves an block attestation in cache.
func (p *AttCaches) SaveBlockAttestation(att *ethpb.Attestation) error {
func (c *AttCaches) SaveBlockAttestation(att *ethpb.Attestation) error {
if att == nil {
return nil
}
@@ -16,9 +16,9 @@ func (p *AttCaches) SaveBlockAttestation(att *ethpb.Attestation) error {
return errors.Wrap(err, "could not tree hash attestation")
}
p.blockAttLock.Lock()
defer p.blockAttLock.Unlock()
atts, ok := p.blockAtt[r]
c.blockAttLock.Lock()
defer c.blockAttLock.Unlock()
atts, ok := c.blockAtt[r]
if !ok {
atts = make([]*ethpb.Attestation, 0, 1)
}
@@ -30,15 +30,15 @@ func (p *AttCaches) SaveBlockAttestation(att *ethpb.Attestation) error {
}
}
p.blockAtt[r] = append(atts, stateTrie.CopyAttestation(att))
c.blockAtt[r] = append(atts, stateTrie.CopyAttestation(att))
return nil
}
// SaveBlockAttestations saves a list of block attestations in cache.
func (p *AttCaches) SaveBlockAttestations(atts []*ethpb.Attestation) error {
func (c *AttCaches) SaveBlockAttestations(atts []*ethpb.Attestation) error {
for _, att := range atts {
if err := p.SaveBlockAttestation(att); err != nil {
if err := c.SaveBlockAttestation(att); err != nil {
return err
}
}
@@ -47,12 +47,12 @@ func (p *AttCaches) SaveBlockAttestations(atts []*ethpb.Attestation) error {
}
// BlockAttestations returns the block attestations in cache.
func (p *AttCaches) BlockAttestations() []*ethpb.Attestation {
func (c *AttCaches) BlockAttestations() []*ethpb.Attestation {
atts := make([]*ethpb.Attestation, 0)
p.blockAttLock.RLock()
defer p.blockAttLock.RUnlock()
for _, att := range p.blockAtt {
c.blockAttLock.RLock()
defer c.blockAttLock.RUnlock()
for _, att := range c.blockAtt {
atts = append(atts, att...)
}
@@ -60,7 +60,7 @@ func (p *AttCaches) BlockAttestations() []*ethpb.Attestation {
}
// DeleteBlockAttestation deletes a block attestation in cache.
func (p *AttCaches) DeleteBlockAttestation(att *ethpb.Attestation) error {
func (c *AttCaches) DeleteBlockAttestation(att *ethpb.Attestation) error {
if att == nil {
return nil
}
@@ -69,9 +69,9 @@ func (p *AttCaches) DeleteBlockAttestation(att *ethpb.Attestation) error {
return errors.Wrap(err, "could not tree hash attestation")
}
p.blockAttLock.Lock()
defer p.blockAttLock.Unlock()
delete(p.blockAtt, r)
c.blockAttLock.Lock()
defer c.blockAttLock.Unlock()
delete(c.blockAtt, r)
return nil
}

View File

@@ -7,7 +7,7 @@ import (
)
// SaveForkchoiceAttestation saves an forkchoice attestation in cache.
func (p *AttCaches) SaveForkchoiceAttestation(att *ethpb.Attestation) error {
func (c *AttCaches) SaveForkchoiceAttestation(att *ethpb.Attestation) error {
if att == nil {
return nil
}
@@ -17,17 +17,17 @@ func (p *AttCaches) SaveForkchoiceAttestation(att *ethpb.Attestation) error {
}
att = stateTrie.CopyAttestation(att)
p.forkchoiceAttLock.Lock()
defer p.forkchoiceAttLock.Unlock()
p.forkchoiceAtt[r] = att
c.forkchoiceAttLock.Lock()
defer c.forkchoiceAttLock.Unlock()
c.forkchoiceAtt[r] = att
return nil
}
// SaveForkchoiceAttestations saves a list of forkchoice attestations in cache.
func (p *AttCaches) SaveForkchoiceAttestations(atts []*ethpb.Attestation) error {
func (c *AttCaches) SaveForkchoiceAttestations(atts []*ethpb.Attestation) error {
for _, att := range atts {
if err := p.SaveForkchoiceAttestation(att); err != nil {
if err := c.SaveForkchoiceAttestation(att); err != nil {
return err
}
}
@@ -36,12 +36,12 @@ func (p *AttCaches) SaveForkchoiceAttestations(atts []*ethpb.Attestation) error
}
// ForkchoiceAttestations returns the forkchoice attestations in cache.
func (p *AttCaches) ForkchoiceAttestations() []*ethpb.Attestation {
p.forkchoiceAttLock.RLock()
defer p.forkchoiceAttLock.RUnlock()
func (c *AttCaches) ForkchoiceAttestations() []*ethpb.Attestation {
c.forkchoiceAttLock.RLock()
defer c.forkchoiceAttLock.RUnlock()
atts := make([]*ethpb.Attestation, 0, len(p.forkchoiceAtt))
for _, att := range p.forkchoiceAtt {
atts := make([]*ethpb.Attestation, 0, len(c.forkchoiceAtt))
for _, att := range c.forkchoiceAtt {
atts = append(atts, stateTrie.CopyAttestation(att) /* Copied */)
}
@@ -49,7 +49,7 @@ func (p *AttCaches) ForkchoiceAttestations() []*ethpb.Attestation {
}
// DeleteForkchoiceAttestation deletes a forkchoice attestation in cache.
func (p *AttCaches) DeleteForkchoiceAttestation(att *ethpb.Attestation) error {
func (c *AttCaches) DeleteForkchoiceAttestation(att *ethpb.Attestation) error {
if att == nil {
return nil
}
@@ -58,9 +58,9 @@ func (p *AttCaches) DeleteForkchoiceAttestation(att *ethpb.Attestation) error {
return errors.Wrap(err, "could not tree hash attestation")
}
p.forkchoiceAttLock.Lock()
defer p.forkchoiceAttLock.Unlock()
delete(p.forkchoiceAtt, r)
c.forkchoiceAttLock.Lock()
defer c.forkchoiceAttLock.Unlock()
delete(c.forkchoiceAtt, r)
return nil
}

View File

@@ -7,13 +7,13 @@ import (
"github.com/prysmaticlabs/go-bitfield"
)
func (p *AttCaches) insertSeenBit(att *ethpb.Attestation) error {
func (c *AttCaches) insertSeenBit(att *ethpb.Attestation) error {
r, err := hashFn(att.Data)
if err != nil {
return err
}
v, ok := p.seenAtt.Get(string(r[:]))
v, ok := c.seenAtt.Get(string(r[:]))
if ok {
seenBits, ok := v.([]bitfield.Bitlist)
if !ok {
@@ -29,21 +29,21 @@ func (p *AttCaches) insertSeenBit(att *ethpb.Attestation) error {
if !alreadyExists {
seenBits = append(seenBits, att.AggregationBits)
}
p.seenAtt.Set(string(r[:]), seenBits, cache.DefaultExpiration /* one epoch */)
c.seenAtt.Set(string(r[:]), seenBits, cache.DefaultExpiration /* one epoch */)
return nil
}
p.seenAtt.Set(string(r[:]), []bitfield.Bitlist{att.AggregationBits}, cache.DefaultExpiration /* one epoch */)
c.seenAtt.Set(string(r[:]), []bitfield.Bitlist{att.AggregationBits}, cache.DefaultExpiration /* one epoch */)
return nil
}
func (p *AttCaches) hasSeenBit(att *ethpb.Attestation) (bool, error) {
func (c *AttCaches) hasSeenBit(att *ethpb.Attestation) (bool, error) {
r, err := hashFn(att.Data)
if err != nil {
return false, err
}
v, ok := p.seenAtt.Get(string(r[:]))
v, ok := c.seenAtt.Get(string(r[:]))
if ok {
seenBits, ok := v.([]bitfield.Bitlist)
if !ok {

View File

@@ -8,7 +8,7 @@ import (
)
// SaveUnaggregatedAttestation saves an unaggregated attestation in cache.
func (p *AttCaches) SaveUnaggregatedAttestation(att *ethpb.Attestation) error {
func (c *AttCaches) SaveUnaggregatedAttestation(att *ethpb.Attestation) error {
if att == nil {
return nil
}
@@ -16,7 +16,7 @@ func (p *AttCaches) SaveUnaggregatedAttestation(att *ethpb.Attestation) error {
return errors.New("attestation is aggregated")
}
seen, err := p.hasSeenBit(att)
seen, err := c.hasSeenBit(att)
if err != nil {
return err
}
@@ -29,17 +29,17 @@ func (p *AttCaches) SaveUnaggregatedAttestation(att *ethpb.Attestation) error {
return errors.Wrap(err, "could not tree hash attestation")
}
att = stateTrie.CopyAttestation(att) // Copied.
p.unAggregateAttLock.Lock()
defer p.unAggregateAttLock.Unlock()
p.unAggregatedAtt[r] = att
c.unAggregateAttLock.Lock()
defer c.unAggregateAttLock.Unlock()
c.unAggregatedAtt[r] = att
return nil
}
// SaveUnaggregatedAttestations saves a list of unaggregated attestations in cache.
func (p *AttCaches) SaveUnaggregatedAttestations(atts []*ethpb.Attestation) error {
func (c *AttCaches) SaveUnaggregatedAttestations(atts []*ethpb.Attestation) error {
for _, att := range atts {
if err := p.SaveUnaggregatedAttestation(att); err != nil {
if err := c.SaveUnaggregatedAttestation(att); err != nil {
return err
}
}
@@ -48,13 +48,13 @@ func (p *AttCaches) SaveUnaggregatedAttestations(atts []*ethpb.Attestation) erro
}
// UnaggregatedAttestations returns all the unaggregated attestations in cache.
func (p *AttCaches) UnaggregatedAttestations() ([]*ethpb.Attestation, error) {
p.unAggregateAttLock.Lock()
defer p.unAggregateAttLock.Unlock()
unAggregatedAtts := p.unAggregatedAtt
func (c *AttCaches) UnaggregatedAttestations() ([]*ethpb.Attestation, error) {
c.unAggregateAttLock.Lock()
defer c.unAggregateAttLock.Unlock()
unAggregatedAtts := c.unAggregatedAtt
atts := make([]*ethpb.Attestation, 0, len(unAggregatedAtts))
for _, att := range unAggregatedAtts {
seen, err := p.hasSeenBit(att)
seen, err := c.hasSeenBit(att)
if err != nil {
return nil, err
}
@@ -67,13 +67,13 @@ func (p *AttCaches) UnaggregatedAttestations() ([]*ethpb.Attestation, error) {
// UnaggregatedAttestationsBySlotIndex returns the unaggregated attestations in cache,
// filtered by committee index and slot.
func (p *AttCaches) UnaggregatedAttestationsBySlotIndex(slot, committeeIndex uint64) []*ethpb.Attestation {
func (c *AttCaches) UnaggregatedAttestationsBySlotIndex(slot, committeeIndex uint64) []*ethpb.Attestation {
atts := make([]*ethpb.Attestation, 0)
p.unAggregateAttLock.RLock()
defer p.unAggregateAttLock.RUnlock()
c.unAggregateAttLock.RLock()
defer c.unAggregateAttLock.RUnlock()
unAggregatedAtts := p.unAggregatedAtt
unAggregatedAtts := c.unAggregatedAtt
for _, a := range unAggregatedAtts {
if slot == a.Data.Slot && committeeIndex == a.Data.CommitteeIndex {
atts = append(atts, a)
@@ -84,7 +84,7 @@ func (p *AttCaches) UnaggregatedAttestationsBySlotIndex(slot, committeeIndex uin
}
// DeleteUnaggregatedAttestation deletes the unaggregated attestations in cache.
func (p *AttCaches) DeleteUnaggregatedAttestation(att *ethpb.Attestation) error {
func (c *AttCaches) DeleteUnaggregatedAttestation(att *ethpb.Attestation) error {
if att == nil {
return nil
}
@@ -92,7 +92,7 @@ func (p *AttCaches) DeleteUnaggregatedAttestation(att *ethpb.Attestation) error
return errors.New("attestation is aggregated")
}
if err := p.insertSeenBit(att); err != nil {
if err := c.insertSeenBit(att); err != nil {
return err
}
@@ -101,30 +101,30 @@ func (p *AttCaches) DeleteUnaggregatedAttestation(att *ethpb.Attestation) error
return errors.Wrap(err, "could not tree hash attestation")
}
p.unAggregateAttLock.Lock()
defer p.unAggregateAttLock.Unlock()
delete(p.unAggregatedAtt, r)
c.unAggregateAttLock.Lock()
defer c.unAggregateAttLock.Unlock()
delete(c.unAggregatedAtt, r)
return nil
}
// DeleteSeenUnaggregatedAttestations deletes the unaggregated attestations in cache
// that have been already processed once. Returns number of attestations deleted.
func (p *AttCaches) DeleteSeenUnaggregatedAttestations() (int, error) {
p.unAggregateAttLock.Lock()
defer p.unAggregateAttLock.Unlock()
func (c *AttCaches) DeleteSeenUnaggregatedAttestations() (int, error) {
c.unAggregateAttLock.Lock()
defer c.unAggregateAttLock.Unlock()
count := 0
for _, att := range p.unAggregatedAtt {
for _, att := range c.unAggregatedAtt {
if att == nil || helpers.IsAggregated(att) {
continue
}
if seen, err := p.hasSeenBit(att); err == nil && seen {
if seen, err := c.hasSeenBit(att); err == nil && seen {
r, err := hashFn(att)
if err != nil {
return count, errors.Wrap(err, "could not tree hash attestation")
}
delete(p.unAggregatedAtt, r)
delete(c.unAggregatedAtt, r)
count++
}
}
@@ -132,8 +132,8 @@ func (p *AttCaches) DeleteSeenUnaggregatedAttestations() (int, error) {
}
// UnaggregatedAttestationCount returns the number of unaggregated attestations key in the pool.
func (p *AttCaches) UnaggregatedAttestationCount() int {
p.unAggregateAttLock.RLock()
defer p.unAggregateAttLock.RUnlock()
return len(p.unAggregatedAtt)
func (c *AttCaches) UnaggregatedAttestationCount() int {
c.unAggregateAttLock.RLock()
defer c.unAggregateAttLock.RUnlock()
return len(c.unAggregatedAtt)
}

View File

@@ -55,12 +55,12 @@ func (s *Service) CanSubscribe(topic string) bool {
// FilterIncomingSubscriptions is invoked for all RPCs containing subscription notifications.
// This method returns only the topics of interest and may return an error if the subscription
// request contains too many topics.
func (sf *Service) FilterIncomingSubscriptions(_ peer.ID, subs []*pubsubpb.RPC_SubOpts) ([]*pubsubpb.RPC_SubOpts, error) {
func (s *Service) FilterIncomingSubscriptions(_ peer.ID, subs []*pubsubpb.RPC_SubOpts) ([]*pubsubpb.RPC_SubOpts, error) {
if len(subs) > pubsubSubscriptionRequestLimit {
return nil, pubsub.ErrTooManySubscriptions
}
return pubsub.FilterSubscriptions(subs, sf.CanSubscribe), nil
return pubsub.FilterSubscriptions(subs, s.CanSubscribe), nil
}
// scanfcheck uses fmt.Sscanf to check that a given string matches expected format. This method

View File

@@ -50,8 +50,8 @@ func (s *SSZUint64) UnmarshalSSZ(buf []byte) error {
type BeaconBlockByRootsReq [][rootLength]byte
// MarshalSSZTo marshals the block by roots request with the provided byte slice.
func (s *BeaconBlockByRootsReq) MarshalSSZTo(dst []byte) ([]byte, error) {
marshalledObj, err := s.MarshalSSZ()
func (r *BeaconBlockByRootsReq) MarshalSSZTo(dst []byte) ([]byte, error) {
marshalledObj, err := r.MarshalSSZ()
if err != nil {
return nil, err
}
@@ -59,25 +59,25 @@ func (s *BeaconBlockByRootsReq) MarshalSSZTo(dst []byte) ([]byte, error) {
}
// MarshalSSZ Marshals the block by roots request type into the serialized object.
func (s *BeaconBlockByRootsReq) MarshalSSZ() ([]byte, error) {
if len(*s) > int(params.BeaconNetworkConfig().MaxRequestBlocks) {
return nil, errors.Errorf("beacon block by roots request exceeds max size: %d > %d", len(*s), params.BeaconNetworkConfig().MaxRequestBlocks)
func (r *BeaconBlockByRootsReq) MarshalSSZ() ([]byte, error) {
if len(*r) > int(params.BeaconNetworkConfig().MaxRequestBlocks) {
return nil, errors.Errorf("beacon block by roots request exceeds max size: %d > %d", len(*r), params.BeaconNetworkConfig().MaxRequestBlocks)
}
buf := make([]byte, 0, s.SizeSSZ())
for _, r := range *s {
buf := make([]byte, 0, r.SizeSSZ())
for _, r := range *r {
buf = append(buf, r[:]...)
}
return buf, nil
}
// SizeSSZ returns the size of the serialized representation.
func (s *BeaconBlockByRootsReq) SizeSSZ() int {
return len(*s) * rootLength
func (r *BeaconBlockByRootsReq) SizeSSZ() int {
return len(*r) * rootLength
}
// UnmarshalSSZ unmarshals the provided bytes buffer into the
// block by roots request object.
func (s *BeaconBlockByRootsReq) UnmarshalSSZ(buf []byte) error {
func (r *BeaconBlockByRootsReq) UnmarshalSSZ(buf []byte) error {
bufLen := len(buf)
maxLength := int(params.BeaconNetworkConfig().MaxRequestBlocks * rootLength)
if bufLen > maxLength {
@@ -93,7 +93,7 @@ func (s *BeaconBlockByRootsReq) UnmarshalSSZ(buf []byte) error {
copy(rt[:], buf[i*rootLength:(i+1)*rootLength])
roots = append(roots, rt)
}
*s = roots
*r = roots
return nil
}
@@ -101,8 +101,8 @@ func (s *BeaconBlockByRootsReq) UnmarshalSSZ(buf []byte) error {
type ErrorMessage []byte
// MarshalSSZTo marshals the error message with the provided byte slice.
func (s *ErrorMessage) MarshalSSZTo(dst []byte) ([]byte, error) {
marshalledObj, err := s.MarshalSSZ()
func (m *ErrorMessage) MarshalSSZTo(dst []byte) ([]byte, error) {
marshalledObj, err := m.MarshalSSZ()
if err != nil {
return nil, err
}
@@ -110,23 +110,23 @@ func (s *ErrorMessage) MarshalSSZTo(dst []byte) ([]byte, error) {
}
// MarshalSSZ Marshals the error message into the serialized object.
func (s *ErrorMessage) MarshalSSZ() ([]byte, error) {
if len(*s) > maxErrorLength {
return nil, errors.Errorf("error message exceeds max size: %d > %d", len(*s), maxErrorLength)
func (m *ErrorMessage) MarshalSSZ() ([]byte, error) {
if len(*m) > maxErrorLength {
return nil, errors.Errorf("error message exceeds max size: %d > %d", len(*m), maxErrorLength)
}
buf := make([]byte, s.SizeSSZ())
copy(buf, *s)
buf := make([]byte, m.SizeSSZ())
copy(buf, *m)
return buf, nil
}
// SizeSSZ returns the size of the serialized representation.
func (s *ErrorMessage) SizeSSZ() int {
return len(*s)
func (m *ErrorMessage) SizeSSZ() int {
return len(*m)
}
// UnmarshalSSZ unmarshals the provided bytes buffer into the
// error message object.
func (s *ErrorMessage) UnmarshalSSZ(buf []byte) error {
func (m *ErrorMessage) UnmarshalSSZ(buf []byte) error {
bufLen := len(buf)
maxLength := maxErrorLength
if bufLen > maxLength {
@@ -134,6 +134,6 @@ func (s *ErrorMessage) UnmarshalSSZ(buf []byte) error {
}
errMsg := make([]byte, bufLen)
copy(errMsg, buf)
*s = errMsg
*m = errMsg
return nil
}

View File

@@ -78,11 +78,11 @@ func newHeaderCache() *headerCache {
// HeaderInfoByHash fetches headerInfo by its header hash. Returns true with a
// reference to the header info, if exists. Otherwise returns false, nil.
func (b *headerCache) HeaderInfoByHash(hash common.Hash) (bool, *types.HeaderInfo, error) {
b.lock.RLock()
defer b.lock.RUnlock()
func (c *headerCache) HeaderInfoByHash(hash common.Hash) (bool, *types.HeaderInfo, error) {
c.lock.RLock()
defer c.lock.RUnlock()
obj, exists, err := b.hashCache.GetByKey(hash.Hex())
obj, exists, err := c.hashCache.GetByKey(hash.Hex())
if err != nil {
return false, nil, err
}
@@ -104,11 +104,11 @@ func (b *headerCache) HeaderInfoByHash(hash common.Hash) (bool, *types.HeaderInf
// HeaderInfoByHeight fetches headerInfo by its header number. Returns true with a
// reference to the header info, if exists. Otherwise returns false, nil.
func (b *headerCache) HeaderInfoByHeight(height *big.Int) (bool, *types.HeaderInfo, error) {
b.lock.RLock()
defer b.lock.RUnlock()
func (c *headerCache) HeaderInfoByHeight(height *big.Int) (bool, *types.HeaderInfo, error) {
c.lock.RLock()
defer c.lock.RUnlock()
obj, exists, err := b.heightCache.GetByKey(height.String())
obj, exists, err := c.heightCache.GetByKey(height.String())
if err != nil {
return false, nil, err
}
@@ -133,26 +133,26 @@ func (b *headerCache) HeaderInfoByHeight(height *big.Int) (bool, *types.HeaderIn
// size limit. This method should be called in sequential header number order if
// the desired behavior is that the blocks with the highest header number should
// be present in the cache.
func (b *headerCache) AddHeader(hdr *gethTypes.Header) error {
b.lock.Lock()
defer b.lock.Unlock()
func (c *headerCache) AddHeader(hdr *gethTypes.Header) error {
c.lock.Lock()
defer c.lock.Unlock()
hInfo, err := types.HeaderToHeaderInfo(hdr)
if err != nil {
return err
}
if err := b.hashCache.AddIfNotPresent(hInfo); err != nil {
if err := c.hashCache.AddIfNotPresent(hInfo); err != nil {
return err
}
if err := b.heightCache.AddIfNotPresent(hInfo); err != nil {
if err := c.heightCache.AddIfNotPresent(hInfo); err != nil {
return err
}
trim(b.hashCache, maxCacheSize)
trim(b.heightCache, maxCacheSize)
trim(c.hashCache, maxCacheSize)
trim(c.heightCache, maxCacheSize)
headerCacheSize.Set(float64(len(b.hashCache.ListKeys())))
headerCacheSize.Set(float64(len(c.hashCache.ListKeys())))
return nil
}

View File

@@ -15,10 +15,10 @@ type proposerAtts []*ethpb.Attestation
// filter separates attestation list into two groups: valid and invalid attestations.
// The first group passes the all the required checks for attestation to be considered for proposing.
// And attestations from the second group should be deleted.
func (al proposerAtts) filter(ctx context.Context, state *stateTrie.BeaconState) (proposerAtts, proposerAtts) {
validAtts := make([]*ethpb.Attestation, 0, len(al))
invalidAtts := make([]*ethpb.Attestation, 0, len(al))
for _, att := range al {
func (a proposerAtts) filter(ctx context.Context, state *stateTrie.BeaconState) (proposerAtts, proposerAtts) {
validAtts := make([]*ethpb.Attestation, 0, len(a))
invalidAtts := make([]*ethpb.Attestation, 0, len(a))
for _, att := range a {
if _, err := blocks.ProcessAttestationNoVerifySignature(ctx, state, att); err == nil {
validAtts = append(validAtts, att)
continue
@@ -29,36 +29,36 @@ func (al proposerAtts) filter(ctx context.Context, state *stateTrie.BeaconState)
}
// sortByProfitability orders attestations by highest slot and by highest aggregation bit count.
func (al proposerAtts) sortByProfitability() proposerAtts {
if len(al) < 2 {
return al
func (a proposerAtts) sortByProfitability() proposerAtts {
if len(a) < 2 {
return a
}
sort.Slice(al, func(i, j int) bool {
if al[i].Data.Slot == al[j].Data.Slot {
return al[i].AggregationBits.Count() > al[j].AggregationBits.Count()
sort.Slice(a, func(i, j int) bool {
if a[i].Data.Slot == a[j].Data.Slot {
return a[i].AggregationBits.Count() > a[j].AggregationBits.Count()
}
return al[i].Data.Slot > al[j].Data.Slot
return a[i].Data.Slot > a[j].Data.Slot
})
return al
return a
}
// limitToMaxAttestations limits attestations to maximum attestations per block.
func (al proposerAtts) limitToMaxAttestations() proposerAtts {
if uint64(len(al)) > params.BeaconConfig().MaxAttestations {
return al[:params.BeaconConfig().MaxAttestations]
func (a proposerAtts) limitToMaxAttestations() proposerAtts {
if uint64(len(a)) > params.BeaconConfig().MaxAttestations {
return a[:params.BeaconConfig().MaxAttestations]
}
return al
return a
}
// dedup removes duplicate attestations (ones with the same bits set on).
// Important: not only exact duplicates are removed, but proper subsets are removed too
// (their known bits are redundant and are already contained in their supersets).
func (al proposerAtts) dedup() proposerAtts {
if len(al) < 2 {
return al
func (a proposerAtts) dedup() proposerAtts {
if len(a) < 2 {
return a
}
attsByDataRoot := make(map[[32]byte][]*ethpb.Attestation, len(al))
for _, att := range al {
attsByDataRoot := make(map[[32]byte][]*ethpb.Attestation, len(a))
for _, att := range a {
attDataRoot, err := att.Data.HashTreeRoot()
if err != nil {
continue
@@ -66,7 +66,7 @@ func (al proposerAtts) dedup() proposerAtts {
attsByDataRoot[attDataRoot] = append(attsByDataRoot[attDataRoot], att)
}
uniqAtts := make([]*ethpb.Attestation, 0, len(al))
uniqAtts := make([]*ethpb.Attestation, 0, len(a))
for _, atts := range attsByDataRoot {
for i := 0; i < len(atts); i++ {
a := atts[i]