Add real public and private key into Peer object; initialize schnorr multi-signature bitmap in the consensus

pull/55/head
Rongjian Lan 6 years ago
parent 8f85868345
commit 86093d6ffc
  1. 6
      benchmark.go
  2. 34
      consensus/consensus.go
  3. 2
      consensus/consensus_leader.go
  4. 3
      consensus/consensus_validator.go
  5. 3
      p2p/peer.go

@ -14,6 +14,7 @@ import (
"time" "time"
"github.com/shirou/gopsutil/process" "github.com/shirou/gopsutil/process"
"harmony-benchmark/crypto"
) )
const ( const (
@ -37,6 +38,8 @@ func getLeader(myShardId string, config *[][]string) p2p.Peer {
if status == "leader" && myShardId == shardId { if status == "leader" && myShardId == shardId {
leaderPeer.Ip = ip leaderPeer.Ip = ip
leaderPeer.Port = port leaderPeer.Port = port
priKey := crypto.Hash(ip + ":" + port) // use ip:port as unique private key for now. TODO: use real private key
leaderPeer.PubKey = crypto.GetPublicKeyFromPrivateKey(crypto.Curve, priKey)
} }
} }
return leaderPeer return leaderPeer
@ -49,7 +52,8 @@ func getPeers(myIp, myPort, myShardId string, config *[][]string) []p2p.Peer {
if status != "validator" || ip == myIp && port == myPort || myShardId != shardId { if status != "validator" || ip == myIp && port == myPort || myShardId != shardId {
continue continue
} }
peer := p2p.Peer{Port: port, Ip: ip} priKey := crypto.Hash(ip + ":" + port) // use ip:port as unique private key for now. TODO: use real private key
peer := p2p.Peer{Port: port, Ip: ip, PubKey: crypto.GetPublicKeyFromPrivateKey(crypto.Curve, priKey)}
peerList = append(peerList, peer) peerList = append(peerList, peer)
} }
return peerList return peerList

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"github.com/dedis/kyber" "github.com/dedis/kyber"
"harmony-benchmark/blockchain" "harmony-benchmark/blockchain"
"harmony-benchmark/crypto"
"harmony-benchmark/log" "harmony-benchmark/log"
"harmony-benchmark/p2p" "harmony-benchmark/p2p"
"regexp" "regexp"
@ -15,8 +16,10 @@ import (
// Consensus data containing all info related to one round of consensus process // Consensus data containing all info related to one round of consensus process
type Consensus struct { type Consensus struct {
state ConsensusState state ConsensusState
// Signatures collected from validators // Commits collected from validators.
commits map[string]string commits map[string]string
// Commits collected from validators.
commitments *crypto.Mask
// Signatures collected from validators // Signatures collected from validators
responses map[string]string responses map[string]string
// List of validators // List of validators
@ -24,7 +27,7 @@ type Consensus struct {
// Leader // Leader
leader p2p.Peer leader p2p.Peer
// private key of current node // private key of current node
priKey string priKey [32]byte
// Whether I am leader. False means I am validator // Whether I am leader. False means I am validator
IsLeader bool IsLeader bool
// Leader or validator Id - 2 byte // Leader or validator Id - 2 byte
@ -74,11 +77,8 @@ type BlockConsensusStatus struct {
// FYI, see https://golang.org/doc/effective_go.html?#package-names // FYI, see https://golang.org/doc/effective_go.html?#package-names
func NewConsensus(ip, port, ShardID string, peers []p2p.Peer, leader p2p.Peer) *Consensus { func NewConsensus(ip, port, ShardID string, peers []p2p.Peer, leader p2p.Peer) *Consensus {
consensus := Consensus{} consensus := Consensus{}
Peers := peers
leaderPeer := leader
selfPeer := p2p.Peer{Port: port, Ip: ip}
if leaderPeer == selfPeer { if leader.Port == port && leader.Ip == ip {
consensus.IsLeader = true consensus.IsLeader = true
} else { } else {
consensus.IsLeader = false consensus.IsLeader = false
@ -87,11 +87,23 @@ func NewConsensus(ip, port, ShardID string, peers []p2p.Peer, leader p2p.Peer) *
consensus.commits = make(map[string]string) consensus.commits = make(map[string]string)
consensus.responses = make(map[string]string) consensus.responses = make(map[string]string)
consensus.leader = leaderPeer consensus.leader = leader
consensus.validators = Peers consensus.validators = peers
consensus.priKey = ip + ":" + port // use ip:port as unique key for now // Initialize cosign bitmap
allPublics := make([]kyber.Point, 0)
for _, validatorPeer := range consensus.validators {
allPublics = append(allPublics, validatorPeer.PubKey)
}
allPublics = append(allPublics, leader.PubKey)
mask, err := crypto.NewMask(crypto.Curve, allPublics, consensus.leader.PubKey)
if err != nil {
panic("Failed to create commitment mask")
}
consensus.commitments = mask
// Set private key for myself so that I can sign messages.
consensus.priKey = crypto.Hash(ip + ":" + port) // use ip:port as unique private key for now. TODO: use real private key
consensus.consensusId = 0 // or view Id in the original pbft paper consensus.consensusId = 0 // or view Id in the original pbft paper
myShardID, err := strconv.Atoi(ShardID) myShardID, err := strconv.Atoi(ShardID)
@ -109,7 +121,7 @@ func NewConsensus(ip, port, ShardID string, peers []p2p.Peer, leader p2p.Peer) *
if err != nil { if err != nil {
consensus.Log.Crit("Regex Compilation Failed", "err", err, "consensus", consensus) consensus.Log.Crit("Regex Compilation Failed", "err", err, "consensus", consensus)
} }
socketId := reg.ReplaceAllString(consensus.priKey, "") socketId := reg.ReplaceAllString(ip+port, "") // A integer Id formed by unique IP/PORT pair
value, err := strconv.Atoi(socketId) value, err := strconv.Atoi(socketId)
consensus.nodeId = uint16(value) consensus.nodeId = uint16(value)
@ -143,5 +155,5 @@ func (consensus *Consensus) String() string {
duty = "VLD" // validator duty = "VLD" // validator
} }
return fmt.Sprintf("[duty:%s, priKey:%s, ShardID:%v, nodeId:%v, state:%s]", return fmt.Sprintf("[duty:%s, priKey:%s, ShardID:%v, nodeId:%v, state:%s]",
duty, consensus.priKey, consensus.ShardID, consensus.nodeId, consensus.state) duty, fmt.Sprintf("%x", consensus.priKey), consensus.ShardID, consensus.nodeId, consensus.state)
} }

@ -81,6 +81,7 @@ func (consensus *Consensus) startConsensus(newBlock *blockchain.Block) {
p2p.BroadcastMessage(consensus.validators, msgToSend) p2p.BroadcastMessage(consensus.validators, msgToSend)
// Set state to ANNOUNCE_DONE // Set state to ANNOUNCE_DONE
consensus.state = ANNOUNCE_DONE consensus.state = ANNOUNCE_DONE
// Generate leader's own commitment
} }
// Constructs the announce message // Constructs the announce message
@ -168,7 +169,6 @@ func (consensus *Consensus) processCommitMessage(payload []byte) {
shouldProcess := !ok shouldProcess := !ok
if shouldProcess { if shouldProcess {
consensus.commits[validatorId] = validatorId consensus.commits[validatorId] = validatorId
//consensus.Log.Debug("Number of commits received", "consensusId", consensus.consensusId, "count", len(consensus.commits))
} }
if !shouldProcess { if !shouldProcess {

@ -5,7 +5,6 @@ import (
"encoding/binary" "encoding/binary"
"encoding/gob" "encoding/gob"
"github.com/dedis/kyber" "github.com/dedis/kyber"
"github.com/dedis/kyber/group/edwards25519"
"harmony-benchmark/attack" "harmony-benchmark/attack"
"harmony-benchmark/blockchain" "harmony-benchmark/blockchain"
"harmony-benchmark/crypto" "harmony-benchmark/crypto"
@ -160,7 +159,7 @@ func (consensus *Consensus) constructCommitMessage() (secret kyber.Scalar, commi
buffer.Write(twoBytes) buffer.Write(twoBytes)
// 32 byte of commit (Note it's different than Zilliqa's ECPoint which takes 33 bytes: https://crypto.stackexchange.com/questions/51703/how-to-convert-from-curve25519-33-byte-to-32-byte-representation) // 32 byte of commit (Note it's different than Zilliqa's ECPoint which takes 33 bytes: https://crypto.stackexchange.com/questions/51703/how-to-convert-from-curve25519-33-byte-to-32-byte-representation)
secret, commitment := crypto.Commit(edwards25519.NewBlakeSHA256Ed25519()) secret, commitment := crypto.Commit(crypto.Curve)
commitment.MarshalTo(buffer) commitment.MarshalTo(buffer)
// 64 byte of signature on previous data // 64 byte of signature on previous data

@ -3,6 +3,7 @@ package p2p
import ( import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"github.com/dedis/kyber"
"harmony-benchmark/attack" "harmony-benchmark/attack"
"log" "log"
"net" "net"
@ -14,7 +15,7 @@ import (
type Peer struct { type Peer struct {
Ip string // Ip address of the peer Ip string // Ip address of the peer
Port string // Port number of the peer Port string // Port number of the peer
PubKey string // Public key of the peer PubKey kyber.Point // Public key of the peer
} }
// SendMessage sends the message to the peer // SendMessage sends the message to the peer

Loading…
Cancel
Save