Fill in multisig mocks

pull/5/merge
Rongjian Lan 7 years ago
parent 95ca931431
commit 6adafcb8c9
  1. 2
      consensus/consensus.go
  2. 54
      consensus/consensus_leader.go
  3. 6
      consensus/consensus_validator.go

@ -31,7 +31,7 @@ type Consensus struct {
// Consensus Id (View Id) - 4 byte // Consensus Id (View Id) - 4 byte
consensusId uint32 consensusId uint32
// Blockhash - 32 byte // Blockhash - 32 byte
blockHash []byte blockHash [32]byte
// BlockHeader to run consensus on // BlockHeader to run consensus on
blockHeader []byte blockHeader []byte
// Shard Id which this node belongs to // Shard Id which this node belongs to

@ -10,6 +10,8 @@ import (
"fmt" "fmt"
"harmony-benchmark/blockchain" "harmony-benchmark/blockchain"
"harmony-benchmark/p2p" "harmony-benchmark/p2p"
"crypto/sha256"
"strings"
) )
var mutex = &sync.Mutex{} var mutex = &sync.Mutex{}
@ -64,7 +66,7 @@ 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 = newBlock.Hash copy(newBlock.Hash[:32], consensus.blockHash[:])
msgToSend, err := consensus.constructAnnounceMessage() msgToSend, err := consensus.constructAnnounceMessage()
if err != nil { if err != nil {
@ -88,7 +90,7 @@ func (consensus Consensus) constructAnnounceMessage() ([]byte, error) {
if len(consensus.blockHash) != 32 { if len(consensus.blockHash) != 32 {
return buffer.Bytes(), errors.New(fmt.Sprintf("Block Hash size is %d bytes", len(consensus.blockHash))) return buffer.Bytes(), errors.New(fmt.Sprintf("Block Hash size is %d bytes", len(consensus.blockHash)))
} }
buffer.Write(consensus.blockHash) buffer.Write(consensus.blockHash[:])
// 2 byte leader id // 2 byte leader id
twoBytes := make([]byte, 2) twoBytes := make([]byte, 2)
@ -111,14 +113,9 @@ func (consensus Consensus) constructAnnounceMessage() ([]byte, error) {
return consensus.ConstructConsensusMessage(ANNOUNCE, buffer.Bytes()), nil return consensus.ConstructConsensusMessage(ANNOUNCE, buffer.Bytes()), nil
} }
// TODO: fill in this function // Get the hash of a block's byte stream
func constructNewBlock() []byte { func getBlockHash(block []byte) [32]byte {
return make([]byte, 200) return sha256.Sum256(block)
}
// TODO: fill in this function
func getBlockHash(block []byte) []byte {
return make([]byte, 32)
} }
// TODO: fill in this function // TODO: fill in this function
@ -126,9 +123,10 @@ func getBlockHeader() []byte {
return make([]byte, 200) return make([]byte, 200)
} }
// TODO: fill in this function
func signMessage(message []byte) []byte { func signMessage(message []byte) []byte {
return make([]byte, 64) // TODO: implement real ECC signature
mockSignature := sha256.Sum256(message)
return append(mockSignature[:], mockSignature[:]...)
} }
func (consensus *Consensus) processCommitMessage(payload []byte) { func (consensus *Consensus) processCommitMessage(payload []byte) {
@ -199,7 +197,7 @@ func (consensus Consensus) constructChallengeMessage() []byte {
buffer.Write(fourBytes) buffer.Write(fourBytes)
// 32 byte block hash // 32 byte block hash
buffer.Write(consensus.blockHash) buffer.Write(consensus.blockHash[:])
// 2 byte leader id // 2 byte leader id
twoBytes := make([]byte, 2) twoBytes := make([]byte, 2)
@ -207,10 +205,10 @@ func (consensus Consensus) constructChallengeMessage() []byte {
buffer.Write(twoBytes) buffer.Write(twoBytes)
// 33 byte aggregated commit // 33 byte aggregated commit
buffer.Write(getAggregatedCommit()) buffer.Write(getAggregatedCommit(consensus.commits))
// 33 byte aggregated key // 33 byte aggregated key
buffer.Write(getAggregatedKey()) buffer.Write(getAggregatedKey(consensus.commits))
// 32 byte challenge // 32 byte challenge
buffer.Write(getChallenge()) buffer.Write(getChallenge())
@ -222,18 +220,30 @@ func (consensus Consensus) constructChallengeMessage() []byte {
return consensus.ConstructConsensusMessage(CHALLENGE, buffer.Bytes()) return consensus.ConstructConsensusMessage(CHALLENGE, buffer.Bytes())
} }
// TODO: fill in this function func getAggregatedCommit(commits map[string]string) []byte {
func getAggregatedCommit() []byte { // TODO: implement actual commit aggregation
return make([]byte, 33) var commitArray []string
for _, val := range commits {
commitArray = append(commitArray, val)
}
var commit [32]byte
commit = sha256.Sum256([]byte(strings.Join(commitArray, "")))
return append(commit[:], byte(0))
} }
// TODO: fill in this function func getAggregatedKey(commits map[string]string) []byte {
func getAggregatedKey() []byte { // TODO: implement actual key aggregation
return make([]byte, 33) var commitArray []string
for key := range commits {
commitArray = append(commitArray, key)
}
var commit [32]byte
commit = sha256.Sum256([]byte(strings.Join(commitArray, "")))
return append(commit[:], byte(0))
} }
// TODO: fill in this function
func getChallenge() []byte { func getChallenge() []byte {
// TODO: implement actual challenge data
return make([]byte, 32) return make([]byte, 32)
} }

@ -71,7 +71,7 @@ func (consensus *Consensus) processAnnounceMessage(payload []byte) {
_ = blockHeaderSize _ = blockHeaderSize
_ = signature _ = signature
consensus.blockHash = blockHash copy(blockHash[:32], consensus.blockHash[:])
// verify block data // verify block data
if consensusId != consensus.consensusId { if consensusId != consensus.consensusId {
log.Printf("Received message with consensus Id: %d. My consensus Id: %d\n", consensusId, consensus.consensusId) log.Printf("Received message with consensus Id: %d. My consensus Id: %d\n", consensusId, consensus.consensusId)
@ -98,7 +98,7 @@ func (consensus Consensus) constructCommitMessage() []byte {
buffer.Write(fourBytes) buffer.Write(fourBytes)
// 32 byte block hash // 32 byte block hash
buffer.Write(consensus.blockHash) buffer.Write(consensus.blockHash[:])
// 2 byte validator id // 2 byte validator id
twoBytes := make([]byte, 2) twoBytes := make([]byte, 2)
@ -190,7 +190,7 @@ func (consensus Consensus) constructResponseMessage() []byte {
buffer.Write(fourBytes) buffer.Write(fourBytes)
// 32 byte block hash // 32 byte block hash
buffer.Write(consensus.blockHash) buffer.Write(consensus.blockHash[:32])
// 2 byte validator id // 2 byte validator id
twoBytes := make([]byte, 2) twoBytes := make([]byte, 2)

Loading…
Cancel
Save