OSDN Git Service

try to fix ban peer bug (#273)
[bytom/vapor.git] / database / store_test.go
1 package database
2
3 import (
4         "os"
5         "testing"
6
7         dbm "github.com/vapor/database/leveldb"
8         "github.com/vapor/database/storage"
9         "github.com/vapor/protocol"
10         "github.com/vapor/protocol/bc"
11         "github.com/vapor/protocol/bc/types"
12         "github.com/vapor/protocol/state"
13         "github.com/vapor/testutil"
14 )
15
16 func TestSaveChainStatus(t *testing.T) {
17         testDB := dbm.NewDB("testdb", "leveldb", "temp")
18         defer func() {
19                 testDB.Close()
20                 os.RemoveAll("temp")
21         }()
22
23         store := NewStore(testDB)
24
25         blockHeader := &types.BlockHeader{Height: 100}
26         blockHash := blockHeader.Hash() //Hash: bc.Hash{V0: 0, V1: 1, V2: 2, V3: 3}
27         view := &state.UtxoViewpoint{
28                 Entries: map[bc.Hash]*storage.UtxoEntry{
29                         bc.Hash{V0: 1, V1: 2, V2: 3, V3: 4}: &storage.UtxoEntry{Type: storage.NormalUTXOType, BlockHeight: 100, Spent: false},
30                         bc.Hash{V0: 1, V1: 2, V2: 3, V3: 4}: &storage.UtxoEntry{Type: storage.CoinbaseUTXOType, BlockHeight: 100, Spent: true},
31                         bc.Hash{V0: 1, V1: 1, V2: 3, V3: 4}: &storage.UtxoEntry{Type: storage.NormalUTXOType, BlockHeight: 100, Spent: true},
32                         bc.Hash{V0: 1, V1: 1, V2: 3, V3: 5}: &storage.UtxoEntry{Type: storage.CrosschainUTXOType, BlockHeight: 100, Spent: false},
33                         bc.Hash{V0: 1, V1: 1, V2: 3, V3: 6}: &storage.UtxoEntry{Type: storage.CrosschainUTXOType, BlockHeight: 100, Spent: true},
34                         bc.Hash{V0: 1, V1: 3, V2: 3, V3: 7}: &storage.UtxoEntry{Type: storage.VoteUTXOType, BlockHeight: 100, Spent: false},
35                         bc.Hash{V0: 1, V1: 3, V2: 3, V3: 7}: &storage.UtxoEntry{Type: storage.VoteUTXOType, BlockHeight: 100, Spent: true},
36                 },
37         }
38
39         if err := store.SaveChainStatus(blockHeader, blockHeader, []*types.BlockHeader{blockHeader}, view, []*state.ConsensusResult{}); err != nil {
40                 t.Fatal(err)
41         }
42
43         expectStatus := &protocol.BlockStoreState{Height: blockHeader.Height, Hash: &blockHash, IrreversibleHeight: blockHeader.Height, IrreversibleHash: &blockHash}
44         if !testutil.DeepEqual(store.GetStoreStatus(), expectStatus) {
45                 t.Errorf("got block status:%v, expect block status:%v", store.GetStoreStatus(), expectStatus)
46         }
47
48         for hash, utxo := range view.Entries {
49                 if (utxo.Type == storage.NormalUTXOType) && utxo.Spent {
50                         continue
51                 }
52                 if (utxo.Type == storage.CrosschainUTXOType) && (!utxo.Spent) {
53                         continue
54                 }
55                 if (utxo.Type == storage.VoteUTXOType) && (utxo.Spent) {
56                         continue
57                 }
58
59                 gotUtxo, err := store.GetUtxo(&hash)
60                 if err != nil {
61                         t.Fatal(err)
62                 }
63
64                 if !testutil.DeepEqual(utxo, gotUtxo) {
65                         t.Errorf("got utxo entry:%v, expect utxo entry:%v", gotUtxo, utxo)
66                 }
67         }
68 }
69
70 func TestSaveBlock(t *testing.T) {
71         testDB := dbm.NewDB("testdb", "leveldb", "temp")
72         defer func() {
73                 testDB.Close()
74                 os.RemoveAll("temp")
75         }()
76
77         store := NewStore(testDB)
78         block := mockGenesisBlock()
79         status := &bc.TransactionStatus{VerifyStatus: []*bc.TxVerifyResult{{StatusFail: true}}}
80         if err := store.SaveBlock(block, status); err != nil {
81                 t.Fatal(err)
82         }
83
84         blockHash := block.Hash()
85         gotBlock, err := store.GetBlock(&blockHash)
86         if err != nil {
87                 t.Fatal(err)
88         }
89
90         gotBlock.Transactions[0].Tx.SerializedSize = 0
91         gotBlock.Transactions[0].SerializedSize = 0
92         if !testutil.DeepEqual(block, gotBlock) {
93                 t.Errorf("got block:%v, expect block:%v", gotBlock, block)
94         }
95
96         gotStatus, err := store.GetTransactionStatus(&blockHash)
97         if err != nil {
98                 t.Fatal(err)
99         }
100
101         if !testutil.DeepEqual(status, gotStatus) {
102                 t.Errorf("got status:%v, expect status:%v", gotStatus, status)
103         }
104
105         data := store.db.Get(calcBlockHeaderKey(&blockHash))
106         gotBlockHeader := types.BlockHeader{}
107         if err := gotBlockHeader.UnmarshalText(data); err != nil {
108                 t.Fatal(err)
109         }
110
111         if !testutil.DeepEqual(block.BlockHeader, gotBlockHeader) {
112                 t.Errorf("got block header:%v, expect block header:%v", gotBlockHeader, block.BlockHeader)
113         }
114 }
115
116 func mockGenesisBlock() *types.Block {
117         txData := types.TxData{
118                 Version: 1,
119                 Inputs: []*types.TxInput{
120                         types.NewCoinbaseInput([]byte("Information is power. -- Jan/11/2013. Computing is power. -- Apr/24/2018.")),
121                 },
122                 Outputs: []*types.TxOutput{
123                         types.NewVoteOutput(bc.AssetID{V0: 1}, uint64(10000), []byte{0x51}, []byte{0x51}),
124                 },
125         }
126         tx := types.NewTx(txData)
127         txStatus := bc.NewTransactionStatus()
128         txStatus.SetStatus(0, false)
129         txStatusHash, _ := types.TxStatusMerkleRoot(txStatus.VerifyStatus)
130         merkleRoot, _ := types.TxMerkleRoot([]*bc.Tx{tx.Tx})
131         block := &types.Block{
132                 BlockHeader: types.BlockHeader{
133                         Version:   1,
134                         Height:    0,
135                         Timestamp: 1528945000,
136                         BlockCommitment: types.BlockCommitment{
137                                 TransactionsMerkleRoot: merkleRoot,
138                                 TransactionStatusHash:  txStatusHash,
139                         },
140                 },
141                 Transactions: []*types.Tx{tx},
142         }
143         return block
144 }