add validator_test.go

pull/1721/head
chao 5 years ago
parent db44ad08e8
commit 6950c450f2
  1. 20
      core/state_transition.go
  2. 29
      staking/types/validator.go
  3. 74
      staking/types/validator_test.go

@ -291,33 +291,33 @@ func (st *StateTransition) StakingTransitionDb() (usedGas uint64, err error) {
if err = rlp.DecodeBytes(msg.Data(), stkMsg); err != nil {
break
}
err = applyNewValidatorTx(stkMsg)
err = st.applyNewValidatorTx(stkMsg)
case types.StakeEditVal:
stkMsg := &staking.EditValidator{}
if err = rlp.DecodeBytes(msg.Data(), stkMsg); err != nil {
break
}
err = applyEditValidatorTx(stkMsg)
err = st.applyEditValidatorTx(stkMsg)
case types.Delegate:
stkMsg := &staking.Delegate{}
if err = rlp.DecodeBytes(msg.Data(), stkMsg); err != nil {
break
}
err = applyDelegateTx(stkMsg)
err = st.applyDelegateTx(stkMsg)
case types.Redelegate:
stkMsg := &staking.Redelegate{}
if err = rlp.DecodeBytes(msg.Data(), stkMsg); err != nil {
break
}
err = applyRedelegateTx(stkMsg)
err = st.applyRedelegateTx(stkMsg)
case types.Undelegate:
stkMsg := &staking.Undelegate{}
if err = rlp.DecodeBytes(msg.Data(), stkMsg); err != nil {
break
}
err = applyUndelegateTx(stkMsg)
err = st.applyUndelegateTx(stkMsg)
default:
return 0, values.ErrInvalidStakingType
}
@ -325,7 +325,7 @@ func (st *StateTransition) StakingTransitionDb() (usedGas uint64, err error) {
return st.gasUsed(), err
}
func applyNewValidatorTx(newValidator *staking.NewValidator) error {
func (st *StateTransition) applyNewValidatorTx(newValidator *staking.NewValidator) error {
amt := new(big.Int)
minDele := new(big.Int)
amt.Set(newValidator.Amount)
@ -337,18 +337,18 @@ func applyNewValidatorTx(newValidator *staking.NewValidator) error {
return nil
}
func applyEditValidatorTx(editValidator *staking.EditValidator) error {
func (st *StateTransition) applyEditValidatorTx(editValidator *staking.EditValidator) error {
return nil
}
func applyDelegateTx(delegate *staking.Delegate) error {
func (st *StateTransition) applyDelegateTx(delegate *staking.Delegate) error {
return nil
}
func applyRedelegateTx(redelegate *staking.Redelegate) error {
func (st *StateTransition) applyRedelegateTx(redelegate *staking.Redelegate) error {
return nil
}
func applyUndelegateTx(undelegate *staking.Undelegate) error {
func (st *StateTransition) applyUndelegateTx(undelegate *staking.Undelegate) error {
return nil
}

@ -3,7 +3,6 @@ package types
import (
"math/big"
common "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
"github.com/harmony-one/bls/ffi/go/bls"
@ -12,27 +11,6 @@ import (
"github.com/harmony-one/harmony/internal/ctxerror"
)
// Constants of the key into Storage field of Object in database
var (
AddressKey = common.HexToHash("Staking-Address")
BlsPubKey = common.HexToHash("Staking-BlsPubKey")
StakeKey = common.HexToHash("Staking-Amount")
UnboundingHeightKey = common.HexToHash("Staking-UnboundingHeight")
MinSelfDeleKey = common.HexToHash("Staking-MinSelfDelegation")
ActiveKey = common.HexToHash("Staking-IsActive")
//Commission related
UpdateHeightKey = common.HexToHash("Staking-UpdateHeight")
CommissionRateKey = common.HexToHash("Staking-CommissionRate")
CommissionMaxRateKey = common.HexToHash("Staking-CommissionMaxRate")
CommissionChangeRateKey = common.HexToHash("Staking-CommissionChangeRate")
//Description related
DescriptionNameKey = common.HexToHash("Staking-DesriptionName")
DescriptionIdentityKey = common.HexToHash("Staking-DesriptionIdentity")
DescriptionWebsiteKey = common.HexToHash("Staking-DesriptionWebsite")
DescriptionContactKey = common.HexToHash("Staking-DesriptionContact")
DescriptionDetailsKey = common.HexToHash("Staking-DesriptionDetails")
)
// Define validator staking related const
const (
MaxNameLength = 70
@ -54,12 +32,12 @@ type Validator struct {
UnbondingHeight *big.Int `json:"unbonding_height" yaml:"unbonding_height"`
// validator's self declared minimum self delegation
MinSelfDelegation *big.Int `json:"min_self_delegation" yaml:"min_self_delegation"`
// Is the validator active in the validating process or not
IsActive bool `json:"active" yaml:"active"`
// commission parameters
Commission `json:"commission" yaml:"commission"`
// description for the validator
Description `json:"description" yaml:"description"`
// Is the validator active in the validating process or not
IsCurrentlyActive bool `json:"active" yaml:"active"`
}
// Description - some possible IRL connections
@ -130,9 +108,6 @@ func (d Description) EnsureLength() (Description, error) {
// GetAddress returns address
func (v Validator) GetAddress() common2.Address { return v.Address }
// IsActive checks whether validator is active
func (v Validator) IsActive() bool { return v.IsCurrentlyActive }
// GetName returns the name of validator in the description
func (v Validator) GetName() string { return v.Description.Name }

@ -0,0 +1,74 @@
package types
import (
"reflect"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/assert"
)
// Constants of the key into Storage field of Object in database
var (
AddressKey = common.BytesToHash(crypto.Keccak256([]byte("Address")))
BlsPubKey = common.BytesToHash(crypto.Keccak256([]byte("ValidatingPubKey")))
StakeKey = common.BytesToHash(crypto.Keccak256([]byte("Stake")))
UnboundingHeightKey = common.BytesToHash(crypto.Keccak256([]byte("UnbondingHeight")))
MinSelfDeleKey = common.BytesToHash(crypto.Keccak256([]byte("MinSelfDelegation")))
ActiveKey = common.BytesToHash(crypto.Keccak256([]byte("IsActive")))
UpdateHeightKey = common.BytesToHash(crypto.Keccak256([]byte("UpdateHeight")))
DescriptionNameKey = common.BytesToHash(crypto.Keccak256([]byte("Name")))
DescriptionIdentityKey = common.BytesToHash(crypto.Keccak256([]byte("Identity")))
DescriptionWebsiteKey = common.BytesToHash(crypto.Keccak256([]byte("Website")))
DescriptionContactKey = common.BytesToHash(crypto.Keccak256([]byte("SecurityContact")))
DescriptionDetailsKey = common.BytesToHash(crypto.Keccak256([]byte("Details")))
CommissionRateKey = common.BytesToHash(crypto.Keccak256([]byte("Rate")))
CommissionMaxRateKey = common.BytesToHash(crypto.Keccak256([]byte("MaxRate")))
CommissionChangeRateKey = common.BytesToHash(crypto.Keccak256([]byte("MaxChangeRate")))
)
func TestKeyMatch(t *testing.T) {
names := []common.Hash{
AddressKey,
BlsPubKey,
StakeKey,
UnboundingHeightKey,
MinSelfDeleKey,
ActiveKey,
UpdateHeightKey,
DescriptionNameKey,
DescriptionIdentityKey,
DescriptionWebsiteKey,
DescriptionContactKey,
DescriptionDetailsKey,
CommissionRateKey,
CommissionMaxRateKey,
CommissionChangeRateKey,
}
v := reflect.TypeOf((*Validator)(nil)).Elem()
res := []common.Hash{}
queue := []reflect.StructField{}
for i := 0; i < v.NumField(); i++ {
queue = append(queue, v.Field(i))
}
for len(queue) > 0 {
item := queue[0]
queue = queue[1:]
if item.Anonymous {
for j := 0; j < item.Type.NumField(); j++ {
queue = append(queue, item.Type.Field(j))
}
} else {
hash := common.BytesToHash(crypto.Keccak256([]byte(item.Name)))
res = append(res, hash)
}
}
assert.Equal(t, len(res), len(names))
for i := 0; i < len(res); i++ {
assert.Equal(t, res[i], names[i])
}
}
Loading…
Cancel
Save