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"
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) {
15 txEntries := []*bc.Tx{nil}
16 txStatus := bc.NewTransactionStatus()
17 if err := txStatus.SetStatus(0, false); err != nil {
21 preBlockHeader := chain.BestBlockHeader()
24 BlockHeader: types.BlockHeader{
26 Height: preBlockHeader.Height + 1,
27 PreviousBlockHash: preBlockHeader.Hash(),
28 Timestamp: preBlockHeader.Timestamp + 1,
29 BlockCommitment: types.BlockCommitment{},
31 Transactions: []*types.Tx{nil},
34 bcBlock := &bc.Block{BlockHeader: &bc.BlockHeader{Height: preBlockHeader.Height + 1}}
35 for _, tx := range txs {
37 gasStatus, err := validation.ValidateTx(tx.Tx, bcBlock)
39 if !gasStatus.GasValid {
45 txStatus.SetStatus(len(b.Transactions), gasOnlyTx)
46 b.Transactions = append(b.Transactions, tx)
47 txEntries = append(txEntries, tx.Tx)
48 gasUsed += uint64(gasStatus.GasUsed)
52 coinbaseTx, err := CreateCoinbaseTx(controlProgram, preBlockHeader.Height+1, txsFee)
57 b.Transactions[0] = coinbaseTx
58 txEntries[0] = coinbaseTx.Tx
59 b.TransactionsMerkleRoot, err = types.TxMerkleRoot(txEntries)
64 b.TransactionStatusHash, err = types.TxStatusMerkleRoot(txStatus.VerifyStatus)
68 // ReplaceCoinbase replace the coinbase tx of block with coinbaseTx
69 func ReplaceCoinbase(block *types.Block, coinbaseTx *types.Tx) (err error) {
70 block.Transactions[0] = coinbaseTx
71 txEntires := []*bc.Tx{coinbaseTx.Tx}
72 for i := 1; i < len(block.Transactions); i++ {
73 txEntires = append(txEntires, block.Transactions[i].Tx)
76 block.TransactionsMerkleRoot, err = types.TxMerkleRoot(txEntires)
80 // AppendBlocks append empty blocks to chain, mainly used to mature the coinbase tx
81 func AppendBlocks(chain *protocol.Chain, num uint64) error {
82 for i := uint64(0); i < num; i++ {
83 block, err := NewBlock(chain, nil, []byte{byte(vm.OP_TRUE)})
87 if err := SolveAndUpdate(chain, block); err != nil {
94 // SolveAndUpdate solve difficulty and update chain status
95 func SolveAndUpdate(chain *protocol.Chain, block *types.Block) error {
96 _, err := chain.ProcessBlock(block)