fix: add lock for haserror variable in timeout.go (#443)

Co-authored-by: wjrjerome <wjrjerome@babylonchain.io>
This commit is contained in:
Banana-J 2024-03-04 22:05:19 +11:00 committed by GitHub
parent 38a192e672
commit 07d40a0038
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -123,6 +123,8 @@ func (x *XDPoS_v2) verifyTC(chain consensus.ChainReader, timeoutCert *types.Time
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(len(signatures)) wg.Add(len(signatures))
var mutex sync.Mutex
var haveError error var haveError error
signedTimeoutObj := types.TimeoutSigHash(&types.TimeoutForSign{ 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) { go func(sig types.Signature) {
defer wg.Done() defer wg.Done()
verified, _, err := x.verifyMsgSignature(signedTimeoutObj, sig, snap.NextEpochMasterNodes) verified, _, err := x.verifyMsgSignature(signedTimeoutObj, sig, snap.NextEpochMasterNodes)
if err != nil { if err != nil || !verified {
log.Error("[verifyTC] Error while verfying TC message signatures", "timeoutCert.Round", timeoutCert.Round, "timeoutCert.GapNumber", timeoutCert.GapNumber, "Signatures len", len(signatures), "Error", err) log.Error("[verifyTC] Error or verification failure", "Signature", sig, "Error", err)
haveError = fmt.Errorf("error while verfying TC message signatures, %s", err) mutex.Lock() // Lock before accessing haveError
return if haveError == nil {
} if err != nil {
if !verified { log.Error("[verifyTC] Error while verfying TC message signatures", "timeoutCert.Round", timeoutCert.Round, "timeoutCert.GapNumber", timeoutCert.GapNumber, "Signatures len", len(signatures), "Error", err)
log.Warn("[verifyTC] Signature not verified doing TC verification", "timeoutCert.Round", timeoutCert.Round, "timeoutCert.GapNumber", timeoutCert.GapNumber, "Signatures len", len(signatures)) haveError = fmt.Errorf("error while verifying TC message signatures, %s", err)
haveError = fmt.Errorf("fail to verify TC due to signature mis-match") } else {
return 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) }(signature)
} }