mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-06 01:32:18 +00:00
0b35743d2c
* Merge branch 'master' of github.com:prysmaticlabs/prysm into update_validators # Conflicts: # slasher/flags/flags.go # slasher/main.go # slasher/service/data_update.go # slasher/service/service.go # slasher/service/service_test.go * proposal and attester store * day to status * comment change * one bucket * Merge branch 'master' of github.com:prysmaticlabs/prysm into attester_proposer_slashing_store # Please enter a commit message to explain why this merge is necessary, # especially if it merges an updated upstream into a topic branch. # # Lines starting with '#' will be ignored, and an empty message aborts # the commit. added comments * comment * typo fix * raul review fix * raul review fix full * nishant feedback * test fix * fix tests and remove update gofmt goimports * remove blank line in imports * nishant fixes * comment and fir delete proposer slashings * avoid marshal twice * remove space * Update slasher/db/attester_slashings.go Co-Authored-By: terence tsao <terence@prysmaticlabs.com> * terence feedback Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> Co-authored-by: terence tsao <terence@prysmaticlabs.com>
170 lines
4.4 KiB
Go
170 lines
4.4 KiB
Go
package db
|
|
|
|
import (
|
|
"bytes"
|
|
|
|
"github.com/boltdb/bolt"
|
|
"github.com/gogo/protobuf/proto"
|
|
"github.com/pkg/errors"
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
|
)
|
|
|
|
// SlashingStatus enum like structure.
|
|
type SlashingStatus uint8
|
|
|
|
const (
|
|
// Unknown default status in case it is not set
|
|
Unknown = iota
|
|
// Active slashing proof hasn't been included yet.
|
|
Active
|
|
// Included slashing proof that has been included in a block.
|
|
Included
|
|
// Reverted slashing proof that has been reverted and therefore is relevant again.
|
|
Reverted //relevant again
|
|
)
|
|
|
|
func (status SlashingStatus) String() string {
|
|
names := [...]string{
|
|
"Unknown",
|
|
"Active",
|
|
"Included",
|
|
"Reverted"}
|
|
|
|
if status < Active || status > Reverted {
|
|
return "Unknown"
|
|
}
|
|
// return the name of a SlashingStatus
|
|
// constant from the names array
|
|
// above.
|
|
return names[status]
|
|
}
|
|
|
|
// SlashingType enum like type of slashing proof.
|
|
type SlashingType uint8
|
|
|
|
const (
|
|
// Proposal enum value.
|
|
Proposal = iota
|
|
// Attestation enum value.
|
|
Attestation
|
|
)
|
|
|
|
func (status SlashingType) String() string {
|
|
names := [...]string{
|
|
"Proposal",
|
|
"Attestation"}
|
|
|
|
if status < Active || status > Reverted {
|
|
return "Unknown"
|
|
}
|
|
// return the name of a SlashingType
|
|
// constant from the names array
|
|
// above.
|
|
return names[status]
|
|
}
|
|
|
|
func createProposerSlashing(enc []byte) (*ethpb.ProposerSlashing, error) {
|
|
protoSlashing := ðpb.ProposerSlashing{}
|
|
|
|
err := proto.Unmarshal(enc, protoSlashing)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to unmarshal encoding")
|
|
}
|
|
return protoSlashing, nil
|
|
}
|
|
|
|
func toProposerSlashings(encoded [][]byte) ([]*ethpb.ProposerSlashing, error) {
|
|
proposerSlashings := make([]*ethpb.ProposerSlashing, len(encoded))
|
|
for i, enc := range encoded {
|
|
ps, err := createProposerSlashing(enc)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
proposerSlashings[i] = ps
|
|
}
|
|
return proposerSlashings, nil
|
|
}
|
|
|
|
// ProposalSlashingsByStatus returns all the proposal slashing proofs with a certain status.
|
|
func (db *Store) ProposalSlashingsByStatus(status SlashingStatus) ([]*ethpb.ProposerSlashing, error) {
|
|
encoded := make([][]byte, 0)
|
|
err := db.view(func(tx *bolt.Tx) error {
|
|
c := tx.Bucket(slashingBucket).Cursor()
|
|
prefix := encodeType(SlashingType(Proposal))
|
|
for k, v := c.Seek(prefix); k != nil && bytes.HasPrefix(k, prefix); k, v = c.Next() {
|
|
if v[0] == byte(status) {
|
|
encoded = append(encoded, v[1:])
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return toProposerSlashings(encoded)
|
|
}
|
|
|
|
// DeleteProposerSlashing deletes a proposer slashing proof.
|
|
func (db *Store) DeleteProposerSlashing(slashing *ethpb.ProposerSlashing) error {
|
|
root, err := hashutil.HashProto(slashing)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to get hash root of proposerSlashing")
|
|
}
|
|
err = db.update(func(tx *bolt.Tx) error {
|
|
bucket := tx.Bucket(slashingBucket)
|
|
k := encodeTypeRoot(SlashingType(Proposal), root)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to get key for for attester slashing.")
|
|
}
|
|
if err := bucket.Delete(k); err != nil {
|
|
return errors.Wrap(err, "failed to delete the slashing proof from slashing bucket")
|
|
}
|
|
return nil
|
|
})
|
|
return err
|
|
}
|
|
|
|
// HasProposerSlashing returns the slashing key if it is found in db.
|
|
func (db *Store) HasProposerSlashing(slashing *ethpb.ProposerSlashing) (bool, SlashingStatus, error) {
|
|
root, err := hashutil.HashProto(slashing)
|
|
key := encodeTypeRoot(SlashingType(Proposal), root)
|
|
var status SlashingStatus
|
|
var found bool
|
|
if err != nil {
|
|
return found, status, errors.Wrap(err, "failed to get hash root of proposerSlashing")
|
|
}
|
|
err = db.view(func(tx *bolt.Tx) error {
|
|
b := tx.Bucket(slashingBucket)
|
|
enc := b.Get(key)
|
|
if enc != nil {
|
|
found = true
|
|
status = SlashingStatus(enc[0])
|
|
}
|
|
return nil
|
|
})
|
|
return found, status, err
|
|
}
|
|
|
|
// SaveProposerSlashing accepts a proposer slashing and its status header and writes it to disk.
|
|
func (db *Store) SaveProposerSlashing(status SlashingStatus, slashing *ethpb.ProposerSlashing) error {
|
|
enc, err := proto.Marshal(slashing)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to marshal")
|
|
}
|
|
root := hashutil.Hash(enc)
|
|
key := encodeTypeRoot(SlashingType(Proposal), root)
|
|
err = db.update(func(tx *bolt.Tx) error {
|
|
b := tx.Bucket(slashingBucket)
|
|
e := b.Put(key, append([]byte{byte(status)}, enc...))
|
|
if e != nil {
|
|
return nil
|
|
}
|
|
return err
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return err
|
|
}
|