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