OSDN Git Service

fix log (#388)
[bytom/vapor.git] / database / store_test.go
1 package database
2
3 import (
4         "os"
5         "testing"
6
7         "github.com/vapor/consensus"
8         dbm "github.com/vapor/database/leveldb"
9         "github.com/vapor/database/storage"
10         "github.com/vapor/protocol"
11         "github.com/vapor/protocol/bc"
12         "github.com/vapor/protocol/bc/types"
13         "github.com/vapor/protocol/state"
14         "github.com/vapor/testutil"
15 )
16
17 func TestSaveChainStatus(t *testing.T) {
18         testDB := dbm.NewDB("testdb", "leveldb", "temp")
19         defer func() {
20                 testDB.Close()
21                 os.RemoveAll("temp")
22         }()
23
24         store := NewStore(testDB)
25
26         blockHeader := &types.BlockHeader{Height: 100}
27         blockHash := blockHeader.Hash() //Hash: bc.Hash{V0: 0, V1: 1, V2: 2, V3: 3}
28         view := &state.UtxoViewpoint{
29                 Entries: map[bc.Hash]*storage.UtxoEntry{
30                         bc.Hash{V0: 1, V1: 2, V2: 3, V3: 4}: &storage.UtxoEntry{Type: storage.NormalUTXOType, BlockHeight: 100, Spent: false},
31                         bc.Hash{V0: 1, V1: 2, V2: 3, V3: 4}: &storage.UtxoEntry{Type: storage.CoinbaseUTXOType, BlockHeight: 100, Spent: true},
32                         bc.Hash{V0: 1, V1: 1, V2: 3, V3: 4}: &storage.UtxoEntry{Type: storage.NormalUTXOType, BlockHeight: 100, Spent: true},
33                         bc.Hash{V0: 1, V1: 1, V2: 3, V3: 5}: &storage.UtxoEntry{Type: storage.CrosschainUTXOType, BlockHeight: 100, Spent: false},
34                         bc.Hash{V0: 1, V1: 1, V2: 3, V3: 6}: &storage.UtxoEntry{Type: storage.CrosschainUTXOType, BlockHeight: 100, Spent: true},
35                         bc.Hash{V0: 1, V1: 3, V2: 3, V3: 7}: &storage.UtxoEntry{Type: storage.VoteUTXOType, BlockHeight: 100, Spent: false},
36                         bc.Hash{V0: 1, V1: 3, V2: 3, V3: 7}: &storage.UtxoEntry{Type: storage.VoteUTXOType, BlockHeight: 100, Spent: true},
37                 },
38         }
39
40         if err := store.SaveChainStatus(blockHeader, blockHeader, []*types.BlockHeader{blockHeader}, view, []*state.ConsensusResult{}); err != nil {
41                 t.Fatal(err)
42         }
43
44         expectStatus := &protocol.BlockStoreState{Height: blockHeader.Height, Hash: &blockHash, IrreversibleHeight: blockHeader.Height, IrreversibleHash: &blockHash}
45         if !testutil.DeepEqual(store.GetStoreStatus(), expectStatus) {
46                 t.Errorf("got block status:%v, expect block status:%v", store.GetStoreStatus(), expectStatus)
47         }
48
49         for hash, utxo := range view.Entries {
50                 if (utxo.Type == storage.NormalUTXOType) && utxo.Spent {
51                         continue
52                 }
53                 if (utxo.Type == storage.CrosschainUTXOType) && (!utxo.Spent) {
54                         continue
55                 }
56                 if (utxo.Type == storage.VoteUTXOType) && (utxo.Spent) {
57                         continue
58                 }
59
60                 gotUtxo, err := store.GetUtxo(&hash)
61                 if err != nil {
62                         t.Fatal(err)
63                 }
64
65                 if !testutil.DeepEqual(utxo, gotUtxo) {
66                         t.Errorf("got utxo entry:%v, expect utxo entry:%v", gotUtxo, utxo)
67                 }
68         }
69 }
70
71 func TestSaveBlock(t *testing.T) {
72         testDB := dbm.NewDB("testdb", "leveldb", "temp")
73         defer func() {
74                 testDB.Close()
75                 os.RemoveAll("temp")
76         }()
77
78         store := NewStore(testDB)
79         coinbaseTxData := &types.TxData{
80                 Version: 1,
81                 Inputs: []*types.TxInput{
82                         types.NewCoinbaseInput([]byte("Information is power. -- Jan/11/2013. Computing is power. -- Apr/24/2018.")),
83                 },
84                 Outputs: []*types.TxOutput{
85                         types.NewVoteOutput(*consensus.BTMAssetID, uint64(10000), []byte{0x51}, []byte{0x51}),
86                 },
87         }
88         coinbaseTx := types.NewTx(*coinbaseTxData)
89
90         cases := []struct {
91                 txData      []*types.TxData
92                 txStatus    *bc.TransactionStatus
93                 blockHeader *types.BlockHeader
94         }{
95                 {
96                         txStatus: &bc.TransactionStatus{
97                                 VerifyStatus: []*bc.TxVerifyResult{
98                                         {StatusFail: true},
99                                 },
100                         },
101                         blockHeader: &types.BlockHeader{
102                                 Version:   uint64(1),
103                                 Height:    uint64(1111),
104                                 Timestamp: uint64(1528945000),
105                         },
106                 },
107                 {
108                         txStatus: &bc.TransactionStatus{
109                                 VerifyStatus: []*bc.TxVerifyResult{
110                                         {StatusFail: false},
111                                 },
112                         },
113                         blockHeader: &types.BlockHeader{
114                                 Version:   uint64(1),
115                                 Height:    uint64(1111),
116                                 Timestamp: uint64(1528945000),
117                         },
118                 },
119                 {
120                         txData: []*types.TxData{
121                                 {
122                                         Version: 1,
123                                         Inputs: []*types.TxInput{
124                                                 types.NewSpendInput([][]byte{}, bc.NewHash([32]byte{}), *consensus.BTMAssetID, 100000000, 0, []byte{0x51}),
125                                         },
126                                         Outputs: []*types.TxOutput{
127                                                 types.NewVoteOutput(*consensus.BTMAssetID, uint64(10000), []byte{0x51}, []byte{0x51}),
128                                         },
129                                 },
130                         },
131                         txStatus: &bc.TransactionStatus{
132                                 VerifyStatus: []*bc.TxVerifyResult{
133                                         {StatusFail: true},
134                                 },
135                         },
136                         blockHeader: &types.BlockHeader{
137                                 Version:   uint64(1),
138                                 Height:    uint64(1111),
139                                 Timestamp: uint64(1528945000),
140                         },
141                 },
142                 {
143                         txData: []*types.TxData{
144                                 {
145                                         Version: 1,
146                                         Inputs: []*types.TxInput{
147                                                 types.NewSpendInput([][]byte{}, bc.NewHash([32]byte{}), *consensus.BTMAssetID, 100000000, 0, []byte{0x51}),
148                                         },
149                                         Outputs: []*types.TxOutput{
150                                                 types.NewVoteOutput(*consensus.BTMAssetID, uint64(88888), []byte{0x51}, []byte{0x51}),
151                                         },
152                                 },
153                         },
154                         txStatus: &bc.TransactionStatus{
155                                 VerifyStatus: []*bc.TxVerifyResult{
156                                         {StatusFail: false},
157                                 },
158                         },
159                         blockHeader: &types.BlockHeader{
160                                 Version:   uint64(1),
161                                 Height:    uint64(0),
162                                 Timestamp: uint64(152894500000),
163                         },
164                 },
165         }
166
167         for i, c := range cases {
168                 txs := []*bc.Tx{coinbaseTx.Tx}
169                 for _, tx := range c.txData {
170                         t := types.NewTx(*tx)
171                         txs = append(txs, t.Tx)
172                 }
173                 merkleRoot, _ := types.TxMerkleRoot(txs)
174                 txStatusHash, _ := types.TxStatusMerkleRoot(c.txStatus.VerifyStatus)
175                 block := &types.Block{
176                         BlockHeader: types.BlockHeader{
177                                 Version:   c.blockHeader.Version,
178                                 Height:    c.blockHeader.Height,
179                                 Timestamp: c.blockHeader.Timestamp,
180                                 BlockCommitment: types.BlockCommitment{
181                                         TransactionsMerkleRoot: merkleRoot,
182                                         TransactionStatusHash:  txStatusHash,
183                                 },
184                         },
185                 }
186
187                 if err := store.SaveBlock(block, c.txStatus); err != nil {
188                         t.Fatal(err)
189                 }
190
191                 blockHash := block.Hash()
192                 gotBlock, err := store.GetBlock(&blockHash)
193                 if err != nil {
194                         t.Fatal(err)
195                 }
196
197                 if !testutil.DeepEqual(gotBlock, block) {
198                         t.Errorf("case %v: block mismatch: have %x, want %x", i, gotBlock, block)
199                 }
200
201                 gotStatus, err := store.GetTransactionStatus(&blockHash)
202                 if err != nil {
203                         t.Fatal(err)
204                 }
205
206                 if !testutil.DeepEqual(gotStatus.VerifyStatus, c.txStatus.VerifyStatus) {
207                         t.Errorf("case %v: VerifyStatus mismatch: have %x, want %x", i, gotStatus.VerifyStatus, c.txStatus.VerifyStatus)
208                 }
209
210                 gotBlockHeader, err := store.GetBlockHeader(&blockHash)
211                 if err != nil {
212                         t.Fatal(err)
213                 }
214
215                 if !testutil.DeepEqual(block.BlockHeader, *gotBlockHeader) {
216                         t.Errorf("got block header:%v, expect block header:%v", gotBlockHeader, block.BlockHeader)
217                 }
218         }
219 }
220
221 func TestSaveBlockHeader(t *testing.T) {
222         testDB := dbm.NewDB("testdb", "leveldb", "temp")
223         defer func() {
224                 testDB.Close()
225                 os.RemoveAll("temp")
226         }()
227
228         store := NewStore(testDB)
229
230         cases := []struct {
231                 blockHeader *types.BlockHeader
232         }{
233                 {
234                         blockHeader: &types.BlockHeader{
235                                 Version:   uint64(1),
236                                 Height:    uint64(1111),
237                                 Timestamp: uint64(1528945000),
238                         },
239                 },
240                 {
241                         blockHeader: &types.BlockHeader{
242                                 Version:           uint64(1),
243                                 Height:            uint64(0),
244                                 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}),
245                                 Timestamp:         uint64(1563186936),
246                                 BlockCommitment: types.BlockCommitment{
247                                         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}),
248                                         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}),
249                                 },
250                                 BlockWitness: types.BlockWitness{
251                                         Witness: [][]byte{[]byte{0x3e, 0x94, 0x5d, 0x35}, []byte{0x3e, 0x94, 0x5d, 0x35}},
252                                 },
253                         },
254                 },
255                 {
256                         blockHeader: &types.BlockHeader{
257                                 Version:           uint64(1),
258                                 Height:            uint64(8848),
259                                 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}),
260                                 Timestamp:         uint64(156318693600),
261                                 BlockCommitment: types.BlockCommitment{
262                                         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}),
263                                         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}),
264                                 },
265                                 BlockWitness: types.BlockWitness{
266                                         Witness: [][]byte{
267                                                 []byte{0x3e, 0x94, 0x5d, 0x35},
268                                                 []byte{0xdd, 0x80, 0x67, 0x29},
269                                                 []byte{0xff, 0xff, 0xff, 0xff},
270                                                 []byte{0x00, 0x01, 0x02, 0x03},
271                                         },
272                                 },
273                         },
274                 },
275         }
276
277         for i, c := range cases {
278                 if err := store.SaveBlockHeader(c.blockHeader); err != nil {
279                         t.Fatal(err)
280                 }
281
282                 blockHash := c.blockHeader.Hash()
283                 gotBlockHeader, err := store.GetBlockHeader(&blockHash)
284                 if err != nil {
285                         t.Fatal(err)
286                 }
287
288                 if !testutil.DeepEqual(gotBlockHeader, c.blockHeader) {
289                         t.Errorf("case %v: block header mismatch: have %x, want %x", i, gotBlockHeader, c.blockHeader)
290                 }
291         }
292 }