10 dbm "github.com/tendermint/tmlibs/db"
12 "github.com/vapor/consensus"
13 "github.com/vapor/protocol/bc"
14 "github.com/vapor/protocol/bc/types"
15 "github.com/vapor/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
37 desc: "block version is 0",
38 version: func() uint64 { return 0 },
39 prevHeight: chain.BestBlockHeight,
40 timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
41 prevHash: chain.BestBlockHash,
46 desc: "block version grater than prevBlock.Version",
47 version: func() uint64 { return chain.BestBlockHeader().Version + 10 },
48 prevHeight: chain.BestBlockHeight,
49 timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
50 prevHash: chain.BestBlockHash,
55 desc: "invalid block, misorder block height",
56 version: func() uint64 { return chain.BestBlockHeader().Version },
57 prevHeight: func() uint64 { return chain.BestBlockHeight() + 1 },
58 timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
59 prevHash: chain.BestBlockHash,
64 desc: "invalid prev hash, prev hash dismatch",
65 version: func() uint64 { return chain.BestBlockHeader().Version },
66 prevHeight: chain.BestBlockHeight,
67 timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
68 prevHash: func() *bc.Hash { hash := genesisHeader.Hash(); return &hash },
73 desc: "invalid timestamp, greater than MaxTimeOffsetSeconds from system time",
74 version: func() uint64 { return chain.BestBlockHeader().Version },
75 prevHeight: chain.BestBlockHeight,
76 timestamp: func() uint64 { return uint64(time.Now().Unix()) + consensus.MaxTimeOffsetSeconds + 60 },
77 prevHash: chain.BestBlockHash,
82 desc: "valid timestamp, greater than last block",
83 version: func() uint64 { return chain.BestBlockHeader().Version },
84 prevHeight: chain.BestBlockHeight,
85 timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp + 3 },
86 prevHash: chain.BestBlockHash,
91 desc: "valid timestamp, less than last block, but greater than median",
92 version: func() uint64 { return chain.BestBlockHeader().Version },
93 prevHeight: chain.BestBlockHeight,
94 timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp - 1 },
95 prevHash: chain.BestBlockHash,
100 desc: "invalid timestamp, less than median",
101 version: func() uint64 { return chain.BestBlockHeader().Version },
102 prevHeight: chain.BestBlockHeight,
103 timestamp: func() uint64 { return genesisHeader.Timestamp },
104 prevHash: chain.BestBlockHash,
110 for _, c := range cases {
111 block, err := NewBlock(chain, nil, []byte{byte(vm.OP_TRUE)})
116 block.Version = c.version()
117 block.Height = c.prevHeight() + 1
118 block.Timestamp = c.timestamp()
119 block.PreviousBlockHash = *c.prevHash()
121 _, err = chain.ProcessBlock(block)
123 if result != c.valid {
124 t.Fatalf("%s test failed, expected: %t, have: %t, err: %s", c.desc, c.valid, result, err)
129 func TestMaxBlockGas(t *testing.T) {
130 chainDB := dbm.NewDB("test_block_db", "leveldb", "test_block_db")
131 defer os.RemoveAll("test_block_db")
132 chain, _, _, err := MockChain(chainDB)
137 if err := AppendBlocks(chain, 7); err != nil {
141 block, err := chain.GetBlockByHeight(1)
146 tx, err := CreateTxFromTx(block.Transactions[0], 0, 600000000000, []byte{byte(vm.OP_TRUE)})
151 outputAmount := uint64(600000000000)
152 txs := []*types.Tx{tx}
153 for i := 1; i < 50000; i++ {
154 outputAmount -= 10000000
155 tx, err := CreateTxFromTx(txs[i-1], 0, outputAmount, []byte{byte(vm.OP_TRUE)})
159 txs = append(txs, tx)
162 block, err = NewBlock(chain, txs, []byte{byte(vm.OP_TRUE)})
167 if err := SolveAndUpdate(chain, block); err == nil {
168 t.Fatalf("test max block gas failed")