mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-07 02:02:18 +00:00
d0c740f477
* WIP * WIP * adding in migration function * updating mock validator and gaz * adding descriptive logs * fixing mocking * fixing tests * fixing mock * adding changes to handle enable builder settings * fixing tests and edge case * reduce cognative complexity of function * further reducing cognative complexity on function * WIP * fixing unit test on migration * adding more tests * gaz and fix unit test * fixing deepsource issues * fixing more deesource issues missed previously * removing unused reciever name * WIP fix to migration logic * fixing loging info * reverting migration logic, converting logic to address issues discussed on slack, adding unit tests * adding test for builder setting only not saved to db * addressing comment * fixing flag * removing accidently missed deprecated flags * rolling back mock on pr * fixing fmt linting * updating comments based on feedback * Update config/features/flags.go Co-authored-by: Sammy Rosso <15244892+saolyn@users.noreply.github.com> * fixing based on feedback on PR * Update config/validator/service/proposer_settings.go Co-authored-by: Preston Van Loon <pvanloon@offchainlabs.com> * Update validator/client/runner.go Co-authored-by: Preston Van Loon <pvanloon@offchainlabs.com> * Update validator/db/kv/proposer_settings.go Co-authored-by: Preston Van Loon <pvanloon@offchainlabs.com> * adding additional logs to clear up some steps based on feedback * fixing log * deepsource * adding comments based on review feedback --------- Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> Co-authored-by: Sammy Rosso <15244892+saolyn@users.noreply.github.com> Co-authored-by: Preston Van Loon <pvanloon@offchainlabs.com>
138 lines
4.8 KiB
Go
138 lines
4.8 KiB
Go
package kv
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/pkg/errors"
|
|
fieldparams "github.com/prysmaticlabs/prysm/v4/config/fieldparams"
|
|
validatorServiceConfig "github.com/prysmaticlabs/prysm/v4/config/validator/service"
|
|
validatorpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1/validator-client"
|
|
bolt "go.etcd.io/bbolt"
|
|
"go.opencensus.io/trace"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
// NoProposerSettingsFound is an error thrown when no settings are found in bucket
|
|
var NoProposerSettingsFound = errors.New("no proposer settings found in bucket")
|
|
|
|
// UpdateProposerSettingsForPubkey updates the existing settings for an internal representation of the proposers settings file at a particular public key
|
|
func (s *Store) UpdateProposerSettingsForPubkey(ctx context.Context, pubkey [fieldparams.BLSPubkeyLength]byte, options *validatorServiceConfig.ProposerOption) error {
|
|
_, span := trace.StartSpan(ctx, "validator.db.UpdateProposerSettingsForPubkey")
|
|
defer span.End()
|
|
err := s.db.Update(func(tx *bolt.Tx) error {
|
|
bkt := tx.Bucket(proposerSettingsBucket)
|
|
b := bkt.Get(proposerSettingsKey)
|
|
if len(b) == 0 {
|
|
return fmt.Errorf("no proposer settings found in bucket")
|
|
}
|
|
to := &validatorpb.ProposerSettingsPayload{}
|
|
if err := proto.Unmarshal(b, to); err != nil {
|
|
return errors.Wrap(err, "failed to unmarshal proposer settings")
|
|
}
|
|
settings, err := validatorServiceConfig.ToSettings(to)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to convert payload to proposer settings")
|
|
}
|
|
if settings.ProposeConfig == nil {
|
|
settings.ProposeConfig = make(map[[fieldparams.BLSPubkeyLength]byte]*validatorServiceConfig.ProposerOption)
|
|
}
|
|
settings.ProposeConfig[pubkey] = options
|
|
m, err := proto.Marshal(settings.ToPayload())
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to marshal proposer settings")
|
|
}
|
|
return bkt.Put(proposerSettingsKey, m)
|
|
})
|
|
return err
|
|
}
|
|
|
|
// UpdateProposerSettingsDefault updates the existing default settings for proposer settings
|
|
func (s *Store) UpdateProposerSettingsDefault(ctx context.Context, options *validatorServiceConfig.ProposerOption) error {
|
|
_, span := trace.StartSpan(ctx, "validator.db.UpdateProposerSettingsDefault")
|
|
defer span.End()
|
|
if options == nil {
|
|
return errors.New("proposer settings option was empty")
|
|
}
|
|
if options.FeeRecipientConfig == nil {
|
|
return errors.New("fee recipient cannot be empty")
|
|
}
|
|
err := s.db.Update(func(tx *bolt.Tx) error {
|
|
bkt := tx.Bucket(proposerSettingsBucket)
|
|
b := bkt.Get(proposerSettingsKey)
|
|
if len(b) == 0 {
|
|
return NoProposerSettingsFound
|
|
}
|
|
to := &validatorpb.ProposerSettingsPayload{}
|
|
if err := proto.Unmarshal(b, to); err != nil {
|
|
return errors.Wrap(err, "failed to unmarshal proposer settings")
|
|
}
|
|
settings, err := validatorServiceConfig.ToSettings(to)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to convert payload to proposer settings")
|
|
}
|
|
settings.DefaultConfig = options
|
|
m, err := proto.Marshal(settings.ToPayload())
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to marshal proposer settings")
|
|
}
|
|
return bkt.Put(proposerSettingsKey, m)
|
|
})
|
|
return err
|
|
}
|
|
|
|
// ProposerSettings gets the current proposer settings
|
|
func (s *Store) ProposerSettings(ctx context.Context) (*validatorServiceConfig.ProposerSettings, error) {
|
|
_, span := trace.StartSpan(ctx, "validator.db.ProposerSettings")
|
|
defer span.End()
|
|
to := &validatorpb.ProposerSettingsPayload{}
|
|
if err := s.db.View(func(tx *bolt.Tx) error {
|
|
bkt := tx.Bucket(proposerSettingsBucket)
|
|
b := bkt.Get(proposerSettingsKey)
|
|
if len(b) == 0 {
|
|
return NoProposerSettingsFound
|
|
}
|
|
if err := proto.Unmarshal(b, to); err != nil {
|
|
return errors.Wrap(err, "failed to unmarshal proposer settings")
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return nil, err
|
|
}
|
|
return validatorServiceConfig.ToSettings(to)
|
|
}
|
|
|
|
// ProposerSettingsExists returns true or false if the settings exist or not
|
|
func (s *Store) ProposerSettingsExists(ctx context.Context) (bool, error) {
|
|
ps, err := s.ProposerSettings(ctx)
|
|
if err != nil {
|
|
if errors.Is(err, NoProposerSettingsFound) {
|
|
return false, nil
|
|
}
|
|
return false, err
|
|
}
|
|
if ps == nil {
|
|
return false, nil
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// SaveProposerSettings saves the entire proposer setting overriding the existing settings
|
|
func (s *Store) SaveProposerSettings(ctx context.Context, settings *validatorServiceConfig.ProposerSettings) error {
|
|
_, span := trace.StartSpan(ctx, "validator.db.SaveProposerSettings")
|
|
defer span.End()
|
|
// nothing to save
|
|
if !settings.ShouldBeSaved() {
|
|
log.Warn("proposer settings are empty, nothing has been saved")
|
|
return nil
|
|
}
|
|
return s.db.Update(func(tx *bolt.Tx) error {
|
|
bkt := tx.Bucket(proposerSettingsBucket)
|
|
m, err := proto.Marshal(settings.ToPayload())
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to marshal proposer settings")
|
|
}
|
|
return bkt.Put(proposerSettingsKey, m)
|
|
})
|
|
}
|