2019-08-21 21:32:44 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2019-11-27 05:08:18 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
2020-03-24 20:00:54 +00:00
|
|
|
bolt "go.etcd.io/bbolt"
|
2019-08-21 21:32:44 +00:00
|
|
|
"go.opencensus.io/trace"
|
|
|
|
)
|
|
|
|
|
|
|
|
// VoluntaryExit retrieval by signing root.
|
2020-06-23 10:15:01 +00:00
|
|
|
func (kv *Store) VoluntaryExit(ctx context.Context, exitRoot [32]byte) (*ethpb.VoluntaryExit, error) {
|
2019-08-21 21:32:44 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.VoluntaryExit")
|
|
|
|
defer span.End()
|
2020-07-07 16:29:25 +00:00
|
|
|
enc, err := kv.voluntaryExitBytes(ctx, exitRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(enc) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
exit := ðpb.VoluntaryExit{}
|
|
|
|
if err := decode(ctx, enc, exit); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return exit, nil
|
2019-08-21 21:32:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HasVoluntaryExit verifies if a voluntary exit is stored in the db by its signing root.
|
2020-06-23 10:15:01 +00:00
|
|
|
func (kv *Store) HasVoluntaryExit(ctx context.Context, exitRoot [32]byte) bool {
|
2019-08-21 21:32:44 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.HasVoluntaryExit")
|
|
|
|
defer span.End()
|
2020-07-07 16:29:25 +00:00
|
|
|
enc, err := kv.voluntaryExitBytes(ctx, exitRoot)
|
|
|
|
if err != nil {
|
2020-04-13 04:11:09 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2020-07-07 16:29:25 +00:00
|
|
|
return len(enc) > 0
|
2019-08-21 21:32:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SaveVoluntaryExit to the db by its signing root.
|
2020-06-23 10:15:01 +00:00
|
|
|
func (kv *Store) SaveVoluntaryExit(ctx context.Context, exit *ethpb.VoluntaryExit) error {
|
2019-08-21 21:32:44 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveVoluntaryExit")
|
|
|
|
defer span.End()
|
2020-08-27 18:13:32 +00:00
|
|
|
exitRoot, err := exit.HashTreeRoot()
|
2019-08-21 21:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-01 04:00:49 +00:00
|
|
|
enc, err := encode(ctx, exit)
|
2019-08-21 21:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-23 10:15:01 +00:00
|
|
|
return kv.db.Update(func(tx *bolt.Tx) error {
|
2019-08-21 21:32:44 +00:00
|
|
|
bucket := tx.Bucket(voluntaryExitsBucket)
|
|
|
|
return bucket.Put(exitRoot[:], enc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-07 16:29:25 +00:00
|
|
|
func (kv *Store) voluntaryExitBytes(ctx context.Context, exitRoot [32]byte) ([]byte, error) {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.voluntaryExitBytes")
|
|
|
|
defer span.End()
|
|
|
|
var dst []byte
|
|
|
|
err := kv.db.View(func(tx *bolt.Tx) error {
|
|
|
|
bkt := tx.Bucket(voluntaryExitsBucket)
|
|
|
|
dst = bkt.Get(exitRoot[:])
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return dst, err
|
|
|
|
}
|
|
|
|
|
2020-06-26 04:29:13 +00:00
|
|
|
// deleteVoluntaryExit clears a voluntary exit from the db by its signing root.
|
|
|
|
func (kv *Store) deleteVoluntaryExit(ctx context.Context, exitRoot [32]byte) error {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.deleteVoluntaryExit")
|
2019-08-21 21:32:44 +00:00
|
|
|
defer span.End()
|
2020-06-23 10:15:01 +00:00
|
|
|
return kv.db.Update(func(tx *bolt.Tx) error {
|
2019-08-21 21:32:44 +00:00
|
|
|
bucket := tx.Bucket(voluntaryExitsBucket)
|
|
|
|
return bucket.Delete(exitRoot[:])
|
|
|
|
})
|
|
|
|
}
|