OSDN Git Service

Merge pull request #41 from Bytom/dev
[bytom/vapor.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/vapor/consensus"
13         "github.com/vapor/protocol/bc"
14         "github.com/vapor/protocol/bc/types"
15         "github.com/vapor/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                 solve      bool
34                 valid      bool
35         }{
36                 {
37                         desc:       "block version is 0",
38                         version:    func() uint64 { return 0 },
39                         prevHeight: chain.BestBlockHeight,
40                         timestamp:  func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
41                         prevHash:   chain.BestBlockHash,
42                         solve:      true,
43                         valid:      false,
44                 },
45                 {
46                         desc:       "block version grater than prevBlock.Version",
47                         version:    func() uint64 { return chain.BestBlockHeader().Version + 10 },
48                         prevHeight: chain.BestBlockHeight,
49                         timestamp:  func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
50                         prevHash:   chain.BestBlockHash,
51                         solve:      true,
52                         valid:      true,
53                 },
54                 {
55                         desc:       "invalid block, misorder block height",
56                         version:    func() uint64 { return chain.BestBlockHeader().Version },
57                         prevHeight: func() uint64 { return chain.BestBlockHeight() + 1 },
58                         timestamp:  func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
59                         prevHash:   chain.BestBlockHash,
60                         solve:      true,
61                         valid:      false,
62                 },
63                 {
64                         desc:       "invalid prev hash, prev hash dismatch",
65                         version:    func() uint64 { return chain.BestBlockHeader().Version },
66                         prevHeight: chain.BestBlockHeight,
67                         timestamp:  func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
68                         prevHash:   func() *bc.Hash { hash := genesisHeader.Hash(); return &hash },
69                         solve:      true,
70                         valid:      false,
71                 },
72                 {
73                         desc:       "invalid timestamp, greater than MaxTimeOffsetSeconds from system time",
74                         version:    func() uint64 { return chain.BestBlockHeader().Version },
75                         prevHeight: chain.BestBlockHeight,
76                         timestamp:  func() uint64 { return uint64(time.Now().Unix()) + consensus.MaxTimeOffsetSeconds + 60 },
77                         prevHash:   chain.BestBlockHash,
78                         solve:      true,
79                         valid:      false,
80                 },
81                 {
82                         desc:       "valid timestamp, greater than last block",
83                         version:    func() uint64 { return chain.BestBlockHeader().Version },
84                         prevHeight: chain.BestBlockHeight,
85                         timestamp:  func() uint64 { return chain.BestBlockHeader().Timestamp + 3 },
86                         prevHash:   chain.BestBlockHash,
87                         solve:      true,
88                         valid:      true,
89                 },
90                 {
91                         desc:       "valid timestamp, less than last block, but greater than median",
92                         version:    func() uint64 { return chain.BestBlockHeader().Version },
93                         prevHeight: chain.BestBlockHeight,
94                         timestamp:  func() uint64 { return chain.BestBlockHeader().Timestamp - 1 },
95                         prevHash:   chain.BestBlockHash,
96                         solve:      true,
97                         valid:      true,
98                 },
99                 {
100                         desc:       "invalid timestamp, less than median",
101                         version:    func() uint64 { return chain.BestBlockHeader().Version },
102                         prevHeight: chain.BestBlockHeight,
103                         timestamp:  func() uint64 { return genesisHeader.Timestamp },
104                         prevHash:   chain.BestBlockHash,
105                         solve:      true,
106                         valid:      false,
107                 },
108         }
109
110         for _, c := range cases {
111                 block, err := NewBlock(chain, nil, []byte{byte(vm.OP_TRUE)})
112                 if err != nil {
113                         t.Fatal(err)
114                 }
115
116                 block.Version = c.version()
117                 block.Height = c.prevHeight() + 1
118                 block.Timestamp = c.timestamp()
119                 block.PreviousBlockHash = *c.prevHash()
120
121                 _, err = chain.ProcessBlock(block)
122                 result := err == nil
123                 if result != c.valid {
124                         t.Fatalf("%s test failed, expected: %t, have: %t, err: %s", c.desc, c.valid, result, err)
125                 }
126         }
127 }
128
129 func TestMaxBlockGas(t *testing.T) {
130         chainDB := dbm.NewDB("test_block_db", "leveldb", "test_block_db")
131         defer os.RemoveAll("test_block_db")
132         chain, _, _, err := MockChain(chainDB)
133         if err != nil {
134                 t.Fatal(err)
135         }
136
137         if err := AppendBlocks(chain, 7); err != nil {
138                 t.Fatal(err)
139         }
140
141         block, err := chain.GetBlockByHeight(1)
142         if err != nil {
143                 t.Fatal(err)
144         }
145
146         tx, err := CreateTxFromTx(block.Transactions[0], 0, 600000000000, []byte{byte(vm.OP_TRUE)})
147         if err != nil {
148                 t.Fatal(err)
149         }
150
151         outputAmount := uint64(600000000000)
152         txs := []*types.Tx{tx}
153         for i := 1; i < 50000; i++ {
154                 outputAmount -= 10000000
155                 tx, err := CreateTxFromTx(txs[i-1], 0, outputAmount, []byte{byte(vm.OP_TRUE)})
156                 if err != nil {
157                         t.Fatal(err)
158                 }
159                 txs = append(txs, tx)
160         }
161
162         block, err = NewBlock(chain, txs, []byte{byte(vm.OP_TRUE)})
163         if err != nil {
164                 t.Fatal(err)
165         }
166
167         if err := SolveAndUpdate(chain, block); err == nil {
168                 t.Fatalf("test max block gas failed")
169         }
170 }