OSDN Git Service

da37b5ad1dd15571ae364093a5ec4c07ffa79d2d
[bytom/bytom.git] / test / integration / block_integration_test.go
1 package integration
2
3 import (
4         "testing"
5
6         "github.com/bytom/config"
7         "github.com/bytom/database"
8         "github.com/bytom/database/storage"
9         "github.com/bytom/protocol"
10         "github.com/bytom/protocol/bc"
11         "github.com/bytom/protocol/bc/types"
12         "github.com/bytom/protocol/state"
13 )
14
15 func TestProcessBlock(t *testing.T) {
16         gensisBlock := config.GenesisBlock()
17         genesisBlockHash := gensisBlock.Hash()
18         fillTransactionSize(gensisBlock)
19
20         cases := []*processBlockTestCase{
21                 {
22                         desc: "process a invalid block",
23                         newBlock: &types.Block{
24                                 BlockHeader: types.BlockHeader{
25                                         Height:            1,
26                                         Version:           1,
27                                         PreviousBlockHash: genesisBlockHash,
28                                 },
29                         },
30                         wantStore: storeItems{
31                                 {
32                                         key: database.BlockStoreKey,
33                                         val: &protocol.BlockStoreState{Height: 0, Hash: &genesisBlockHash},
34                                 },
35                                 {
36                                         key: database.CalcBlockKey(&genesisBlockHash),
37                                         val: gensisBlock,
38                                 },
39                                 {
40                                         key: database.CalcTxStatusKey(&genesisBlockHash),
41                                         val: &bc.TransactionStatus{Version: 1, VerifyStatus: []*bc.TxVerifyResult{{StatusFail: false}}},
42                                 },
43                                 {
44                                         key: database.CalcBlockHeaderKey(gensisBlock.Height, &genesisBlockHash),
45                                         val: gensisBlock.BlockHeader,
46                                 },
47                                 {
48                                         key: database.CalcUtxoKey(gensisBlock.Transactions[0].Tx.ResultIds[0]),
49                                         val: &storage.UtxoEntry{IsCoinBase: true, BlockHeight: 0, Spent: false},
50                                 },
51                         },
52                         wantBlockIndex: state.NewBlockIndexWithInitData(
53                                 map[bc.Hash]*state.BlockNode{
54                                         genesisBlockHash: mustNewBlockNode(&gensisBlock.BlockHeader, nil),
55                                 },
56                                 []*state.BlockNode{
57                                         mustNewBlockNode(&gensisBlock.BlockHeader, nil),
58                                 },
59                         ),
60                         wantOrphanManage: protocol.NewOrphanManage(),
61                         wantError:        true,
62                 },
63         }
64
65         for _, c := range cases {
66                 if err := c.Run(); err != nil {
67                         panic(err)
68                 }
69         }
70 }
71
72 func mustNewBlockNode(h *types.BlockHeader, parent *state.BlockNode) *state.BlockNode {
73         node, err := state.NewBlockNode(h, parent)
74         if err != nil {
75                 panic(err)
76         }
77         return node
78 }
79
80 func fillTransactionSize(block *types.Block) {
81         for _, tx := range block.Transactions {
82                 bytes, err := tx.MarshalText()
83                 if err != nil {
84                         panic(err)
85                 }
86                 tx.TxData.SerializedSize = uint64(len(bytes) / 2)
87                 tx.Tx.SerializedSize = uint64(len(bytes) / 2)
88         }
89 }