2020-08-05 17:11:14 +00:00
|
|
|
package consensus
|
2020-10-21 19:54:40 +00:00
|
|
|
|
|
|
|
import (
|
2021-07-15 20:51:22 +00:00
|
|
|
"encoding/hex"
|
2021-06-08 21:30:23 +00:00
|
|
|
"errors"
|
2021-07-19 22:40:49 +00:00
|
|
|
"fmt"
|
2020-11-15 13:46:58 +00:00
|
|
|
"math/big"
|
2020-11-20 21:29:30 +00:00
|
|
|
|
2021-07-21 21:56:58 +00:00
|
|
|
drand2 "github.com/Secured-Finance/dione/beacon/drand"
|
2021-07-19 20:19:06 +00:00
|
|
|
|
2021-07-21 21:56:58 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2021-07-11 23:23:00 +00:00
|
|
|
|
2021-07-10 22:07:35 +00:00
|
|
|
"github.com/fxamacker/cbor/v2"
|
|
|
|
|
2021-06-14 21:45:35 +00:00
|
|
|
"github.com/Secured-Finance/dione/cache"
|
|
|
|
|
2021-06-11 11:40:32 +00:00
|
|
|
"github.com/asaskevich/EventBus"
|
|
|
|
|
2021-06-08 21:30:23 +00:00
|
|
|
"github.com/Secured-Finance/dione/blockchain"
|
|
|
|
|
|
|
|
types3 "github.com/Secured-Finance/dione/blockchain/types"
|
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p-core/crypto"
|
|
|
|
|
|
|
|
"github.com/Secured-Finance/dione/blockchain/pool"
|
2020-12-02 13:42:02 +00:00
|
|
|
|
2020-11-18 19:53:52 +00:00
|
|
|
"github.com/Secured-Finance/dione/consensus/types"
|
2021-07-10 22:07:35 +00:00
|
|
|
types2 "github.com/Secured-Finance/dione/types"
|
2020-11-18 19:53:52 +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"
|
2021-06-08 21:30:23 +00:00
|
|
|
)
|
|
|
|
|
2021-07-11 00:32:58 +00:00
|
|
|
var (
|
|
|
|
ErrNoAcceptedBlocks = errors.New("there is no accepted blocks")
|
|
|
|
)
|
|
|
|
|
2020-10-21 19:54:40 +00:00
|
|
|
type PBFTConsensusManager struct {
|
2021-07-26 21:18:16 +00:00
|
|
|
bus EventBus.Bus
|
|
|
|
psb *pubsub.PubSubRouter
|
|
|
|
minApprovals int // FIXME
|
|
|
|
privKey crypto.PrivKey
|
|
|
|
msgLog *ConsensusMessageLog
|
|
|
|
validator *ConsensusValidator
|
|
|
|
ethereumClient *ethclient.EthereumClient
|
|
|
|
miner *blockchain.Miner
|
|
|
|
consensusRoundPool *ConsensusRoundPool
|
|
|
|
mempool *pool.Mempool
|
|
|
|
blockchain *blockchain.BlockChain
|
|
|
|
address peer.ID
|
|
|
|
stateChangeChannels map[string]map[State][]chan bool
|
2020-11-20 21:29:30 +00:00
|
|
|
}
|
2020-10-21 19:54:40 +00:00
|
|
|
|
2021-07-11 00:32:58 +00:00
|
|
|
func NewPBFTConsensusManager(
|
|
|
|
bus EventBus.Bus,
|
|
|
|
psb *pubsub.PubSubRouter,
|
|
|
|
minApprovals int,
|
|
|
|
privKey crypto.PrivKey,
|
|
|
|
ethereumClient *ethclient.EthereumClient,
|
2021-07-19 20:19:06 +00:00
|
|
|
miner *blockchain.Miner,
|
2021-07-11 00:32:58 +00:00
|
|
|
bc *blockchain.BlockChain,
|
2021-07-26 21:18:16 +00:00
|
|
|
bp *ConsensusRoundPool,
|
2021-07-21 21:56:58 +00:00
|
|
|
db *drand2.DrandBeacon,
|
2021-07-11 23:23:00 +00:00
|
|
|
mempool *pool.Mempool,
|
2021-07-19 20:19:06 +00:00
|
|
|
address peer.ID,
|
2021-07-11 00:32:58 +00:00
|
|
|
) *PBFTConsensusManager {
|
2021-07-19 20:19:06 +00:00
|
|
|
pcm := &PBFTConsensusManager{
|
2021-07-26 21:18:16 +00:00
|
|
|
psb: psb,
|
|
|
|
miner: miner,
|
|
|
|
validator: NewConsensusValidator(miner, bc, db),
|
|
|
|
msgLog: NewConsensusMessageLog(),
|
|
|
|
minApprovals: minApprovals,
|
|
|
|
privKey: privKey,
|
|
|
|
ethereumClient: ethereumClient,
|
|
|
|
bus: bus,
|
|
|
|
consensusRoundPool: bp,
|
|
|
|
mempool: mempool,
|
|
|
|
blockchain: bc,
|
|
|
|
address: address,
|
|
|
|
stateChangeChannels: map[string]map[State][]chan bool{},
|
2021-06-11 11:40:32 +00:00
|
|
|
}
|
2021-07-19 20:19:06 +00:00
|
|
|
|
2021-07-21 21:56:58 +00:00
|
|
|
return pcm
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pcm *PBFTConsensusManager) Run() {
|
2021-07-11 23:23:00 +00:00
|
|
|
pcm.psb.Hook(pubsub.PrePrepareMessageType, pcm.handlePrePrepare)
|
|
|
|
pcm.psb.Hook(pubsub.PrepareMessageType, pcm.handlePrepare)
|
|
|
|
pcm.psb.Hook(pubsub.CommitMessageType, pcm.handleCommit)
|
2021-07-21 21:56:58 +00:00
|
|
|
pcm.bus.SubscribeAsync("beacon:newEntry", func(entry types2.BeaconEntry) {
|
2021-07-15 20:51:22 +00:00
|
|
|
pcm.onNewBeaconEntry(entry)
|
|
|
|
}, true)
|
2020-10-21 19:54:40 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 21:30:23 +00:00
|
|
|
func (pcm *PBFTConsensusManager) propose(blk *types3.Block) error {
|
|
|
|
prePrepareMsg, err := NewMessage(types.ConsensusMessage{Block: blk}, types.ConsensusMessageTypePrePrepare, pcm.privKey)
|
2020-11-15 13:46:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pcm.psb.BroadcastToServiceTopic(prePrepareMsg)
|
2021-07-26 21:18:16 +00:00
|
|
|
pcm.consensusRoundPool.AddConsensusInfo(blk)
|
2021-07-19 22:40:49 +00:00
|
|
|
logrus.WithField("blockHash", fmt.Sprintf("%x", blk.Header.Hash)).Debugf("Entered into PREPREPARED state")
|
2020-11-15 13:46:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-11-12 14:18:30 +00:00
|
|
|
|
2021-07-11 23:23:00 +00:00
|
|
|
func (pcm *PBFTConsensusManager) handlePrePrepare(message *pubsub.PubSubMessage) {
|
|
|
|
var prePrepare types.PrePrepareMessage
|
|
|
|
err := cbor.Unmarshal(message.Payload, &prePrepare)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("failed to convert payload to PrePrepare message: %s", err.Error())
|
2021-06-02 21:19:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-19 20:19:06 +00:00
|
|
|
if *prePrepare.Block.Header.Proposer == pcm.address {
|
2021-03-15 20:39:52 +00:00
|
|
|
return
|
|
|
|
}
|
2021-06-08 21:30:23 +00:00
|
|
|
|
|
|
|
cmsg := types.ConsensusMessage{
|
2021-07-11 23:23:00 +00:00
|
|
|
Type: types.ConsensusMessageTypePrePrepare,
|
|
|
|
From: message.From,
|
|
|
|
Block: prePrepare.Block,
|
|
|
|
Blockhash: prePrepare.Block.Header.Hash,
|
2021-06-08 21:30:23 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 21:19:52 +00:00
|
|
|
if pcm.msgLog.Exists(cmsg) {
|
2021-07-11 23:23:00 +00:00
|
|
|
logrus.Tracef("received existing pre_prepare msg for block %x", cmsg.Block.Header.Hash)
|
2020-11-15 13:46:58 +00:00
|
|
|
return
|
|
|
|
}
|
2021-07-19 20:19:06 +00:00
|
|
|
if !pcm.validator.Valid(cmsg) {
|
2021-07-11 23:23:00 +00:00
|
|
|
logrus.Warnf("received invalid pre_prepare msg for block %x", cmsg.Block.Header.Hash)
|
2020-11-15 13:46:58 +00:00
|
|
|
return
|
|
|
|
}
|
2020-10-21 19:54:40 +00:00
|
|
|
|
2021-06-02 21:19:52 +00:00
|
|
|
pcm.msgLog.AddMessage(cmsg)
|
2021-07-19 22:40:49 +00:00
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"blockHash": fmt.Sprintf("%x", cmsg.Block.Header.Hash),
|
|
|
|
"from": message.From.String(),
|
|
|
|
}).Debug("Received PREPREPARE message")
|
2021-07-26 21:18:16 +00:00
|
|
|
pcm.consensusRoundPool.AddConsensusInfo(cmsg.Block)
|
|
|
|
|
|
|
|
encodedHash := hex.EncodeToString(cmsg.Blockhash)
|
|
|
|
if m, ok := pcm.stateChangeChannels[encodedHash]; ok {
|
|
|
|
if channels, ok := m[StateStatusPrePrepared]; ok {
|
|
|
|
for _, v := range channels {
|
|
|
|
v <- true
|
|
|
|
close(v)
|
|
|
|
delete(pcm.stateChangeChannels, encodedHash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-20 21:29:30 +00:00
|
|
|
|
2021-06-08 21:30:23 +00:00
|
|
|
prepareMsg, err := NewMessage(cmsg, types.ConsensusMessageTypePrepare, pcm.privKey)
|
2020-11-15 13:46:58 +00:00
|
|
|
if err != nil {
|
2021-04-30 20:09:55 +00:00
|
|
|
logrus.Errorf("failed to create prepare message: %v", err)
|
2021-06-03 21:15:32 +00:00
|
|
|
return
|
2020-11-15 13:46:58 +00:00
|
|
|
}
|
2021-03-15 20:39:52 +00:00
|
|
|
|
2021-07-19 22:40:49 +00:00
|
|
|
logrus.WithField("blockHash", fmt.Sprintf("%x", prePrepare.Block.Header.Hash)).Debugf("Entered into PREPREPARED state")
|
2021-06-08 21:30:23 +00:00
|
|
|
pcm.psb.BroadcastToServiceTopic(prepareMsg)
|
2020-10-21 19:54:40 +00:00
|
|
|
}
|
|
|
|
|
2021-07-11 23:23:00 +00:00
|
|
|
func (pcm *PBFTConsensusManager) handlePrepare(message *pubsub.PubSubMessage) {
|
|
|
|
var prepare types.PrepareMessage
|
|
|
|
err := cbor.Unmarshal(message.Payload, &prepare)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("failed to convert payload to Prepare message: %s", err.Error())
|
2021-06-08 21:30:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cmsg := types.ConsensusMessage{
|
|
|
|
Type: types.ConsensusMessageTypePrepare,
|
|
|
|
From: message.From,
|
|
|
|
Blockhash: prepare.Blockhash,
|
2021-06-14 21:45:35 +00:00
|
|
|
Signature: prepare.Signature,
|
2021-06-08 21:30:23 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 21:18:16 +00:00
|
|
|
if _, err := pcm.consensusRoundPool.GetConsensusInfo(cmsg.Blockhash); errors.Is(err, cache.ErrNotFound) {
|
|
|
|
encodedHash := hex.EncodeToString(cmsg.Blockhash)
|
|
|
|
logrus.WithField("blockHash", encodedHash).Warn("received PREPARE for unknown block")
|
|
|
|
waitingCh := make(chan bool)
|
|
|
|
if _, ok := pcm.stateChangeChannels[encodedHash]; !ok {
|
|
|
|
pcm.stateChangeChannels[encodedHash] = map[State][]chan bool{}
|
|
|
|
}
|
|
|
|
pcm.stateChangeChannels[encodedHash][StateStatusPrePrepared] = append(pcm.stateChangeChannels[encodedHash][StateStatusPrePrepared], waitingCh)
|
|
|
|
result := <-waitingCh
|
|
|
|
if !result {
|
|
|
|
return
|
|
|
|
}
|
2021-06-02 21:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if pcm.msgLog.Exists(cmsg) {
|
2021-07-11 23:23:00 +00:00
|
|
|
logrus.Tracef("received existing prepare msg for block %x", cmsg.Blockhash)
|
2020-11-15 13:46:58 +00:00
|
|
|
return
|
|
|
|
}
|
2021-06-14 21:45:35 +00:00
|
|
|
|
2021-07-19 20:19:06 +00:00
|
|
|
if !pcm.validator.Valid(cmsg) {
|
2021-07-11 23:23:00 +00:00
|
|
|
logrus.Warnf("received invalid prepare msg for block %x", cmsg.Blockhash)
|
2020-10-21 19:54:40 +00:00
|
|
|
return
|
|
|
|
}
|
2020-11-15 13:46:58 +00:00
|
|
|
|
2021-06-02 21:19:52 +00:00
|
|
|
pcm.msgLog.AddMessage(cmsg)
|
2021-07-19 22:40:49 +00:00
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"blockHash": fmt.Sprintf("%x", cmsg.Blockhash),
|
|
|
|
"from": message.From.String(),
|
|
|
|
}).Debug("Received PREPARE message")
|
2020-11-15 13:46:58 +00:00
|
|
|
|
2021-07-19 22:40:49 +00:00
|
|
|
if len(pcm.msgLog.Get(types.ConsensusMessageTypePrepare, cmsg.Blockhash)) >= pcm.minApprovals-1 {
|
2021-06-08 21:30:23 +00:00
|
|
|
commitMsg, err := NewMessage(cmsg, types.ConsensusMessageTypeCommit, pcm.privKey)
|
2020-10-21 19:54:40 +00:00
|
|
|
if err != nil {
|
2021-06-08 21:30:23 +00:00
|
|
|
logrus.Errorf("failed to create commit message: %v", err)
|
2021-07-11 23:23:00 +00:00
|
|
|
return
|
2020-10-21 19:54:40 +00:00
|
|
|
}
|
2021-06-08 21:30:23 +00:00
|
|
|
pcm.psb.BroadcastToServiceTopic(commitMsg)
|
2021-07-19 22:40:49 +00:00
|
|
|
logrus.WithField("blockHash", fmt.Sprintf("%x", cmsg.Blockhash)).Debugf("Entered into PREPARED state")
|
2021-07-26 21:18:16 +00:00
|
|
|
pcm.consensusRoundPool.UpdateConsensusState(cmsg.Blockhash, StateStatusPrepared)
|
|
|
|
|
|
|
|
// pull watchers
|
|
|
|
encodedHash := hex.EncodeToString(cmsg.Blockhash)
|
|
|
|
if m, ok := pcm.stateChangeChannels[encodedHash]; ok {
|
|
|
|
if channels, ok := m[StateStatusPrepared]; ok {
|
|
|
|
for _, v := range channels {
|
|
|
|
v <- true
|
|
|
|
close(v)
|
|
|
|
delete(pcm.stateChangeChannels, encodedHash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-21 19:54:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-11 23:23:00 +00:00
|
|
|
func (pcm *PBFTConsensusManager) handleCommit(message *pubsub.PubSubMessage) {
|
|
|
|
var commit types.CommitMessage
|
|
|
|
err := cbor.Unmarshal(message.Payload, &commit)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("failed to convert payload to Commit message: %s", err.Error())
|
2021-06-08 21:30:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cmsg := types.ConsensusMessage{
|
|
|
|
Type: types.ConsensusMessageTypeCommit,
|
|
|
|
From: message.From,
|
|
|
|
Blockhash: commit.Blockhash,
|
2021-07-11 23:23:00 +00:00
|
|
|
Signature: commit.Signature,
|
2021-06-08 21:30:23 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 21:18:16 +00:00
|
|
|
ci, err := pcm.consensusRoundPool.GetConsensusInfo(cmsg.Blockhash)
|
|
|
|
|
|
|
|
if errors.Is(err, cache.ErrNotFound) {
|
|
|
|
logrus.WithField("blockHash", hex.EncodeToString(cmsg.Blockhash)).Warnf("received COMMIT for unknown block")
|
2021-06-02 21:19:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-26 21:18:16 +00:00
|
|
|
if ci.State < StateStatusPrepared {
|
|
|
|
encodedHash := hex.EncodeToString(cmsg.Blockhash)
|
|
|
|
logrus.WithField("blockHash", encodedHash).Warnf("incorrect state of block consensus")
|
|
|
|
waitingCh := make(chan bool)
|
|
|
|
if _, ok := pcm.stateChangeChannels[encodedHash]; !ok {
|
|
|
|
pcm.stateChangeChannels[encodedHash] = map[State][]chan bool{}
|
|
|
|
}
|
|
|
|
pcm.stateChangeChannels[encodedHash][StateStatusPrepared] = append(pcm.stateChangeChannels[encodedHash][StateStatusPrepared], waitingCh)
|
|
|
|
result := <-waitingCh
|
|
|
|
if !result {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 21:19:52 +00:00
|
|
|
if pcm.msgLog.Exists(cmsg) {
|
2021-07-11 23:23:00 +00:00
|
|
|
logrus.Tracef("received existing commit msg for block %x", cmsg.Blockhash)
|
2020-10-21 19:54:40 +00:00
|
|
|
return
|
|
|
|
}
|
2021-07-19 20:19:06 +00:00
|
|
|
if !pcm.validator.Valid(cmsg) {
|
2021-07-11 23:23:00 +00:00
|
|
|
logrus.Warnf("received invalid commit msg for block %x", cmsg.Blockhash)
|
2020-10-22 14:37:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-02 21:19:52 +00:00
|
|
|
pcm.msgLog.AddMessage(cmsg)
|
2020-10-22 14:37:31 +00:00
|
|
|
|
2021-07-19 22:40:49 +00:00
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"blockHash": fmt.Sprintf("%x", cmsg.Blockhash),
|
|
|
|
"from": message.From.String(),
|
|
|
|
}).Debug("Received COMMIT message")
|
|
|
|
|
2021-06-08 21:30:23 +00:00
|
|
|
if len(pcm.msgLog.Get(types.ConsensusMessageTypeCommit, cmsg.Blockhash)) >= pcm.minApprovals {
|
2021-07-19 22:40:49 +00:00
|
|
|
logrus.WithField("blockHash", fmt.Sprintf("%x", cmsg.Blockhash)).Debugf("Entered into COMMIT state")
|
2021-07-26 21:18:16 +00:00
|
|
|
pcm.consensusRoundPool.UpdateConsensusState(cmsg.Blockhash, StateStatusCommited)
|
2021-06-08 21:30:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 20:51:22 +00:00
|
|
|
func (pcm *PBFTConsensusManager) onNewBeaconEntry(entry types2.BeaconEntry) {
|
|
|
|
block, err := pcm.commitAcceptedBlocks()
|
2021-07-26 21:18:16 +00:00
|
|
|
height, _ := pcm.blockchain.GetLatestBlockHeight()
|
2021-07-15 20:51:22 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, ErrNoAcceptedBlocks) {
|
|
|
|
logrus.WithFields(logrus.Fields{
|
2021-07-26 21:18:16 +00:00
|
|
|
"height": height + 1,
|
2021-07-15 20:51:22 +00:00
|
|
|
}).Infof("No accepted blocks in the current consensus round")
|
|
|
|
} else {
|
2021-07-15 20:55:06 +00:00
|
|
|
logrus.WithFields(logrus.Fields{
|
2021-07-26 21:18:16 +00:00
|
|
|
"height": height + 1,
|
2021-07-15 20:55:06 +00:00
|
|
|
"err": err.Error(),
|
|
|
|
}).Errorf("Failed to select the block in the current consensus round")
|
2021-07-15 20:51:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if block != nil {
|
|
|
|
// broadcast new block
|
|
|
|
var newBlockMessage pubsub.PubSubMessage
|
|
|
|
newBlockMessage.Type = pubsub.NewBlockMessageType
|
|
|
|
blockSerialized, err := cbor.Marshal(block)
|
2021-06-08 21:30:23 +00:00
|
|
|
if err != nil {
|
2021-07-15 20:51:22 +00:00
|
|
|
logrus.Errorf("Failed to serialize block %x for broadcasting!", block.Header.Hash)
|
|
|
|
} else {
|
|
|
|
newBlockMessage.Payload = blockSerialized
|
|
|
|
pcm.psb.BroadcastToServiceTopic(&newBlockMessage)
|
2021-03-15 20:39:52 +00:00
|
|
|
}
|
2021-07-10 22:07:35 +00:00
|
|
|
|
2021-07-15 20:51:22 +00:00
|
|
|
// if we are miner of this block
|
|
|
|
// then post dione tasks to target chains (currently, only Ethereum)
|
2021-07-19 20:19:06 +00:00
|
|
|
if block.Header.Proposer.String() == pcm.address.String() {
|
|
|
|
pcm.submitTasksFromBlock(block)
|
2021-07-11 23:23:00 +00:00
|
|
|
}
|
2021-07-15 20:51:22 +00:00
|
|
|
}
|
2021-03-15 20:39:52 +00:00
|
|
|
|
2021-07-26 21:18:16 +00:00
|
|
|
for k, v := range pcm.stateChangeChannels {
|
|
|
|
for k1, j := range v {
|
|
|
|
for _, ch := range j {
|
|
|
|
ch <- true
|
|
|
|
close(ch)
|
|
|
|
}
|
|
|
|
delete(v, k1)
|
|
|
|
}
|
|
|
|
delete(pcm.stateChangeChannels, k)
|
2021-07-15 20:51:22 +00:00
|
|
|
}
|
2021-07-11 23:23:00 +00:00
|
|
|
|
2021-07-26 21:18:16 +00:00
|
|
|
minedBlock, err := pcm.miner.MineBlock(entry.Data, entry.Round)
|
2021-07-15 20:51:22 +00:00
|
|
|
if err != nil {
|
2021-07-19 20:19:06 +00:00
|
|
|
if errors.Is(err, blockchain.ErrNoTxForBlock) {
|
2021-07-15 20:51:22 +00:00
|
|
|
logrus.Info("Sealing skipped, no transactions in mempool")
|
|
|
|
} else {
|
|
|
|
logrus.Errorf("Failed to mine the block: %s", err.Error())
|
2021-07-11 23:23:00 +00:00
|
|
|
}
|
2021-07-15 20:51:22 +00:00
|
|
|
return
|
|
|
|
}
|
2021-03-15 20:39:52 +00:00
|
|
|
|
2021-07-15 20:51:22 +00:00
|
|
|
// if we are round winner
|
|
|
|
if minedBlock != nil {
|
|
|
|
err = pcm.propose(minedBlock)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Failed to propose the block: %s", err.Error())
|
|
|
|
return
|
2021-04-15 21:01:27 +00:00
|
|
|
}
|
2021-07-15 20:51:22 +00:00
|
|
|
}
|
2021-03-15 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 20:19:06 +00:00
|
|
|
func (pcm *PBFTConsensusManager) submitTasksFromBlock(block *types3.Block) {
|
|
|
|
for _, tx := range block.Data {
|
|
|
|
var task types2.DioneTask
|
|
|
|
err := cbor.Unmarshal(tx.Data, &task)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"err": err.Error(),
|
|
|
|
"txHash": hex.EncodeToString(tx.Hash),
|
|
|
|
}).Error("Failed to unmarshal transaction payload")
|
|
|
|
continue // FIXME
|
|
|
|
}
|
|
|
|
reqIDNumber, ok := big.NewInt(0).SetString(task.RequestID, 10)
|
|
|
|
if !ok {
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"txHash": hex.EncodeToString(tx.Hash),
|
|
|
|
}).Error("Failed to parse request id number in Dione task")
|
|
|
|
continue // FIXME
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pcm.ethereumClient.SubmitRequestAnswer(reqIDNumber, task.Payload)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"err": err.Error(),
|
|
|
|
"txHash": hex.EncodeToString(tx.Hash),
|
|
|
|
"reqID": reqIDNumber.String(),
|
|
|
|
}).Error("Failed to submit task to ETH chain")
|
|
|
|
continue // FIXME
|
|
|
|
}
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"txHash": hex.EncodeToString(tx.Hash),
|
|
|
|
"reqID": reqIDNumber.String(),
|
|
|
|
}).Debug("Dione task has been sucessfully submitted to ETH chain (DioneOracle contract)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 21:30:23 +00:00
|
|
|
func (pcm *PBFTConsensusManager) commitAcceptedBlocks() (*types3.Block, error) {
|
2021-07-26 21:18:16 +00:00
|
|
|
blocks := pcm.consensusRoundPool.GetAllBlocksWithCommit()
|
2021-06-08 21:30:23 +00:00
|
|
|
if blocks == nil {
|
2021-07-11 00:32:58 +00:00
|
|
|
return nil, ErrNoAcceptedBlocks
|
2021-03-15 20:39:52 +00:00
|
|
|
}
|
2021-06-08 21:30:23 +00:00
|
|
|
var maxStake *big.Int
|
2021-07-26 21:18:16 +00:00
|
|
|
var maxWinCount int64 = -1
|
2021-06-08 21:30:23 +00:00
|
|
|
var selectedBlock *types3.Block
|
|
|
|
for _, v := range blocks {
|
2021-07-26 21:18:16 +00:00
|
|
|
stake, err := pcm.ethereumClient.GetMinerStake(v.Block.Header.ProposerEth)
|
2021-06-08 21:30:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-06-02 21:19:52 +00:00
|
|
|
}
|
2021-06-08 21:30:23 +00:00
|
|
|
|
2021-07-26 21:18:16 +00:00
|
|
|
if maxStake != nil && maxWinCount != -1 {
|
|
|
|
if stake.Cmp(maxStake) == -1 || v.Block.Header.ElectionProof.WinCount < maxWinCount {
|
2021-06-08 21:30:23 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-06-02 21:19:52 +00:00
|
|
|
}
|
2021-06-08 21:30:23 +00:00
|
|
|
maxStake = stake
|
2021-07-26 21:18:16 +00:00
|
|
|
maxWinCount = v.Block.Header.ElectionProof.WinCount
|
|
|
|
selectedBlock = v.Block
|
2021-06-02 21:19:52 +00:00
|
|
|
}
|
2021-07-15 20:51:22 +00:00
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"hash": hex.EncodeToString(selectedBlock.Header.Hash),
|
|
|
|
"height": selectedBlock.Header.Height,
|
|
|
|
"miner": selectedBlock.Header.Proposer.String(),
|
|
|
|
}).Info("Committed new block")
|
2021-07-26 21:18:16 +00:00
|
|
|
pcm.consensusRoundPool.Prune()
|
2021-07-11 23:23:00 +00:00
|
|
|
for _, v := range selectedBlock.Data {
|
2021-07-15 20:51:22 +00:00
|
|
|
err := pcm.mempool.DeleteTx(v.Hash)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"err": err.Error(),
|
|
|
|
"tx": hex.EncodeToString(v.Hash),
|
|
|
|
}).Errorf("Failed to delete committed tx from mempool")
|
|
|
|
continue
|
|
|
|
}
|
2021-07-11 23:23:00 +00:00
|
|
|
}
|
2021-06-08 21:30:23 +00:00
|
|
|
return selectedBlock, pcm.blockchain.StoreBlock(selectedBlock)
|
2021-06-02 21:19:52 +00:00
|
|
|
}
|