|
|
@ -9,10 +9,23 @@ import ( |
|
|
|
"encoding/binary" |
|
|
|
"encoding/binary" |
|
|
|
"errors" |
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
|
|
|
|
"harmony-benchmark/blockchain" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
var mutex = &sync.Mutex{} |
|
|
|
var mutex = &sync.Mutex{} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (consensus *Consensus) WaitForNewBlock(blockChannel chan blockchain.Block) { |
|
|
|
|
|
|
|
for { // keep waiting for new blocks
|
|
|
|
|
|
|
|
newBlock := <- blockChannel |
|
|
|
|
|
|
|
log.Println("got block.....") |
|
|
|
|
|
|
|
// TODO: think about potential race condition
|
|
|
|
|
|
|
|
if consensus.state == READY { |
|
|
|
|
|
|
|
consensus.startConsensus(&newBlock) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Leader's consensus message dispatcher
|
|
|
|
// Leader's consensus message dispatcher
|
|
|
|
func (consensus *Consensus) ProcessMessageLeader(message []byte) { |
|
|
|
func (consensus *Consensus) ProcessMessageLeader(message []byte) { |
|
|
|
msgType, err := GetConsensusMessageType(message) |
|
|
|
msgType, err := GetConsensusMessageType(message) |
|
|
@ -25,7 +38,6 @@ func (consensus *Consensus) ProcessMessageLeader(message []byte) { |
|
|
|
log.Print(err) |
|
|
|
log.Print(err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
msg := string(payload) |
|
|
|
|
|
|
|
log.Printf("[Leader] Received and processing message: %s\n", msgType) |
|
|
|
log.Printf("[Leader] Received and processing message: %s\n", msgType) |
|
|
|
switch msgType { |
|
|
|
switch msgType { |
|
|
|
case ANNOUNCE: |
|
|
|
case ANNOUNCE: |
|
|
@ -37,18 +49,22 @@ func (consensus *Consensus) ProcessMessageLeader(message []byte) { |
|
|
|
case RESPONSE: |
|
|
|
case RESPONSE: |
|
|
|
consensus.processResponseMessage(payload) |
|
|
|
consensus.processResponseMessage(payload) |
|
|
|
case START_CONSENSUS: |
|
|
|
case START_CONSENSUS: |
|
|
|
consensus.processStartConsensusMessage(msg) |
|
|
|
consensus.processStartConsensusMessage(payload) |
|
|
|
default: |
|
|
|
default: |
|
|
|
log.Println("Unexpected message type: %s", msgType) |
|
|
|
log.Println("Unexpected message type: %s", msgType) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Handler for message which triggers consensus process
|
|
|
|
// Handler for message which triggers consensus process
|
|
|
|
func (consensus *Consensus) processStartConsensusMessage(msg string) { |
|
|
|
func (consensus *Consensus) processStartConsensusMessage(payload []byte) { |
|
|
|
|
|
|
|
consensus.startConsensus(blockchain.NewGenesisBlock(blockchain.NewCoinbaseTX("x", "y"))) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (consensus *Consensus) startConsensus(newBlock *blockchain.Block) { |
|
|
|
// prepare message and broadcast to validators
|
|
|
|
// prepare message and broadcast to validators
|
|
|
|
// Construct new block
|
|
|
|
// Construct new block
|
|
|
|
newBlock := constructNewBlock() |
|
|
|
//newBlock := constructNewBlock()
|
|
|
|
consensus.blockHash = getBlockHash(newBlock) |
|
|
|
consensus.blockHash = newBlock.Hash |
|
|
|
|
|
|
|
|
|
|
|
msgToSend, err := consensus.constructAnnounceMessage() |
|
|
|
msgToSend, err := consensus.constructAnnounceMessage() |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
@ -273,6 +289,7 @@ func (consensus *Consensus) processResponseMessage(payload []byte) { |
|
|
|
// TODO: do followups on the consensus
|
|
|
|
// TODO: do followups on the consensus
|
|
|
|
log.Printf("HOORAY!!! CONSENSUS REACHED AMONG %d NODES!!!\n", len(consensus.validators)) |
|
|
|
log.Printf("HOORAY!!! CONSENSUS REACHED AMONG %d NODES!!!\n", len(consensus.validators)) |
|
|
|
consensus.ResetState() |
|
|
|
consensus.ResetState() |
|
|
|
|
|
|
|
consensus.ReadySignal <- 1 |
|
|
|
} |
|
|
|
} |
|
|
|
// TODO: composes new block and broadcast the new block to validators
|
|
|
|
// TODO: composes new block and broadcast the new block to validators
|
|
|
|
} |
|
|
|
} |
|
|
|