Fix staking epoch transition

pull/1875/head
Rongjian Lan 5 years ago
parent 3525de669e
commit 7291f8b248
  1. 3
      consensus/consensus.go
  2. 56
      consensus/consensus_service.go
  3. 8
      consensus/quorum/one-node-staked-vote.go
  4. 2
      core/rawdb/accessors_chain.go
  5. 4
      internal/chain/engine.go
  6. 1
      shard/committee/assignment.go

@ -89,9 +89,6 @@ type Consensus struct {
// the publickey of leader // the publickey of leader
LeaderPubKey *bls.PublicKey LeaderPubKey *bls.PublicKey
// number of publickeys of previous epoch
numPrevPubKeys int
viewID uint64 viewID uint64
// Blockhash - 32 byte // Blockhash - 32 byte

@ -480,27 +480,6 @@ func (consensus *Consensus) UpdateConsensusInformation() Mode {
consensus.Decider.SetShardIDProvider(func() (uint32, error) { consensus.Decider.SetShardIDProvider(func() (uint32, error) {
return consensus.ShardID, nil return consensus.ShardID, nil
}) })
s, err := committee.WithStakingEnabled.ReadFromDB(
nextEpoch, consensus.ChainReader,
)
if err != nil {
utils.Logger().Error().
Err(err).
Uint32("shard", consensus.ShardID).
Msg("Error when reading staking committee")
return Syncing
}
if _, err := consensus.Decider.SetVoters(
s.FindCommitteeByID(consensus.ShardID).Slots,
); err != nil {
utils.Logger().Error().
Err(err).
Uint32("shard", consensus.ShardID).
Msg("Error when updating voting power")
return Syncing
}
utils.Logger().Info(). utils.Logger().Info().
Uint64("block-number", curHeader.Number().Uint64()). Uint64("block-number", curHeader.Number().Uint64()).
@ -511,7 +490,7 @@ func (consensus *Consensus) UpdateConsensusInformation() Mode {
Msg("changing committee") Msg("changing committee")
} }
pubKeys := []*bls.PublicKey{} committeeToSet := &shard.Committee{}
hasError := false hasError := false
// TODO: change GetCommitteePublicKeys to read from DB // TODO: change GetCommitteePublicKeys to read from DB
@ -526,11 +505,6 @@ func (consensus *Consensus) UpdateConsensusInformation() Mode {
return Syncing return Syncing
} }
curCommittee := curShardState.FindCommitteeByID(curHeader.ShardID())
curPubKeys := committee.WithStakingEnabled.GetCommitteePublicKeys(
curCommittee,
)
consensus.numPrevPubKeys = len(curPubKeys)
consensus.getLogger().Info().Msg("[UpdateConsensusInformation] Updating.....") consensus.getLogger().Info().Msg("[UpdateConsensusInformation] Updating.....")
if len(curHeader.ShardState()) > 0 { if len(curHeader.ShardState()) > 0 {
// increase curEpoch by one if it's the last block // increase curEpoch by one if it's the last block
@ -549,28 +523,40 @@ func (consensus *Consensus) UpdateConsensusInformation() Mode {
return Syncing return Syncing
} }
nextCommittee := nextShardState.FindCommitteeByID(curHeader.ShardID()) committeeToSet = nextShardState.FindCommitteeByID(curHeader.ShardID())
pubKeys = committee.WithStakingEnabled.GetCommitteePublicKeys(
nextCommittee,
)
} else { } else {
consensus.SetEpochNum(curEpoch.Uint64()) consensus.SetEpochNum(curEpoch.Uint64())
pubKeys = curPubKeys committeeToSet = curShardState.FindCommitteeByID(curHeader.ShardID())
} }
if len(pubKeys) == 0 { if len(committeeToSet.Slots) == 0 {
consensus.getLogger().Warn(). consensus.getLogger().Warn().
Msg("[UpdateConsensusInformation] PublicKeys is Nil") Msg("[UpdateConsensusInformation] No members in the committee to update")
hasError = true hasError = true
} }
// update public keys committee // update public keys in the committee
oldLeader := consensus.LeaderPubKey oldLeader := consensus.LeaderPubKey
pubKeys := committee.WithStakingEnabled.GetCommitteePublicKeys(
committeeToSet,
)
consensus.getLogger().Info(). consensus.getLogger().Info().
Int("numPubKeys", len(pubKeys)). Int("numPubKeys", len(pubKeys)).
Msg("[UpdateConsensusInformation] Successfully updated public keys") Msg("[UpdateConsensusInformation] Successfully updated public keys")
consensus.UpdatePublicKeys(pubKeys) consensus.UpdatePublicKeys(pubKeys)
// Update voters in the committee
if _, err := consensus.Decider.SetVoters(
committeeToSet.Slots,
); err != nil {
utils.Logger().Error().
Err(err).
Uint32("shard", consensus.ShardID).
Msg("Error when updating voters")
return Syncing
}
// take care of possible leader change during the curEpoch // take care of possible leader change during the curEpoch
if !shard.Schedule.IsLastBlock(curHeader.Number().Uint64()) && if !shard.Schedule.IsLastBlock(curHeader.Number().Uint64()) &&
curHeader.Number().Uint64() != 0 { curHeader.Number().Uint64() != 0 {

@ -77,8 +77,14 @@ func (v *stakedVoteWeight) computeCurrentTotalPower(p Phase) numeric.Dec {
for i := range members { for i := range members {
if v.ReadSignature(p, members[i]) != nil { if v.ReadSignature(p, members[i]) != nil {
w.FromLibBLSPublicKey(members[i]) w.FromLibBLSPublicKey(members[i])
voter, ok := v.voters[w]
if !ok {
utils.Logger().Error().Bytes("BlsPubKey", w[:]).Msg("No voter found")
return numeric.ZeroDec()
}
currentTotalPower = currentTotalPower.Add( currentTotalPower = currentTotalPower.Add(
v.voters[w].effectivePercent, voter.effectivePercent,
) )
} }
} }

@ -543,12 +543,10 @@ func WriteShardLastCrossLink(db DatabaseWriter, shardID uint32, data []byte) err
func ReadCXReceipts(db DatabaseReader, shardID uint32, number uint64, hash common.Hash, temp bool) (types.CXReceipts, error) { func ReadCXReceipts(db DatabaseReader, shardID uint32, number uint64, hash common.Hash, temp bool) (types.CXReceipts, error) {
data, err := db.Get(cxReceiptKey(shardID, number, hash, temp)) data, err := db.Get(cxReceiptKey(shardID, number, hash, temp))
if err != nil || len(data) == 0 { if err != nil || len(data) == 0 {
utils.Logger().Info().Err(err).Uint64("number", number).Int("dataLen", len(data)).Msg("ReadCXReceipts")
return nil, err return nil, err
} }
cxReceipts := types.CXReceipts{} cxReceipts := types.CXReceipts{}
if err := rlp.DecodeBytes(data, &cxReceipts); err != nil { if err := rlp.DecodeBytes(data, &cxReceipts); err != nil {
utils.Logger().Error().Err(err).Str("hash", hash.Hex()).Msg("Invalid cross-shard tx receipt array RLP")
return nil, err return nil, err
} }
return cxReceipts, nil return cxReceipts, nil

@ -247,6 +247,10 @@ func QuorumForBlock(
// i.e. this header verification api is more flexible since the caller specifies which commit signature and bitmap to use // i.e. this header verification api is more flexible since the caller specifies which commit signature and bitmap to use
// for verifying the block header, which is necessary for cross-shard block header verification. Example of such is cross-shard transaction. // for verifying the block header, which is necessary for cross-shard block header verification. Example of such is cross-shard transaction.
func (e *engineImpl) VerifyHeaderWithSignature(chain engine.ChainReader, header *block.Header, commitSig []byte, commitBitmap []byte, reCalculate bool) error { func (e *engineImpl) VerifyHeaderWithSignature(chain engine.ChainReader, header *block.Header, commitSig []byte, commitBitmap []byte, reCalculate bool) error {
if chain.Config().IsStaking(header.Epoch()) {
// Never recalculate after staking is enabled
reCalculate = false
}
publicKeys, err := GetPublicKeys(chain, header, reCalculate) publicKeys, err := GetPublicKeys(chain, header, reCalculate)
if err != nil { if err != nil {
return ctxerror.New("[VerifyHeaderWithSignature] Cannot get publickeys for block header").WithCause(err) return ctxerror.New("[VerifyHeaderWithSignature] Cannot get publickeys for block header").WithCause(err)

@ -148,7 +148,6 @@ func eposStakedCommittee(
nil, nil,
}) })
} }
staked := effective.Apply(essentials, stakedSlotsCount) staked := effective.Apply(essentials, stakedSlotsCount)
shardBig := big.NewInt(int64(shardCount)) shardBig := big.NewInt(int64(shardCount))

Loading…
Cancel
Save