prysm-pulse/slasher/db/proposer_slashings.go
shayzluf ade61717a4
Slasher data update from archive (#4563)
* first version

* cli context

* fix service

* starting change to ccache

* ristretto cache

* added test

* test on evict

* remove evict test

* test onevict

* comment for exported flag

* update all span maps on load

* fix setup db

* span cache added to help flags

* start save cache on exit

* save cache to db before close

* comment fix

* fix flags

* setup db new

* data update from archive node

* gaz

* slashing detection on old attestations

* un-export

* rename

* nishant feedback

* workspace cr

* lint fix

* fix calls

* start db

* fix test

* Update slasher/db/db.go

Co-Authored-By: Nishant Das <nishdas93@gmail.com>

* add flag

* fix fail to start beacon client

* mock beacon service

* fix imports

* gaz

* goimports

* add clear db flag

* print finalized epoch

* better msg

* Update slasher/db/attester_slashings.go

Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com>

* raul feedback

* raul feedback

* raul feedback

* raul feedback

* raul feedback

* add detection in runtime

* fix tests

* raul feedbacks

* raul feedback

* raul feedback

* goimports

* Update beacon-chain/blockchain/process_attestation_helpers.go

* Update beacon-chain/blockchain/receive_block.go

* Update beacon-chain/core/blocks/block_operations_test.go

* Update beacon-chain/core/blocks/block_operations.go

* Update beacon-chain/core/epoch/epoch_processing.go

* Update beacon-chain/sync/validate_aggregate_proof_test.go

* Update shared/testutil/block.go

* Update slasher/service/data_update.go

* Update tools/blocktree/main.go

* Update slasher/service/service.go

* Update beacon-chain/core/epoch/precompute/attestation_test.go

* Update beacon-chain/core/helpers/committee_test.go

* Update beacon-chain/core/state/transition_test.go

* Update beacon-chain/rpc/aggregator/server_test.go

* Update beacon-chain/sync/validate_aggregate_proof.go

* Update beacon-chain/rpc/validator/proposer_test.go

* Update beacon-chain/blockchain/forkchoice/process_attestation.go

Co-Authored-By: terence tsao <terence@prysmaticlabs.com>

* Update slasher/db/indexed_attestations.go

Co-Authored-By: terence tsao <terence@prysmaticlabs.com>

* Update slasher/service/data_update.go

Co-Authored-By: terence tsao <terence@prysmaticlabs.com>

* terence feedback

* terence feedback

* goimports

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
Co-authored-by: Nishant Das <nish1993@hotmail.com>
Co-authored-by: terence tsao <terence@prysmaticlabs.com>
2020-01-29 07:14:51 +05:30

188 lines
5.1 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 := &ethpb.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)
return db.update(func(tx *bolt.Tx) error {
b := tx.Bucket(slashingBucket)
e := b.Put(key, append([]byte{byte(status)}, enc...))
return e
})
}
// SaveProposeerSlashings accepts a slice of slashing proof and its status and writes it to disk.
func (db *Store) SaveProposeerSlashings(status SlashingStatus, slashings []*ethpb.ProposerSlashing) error {
enc := make([][]byte, len(slashings))
key := make([][]byte, len(slashings))
var err error
for i, slashing := range slashings {
enc[i], err = proto.Marshal(slashing)
if err != nil {
return errors.Wrap(err, "failed to marshal")
}
root := hashutil.Hash(enc[i])
key[i] = encodeTypeRoot(SlashingType(Proposal), root)
}
return db.update(func(tx *bolt.Tx) error {
b := tx.Bucket(slashingBucket)
for i := 0; i < len(enc); i++ {
e := b.Put(key[i], append([]byte{byte(status)}, enc[i]...))
if e != nil {
return e
}
}
return nil
})
}