6 log "github.com/sirupsen/logrus"
8 "github.com/bytom/vapor/consensus"
9 "github.com/bytom/vapor/errors"
10 "github.com/bytom/vapor/protocol/bc"
11 "github.com/bytom/vapor/protocol/bc/types"
12 "github.com/bytom/vapor/protocol/state"
15 const logModule = "leveldb"
18 errBadTimestamp = errors.New("block timestamp is not in the valid range")
19 errBadBits = errors.New("block bits is invalid")
20 errMismatchedBlock = errors.New("mismatched block")
21 errMismatchedMerkleRoot = errors.New("mismatched merkle root")
22 errMisorderedBlockHeight = errors.New("misordered block height")
23 errOverBlockLimit = errors.New("block's gas is over the limit")
24 errWorkProof = errors.New("invalid difficulty proof of work")
25 errVersionRegression = errors.New("version regression")
28 func checkBlockTime(b *bc.Block, parent *types.BlockHeader) error {
29 now := uint64(time.Now().UnixNano() / 1e6)
30 if b.Timestamp < (parent.Timestamp + consensus.ActiveNetParams.BlockTimeInterval) {
31 return errBadTimestamp
33 if b.Timestamp > (now + consensus.ActiveNetParams.MaxTimeOffsetMs) {
34 return errBadTimestamp
40 func checkCoinbaseTx(b *bc.Block, rewards []state.CoinbaseReward) error {
41 if len(b.Transactions) == 0 {
42 return errors.Wrap(ErrWrongCoinbaseTransaction, "block is empty")
45 tx := b.Transactions[0]
46 var rewardAmount, coinbaseAmount uint64
47 for _, output := range tx.TxHeader.ResultIds {
48 out, err := tx.IntraChainOutput(*output)
53 coinbaseAmount += out.Source.Value.Amount
56 for _, reward := range rewards {
57 rewardAmount += reward.Amount
60 if rewardAmount != coinbaseAmount {
61 return errors.Wrapf(ErrWrongCoinbaseTransaction, "dismatch output amount, got:%d, want:%d", coinbaseAmount, rewardAmount)
66 // ValidateBlockHeader check the block's header
67 func ValidateBlockHeader(b *bc.Block, parent *types.BlockHeader) error {
69 return errors.WithDetailf(errVersionRegression, "previous block verson %d, current block version %d", parent.Version, b.Version)
71 if b.Height != parent.Height+1 {
72 return errors.WithDetailf(errMisorderedBlockHeight, "previous block height %d, current block height %d", parent.Height, b.Height)
74 if parent.Hash() != *b.PreviousBlockId {
75 return errors.WithDetailf(errMismatchedBlock, "previous block ID %x, current block wants %x", parent.Hash().Bytes(), b.PreviousBlockId.Bytes())
78 return checkBlockTime(b, parent)
81 // ValidateBlock validates a block and the transactions within.
82 func ValidateBlock(b *bc.Block, parent *types.BlockHeader, rewards []state.CoinbaseReward) error {
83 startTime := time.Now()
84 if err := ValidateBlockHeader(b, parent); err != nil {
88 blockGasSum := uint64(0)
89 b.TransactionStatus = bc.NewTransactionStatus()
90 validateResults := ValidateTxs(b.Transactions, b)
91 for i, validateResult := range validateResults {
92 if !validateResult.gasStatus.GasValid {
93 return errors.Wrapf(validateResult.err, "validate of transaction %d of %d", i, len(b.Transactions))
96 if err := b.TransactionStatus.SetStatus(i, validateResult.err != nil); err != nil {
100 if blockGasSum += uint64(validateResult.gasStatus.GasUsed); blockGasSum > consensus.ActiveNetParams.MaxBlockGas {
101 return errOverBlockLimit
105 if err := checkCoinbaseTx(b, rewards); err != nil {
109 txMerkleRoot, err := types.TxMerkleRoot(b.Transactions)
111 return errors.Wrap(err, "computing transaction id merkle root")
113 if txMerkleRoot != *b.TransactionsRoot {
114 return errors.WithDetailf(errMismatchedMerkleRoot, "transaction id merkle root. compute: %v, given: %v", txMerkleRoot, *b.TransactionsRoot)
117 txStatusHash, err := types.TxStatusMerkleRoot(b.TransactionStatus.VerifyStatus)
119 return errors.Wrap(err, "computing transaction status merkle root")
121 if txStatusHash != *b.TransactionStatusHash {
122 return errors.WithDetailf(errMismatchedMerkleRoot, "transaction status merkle root. compute: %v, given: %v", txStatusHash, *b.TransactionStatusHash)
125 log.WithFields(log.Fields{
128 "hash": b.ID.String(),
129 "duration": time.Since(startTime),
130 }).Debug("finish validate block")