OSDN Git Service

feat(version): update version to 1.1.0
[bytom/bytom.git] / test / block_test_util.go
index acd8d26..66d3454 100644 (file)
@@ -1,14 +1,12 @@
 package test
 
 import (
-       "github.com/bytom/consensus"
-       "github.com/bytom/consensus/difficulty"
-       "github.com/bytom/mining/tensority"
-       "github.com/bytom/protocol"
-       "github.com/bytom/protocol/bc"
-       "github.com/bytom/protocol/bc/types"
-       "github.com/bytom/protocol/validation"
-       "github.com/bytom/protocol/vm"
+       "github.com/bytom/bytom/mining/tensority"
+       "github.com/bytom/bytom/protocol"
+       "github.com/bytom/bytom/protocol/bc"
+       "github.com/bytom/bytom/protocol/bc/types"
+       "github.com/bytom/bytom/protocol/validation"
+       "github.com/bytom/bytom/protocol/vm"
 )
 
 // NewBlock create block according to the current status of chain
@@ -17,12 +15,15 @@ func NewBlock(chain *protocol.Chain, txs []*types.Tx, controlProgram []byte) (*t
        txsFee := uint64(0)
        txEntries := []*bc.Tx{nil}
        txStatus := bc.NewTransactionStatus()
-       txStatus.SetStatus(0, false)
+       if err := txStatus.SetStatus(0, false); err != nil {
+               return nil, err
+       }
 
        preBlockHeader := chain.BestBlockHeader()
-       var compareDiffBH *types.BlockHeader
-       if compareDiffBlock, err := chain.GetBlockByHeight(preBlockHeader.Height - consensus.BlocksPerRetarget); err == nil {
-               compareDiffBH = &compareDiffBlock.BlockHeader
+       preBlockHash := preBlockHeader.Hash()
+       nextBits, err := chain.CalcNextBits(&preBlockHash)
+       if err != nil {
+               return nil, err
        }
 
        b := &types.Block{
@@ -32,7 +33,7 @@ func NewBlock(chain *protocol.Chain, txs []*types.Tx, controlProgram []byte) (*t
                        PreviousBlockHash: preBlockHeader.Hash(),
                        Timestamp:         preBlockHeader.Timestamp + 1,
                        BlockCommitment:   types.BlockCommitment{},
-                       Bits:              difficulty.CalcNextRequiredDifficulty(preBlockHeader, compareDiffBH),
+                       Bits:              nextBits,
                },
                Transactions: []*types.Tx{nil},
        }
@@ -42,7 +43,7 @@ func NewBlock(chain *protocol.Chain, txs []*types.Tx, controlProgram []byte) (*t
                gasOnlyTx := false
                gasStatus, err := validation.ValidateTx(tx.Tx, bcBlock)
                if err != nil {
-                       if !gasStatus.GasVaild {
+                       if !gasStatus.GasValid {
                                continue
                        }
                        gasOnlyTx = true
@@ -62,12 +63,12 @@ func NewBlock(chain *protocol.Chain, txs []*types.Tx, controlProgram []byte) (*t
 
        b.Transactions[0] = coinbaseTx
        txEntries[0] = coinbaseTx.Tx
-       b.TransactionsMerkleRoot, err = bc.TxMerkleRoot(txEntries)
+       b.TransactionsMerkleRoot, err = types.TxMerkleRoot(txEntries)
        if err != nil {
                return nil, err
        }
 
-       b.TransactionStatusHash, err = bc.TxStatusMerkleRoot(txStatus.VerifyStatus)
+       b.TransactionStatusHash, err = types.TxStatusMerkleRoot(txStatus.VerifyStatus)
        return b, err
 }
 
@@ -79,7 +80,7 @@ func ReplaceCoinbase(block *types.Block, coinbaseTx *types.Tx) (err error) {
                txEntires = append(txEntires, block.Transactions[i].Tx)
        }
 
-       block.TransactionsMerkleRoot, err = bc.TxMerkleRoot(txEntires)
+       block.TransactionsMerkleRoot, err = types.TxMerkleRoot(txEntires)
        return
 }