2020-08-05 17:11:14 +00:00
|
|
|
package consensus
|
2020-10-21 19:54:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/Secured-Finance/dione/models"
|
|
|
|
"github.com/Secured-Finance/dione/pb"
|
2020-10-21 20:36:05 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-10-21 19:54:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ConsensusState int
|
|
|
|
|
|
|
|
const (
|
|
|
|
consensusPrePrepared ConsensusState = 0x0
|
|
|
|
consensusPrepared ConsensusState = 0x1
|
|
|
|
consensusCommitted ConsensusState = 0x2
|
|
|
|
|
|
|
|
testValidData = "test"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PBFTConsensusManager struct {
|
|
|
|
psb *pb.PubSubRouter
|
|
|
|
Consensuses map[string]*ConsensusData
|
|
|
|
maxFaultNodes int
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConsensusData struct {
|
2020-11-12 14:18:30 +00:00
|
|
|
preparedCount int
|
|
|
|
commitCount int
|
|
|
|
State ConsensusState
|
|
|
|
mutex sync.Mutex
|
|
|
|
result string
|
|
|
|
test bool
|
2020-11-04 18:17:01 +00:00
|
|
|
onConsensusFinishCallback func(finalData string)
|
2020-10-21 19:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewPBFTConsensusManager(psb *pb.PubSubRouter, maxFaultNodes int) *PBFTConsensusManager {
|
|
|
|
pcm := &PBFTConsensusManager{}
|
|
|
|
pcm.Consensuses = make(map[string]*ConsensusData)
|
|
|
|
pcm.psb = psb
|
|
|
|
pcm.psb.Hook("prepared", pcm.handlePreparedMessage)
|
|
|
|
pcm.psb.Hook("commit", pcm.handleCommitMessage)
|
|
|
|
return pcm
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:17:01 +00:00
|
|
|
func (pcm *PBFTConsensusManager) NewTestConsensus(data string, consensusID string, onConsensusFinishCallback func(finalData string)) {
|
2020-10-21 22:05:12 +00:00
|
|
|
//consensusID := uuid.New().String()
|
2020-10-21 19:54:40 +00:00
|
|
|
cData := &ConsensusData{}
|
|
|
|
cData.test = true
|
2020-11-04 18:17:01 +00:00
|
|
|
cData.onConsensusFinishCallback = onConsensusFinishCallback
|
2020-10-21 19:54:40 +00:00
|
|
|
pcm.Consensuses[consensusID] = cData
|
|
|
|
|
2020-11-12 14:18:30 +00:00
|
|
|
// here we will create DioneTask
|
|
|
|
|
2020-10-21 19:54:40 +00:00
|
|
|
msg := models.Message{}
|
|
|
|
msg.Type = "prepared"
|
|
|
|
msg.Payload = make(map[string]interface{})
|
|
|
|
msg.Payload["consensusID"] = consensusID
|
|
|
|
msg.Payload["data"] = data
|
|
|
|
pcm.psb.BroadcastToServiceTopic(&msg)
|
|
|
|
|
|
|
|
cData.State = consensusPrePrepared
|
2020-10-21 20:36:05 +00:00
|
|
|
logrus.Debug("started new consensus: " + consensusID)
|
2020-10-21 19:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pcm *PBFTConsensusManager) handlePreparedMessage(message *models.Message) {
|
|
|
|
// TODO add check on view of the message
|
|
|
|
consensusID := message.Payload["consensusID"].(string)
|
|
|
|
if _, ok := pcm.Consensuses[consensusID]; !ok {
|
2020-10-21 20:36:05 +00:00
|
|
|
logrus.Warn("Unknown consensus ID: " + consensusID)
|
2020-10-21 19:54:40 +00:00
|
|
|
return
|
|
|
|
}
|
2020-10-21 22:05:12 +00:00
|
|
|
logrus.Debug("received prepared msg")
|
2020-10-21 19:54:40 +00:00
|
|
|
data := pcm.Consensuses[consensusID]
|
|
|
|
|
2020-11-12 14:18:30 +00:00
|
|
|
//// validate payload data
|
|
|
|
//if data.test {
|
|
|
|
// rData := message.Payload["data"].(string)
|
|
|
|
// if rData != testValidData {
|
|
|
|
// logrus.Error("Incorrect data was received! Ignoring this message, because it was sent from fault node!")
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
//} else {
|
|
|
|
// // TODO
|
|
|
|
//}
|
|
|
|
|
|
|
|
// here we can validate miner which produced this task, is he winner, and so on
|
|
|
|
// we must to reconstruct transaction here for validating task itself
|
2020-10-21 19:54:40 +00:00
|
|
|
|
|
|
|
data.mutex.Lock()
|
|
|
|
data.preparedCount++
|
|
|
|
data.mutex.Unlock()
|
|
|
|
|
|
|
|
if data.preparedCount > 2*pcm.maxFaultNodes+1 {
|
|
|
|
msg := models.Message{}
|
2020-10-22 14:37:31 +00:00
|
|
|
msg.Payload = make(map[string]interface{})
|
2020-10-21 19:54:40 +00:00
|
|
|
msg.Type = "commit"
|
|
|
|
msg.Payload["consensusID"] = consensusID
|
2020-10-22 14:37:31 +00:00
|
|
|
msg.Payload["data"] = message.Payload["data"]
|
2020-10-21 19:54:40 +00:00
|
|
|
err := pcm.psb.BroadcastToServiceTopic(&msg)
|
|
|
|
if err != nil {
|
2020-10-21 20:36:05 +00:00
|
|
|
logrus.Warn("Unable to send COMMIT message: " + err.Error())
|
2020-10-21 19:54:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
data.State = consensusPrepared
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pcm *PBFTConsensusManager) handleCommitMessage(message *models.Message) {
|
|
|
|
// TODO add check on view of the message
|
2020-11-12 14:18:30 +00:00
|
|
|
// TODO add validation of data by hash to this stage
|
2020-10-21 19:54:40 +00:00
|
|
|
consensusID := message.Payload["consensusID"].(string)
|
|
|
|
if _, ok := pcm.Consensuses[consensusID]; !ok {
|
2020-10-21 20:36:05 +00:00
|
|
|
logrus.Warn("Unknown consensus ID: " + consensusID)
|
2020-10-21 19:54:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
data := pcm.Consensuses[consensusID]
|
2020-10-22 14:37:31 +00:00
|
|
|
|
2020-11-04 18:17:01 +00:00
|
|
|
data.mutex.Lock()
|
|
|
|
defer data.mutex.Unlock()
|
2020-10-22 14:37:31 +00:00
|
|
|
if data.State == consensusCommitted {
|
|
|
|
logrus.Debug("consensus already finished, dropping COMMIT message")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Debug("received commit msg")
|
|
|
|
|
2020-10-21 19:54:40 +00:00
|
|
|
data.commitCount++
|
|
|
|
|
|
|
|
if data.commitCount > 2*pcm.maxFaultNodes+1 {
|
2020-10-21 21:05:46 +00:00
|
|
|
data.State = consensusCommitted
|
2020-10-22 14:37:31 +00:00
|
|
|
data.result = message.Payload["data"].(string)
|
|
|
|
logrus.Debug("consensus successfully finished with result: " + data.result)
|
2020-11-04 18:17:01 +00:00
|
|
|
data.onConsensusFinishCallback(data.result)
|
2020-10-21 19:54:40 +00:00
|
|
|
}
|
|
|
|
}
|