Add challenge broadcast; put consensus signature in map; refactor consensus init into consensus package

pull/2/head
Rongjian Lan 7 years ago
parent c0fd09be05
commit 0e66f7147f
  1. 27
      benchmark_node.go
  2. 21
      consensus/consensus.go
  3. 18
      consensus/consensus_leader.go

@ -128,24 +128,6 @@ func NodeHandler(conn net.Conn, consensus *consensus.Consensus) {
//relayToPorts(receivedMessage, conn) //relayToPorts(receivedMessage, conn)
} }
func initConsensus(ip, port, ipfile string) consensus.Consensus {
// The first Ip, port passed will be leader.
consensus := consensus.Consensus{}
peer := p2p.Peer{Port: port, Ip: ip}
Peers := getPeers(ip, port, ipfile)
leaderPeer := getLeader(ipfile)
if leaderPeer == peer {
consensus.IsLeader = true
} else {
consensus.IsLeader = false
}
consensus.Leader = leaderPeer
consensus.Validators = Peers
consensus.PriKey = ip + ":" + port // use ip:port as unique key for now
return consensus
}
func getLeader(iplist string) p2p.Peer { func getLeader(iplist string) p2p.Peer {
file, _ := os.Open(iplist) file, _ := os.Open(iplist)
fscanner := bufio.NewScanner(file) fscanner := bufio.NewScanner(file)
@ -183,15 +165,16 @@ func main() {
ipfile := flag.String("ipfile", "iplist.txt", "file containing all ip addresses") ipfile := flag.String("ipfile", "iplist.txt", "file containing all ip addresses")
flag.Parse() flag.Parse()
fmt.Println() fmt.Println()
consensus := initConsensus(*ip, *port, *ipfile)
consensusObj := consensus.InitConsensus(*ip, *port, getPeers(*ip, *port, *ipfile), getLeader(*ipfile))
var nodeStatus string var nodeStatus string
if consensus.IsLeader { if consensusObj.IsLeader {
nodeStatus = "leader" nodeStatus = "leader"
} else { } else {
nodeStatus = "validator" nodeStatus = "validator"
} }
fmt.Println(consensus) fmt.Println(consensusObj)
fmt.Printf("This node is a %s node with ip: %s and port: %s\n", nodeStatus, *ip, *port) fmt.Printf("This node is a %s node with ip: %s and port: %s\n", nodeStatus, *ip, *port)
fmt.Println() fmt.Println()
startServer(*port, NodeHandler, &consensus) startServer(*port, NodeHandler, &consensusObj)
} }

@ -9,7 +9,7 @@ import (
type Consensus struct { type Consensus struct {
State ConsensusState State ConsensusState
// Signatures collected from validators // Signatures collected from validators
Signatures []string Signatures map[string]string
// Actual block data to reach consensus on // Actual block data to reach consensus on
Data string Data string
// List of validators // List of validators
@ -53,3 +53,22 @@ func (state ConsensusState) String() string {
} }
return names[state] return names[state]
} }
func InitConsensus(ip, port string, peers []p2p.Peer, leader p2p.Peer) Consensus {
// The first Ip, port passed will be leader.
consensus := Consensus{}
peer := p2p.Peer{Port: port, Ip: ip}
Peers := peers
leaderPeer := leader
if leaderPeer == peer {
consensus.IsLeader = true
} else {
consensus.IsLeader = false
}
consensus.Signatures = make(map[string]string)
consensus.Leader = leaderPeer
consensus.Validators = Peers
consensus.PriKey = ip + ":" + port // use ip:port as unique key for now
return consensus
}

@ -55,16 +55,26 @@ func (consensus *Consensus) startConsensus(msg string) {
func (consensus *Consensus) processCommitMessage(msg string) { func (consensus *Consensus) processCommitMessage(msg string) {
// verify and aggregate all the signatures // verify and aggregate all the signatures
if _, ok := consensus.Signatures[msg]; !ok {
mutex.Lock() mutex.Lock()
consensus.Signatures = append(consensus.Signatures, msg) consensus.Signatures[msg] = msg
mutex.Unlock()
}
// Broadcast challenge
log.Printf("Number of signatures received: %d", len(consensus.Signatures))
if consensus.State != CHALLENGE_DONE && len(consensus.Signatures) >= (2 * len(consensus.Validators)) / 3 + 1 {
mutex.Lock()
if consensus.State == ANNOUNCE_DONE {
// Set state to CHALLENGE_DONE // Set state to CHALLENGE_DONE
consensus.State = CHALLENGE_DONE consensus.State = CHALLENGE_DONE
}
mutex.Unlock() mutex.Unlock()
// Broadcast challenge
msgToSend := ConstructConsensusMessage(CHALLENGE, []byte("challenge"))
p2p.BroadcastMessage(consensus.Validators, msgToSend)
log.Printf("Number of signatures received: %d", len(consensus.Signatures))
if len(consensus.Signatures) >= (2 * len(consensus.Validators)) / 3 + 1 {
log.Printf("Consensus reached with %d signatures: %s", len(consensus.Signatures), consensus.Signatures) log.Printf("Consensus reached with %d signatures: %s", len(consensus.Signatures), consensus.Signatures)
} }

Loading…
Cancel
Save