OSDN Git Service

add tx unit test
[bytom/bytom.git] / test / block_test.go
1 // +build functional
2
3 package test
4
5 import (
6         "os"
7         "testing"
8         "time"
9
10         dbm "github.com/tendermint/tmlibs/db"
11
12         "github.com/bytom/consensus"
13         "github.com/bytom/protocol/bc"
14         "github.com/bytom/protocol/bc/types"
15         "github.com/bytom/protocol/vm"
16 )
17
18 func TestBlockHeader(t *testing.T) {
19         db := dbm.NewDB("block_test_db", "leveldb", "block_test_db")
20         defer os.RemoveAll("block_test_db")
21         chain, _, _, _ := MockChain(db)
22         genesisHeader := chain.BestBlockHeader()
23         if err := AppendBlocks(chain, 1); err != nil {
24                 t.Fatal(err)
25         }
26
27         cases := []struct {
28                 desc       string
29                 version    func() uint64
30                 prevHeight func() uint64
31                 timestamp  func() uint64
32                 prevHash   func() *bc.Hash
33                 bits       func() uint64
34                 solve      bool
35                 valid      bool
36         }{
37                 {
38                         desc:       "block version is 1",
39                         version:    func() uint64 { return 1 },
40                         prevHeight: chain.BestBlockHeight,
41                         timestamp:  func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
42                         prevHash:   chain.BestBlockHash,
43                         bits:       func() uint64 { return chain.BestBlockHeader().Bits },
44                         solve:      true,
45                         valid:      true,
46                 },
47                 {
48                         desc:       "invalid block, misorder block height",
49                         version:    func() uint64 { return chain.BestBlockHeader().Version },
50                         prevHeight: func() uint64 { return chain.BestBlockHeight() + 1 },
51                         timestamp:  func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
52                         prevHash:   chain.BestBlockHash,
53                         bits:       func() uint64 { return chain.BestBlockHeader().Bits },
54                         solve:      true,
55                         valid:      false,
56                 },
57                 {
58                         desc:       "invalid prev hash, prev hash dismatch",
59                         version:    func() uint64 { return chain.BestBlockHeader().Version },
60                         prevHeight: chain.BestBlockHeight,
61                         timestamp:  func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
62                         prevHash:   func() *bc.Hash { hash := genesisHeader.Hash(); return &hash },
63                         bits:       func() uint64 { return chain.BestBlockHeader().Bits },
64                         solve:      true,
65                         valid:      false,
66                 },
67                 {
68                         desc:       "invalid bits",
69                         version:    func() uint64 { return chain.BestBlockHeader().Version },
70                         prevHeight: chain.BestBlockHeight,
71                         timestamp:  func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
72                         prevHash:   chain.BestBlockHash,
73                         bits:       func() uint64 { return chain.BestBlockHeader().Bits + 100 },
74                         solve:      true,
75                         valid:      false,
76                 },
77                 {
78                         desc:       "invalid timestamp, greater than MaxTimeOffsetSeconds from system time",
79                         version:    func() uint64 { return chain.BestBlockHeader().Version },
80                         prevHeight: chain.BestBlockHeight,
81                         timestamp:  func() uint64 { return uint64(time.Now().Unix()) + consensus.MaxTimeOffsetSeconds + 60 },
82                         prevHash:   chain.BestBlockHash,
83                         bits:       func() uint64 { return chain.BestBlockHeader().Bits },
84                         solve:      true,
85                         valid:      false,
86                 },
87                 {
88                         desc:       "valid timestamp, greater than last block",
89                         version:    func() uint64 { return chain.BestBlockHeader().Version },
90                         prevHeight: chain.BestBlockHeight,
91                         timestamp:  func() uint64 { return chain.BestBlockHeader().Timestamp + 3 },
92                         prevHash:   chain.BestBlockHash,
93                         bits:       func() uint64 { return chain.BestBlockHeader().Bits },
94                         solve:      true,
95                         valid:      true,
96                 },
97                 {
98                         desc:       "valid timestamp, less than last block, but greater than median",
99                         version:    func() uint64 { return chain.BestBlockHeader().Version },
100                         prevHeight: chain.BestBlockHeight,
101                         timestamp:  func() uint64 { return chain.BestBlockHeader().Timestamp - 1 },
102                         prevHash:   chain.BestBlockHash,
103                         bits:       func() uint64 { return chain.BestBlockHeader().Bits },
104                         solve:      true,
105                         valid:      true,
106                 },
107                 {
108                         desc:       "invalid timestamp, less than median",
109                         version:    func() uint64 { return chain.BestBlockHeader().Version },
110                         prevHeight: chain.BestBlockHeight,
111                         timestamp:  func() uint64 { return genesisHeader.Timestamp },
112                         prevHash:   chain.BestBlockHash,
113                         bits:       func() uint64 { return chain.BestBlockHeader().Bits },
114                         solve:      true,
115                         valid:      false,
116                 },
117         }
118
119         for _, c := range cases {
120                 block, err := NewBlock(chain, nil, []byte{byte(vm.OP_TRUE)})
121                 if err != nil {
122                         t.Fatal(err)
123                 }
124
125                 block.Version = c.version()
126                 block.Height = c.prevHeight() + 1
127                 block.Timestamp = c.timestamp()
128                 block.PreviousBlockHash = *c.prevHash()
129                 block.Bits = c.bits()
130                 seed, err := chain.CalcNextSeed(&block.PreviousBlockHash)
131                 if err != nil && c.valid {
132                         t.Fatal(err)
133                 }
134
135                 if c.solve {
136                         Solve(seed, block)
137                 }
138                 _, err = chain.ProcessBlock(block)
139                 result := err == nil
140                 if result != c.valid {
141                         t.Fatalf("%s test failed, expected: %t, have: %t, err: %s", c.desc, c.valid, result, err)
142                 }
143         }
144 }
145
146 func TestMaxBlockGas(t *testing.T) {
147         chainDB := dbm.NewDB("test_block_db", "leveldb", "test_block_db")
148         defer os.RemoveAll("test_block_db")
149         chain, _, _, err := MockChain(chainDB)
150         if err != nil {
151                 t.Fatal(err)
152         }
153
154         if err := AppendBlocks(chain, 7); err != nil {
155                 t.Fatal(err)
156         }
157
158         block, err := chain.GetBlockByHeight(1)
159         if err != nil {
160                 t.Fatal(err)
161         }
162
163         tx, err := CreateTxFromTx(block.Transactions[0], 0, 600000000000, []byte{byte(vm.OP_TRUE)})
164         if err != nil {
165                 t.Fatal(err)
166         }
167
168         outputAmount := uint64(600000000000)
169         txs := []*types.Tx{tx}
170         for i := 1; i < 50000; i++ {
171                 outputAmount -= 10000000
172                 tx, err := CreateTxFromTx(txs[i-1], 0, outputAmount, []byte{byte(vm.OP_TRUE)})
173                 if err != nil {
174                         t.Fatal(err)
175                 }
176                 txs = append(txs, tx)
177         }
178
179         block, err = NewBlock(chain, txs, []byte{byte(vm.OP_TRUE)})
180         if err != nil {
181                 t.Fatal(err)
182         }
183
184         if err := SolveAndUpdate(chain, block); err == nil {
185                 t.Fatalf("test max block gas failed")
186         }
187 }