|
|
|
@ -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) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|