mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-15 22:48:19 +00:00
ade61717a4
* 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>
159 lines
4.8 KiB
Go
159 lines
4.8 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/bytesutil"
|
|
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
|
)
|
|
|
|
func createAttesterSlashing(enc []byte) (*ethpb.AttesterSlashing, error) {
|
|
protoSlashing := ðpb.AttesterSlashing{}
|
|
err := proto.Unmarshal(enc, protoSlashing)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to unmarshal encoding")
|
|
}
|
|
return protoSlashing, nil
|
|
}
|
|
|
|
func toAttesterSlashings(encoded [][]byte) ([]*ethpb.AttesterSlashing, error) {
|
|
attesterSlashings := make([]*ethpb.AttesterSlashing, len(encoded))
|
|
for i, enc := range encoded {
|
|
ps, err := createAttesterSlashing(enc)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
attesterSlashings[i] = ps
|
|
}
|
|
return attesterSlashings, nil
|
|
}
|
|
|
|
// AttesterSlashings accepts a status and returns all slashings with this status.
|
|
// returns empty []*ethpb.AttesterSlashing if no slashing has been found with this status.
|
|
func (db *Store) AttesterSlashings(status SlashingStatus) ([]*ethpb.AttesterSlashing, error) {
|
|
encoded := make([][]byte, 0)
|
|
err := db.view(func(tx *bolt.Tx) error {
|
|
c := tx.Bucket(slashingBucket).Cursor()
|
|
prefix := encodeType(SlashingType(Attestation))
|
|
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 toAttesterSlashings(encoded)
|
|
}
|
|
|
|
// DeleteAttesterSlashing deletes an attester slashing proof from db.
|
|
func (db *Store) DeleteAttesterSlashing(attesterSlashing *ethpb.AttesterSlashing) error {
|
|
root, err := hashutil.HashProto(attesterSlashing)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to get hash root of attesterSlashing")
|
|
}
|
|
return db.update(func(tx *bolt.Tx) error {
|
|
bucket := tx.Bucket(slashingBucket)
|
|
k := encodeTypeRoot(SlashingType(Attestation), 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
|
|
})
|
|
}
|
|
|
|
// HasAttesterSlashing returns true and slashing status if slashing is found in 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)
|
|
if err != nil {
|
|
return found, status, errors.Wrap(err, "failed to get hash root of attesterSlashing")
|
|
}
|
|
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
|
|
}
|
|
|
|
// SaveAttesterSlashing accepts a slashing proof and its status and writes it to disk.
|
|
func (db *Store) SaveAttesterSlashing(status SlashingStatus, slashing *ethpb.AttesterSlashing) error {
|
|
enc, err := proto.Marshal(slashing)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to marshal")
|
|
}
|
|
root := hashutil.Hash(enc)
|
|
key := encodeTypeRoot(SlashingType(Attestation), root)
|
|
return db.update(func(tx *bolt.Tx) error {
|
|
b := tx.Bucket(slashingBucket)
|
|
e := b.Put(key, append([]byte{byte(status)}, enc...))
|
|
return e
|
|
})
|
|
}
|
|
|
|
// SaveAttesterSlashings accepts a slice of slashing proof and its status and writes it to disk.
|
|
func (db *Store) SaveAttesterSlashings(status SlashingStatus, slashings []*ethpb.AttesterSlashing) 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(Attestation), 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
|
|
})
|
|
}
|
|
|
|
// GetLatestEpochDetected returns the latest detected epoch from db.
|
|
func (db *Store) GetLatestEpochDetected() (uint64, error) {
|
|
var epoch uint64
|
|
err := db.view(func(tx *bolt.Tx) error {
|
|
b := tx.Bucket(slashingBucket)
|
|
enc := b.Get([]byte(latestEpochKey))
|
|
if enc == nil {
|
|
epoch = 0
|
|
return nil
|
|
}
|
|
epoch = bytesutil.FromBytes8(enc)
|
|
return nil
|
|
})
|
|
return epoch, err
|
|
}
|
|
|
|
// SetLatestEpochDetected sets the latest slashing detected epoch in db.
|
|
func (db *Store) SetLatestEpochDetected(epoch uint64) error {
|
|
return db.update(func(tx *bolt.Tx) error {
|
|
b := tx.Bucket(slashingBucket)
|
|
err := b.Put([]byte(latestEpochKey), bytesutil.Bytes8(epoch))
|
|
return err
|
|
})
|
|
}
|