Check fork choice attestation's block and state in DB (#4475)

This commit is contained in:
terence tsao 2020-01-10 06:25:43 -08:00 committed by GitHub
parent 9d8364bdfa
commit 01b8a84e21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 35 additions and 0 deletions

View File

@ -401,6 +401,7 @@ func (s *Store) filterBlockTree(ctx context.Context, blockRoot [32]byte, filtere
return false, nil return false, nil
} }
headState, err := s.db.State(ctx, blockRoot) headState, err := s.db.State(ctx, blockRoot)
if err != nil { if err != nil {
return false, err return false, err

View File

@ -74,6 +74,12 @@ func (s *Service) processAttestation() {
ctx := context.Background() ctx := context.Background()
atts := s.attPool.ForkchoiceAttestations() atts := s.attPool.ForkchoiceAttestations()
for _, a := range atts { 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 { if err := s.attPool.DeleteForkchoiceAttestation(a); err != nil {
log.WithError(err).Error("Could not delete fork choice attestation in pool") log.WithError(err).Error("Could not delete fork choice attestation in pool")
} }

View File

@ -55,6 +55,7 @@ type Database interface {
SaveState(ctx context.Context, state *ethereum_beacon_p2p_v1.BeaconState, blockRoot [32]byte) error SaveState(ctx context.Context, state *ethereum_beacon_p2p_v1.BeaconState, blockRoot [32]byte) error
DeleteState(ctx context.Context, blockRoot [32]byte) error DeleteState(ctx context.Context, blockRoot [32]byte) error
DeleteStates(ctx context.Context, blockRoots [][32]byte) error DeleteStates(ctx context.Context, blockRoots [][32]byte) error
HasState(ctx context.Context, blockRoot [32]byte) bool
// Slashing operations. // Slashing operations.
ProposerSlashing(ctx context.Context, slashingRoot [32]byte) (*eth.ProposerSlashing, error) ProposerSlashing(ctx context.Context, slashingRoot [32]byte) (*eth.ProposerSlashing, error)
AttesterSlashing(ctx context.Context, slashingRoot [32]byte) (*eth.AttesterSlashing, error) AttesterSlashing(ctx context.Context, slashingRoot [32]byte) (*eth.AttesterSlashing, error)

View File

@ -281,6 +281,11 @@ func (e Exporter) DeleteStates(ctx context.Context, blockRoots [][32]byte) error
return e.db.DeleteStates(ctx, blockRoots) 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. // IsFinalizedBlock -- passthrough.
func (e Exporter) IsFinalizedBlock(ctx context.Context, blockRoot [32]byte) bool { func (e Exporter) IsFinalizedBlock(ctx context.Context, blockRoot [32]byte) bool {
return e.db.IsFinalizedBlock(ctx, blockRoot) return e.db.IsFinalizedBlock(ctx, blockRoot)

View File

@ -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. // DeleteState by block root.
func (k *Store) DeleteState(ctx context.Context, blockRoot [32]byte) error { func (k *Store) DeleteState(ctx context.Context, blockRoot [32]byte) error {
ctx, span := trace.StartSpan(ctx, "BeaconDB.DeleteState") ctx, span := trace.StartSpan(ctx, "BeaconDB.DeleteState")

View File

@ -18,10 +18,18 @@ func TestState_CanSaveRetrieve(t *testing.T) {
s := &pb.BeaconState{Slot: 100} s := &pb.BeaconState{Slot: 100}
r := [32]byte{'A'} r := [32]byte{'A'}
if db.HasState(context.Background(), r) {
t.Fatal("wanted false")
}
if err := db.SaveState(context.Background(), s, r); err != nil { if err := db.SaveState(context.Background(), s, r); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if !db.HasState(context.Background(), r) {
t.Fatal("wanted true")
}
savedS, err := db.State(context.Background(), r) savedS, err := db.State(context.Background(), r)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)