Add saving consensus result and make dropping commit messages which related to finished consensus

This commit is contained in:
ChronosX88 2020-10-22 18:37:31 +04:00
parent d717318e71
commit 97a4e7a7ad
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A

View File

@ -29,6 +29,7 @@ type ConsensusData struct {
commitCount int commitCount int
State ConsensusState State ConsensusState
mutex sync.Mutex mutex sync.Mutex
result string
test bool test bool
} }
@ -85,8 +86,10 @@ func (pcm *PBFTConsensusManager) handlePreparedMessage(message *models.Message)
if data.preparedCount > 2*pcm.maxFaultNodes+1 { if data.preparedCount > 2*pcm.maxFaultNodes+1 {
msg := models.Message{} msg := models.Message{}
msg.Payload = make(map[string]interface{})
msg.Type = "commit" msg.Type = "commit"
msg.Payload["consensusID"] = consensusID msg.Payload["consensusID"] = consensusID
msg.Payload["data"] = message.Payload["data"]
err := pcm.psb.BroadcastToServiceTopic(&msg) err := pcm.psb.BroadcastToServiceTopic(&msg)
if err != nil { if err != nil {
logrus.Warn("Unable to send COMMIT message: " + err.Error()) logrus.Warn("Unable to send COMMIT message: " + err.Error())
@ -98,18 +101,28 @@ func (pcm *PBFTConsensusManager) handlePreparedMessage(message *models.Message)
func (pcm *PBFTConsensusManager) handleCommitMessage(message *models.Message) { func (pcm *PBFTConsensusManager) handleCommitMessage(message *models.Message) {
// TODO add check on view of the message // TODO add check on view of the message
// TODO add validation of data to this stage
consensusID := message.Payload["consensusID"].(string) consensusID := message.Payload["consensusID"].(string)
if _, ok := pcm.Consensuses[consensusID]; !ok { if _, ok := pcm.Consensuses[consensusID]; !ok {
logrus.Warn("Unknown consensus ID: " + consensusID) logrus.Warn("Unknown consensus ID: " + consensusID)
return return
} }
data := pcm.Consensuses[consensusID] data := pcm.Consensuses[consensusID]
if data.State == consensusCommitted {
logrus.Debug("consensus already finished, dropping COMMIT message")
return
}
logrus.Debug("received commit msg")
data.mutex.Lock() data.mutex.Lock()
data.commitCount++ data.commitCount++
data.mutex.Unlock() data.mutex.Unlock()
if data.commitCount > 2*pcm.maxFaultNodes+1 { if data.commitCount > 2*pcm.maxFaultNodes+1 {
logrus.Debug("consensus successfully finished")
data.State = consensusCommitted data.State = consensusCommitted
data.result = message.Payload["data"].(string)
logrus.Debug("consensus successfully finished with result: " + data.result)
} }
} }