From 3dd5d2899b503936e4c270b94e7437d7313a16b4 Mon Sep 17 00:00:00 2001 From: Gui Iribarren Date: Mon, 30 Oct 2023 12:29:57 +0100 Subject: [PATCH] state: move Validator methods into validators.go --- vochain/state/state.go | 68 ----------------------------------- vochain/state/validators.go | 71 +++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 68 deletions(-) diff --git a/vochain/state/state.go b/vochain/state/state.go index edfb51af5..152f25bfc 100644 --- a/vochain/state/state.go +++ b/vochain/state/state.go @@ -2,7 +2,6 @@ package state import ( "bytes" - "encoding/hex" "errors" "fmt" "os" @@ -17,11 +16,9 @@ import ( "go.vocdoni.io/dvote/db/metadb" "go.vocdoni.io/dvote/log" "go.vocdoni.io/dvote/statedb" - "go.vocdoni.io/dvote/tree/arbo" "go.vocdoni.io/dvote/vochain/state/electionprice" "go.vocdoni.io/proto/build/go/models" - "google.golang.org/protobuf/proto" ) const ( @@ -263,71 +260,6 @@ func (v *State) SetElectionPriceCalc() error { return nil } -// RemoveValidator removes a tendermint validator identified by its address -func (v *State) RemoveValidator(address []byte) error { - v.tx.Lock() - defer v.tx.Unlock() - validators, err := v.tx.SubTree(StateTreeCfg(TreeValidators)) - if err != nil { - return err - } - if _, err := validators.Get(address); errors.Is(err, arbo.ErrKeyNotFound) { - return fmt.Errorf("validator not found: %w", err) - } else if err != nil { - return err - } - return validators.Set(address, nil) -} - -// Validators returns a list of the chain validators -// When committed is false, the operation is executed also on not yet committed -// data from the currently open StateDB transaction. -// When committed is true, the operation is executed on the last committed version. -func (v *State) Validators(committed bool) (map[string]*models.Validator, error) { - if !committed { - v.tx.RLock() - defer v.tx.RUnlock() - } - - validatorsTree, err := v.mainTreeViewer(committed).SubTree(StateTreeCfg(TreeValidators)) - if err != nil { - return nil, err - } - - validators := make(map[string]*models.Validator) - var callbackErr error - if err := validatorsTree.Iterate(func(key, value []byte) bool { - // removed validators are still in the tree but with value set - // to nil - if len(value) == 0 { - return true - } - validator := &models.Validator{} - if err := proto.Unmarshal(value, validator); err != nil { - callbackErr = err - return false - } - validators[hex.EncodeToString(validator.GetAddress())] = validator - return true - }); err != nil { - return nil, err - } - if callbackErr != nil { - return nil, callbackErr - } - return validators, nil -} - -// Validator returns an existing validator identified by the given signing address. -// If the validator is not found, returns nil and no error. -func (v *State) Validator(address common.Address, committed bool) (*models.Validator, error) { - list, err := v.Validators(committed) - if err != nil { - return nil, err - } - return list[hex.EncodeToString(address.Bytes())], nil -} - // AddProcessKeys adds the keys to the process func (v *State) AddProcessKeys(tx *models.AdminTx) error { if tx.ProcessId == nil || tx.KeyIndex == nil { diff --git a/vochain/state/validators.go b/vochain/state/validators.go index f05a20a88..bd544a797 100644 --- a/vochain/state/validators.go +++ b/vochain/state/validators.go @@ -1,6 +1,12 @@ package state import ( + "encoding/hex" + "errors" + "fmt" + + "github.com/ethereum/go-ethereum/common" + "go.vocdoni.io/dvote/tree/arbo" "go.vocdoni.io/proto/build/go/models" "google.golang.org/protobuf/proto" ) @@ -15,3 +21,68 @@ func (v *State) AddValidator(validator *models.Validator) error { } return v.tx.DeepSet(validator.Address, validatorBytes, StateTreeCfg(TreeValidators)) } + +// RemoveValidator removes a tendermint validator identified by its address +func (v *State) RemoveValidator(address []byte) error { + v.tx.Lock() + defer v.tx.Unlock() + validators, err := v.tx.SubTree(StateTreeCfg(TreeValidators)) + if err != nil { + return err + } + if _, err := validators.Get(address); errors.Is(err, arbo.ErrKeyNotFound) { + return fmt.Errorf("validator not found: %w", err) + } else if err != nil { + return err + } + return validators.Set(address, nil) +} + +// Validators returns a list of the chain validators +// When committed is false, the operation is executed also on not yet committed +// data from the currently open StateDB transaction. +// When committed is true, the operation is executed on the last committed version. +func (v *State) Validators(committed bool) (map[string]*models.Validator, error) { + if !committed { + v.tx.RLock() + defer v.tx.RUnlock() + } + + validatorsTree, err := v.mainTreeViewer(committed).SubTree(StateTreeCfg(TreeValidators)) + if err != nil { + return nil, err + } + + validators := make(map[string]*models.Validator) + var callbackErr error + if err := validatorsTree.Iterate(func(key, value []byte) bool { + // removed validators are still in the tree but with value set + // to nil + if len(value) == 0 { + return true + } + validator := &models.Validator{} + if err := proto.Unmarshal(value, validator); err != nil { + callbackErr = err + return false + } + validators[hex.EncodeToString(validator.GetAddress())] = validator + return true + }); err != nil { + return nil, err + } + if callbackErr != nil { + return nil, callbackErr + } + return validators, nil +} + +// Validator returns an existing validator identified by the given signing address. +// If the validator is not found, returns nil and no error. +func (v *State) Validator(address common.Address, committed bool) (*models.Validator, error) { + list, err := v.Validators(committed) + if err != nil { + return nil, err + } + return list[hex.EncodeToString(address.Bytes())], nil +}