prysm-pulse/beacon-chain/operations/attestations/kv/seen_bits.go
Raul Jordan 5aac06f04e
Move EthereumAPIs Into Prysm (#8968)
* begin move

* use same import path

* imports

* regen protos

* regen

* no rename

* generate ssz

* gaz

* fmt

* edit build file

* imports

* modify

* remove generated files

* remove protos

* edit imports in prysm

* beacon chain all builds

* edit script

* add generated pbs

* add replace rules

* license for ethereumapis protos

* change visibility

* fmt

* update build files to gaz ignore

* use proper form

* edit imports

* wrap block

* revert scripts

* revert go mod
2021-06-02 18:49:52 -05:00

60 lines
1.4 KiB
Go

package kv
import (
"github.com/patrickmn/go-cache"
"github.com/pkg/errors"
"github.com/prysmaticlabs/go-bitfield"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
)
func (c *AttCaches) insertSeenBit(att *ethpb.Attestation) error {
r, err := hashFn(att.Data)
if err != nil {
return err
}
v, ok := c.seenAtt.Get(string(r[:]))
if ok {
seenBits, ok := v.([]bitfield.Bitlist)
if !ok {
return errors.New("could not convert to bitlist type")
}
alreadyExists := false
for _, bit := range seenBits {
if bit.Len() == att.AggregationBits.Len() && bit.Contains(att.AggregationBits) {
alreadyExists = true
break
}
}
if !alreadyExists {
seenBits = append(seenBits, att.AggregationBits)
}
c.seenAtt.Set(string(r[:]), seenBits, cache.DefaultExpiration /* one epoch */)
return nil
}
c.seenAtt.Set(string(r[:]), []bitfield.Bitlist{att.AggregationBits}, cache.DefaultExpiration /* one epoch */)
return nil
}
func (c *AttCaches) hasSeenBit(att *ethpb.Attestation) (bool, error) {
r, err := hashFn(att.Data)
if err != nil {
return false, err
}
v, ok := c.seenAtt.Get(string(r[:]))
if ok {
seenBits, ok := v.([]bitfield.Bitlist)
if !ok {
return false, errors.New("could not convert to bitlist type")
}
for _, bit := range seenBits {
if bit.Len() == att.AggregationBits.Len() && bit.Contains(att.AggregationBits) {
return true, nil
}
}
}
return false, nil
}