Skip to content

Commit

Permalink
state: move Validator methods into validators.go
Browse files Browse the repository at this point in the history
  • Loading branch information
altergui committed Nov 6, 2023
1 parent 4f1421d commit 15f5ac4
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 68 deletions.
68 changes: 0 additions & 68 deletions vochain/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package state

import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"os"
Expand All @@ -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 (
Expand Down Expand Up @@ -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 {
Expand Down
71 changes: 71 additions & 0 deletions vochain/state/validators.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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
}

0 comments on commit 15f5ac4

Please sign in to comment.