2020-08-05 17:11:14 +00:00
|
|
|
package consensus
|
2020-10-21 19:54:40 +00:00
|
|
|
|
|
|
|
import (
|
2020-11-15 13:46:58 +00:00
|
|
|
"math/big"
|
2020-11-20 21:29:30 +00:00
|
|
|
"sync"
|
|
|
|
|
2020-12-02 13:42:02 +00:00
|
|
|
"github.com/Secured-Finance/dione/node"
|
|
|
|
|
|
|
|
oracleEmitter "github.com/Secured-Finance/dione/contracts/oracleemitter"
|
|
|
|
|
2020-11-18 19:53:52 +00:00
|
|
|
"github.com/Secured-Finance/dione/consensus/types"
|
|
|
|
|
2020-11-15 13:46:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2020-10-21 19:54:40 +00:00
|
|
|
|
2020-11-15 13:46:58 +00:00
|
|
|
"github.com/Secured-Finance/dione/ethclient"
|
|
|
|
"github.com/sirupsen/logrus"
|
2020-10-21 19:54:40 +00:00
|
|
|
|
2020-11-18 19:53:52 +00:00
|
|
|
"github.com/Secured-Finance/dione/pubsub"
|
2020-11-20 21:29:30 +00:00
|
|
|
types2 "github.com/Secured-Finance/dione/types"
|
2020-10-21 19:54:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type PBFTConsensusManager struct {
|
2020-11-20 21:29:30 +00:00
|
|
|
psb *pubsub.PubSubRouter
|
|
|
|
minApprovals int
|
|
|
|
privKey []byte
|
|
|
|
prePreparePool *PrePreparePool
|
|
|
|
preparePool *PreparePool
|
|
|
|
commitPool *CommitPool
|
|
|
|
consensusInfo map[string]*ConsensusData
|
|
|
|
ethereumClient *ethclient.EthereumClient
|
2020-11-27 16:16:08 +00:00
|
|
|
miner *Miner
|
2020-10-21 19:54:40 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 21:29:30 +00:00
|
|
|
type ConsensusData struct {
|
|
|
|
mutex sync.Mutex
|
|
|
|
alreadySubmitted bool
|
|
|
|
}
|
2020-10-21 19:54:40 +00:00
|
|
|
|
2020-12-02 13:42:02 +00:00
|
|
|
func NewPBFTConsensusManager(psb *pubsub.PubSubRouter, minApprovals int, privKey []byte, ethereumClient *ethclient.EthereumClient, miner *Miner, evc *node.EventLogCache) *PBFTConsensusManager {
|
2020-10-21 19:54:40 +00:00
|
|
|
pcm := &PBFTConsensusManager{}
|
|
|
|
pcm.psb = psb
|
2020-11-27 16:16:08 +00:00
|
|
|
pcm.miner = miner
|
2020-12-02 13:42:02 +00:00
|
|
|
pcm.prePreparePool = NewPrePreparePool(miner, evc)
|
2020-11-15 13:46:58 +00:00
|
|
|
pcm.preparePool = NewPreparePool()
|
|
|
|
pcm.commitPool = NewCommitPool()
|
|
|
|
pcm.minApprovals = minApprovals
|
|
|
|
pcm.privKey = privKey
|
|
|
|
pcm.ethereumClient = ethereumClient
|
2020-11-20 21:29:30 +00:00
|
|
|
pcm.consensusInfo = map[string]*ConsensusData{}
|
2020-11-18 19:53:52 +00:00
|
|
|
pcm.psb.Hook(types.MessageTypePrePrepare, pcm.handlePrePrepare)
|
|
|
|
pcm.psb.Hook(types.MessageTypePrepare, pcm.handlePrepare)
|
|
|
|
pcm.psb.Hook(types.MessageTypeCommit, pcm.handleCommit)
|
2020-10-21 19:54:40 +00:00
|
|
|
return pcm
|
|
|
|
}
|
|
|
|
|
2020-12-02 13:42:02 +00:00
|
|
|
func (pcm *PBFTConsensusManager) Propose(consensusID string, task types2.DioneTask, requestEvent *oracleEmitter.OracleEmitterNewOracleRequest) error {
|
2020-11-20 21:29:30 +00:00
|
|
|
pcm.consensusInfo[consensusID] = &ConsensusData{}
|
2020-12-02 13:42:02 +00:00
|
|
|
|
|
|
|
prePrepareMsg, err := pcm.prePreparePool.CreatePrePrepare(
|
|
|
|
consensusID,
|
|
|
|
task,
|
|
|
|
requestEvent.RequestID.String(),
|
|
|
|
requestEvent.CallbackAddress.Hex(),
|
|
|
|
string(requestEvent.CallbackMethodID[:]),
|
|
|
|
pcm.privKey,
|
|
|
|
)
|
2020-11-15 13:46:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pcm.psb.BroadcastToServiceTopic(prePrepareMsg)
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-12 14:18:30 +00:00
|
|
|
|
2020-11-18 19:53:52 +00:00
|
|
|
func (pcm *PBFTConsensusManager) handlePrePrepare(message *types.Message) {
|
2020-11-15 13:46:58 +00:00
|
|
|
if pcm.prePreparePool.IsExistingPrePrepare(message) {
|
|
|
|
logrus.Debug("received existing pre_prepare msg, dropping...")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !pcm.prePreparePool.IsValidPrePrepare(message) {
|
|
|
|
logrus.Debug("received invalid pre_prepare msg, dropping...")
|
|
|
|
return
|
|
|
|
}
|
2020-10-21 19:54:40 +00:00
|
|
|
|
2020-11-20 21:29:30 +00:00
|
|
|
pcm.prePreparePool.AddPrePrepare(message)
|
2020-11-27 16:16:08 +00:00
|
|
|
err := pcm.psb.BroadcastToServiceTopic(message)
|
2020-11-20 21:29:30 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-15 13:46:58 +00:00
|
|
|
prepareMsg, err := pcm.preparePool.CreatePrepare(message, pcm.privKey)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("failed to create prepare message: %w", err)
|
|
|
|
}
|
|
|
|
pcm.psb.BroadcastToServiceTopic(prepareMsg)
|
2020-10-21 19:54:40 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 19:53:52 +00:00
|
|
|
func (pcm *PBFTConsensusManager) handlePrepare(message *types.Message) {
|
2020-11-15 13:46:58 +00:00
|
|
|
if pcm.preparePool.IsExistingPrepare(message) {
|
|
|
|
logrus.Debug("received existing prepare msg, dropping...")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !pcm.preparePool.IsValidPrepare(message) {
|
|
|
|
logrus.Debug("received invalid prepare msg, dropping...")
|
2020-10-21 19:54:40 +00:00
|
|
|
return
|
|
|
|
}
|
2020-11-15 13:46:58 +00:00
|
|
|
|
|
|
|
pcm.preparePool.AddPrepare(message)
|
2020-11-27 16:16:08 +00:00
|
|
|
err := pcm.psb.BroadcastToServiceTopic(message)
|
2020-11-20 21:29:30 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2020-11-15 13:46:58 +00:00
|
|
|
|
|
|
|
if pcm.preparePool.PrepareSize(message.Payload.ConsensusID) >= pcm.minApprovals {
|
|
|
|
commitMsg, err := pcm.commitPool.CreateCommit(message, pcm.privKey)
|
2020-10-21 19:54:40 +00:00
|
|
|
if err != nil {
|
2020-11-15 13:46:58 +00:00
|
|
|
logrus.Errorf("failed to create commit message: %w", err)
|
2020-10-21 19:54:40 +00:00
|
|
|
}
|
2020-11-15 13:46:58 +00:00
|
|
|
pcm.psb.BroadcastToServiceTopic(commitMsg)
|
2020-10-21 19:54:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-18 19:53:52 +00:00
|
|
|
func (pcm *PBFTConsensusManager) handleCommit(message *types.Message) {
|
2020-11-15 13:46:58 +00:00
|
|
|
if pcm.commitPool.IsExistingCommit(message) {
|
|
|
|
logrus.Debug("received existing commit msg, dropping...")
|
2020-10-21 19:54:40 +00:00
|
|
|
return
|
|
|
|
}
|
2020-11-15 13:46:58 +00:00
|
|
|
if !pcm.commitPool.IsValidCommit(message) {
|
|
|
|
logrus.Debug("received invalid commit msg, dropping...")
|
2020-10-22 14:37:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-15 13:46:58 +00:00
|
|
|
pcm.commitPool.AddCommit(message)
|
2020-11-27 16:16:08 +00:00
|
|
|
err := pcm.psb.BroadcastToServiceTopic(message)
|
2020-11-20 21:29:30 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2020-10-22 14:37:31 +00:00
|
|
|
|
2020-11-15 13:46:58 +00:00
|
|
|
consensusMsg := message.Payload
|
|
|
|
if pcm.commitPool.CommitSize(consensusMsg.ConsensusID) >= pcm.minApprovals {
|
2020-11-20 21:29:30 +00:00
|
|
|
if info, ok := pcm.consensusInfo[consensusMsg.ConsensusID]; ok {
|
|
|
|
info.mutex.Lock()
|
|
|
|
defer info.mutex.Unlock()
|
|
|
|
if info.alreadySubmitted {
|
|
|
|
return
|
|
|
|
}
|
2020-11-15 13:46:58 +00:00
|
|
|
logrus.Infof("Submitting on-chain result for consensus ID: %s", consensusMsg.ConsensusID)
|
|
|
|
reqID, ok := new(big.Int).SetString(consensusMsg.RequestID, 10)
|
|
|
|
if !ok {
|
|
|
|
logrus.Errorf("Failed to parse big int: %v", consensusMsg.RequestID)
|
|
|
|
}
|
|
|
|
callbackAddress := common.HexToAddress(consensusMsg.CallbackAddress)
|
2020-11-27 16:16:08 +00:00
|
|
|
err := pcm.ethereumClient.SubmitRequestAnswer(reqID, string(consensusMsg.Task.Payload), callbackAddress)
|
2020-11-15 13:46:58 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Failed to submit on-chain result: %w", err)
|
|
|
|
}
|
2020-11-20 21:29:30 +00:00
|
|
|
info.alreadySubmitted = true
|
2020-11-15 13:46:58 +00:00
|
|
|
}
|
2020-10-21 19:54:40 +00:00
|
|
|
}
|
|
|
|
}
|