Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions consensus/spos/bls/v2/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,11 @@ func (sr *subroundSignature) WaitIfCompetingBlock(ctx context.Context, pkBytes [
return sr.waitIfCompetingBlock(ctx, pkBytes, nonce, currentHash)
}

// WaitIfCompetingBlockForNode calls the unexported waitIfCompetingBlockForNode function
func (sr *subroundSignature) WaitIfCompetingBlockForNode(ctx context.Context, nonce uint64, currentHash []byte) bool {
return sr.waitIfCompetingBlockForNode(ctx, nonce, currentHash)
}

// ShouldSendProof calls the unexported shouldSendProof function
func (sr *subroundEndRound) ShouldSendProof() bool {
return sr.shouldSendProof()
Expand Down
11 changes: 7 additions & 4 deletions consensus/spos/bls/v2/subroundEndRound.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,8 @@ func (sr *subroundEndRound) doEndRoundJobByNode() bool {
}

proofSent, err := sr.sendProof()
shouldWaitForMoreSignatures := errors.Is(err, spos.ErrInvalidNumSigShares)
// if not enough valid signatures were detected, wait a bit more
// either more signatures will be received, either proof from another participant
if shouldWaitForMoreSignatures {
// Not enough valid signatures: wait for more or for a proof from another participant
if errors.Is(err, spos.ErrInvalidNumSigShares) {
continue
}

Expand Down Expand Up @@ -389,6 +387,11 @@ func (sr *subroundEndRound) sendProof() (bool, error) {
return false, err
}

// Re-check grace period after aggregation which may have been slow under CPU contention
if !sr.shouldSendProof() {
return false, nil
}

// broadcast header proof
err = sr.createAndBroadcastProof(sig, bitmap, currentSender)
if err != nil && !errors.Is(err, ErrProofAlreadyPropagated) {
Expand Down
62 changes: 45 additions & 17 deletions consensus/spos/bls/v2/subroundSignature.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ func (sr *subroundSignature) doSignatureJob(ctx context.Context) bool {
return false
}

// Wait once for the entire node if competing block detected
nonce := sr.GetHeader().GetNonce()
currentHash := sr.GetData()
shouldAbort := sr.waitIfCompetingBlockForNode(ctx, nonce, currentHash)
if shouldAbort {
return false
}

isSelfSingleKeyInConsensusGroup := sr.IsNodeInConsensusGroup(sr.SelfPubKey()) && commonConsensus.ShouldConsiderSelfKeyInConsensus(sr.NodeRedundancyHandler())
if isSelfSingleKeyInConsensusGroup {
if !sr.doSignatureJobForSingleKey(ctx) {
Expand Down Expand Up @@ -251,16 +259,11 @@ func (sr *subroundSignature) doSignatureJobForManagedKeys(ctx context.Context) b
return sentSigForAllKeys.IsSet()
}

func (sr *subroundSignature) sendSignatureForManagedKey(ctx context.Context, idx int, pk string) bool {
func (sr *subroundSignature) sendSignatureForManagedKey(_ context.Context, idx int, pk string) bool {
pkBytes := []byte(pk)
nonce := sr.GetHeader().GetNonce()
currentHash := sr.GetData()

shouldAbort := sr.waitIfCompetingBlock(ctx, pkBytes, nonce, currentHash)
if shouldAbort {
return false
}

signatureShare, err := sr.SigningHandler().CreateSignatureShareForPublicKey(
currentHash,
uint16(idx),
Expand Down Expand Up @@ -301,16 +304,11 @@ func (sr *subroundSignature) checkGoRoutinesThrottler(ctx context.Context) error
return nil
}

func (sr *subroundSignature) doSignatureJobForSingleKey(ctx context.Context) bool {
func (sr *subroundSignature) doSignatureJobForSingleKey(_ context.Context) bool {
pkBytes := []byte(sr.SelfPubKey())
nonce := sr.GetHeader().GetNonce()
currentHash := sr.GetData()

shouldAbort := sr.waitIfCompetingBlock(ctx, pkBytes, nonce, currentHash)
if shouldAbort {
return false
}

selfIndex, err := sr.SelfConsensusGroupIndex()
if err != nil {
log.Debug("doSignatureJobForSingleKey.SelfConsensusGroupIndex: not in consensus group")
Expand Down Expand Up @@ -341,9 +339,33 @@ func (sr *subroundSignature) doSignatureJobForSingleKey(ctx context.Context) boo
return sr.completeSignatureSubRound(sr.SelfPubKey())
}

// waitIfCompetingBlock checks if this node already signed a different block for the same nonce.
// If so, it waits for a fraction of the round time to give the previous block's proof a chance to arrive.
// Returns true if the signing should be aborted (proof for previous block arrived or context cancelled).
// waitIfCompetingBlockForNode checks if any key managed by this node previously signed a different
// hash for the given nonce. If found, waits once for the entire node instead of per-key.
func (sr *subroundSignature) waitIfCompetingBlockForNode(ctx context.Context, nonce uint64, currentHash []byte) bool {
// Check self key first
selfPk := []byte(sr.SelfPubKey())
previousHash, exists := sr.sentSignatureTracker.GetSignedHash(selfPk, nonce)
if exists && !bytes.Equal(previousHash, currentHash) {
return sr.waitIfCompetingBlock(ctx, selfPk, nonce, currentHash)
}

// Check managed keys
for _, pk := range sr.ConsensusGroup() {
pkBytes := []byte(pk)
if !sr.IsKeyManagedBySelf(pkBytes) {
Comment thread
raduchis marked this conversation as resolved.
continue
}
previousHash, exists = sr.sentSignatureTracker.GetSignedHash(pkBytes, nonce)
if exists && !bytes.Equal(previousHash, currentHash) {
return sr.waitIfCompetingBlock(ctx, pkBytes, nonce, currentHash)
}
Comment on lines +347 to +361

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can come as an improvement afterwards, keeping the changes minimal for now.

}

return false
}

// waitIfCompetingBlock waits if this node already signed a different block for the same nonce.
// The delay is measured from round start. Returns true if signing should be aborted.
func (sr *subroundSignature) waitIfCompetingBlock(ctx context.Context, pkBytes []byte, nonce uint64, currentHash []byte) bool {
previousHash, exists := sr.sentSignatureTracker.GetSignedHash(pkBytes, nonce)
if !exists {
Expand All @@ -354,10 +376,16 @@ func (sr *subroundSignature) waitIfCompetingBlock(ctx context.Context, pkBytes [
return false
}

delay := time.Duration(float64(sr.RoundHandler().TimeDuration()) * competingBlockSignDelay)
// Delay is measured from round start, not from when this function is called
roundStart := sr.GetRoundTimeStamp()
targetTime := time.Duration(float64(sr.RoundHandler().TimeDuration()) * competingBlockSignDelay)
delay := sr.RoundHandler().RemainingTime(roundStart, targetTime)
if delay <= 0 {
log.Debug("waitIfCompetingBlock: already past competing block delay deadline, proceeding to sign")
return false
}

// Cap the delay so signing still happens within the signature subround window.
roundStart := sr.GetRoundTimeStamp()
sigEndDuration := time.Duration(sr.EndTime())
remaining := sr.RoundHandler().RemainingTime(roundStart, sigEndDuration)
safetyMargin := 10 * time.Millisecond
Expand Down
Loading
Loading