Fill in multisig mocks

pull/5/merge
Rongjian Lan 6 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
consensusId uint32
// Blockhash - 32 byte
blockHash []byte
blockHash [32]byte
// BlockHeader to run consensus on
blockHeader []byte
// Shard Id which this node belongs to

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

@ -71,7 +71,7 @@ func (consensus *Consensus) processAnnounceMessage(payload []byte) {
_ = blockHeaderSize
_ = signature
consensus.blockHash = blockHash
copy(blockHash[:32], consensus.blockHash[:])
// verify block data
if 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)
// 32 byte block hash
buffer.Write(consensus.blockHash)
buffer.Write(consensus.blockHash[:])
// 2 byte validator id
twoBytes := make([]byte, 2)
@ -190,7 +190,7 @@ func (consensus Consensus) constructResponseMessage() []byte {
buffer.Write(fourBytes)
// 32 byte block hash
buffer.Write(consensus.blockHash)
buffer.Write(consensus.blockHash[:32])
// 2 byte validator id
twoBytes := make([]byte, 2)

Loading…
Cancel
Save