OSDN Git Service

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