Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup slasher codebase #4698

Merged
merged 21 commits into from Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 10 additions & 9 deletions slasher/db/attester_slashings.go
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/prysmaticlabs/prysm/shared/hashutil"
)

func createAttesterSlashing(enc []byte) (*ethpb.AttesterSlashing, error) {
func unmarshalAttSlashing(enc []byte) (*ethpb.AttesterSlashing, error) {
protoSlashing := &ethpb.AttesterSlashing{}
err := proto.Unmarshal(enc, protoSlashing)
if err != nil {
Expand All @@ -20,10 +20,10 @@ func createAttesterSlashing(enc []byte) (*ethpb.AttesterSlashing, error) {
return protoSlashing, nil
}

func toAttesterSlashings(encoded [][]byte) ([]*ethpb.AttesterSlashing, error) {
func unmarshalAttSlashings(encoded [][]byte) ([]*ethpb.AttesterSlashing, error) {
attesterSlashings := make([]*ethpb.AttesterSlashing, len(encoded))
for i, enc := range encoded {
ps, err := createAttesterSlashing(enc)
ps, err := unmarshalAttSlashing(enc)
if err != nil {
return nil, err
}
Expand All @@ -49,7 +49,7 @@ func (db *Store) AttesterSlashings(status SlashingStatus) ([]*ethpb.AttesterSlas
if err != nil {
return nil, err
}
return toAttesterSlashings(encoded)
return unmarshalAttSlashings(encoded)
}

// DeleteAttesterSlashing deletes an attester slashing proof from db.
Expand All @@ -71,15 +71,15 @@ func (db *Store) DeleteAttesterSlashing(attesterSlashing *ethpb.AttesterSlashing
})
}

// HasAttesterSlashing returns true and slashing status if slashing is found in db.
// HasAttesterSlashing returns true and slashing status if a slashing is found in the db.
func (db *Store) HasAttesterSlashing(slashing *ethpb.AttesterSlashing) (bool, SlashingStatus, error) {
root, err := hashutil.HashProto(slashing)
var status SlashingStatus
var found bool
key := encodeTypeRoot(SlashingType(Attestation), root)
root, err := hashutil.HashProto(slashing)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isnt really a root, just a hash - maybe rename?

if err != nil {
return found, status, errors.Wrap(err, "failed to get hash root of attesterSlashing")
}
key := encodeTypeRoot(SlashingType(Attestation), root)
err = db.view(func(tx *bolt.Tx) error {
b := tx.Bucket(slashingBucket)
enc := b.Get(key)
Expand Down Expand Up @@ -117,9 +117,10 @@ func (db *Store) SaveAttesterSlashings(status SlashingStatus, slashings []*ethpb
if err != nil {
return errors.Wrap(err, "failed to marshal")
}
root := hashutil.Hash(enc[i])
key[i] = encodeTypeRoot(SlashingType(Attestation), root)
encHash := hashutil.Hash(enc[i])
key[i] = encodeTypeRoot(SlashingType(Attestation), encHash)
}

return db.update(func(tx *bolt.Tx) error {
b := tx.Bucket(slashingBucket)
for i := 0; i < len(enc); i++ {
Expand Down
19 changes: 9 additions & 10 deletions slasher/db/block_header.go
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/prysmaticlabs/prysm/shared/params"
)

func createBlockHeader(enc []byte) (*ethpb.SignedBeaconBlockHeader, error) {
func unmarshalBlockHeader(enc []byte) (*ethpb.SignedBeaconBlockHeader, error) {
protoBlockHeader := &ethpb.SignedBeaconBlockHeader{}
err := proto.Unmarshal(enc, protoBlockHeader)
if err != nil {
Expand All @@ -28,7 +28,7 @@ func (db *Store) BlockHeader(epoch uint64, validatorID uint64) ([]*ethpb.SignedB
c := tx.Bucket(historicBlockHeadersBucket).Cursor()
prefix := encodeEpochValidatorID(epoch, validatorID)
for k, v := c.Seek(prefix); k != nil && bytes.HasPrefix(k, prefix); k, v = c.Next() {
bh, err := createBlockHeader(v)
bh, err := unmarshalBlockHeader(v)
if err != nil {
return err
}
Expand Down Expand Up @@ -68,35 +68,34 @@ func (db *Store) SaveBlockHeader(epoch uint64, validatorID uint64, blockHeader *
err = db.update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(historicBlockHeadersBucket)
if err := bucket.Put(key, enc); err != nil {
return errors.Wrap(err, "failed to include the block header in the historic block header bucket")
return errors.Wrap(err, "failed to include block header in the historical bucket")
}

return err
})

// prune history to max size every 10th epoch
// Prune block header history every 10th epoch.
if epoch%params.BeaconConfig().PruneSlasherStoragePeriod == 0 {
err = db.PruneHistory(epoch, params.BeaconConfig().WeakSubjectivityPeriod)
err = db.pruneBlockHistory(epoch, params.BeaconConfig().WeakSubjectivityPeriod)
}
return err
}

// DeleteBlockHeader deletes a block header using the epoch and validator id.
func (db *Store) DeleteBlockHeader(epoch uint64, validatorID uint64, blockHeader *ethpb.SignedBeaconBlockHeader) error {

key := encodeEpochValidatorIDSig(epoch, validatorID, blockHeader.Signature)

return db.update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(historicBlockHeadersBucket)
if err := bucket.Delete(key); err != nil {
return errors.Wrap(err, "failed to delete the block header from historic block header bucket")
return errors.Wrap(err, "failed to delete the block header from historical bucket")
}
return bucket.Delete(key)
})
}

// PruneHistory leaves only records younger then history size.
func (db *Store) PruneHistory(currentEpoch uint64, historySize uint64) error {
// pruneBlockHistory leaves only records younger then history size.
func (db *Store) pruneBlockHistory(currentEpoch uint64, historySize uint64) error {
pruneTill := int64(currentEpoch) - int64(historySize)
if pruneTill <= 0 {
return nil
Expand All @@ -106,7 +105,7 @@ func (db *Store) PruneHistory(currentEpoch uint64, historySize uint64) error {
c := tx.Bucket(historicBlockHeadersBucket).Cursor()
for k, _ := c.First(); k != nil && bytesutil.FromBytes8(k[:8]) <= uint64(pruneTill); k, _ = c.Next() {
if err := bucket.Delete(k); err != nil {
return errors.Wrap(err, "failed to delete the block header from historic block header bucket")
return errors.Wrap(err, "failed to delete the block header from historical bucket")
}
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion slasher/db/block_header_test.go
Expand Up @@ -247,7 +247,7 @@ func TestPruneHistoryBlkHdr(t *testing.T) {
}
currentEpoch := uint64(3)
historyToKeep := uint64(2)
err := db.PruneHistory(currentEpoch, historyToKeep)
err := db.pruneBlockHistory(currentEpoch, historyToKeep)
if err != nil {
t.Fatalf("failed to prune: %v", err)
}
Expand Down