10 "github.com/bytom/consensus"
11 "github.com/bytom/protocol/bc"
12 "github.com/bytom/protocol/bc/types"
13 "github.com/bytom/protocol/vm"
14 dbm "github.com/bytom/database/leveldb"
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, _, _, _ := MockChain(db)
21 genesisHeader := chain.BestBlockHeader()
22 if err := AppendBlocks(chain, 1); err != nil {
29 prevHeight func() uint64
30 timestamp func() uint64
31 prevHash func() *bc.Hash
37 desc: "block version is 1",
38 version: func() uint64 { return 1 },
39 prevHeight: chain.BestBlockHeight,
40 timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
41 prevHash: chain.BestBlockHash,
42 bits: func() uint64 { return chain.BestBlockHeader().Bits },
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 bits: func() uint64 { return chain.BestBlockHeader().Bits },
57 desc: "invalid prev hash, prev hash dismatch",
58 version: func() uint64 { return chain.BestBlockHeader().Version },
59 prevHeight: chain.BestBlockHeight,
60 timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
61 prevHash: func() *bc.Hash { hash := genesisHeader.Hash(); return &hash },
62 bits: func() uint64 { return chain.BestBlockHeader().Bits },
68 version: func() uint64 { return chain.BestBlockHeader().Version },
69 prevHeight: chain.BestBlockHeight,
70 timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
71 prevHash: chain.BestBlockHash,
72 bits: func() uint64 { return chain.BestBlockHeader().Bits + 100 },
77 desc: "invalid timestamp, greater than MaxTimeOffsetSeconds from system time",
78 version: func() uint64 { return chain.BestBlockHeader().Version },
79 prevHeight: chain.BestBlockHeight,
80 timestamp: func() uint64 { return uint64(time.Now().Unix()) + consensus.MaxTimeOffsetSeconds + 60 },
81 prevHash: chain.BestBlockHash,
82 bits: func() uint64 { return chain.BestBlockHeader().Bits },
87 desc: "valid timestamp, greater than last block",
88 version: func() uint64 { return chain.BestBlockHeader().Version },
89 prevHeight: chain.BestBlockHeight,
90 timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp + 3 },
91 prevHash: chain.BestBlockHash,
92 bits: func() uint64 { return chain.BestBlockHeader().Bits },
97 desc: "valid timestamp, less than last block, but greater than median",
98 version: func() uint64 { return chain.BestBlockHeader().Version },
99 prevHeight: chain.BestBlockHeight,
100 timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp - 1 },
101 prevHash: chain.BestBlockHash,
102 bits: func() uint64 { return chain.BestBlockHeader().Bits },
107 desc: "invalid timestamp, less than median",
108 version: func() uint64 { return chain.BestBlockHeader().Version },
109 prevHeight: chain.BestBlockHeight,
110 timestamp: func() uint64 { return genesisHeader.Timestamp },
111 prevHash: chain.BestBlockHash,
112 bits: func() uint64 { return chain.BestBlockHeader().Bits },
118 for _, c := range cases {
119 block, err := NewBlock(chain, nil, []byte{byte(vm.OP_TRUE)})
124 block.Version = c.version()
125 block.Height = c.prevHeight() + 1
126 block.Timestamp = c.timestamp()
127 block.PreviousBlockHash = *c.prevHash()
128 block.Bits = c.bits()
129 seed, err := chain.CalcNextSeed(&block.PreviousBlockHash)
130 if err != nil && c.valid {
137 _, err = chain.ProcessBlock(block)
139 if result != c.valid {
140 t.Fatalf("%s test failed, expected: %t, have: %t, err: %s", c.desc, c.valid, result, err)
145 func TestMaxBlockGas(t *testing.T) {
146 chainDB := dbm.NewDB("test_block_db", "leveldb", "test_block_db")
147 defer os.RemoveAll("test_block_db")
148 chain, _, _, err := MockChain(chainDB)
153 if err := AppendBlocks(chain, 7); err != nil {
157 block, err := chain.GetBlockByHeight(1)
162 tx, err := CreateTxFromTx(block.Transactions[0], 0, 600000000000, []byte{byte(vm.OP_TRUE)})
167 outputAmount := uint64(600000000000)
168 txs := []*types.Tx{tx}
169 for i := 1; i < 50000; i++ {
170 outputAmount -= 10000000
171 tx, err := CreateTxFromTx(txs[i-1], 0, outputAmount, []byte{byte(vm.OP_TRUE)})
175 txs = append(txs, tx)
178 block, err = NewBlock(chain, txs, []byte{byte(vm.OP_TRUE)})
183 if err := SolveAndUpdate(chain, block); err == nil {
184 t.Fatalf("test max block gas failed")