OSDN Git Service

speed up valiate tx (#226)
[bytom/vapor.git] / protocol / validation / block.go
index 630a87d..8ea143c 100644 (file)
@@ -9,7 +9,6 @@ import (
        "github.com/vapor/errors"
        "github.com/vapor/protocol/bc"
        "github.com/vapor/protocol/bc/types"
-       "github.com/vapor/protocol/state"
 )
 
 const logModule = "leveldb"
@@ -25,7 +24,7 @@ var (
        errVersionRegression     = errors.New("version regression")
 )
 
-func checkBlockTime(b *bc.Block, parent *state.BlockNode) error {
+func checkBlockTime(b *bc.Block, parent *types.BlockHeader) error {
        now := uint64(time.Now().UnixNano() / 1e6)
        if b.Timestamp < (parent.Timestamp + consensus.BlockTimeInterval) {
                return errBadTimestamp
@@ -64,22 +63,22 @@ func checkCoinbaseAmount(b *bc.Block, amount uint64) error {
 }
 
 // ValidateBlockHeader check the block's header
-func ValidateBlockHeader(b *bc.Block, parent *state.BlockNode) error {
+func ValidateBlockHeader(b *bc.Block, parent *types.BlockHeader) error {
        if b.Version != 1 {
                return errors.WithDetailf(errVersionRegression, "previous block verson %d, current block version %d", parent.Version, b.Version)
        }
        if b.Height != parent.Height+1 {
                return errors.WithDetailf(errMisorderedBlockHeight, "previous block height %d, current block height %d", parent.Height, b.Height)
        }
-       if parent.Hash != *b.PreviousBlockId {
-               return errors.WithDetailf(errMismatchedBlock, "previous block ID %x, current block wants %x", parent.Hash.Bytes(), b.PreviousBlockId.Bytes())
+       if parent.Hash() != *b.PreviousBlockId {
+               return errors.WithDetailf(errMismatchedBlock, "previous block ID %x, current block wants %x", parent.Hash().Bytes(), b.PreviousBlockId.Bytes())
        }
 
        return checkBlockTime(b, parent)
 }
 
 // ValidateBlock validates a block and the transactions within.
-func ValidateBlock(b *bc.Block, parent *state.BlockNode) error {
+func ValidateBlock(b *bc.Block, parent *types.BlockHeader) error {
        startTime := time.Now()
        if err := ValidateBlockHeader(b, parent); err != nil {
                return err
@@ -89,17 +88,17 @@ func ValidateBlock(b *bc.Block, parent *state.BlockNode) error {
        coinbaseAmount := consensus.BlockSubsidy(b.BlockHeader.Height)
        b.TransactionStatus = bc.NewTransactionStatus()
 
-       for i, tx := range b.Transactions {
-               gasStatus, err := ValidateTx(tx, b)
-               if !gasStatus.GasValid {
-                       return errors.Wrapf(err, "validate of transaction %d of %d", i, len(b.Transactions))
+       validateResults := ValidateTxs(b.Transactions, b)
+       for i, validateResult := range validateResults {
+               if !validateResult.gasStatus.GasValid {
+                       return errors.Wrapf(validateResult.err, "validate of transaction %d of %d", i, len(b.Transactions))
                }
 
-               if err := b.TransactionStatus.SetStatus(i, err != nil); err != nil {
+               if err := b.TransactionStatus.SetStatus(i, validateResult.err != nil); err != nil {
                        return err
                }
-               coinbaseAmount += gasStatus.BTMValue
-               if blockGasSum += uint64(gasStatus.GasUsed); blockGasSum > consensus.MaxBlockGas {
+               coinbaseAmount += validateResult.gasStatus.BTMValue
+               if blockGasSum += uint64(validateResult.gasStatus.GasUsed); blockGasSum > consensus.MaxBlockGas {
                        return errOverBlockLimit
                }
        }