10 "github.com/bytom/bytom/consensus"
11 dbm "github.com/bytom/bytom/database/leveldb"
12 "github.com/bytom/bytom/protocol/bc"
13 "github.com/bytom/bytom/protocol/bc/types"
14 "github.com/bytom/bytom/protocol/vm"
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
35 desc: "block version is 1",
36 version: func() uint64 { return 1 },
37 prevHeight: chain.BestBlockHeight,
38 timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
39 prevHash: chain.BestBlockHash,
43 desc: "invalid block, misorder block height",
44 version: func() uint64 { return chain.BestBlockHeader().Version },
45 prevHeight: func() uint64 { return chain.BestBlockHeight() + 1 },
46 timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
47 prevHash: chain.BestBlockHash,
51 desc: "invalid prev hash, prev hash dismatch",
52 version: func() uint64 { return chain.BestBlockHeader().Version },
53 prevHeight: chain.BestBlockHeight,
54 timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
55 prevHash: func() *bc.Hash { hash := genesisHeader.Hash(); return &hash },
59 desc: "valid timestamp, greater than last block",
60 version: func() uint64 { return chain.BestBlockHeader().Version },
61 prevHeight: chain.BestBlockHeight,
62 timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp + 3 },
63 prevHash: chain.BestBlockHash,
67 desc: "valid timestamp, less than last block, but greater than median",
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,
75 desc: "invalid timestamp, less than median",
76 version: func() uint64 { return chain.BestBlockHeader().Version },
77 prevHeight: chain.BestBlockHeight,
78 timestamp: func() uint64 { return genesisHeader.Timestamp },
79 prevHash: chain.BestBlockHash,
84 for _, c := range cases {
85 block, err := NewBlock(chain, nil, []byte{byte(vm.OP_TRUE)})
90 block.Version = c.version()
91 block.Height = c.prevHeight() + 1
92 block.Timestamp = c.timestamp()
93 block.PreviousBlockHash = *c.prevHash()
94 if err != nil && c.valid {
98 _, err = chain.ProcessBlock(block)
100 if result != c.valid {
101 t.Fatalf("%s test failed, expected: %t, have: %t, err: %s", c.desc, c.valid, result, err)
106 func TestMaxBlockGas(t *testing.T) {
107 chainDB := dbm.NewDB("test_block_db", "leveldb", "test_block_db")
108 defer os.RemoveAll("test_block_db")
109 chain, _, _, err := MockChain(chainDB)
114 if err := AppendBlocks(chain, 7); err != nil {
118 block, err := chain.GetBlockByHeight(1)
123 tx, err := CreateTxFromTx(block.Transactions[0], 0, 600000000000, []byte{byte(vm.OP_TRUE)})
128 outputAmount := uint64(600000000000)
129 txs := []*types.Tx{tx}
130 for i := 1; i < 50000; i++ {
131 outputAmount -= 10000000
132 tx, err := CreateTxFromTx(txs[i-1], 0, outputAmount, []byte{byte(vm.OP_TRUE)})
136 txs = append(txs, tx)
139 block, err = NewBlock(chain, txs, []byte{byte(vm.OP_TRUE)})
144 if _, err := chain.ProcessBlock(block); err == nil {
145 t.Fatalf("test max block gas failed")