10 "github.com/vapor/consensus"
11 dbm "github.com/vapor/database/leveldb"
12 "github.com/vapor/protocol/bc"
13 "github.com/vapor/protocol/bc/types"
14 "github.com/vapor/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, _, _, err := MockChain(db)
25 genesisHeader := chain.BestBlockHeader()
26 if err := AppendBlocks(chain, 1); err != nil {
33 prevHeight func() uint64
34 timestamp func() uint64
35 prevHash func() *bc.Hash
39 desc: "block version is 1",
40 version: func() uint64 { return 1 },
41 prevHeight: chain.BestBlockHeight,
42 timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
43 prevHash: chain.BestBlockHash,
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,
55 desc: "invalid prev hash, prev hash dismatch",
56 version: func() uint64 { return chain.BestBlockHeader().Version },
57 prevHeight: chain.BestBlockHeight,
58 timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
59 prevHash: func() *bc.Hash { hash := genesisHeader.Hash(); return &hash },
63 desc: "invalid timestamp, greater than MaxTimeOffsetMs from system time",
64 version: func() uint64 { return chain.BestBlockHeader().Version },
65 prevHeight: chain.BestBlockHeight,
66 timestamp: func() uint64 { return uint64(time.Now().Unix()) + consensus.ActiveNetParams.MaxTimeOffsetMs + 60 },
67 prevHash: chain.BestBlockHash,
71 desc: "valid timestamp, greater than last block",
72 version: func() uint64 { return chain.BestBlockHeader().Version },
73 prevHeight: chain.BestBlockHeight,
74 timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp + 3 },
75 prevHash: chain.BestBlockHash,
79 desc: "valid timestamp, less than last block, but greater than median",
80 version: func() uint64 { return chain.BestBlockHeader().Version },
81 prevHeight: chain.BestBlockHeight,
82 timestamp: func() uint64 { return chain.BestBlockHeader().Timestamp - 1 },
83 prevHash: chain.BestBlockHash,
87 desc: "invalid timestamp, less than median",
88 version: func() uint64 { return chain.BestBlockHeader().Version },
89 prevHeight: chain.BestBlockHeight,
90 timestamp: func() uint64 { return genesisHeader.Timestamp },
91 prevHash: chain.BestBlockHash,
96 for _, c := range cases {
97 block, err := NewBlock(chain, nil, []byte{byte(vm.OP_TRUE)})
102 block.Version = c.version()
103 block.Height = c.prevHeight() + 1
104 block.Timestamp = c.timestamp()
105 block.PreviousBlockHash = *c.prevHash()
107 _, err = chain.ProcessBlock(block)
109 if result != c.valid {
110 t.Fatalf("%s test failed, expected: %t, have: %t, err: %s", c.desc, c.valid, result, err)
115 func TestMaxBlockGas(t *testing.T) {
116 chainDB := dbm.NewDB("test_block_db", "leveldb", "test_block_db")
117 defer os.RemoveAll("test_block_db")
118 chain, _, _, err := MockChain(chainDB)
123 if err := AppendBlocks(chain, 7); err != nil {
127 block, err := chain.GetBlockByHeight(1)
132 tx, err := CreateTxFromTx(block.Transactions[0], 0, 600000000000, []byte{byte(vm.OP_TRUE)})
137 outputAmount := uint64(600000000000)
138 txs := []*types.Tx{tx}
139 for i := 1; i < 50000; i++ {
140 outputAmount -= 10000000
141 tx, err := CreateTxFromTx(txs[i-1], 0, outputAmount, []byte{byte(vm.OP_TRUE)})
145 txs = append(txs, tx)
148 block, err = NewBlock(chain, txs, []byte{byte(vm.OP_TRUE)})
153 if _, err := chain.ProcessBlock(block); err == nil {
154 t.Fatalf("test max block gas failed")