aboutsummaryrefslogtreecommitdiffstats
path: root/core/crypto_test.go
diff options
context:
space:
mode:
authorJimmy Hu <jimmy.hu@dexon.org>2018-08-10 10:39:29 +0800
committerGitHub <noreply@github.com>2018-08-10 10:39:29 +0800
commit99d9591e5f0af54bf06f41cfd2658cfcc9ee6436 (patch)
tree784fb11e9830c97056524bb1330df5b551453a80 /core/crypto_test.go
parent36cce13f83e6aa2e3ee67b787f6cb133066425a9 (diff)
downloaddexon-consensus-99d9591e5f0af54bf06f41cfd2658cfcc9ee6436.tar
dexon-consensus-99d9591e5f0af54bf06f41cfd2658cfcc9ee6436.tar.gz
dexon-consensus-99d9591e5f0af54bf06f41cfd2658cfcc9ee6436.tar.bz2
dexon-consensus-99d9591e5f0af54bf06f41cfd2658cfcc9ee6436.tar.lz
dexon-consensus-99d9591e5f0af54bf06f41cfd2658cfcc9ee6436.tar.xz
dexon-consensus-99d9591e5f0af54bf06f41cfd2658cfcc9ee6436.tar.zst
dexon-consensus-99d9591e5f0af54bf06f41cfd2658cfcc9ee6436.zip
core: Add block hash signature functions in core/ctypto.go. (#39)
Diffstat (limited to 'core/crypto_test.go')
-rw-r--r--core/crypto_test.go81
1 files changed, 77 insertions, 4 deletions
diff --git a/core/crypto_test.go b/core/crypto_test.go
index 0ee28e4..be130e2 100644
--- a/core/crypto_test.go
+++ b/core/crypto_test.go
@@ -32,10 +32,28 @@ type CryptoTestSuite struct {
suite.Suite
}
-func (s *CryptoTestSuite) newBlock(prevBlock *types.Block) *types.Block {
+var myVID = types.ValidatorID{Hash: common.NewRandomHash()}
+
+type simpleBlock struct {
+ block *types.Block
+}
+
+func (sb *simpleBlock) Block() *types.Block {
+ return sb.block
+}
+
+func (sb *simpleBlock) GetPayloads() [][]byte {
+ return [][]byte{}
+}
+
+func (s *CryptoTestSuite) prepareBlock(prevBlock *types.Block) *types.Block {
+ acks := make(map[common.Hash]struct{})
+ timestamps := make(map[types.ValidatorID]time.Time)
+ timestamps[myVID] = time.Now().UTC()
if prevBlock == nil {
return &types.Block{
- Hash: common.NewRandomHash(),
+ Acks: acks,
+ Timestamps: timestamps,
ConsensusInfo: types.ConsensusInfo{
Timestamp: time.Now(),
Height: 0,
@@ -44,9 +62,14 @@ func (s *CryptoTestSuite) newBlock(prevBlock *types.Block) *types.Block {
}
parentHash, err := hashCompactionChainAck(prevBlock)
s.Require().Nil(err)
+ s.Require().NotEqual(prevBlock.Hash, common.Hash{})
+ acks[parentHash] = struct{}{}
return &types.Block{
- Hash: common.NewRandomHash(),
+ ParentHash: prevBlock.Hash,
+ Acks: acks,
+ Timestamps: timestamps,
ConsensusInfoParentHash: parentHash,
+ Height: prevBlock.Height + 1,
CompactionChainAck: types.CompactionChainAck{
AckingBlockHash: prevBlock.Hash,
},
@@ -57,6 +80,14 @@ func (s *CryptoTestSuite) newBlock(prevBlock *types.Block) *types.Block {
}
}
+func (s *CryptoTestSuite) newBlock(prevBlock *types.Block) *types.Block {
+ block := s.prepareBlock(prevBlock)
+ var err error
+ block.Hash, err = hashBlock(&simpleBlock{block: block})
+ s.Require().Nil(err)
+ return block
+}
+
func (s *CryptoTestSuite) generateCompactionChain(
length int, prv crypto.PrivateKey) []*types.Block {
blocks := make([]*types.Block, length)
@@ -76,7 +107,7 @@ func (s *CryptoTestSuite) generateCompactionChain(
return blocks
}
-func (s *CryptoTestSuite) TestSignature() {
+func (s *CryptoTestSuite) TestCompactionChainAckSignature() {
prv, err := eth.NewPrivateKey()
pub := prv.PublicKey()
s.Require().Nil(err)
@@ -111,6 +142,48 @@ func (s *CryptoTestSuite) TestSignature() {
}
}
+func (s *CryptoTestSuite) generateBlockChain(
+ length int, prv crypto.PrivateKey) []*types.Block {
+ blocks := make([]*types.Block, length)
+ var prevBlock *types.Block
+ for idx := range blocks {
+ block := s.newBlock(prevBlock)
+ blocks[idx] = block
+ var err error
+ block.Signature, err = signBlock(&simpleBlock{block: block}, prv)
+ s.Require().Nil(err)
+ }
+ return blocks
+}
+
+func (s *CryptoTestSuite) TestBlockSignature() {
+ prv, err := eth.NewPrivateKey()
+ pub := prv.PublicKey()
+ s.Require().Nil(err)
+ blocks := s.generateBlockChain(10, prv)
+ blockMap := make(map[common.Hash]*types.Block)
+ for _, block := range blocks {
+ blockMap[block.Hash] = block
+ }
+ for _, block := range blocks {
+ if !block.IsGenesis() {
+ parentBlock, exist := blockMap[block.ParentHash]
+ s.Require().True(exist)
+ s.True(parentBlock.Height == block.Height-1)
+ hash, err := hashBlock(&simpleBlock{block: parentBlock})
+ s.Require().Nil(err)
+ s.Equal(hash, block.ParentHash)
+ }
+ s.True(verifyBlockSignature(pub, &simpleBlock{block: block}, block.Signature))
+ }
+ // Modify Block.Acks and verify signature again.
+ for _, block := range blocks {
+ block.Acks[common.NewRandomHash()] = struct{}{}
+ s.False(verifyBlockSignature(
+ pub, &simpleBlock{block: block}, block.Signature))
+ }
+}
+
func TestCrypto(t *testing.T) {
suite.Run(t, new(CryptoTestSuite))
}