Fix both total stake counting and delegation index for validator itself (#2952)

pull/2953/head
Rongjian Lan 5 years ago committed by GitHub
parent ff488a65be
commit 3d42325d98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      core/blockchain.go
  2. 1
      core/rawdb/accessors_offchain.go
  3. 19
      shard/committee/assignment.go

@ -2651,11 +2651,15 @@ func (bc *BlockChain) prepareStakingMetaData(
blockNum, blockNum,
} }
delegations, ok := newDelegations[createValidator.ValidatorAddress] delegations, ok := newDelegations[createValidator.ValidatorAddress]
if ok { if !ok {
delegations = append(delegations, selfIndex) // If the cache doesn't have it, load it from DB for the first time.
} else { delegations, err = bc.ReadDelegationsByDelegator(createValidator.ValidatorAddress)
delegations = staking.DelegationIndexes{selfIndex} if err != nil {
return nil, nil, err
}
} }
delegations = append(delegations, selfIndex)
newDelegations[createValidator.ValidatorAddress] = delegations newDelegations[createValidator.ValidatorAddress] = delegations
case staking.DirectiveEditValidator: case staking.DirectiveEditValidator:
case staking.DirectiveDelegate: case staking.DirectiveDelegate:

@ -242,6 +242,7 @@ func WriteValidatorList(
} }
// ReadDelegationsByDelegator retrieves the list of validators delegated by a delegator // ReadDelegationsByDelegator retrieves the list of validators delegated by a delegator
// Returns empty results instead of error if there is not data found.
func ReadDelegationsByDelegator(db DatabaseReader, delegator common.Address) (staking.DelegationIndexes, error) { func ReadDelegationsByDelegator(db DatabaseReader, delegator common.Address) (staking.DelegationIndexes, error) {
data, err := db.Get(delegatorValidatorListKey(delegator)) data, err := db.Get(delegatorValidatorListKey(delegator))
if err != nil || len(data) == 0 { if err != nil || len(data) == 0 {

@ -146,15 +146,6 @@ func prepareOrders(
continue continue
} }
validatorStake := big.NewInt(0)
for i := range validator.Delegations {
validatorStake.Add(
validatorStake, validator.Delegations[i].Amount,
)
}
totalStaked.Add(totalStaked, validatorStake)
found := false found := false
for _, key := range validator.SlotPubKeys { for _, key := range validator.SlotPubKeys {
if _, ok := blsKeys[key]; ok { if _, ok := blsKeys[key]; ok {
@ -168,6 +159,15 @@ func prepareOrders(
continue continue
} }
validatorStake := big.NewInt(0)
for i := range validator.Delegations {
validatorStake.Add(
validatorStake, validator.Delegations[i].Amount,
)
}
totalStaked.Add(totalStaked, validatorStake)
essentials[validator.Address] = &effective.SlotOrder{ essentials[validator.Address] = &effective.SlotOrder{
validatorStake, validatorStake,
validator.SlotPubKeys, validator.SlotPubKeys,
@ -240,6 +240,7 @@ var (
ErrComputeForEpochInPast = errors.New("cannot compute for epoch in past") ErrComputeForEpochInPast = errors.New("cannot compute for epoch in past")
) )
// This is the shard state computation logic before staking epoch.
func preStakingEnabledCommittee(s shardingconfig.Instance) *shard.State { func preStakingEnabledCommittee(s shardingconfig.Instance) *shard.State {
shardNum := int(s.NumShards()) shardNum := int(s.NumShards())
shardHarmonyNodes := s.NumHarmonyOperatedNodesPerShard() shardHarmonyNodes := s.NumHarmonyOperatedNodesPerShard()

Loading…
Cancel
Save