From 9c8f9a447bfd768a7b29db904bd604410ec66a09 Mon Sep 17 00:00:00 2001 From: Jimmy Hu Date: Tue, 28 Aug 2018 11:21:48 +0800 Subject: core: Add vote type and add field to block. (#76) --- core/crypto.go | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) (limited to 'core/crypto.go') diff --git a/core/crypto.go b/core/crypto.go index 4fc49d7..17426f7 100644 --- a/core/crypto.go +++ b/core/crypto.go @@ -51,8 +51,8 @@ func verifyNotarySignature(pubkey crypto.PublicKey, func hashBlock(blockConv types.BlockConverter) (common.Hash, error) { block := blockConv.Block() - binaryHeight := make([]byte, 8) - binary.LittleEndian.PutUint64(binaryHeight, block.Height) + + hashPosition := hashPosition(block.ShardID, block.ChainID, block.Height) // Handling Block.Acks. acks := make(common.Hashes, 0, len(block.Acks)) for ack := range block.Acks { @@ -85,7 +85,7 @@ func hashBlock(blockConv types.BlockConverter) (common.Hash, error) { hash := crypto.Keccak256Hash( block.ProposerID.Hash[:], block.ParentHash[:], - binaryHeight, + hashPosition[:], hashAcks[:], hashTimestamps[:], payloadHash[:]) @@ -100,3 +100,45 @@ func verifyBlockSignature(pubkey crypto.PublicKey, } return pubkey.VerifySignature(hash, sig), nil } + +func hashVote(vote *types.Vote) common.Hash { + binaryPeriod := make([]byte, 8) + binary.LittleEndian.PutUint64(binaryPeriod, vote.Period) + + hash := crypto.Keccak256Hash( + vote.ProposerID.Hash[:], + vote.BlockHash[:], + binaryPeriod, + []byte{byte(vote.Type)}, + ) + return hash +} + +func verifyVoteSignature(vote *types.Vote, sigToPub SigToPubFn) (bool, error) { + hash := hashVote(vote) + pubKey, err := sigToPub(hash, vote.Signature) + if err != nil { + return false, err + } + if vote.ProposerID != types.NewValidatorID(pubKey) { + return false, nil + } + return true, nil +} + +func hashPosition(shardID, chainID, height uint64) common.Hash { + binaryShardID := make([]byte, 8) + binary.LittleEndian.PutUint64(binaryShardID, shardID) + + binaryChainID := make([]byte, 8) + binary.LittleEndian.PutUint64(binaryChainID, chainID) + + binaryHeight := make([]byte, 8) + binary.LittleEndian.PutUint64(binaryHeight, height) + + return crypto.Keccak256Hash( + binaryShardID, + binaryChainID, + binaryHeight, + ) +} -- cgit v1.2.3