write validator stats at the end of prestaking epoch (#3021)

* write validator stats at the end of prestaking epoch

* also need to write block reward accumulator
pull/3023/head
Ganesha Upadhyaya 5 years ago committed by GitHub
parent a545327567
commit 31ecffc5f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 68
      core/offchain.go
  2. 9
      hmy/api_backend.go

@ -35,6 +35,7 @@ func (bc *BlockChain) CommitOffChainData(
rawdb.WriteReceipts(batch, block.Hash(), block.NumberU64(), receipts)
isBeaconChain := bc.CurrentHeader().ShardID() == shard.BeaconChainShardID
isStaking := bc.chainConfig.IsStaking(block.Epoch())
isPreStaking := bc.chainConfig.IsPreStaking(block.Epoch())
header := block.Header()
isNewEpoch := len(header.ShardState()) > 0
// Cross-shard txns
@ -253,34 +254,8 @@ func (bc *BlockChain) CommitOffChainData(
}
}
type t struct {
addr common.Address
stats *staking.ValidatorStats
}
sortedStats, i := make([]t, len(tempValidatorStats)), 0
for key := range tempValidatorStats {
sortedStats[i] = t{key, tempValidatorStats[key]}
i++
}
sort.SliceStable(
sortedStats,
func(i, j int) bool {
return bytes.Compare(
sortedStats[i].addr[:], sortedStats[j].addr[:],
) == -1
},
)
for _, stat := range sortedStats {
if err := rawdb.WriteValidatorStats(
batch, stat.addr, stat.stats,
); err != nil {
utils.Logger().Info().Err(err).
Str("validator address", stat.addr.Hex()).
Msg("could not update stats for validator")
}
}
bc.writeValidatorStats(tempValidatorStats, batch)
records := slash.Records{}
if s := header.Slashes(); len(s) > 0 {
@ -292,6 +267,11 @@ func (bc *BlockChain) CommitOffChainData(
}
}
} else {
if isNewEpoch && isPreStaking {
// if prestaking and last block, write out the validator stats
// so that it is available for the staking epoch
bc.writeValidatorStats(tempValidatorStats, batch)
}
// block reward never accumulate before staking
bc.WriteBlockRewardAccumulator(batch, common.Big0, block.Number().Uint64())
}
@ -300,6 +280,40 @@ func (bc *BlockChain) CommitOffChainData(
return CanonStatTy, nil
}
func (bc *BlockChain) writeValidatorStats(
tempValidatorStats map[common.Address]*staking.ValidatorStats,
batch rawdb.DatabaseWriter,
) {
type t struct {
addr common.Address
stats *staking.ValidatorStats
}
sortedStats, i := make([]t, len(tempValidatorStats)), 0
for key := range tempValidatorStats {
sortedStats[i] = t{key, tempValidatorStats[key]}
i++
}
sort.SliceStable(
sortedStats,
func(i, j int) bool {
return bytes.Compare(
sortedStats[i].addr[:], sortedStats[j].addr[:],
) == -1
},
)
for _, stat := range sortedStats {
if err := rawdb.WriteValidatorStats(
batch, stat.addr, stat.stats,
); err != nil {
utils.Logger().Info().Err(err).
Str("validator address", stat.addr.Hex()).
Msg("could not update stats for validator")
}
}
}
func (bc *BlockChain) getNextBlockEpoch(header *block.Header) (*big.Int, error) {
nextBlockEpoch := header.Epoch()
if len(header.ShardState()) > 0 {

@ -433,6 +433,12 @@ func (b *APIBackend) GetValidatorInformation(
) % shard.Schedule.BlocksPerEpoch()
computed.BlocksLeftInEpoch = shard.Schedule.BlocksPerEpoch() - beaconChainBlocks
if defaultReply.CurrentlyInCommittee {
defaultReply.Performance = &staking.CurrentEpochPerformance{
CurrentSigningPercentage: *computed,
}
}
stats, err := bc.ReadValidatorStats(addr)
if err != nil {
// when validator has no stats, default boot-status to not booted
@ -491,9 +497,6 @@ func (b *APIBackend) GetValidatorInformation(
}
if defaultReply.CurrentlyInCommittee {
defaultReply.Performance = &staking.CurrentEpochPerformance{
CurrentSigningPercentage: *computed,
}
defaultReply.ComputedMetrics = stats
defaultReply.EPoSWinningStake = &stats.TotalEffectiveStake
}

Loading…
Cancel
Save