From 07d40a0038878b3008f31c3c87c72d654c1f895a Mon Sep 17 00:00:00 2001 From: Banana-J Date: Mon, 4 Mar 2024 22:05:19 +1100 Subject: [PATCH] fix: add lock for haserror variable in timeout.go (#443) Co-authored-by: wjrjerome --- consensus/XDPoS/engines/engine_v2/timeout.go | 24 ++++++++++++-------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/consensus/XDPoS/engines/engine_v2/timeout.go b/consensus/XDPoS/engines/engine_v2/timeout.go index d03f2d656a..4331ba3ecd 100644 --- a/consensus/XDPoS/engines/engine_v2/timeout.go +++ b/consensus/XDPoS/engines/engine_v2/timeout.go @@ -123,6 +123,8 @@ func (x *XDPoS_v2) verifyTC(chain consensus.ChainReader, timeoutCert *types.Time var wg sync.WaitGroup wg.Add(len(signatures)) + + var mutex sync.Mutex var haveError error signedTimeoutObj := types.TimeoutSigHash(&types.TimeoutForSign{ @@ -134,15 +136,19 @@ func (x *XDPoS_v2) verifyTC(chain consensus.ChainReader, timeoutCert *types.Time go func(sig types.Signature) { defer wg.Done() verified, _, err := x.verifyMsgSignature(signedTimeoutObj, sig, snap.NextEpochMasterNodes) - if err != nil { - log.Error("[verifyTC] Error while verfying TC message signatures", "timeoutCert.Round", timeoutCert.Round, "timeoutCert.GapNumber", timeoutCert.GapNumber, "Signatures len", len(signatures), "Error", err) - haveError = fmt.Errorf("error while verfying TC message signatures, %s", err) - return - } - if !verified { - log.Warn("[verifyTC] Signature not verified doing TC verification", "timeoutCert.Round", timeoutCert.Round, "timeoutCert.GapNumber", timeoutCert.GapNumber, "Signatures len", len(signatures)) - haveError = fmt.Errorf("fail to verify TC due to signature mis-match") - return + if err != nil || !verified { + log.Error("[verifyTC] Error or verification failure", "Signature", sig, "Error", err) + mutex.Lock() // Lock before accessing haveError + if haveError == nil { + if err != nil { + log.Error("[verifyTC] Error while verfying TC message signatures", "timeoutCert.Round", timeoutCert.Round, "timeoutCert.GapNumber", timeoutCert.GapNumber, "Signatures len", len(signatures), "Error", err) + haveError = fmt.Errorf("error while verifying TC message signatures, %s", err) + } else { + log.Warn("[verifyTC] Signature not verified doing TC verification", "timeoutCert.Round", timeoutCert.Round, "timeoutCert.GapNumber", timeoutCert.GapNumber, "Signatures len", len(signatures)) + haveError = fmt.Errorf("fail to verify TC due to signature mis-match") + } + } + mutex.Unlock() // Unlock after modifying haveError } }(signature) }