OSDN Git Service

Merge pull request #201 from Bytom/v0.1
[bytom/vapor.git] / protocol / validation / block.go
index 17aff8a..630a87d 100644 (file)
@@ -26,13 +26,14 @@ var (
 )
 
 func checkBlockTime(b *bc.Block, parent *state.BlockNode) error {
-       if b.Timestamp > uint64(time.Now().Unix())+consensus.MaxTimeOffsetSeconds {
+       now := uint64(time.Now().UnixNano() / 1e6)
+       if b.Timestamp < (parent.Timestamp + consensus.BlockTimeInterval) {
                return errBadTimestamp
        }
-
-       if b.Timestamp <= parent.CalcPastMedianTime() {
+       if b.Timestamp > (now + consensus.MaxTimeOffsetMs) {
                return errBadTimestamp
        }
+
        return nil
 }
 
@@ -42,20 +43,29 @@ func checkCoinbaseAmount(b *bc.Block, amount uint64) error {
        }
 
        tx := b.Transactions[0]
-       output, err := tx.Output(*tx.TxHeader.ResultIds[0])
-       if err != nil {
-               return err
+       if len(tx.TxHeader.ResultIds) != 1 {
+               return errors.Wrap(ErrWrongCoinbaseTransaction, "have more than 1 output")
+       }
+
+       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
 }
 
 // ValidateBlockHeader check the block's header
-func ValidateBlockHeader(b *bc.Block, block *types.Block, parent *state.BlockNode) error {
-       if b.Version < parent.Version {
+func ValidateBlockHeader(b *bc.Block, parent *state.BlockNode) 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 {
@@ -64,23 +74,21 @@ func ValidateBlockHeader(b *bc.Block, block *types.Block, parent *state.BlockNod
        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
-       }
 
-       return nil
+       return checkBlockTime(b, parent)
 }
 
 // ValidateBlock validates a block and the transactions within.
-func ValidateBlock(b *bc.Block, parent *state.BlockNode, block *types.Block) error {
+func ValidateBlock(b *bc.Block, parent *state.BlockNode) error {
        startTime := time.Now()
-       if err := ValidateBlockHeader(b, block, parent); err != nil {
+       if err := ValidateBlockHeader(b, parent); err != nil {
                return err
        }
 
        blockGasSum := uint64(0)
        coinbaseAmount := consensus.BlockSubsidy(b.BlockHeader.Height)
        b.TransactionStatus = bc.NewTransactionStatus()
+
        for i, tx := range b.Transactions {
                gasStatus, err := ValidateTx(tx, b)
                if !gasStatus.GasValid {
@@ -105,7 +113,7 @@ func ValidateBlock(b *bc.Block, parent *state.BlockNode, block *types.Block) err
                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)