add more fields editing in updateEditValidator; add commission ratels in staking tx sending;

add current/future staking validator api call;
pull/1795/head
Chao Ma 5 years ago
parent 71e4914bbf
commit 74d774de05
  1. 16
      cmd/staking/root.go
  2. 28
      core/blockchain.go
  3. 9
      core/state_transition.go
  4. 5
      node/node_handler.go
  5. 13
      staking/types/commission.go
  6. 39
      staking/types/validator.go

@ -22,7 +22,6 @@ import (
"github.com/harmony-one/harmony/shard" "github.com/harmony-one/harmony/shard"
staking "github.com/harmony-one/harmony/staking/types" staking "github.com/harmony-one/harmony/staking/types"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
) )
const ( const (
@ -52,6 +51,7 @@ var (
name = "NewName" name = "NewName"
index = 0 index = 0
minDele = 777 minDele = 777
rate = "0.0"
testAccounts = []string{ testAccounts = []string{
"one1pdv9lrdwl0rg5vglh4xtyrv3wjk3wsqket7zxy", "one1pdv9lrdwl0rg5vglh4xtyrv3wjk3wsqket7zxy",
@ -74,6 +74,10 @@ func (s *staker) run(cmd *cobra.Command, args []string) error {
p.DeserializeHexStr(testBLSPubKeys[index]) p.DeserializeHexStr(testBLSPubKeys[index])
pub := shard.BlsPublicKey{} pub := shard.BlsPublicKey{}
pub.FromLibBLSPublicKey(p) pub.FromLibBLSPublicKey(p)
ra, _ := numeric.NewDecFromStr("27.27")
maxRate, _ := numeric.NewDecFromStr("150.99")
maxChangeRate, _ := numeric.NewDecFromStr("0.5")
if cmdType == "create" { if cmdType == "create" {
return staking.DirectiveCreateValidator, staking.CreateValidator{ return staking.DirectiveCreateValidator, staking.CreateValidator{
Description: &staking.Description{ Description: &staking.Description{
@ -84,9 +88,9 @@ func (s *staker) run(cmd *cobra.Command, args []string) error {
Details: "blah blah blah", Details: "blah blah blah",
}, },
CommissionRates: staking.CommissionRates{ CommissionRates: staking.CommissionRates{
Rate: numeric.NewDec(100), Rate: ra,
MaxRate: numeric.NewDec(150), MaxRate: maxRate,
MaxChangeRate: numeric.NewDec(5), MaxChangeRate: maxChangeRate,
}, },
MinSelfDelegation: big.NewInt(10), MinSelfDelegation: big.NewInt(10),
MaxTotalDelegation: big.NewInt(3000), MaxTotalDelegation: big.NewInt(3000),
@ -106,11 +110,13 @@ func (s *staker) run(cmd *cobra.Command, args []string) error {
} }
*/ */
newRate, _ := numeric.NewDecFromStr(rate)
return staking.DirectiveEditValidator, staking.EditValidator{ return staking.DirectiveEditValidator, staking.EditValidator{
Description: &staking.Description{ Description: &staking.Description{
Name: name, Name: name,
}, },
MinSelfDelegation: big.NewInt(int64(minDele)), MinSelfDelegation: big.NewInt(int64(minDele)),
CommissionRate: &newRate,
ValidatorAddress: common.Address(dAddr), ValidatorAddress: common.Address(dAddr),
} }
} }
@ -189,8 +195,8 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&cmdType, "type", "t", "create", "type of commands: create|edit") rootCmd.PersistentFlags().StringVarP(&cmdType, "type", "t", "create", "type of commands: create|edit")
rootCmd.PersistentFlags().StringVarP(&name, "name", "m", "ANewName", "Name of Validator") rootCmd.PersistentFlags().StringVarP(&name, "name", "m", "ANewName", "Name of Validator")
rootCmd.PersistentFlags().IntVarP(&minDele, "minDele", "d", 666, "MinSelfDelegation Fee") rootCmd.PersistentFlags().IntVarP(&minDele, "minDele", "d", 666, "MinSelfDelegation Fee")
rootCmd.PersistentFlags().StringVarP(&rate, "rate", "r", "22.22", "Commision Rate")
viper.BindPFlag("nonce", rootCmd.PersistentFlags().Lookup("nonce"))
rootCmd.AddCommand(&cobra.Command{ rootCmd.AddCommand(&cobra.Command{
Use: "staking-iterate", Use: "staking-iterate",
Short: "run through staking process", Short: "run through staking process",

@ -2370,12 +2370,36 @@ func (bc *BlockChain) UpdateValidatorList(tx *staking.StakingTransaction) error
// CurrentValidatorAddresses returns the address of active validators for current epoch // CurrentValidatorAddresses returns the address of active validators for current epoch
func (bc *BlockChain) CurrentValidatorAddresses() []common.Address { func (bc *BlockChain) CurrentValidatorAddresses() []common.Address {
return make([]common.Address, 0) list, err := bc.ReadValidatorList()
if err != nil {
return make([]common.Address, 0)
}
currentEpoch := bc.CurrentBlock().Epoch()
filtered := []common.Address{}
for _, addr := range list {
val, err := bc.ValidatorInformation(addr)
if err != nil {
continue
}
epoch := ShardingSchedule.CalcEpochNumber(val.CreationHeight.Uint64())
if epoch.Cmp(currentEpoch) >= 0 {
// wait for next epoch
continue
}
filtered = append(filtered, addr)
}
return filtered
} }
// ValidatorCandidates returns the up to date validator candidates for next epoch // ValidatorCandidates returns the up to date validator candidates for next epoch
func (bc *BlockChain) ValidatorCandidates() []common.Address { func (bc *BlockChain) ValidatorCandidates() []common.Address {
return make([]common.Address, 0) list, err := bc.ReadValidatorList()
if err != nil {
return make([]common.Address, 0)
}
return list
} }
// ValidatorInformation returns the information of validator // ValidatorInformation returns the information of validator

@ -335,6 +335,7 @@ func (st *StateTransition) applyCreateValidatorTx(nv *staking.CreateValidator, b
return err return err
} }
v.UpdateHeight = blockNum v.UpdateHeight = blockNum
v.CreationHeight = blockNum
wrapper := staking.ValidatorWrapper{*v, nil, nil, nil} wrapper := staking.ValidatorWrapper{*v, nil, nil, nil}
if err := st.state.UpdateStakingInfo(v.Address, &wrapper); err != nil { if err := st.state.UpdateStakingInfo(v.Address, &wrapper); err != nil {
return err return err
@ -348,10 +349,16 @@ func (st *StateTransition) applyEditValidatorTx(ev *staking.EditValidator, block
return errValidatorNotExist return errValidatorNotExist
} }
wrapper := st.state.GetStakingInfo(ev.ValidatorAddress) wrapper := st.state.GetStakingInfo(ev.ValidatorAddress)
oldRate := wrapper.Validator.Rate
if err := staking.UpdateValidatorFromEditMsg(&wrapper.Validator, ev); err != nil { if err := staking.UpdateValidatorFromEditMsg(&wrapper.Validator, ev); err != nil {
return err return err
} }
wrapper.Validator.UpdateHeight = blockNum newRate := wrapper.Validator.Rate
// update the commision rate change height
if oldRate.IsNil() || (!newRate.IsNil() && !oldRate.Equal(newRate)) {
wrapper.Validator.UpdateHeight = blockNum
}
if err := st.state.UpdateStakingInfo(ev.ValidatorAddress, wrapper); err != nil { if err := st.state.UpdateStakingInfo(ev.ValidatorAddress, wrapper); err != nil {
return err return err
} }

@ -421,6 +421,11 @@ func (node *Node) AddNewBlock(newBlock *types.Block) error {
} }
utils.Logger().Debug().Msgf("ValidatorInformation %v: %v", i, val) utils.Logger().Debug().Msgf("ValidatorInformation %v: %v", i, val)
} }
currAddrs := node.Blockchain().CurrentValidatorAddresses()
utils.Logger().Debug().Msgf("CurrentValidators : %v", currAddrs)
candidates := node.Blockchain().ValidatorCandidates()
utils.Logger().Debug().Msgf("CandidateValidators : %v", candidates)
// Finish debug
if err != nil { if err != nil {
utils.Logger().Error(). utils.Logger().Error().

@ -1,6 +1,7 @@
package types package types
import ( import (
"fmt"
"math/big" "math/big"
"github.com/harmony-one/harmony/numeric" "github.com/harmony-one/harmony/numeric"
@ -22,3 +23,15 @@ type (
MaxChangeRate numeric.Dec `json:"max_change_rate" yaml:"max_change_rate"` // maximum increase of the validator commission every epoch, as a fraction MaxChangeRate numeric.Dec `json:"max_change_rate" yaml:"max_change_rate"` // maximum increase of the validator commission every epoch, as a fraction
} }
) )
// String returns a human readable string representation of a validator.
func (c Commission) String() string {
return fmt.Sprintf(`
Commission:
Rate: %s
MaxRate: %s
MaxChangeRate: %s
UpdateHeight: %v`,
c.Rate, c.MaxRate, c.MaxChangeRate,
c.UpdateHeight)
}

@ -46,12 +46,16 @@ type Validator struct {
UnbondingHeight *big.Int `json:"unbonding_height" yaml:"unbonding_height"` UnbondingHeight *big.Int `json:"unbonding_height" yaml:"unbonding_height"`
// validator's self declared minimum self delegation // validator's self declared minimum self delegation
MinSelfDelegation *big.Int `json:"min_self_delegation" yaml:"min_self_delegation"` MinSelfDelegation *big.Int `json:"min_self_delegation" yaml:"min_self_delegation"`
// maximum total delgation allowed
MaxTotalDelegation *big.Int `json:"min_self_delegation" yaml:"min_self_delegation"`
// Is the validator active in the validating process or not // Is the validator active in the validating process or not
Active bool `json:"active" yaml:"active"` Active bool `json:"active" yaml:"active"`
// commission parameters // commission parameters
Commission `json:"commission" yaml:"commission"` Commission `json:"commission" yaml:"commission"`
// description for the validator // description for the validator
Description `json:"description" yaml:"description"` Description `json:"description" yaml:"description"`
// CreationHeight is the height of creation
CreationHeight *big.Int `json:"creation_height" yaml:"creation_height"`
} }
func printSlotPubKeys(pubKeys []shard.BlsPublicKey) string { func printSlotPubKeys(pubKeys []shard.BlsPublicKey) string {
@ -153,12 +157,13 @@ func CreateValidatorFromNewMsg(val *CreateValidator) (*Validator, error) {
pubKeys := []shard.BlsPublicKey{} pubKeys := []shard.BlsPublicKey{}
pubKeys = append(pubKeys, val.SlotPubKeys...) pubKeys = append(pubKeys, val.SlotPubKeys...)
v := Validator{val.ValidatorAddress, pubKeys, v := Validator{val.ValidatorAddress, pubKeys,
val.Amount, new(big.Int), val.MinSelfDelegation, false, val.Amount, new(big.Int), val.MinSelfDelegation, val.MaxTotalDelegation, false,
commission, desc} commission, desc, big.NewInt(0)}
return &v, nil return &v, nil
} }
// UpdateValidatorFromEditMsg updates validator from EditValidator message // UpdateValidatorFromEditMsg updates validator from EditValidator message
// TODO check the validity of the fields of edit message
func UpdateValidatorFromEditMsg(validator *Validator, edit *EditValidator) error { func UpdateValidatorFromEditMsg(validator *Validator, edit *EditValidator) error {
if validator.Address != edit.ValidatorAddress { if validator.Address != edit.ValidatorAddress {
return errAddressNotMatch return errAddressNotMatch
@ -172,11 +177,41 @@ func UpdateValidatorFromEditMsg(validator *Validator, edit *EditValidator) error
if edit.CommissionRate != nil { if edit.CommissionRate != nil {
validator.Rate = *edit.CommissionRate validator.Rate = *edit.CommissionRate
if err != nil {
return err
}
//TODO update other rates
} }
if edit.MinSelfDelegation != nil { if edit.MinSelfDelegation != nil {
validator.MinSelfDelegation = edit.MinSelfDelegation validator.MinSelfDelegation = edit.MinSelfDelegation
} }
if edit.MaxTotalDelegation != nil {
validator.MaxTotalDelegation = edit.MaxTotalDelegation
}
if edit.SlotKeyToAdd != nil {
for _, key := range validator.SlotPubKeys {
if key == *edit.SlotKeyToAdd {
break
}
validator.SlotPubKeys = append(validator.SlotPubKeys, *edit.SlotKeyToAdd)
}
}
if edit.SlotKeyToRemove != nil {
index := -1
for i, key := range validator.SlotPubKeys {
if key == *edit.SlotKeyToRemove {
index = i
}
}
// we found key to be removed
if index >= 0 {
validator.SlotPubKeys = append(validator.SlotPubKeys[:index], validator.SlotPubKeys[index+1:]...)
}
}
return nil return nil
} }

Loading…
Cancel
Save