diff --git a/consensus/consensus.go b/consensus/consensus.go index fc59d5a2c..56f10f999 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -89,9 +89,6 @@ type Consensus struct { // the publickey of leader LeaderPubKey *bls.PublicKey - // number of publickeys of previous epoch - numPrevPubKeys int - viewID uint64 // Blockhash - 32 byte diff --git a/consensus/consensus_service.go b/consensus/consensus_service.go index ad5cba684..24598f388 100644 --- a/consensus/consensus_service.go +++ b/consensus/consensus_service.go @@ -480,27 +480,6 @@ func (consensus *Consensus) UpdateConsensusInformation() Mode { consensus.Decider.SetShardIDProvider(func() (uint32, error) { 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(). Uint64("block-number", curHeader.Number().Uint64()). @@ -511,7 +490,7 @@ func (consensus *Consensus) UpdateConsensusInformation() Mode { Msg("changing committee") } - pubKeys := []*bls.PublicKey{} + committeeToSet := &shard.Committee{} hasError := false // TODO: change GetCommitteePublicKeys to read from DB @@ -526,11 +505,6 @@ func (consensus *Consensus) UpdateConsensusInformation() Mode { return Syncing } - curCommittee := curShardState.FindCommitteeByID(curHeader.ShardID()) - curPubKeys := committee.WithStakingEnabled.GetCommitteePublicKeys( - curCommittee, - ) - consensus.numPrevPubKeys = len(curPubKeys) consensus.getLogger().Info().Msg("[UpdateConsensusInformation] Updating.....") if len(curHeader.ShardState()) > 0 { // increase curEpoch by one if it's the last block @@ -549,28 +523,40 @@ func (consensus *Consensus) UpdateConsensusInformation() Mode { return Syncing } - nextCommittee := nextShardState.FindCommitteeByID(curHeader.ShardID()) - pubKeys = committee.WithStakingEnabled.GetCommitteePublicKeys( - nextCommittee, - ) + committeeToSet = nextShardState.FindCommitteeByID(curHeader.ShardID()) + } else { consensus.SetEpochNum(curEpoch.Uint64()) - pubKeys = curPubKeys + committeeToSet = curShardState.FindCommitteeByID(curHeader.ShardID()) } - if len(pubKeys) == 0 { + if len(committeeToSet.Slots) == 0 { consensus.getLogger().Warn(). - Msg("[UpdateConsensusInformation] PublicKeys is Nil") + Msg("[UpdateConsensusInformation] No members in the committee to update") hasError = true } - // update public keys committee + // update public keys in the committee oldLeader := consensus.LeaderPubKey + pubKeys := committee.WithStakingEnabled.GetCommitteePublicKeys( + committeeToSet, + ) consensus.getLogger().Info(). Int("numPubKeys", len(pubKeys)). Msg("[UpdateConsensusInformation] Successfully updated public keys") 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 if !shard.Schedule.IsLastBlock(curHeader.Number().Uint64()) && curHeader.Number().Uint64() != 0 { diff --git a/consensus/quorum/one-node-staked-vote.go b/consensus/quorum/one-node-staked-vote.go index a3d3bf220..cc6aee59e 100644 --- a/consensus/quorum/one-node-staked-vote.go +++ b/consensus/quorum/one-node-staked-vote.go @@ -77,8 +77,14 @@ func (v *stakedVoteWeight) computeCurrentTotalPower(p Phase) numeric.Dec { for i := range members { if v.ReadSignature(p, members[i]) != nil { 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( - v.voters[w].effectivePercent, + voter.effectivePercent, ) } } diff --git a/core/rawdb/accessors_chain.go b/core/rawdb/accessors_chain.go index 35492dd0d..2647e78f1 100644 --- a/core/rawdb/accessors_chain.go +++ b/core/rawdb/accessors_chain.go @@ -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) { data, err := db.Get(cxReceiptKey(shardID, number, hash, temp)) if err != nil || len(data) == 0 { - utils.Logger().Info().Err(err).Uint64("number", number).Int("dataLen", len(data)).Msg("ReadCXReceipts") return nil, err } cxReceipts := types.CXReceipts{} 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 cxReceipts, nil diff --git a/internal/chain/engine.go b/internal/chain/engine.go index 12ef426fe..05cfb504e 100644 --- a/internal/chain/engine.go +++ b/internal/chain/engine.go @@ -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 // 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 { + if chain.Config().IsStaking(header.Epoch()) { + // Never recalculate after staking is enabled + reCalculate = false + } publicKeys, err := GetPublicKeys(chain, header, reCalculate) if err != nil { return ctxerror.New("[VerifyHeaderWithSignature] Cannot get publickeys for block header").WithCause(err) diff --git a/shard/committee/assignment.go b/shard/committee/assignment.go index b53d1d108..7e7d78078 100644 --- a/shard/committee/assignment.go +++ b/shard/committee/assignment.go @@ -148,7 +148,6 @@ func eposStakedCommittee( nil, }) } - staked := effective.Apply(essentials, stakedSlotsCount) shardBig := big.NewInt(int64(shardCount))