WIP: adding message to stop running consensus

pull/3/head
alok 7 years ago
parent f2de27fa32
commit 855ed6d603
  1. 15
      aws-code/transaction_generator.go
  2. 24
      consensus/consensus_leader.go
  3. 7
      consensus/message.go

@ -1,12 +1,12 @@
package main package main
import ( import (
"harmony-benchmark/blockchain"
"math/rand"
"time"
"flag" "flag"
"harmony-benchmark/blockchain"
"harmony-benchmark/node" "harmony-benchmark/node"
"harmony-benchmark/p2p" "harmony-benchmark/p2p"
"math/rand"
"time"
) )
func newRandTransaction() blockchain.Transaction { func newRandTransaction() blockchain.Transaction {
@ -22,9 +22,14 @@ func main() {
ip := flag.String("ip", "127.0.0.1", "IP of the leader") ip := flag.String("ip", "127.0.0.1", "IP of the leader")
port := flag.String("port", "9000", "port of the leader.") port := flag.String("port", "9000", "port of the leader.")
start := time.Now()
totalTime = 30000
txs := make([]blockchain.Transaction, 10) txs := make([]blockchain.Transaction, 10)
for true { for true {
t = time.Now()
if t.Sub(start) >= totalTime {
break
}
for i := range txs { for i := range txs {
txs[i] = newRandTransaction() txs[i] = newRandTransaction()
@ -33,4 +38,6 @@ func main() {
p2p.SendMessage(p2p.Peer{*ip, *port, "n/a"}, msg) p2p.SendMessage(p2p.Peer{*ip, *port, "n/a"}, msg)
time.Sleep(1 * time.Second) // 10 transactions per second time.Sleep(1 * time.Second) // 10 transactions per second
} }
msg := node.ConstructTransactionListMessage(txs)
p2p.SendMessage(p2p.Peer{*ip, *port, "n/a"}, msg)
} }

@ -4,11 +4,11 @@ import (
"log" "log"
"sync" "sync"
"harmony-benchmark/p2p"
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"errors" "errors"
"fmt" "fmt"
"harmony-benchmark/p2p"
) )
var mutex = &sync.Mutex{} var mutex = &sync.Mutex{}
@ -38,6 +38,8 @@ func (consensus *Consensus) ProcessMessageLeader(message []byte) {
consensus.processResponseMessage(payload) consensus.processResponseMessage(payload)
case START_CONSENSUS: case START_CONSENSUS:
consensus.processStartConsensusMessage(msg) consensus.processStartConsensusMessage(msg)
case STOP_CONSENSUS:
consensus.stopConsensusMessage(msg)
default: default:
log.Println("Unexpected message type: %s", msgType) log.Println("Unexpected message type: %s", msgType)
} }
@ -119,23 +121,23 @@ func (consensus *Consensus) processCommitMessage(payload []byte) {
//#### Read payload data //#### Read payload data
offset := 0 offset := 0
// 4 byte consensus id // 4 byte consensus id
consensusId := binary.BigEndian.Uint32(payload[offset:offset+4]) consensusId := binary.BigEndian.Uint32(payload[offset : offset+4])
offset += 4 offset += 4
// 32 byte block hash // 32 byte block hash
blockHash := payload[offset:offset+32] blockHash := payload[offset : offset+32]
offset += 32 offset += 32
// 2 byte validator id // 2 byte validator id
validatorId := string(payload[offset:offset+2]) validatorId := string(payload[offset : offset+2])
offset += 2 offset += 2
// 33 byte commit // 33 byte commit
commit := payload[offset:offset+33] commit := payload[offset : offset+33]
offset += 33 offset += 33
// 64 byte of signature on previous data // 64 byte of signature on previous data
signature := payload[offset:offset+64] signature := payload[offset : offset+64]
offset += 64 offset += 64
//#### END: Read payload data //#### END: Read payload data
@ -225,23 +227,23 @@ func (consensus *Consensus) processResponseMessage(payload []byte) {
//#### Read payload data //#### Read payload data
offset := 0 offset := 0
// 4 byte consensus id // 4 byte consensus id
consensusId := binary.BigEndian.Uint32(payload[offset:offset+4]) consensusId := binary.BigEndian.Uint32(payload[offset : offset+4])
offset += 4 offset += 4
// 32 byte block hash // 32 byte block hash
blockHash := payload[offset:offset+32] blockHash := payload[offset : offset+32]
offset += 32 offset += 32
// 2 byte validator id // 2 byte validator id
validatorId := string(payload[offset:offset+2]) validatorId := string(payload[offset : offset+2])
offset += 2 offset += 2
// 32 byte response // 32 byte response
response := payload[offset:offset+32] response := payload[offset : offset+32]
offset += 32 offset += 32
// 64 byte of signature on previous data // 64 byte of signature on previous data
signature := payload[offset:offset+64] signature := payload[offset : offset+64]
offset += 64 offset += 64
//#### END: Read payload data //#### END: Read payload data

@ -78,6 +78,7 @@ const (
CHALLENGE CHALLENGE
RESPONSE RESPONSE
START_CONSENSUS START_CONSENSUS
STOP_CONSENSUS
) )
// Returns string name for the MessageType enum // Returns string name for the MessageType enum
@ -87,9 +88,11 @@ func (msgType MessageType) String() string {
"COMMIT", "COMMIT",
"CHALLENGE", "CHALLENGE",
"RESPONSE", "RESPONSE",
"START_CONSENSUS"} "START_CONSENSUS"
"STOP_CONSENSUS"
}
if msgType < ANNOUNCE || msgType > START_CONSENSUS { if msgType < ANNOUNCE || msgType > STOP_CONSENSUS {
return "Unknown" return "Unknown"
} }
return names[msgType] return names[msgType]

Loading…
Cancel
Save