From 3f9bdde12ec5112a9223470f0445c7225efeff85 Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Wed, 20 Jun 2018 12:49:18 -0700 Subject: [PATCH] Implement block verifier for verifying new block --- consensus/consensus_validator.go | 32 +++++++++++++++++++++++--------- node/node_handler.go | 3 +-- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/consensus/consensus_validator.go b/consensus/consensus_validator.go index 861ee0ec7..8063c9470 100644 --- a/consensus/consensus_validator.go +++ b/consensus/consensus_validator.go @@ -93,7 +93,7 @@ func (consensus *Consensus) processAnnounceMessage(payload []byte) { } // check block header is valid - txDecoder := gob.NewDecoder(bytes.NewReader(blockHeader)) // skip the SEND messge type + txDecoder := gob.NewDecoder(bytes.NewReader(blockHeader)) var blockHeaderObj blockchain.Block // TODO: separate header from block err := txDecoder.Decode(&blockHeaderObj) @@ -114,9 +114,6 @@ func (consensus *Consensus) processAnnounceMessage(payload []byte) { return } - - // sign block - // TODO: return the signature(commit) to leader // For now, simply return the private key of this node. msgToSend := consensus.constructCommitMessage() @@ -154,8 +151,8 @@ func (consensus Consensus) constructCommitMessage() []byte { return consensus.ConstructConsensusMessage(COMMIT, buffer.Bytes()) } -// TODO: fill in this function func getCommitMessage() []byte { + // TODO: use real cosi signature return make([]byte, 33) } @@ -171,7 +168,7 @@ func (consensus *Consensus) processChallengeMessage(payload []byte) { offset += 32 // 2 byte leader id - leaderId := string(payload[offset : offset+2]) + leaderId := binary.BigEndian.Uint16(payload[offset : offset+2]) offset += 2 // 33 byte of aggregated commit @@ -200,13 +197,30 @@ func (consensus *Consensus) processChallengeMessage(payload []byte) { _ = challenge _ = signature - // verify block data and the aggregated signatures + // erify block data and the aggregated signatures + // check consensus Id if consensusId != consensus.consensusId { - consensus.Log.Debug("Received message", "fromConsensus", consensusId) + consensus.Log.Debug("[ERROR] Received message with wrong consensus Id", "myConsensusId", consensus.consensusId, "theirConsensusId", consensusId) + return + } + + // check leader Id + leaderPrivKey := consensus.leader.Ip + consensus.leader.Port + reg, _ := regexp.Compile("[^0-9]+") + socketId := reg.ReplaceAllString(leaderPrivKey, "") + value, _ := strconv.Atoi(socketId) + if leaderId != uint16(value) { + consensus.Log.Debug("[ERROR] Received message from wrong leader", "myLeaderId", consensus.consensusId, "receivedLeaderId", consensusId) + return + } + + // check block hash + if bytes.Compare(blockHash[:], consensus.blockHash[:]) != 0 { + consensus.Log.Debug("[ERROR] Block hash doesn't match") return } - // sign the message + // TODO: verify aggregated commits with real schnor cosign verification // TODO: return the signature(response) to leader // For now, simply return the private key of this node. diff --git a/node/node_handler.go b/node/node_handler.go index 570c9044b..e0be379d7 100644 --- a/node/node_handler.go +++ b/node/node_handler.go @@ -150,6 +150,5 @@ func (node *Node) WaitForConsensusReady(readySignal chan int) { } func (node *Node) VerifyNewBlock(block *blockchain.Block) bool { - // TODO: fill in this function - return true + return node.UtxoPool.VerifyTransactions(block.Transactions) } \ No newline at end of file