fix rlp decoding issue for validatorWrapper

pull/1795/head
chao 5 years ago
parent 0f71a3e984
commit aed4b17e8a
  1. 38
      core/blockchain.go
  2. 2
      core/state/statedb.go
  3. 3
      hmy/api_backend.go
  4. 14
      node/node_handler.go
  5. 23
      staking/types/validator.go

@ -41,11 +41,9 @@ import (
"github.com/harmony-one/harmony/core/state"
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/core/vm"
internal_common "github.com/harmony-one/harmony/internal/common"
"github.com/harmony-one/harmony/internal/ctxerror"
"github.com/harmony-one/harmony/internal/params"
"github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/numeric"
"github.com/harmony-one/harmony/shard"
staking "github.com/harmony-one/harmony/staking/types"
lru "github.com/hashicorp/golang-lru"
@ -2381,32 +2379,16 @@ func (bc *BlockChain) ValidatorCandidates() []common.Address {
}
// ValidatorInformation returns the information of validator
func (bc *BlockChain) ValidatorInformation(addr common.Address) *staking.Validator {
commission := staking.Commission{
UpdateHeight: big.NewInt(0),
}
commission.CommissionRates = staking.CommissionRates{
Rate: numeric.Dec{Int: big.NewInt(0)},
MaxRate: numeric.Dec{Int: big.NewInt(0)},
MaxChangeRate: numeric.Dec{Int: big.NewInt(0)},
}
validator := &staking.Validator{
Address: internal_common.ParseAddr("0x0000000000000000000000000000000000000000000000000000000000000000"),
SlotPubKeys: make([]shard.BlsPublicKey, 0),
Stake: big.NewInt(0),
UnbondingHeight: big.NewInt(0),
MinSelfDelegation: big.NewInt(0),
Active: false,
}
validator.Commission = commission
validator.Description = staking.Description{
Name: "lol",
Identity: "lol",
Website: "lol",
SecurityContact: "lol",
Details: "lol",
}
return validator
func (bc *BlockChain) ValidatorInformation(addr common.Address) (*staking.Validator, error) {
state, err := bc.StateAt(bc.CurrentBlock().Root())
if err != nil || state == nil {
return nil, err
}
wrapper := state.GetStakingInfo(addr)
if wrapper == nil {
return nil, fmt.Errorf("ValidatorInformation not found: %v", addr)
}
return &wrapper.Validator, nil
}
// DelegatorsInformation returns up to date information of delegators of a given validator address

@ -691,6 +691,7 @@ func (db *DB) GetStakingInfo(addr common.Address) *stk.ValidatorWrapper {
val := stk.ValidatorWrapper{}
err := rlp.DecodeBytes(by, &val)
if err != nil {
fmt.Printf("GetStakingInfo unable to decode: %v\n", err)
return nil
}
return &val
@ -703,6 +704,7 @@ func (db *DB) UpdateStakingInfo(addr common.Address, val *stk.ValidatorWrapper)
return err
}
db.SetCode(addr, by)
return nil
}

@ -302,7 +302,8 @@ func (b *APIBackend) GetValidatorCandidates() []common.Address {
// GetValidatorInformation returns the information of validator
func (b *APIBackend) GetValidatorInformation(addr common.Address) *staking.Validator {
return b.hmy.BlockChain().ValidatorInformation(addr)
val, _ := b.hmy.BlockChain().ValidatorInformation(addr)
return val
}
// GetDelegatorsInformation returns up to date information of delegators of a given validator address

@ -410,10 +410,18 @@ func (node *Node) PostConsensusProcessing(newBlock *types.Block, commitSigAndBit
// AddNewBlock is usedd to add new block into the blockchain.
func (node *Node) AddNewBlock(newBlock *types.Block) error {
_, err := node.Blockchain().InsertChain([]*types.Block{newBlock}, true /* verifyHeaders */)
if len(newBlock.StakingTransactions()) >= 1 {
addrs, err := node.Blockchain().ReadValidatorList()
utils.Logger().Debug().Msgf("validator list updated, err=%v, len(addrs)=%v", err, len(addrs))
// Debug only
addrs, err := node.Blockchain().ReadValidatorList()
utils.Logger().Debug().Msgf("validator list updated, err=%v, len(addrs)=%v", err, len(addrs))
for i, addr := range addrs {
val, err := node.Blockchain().ValidatorInformation(addr)
if err != nil {
utils.Logger().Debug().Msgf("ValidatorInformation Error %v: err %v", i, err)
}
utils.Logger().Debug().Msgf("ValidatorInformation %v: %v", i, val)
}
if err != nil {
utils.Logger().Error().
Err(err).

@ -2,6 +2,7 @@ package types
import (
"errors"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
@ -27,10 +28,10 @@ var (
// ValidatorWrapper contains validator and its delegation information
type ValidatorWrapper struct {
Validator `json:"validator" yaml:"validator"`
Delegations []Delegation `json:"delegations" yaml:"delegations"`
SnapshotValidator *Validator `json:"snapshot_validator" yaml:"snaphost_validator"`
SnapshotDelegations []Delegation `json:"snapshot_delegations" yaml:"snapshot_delegations"`
Validator `json:"validator" yaml:"validator" rlp:"nil"`
Delegations []Delegation `json:"delegations" yaml:"delegations" rlp:"nil"`
SnapshotValidator *Validator `json:"snapshot_validator" yaml:"snaphost_validator" rlp:"nil"`
SnapshotDelegations []Delegation `json:"snapshot_delegations" yaml:"snapshot_delegations" rlp:"nil"`
}
// Validator - data fields for a validator
@ -169,3 +170,17 @@ func UpdateValidatorFromEditMsg(validator *Validator, edit *EditValidator) error
}
return nil
}
// String returns a human readable string representation of a validator.
func (v *Validator) String() string {
return fmt.Sprintf(`Validator
Address: %s
SlotPubKeys: %s
Stake: %s
Unbonding Height: %v
Minimum SelfDelegation: %v
Description: %v
Commission: %v`, v.Address, v.SlotPubKeys,
v.Stake, v.UnbondingHeight,
v.MinSelfDelegation, v.Description, v.Commission)
}

Loading…
Cancel
Save