OSDN Git Service

add log (#373)
[bytom/vapor.git] / test / block_test_util.go
1 package test
2
3 import (
4         "github.com/vapor/protocol"
5         "github.com/vapor/protocol/bc"
6         "github.com/vapor/protocol/bc/types"
7         "github.com/vapor/protocol/validation"
8         "github.com/vapor/protocol/vm"
9 )
10
11 // NewBlock create block according to the current status of chain
12 func NewBlock(chain *protocol.Chain, txs []*types.Tx, controlProgram []byte) (*types.Block, error) {
13         gasUsed := uint64(0)
14         txsFee := uint64(0)
15         txEntries := []*bc.Tx{nil}
16         txStatus := bc.NewTransactionStatus()
17         if err := txStatus.SetStatus(0, false); err != nil {
18                 return nil, err
19         }
20
21         preBlockHeader := chain.BestBlockHeader()
22         b := &types.Block{
23                 BlockHeader: types.BlockHeader{
24                         Version:           1,
25                         Height:            preBlockHeader.Height + 1,
26                         PreviousBlockHash: preBlockHeader.Hash(),
27                         Timestamp:         preBlockHeader.Timestamp + 1,
28                         BlockCommitment:   types.BlockCommitment{},
29                 },
30                 Transactions: []*types.Tx{nil},
31         }
32
33         bcBlock := &bc.Block{BlockHeader: &bc.BlockHeader{Height: preBlockHeader.Height + 1}}
34         for _, tx := range txs {
35                 gasOnlyTx := false
36                 gasStatus, err := validation.ValidateTx(tx.Tx, bcBlock)
37                 if err != nil {
38                         if !gasStatus.GasValid {
39                                 continue
40                         }
41                         gasOnlyTx = true
42                 }
43
44                 txStatus.SetStatus(len(b.Transactions), gasOnlyTx)
45                 b.Transactions = append(b.Transactions, tx)
46                 txEntries = append(txEntries, tx.Tx)
47                 gasUsed += uint64(gasStatus.GasUsed)
48                 txsFee += txFee(tx)
49         }
50
51         coinbaseTx, err := CreateCoinbaseTx(controlProgram, preBlockHeader.Height+1, txsFee)
52         if err != nil {
53                 return nil, err
54         }
55
56         b.Transactions[0] = coinbaseTx
57         txEntries[0] = coinbaseTx.Tx
58         b.TransactionsMerkleRoot, err = types.TxMerkleRoot(txEntries)
59         if err != nil {
60                 return nil, err
61         }
62
63         b.TransactionStatusHash, err = types.TxStatusMerkleRoot(txStatus.VerifyStatus)
64         return b, err
65 }
66
67 // ReplaceCoinbase replace the coinbase tx of block with coinbaseTx
68 func ReplaceCoinbase(block *types.Block, coinbaseTx *types.Tx) (err error) {
69         block.Transactions[0] = coinbaseTx
70         txEntires := []*bc.Tx{coinbaseTx.Tx}
71         for i := 1; i < len(block.Transactions); i++ {
72                 txEntires = append(txEntires, block.Transactions[i].Tx)
73         }
74
75         block.TransactionsMerkleRoot, err = types.TxMerkleRoot(txEntires)
76         return
77 }
78
79 // AppendBlocks append empty blocks to chain, mainly used to mature the coinbase tx
80 func AppendBlocks(chain *protocol.Chain, num uint64) error {
81         for i := uint64(0); i < num; i++ {
82                 block, err := NewBlock(chain, nil, []byte{byte(vm.OP_TRUE)})
83                 if err != nil {
84                         return err
85                 }
86                 if _, err := chain.ProcessBlock(block); err != nil {
87                         return err
88                 }
89         }
90         return nil
91 }