mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-23 11:57:18 +00:00
Check fork choice attestation's block and state in DB (#4475)
This commit is contained in:
parent
9d8364bdfa
commit
01b8a84e21
@ -401,6 +401,7 @@ func (s *Store) filterBlockTree(ctx context.Context, blockRoot [32]byte, filtere
|
||||
return false, nil
|
||||
}
|
||||
|
||||
|
||||
headState, err := s.db.State(ctx, blockRoot)
|
||||
if err != nil {
|
||||
return false, err
|
||||
|
@ -74,6 +74,12 @@ func (s *Service) processAttestation() {
|
||||
ctx := context.Background()
|
||||
atts := s.attPool.ForkchoiceAttestations()
|
||||
for _, a := range atts {
|
||||
hasState := s.beaconDB.HasState(ctx, bytesutil.ToBytes32(a.Data.BeaconBlockRoot))
|
||||
hasBlock := s.beaconDB.HasBlock(ctx, bytesutil.ToBytes32(a.Data.BeaconBlockRoot))
|
||||
if !(hasState && hasBlock) {
|
||||
continue
|
||||
}
|
||||
|
||||
if err := s.attPool.DeleteForkchoiceAttestation(a); err != nil {
|
||||
log.WithError(err).Error("Could not delete fork choice attestation in pool")
|
||||
}
|
||||
|
@ -55,6 +55,7 @@ type Database interface {
|
||||
SaveState(ctx context.Context, state *ethereum_beacon_p2p_v1.BeaconState, blockRoot [32]byte) error
|
||||
DeleteState(ctx context.Context, blockRoot [32]byte) error
|
||||
DeleteStates(ctx context.Context, blockRoots [][32]byte) error
|
||||
HasState(ctx context.Context, blockRoot [32]byte) bool
|
||||
// Slashing operations.
|
||||
ProposerSlashing(ctx context.Context, slashingRoot [32]byte) (*eth.ProposerSlashing, error)
|
||||
AttesterSlashing(ctx context.Context, slashingRoot [32]byte) (*eth.AttesterSlashing, error)
|
||||
|
@ -281,6 +281,11 @@ func (e Exporter) DeleteStates(ctx context.Context, blockRoots [][32]byte) error
|
||||
return e.db.DeleteStates(ctx, blockRoots)
|
||||
}
|
||||
|
||||
// HasState -- passthrough.
|
||||
func (e Exporter) HasState(ctx context.Context, blockRoot [32]byte) bool {
|
||||
return e.db.HasState(ctx, blockRoot)
|
||||
}
|
||||
|
||||
// IsFinalizedBlock -- passthrough.
|
||||
func (e Exporter) IsFinalizedBlock(ctx context.Context, blockRoot [32]byte) bool {
|
||||
return e.db.IsFinalizedBlock(ctx, blockRoot)
|
||||
|
@ -98,6 +98,20 @@ func (k *Store) SaveState(ctx context.Context, state *pb.BeaconState, blockRoot
|
||||
})
|
||||
}
|
||||
|
||||
// HasState checks if a state by root exists in the db.
|
||||
func (k *Store) HasState(ctx context.Context, blockRoot [32]byte) bool {
|
||||
ctx, span := trace.StartSpan(ctx, "BeaconDB.HasState")
|
||||
defer span.End()
|
||||
var exists bool
|
||||
// #nosec G104. Always returns nil.
|
||||
k.db.View(func(tx *bolt.Tx) error {
|
||||
bucket := tx.Bucket(stateBucket)
|
||||
exists = bucket.Get(blockRoot[:]) != nil
|
||||
return nil
|
||||
})
|
||||
return exists
|
||||
}
|
||||
|
||||
// DeleteState by block root.
|
||||
func (k *Store) DeleteState(ctx context.Context, blockRoot [32]byte) error {
|
||||
ctx, span := trace.StartSpan(ctx, "BeaconDB.DeleteState")
|
||||
|
@ -18,10 +18,18 @@ func TestState_CanSaveRetrieve(t *testing.T) {
|
||||
s := &pb.BeaconState{Slot: 100}
|
||||
r := [32]byte{'A'}
|
||||
|
||||
if db.HasState(context.Background(), r) {
|
||||
t.Fatal("wanted false")
|
||||
}
|
||||
|
||||
if err := db.SaveState(context.Background(), s, r); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !db.HasState(context.Background(), r) {
|
||||
t.Fatal("wanted true")
|
||||
}
|
||||
|
||||
savedS, err := db.State(context.Background(), r)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
Loading…
Reference in New Issue
Block a user