OSDN Git Service

TestSaveBlockHeader
authorChengcheng Zhang <943420582@qq.com>
Mon, 15 Jul 2019 10:58:22 +0000 (18:58 +0800)
committerChengcheng Zhang <943420582@qq.com>
Mon, 15 Jul 2019 10:58:22 +0000 (18:58 +0800)
database/store_test.go

index bbd4916..e9ec0fb 100644 (file)
@@ -5,7 +5,6 @@ import (
        "testing"
 
        "github.com/vapor/consensus"
-
        dbm "github.com/vapor/database/leveldb"
        "github.com/vapor/database/storage"
        "github.com/vapor/protocol"
@@ -219,3 +218,77 @@ func TestSaveBlock(t *testing.T) {
                }
        }
 }
+
+func TestSaveBlockHeader(t *testing.T) {
+       testDB := dbm.NewDB("testdb", "leveldb", "temp")
+       defer func() {
+               testDB.Close()
+               os.RemoveAll("temp")
+       }()
+
+       store := NewStore(testDB)
+
+       cases := []struct {
+               blockHeader *types.BlockHeader
+       }{
+               {
+                       blockHeader: &types.BlockHeader{
+                               Version:   uint64(1),
+                               Height:    uint64(1111),
+                               Timestamp: uint64(1528945000),
+                       },
+               },
+               {
+                       blockHeader: &types.BlockHeader{
+                               Version:           uint64(1),
+                               Height:            uint64(0),
+                               PreviousBlockHash: bc.NewHash([32]byte{0x3e, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                               Timestamp:         uint64(1563186936),
+                               BlockCommitment: types.BlockCommitment{
+                                       TransactionsMerkleRoot: bc.NewHash([32]byte{0x3e, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                                       TransactionStatusHash:  bc.NewHash([32]byte{0x3e, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                               },
+                               BlockWitness: types.BlockWitness{
+                                       Witness: [][]byte{[]byte{0x3e, 0x94, 0x5d, 0x35}, []byte{0x3e, 0x94, 0x5d, 0x35}},
+                               },
+                       },
+               },
+               {
+                       blockHeader: &types.BlockHeader{
+                               Version:           uint64(1),
+                               Height:            uint64(8848),
+                               PreviousBlockHash: bc.NewHash([32]byte{0x3e, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                               Timestamp:         uint64(156318693600),
+                               BlockCommitment: types.BlockCommitment{
+                                       TransactionsMerkleRoot: bc.NewHash([32]byte{0x3e, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                                       TransactionStatusHash:  bc.NewHash([32]byte{0x3e, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                               },
+                               BlockWitness: types.BlockWitness{
+                                       Witness: [][]byte{
+                                               []byte{0x3e, 0x94, 0x5d, 0x35},
+                                               []byte{0x3e, 0x94, 0x5d, 0x35},
+                                               []byte{0x3e, 0x94, 0x5d, 0x35},
+                                               []byte{0xd4, 0x3b, 0x3d, 0xe3},
+                                       },
+                               },
+                       },
+               },
+       }
+
+       for i, c := range cases {
+               if err := store.SaveBlockHeader(c.blockHeader); err != nil {
+                       t.Fatal(err)
+               }
+
+               blockHash := c.blockHeader.Hash()
+               gotBlockHeaderData := store.db.Get(calcBlockHeaderKey(&blockHash))
+               binaryBlockHeader, err := c.blockHeader.MarshalText()
+               if err != nil {
+                       t.Fatal(err)
+               }
+
+               if !testutil.DeepEqual(gotBlockHeaderData, binaryBlockHeader) {
+                       t.Errorf("case %v: block header mismatch: have %x, want %x", i, gotBlockHeaderData, binaryBlockHeader)
+               }
+       }
+}