diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2018-08-28 11:21:48 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-28 11:21:48 +0800 |
commit | 9c8f9a447bfd768a7b29db904bd604410ec66a09 (patch) | |
tree | 76495e11738e24e4ff3f27d647509f29012eea6f /core/crypto.go | |
parent | e122cb236312e0ca3ef6e0207a20890ec1e7bfaf (diff) | |
download | dexon-consensus-9c8f9a447bfd768a7b29db904bd604410ec66a09.tar dexon-consensus-9c8f9a447bfd768a7b29db904bd604410ec66a09.tar.gz dexon-consensus-9c8f9a447bfd768a7b29db904bd604410ec66a09.tar.bz2 dexon-consensus-9c8f9a447bfd768a7b29db904bd604410ec66a09.tar.lz dexon-consensus-9c8f9a447bfd768a7b29db904bd604410ec66a09.tar.xz dexon-consensus-9c8f9a447bfd768a7b29db904bd604410ec66a09.tar.zst dexon-consensus-9c8f9a447bfd768a7b29db904bd604410ec66a09.zip |
core: Add vote type and add field to block. (#76)
Diffstat (limited to 'core/crypto.go')
-rw-r--r-- | core/crypto.go | 48 |
1 files changed, 45 insertions, 3 deletions
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, + ) +} |