From 5b4a09802f45ffe1d3a4dd60aab2b31b7c62f110 Mon Sep 17 00:00:00 2001 From: ssd04 Date: Wed, 18 Dec 2024 15:31:30 +0200 Subject: [PATCH 1/5] subround endround fixes --- consensus/spos/bls/v2/subroundEndRound.go | 50 ++++++++++++++++------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/consensus/spos/bls/v2/subroundEndRound.go b/consensus/spos/bls/v2/subroundEndRound.go index e5bc0d9a6a..30194e3970 100644 --- a/consensus/spos/bls/v2/subroundEndRound.go +++ b/consensus/spos/bls/v2/subroundEndRound.go @@ -101,6 +101,8 @@ func (sr *subroundEndRound) receivedProof(proof consensus.ProofHandler) { sr.mutProcessingEndRound.Lock() defer sr.mutProcessingEndRound.Unlock() + log.Error("receivedProof: PROOF", "proofHeader", proof.GetHeaderHash()) + if sr.IsJobDone(sr.SelfPubKey(), sr.Current()) { return } @@ -219,6 +221,9 @@ func (sr *subroundEndRound) doEndRoundJob(_ context.Context) bool { return false } + sr.mutProcessingEndRound.Lock() + defer sr.mutProcessingEndRound.Unlock() + return sr.doEndRoundJobByNode() } @@ -240,14 +245,17 @@ func (sr *subroundEndRound) commitBlock() error { } func (sr *subroundEndRound) doEndRoundJobByNode() bool { - if !sr.waitForSignalSync() { - return false + if sr.shouldSendProof() { + if !sr.waitForSignalSync() { + return false + } } - sr.mutProcessingEndRound.Lock() - defer sr.mutProcessingEndRound.Unlock() + proof, ok := sr.sendProof() - proof := sr.sendProof() + if !ok { + return false + } err := sr.commitBlock() if err != nil { @@ -268,6 +276,7 @@ func (sr *subroundEndRound) doEndRoundJobByNode() bool { sr.worker.DisplayStatistics() log.Debug("step 3: Body and Header have been committed") + log.Trace("step 3: Body and Header have been committed", "headerHash", sr.GetData()) msg := fmt.Sprintf("Added proposed block with nonce %d in blockchain", sr.GetHeader().GetNonce()) log.Debug(display.Headline(msg, sr.SyncTimer().FormattedCurrentTime(), "+")) @@ -277,29 +286,31 @@ func (sr *subroundEndRound) doEndRoundJobByNode() bool { return true } -func (sr *subroundEndRound) sendProof() data.HeaderProofHandler { +func (sr *subroundEndRound) sendProof() (data.HeaderProofHandler, bool) { if !sr.shouldSendProof() { - return nil + log.Trace("sendProof: should not send proof") + return nil, true } bitmap := sr.GenerateBitmap(bls.SrSignature) err := sr.checkSignaturesValidity(bitmap) if err != nil { log.Debug("sendProof.checkSignaturesValidity", "error", err.Error()) - return nil + return nil, false } // Aggregate signatures, handle invalid signers and send final info if needed bitmap, sig, err := sr.aggregateSigsAndHandleInvalidSigners(bitmap) if err != nil { log.Debug("sendProof.aggregateSigsAndHandleInvalidSigners", "error", err.Error()) - return nil + return nil, false } ok := sr.ScheduledProcessor().IsProcessedOKWithTimeout() // placeholder for subroundEndRound.doEndRoundJobByLeader script if !ok { - return nil + log.Error("sendProof: sheduled processor timeout") + return nil, false } roundHandler := sr.RoundHandler() @@ -307,7 +318,7 @@ func (sr *subroundEndRound) sendProof() data.HeaderProofHandler { log.Debug("sendProof: time is out -> cancel broadcasting final info and header", "round time stamp", roundHandler.TimeStamp(), "current time", time.Now()) - return nil + return nil, false } // broadcast header proof @@ -519,7 +530,7 @@ func (sr *subroundEndRound) computeAggSigOnValidNodes() ([]byte, []byte, error) return bitmap, sig, nil } -func (sr *subroundEndRound) createAndBroadcastProof(signature []byte, bitmap []byte) *block.HeaderProof { +func (sr *subroundEndRound) createAndBroadcastProof(signature []byte, bitmap []byte) (*block.HeaderProof, bool) { headerProof := &block.HeaderProof{ PubKeysBitmap: bitmap, AggregatedSignature: signature, @@ -531,14 +542,15 @@ func (sr *subroundEndRound) createAndBroadcastProof(signature []byte, bitmap []b err := sr.BroadcastMessenger().BroadcastEquivalentProof(headerProof, []byte(sr.SelfPubKey())) if err != nil { - return nil + log.Error("failed to broadcast equivalent proof", "error", err) + return nil, false } log.Debug("step 3: block header proof has been sent", "PubKeysBitmap", bitmap, "AggregateSignature", signature) - return headerProof + return headerProof, true } func (sr *subroundEndRound) createAndBroadcastInvalidSigners(invalidSigners []byte) { @@ -710,7 +722,7 @@ func (sr *subroundEndRound) waitForSignalSync() bool { case <-timerBetweenStatusChecks.C: if sr.IsSubroundFinished(sr.Current()) { log.Trace("subround already finished", "subround", sr.Name()) - return false + return true } if sr.checkReceivedSignatures() { @@ -828,6 +840,8 @@ func (sr *subroundEndRound) checkReceivedSignatures() bool { isSelfJobDone := sr.IsSelfJobDone(bls.SrSignature) + // log.Error("checkReceivedSignatures", "isSelfJobDone", isSelfJobDone, "numSigs", numSigs, "consensusSize", sr.ConsensusGroupSize()) + shouldStopWaitingSignatures := isSelfJobDone && isSignatureCollectionDone if shouldStopWaitingSignatures { log.Debug("step 2: signatures collection done", @@ -856,6 +870,12 @@ func (sr *subroundEndRound) getNumOfSignaturesCollected() int { continue } + // log.Error("getNumOfSignaturesCollected.JobDone", + // "node", node, + // "isSignJobDone", isSignJobDone, + // "subround", sr.Name(), + // ) + if isSignJobDone { n++ } From 537eb345ea047f26ab5f5f18820204725c74898b Mon Sep 17 00:00:00 2001 From: ssd04 Date: Wed, 18 Dec 2024 15:34:59 +0200 Subject: [PATCH 2/5] nil proof check in worker --- consensus/spos/worker.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/consensus/spos/worker.go b/consensus/spos/worker.go index 62f31dfc8a..e539071331 100644 --- a/consensus/spos/worker.go +++ b/consensus/spos/worker.go @@ -312,7 +312,12 @@ func (wrk *Worker) AddReceivedHeaderHandler(handler func(data.HeaderHandler)) { // ReceivedProof process the received proof, calling each received proof handler registered in worker instance func (wrk *Worker) ReceivedProof(proofHandler consensus.ProofHandler) { - // TODO: add preliminary checks + if check.IfNilReflect(proofHandler) { + log.Trace("ReceivedProof: nil proof handler") + return + } + + log.Trace("ReceivedProof:", "proof header", proofHandler.GetHeaderHash()) wrk.mutReceivedProofHandler.RLock() for _, handler := range wrk.receivedProofHandlers { From 2f56ebd13e788a8432103bc596924775d10b3190 Mon Sep 17 00:00:00 2001 From: ssd04 Date: Wed, 18 Dec 2024 15:39:29 +0200 Subject: [PATCH 3/5] remove debug logs --- consensus/spos/bls/v2/subroundEndRound.go | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/consensus/spos/bls/v2/subroundEndRound.go b/consensus/spos/bls/v2/subroundEndRound.go index 30194e3970..117b861bed 100644 --- a/consensus/spos/bls/v2/subroundEndRound.go +++ b/consensus/spos/bls/v2/subroundEndRound.go @@ -101,8 +101,6 @@ func (sr *subroundEndRound) receivedProof(proof consensus.ProofHandler) { sr.mutProcessingEndRound.Lock() defer sr.mutProcessingEndRound.Unlock() - log.Error("receivedProof: PROOF", "proofHeader", proof.GetHeaderHash()) - if sr.IsJobDone(sr.SelfPubKey(), sr.Current()) { return } @@ -276,7 +274,6 @@ func (sr *subroundEndRound) doEndRoundJobByNode() bool { sr.worker.DisplayStatistics() log.Debug("step 3: Body and Header have been committed") - log.Trace("step 3: Body and Header have been committed", "headerHash", sr.GetData()) msg := fmt.Sprintf("Added proposed block with nonce %d in blockchain", sr.GetHeader().GetNonce()) log.Debug(display.Headline(msg, sr.SyncTimer().FormattedCurrentTime(), "+")) @@ -288,7 +285,6 @@ func (sr *subroundEndRound) doEndRoundJobByNode() bool { func (sr *subroundEndRound) sendProof() (data.HeaderProofHandler, bool) { if !sr.shouldSendProof() { - log.Trace("sendProof: should not send proof") return nil, true } @@ -322,7 +318,8 @@ func (sr *subroundEndRound) sendProof() (data.HeaderProofHandler, bool) { } // broadcast header proof - return sr.createAndBroadcastProof(sig, bitmap) + proof, err := sr.createAndBroadcastProof(sig, bitmap) + return proof, err == nil } func (sr *subroundEndRound) shouldSendProof() bool { @@ -530,7 +527,7 @@ func (sr *subroundEndRound) computeAggSigOnValidNodes() ([]byte, []byte, error) return bitmap, sig, nil } -func (sr *subroundEndRound) createAndBroadcastProof(signature []byte, bitmap []byte) (*block.HeaderProof, bool) { +func (sr *subroundEndRound) createAndBroadcastProof(signature []byte, bitmap []byte) (*block.HeaderProof, error) { headerProof := &block.HeaderProof{ PubKeysBitmap: bitmap, AggregatedSignature: signature, @@ -543,14 +540,14 @@ func (sr *subroundEndRound) createAndBroadcastProof(signature []byte, bitmap []b err := sr.BroadcastMessenger().BroadcastEquivalentProof(headerProof, []byte(sr.SelfPubKey())) if err != nil { log.Error("failed to broadcast equivalent proof", "error", err) - return nil, false + return nil, err } log.Debug("step 3: block header proof has been sent", "PubKeysBitmap", bitmap, "AggregateSignature", signature) - return headerProof, true + return headerProof, nil } func (sr *subroundEndRound) createAndBroadcastInvalidSigners(invalidSigners []byte) { @@ -722,7 +719,7 @@ func (sr *subroundEndRound) waitForSignalSync() bool { case <-timerBetweenStatusChecks.C: if sr.IsSubroundFinished(sr.Current()) { log.Trace("subround already finished", "subround", sr.Name()) - return true + return false } if sr.checkReceivedSignatures() { @@ -840,8 +837,6 @@ func (sr *subroundEndRound) checkReceivedSignatures() bool { isSelfJobDone := sr.IsSelfJobDone(bls.SrSignature) - // log.Error("checkReceivedSignatures", "isSelfJobDone", isSelfJobDone, "numSigs", numSigs, "consensusSize", sr.ConsensusGroupSize()) - shouldStopWaitingSignatures := isSelfJobDone && isSignatureCollectionDone if shouldStopWaitingSignatures { log.Debug("step 2: signatures collection done", @@ -870,12 +865,6 @@ func (sr *subroundEndRound) getNumOfSignaturesCollected() int { continue } - // log.Error("getNumOfSignaturesCollected.JobDone", - // "node", node, - // "isSignJobDone", isSignJobDone, - // "subround", sr.Name(), - // ) - if isSignJobDone { n++ } From f9527f6336868b9836e485694f942fdbbdf1b53d Mon Sep 17 00:00:00 2001 From: ssd04 Date: Wed, 18 Dec 2024 15:41:16 +0200 Subject: [PATCH 4/5] return true if subround already finished --- consensus/spos/bls/v2/subroundEndRound.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/consensus/spos/bls/v2/subroundEndRound.go b/consensus/spos/bls/v2/subroundEndRound.go index 117b861bed..4284eeab06 100644 --- a/consensus/spos/bls/v2/subroundEndRound.go +++ b/consensus/spos/bls/v2/subroundEndRound.go @@ -305,7 +305,6 @@ func (sr *subroundEndRound) sendProof() (data.HeaderProofHandler, bool) { ok := sr.ScheduledProcessor().IsProcessedOKWithTimeout() // placeholder for subroundEndRound.doEndRoundJobByLeader script if !ok { - log.Error("sendProof: sheduled processor timeout") return nil, false } @@ -539,7 +538,6 @@ func (sr *subroundEndRound) createAndBroadcastProof(signature []byte, bitmap []b err := sr.BroadcastMessenger().BroadcastEquivalentProof(headerProof, []byte(sr.SelfPubKey())) if err != nil { - log.Error("failed to broadcast equivalent proof", "error", err) return nil, err } @@ -719,7 +717,7 @@ func (sr *subroundEndRound) waitForSignalSync() bool { case <-timerBetweenStatusChecks.C: if sr.IsSubroundFinished(sr.Current()) { log.Trace("subround already finished", "subround", sr.Name()) - return false + return true } if sr.checkReceivedSignatures() { From 5d2773fdce60bfefff08126a90c884776bf51793 Mon Sep 17 00:00:00 2001 From: ssd04 Date: Wed, 18 Dec 2024 16:30:00 +0200 Subject: [PATCH 5/5] remove extra empty line --- consensus/spos/bls/v2/subroundEndRound.go | 1 - 1 file changed, 1 deletion(-) diff --git a/consensus/spos/bls/v2/subroundEndRound.go b/consensus/spos/bls/v2/subroundEndRound.go index 4284eeab06..b5e6440685 100644 --- a/consensus/spos/bls/v2/subroundEndRound.go +++ b/consensus/spos/bls/v2/subroundEndRound.go @@ -250,7 +250,6 @@ func (sr *subroundEndRound) doEndRoundJobByNode() bool { } proof, ok := sr.sendProof() - if !ok { return false }