OSDN Git Service

fix: add CrossChainInput in NewTxVMContext (#124)
[bytom/vapor.git] / protocol / validation / block.go
index 2622a29..8b63df0 100644 (file)
@@ -6,7 +6,6 @@ import (
        log "github.com/sirupsen/logrus"
 
        "github.com/vapor/consensus"
-       "github.com/vapor/consensus/difficulty"
        "github.com/vapor/errors"
        "github.com/vapor/protocol/bc"
        "github.com/vapor/protocol/bc/types"
@@ -27,7 +26,7 @@ var (
 )
 
 func checkBlockTime(b *bc.Block, parent *state.BlockNode) error {
-       if b.Timestamp > uint64(time.Now().Unix())+consensus.MaxTimeOffsetSeconds {
+       if b.Timestamp > uint64(time.Now().UnixNano()/int64(time.Millisecond))+consensus.MaxTimeOffsetMs {
                return errBadTimestamp
        }
 
@@ -47,12 +46,17 @@ func checkCoinbaseAmount(b *bc.Block, amount uint64) error {
                return errors.Wrap(ErrWrongCoinbaseTransaction, "have more than 1 output")
        }
 
-       output, err := tx.Output(*tx.TxHeader.ResultIds[0])
-       if err != nil {
-               return err
+       var SourceAmount uint64
+       switch output := tx.Entries[*tx.TxHeader.ResultIds[0]].(type) {
+       case *bc.IntraChainOutput:
+               SourceAmount = output.Source.Value.Amount
+       case *bc.VoteOutput:
+               SourceAmount = output.Source.Value.Amount
+       default:
+               return errors.Wrapf(bc.ErrEntryType, "entry %x has unexpected type %T", tx.TxHeader.ResultIds[0].Bytes(), output)
        }
 
-       if output.Source.Value.Amount != amount {
+       if SourceAmount != amount {
                return errors.Wrap(ErrWrongCoinbaseTransaction, "dismatch output amount")
        }
        return nil
@@ -66,19 +70,11 @@ func ValidateBlockHeader(b *bc.Block, parent *state.BlockNode) error {
        if b.Height != parent.Height+1 {
                return errors.WithDetailf(errMisorderedBlockHeight, "previous block height %d, current block height %d", parent.Height, b.Height)
        }
-       if b.Bits != parent.CalcNextBits() {
-               return errBadBits
-       }
        if parent.Hash != *b.PreviousBlockId {
                return errors.WithDetailf(errMismatchedBlock, "previous block ID %x, current block wants %x", parent.Hash.Bytes(), b.PreviousBlockId.Bytes())
        }
-       if err := checkBlockTime(b, parent); err != nil {
-               return err
-       }
-       if !difficulty.CheckProofOfWork(&b.ID, parent.CalcNextSeed(), b.BlockHeader.Bits) {
-               return errWorkProof
-       }
-       return nil
+
+       return checkBlockTime(b, parent)
 }
 
 // ValidateBlock validates a block and the transactions within.
@@ -116,7 +112,7 @@ func ValidateBlock(b *bc.Block, parent *state.BlockNode) error {
                return errors.Wrap(err, "computing transaction id merkle root")
        }
        if txMerkleRoot != *b.TransactionsRoot {
-               return errors.WithDetailf(errMismatchedMerkleRoot, "transaction id merkle root")
+               return errors.WithDetailf(errMismatchedMerkleRoot, "transaction id merkle root. compute: %v, given: %v", txMerkleRoot, *b.TransactionsRoot)
        }
 
        txStatusHash, err := types.TxStatusMerkleRoot(b.TransactionStatus.VerifyStatus)