10 dbm "github.com/tendermint/tmlibs/db"
12 "github.com/bytom/consensus"
13 "github.com/bytom/protocol/bc"
14 "github.com/bytom/protocol/bc/types"
15 "github.com/bytom/protocol/vm"
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 {
30 prevHeight func() uint64
31 timestamp func() uint64
32 prevHash func() *bc.Hash
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 },
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 },
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 },
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 },
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 },
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 },
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 },
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 },
119 for _, c := range cases {
120 block, err := NewBlock(chain, nil, []byte{byte(vm.OP_TRUE)})
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 {
138 _, err = chain.ProcessBlock(block)
140 if result != c.valid {
141 t.Fatalf("%s test failed, expected: %t, have: %t, err: %s", c.desc, c.valid, result, err)
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)
154 if err := AppendBlocks(chain, 7); err != nil {
158 block, err := chain.GetBlockByHeight(1)
163 tx, err := CreateTxFromTx(block.Transactions[0], 0, 600000000000, []byte{byte(vm.OP_TRUE)})
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)})
176 txs = append(txs, tx)
179 block, err = NewBlock(chain, txs, []byte{byte(vm.OP_TRUE)})
184 if err := SolveAndUpdate(chain, block); err == nil {
185 t.Fatalf("test max block gas failed")