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 0",
39 version: func() uint64 { return 0 },
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: "block version grater than prevBlock.Version",
49 version: func() uint64 { return chain.BestBlockHeader().Version + 10 },
50 prevHeight: chain.BestBlockHeight,
51 timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
52 prevHash: chain.BestBlockHash,
53 bits: func() uint64 { return chain.BestBlockHeader().Bits },
58 desc: "invalid block, misorder block height",
59 version: func() uint64 { return chain.BestBlockHeader().Version },
60 prevHeight: func() uint64 { return chain.BestBlockHeight() + 1 },
61 timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
62 prevHash: chain.BestBlockHash,
63 bits: func() uint64 { return chain.BestBlockHeader().Bits },
68 desc: "invalid prev hash, prev hash dismatch",
69 version: func() uint64 { return chain.BestBlockHeader().Version },
70 prevHeight: chain.BestBlockHeight,
71 timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
72 prevHash: func() *bc.Hash { hash := genesisHeader.Hash(); return &hash },
73 bits: func() uint64 { return chain.BestBlockHeader().Bits },
79 version: func() uint64 { return chain.BestBlockHeader().Version },
80 prevHeight: chain.BestBlockHeight,
81 timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
82 prevHash: chain.BestBlockHash,
83 bits: func() uint64 { return chain.BestBlockHeader().Bits + 100 },
88 desc: "invalid timestamp, greater than MaxTimeOffsetSeconds from system time",
89 version: func() uint64 { return chain.BestBlockHeader().Version },
90 prevHeight: chain.BestBlockHeight,
91 timestamp: func() uint64 { return uint64(time.Now().Unix()) + consensus.MaxTimeOffsetSeconds + 60 },
92 prevHash: chain.BestBlockHash,
93 bits: func() uint64 { return chain.BestBlockHeader().Bits },
98 desc: "valid timestamp, greater than last block",
99 version: func() uint64 { return chain.BestBlockHeader().Version },
100 prevHeight: chain.BestBlockHeight,
101 timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp + 3 },
102 prevHash: chain.BestBlockHash,
103 bits: func() uint64 { return chain.BestBlockHeader().Bits },
108 desc: "valid timestamp, less than last block, but greater than median",
109 version: func() uint64 { return chain.BestBlockHeader().Version },
110 prevHeight: chain.BestBlockHeight,
111 timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp - 1 },
112 prevHash: chain.BestBlockHash,
113 bits: func() uint64 { return chain.BestBlockHeader().Bits },
118 desc: "invalid timestamp, less than median",
119 version: func() uint64 { return chain.BestBlockHeader().Version },
120 prevHeight: chain.BestBlockHeight,
121 timestamp: func() uint64 { return genesisHeader.Timestamp },
122 prevHash: chain.BestBlockHash,
123 bits: func() uint64 { return chain.BestBlockHeader().Bits },
129 for _, c := range cases {
130 block, err := NewBlock(chain, nil, []byte{byte(vm.OP_TRUE)})
135 block.Version = c.version()
136 block.Height = c.prevHeight() + 1
137 block.Timestamp = c.timestamp()
138 block.PreviousBlockHash = *c.prevHash()
139 block.Bits = c.bits()
140 seed, err := chain.CalcNextSeed(&block.PreviousBlockHash)
141 if err != nil && c.valid {
148 _, err = chain.ProcessBlock(block)
150 if result != c.valid {
151 t.Fatalf("%s test failed, expected: %t, have: %t, err: %s", c.desc, c.valid, result, err)
156 func TestMaxBlockGas(t *testing.T) {
157 chainDB := dbm.NewDB("test_block_db", "leveldb", "test_block_db")
158 defer os.RemoveAll("test_block_db")
159 chain, _, _, err := MockChain(chainDB)
164 if err := AppendBlocks(chain, 7); err != nil {
168 block, err := chain.GetBlockByHeight(1)
173 tx, err := CreateTxFromTx(block.Transactions[0], 0, 600000000000, []byte{byte(vm.OP_TRUE)})
178 outputAmount := uint64(600000000000)
179 txs := []*types.Tx{tx}
180 for i := 1; i < 50000; i++ {
181 outputAmount -= 10000000
182 tx, err := CreateTxFromTx(txs[i-1], 0, outputAmount, []byte{byte(vm.OP_TRUE)})
186 txs = append(txs, tx)
189 block, err = NewBlock(chain, txs, []byte{byte(vm.OP_TRUE)})
194 if err := SolveAndUpdate(chain, block); err == nil {
195 t.Fatalf("test max block gas failed")