github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/mining.go (about)

     1  // Copyright (c) 2014-2016 The btcsuite developers
     2  // Copyright (c) 2016 The Dash developers
     3  // Use of this source code is governed by an ISC
     4  // license that can be found in the LICENSE file.
     5  
     6  package main
     7  
     8  import (
     9  	"container/heap"
    10  	"container/list"
    11  	"fmt"
    12  	"time"
    13  
    14  	"github.com/BlockABC/godash/blockchain"
    15  	"github.com/BlockABC/godash/mining"
    16  	"github.com/BlockABC/godash/txscript"
    17  	"github.com/BlockABC/godash/wire"
    18  	"github.com/BlockABC/godashutil"
    19  )
    20  
    21  const (
    22  	// generatedBlockVersion is the version of the block being generated.
    23  	// It is defined as a constant here rather than using the
    24  	// wire.BlockVersion constant since a change in the block version
    25  	// will require changes to the generated block.  Using the wire constant
    26  	// for generated block version could allow creation of invalid blocks
    27  	// for the updated version.
    28  	generatedBlockVersion = 4
    29  
    30  	// minHighPriority is the minimum priority value that allows a
    31  	// transaction to be considered high priority.
    32  	minHighPriority = godashutil.SatoshiPerBitcoin * 144.0 / 250
    33  
    34  	// blockHeaderOverhead is the max number of bytes it takes to serialize
    35  	// a block header and max possible transaction count.
    36  	blockHeaderOverhead = wire.MaxBlockHeaderPayload + wire.MaxVarIntPayload
    37  
    38  	// coinbaseFlags is added to the coinbase script of a generated block
    39  	// and is used to monitor BIP16 support as well as blocks that are
    40  	// generated via btcd.
    41  	coinbaseFlags = "/P2SH/btcd/"
    42  )
    43  
    44  // txPrioItem houses a transaction along with extra information that allows the
    45  // transaction to be prioritized and track dependencies on other transactions
    46  // which have not been mined into a block yet.
    47  type txPrioItem struct {
    48  	tx       *godashutil.Tx
    49  	fee      int64
    50  	priority float64
    51  	feePerKB int64
    52  
    53  	// dependsOn holds a map of transaction hashes which this one depends
    54  	// on.  It will only be set when the transaction references other
    55  	// transactions in the source pool and hence must come after them in
    56  	// a block.
    57  	dependsOn map[wire.ShaHash]struct{}
    58  }
    59  
    60  // txPriorityQueueLessFunc describes a function that can be used as a compare
    61  // function for a transaction priority queue (txPriorityQueue).
    62  type txPriorityQueueLessFunc func(*txPriorityQueue, int, int) bool
    63  
    64  // txPriorityQueue implements a priority queue of txPrioItem elements that
    65  // supports an arbitrary compare function as defined by txPriorityQueueLessFunc.
    66  type txPriorityQueue struct {
    67  	lessFunc txPriorityQueueLessFunc
    68  	items    []*txPrioItem
    69  }
    70  
    71  // Len returns the number of items in the priority queue.  It is part of the
    72  // heap.Interface implementation.
    73  func (pq *txPriorityQueue) Len() int {
    74  	return len(pq.items)
    75  }
    76  
    77  // Less returns whether the item in the priority queue with index i should sort
    78  // before the item with index j by deferring to the assigned less function.  It
    79  // is part of the heap.Interface implementation.
    80  func (pq *txPriorityQueue) Less(i, j int) bool {
    81  	return pq.lessFunc(pq, i, j)
    82  }
    83  
    84  // Swap swaps the items at the passed indices in the priority queue.  It is
    85  // part of the heap.Interface implementation.
    86  func (pq *txPriorityQueue) Swap(i, j int) {
    87  	pq.items[i], pq.items[j] = pq.items[j], pq.items[i]
    88  }
    89  
    90  // Push pushes the passed item onto the priority queue.  It is part of the
    91  // heap.Interface implementation.
    92  func (pq *txPriorityQueue) Push(x interface{}) {
    93  	pq.items = append(pq.items, x.(*txPrioItem))
    94  }
    95  
    96  // Pop removes the highest priority item (according to Less) from the priority
    97  // queue and returns it.  It is part of the heap.Interface implementation.
    98  func (pq *txPriorityQueue) Pop() interface{} {
    99  	n := len(pq.items)
   100  	item := pq.items[n-1]
   101  	pq.items[n-1] = nil
   102  	pq.items = pq.items[0 : n-1]
   103  	return item
   104  }
   105  
   106  // SetLessFunc sets the compare function for the priority queue to the provided
   107  // function.  It also invokes heap.Init on the priority queue using the new
   108  // function so it can immediately be used with heap.Push/Pop.
   109  func (pq *txPriorityQueue) SetLessFunc(lessFunc txPriorityQueueLessFunc) {
   110  	pq.lessFunc = lessFunc
   111  	heap.Init(pq)
   112  }
   113  
   114  // txPQByPriority sorts a txPriorityQueue by transaction priority and then fees
   115  // per kilobyte.
   116  func txPQByPriority(pq *txPriorityQueue, i, j int) bool {
   117  	// Using > here so that pop gives the highest priority item as opposed
   118  	// to the lowest.  Sort by priority first, then fee.
   119  	if pq.items[i].priority == pq.items[j].priority {
   120  		return pq.items[i].feePerKB > pq.items[j].feePerKB
   121  	}
   122  	return pq.items[i].priority > pq.items[j].priority
   123  
   124  }
   125  
   126  // txPQByFee sorts a txPriorityQueue by fees per kilobyte and then transaction
   127  // priority.
   128  func txPQByFee(pq *txPriorityQueue, i, j int) bool {
   129  	// Using > here so that pop gives the highest fee item as opposed
   130  	// to the lowest.  Sort by fee first, then priority.
   131  	if pq.items[i].feePerKB == pq.items[j].feePerKB {
   132  		return pq.items[i].priority > pq.items[j].priority
   133  	}
   134  	return pq.items[i].feePerKB > pq.items[j].feePerKB
   135  }
   136  
   137  // newTxPriorityQueue returns a new transaction priority queue that reserves the
   138  // passed amount of space for the elements.  The new priority queue uses either
   139  // the txPQByPriority or the txPQByFee compare function depending on the
   140  // sortByFee parameter and is already initialized for use with heap.Push/Pop.
   141  // The priority queue can grow larger than the reserved space, but extra copies
   142  // of the underlying array can be avoided by reserving a sane value.
   143  func newTxPriorityQueue(reserve int, sortByFee bool) *txPriorityQueue {
   144  	pq := &txPriorityQueue{
   145  		items: make([]*txPrioItem, 0, reserve),
   146  	}
   147  	if sortByFee {
   148  		pq.SetLessFunc(txPQByFee)
   149  	} else {
   150  		pq.SetLessFunc(txPQByPriority)
   151  	}
   152  	return pq
   153  }
   154  
   155  // BlockTemplate houses a block that has yet to be solved along with additional
   156  // details about the fees and the number of signature operations for each
   157  // transaction in the block.
   158  type BlockTemplate struct {
   159  	// Block is a block that is ready to be solved by miners.  Thus, it is
   160  	// completely valid with the exception of satisfying the proof-of-work
   161  	// requirement.
   162  	Block *wire.MsgBlock
   163  
   164  	// Fees contains the amount of fees each transaction in the generated
   165  	// template pays in base units.  Since the first transaction is the
   166  	// coinbase, the first entry (offset 0) will contain the negative of the
   167  	// sum of the fees of all other transactions.
   168  	Fees []int64
   169  
   170  	// SigOpCounts contains the number of signature operations each
   171  	// transaction in the generated template performs.
   172  	SigOpCounts []int64
   173  
   174  	// Height is the height at which the block template connects to the main
   175  	// chain.
   176  	Height int32
   177  
   178  	// ValidPayAddress indicates whether or not the template coinbase pays
   179  	// to an address or is redeemable by anyone.  See the documentation on
   180  	// NewBlockTemplate for details on which this can be useful to generate
   181  	// templates without a coinbase payment address.
   182  	ValidPayAddress bool
   183  }
   184  
   185  // mergeUtxoView adds all of the entries in view to viewA.  The result is that
   186  // viewA will contain all of its original entries plus all of the entries
   187  // in viewB.  It will replace any entries in viewB which also exist in viewA
   188  // if the entry in viewA is fully spent.
   189  func mergeUtxoView(viewA *blockchain.UtxoViewpoint, viewB *blockchain.UtxoViewpoint) {
   190  	viewAEntries := viewA.Entries()
   191  	for hash, entryB := range viewB.Entries() {
   192  		if entryA, exists := viewAEntries[hash]; !exists ||
   193  			entryA == nil || entryA.IsFullySpent() {
   194  
   195  			viewAEntries[hash] = entryB
   196  		}
   197  	}
   198  }
   199  
   200  // standardCoinbaseScript returns a standard script suitable for use as the
   201  // signature script of the coinbase transaction of a new block.  In particular,
   202  // it starts with the block height that is required by version 2 blocks and adds
   203  // the extra nonce as well as additional coinbase flags.
   204  func standardCoinbaseScript(nextBlockHeight int32, extraNonce uint64) ([]byte, error) {
   205  	return txscript.NewScriptBuilder().AddInt64(int64(nextBlockHeight)).
   206  		AddInt64(int64(extraNonce)).AddData([]byte(coinbaseFlags)).
   207  		Script()
   208  }
   209  
   210  // createCoinbaseTx returns a coinbase transaction paying an appropriate subsidy
   211  // based on the passed block height to the provided address.  When the address
   212  // is nil, the coinbase transaction will instead be redeemable by anyone.
   213  //
   214  // See the comment for NewBlockTemplate for more information about why the nil
   215  // address handling is useful.
   216  func createCoinbaseTx(coinbaseScript []byte, nextBlockHeight int32, addr godashutil.Address) (*godashutil.Tx, error) {
   217  	// Create the script to pay to the provided payment address if one was
   218  	// specified.  Otherwise create a script that allows the coinbase to be
   219  	// redeemable by anyone.
   220  	var pkScript []byte
   221  	if addr != nil {
   222  		var err error
   223  		pkScript, err = txscript.PayToAddrScript(addr)
   224  		if err != nil {
   225  			return nil, err
   226  		}
   227  	} else {
   228  		var err error
   229  		scriptBuilder := txscript.NewScriptBuilder()
   230  		pkScript, err = scriptBuilder.AddOp(txscript.OP_TRUE).Script()
   231  		if err != nil {
   232  			return nil, err
   233  		}
   234  	}
   235  
   236  	tx := wire.NewMsgTx()
   237  	tx.AddTxIn(&wire.TxIn{
   238  		// Coinbase transactions have no inputs, so previous outpoint is
   239  		// zero hash and max index.
   240  		PreviousOutPoint: *wire.NewOutPoint(&wire.ShaHash{},
   241  			wire.MaxPrevOutIndex),
   242  		SignatureScript: coinbaseScript,
   243  		Sequence:        wire.MaxTxInSequenceNum,
   244  	})
   245  	tx.AddTxOut(&wire.TxOut{
   246  		Value: blockchain.CalcBlockSubsidy(nextBlockHeight,
   247  			activeNetParams.Params),
   248  		PkScript: pkScript,
   249  	})
   250  	return godashutil.NewTx(tx), nil
   251  }
   252  
   253  // spendTransaction updates the passed view by marking the inputs to the passed
   254  // transaction as spent.  It also adds all outputs in the passed transaction
   255  // which are not provably unspendable as available unspent transaction outputs.
   256  func spendTransaction(utxoView *blockchain.UtxoViewpoint, tx *godashutil.Tx, height int32) error {
   257  	for _, txIn := range tx.MsgTx().TxIn {
   258  		originHash := &txIn.PreviousOutPoint.Hash
   259  		originIndex := txIn.PreviousOutPoint.Index
   260  		entry := utxoView.LookupEntry(originHash)
   261  		if entry != nil {
   262  			entry.SpendOutput(originIndex)
   263  		}
   264  	}
   265  
   266  	utxoView.AddTxOuts(tx, height)
   267  	return nil
   268  }
   269  
   270  // logSkippedDeps logs any dependencies which are also skipped as a result of
   271  // skipping a transaction while generating a block template at the trace level.
   272  func logSkippedDeps(tx *godashutil.Tx, deps *list.List) {
   273  	if deps == nil {
   274  		return
   275  	}
   276  
   277  	for e := deps.Front(); e != nil; e = e.Next() {
   278  		item := e.Value.(*txPrioItem)
   279  		minrLog.Tracef("Skipping tx %s since it depends on %s\n",
   280  			item.tx.Sha(), tx.Sha())
   281  	}
   282  }
   283  
   284  // minimumMedianTime returns the minimum allowed timestamp for a block building
   285  // on the end of the current best chain.  In particular, it is one second after
   286  // the median timestamp of the last several blocks per the chain consensus
   287  // rules.
   288  func minimumMedianTime(chainState *chainState) (time.Time, error) {
   289  	chainState.Lock()
   290  	defer chainState.Unlock()
   291  	if chainState.pastMedianTimeErr != nil {
   292  		return time.Time{}, chainState.pastMedianTimeErr
   293  	}
   294  
   295  	return chainState.pastMedianTime.Add(time.Second), nil
   296  }
   297  
   298  // medianAdjustedTime returns the current time adjusted to ensure it is at least
   299  // one second after the median timestamp of the last several blocks per the
   300  // chain consensus rules.
   301  func medianAdjustedTime(chainState *chainState, timeSource blockchain.MedianTimeSource) (time.Time, error) {
   302  	chainState.Lock()
   303  	defer chainState.Unlock()
   304  	if chainState.pastMedianTimeErr != nil {
   305  		return time.Time{}, chainState.pastMedianTimeErr
   306  	}
   307  
   308  	// The timestamp for the block must not be before the median timestamp
   309  	// of the last several blocks.  Thus, choose the maximum between the
   310  	// current time and one second after the past median time.  The current
   311  	// timestamp is truncated to a second boundary before comparison since a
   312  	// block timestamp does not supported a precision greater than one
   313  	// second.
   314  	newTimestamp := timeSource.AdjustedTime()
   315  	minTimestamp := chainState.pastMedianTime.Add(time.Second)
   316  	if newTimestamp.Before(minTimestamp) {
   317  		newTimestamp = minTimestamp
   318  	}
   319  
   320  	return newTimestamp, nil
   321  }
   322  
   323  // NewBlockTemplate returns a new block template that is ready to be solved
   324  // using the transactions from the passed transaction source pool and a coinbase
   325  // that either pays to the passed address if it is not nil, or a coinbase that
   326  // is redeemable by anyone if the passed address is nil.  The nil address
   327  // functionality is useful since there are cases such as the getblocktemplate
   328  // RPC where external mining software is responsible for creating their own
   329  // coinbase which will replace the one generated for the block template.  Thus
   330  // the need to have configured address can be avoided.
   331  //
   332  // The transactions selected and included are prioritized according to several
   333  // factors.  First, each transaction has a priority calculated based on its
   334  // value, age of inputs, and size.  Transactions which consist of larger
   335  // amounts, older inputs, and small sizes have the highest priority.  Second, a
   336  // fee per kilobyte is calculated for each transaction.  Transactions with a
   337  // higher fee per kilobyte are preferred.  Finally, the block generation related
   338  // policy settings are all taken into account.
   339  //
   340  // Transactions which only spend outputs from other transactions already in the
   341  // block chain are immediately added to a priority queue which either
   342  // prioritizes based on the priority (then fee per kilobyte) or the fee per
   343  // kilobyte (then priority) depending on whether or not the BlockPrioritySize
   344  // policy setting allots space for high-priority transactions.  Transactions
   345  // which spend outputs from other transactions in the source pool are added to a
   346  // dependency map so they can be added to the priority queue once the
   347  // transactions they depend on have been included.
   348  //
   349  // Once the high-priority area (if configured) has been filled with
   350  // transactions, or the priority falls below what is considered high-priority,
   351  // the priority queue is updated to prioritize by fees per kilobyte (then
   352  // priority).
   353  //
   354  // When the fees per kilobyte drop below the TxMinFreeFee policy setting, the
   355  // transaction will be skipped unless the BlockMinSize policy setting is
   356  // nonzero, in which case the block will be filled with the low-fee/free
   357  // transactions until the block size reaches that minimum size.
   358  //
   359  // Any transactions which would cause the block to exceed the BlockMaxSize
   360  // policy setting, exceed the maximum allowed signature operations per block, or
   361  // otherwise cause the block to be invalid are skipped.
   362  //
   363  // Given the above, a block generated by this function is of the following form:
   364  //
   365  //   -----------------------------------  --  --
   366  //  |      Coinbase Transaction         |   |   |
   367  //  |-----------------------------------|   |   |
   368  //  |                                   |   |   | ----- policy.BlockPrioritySize
   369  //  |   High-priority Transactions      |   |   |
   370  //  |                                   |   |   |
   371  //  |-----------------------------------|   | --
   372  //  |                                   |   |
   373  //  |                                   |   |
   374  //  |                                   |   |--- policy.BlockMaxSize
   375  //  |  Transactions prioritized by fee  |   |
   376  //  |  until <= policy.TxMinFreeFee     |   |
   377  //  |                                   |   |
   378  //  |                                   |   |
   379  //  |                                   |   |
   380  //  |-----------------------------------|   |
   381  //  |  Low-fee/Non high-priority (free) |   |
   382  //  |  transactions (while block size   |   |
   383  //  |  <= policy.BlockMinSize)          |   |
   384  //   -----------------------------------  --
   385  func NewBlockTemplate(policy *mining.Policy, server *server, payToAddress godashutil.Address) (*BlockTemplate, error) {
   386  	var txSource mining.TxSource = server.txMemPool
   387  	blockManager := server.blockManager
   388  	timeSource := server.timeSource
   389  	chainState := &blockManager.chainState
   390  
   391  	// Extend the most recently known best block.
   392  	chainState.Lock()
   393  	prevHash := chainState.newestHash
   394  	nextBlockHeight := chainState.newestHeight + 1
   395  	chainState.Unlock()
   396  
   397  	// Create a standard coinbase transaction paying to the provided
   398  	// address.  NOTE: The coinbase value will be updated to include the
   399  	// fees from the selected transactions later after they have actually
   400  	// been selected.  It is created here to detect any errors early
   401  	// before potentially doing a lot of work below.  The extra nonce helps
   402  	// ensure the transaction is not a duplicate transaction (paying the
   403  	// same value to the same public key address would otherwise be an
   404  	// identical transaction for block version 1).
   405  	extraNonce := uint64(0)
   406  	coinbaseScript, err := standardCoinbaseScript(nextBlockHeight, extraNonce)
   407  	if err != nil {
   408  		return nil, err
   409  	}
   410  	coinbaseTx, err := createCoinbaseTx(coinbaseScript, nextBlockHeight,
   411  		payToAddress)
   412  	if err != nil {
   413  		return nil, err
   414  	}
   415  	numCoinbaseSigOps := int64(blockchain.CountSigOps(coinbaseTx))
   416  
   417  	// Get the current source transactions and create a priority queue to
   418  	// hold the transactions which are ready for inclusion into a block
   419  	// along with some priority related and fee metadata.  Reserve the same
   420  	// number of items that are available for the priority queue.  Also,
   421  	// choose the initial sort order for the priority queue based on whether
   422  	// or not there is an area allocated for high-priority transactions.
   423  	sourceTxns := txSource.MiningDescs()
   424  	sortedByFee := policy.BlockPrioritySize == 0
   425  	priorityQueue := newTxPriorityQueue(len(sourceTxns), sortedByFee)
   426  
   427  	// Create a slice to hold the transactions to be included in the
   428  	// generated block with reserved space.  Also create a utxo view to
   429  	// house all of the input transactions so multiple lookups can be
   430  	// avoided.
   431  	blockTxns := make([]*godashutil.Tx, 0, len(sourceTxns))
   432  	blockTxns = append(blockTxns, coinbaseTx)
   433  	blockUtxos := blockchain.NewUtxoViewpoint()
   434  
   435  	// dependers is used to track transactions which depend on another
   436  	// transaction in the source pool.  This, in conjunction with the
   437  	// dependsOn map kept with each dependent transaction helps quickly
   438  	// determine which dependent transactions are now eligible for inclusion
   439  	// in the block once each transaction has been included.
   440  	dependers := make(map[wire.ShaHash]*list.List)
   441  
   442  	// Create slices to hold the fees and number of signature operations
   443  	// for each of the selected transactions and add an entry for the
   444  	// coinbase.  This allows the code below to simply append details about
   445  	// a transaction as it is selected for inclusion in the final block.
   446  	// However, since the total fees aren't known yet, use a dummy value for
   447  	// the coinbase fee which will be updated later.
   448  	txFees := make([]int64, 0, len(sourceTxns))
   449  	txSigOpCounts := make([]int64, 0, len(sourceTxns))
   450  	txFees = append(txFees, -1) // Updated once known
   451  	txSigOpCounts = append(txSigOpCounts, numCoinbaseSigOps)
   452  
   453  	minrLog.Debugf("Considering %d transactions for inclusion to new block",
   454  		len(sourceTxns))
   455  
   456  mempoolLoop:
   457  	for _, txDesc := range sourceTxns {
   458  		// A block can't have more than one coinbase or contain
   459  		// non-finalized transactions.
   460  		tx := txDesc.Tx
   461  		if blockchain.IsCoinBase(tx) {
   462  			minrLog.Tracef("Skipping coinbase tx %s", tx.Sha())
   463  			continue
   464  		}
   465  		if !blockchain.IsFinalizedTransaction(tx, nextBlockHeight,
   466  			timeSource.AdjustedTime()) {
   467  
   468  			minrLog.Tracef("Skipping non-finalized tx %s", tx.Sha())
   469  			continue
   470  		}
   471  
   472  		// Fetch all of the utxos referenced by the this transaction.
   473  		// NOTE: This intentionally does not fetch inputs from the
   474  		// mempool since a transaction which depends on other
   475  		// transactions in the mempool must come after those
   476  		// dependencies in the final generated block.
   477  		utxos, err := blockManager.chain.FetchUtxoView(tx)
   478  		if err != nil {
   479  			minrLog.Warnf("Unable to fetch utxo view for tx %s: "+
   480  				"%v", tx.Sha(), err)
   481  			continue
   482  		}
   483  
   484  		// Setup dependencies for any transactions which reference
   485  		// other transactions in the mempool so they can be properly
   486  		// ordered below.
   487  		prioItem := &txPrioItem{tx: tx}
   488  		for _, txIn := range tx.MsgTx().TxIn {
   489  			originHash := &txIn.PreviousOutPoint.Hash
   490  			originIndex := txIn.PreviousOutPoint.Index
   491  			utxoEntry := utxos.LookupEntry(originHash)
   492  			if utxoEntry == nil || utxoEntry.IsOutputSpent(originIndex) {
   493  				if !txSource.HaveTransaction(originHash) {
   494  					minrLog.Tracef("Skipping tx %s because "+
   495  						"it references unspent output "+
   496  						"%s which is not available",
   497  						tx.Sha(), txIn.PreviousOutPoint)
   498  					continue mempoolLoop
   499  				}
   500  
   501  				// The transaction is referencing another
   502  				// transaction in the source pool, so setup an
   503  				// ordering dependency.
   504  				depList, exists := dependers[*originHash]
   505  				if !exists {
   506  					depList = list.New()
   507  					dependers[*originHash] = depList
   508  				}
   509  				depList.PushBack(prioItem)
   510  				if prioItem.dependsOn == nil {
   511  					prioItem.dependsOn = make(
   512  						map[wire.ShaHash]struct{})
   513  				}
   514  				prioItem.dependsOn[*originHash] = struct{}{}
   515  
   516  				// Skip the check below. We already know the
   517  				// referenced transaction is available.
   518  				continue
   519  			}
   520  		}
   521  
   522  		// Calculate the final transaction priority using the input
   523  		// value age sum as well as the adjusted transaction size.  The
   524  		// formula is: sum(inputValue * inputAge) / adjustedTxSize
   525  		prioItem.priority = calcPriority(tx.MsgTx(), utxos,
   526  			nextBlockHeight)
   527  
   528  		// Calculate the fee in Satoshi/kB.
   529  		txSize := tx.MsgTx().SerializeSize()
   530  		prioItem.feePerKB = (txDesc.Fee * 1000) / int64(txSize)
   531  		prioItem.fee = txDesc.Fee
   532  
   533  		// Add the transaction to the priority queue to mark it ready
   534  		// for inclusion in the block unless it has dependencies.
   535  		if prioItem.dependsOn == nil {
   536  			heap.Push(priorityQueue, prioItem)
   537  		}
   538  
   539  		// Merge the referenced outputs from the input transactions to
   540  		// this transaction into the block utxo view.  This allows the
   541  		// code below to avoid a second lookup.
   542  		mergeUtxoView(blockUtxos, utxos)
   543  	}
   544  
   545  	minrLog.Tracef("Priority queue len %d, dependers len %d",
   546  		priorityQueue.Len(), len(dependers))
   547  
   548  	// The starting block size is the size of the block header plus the max
   549  	// possible transaction count size, plus the size of the coinbase
   550  	// transaction.
   551  	blockSize := blockHeaderOverhead + uint32(coinbaseTx.MsgTx().SerializeSize())
   552  	blockSigOps := numCoinbaseSigOps
   553  	totalFees := int64(0)
   554  
   555  	// Choose which transactions make it into the block.
   556  	for priorityQueue.Len() > 0 {
   557  		// Grab the highest priority (or highest fee per kilobyte
   558  		// depending on the sort order) transaction.
   559  		prioItem := heap.Pop(priorityQueue).(*txPrioItem)
   560  		tx := prioItem.tx
   561  
   562  		// Grab the list of transactions which depend on this one (if
   563  		// any) and remove the entry for this transaction as it will
   564  		// either be included or skipped, but in either case the deps
   565  		// are no longer needed.
   566  		deps := dependers[*tx.Sha()]
   567  		delete(dependers, *tx.Sha())
   568  
   569  		// Enforce maximum block size.  Also check for overflow.
   570  		txSize := uint32(tx.MsgTx().SerializeSize())
   571  		blockPlusTxSize := blockSize + txSize
   572  		if blockPlusTxSize < blockSize || blockPlusTxSize >= policy.BlockMaxSize {
   573  			minrLog.Tracef("Skipping tx %s because it would exceed "+
   574  				"the max block size", tx.Sha())
   575  			logSkippedDeps(tx, deps)
   576  			continue
   577  		}
   578  
   579  		// Enforce maximum signature operations per block.  Also check
   580  		// for overflow.
   581  		numSigOps := int64(blockchain.CountSigOps(tx))
   582  		if blockSigOps+numSigOps < blockSigOps ||
   583  			blockSigOps+numSigOps > blockchain.MaxSigOpsPerBlock {
   584  			minrLog.Tracef("Skipping tx %s because it would "+
   585  				"exceed the maximum sigops per block", tx.Sha())
   586  			logSkippedDeps(tx, deps)
   587  			continue
   588  		}
   589  		numP2SHSigOps, err := blockchain.CountP2SHSigOps(tx, false,
   590  			blockUtxos)
   591  		if err != nil {
   592  			minrLog.Tracef("Skipping tx %s due to error in "+
   593  				"CountP2SHSigOps: %v", tx.Sha(), err)
   594  			logSkippedDeps(tx, deps)
   595  			continue
   596  		}
   597  		numSigOps += int64(numP2SHSigOps)
   598  		if blockSigOps+numSigOps < blockSigOps ||
   599  			blockSigOps+numSigOps > blockchain.MaxSigOpsPerBlock {
   600  			minrLog.Tracef("Skipping tx %s because it would "+
   601  				"exceed the maximum sigops per block (p2sh)",
   602  				tx.Sha())
   603  			logSkippedDeps(tx, deps)
   604  			continue
   605  		}
   606  
   607  		// Skip free transactions once the block is larger than the
   608  		// minimum block size.
   609  		if sortedByFee &&
   610  			prioItem.feePerKB < int64(policy.TxMinFreeFee) &&
   611  			blockPlusTxSize >= policy.BlockMinSize {
   612  
   613  			minrLog.Tracef("Skipping tx %s with feePerKB %d "+
   614  				"< TxMinFreeFee %d and block size %d >= "+
   615  				"minBlockSize %d", tx.Sha(), prioItem.feePerKB,
   616  				policy.TxMinFreeFee, blockPlusTxSize,
   617  				policy.BlockMinSize)
   618  			logSkippedDeps(tx, deps)
   619  			continue
   620  		}
   621  
   622  		// Prioritize by fee per kilobyte once the block is larger than
   623  		// the priority size or there are no more high-priority
   624  		// transactions.
   625  		if !sortedByFee && (blockPlusTxSize >= policy.BlockPrioritySize ||
   626  			prioItem.priority <= minHighPriority) {
   627  
   628  			minrLog.Tracef("Switching to sort by fees per "+
   629  				"kilobyte blockSize %d >= BlockPrioritySize "+
   630  				"%d || priority %.2f <= minHighPriority %.2f",
   631  				blockPlusTxSize, policy.BlockPrioritySize,
   632  				prioItem.priority, minHighPriority)
   633  
   634  			sortedByFee = true
   635  			priorityQueue.SetLessFunc(txPQByFee)
   636  
   637  			// Put the transaction back into the priority queue and
   638  			// skip it so it is re-priortized by fees if it won't
   639  			// fit into the high-priority section or the priority is
   640  			// too low.  Otherwise this transaction will be the
   641  			// final one in the high-priority section, so just fall
   642  			// though to the code below so it is added now.
   643  			if blockPlusTxSize > policy.BlockPrioritySize ||
   644  				prioItem.priority < minHighPriority {
   645  
   646  				heap.Push(priorityQueue, prioItem)
   647  				continue
   648  			}
   649  		}
   650  
   651  		// Ensure the transaction inputs pass all of the necessary
   652  		// preconditions before allowing it to be added to the block.
   653  		_, err = blockchain.CheckTransactionInputs(tx, nextBlockHeight,
   654  			blockUtxos)
   655  		if err != nil {
   656  			minrLog.Tracef("Skipping tx %s due to error in "+
   657  				"CheckTransactionInputs: %v", tx.Sha(), err)
   658  			logSkippedDeps(tx, deps)
   659  			continue
   660  		}
   661  		err = blockchain.ValidateTransactionScripts(tx, blockUtxos,
   662  			txscript.StandardVerifyFlags, server.sigCache)
   663  		if err != nil {
   664  			minrLog.Tracef("Skipping tx %s due to error in "+
   665  				"ValidateTransactionScripts: %v", tx.Sha(), err)
   666  			logSkippedDeps(tx, deps)
   667  			continue
   668  		}
   669  
   670  		// Spend the transaction inputs in the block utxo view and add
   671  		// an entry for it to ensure any transactions which reference
   672  		// this one have it available as an input and can ensure they
   673  		// aren't double spending.
   674  		spendTransaction(blockUtxos, tx, nextBlockHeight)
   675  
   676  		// Add the transaction to the block, increment counters, and
   677  		// save the fees and signature operation counts to the block
   678  		// template.
   679  		blockTxns = append(blockTxns, tx)
   680  		blockSize += txSize
   681  		blockSigOps += numSigOps
   682  		totalFees += prioItem.fee
   683  		txFees = append(txFees, prioItem.fee)
   684  		txSigOpCounts = append(txSigOpCounts, numSigOps)
   685  
   686  		minrLog.Tracef("Adding tx %s (priority %.2f, feePerKB %d)",
   687  			prioItem.tx.Sha(), prioItem.priority, prioItem.feePerKB)
   688  
   689  		// Add transactions which depend on this one (and also do not
   690  		// have any other unsatisified dependencies) to the priority
   691  		// queue.
   692  		if deps != nil {
   693  			for e := deps.Front(); e != nil; e = e.Next() {
   694  				// Add the transaction to the priority queue if
   695  				// there are no more dependencies after this
   696  				// one.
   697  				item := e.Value.(*txPrioItem)
   698  				delete(item.dependsOn, *tx.Sha())
   699  				if len(item.dependsOn) == 0 {
   700  					heap.Push(priorityQueue, item)
   701  				}
   702  			}
   703  		}
   704  	}
   705  
   706  	// Now that the actual transactions have been selected, update the
   707  	// block size for the real transaction count and coinbase value with
   708  	// the total fees accordingly.
   709  	blockSize -= wire.MaxVarIntPayload -
   710  		uint32(wire.VarIntSerializeSize(uint64(len(blockTxns))))
   711  	coinbaseTx.MsgTx().TxOut[0].Value += totalFees
   712  	txFees[0] = -totalFees
   713  
   714  	// Calculate the required difficulty for the block.  The timestamp
   715  	// is potentially adjusted to ensure it comes after the median time of
   716  	// the last several blocks per the chain consensus rules.
   717  	ts, err := medianAdjustedTime(chainState, timeSource)
   718  	if err != nil {
   719  		return nil, err
   720  	}
   721  	reqDifficulty, err := blockManager.chain.CalcNextRequiredDifficulty(ts)
   722  	if err != nil {
   723  		return nil, err
   724  	}
   725  
   726  	// Create a new block ready to be solved.
   727  	merkles := blockchain.BuildMerkleTreeStore(blockTxns)
   728  	var msgBlock wire.MsgBlock
   729  	msgBlock.Header = wire.BlockHeader{
   730  		Version:    generatedBlockVersion,
   731  		PrevBlock:  *prevHash,
   732  		MerkleRoot: *merkles[len(merkles)-1],
   733  		Timestamp:  ts,
   734  		Bits:       reqDifficulty,
   735  	}
   736  	for _, tx := range blockTxns {
   737  		if err := msgBlock.AddTransaction(tx.MsgTx()); err != nil {
   738  			return nil, err
   739  		}
   740  	}
   741  
   742  	// Finally, perform a full check on the created block against the chain
   743  	// consensus rules to ensure it properly connects to the current best
   744  	// chain with no issues.
   745  	block := godashutil.NewBlock(&msgBlock)
   746  	block.SetHeight(nextBlockHeight)
   747  	if err := blockManager.chain.CheckConnectBlock(block); err != nil {
   748  		return nil, err
   749  	}
   750  
   751  	minrLog.Debugf("Created new block template (%d transactions, %d in "+
   752  		"fees, %d signature operations, %d bytes, target difficulty "+
   753  		"%064x)", len(msgBlock.Transactions), totalFees, blockSigOps,
   754  		blockSize, blockchain.CompactToBig(msgBlock.Header.Bits))
   755  
   756  	return &BlockTemplate{
   757  		Block:           &msgBlock,
   758  		Fees:            txFees,
   759  		SigOpCounts:     txSigOpCounts,
   760  		Height:          nextBlockHeight,
   761  		ValidPayAddress: payToAddress != nil,
   762  	}, nil
   763  }
   764  
   765  // UpdateBlockTime updates the timestamp in the header of the passed block to
   766  // the current time while taking into account the median time of the last
   767  // several blocks to ensure the new time is after that time per the chain
   768  // consensus rules.  Finally, it will update the target difficulty if needed
   769  // based on the new time for the test networks since their target difficulty can
   770  // change based upon time.
   771  func UpdateBlockTime(msgBlock *wire.MsgBlock, bManager *blockManager) error {
   772  	// The new timestamp is potentially adjusted to ensure it comes after
   773  	// the median time of the last several blocks per the chain consensus
   774  	// rules.
   775  	newTimestamp, err := medianAdjustedTime(&bManager.chainState,
   776  		bManager.server.timeSource)
   777  	if err != nil {
   778  		return err
   779  	}
   780  	msgBlock.Header.Timestamp = newTimestamp
   781  
   782  	// If running on a network that requires recalculating the difficulty,
   783  	// do so now.
   784  	if activeNetParams.ResetMinDifficulty {
   785  		difficulty, err := bManager.chain.CalcNextRequiredDifficulty(
   786  			newTimestamp)
   787  		if err != nil {
   788  			return err
   789  		}
   790  		msgBlock.Header.Bits = difficulty
   791  	}
   792  
   793  	return nil
   794  }
   795  
   796  // UpdateExtraNonce updates the extra nonce in the coinbase script of the passed
   797  // block by regenerating the coinbase script with the passed value and block
   798  // height.  It also recalculates and updates the new merkle root that results
   799  // from changing the coinbase script.
   800  func UpdateExtraNonce(msgBlock *wire.MsgBlock, blockHeight int32, extraNonce uint64) error {
   801  	coinbaseScript, err := standardCoinbaseScript(blockHeight, extraNonce)
   802  	if err != nil {
   803  		return err
   804  	}
   805  	if len(coinbaseScript) > blockchain.MaxCoinbaseScriptLen {
   806  		return fmt.Errorf("coinbase transaction script length "+
   807  			"of %d is out of range (min: %d, max: %d)",
   808  			len(coinbaseScript), blockchain.MinCoinbaseScriptLen,
   809  			blockchain.MaxCoinbaseScriptLen)
   810  	}
   811  	msgBlock.Transactions[0].TxIn[0].SignatureScript = coinbaseScript
   812  
   813  	// TODO(davec): A godashutil.Block should use saved in the state to avoid
   814  	// recalculating all of the other transaction hashes.
   815  	// block.Transactions[0].InvalidateCache()
   816  
   817  	// Recalculate the merkle root with the updated extra nonce.
   818  	block := godashutil.NewBlock(msgBlock)
   819  	merkles := blockchain.BuildMerkleTreeStore(block.Transactions())
   820  	msgBlock.Header.MerkleRoot = *merkles[len(merkles)-1]
   821  	return nil
   822  }