diff --git a/consensus/consensus_msg_sender.go b/consensus/consensus_msg_sender.go index ffa9d8b1c6..ad32b91ce3 100644 --- a/consensus/consensus_msg_sender.go +++ b/consensus/consensus_msg_sender.go @@ -121,7 +121,7 @@ func (sender *MessageSender) Retry(msgRetry *MessageRetry) { msgRetry.retryCount++ if err := sender.host.SendMessageToGroups(msgRetry.groups, msgRetry.p2pMsg); err != nil { - utils.Logger().Warn().Str("groupID[0]", msgRetry.groups[0].String()).Uint64("blockNum", msgRetry.blockNum).Str("MsgType", msgRetry.msgType.String()).Int("RetryCount", msgRetry.retryCount).Msg("[Retry] Failed re-sending consensus message") + utils.Logger().Warn().Str("groupID[0]", msgRetry.groups[0].String()).Uint64("blockNum", msgRetry.blockNum).Str("MsgType", msgRetry.msgType.String()).Int("RetryCount", msgRetry.retryCount).Err(err).Msg("[Retry] Failed re-sending consensus message") } else { utils.Logger().Info().Str("groupID[0]", msgRetry.groups[0].String()).Uint64("blockNum", msgRetry.blockNum).Str("MsgType", msgRetry.msgType.String()).Int("RetryCount", msgRetry.retryCount).Msg("[Retry] Successfully resent consensus message") } diff --git a/consensus/consensus_service.go b/consensus/consensus_service.go index d658fe83d6..2a2c306264 100644 --- a/consensus/consensus_service.go +++ b/consensus/consensus_service.go @@ -435,7 +435,7 @@ func (consensus *Consensus) updateConsensusInformation() Mode { } else { consensus.getLogger().Info(). Str("leaderPubKey", leaderPubKey.Bytes.Hex()). - Msg("[UpdateConsensusInformation] Most Recent LeaderPubKey Updated Based on BlockChain") + Msgf("[UpdateConsensusInformation] Most Recent LeaderPubKey Updated Based on BlockChain, blocknum: %d", curHeader.NumberU64()) consensus.LeaderPubKey = leaderPubKey } } @@ -466,7 +466,7 @@ func (consensus *Consensus) updateConsensusInformation() Mode { } } consensus.getLogger().Info(). - Msg("[UpdateConsensusInformation] not in committee, Listening") + Msgf("[UpdateConsensusInformation] not in committee, keys len %d Listening", len(pubKeys)) // not in committee return Listening @@ -652,6 +652,7 @@ func (consensus *Consensus) GetLogger() *zerolog.Logger { // getLogger returns logger for consensus contexts added func (consensus *Consensus) getLogger() *zerolog.Logger { logger := utils.Logger().With(). + Uint32("shardID", consensus.ShardID). Uint64("myBlock", consensus.blockNum). Uint64("myViewID", consensus.getCurBlockViewID()). Str("phase", consensus.phase.String()). diff --git a/consensus/leader.go b/consensus/leader.go index 4a227fa62d..d851668aa8 100644 --- a/consensus/leader.go +++ b/consensus/leader.go @@ -82,12 +82,12 @@ func (consensus *Consensus) announce(block *types.Block) { Str("groupID", string(nodeconfig.NewGroupIDByShardID( nodeconfig.ShardID(consensus.ShardID), ))). - Msg("[Announce] Cannot send announce message") + Msgf("[Announce] Cannot send announce message with message signer %s", key.Pub.Hex()) } else { consensus.getLogger().Info(). Str("blockHash", block.Hash().Hex()). Uint64("blockNum", block.NumberU64()). - Msg("[Announce] Sent Announce Message!!") + Msgf("[Announce] Sent Announce Message with message signer %s", key.Pub.Hex()) } consensus.switchPhase("Announce", FBFTPrepare) diff --git a/consensus/validator.go b/consensus/validator.go index 2f14f76b42..14f4b52167 100644 --- a/consensus/validator.go +++ b/consensus/validator.go @@ -137,7 +137,11 @@ func (consensus *Consensus) prepare() { return } - priKeys := consensus.getPriKeysInCommittee() + priKeys, err := consensus.getPriKeysInCommittee() + if err != nil { + consensus.getLogger().Warn().Err(err).Msg("[OnAnnounce] Cannot get priKey in committee") + return + } p2pMsgs := consensus.constructP2pMessages(msg_pb.MessageType_PREPARE, nil, priKeys) @@ -156,7 +160,11 @@ func (consensus *Consensus) sendCommitMessages(blockObj *types.Block) { return } - priKeys := consensus.getPriKeysInCommittee() + priKeys, err := consensus.getPriKeysInCommittee() + if err != nil { + consensus.getLogger().Warn().Err(err).Msg("[sendCommitMessages] Cannot get priKey in committee") + return + } // Sign commit signature on the received block and construct the p2p messages commitPayload := signature.ConstructCommitPayload(consensus.Blockchain().Config(), @@ -392,7 +400,10 @@ func (consensus *Consensus) onCommitted(recvMsg *FBFTMessage) { // Collect private keys that are part of the current committee. // TODO: cache valid private keys and only update when keys change. -func (consensus *Consensus) getPriKeysInCommittee() []*bls.PrivateKeyWrapper { +func (consensus *Consensus) getPriKeysInCommittee() ([]*bls.PrivateKeyWrapper, error) { + if len(consensus.priKey) == 0 { + return nil, errors.New("no private keys in the committee") + } priKeys := []*bls.PrivateKeyWrapper{} for i, key := range consensus.priKey { if !consensus.isValidatorInCommittee(key.Pub.Bytes) { @@ -400,7 +411,7 @@ func (consensus *Consensus) getPriKeysInCommittee() []*bls.PrivateKeyWrapper { } priKeys = append(priKeys, &consensus.priKey[i]) } - return priKeys + return priKeys, nil } func (consensus *Consensus) constructP2pMessages(msgType msg_pb.MessageType, payloadForSign []byte, priKeys []*bls.PrivateKeyWrapper) []*NetworkMessage { diff --git a/consensus/votepower/roster.go b/consensus/votepower/roster.go index bebd90bbaa..96a3ac9c6b 100644 --- a/consensus/votepower/roster.go +++ b/consensus/votepower/roster.go @@ -90,19 +90,16 @@ func (v AccommodateHarmonyVote) String() string { return string(s) } -type topLevelRegistry struct { - OurVotingPowerTotalPercentage numeric.Dec - TheirVotingPowerTotalPercentage numeric.Dec - TotalEffectiveStake numeric.Dec - HMYSlotCount int64 -} - // Roster .. type Roster struct { - Voters map[bls.SerializedPublicKey]*AccommodateHarmonyVote - topLevelRegistry + Voters map[bls.SerializedPublicKey]*AccommodateHarmonyVote ShardID uint32 OrderedSlots []bls.SerializedPublicKey + + OurVotingPowerTotalPercentage numeric.Dec + TheirVotingPowerTotalPercentage numeric.Dec + TotalEffectiveStake numeric.Dec + HMYSlotCount int64 } func (r Roster) String() string { @@ -244,13 +241,12 @@ func Compute(subComm *shard.Committee, epoch *big.Int) (*Roster, error) { func NewRoster(shardID uint32) *Roster { m := map[bls.SerializedPublicKey]*AccommodateHarmonyVote{} return &Roster{ - Voters: m, - topLevelRegistry: topLevelRegistry{ - OurVotingPowerTotalPercentage: numeric.ZeroDec(), - TheirVotingPowerTotalPercentage: numeric.ZeroDec(), - TotalEffectiveStake: numeric.ZeroDec(), - }, + Voters: m, ShardID: shardID, + + OurVotingPowerTotalPercentage: numeric.ZeroDec(), + TheirVotingPowerTotalPercentage: numeric.ZeroDec(), + TotalEffectiveStake: numeric.ZeroDec(), } } diff --git a/crypto/bls/bls.go b/crypto/bls/bls.go index 7a8159ef87..4b6fe593bc 100644 --- a/crypto/bls/bls.go +++ b/crypto/bls/bls.go @@ -32,6 +32,11 @@ type PublicKeyWrapper struct { Object *bls.PublicKey } +// Hex returns the hex string of the public key +func (pk *PublicKeyWrapper) Hex() string { + return pk.Bytes.Hex() +} + // WrapperFromPrivateKey makes a PrivateKeyWrapper from bls secret key func WrapperFromPrivateKey(pri *bls.SecretKey) PrivateKeyWrapper { pub := pri.GetPublicKey() diff --git a/internal/chain/reward.go b/internal/chain/reward.go index c64199a20c..42212d6db2 100644 --- a/internal/chain/reward.go +++ b/internal/chain/reward.go @@ -511,7 +511,6 @@ func distributeRewardBeforeAggregateEpoch(bc engine.ChainReader, state *state.DB subComm := shard.Committee{ShardID: shard.BeaconChainShardID, Slots: members} if err := availability.IncrementValidatorSigningCounts( - beaconChain, subComm.StakedValidators(), state, payable, @@ -599,7 +598,7 @@ func processOneCrossLink(bc engine.ChainReader, state *state.DB, cxLink types.Cr staked := subComm.StakedValidators() startTimeLocal = time.Now() if err := availability.IncrementValidatorSigningCounts( - bc, staked, state, payableSigners, missing, + staked, state, payableSigners, missing, ); err != nil { return nil, nil, err } diff --git a/staking/availability/measure.go b/staking/availability/measure.go index 6bf36bfb05..575c804380 100644 --- a/staking/availability/measure.go +++ b/staking/availability/measure.go @@ -92,7 +92,6 @@ type signerKind struct { } func bumpCount( - bc Reader, state ValidatorState, signers []signerKind, stakedAddrSet map[common.Address]struct{}, @@ -129,13 +128,12 @@ func bumpCount( // IncrementValidatorSigningCounts .. func IncrementValidatorSigningCounts( - bc Reader, staked *shard.StakedSlots, state ValidatorState, signers, missing shard.SlotList, ) error { return bumpCount( - bc, state, []signerKind{{false, missing}, {true, signers}}, + state, []signerKind{{false, missing}, {true, signers}}, staked.LookupSet, ) } diff --git a/staking/availability/measure_test.go b/staking/availability/measure_test.go index 9a35ad80eb..393d19bb4f 100644 --- a/staking/availability/measure_test.go +++ b/staking/availability/measure_test.go @@ -158,7 +158,7 @@ func TestIncrementValidatorSigningCounts(t *testing.T) { if err != nil { t.Fatal(err) } - if err := IncrementValidatorSigningCounts(nil, ctx.staked, ctx.state, ctx.signers, + if err := IncrementValidatorSigningCounts(ctx.staked, ctx.state, ctx.signers, ctx.missings); err != nil { t.Fatal(err)