Change active to elected for validator status (#2320)

pull/2326/head
Rongjian Lan 5 years ago committed by GitHub
parent 1a14dce41e
commit bbab20442c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      consensus/engine/consensus_engine.go
  2. 12
      core/blockchain.go
  3. 2
      core/chain_makers.go
  4. 10
      core/offchain.go
  5. 16
      core/rawdb/accessors_offchain.go
  6. 10
      core/rawdb/schema.go
  7. 6
      hmy/api_backend.go
  8. 2
      internal/chain/engine.go
  9. 2
      internal/hmyapi/apiv1/backend.go
  10. 6
      internal/hmyapi/apiv1/blockchain.go
  11. 2
      internal/hmyapi/apiv2/backend.go
  12. 6
      internal/hmyapi/apiv2/blockchain.go
  13. 2
      internal/hmyapi/backend.go
  14. 2
      staking/availability/measure.go
  15. 4
      staking/network/reward.go

@ -44,8 +44,8 @@ type ChainReader interface {
// Thus, only should be used to read the shard state of the current chain.
ReadShardState(epoch *big.Int) (*shard.State, error)
// ReadActiveValidatorList retrieves the list of active validators
ReadActiveValidatorList() ([]common.Address, error)
// ReadElectedValidatorList retrieves the list of elected validators
ReadElectedValidatorList() ([]common.Address, error)
// ReadValidatorList retrieves the list of all validators
ReadValidatorList() ([]common.Address, error)

@ -2328,9 +2328,9 @@ func (bc *BlockChain) WriteValidatorList(db rawdb.DatabaseWriter, addrs []common
return nil
}
// ReadActiveValidatorList reads the addresses of active validators
func (bc *BlockChain) ReadActiveValidatorList() ([]common.Address, error) {
if cached, ok := bc.validatorListCache.Get("activeValidatorList"); ok {
// ReadElectedValidatorList reads the addresses of elected validators
func (bc *BlockChain) ReadElectedValidatorList() ([]common.Address, error) {
if cached, ok := bc.validatorListCache.Get("electedValidatorList"); ok {
by := cached.([]byte)
m := []common.Address{}
if err := rlp.DecodeBytes(by, &m); err != nil {
@ -2341,16 +2341,16 @@ func (bc *BlockChain) ReadActiveValidatorList() ([]common.Address, error) {
return rawdb.ReadValidatorList(bc.db, true)
}
// WriteActiveValidatorList writes the list of active validator addresses to database
// WriteElectedValidatorList writes the list of elected validator addresses to database
// Note: this should only be called within the blockchain insert process.
func (bc *BlockChain) WriteActiveValidatorList(batch rawdb.DatabaseWriter, addrs []common.Address) error {
func (bc *BlockChain) WriteElectedValidatorList(batch rawdb.DatabaseWriter, addrs []common.Address) error {
err := rawdb.WriteValidatorList(batch, addrs, true)
if err != nil {
return err
}
bytes, err := rlp.EncodeToBytes(addrs)
if err == nil {
bc.validatorListCache.Add("activeValidatorList", bytes)
bc.validatorListCache.Add("electedValidatorList", bytes)
}
return nil
}

@ -271,7 +271,7 @@ func (cr *fakeChainReader) GetHeaderByHash(hash common.Hash) *block.Header
func (cr *fakeChainReader) GetHeader(hash common.Hash, number uint64) *block.Header { return nil }
func (cr *fakeChainReader) GetBlock(hash common.Hash, number uint64) *types.Block { return nil }
func (cr *fakeChainReader) ReadShardState(epoch *big.Int) (*shard.State, error) { return nil, nil }
func (cr *fakeChainReader) ReadActiveValidatorList() ([]common.Address, error) { return nil, nil }
func (cr *fakeChainReader) ReadElectedValidatorList() ([]common.Address, error) { return nil, nil }
func (cr *fakeChainReader) ReadValidatorList() ([]common.Address, error) { return nil, nil }
func (cr *fakeChainReader) ValidatorCandidates() []common.Address { return nil }
func (cr *fakeChainReader) SuperCommitteeForNextEpoch(beacon consensus_engine.ChainReader, header *block.Header, isVerify bool) (*shard.State, error) {

@ -92,8 +92,8 @@ func (bc *BlockChain) CommitOffChainData(
return NonStatTy, err
}
// Find all the active validator addresses and store them in db
allActiveValidators := []common.Address{}
// Find all the elected validator addresses and store them in db
allElectedValidators := []common.Address{}
processed := make(map[common.Address]struct{})
for i := range newShardState.Shards {
shard := newShardState.Shards[i]
@ -103,14 +103,14 @@ func (bc *BlockChain) CommitOffChainData(
_, ok := processed[slot.EcdsaAddress]
if !ok {
processed[slot.EcdsaAddress] = struct{}{}
allActiveValidators = append(allActiveValidators, shard.Slots[j].EcdsaAddress)
allElectedValidators = append(allElectedValidators, shard.Slots[j].EcdsaAddress)
}
}
}
}
// Update active validators
if err := bc.WriteActiveValidatorList(batch, allActiveValidators); err != nil {
// Update elected validators
if err := bc.WriteElectedValidatorList(batch, allElectedValidators); err != nil {
return NonStatTy, err
}

@ -246,11 +246,11 @@ func WriteValidatorStats(
}
// ReadValidatorList retrieves staking validator by its address
// Return only active validators if activeOnly==true, otherwise, return all validators
func ReadValidatorList(db DatabaseReader, activeOnly bool) ([]common.Address, error) {
// Return only elected validators if electedOnly==true, otherwise, return all validators
func ReadValidatorList(db DatabaseReader, electedOnly bool) ([]common.Address, error) {
key := validatorListKey
if activeOnly {
key = activeValidatorListKey
if electedOnly {
key = electedValidatorListKey
}
data, err := db.Get(key)
if err != nil || len(data) == 0 {
@ -265,11 +265,11 @@ func ReadValidatorList(db DatabaseReader, activeOnly bool) ([]common.Address, er
}
// WriteValidatorList stores staking validator's information by its address
// Writes only for active validators if activeOnly==true, otherwise, writes for all validators
func WriteValidatorList(db DatabaseWriter, addrs []common.Address, activeOnly bool) error {
// Writes only for elected validators if electedOnly==true, otherwise, writes for all validators
func WriteValidatorList(db DatabaseWriter, addrs []common.Address, electedOnly bool) error {
key := validatorListKey
if activeOnly {
key = activeValidatorListKey
if electedOnly {
key = electedValidatorListKey
}
bytes, err := rlp.EncodeToBytes(addrs)

@ -71,11 +71,11 @@ var (
cxReceiptSpentPrefix = []byte("cxReceiptSpent") // prefix for indicator of unspent of cxReceiptsProof
cxReceiptUnspentCheckpointPrefix = []byte("cxReceiptUnspentCheckpoint") // prefix for cxReceiptsProof unspent checkpoint
validatorPrefix = []byte("validator") // prefix for staking validator information
validatorSnapshotPrefix = []byte("validator-snapshot") // prefix for staking validator's snapshot information
validatorStatsPrefix = []byte("validator-stats") // prefix for staking validator's stats information
validatorListKey = []byte("validator-list") // key for all validators list
activeValidatorListKey = []byte("active-validator-list") // key for active validators list
validatorPrefix = []byte("validator") // prefix for staking validator information
validatorSnapshotPrefix = []byte("validator-snapshot") // prefix for staking validator's snapshot information
validatorStatsPrefix = []byte("validator-stats") // prefix for staking validator's stats information
validatorListKey = []byte("validator-list") // key for all validators list
electedValidatorListKey = []byte("elected-validator-list") // key for elected validators list
// epochBlockNumberPrefix + epoch (big.Int.Bytes())
// -> epoch block number (big.Int.Bytes())

@ -316,9 +316,9 @@ func (b *APIBackend) SendStakingTx(
return nil
}
// GetActiveValidatorAddresses returns the address of active validators for current epoch
func (b *APIBackend) GetActiveValidatorAddresses() []common.Address {
list, _ := b.hmy.BlockChain().ReadActiveValidatorList()
// GetElectedValidatorAddresses returns the address of elected validators for current epoch
func (b *APIBackend) GetElectedValidatorAddresses() []common.Address {
list, _ := b.hmy.BlockChain().ReadElectedValidatorList()
return list
}

@ -278,7 +278,7 @@ func (e *engineImpl) Finalize(
if isBeaconChain && isNewEpoch && inStakingEra {
validators, err := chain.ReadValidatorList()
if err != nil {
return nil, nil, ctxerror.New("[Finalize] failed to read active validators").WithCause(err)
return nil, nil, ctxerror.New("[Finalize] failed to read all validators").WithCause(err)
}
// Payout undelegated/unlocked tokens
for _, validator := range validators {

@ -71,7 +71,7 @@ type Backend interface {
ResendCx(ctx context.Context, txID common.Hash) (uint64, bool)
IsLeader() bool
SendStakingTx(ctx context.Context, newStakingTx *staking.StakingTransaction) error
GetActiveValidatorAddresses() []common.Address
GetElectedValidatorAddresses() []common.Address
GetAllValidatorAddresses() []common.Address
GetValidatorInformation(addr common.Address) *staking.Validator
GetValidatorStats(addr common.Address) *staking.ValidatorStats

@ -551,10 +551,10 @@ func (s *PublicBlockChainAPI) GetAllValidatorAddresses() ([]string, error) {
return addresses, nil
}
// GetActiveValidatorAddresses returns active validator addresses.
func (s *PublicBlockChainAPI) GetActiveValidatorAddresses() ([]string, error) {
// GetElectedValidatorAddresses returns elected validator addresses.
func (s *PublicBlockChainAPI) GetElectedValidatorAddresses() ([]string, error) {
addresses := []string{}
for _, addr := range s.b.GetActiveValidatorAddresses() {
for _, addr := range s.b.GetElectedValidatorAddresses() {
oneAddr, _ := internal_common.AddressToBech32(addr)
addresses = append(addresses, oneAddr)
}

@ -71,7 +71,7 @@ type Backend interface {
ResendCx(ctx context.Context, txID common.Hash) (uint64, bool)
IsLeader() bool
SendStakingTx(ctx context.Context, newStakingTx *staking.StakingTransaction) error
GetActiveValidatorAddresses() []common.Address
GetElectedValidatorAddresses() []common.Address
GetAllValidatorAddresses() []common.Address
GetValidatorInformation(addr common.Address) *staking.Validator
GetValidatorStats(addr common.Address) *staking.ValidatorStats

@ -516,10 +516,10 @@ func (s *PublicBlockChainAPI) GetAllValidatorAddresses() ([]string, error) {
return addresses, nil
}
// GetActiveValidatorAddresses returns active validator addresses.
func (s *PublicBlockChainAPI) GetActiveValidatorAddresses() ([]string, error) {
// GetElectedValidatorAddresses returns elected validator addresses.
func (s *PublicBlockChainAPI) GetElectedValidatorAddresses() ([]string, error) {
addresses := []string{}
for _, addr := range s.b.GetActiveValidatorAddresses() {
for _, addr := range s.b.GetElectedValidatorAddresses() {
oneAddr, _ := internal_common.AddressToBech32(addr)
addresses = append(addresses, oneAddr)
}

@ -73,7 +73,7 @@ type Backend interface {
ResendCx(ctx context.Context, txID common.Hash) (uint64, bool)
IsLeader() bool
SendStakingTx(ctx context.Context, newStakingTx *staking.StakingTransaction) error
GetActiveValidatorAddresses() []common.Address
GetElectedValidatorAddresses() []common.Address
GetAllValidatorAddresses() []common.Address
GetValidatorInformation(addr common.Address) *staking.Validator
GetValidatorStats(addr common.Address) *staking.ValidatorStats

@ -168,7 +168,7 @@ func SetInactiveUnavailableValidators(
bc engine.ChainReader, state *state.DB,
onlyConsider map[common.Address]struct{},
) error {
addrs, err := bc.ReadActiveValidatorList()
addrs, err := bc.ReadElectedValidatorList()
if err != nil {
return err
}

@ -52,8 +52,8 @@ func WhatPercentStakedNow(
timestamp int64,
) (*big.Int, *numeric.Dec, error) {
stakedNow := numeric.ZeroDec()
// Only active validators' stake is counted in stake ratio because only their stake is under slashing risk
active, err := beaconchain.ReadActiveValidatorList()
// Only elected validators' stake is counted in stake ratio because only their stake is under slashing risk
active, err := beaconchain.ReadElectedValidatorList()
if err != nil {
return nil, nil, err
}

Loading…
Cancel
Save