mining.go raw

   1  package mining
   2  
   3  import (
   4  	"container/heap"
   5  	"fmt"
   6  	"github.com/p9c/p9/pkg/amt"
   7  	"github.com/p9c/p9/pkg/bits"
   8  	block2 "github.com/p9c/p9/pkg/block"
   9  	"github.com/p9c/p9/pkg/btcaddr"
  10  	"github.com/p9c/p9/pkg/chaincfg"
  11  	"github.com/p9c/p9/pkg/fork"
  12  	"math/rand"
  13  	"time"
  14  	
  15  	"github.com/p9c/p9/pkg/blockchain"
  16  	"github.com/p9c/p9/pkg/chainhash"
  17  	"github.com/p9c/p9/pkg/txscript"
  18  	"github.com/p9c/p9/pkg/util"
  19  	"github.com/p9c/p9/pkg/wire"
  20  )
  21  
  22  const (
  23  	// MinHighPriority is the minimum priority value that allows a transaction to be
  24  	// considered high priority.
  25  	MinHighPriority = amt.SatoshiPerBitcoin * 144.0 / 250
  26  	// blockHeaderOverhead is the max number of bytes it takes to serialize a block
  27  	// header and max possible transaction count.
  28  	blockHeaderOverhead = wire.MaxBlockHeaderPayload + wire.MaxVarIntPayload
  29  	// CoinbaseFlags is added to the coinbase script of a generated block and is
  30  	// used to monitor BIP16 support as well as blocks that are generated via pod.
  31  	CoinbaseFlags = "/P2SH/pod/"
  32  )
  33  
  34  type (
  35  	// TxDesc is a descriptor about a transaction in a transaction source along with
  36  	// additional metadata.
  37  	TxDesc struct {
  38  		// Tx is the transaction associated with the entry.
  39  		Tx *util.Tx
  40  		// Added is the time when the entry was added to the source pool.
  41  		Added time.Time
  42  		// Height is the block height when the entry was added to the the source pool.
  43  		Height int32
  44  		// Fee is the total fee the transaction associated with the entry pays.
  45  		Fee int64
  46  		// FeePerKB is the fee the transaction pays in Satoshi per 1000 bytes.
  47  		FeePerKB int64
  48  	}
  49  	// TxSource represents a source of transactions to consider for inclusion in new
  50  	// blocks. The interface contract requires that all of these methods are safe
  51  	// for concurrent access with respect to the source.
  52  	TxSource interface {
  53  		// LastUpdated returns the last time a transaction was added to or removed from
  54  		// the source pool.
  55  		LastUpdated() time.Time
  56  		// MiningDescs returns a slice of mining descriptors for all the transactions in
  57  		// the source pool.
  58  		MiningDescs() []*TxDesc
  59  		// HaveTransaction returns whether or not the passed transaction hash exists in
  60  		// the source pool.
  61  		HaveTransaction(hash *chainhash.Hash) bool
  62  	}
  63  	// txPrioItem houses a transaction along with extra information that allows the
  64  	// transaction to be prioritized and track dependencies on other transactions
  65  	// which have not been mined into a block yet.
  66  	txPrioItem struct {
  67  		tx       *util.Tx
  68  		fee      int64
  69  		priority float64
  70  		feePerKB int64
  71  		// dependsOn holds a map of transaction hashes which this one depends on.
  72  		//
  73  		// It will only be set when the transaction references other transactions in the
  74  		// source pool and hence must come after them in a block.
  75  		dependsOn map[chainhash.Hash]struct{}
  76  	}
  77  	// txPriorityQueueLessFunc describes a function that can be used as a compare
  78  	// function for a transaction priority queue (txPriorityQueue).
  79  	txPriorityQueueLessFunc func(*txPriorityQueue, int, int) bool
  80  	// txPriorityQueue implements a priority queue of txPrioItem elements that
  81  	// supports an arbitrary compare function as defined by txPriorityQueueLessFunc.
  82  	txPriorityQueue struct {
  83  		lessFunc txPriorityQueueLessFunc
  84  		items    []*txPrioItem
  85  	}
  86  	// BlockTemplate houses a block that has yet to be solved along with additional
  87  	// details about the fees and the number of signature operations for each
  88  	// transaction in the block.
  89  	BlockTemplate struct {
  90  		// Block is a block that is ready to be solved by miners. Thus, it is completely
  91  		// valid with the exception of satisfying the proof-of-work requirement.
  92  		Block *wire.Block
  93  		// Fees contains the amount of fees each transaction in the generated template
  94  		// pays in base units. Since the first transaction is the coinbase, the first
  95  		// entry (offset 0) will contain the negative of the sum of the fees of all
  96  		// other transactions.
  97  		Fees []int64
  98  		// SigOpCosts contains the number of signature operations each transaction in
  99  		// the generated template performs.
 100  		SigOpCosts []int64
 101  		// Height is the height at which the block template connects to the main chain.
 102  		Height int32
 103  		// ValidPayAddress indicates whether or not the template coinbase pays to an
 104  		// address or is redeemable by anyone. See the documentation on NewBlockTemplate
 105  		// for details on which this can be useful to generate templates without a
 106  		// coinbase payment address.
 107  		ValidPayAddress bool
 108  		// WitnessCommitment is a commitment to the witness data (if any) within the
 109  		// block. This field will only be populated once segregated witness has been
 110  		// activated, and the block contains a transaction which has witness data.
 111  		WitnessCommitment []byte
 112  	}
 113  	// BlkTmplGenerator provides a type that can be used to generate block templates
 114  	// based on a given mining policy and source of transactions to choose from. It
 115  	// also houses additional state required in order to ensure the templates are
 116  	// built on top of the current best chain and adhere to the consensus rules.
 117  	BlkTmplGenerator struct {
 118  		Policy      *Policy
 119  		ChainParams *chaincfg.Params
 120  		TxSource    TxSource
 121  		Chain       *blockchain.BlockChain
 122  		TimeSource  blockchain.MedianTimeSource
 123  		SigCache    *txscript.SigCache
 124  		HashCache   *txscript.HashCache
 125  	}
 126  )
 127  
 128  // Len returns the number of items in the priority queue.
 129  //
 130  // It is part of the heap.Interface implementation.
 131  func (pq *txPriorityQueue) Len() int {
 132  	return len(pq.items)
 133  }
 134  
 135  // Less returns whether the item in the priority queue with index i should txsort
 136  // before the item with index j by deferring to the assigned less function.
 137  //
 138  // It is part of the heap.Interface implementation.
 139  func (pq *txPriorityQueue) Less(i, j int) bool {
 140  	return pq.lessFunc(pq, i, j)
 141  }
 142  
 143  // Swap swaps the items at the passed indices in the priority queue.
 144  //
 145  // It is part of the heap.Interface implementation.
 146  func (pq *txPriorityQueue) Swap(i, j int) {
 147  	pq.items[i], pq.items[j] = pq.items[j], pq.items[i]
 148  }
 149  
 150  // Push pushes the passed item onto the priority queue.
 151  //
 152  // It is part of the heap.Interface implementation.
 153  func (pq *txPriorityQueue) Push(x interface{}) {
 154  	pq.items = append(pq.items, x.(*txPrioItem))
 155  }
 156  
 157  // Pop removes the highest priority item (according to Less) from the priority
 158  // queue and returns it.
 159  //
 160  // It is part of the heap.Interface implementation.
 161  func (pq *txPriorityQueue) Pop() interface{} {
 162  	n := len(pq.items)
 163  	item := pq.items[n-1]
 164  	pq.items[n-1] = nil
 165  	pq.items = pq.items[0 : n-1]
 166  	return item
 167  }
 168  
 169  // SetLessFunc sets the compare function for the priority queue to the provided
 170  // function. It also invokes heap.Init on the priority queue using the new
 171  // function so it can immediately be used with heap.Push/Pop.
 172  func (pq *txPriorityQueue) SetLessFunc(lessFunc txPriorityQueueLessFunc) {
 173  	pq.lessFunc = lessFunc
 174  	heap.Init(pq)
 175  }
 176  
 177  // txPQByPriority sorts a txPriorityQueue by transaction priority and then fees
 178  // per kilobyte.
 179  func txPQByPriority(pq *txPriorityQueue, i, j int) bool {
 180  	// Using > here so that pop gives the highest priority item as opposed to the
 181  	// lowest. Sort by priority first, then fee.
 182  	if pq.items[i].priority == pq.items[j].priority {
 183  		return pq.items[i].feePerKB > pq.items[j].feePerKB
 184  	}
 185  	return pq.items[i].priority > pq.items[j].priority
 186  }
 187  
 188  // txPQByFee sorts a txPriorityQueue by fees per kilobyte and then transaction
 189  // priority.
 190  func txPQByFee(pq *txPriorityQueue, i, j int) bool {
 191  	// Using > here so that pop gives the highest fee item as opposed to the lowest.
 192  	// Sort by fee first, then priority.
 193  	if pq.items[i].feePerKB == pq.items[j].feePerKB {
 194  		return pq.items[i].priority > pq.items[j].priority
 195  	}
 196  	return pq.items[i].feePerKB > pq.items[j].feePerKB
 197  }
 198  
 199  // newTxPriorityQueue returns a new transaction priority queue that reserves the
 200  // passed amount of space for the elements. The new priority queue uses either
 201  // the txPQByPriority or the txPQByFee compare function depending on the
 202  // sortByFee parameter and is already initialized for use with heap.Push/Pop.
 203  // The priority queue can grow larger than the reserved space, but extra copies
 204  // of the underlying array can be avoided by reserving a sane value.
 205  func newTxPriorityQueue(reserve int, sortByFee bool) *txPriorityQueue {
 206  	pq := &txPriorityQueue{
 207  		items: make([]*txPrioItem, 0, reserve),
 208  	}
 209  	if sortByFee {
 210  		pq.SetLessFunc(txPQByFee)
 211  	} else {
 212  		pq.SetLessFunc(txPQByPriority)
 213  	}
 214  	return pq
 215  }
 216  
 217  // mergeUtxoView adds all of the entries in viewB to viewA. The result is that
 218  // viewA will contain all of its original entries plus all of the entries in
 219  // viewB. It will replace any entries in viewB which also exist in viewA if the
 220  // entry in viewA is spent.
 221  func mergeUtxoView(viewA *blockchain.UtxoViewpoint, viewB *blockchain.UtxoViewpoint) {
 222  	viewAEntries := viewA.Entries()
 223  	for outpoint, entryB := range viewB.Entries() {
 224  		if entryA, exists := viewAEntries[outpoint]; !exists ||
 225  			entryA == nil || entryA.IsSpent() {
 226  			viewAEntries[outpoint] = entryB
 227  		}
 228  	}
 229  }
 230  
 231  // standardCoinbaseScript returns a standard script suitable for use as the
 232  // signature script of the coinbase transaction of a new block. In particular,
 233  // it starts with the block height that is required by version 2 blocks and adds
 234  // the extra nonce as well as additional coinbase flags.
 235  func standardCoinbaseScript(nextBlockHeight int32, extraNonce uint64) ([]byte, error) {
 236  	return txscript.NewScriptBuilder().AddInt64(int64(nextBlockHeight)).
 237  		AddInt64(int64(extraNonce)).
 238  		AddData([]byte(CoinbaseFlags)).
 239  		Script()
 240  }
 241  
 242  // createCoinbaseTx returns a coinbase transaction paying an appropriate subsidy
 243  // based on the passed block height to the provided address. When the address is
 244  // nil, the coinbase transaction will instead be redeemable by anyone. See the
 245  // comment for NewBlockTemplate for more information about why the nil address
 246  // handling is useful.
 247  func createCoinbaseTx(
 248  	params *chaincfg.Params, coinbaseScript []byte, nextBlockHeight int32,
 249  	addr btcaddr.Address, version int32,
 250  ) (*util.Tx, error) {
 251  	// if this is the hard fork activation height coming up, we create the special
 252  	// disbursement coinbase
 253  	if nextBlockHeight == fork.List[1].ActivationHeight &&
 254  		params.Net == wire.MainNet ||
 255  		nextBlockHeight == fork.List[1].TestnetStart &&
 256  			params.Net == wire.TestNet3 {
 257  		return blockchain.CreateHardForkSubsidyTx(params, coinbaseScript, nextBlockHeight, addr, version)
 258  	}
 259  	
 260  	// Create the script to pay to the provided payment address if one was
 261  	// specified. Otherwise create a script that allows the coinbase to be
 262  	// redeemable by anyone.
 263  	var pkScript []byte
 264  	if addr != nil {
 265  		var e error
 266  		pkScript, e = txscript.PayToAddrScript(addr)
 267  		if e != nil {
 268  			return nil, e
 269  		}
 270  	} else {
 271  		var e error
 272  		scriptBuilder := txscript.NewScriptBuilder()
 273  		pkScript, e = scriptBuilder.AddOp(txscript.OP_TRUE).Script()
 274  		if e != nil {
 275  			return nil, e
 276  		}
 277  	}
 278  	tx := wire.NewMsgTx(wire.TxVersion)
 279  	tx.AddTxIn(
 280  		&wire.TxIn{
 281  			// Coinbase transactions have no inputs, so previous outpoint is zero hash and
 282  			// max index.
 283  			PreviousOutPoint: *wire.NewOutPoint(
 284  				&chainhash.Hash{},
 285  				wire.MaxPrevOutIndex,
 286  			),
 287  			SignatureScript: coinbaseScript,
 288  			Sequence:        wire.MaxTxInSequenceNum,
 289  		},
 290  	)
 291  	tx.AddTxOut(
 292  		&wire.TxOut{
 293  			Value:    blockchain.CalcBlockSubsidy(nextBlockHeight, params, version),
 294  			PkScript: pkScript,
 295  		},
 296  	)
 297  	return util.NewTx(tx), nil
 298  }
 299  
 300  // spendTransaction updates the passed view by marking the inputs to the passed
 301  // transaction as spent. It also adds all outputs in the passed transaction
 302  // which are not provably unspendable as available unspent transaction outputs.
 303  func spendTransaction(utxoView *blockchain.UtxoViewpoint, tx *util.Tx, height int32) (e error) {
 304  	for _, txIn := range tx.MsgTx().TxIn {
 305  		entry := utxoView.LookupEntry(txIn.PreviousOutPoint)
 306  		if entry != nil {
 307  			entry.Spend()
 308  		}
 309  	}
 310  	utxoView.AddTxOuts(tx, height)
 311  	return nil
 312  }
 313  
 314  // logSkippedDeps logs any dependencies which are also skipped as a result of
 315  // skipping a transaction while generating a block template at the trace level.
 316  func logSkippedDeps(tx *util.Tx, deps map[chainhash.Hash]*txPrioItem) {
 317  	if deps == nil {
 318  		return
 319  	}
 320  	for _, item := range deps {
 321  		T.F(
 322  			"skipping tx %s since it depends on %s", item.tx.Hash(),
 323  			tx.Hash(),
 324  		)
 325  	}
 326  }
 327  
 328  // MinimumMedianTime returns the minimum allowed timestamp for a block building
 329  // on the end of the provided best chain. In particular, it is one second after
 330  // the median timestamp of the last several blocks per the chain consensus
 331  // rules.
 332  func MinimumMedianTime(chainState *blockchain.BestState) time.Time {
 333  	return chainState.MedianTime.Add(time.Second)
 334  }
 335  
 336  // medianAdjustedTime returns the current time adjusted to ensure it is at least
 337  // one second after the median timestamp of the last several blocks per the
 338  // chain consensus rules.
 339  func medianAdjustedTime(chainState *blockchain.BestState, timeSource blockchain.MedianTimeSource) time.Time {
 340  	// The timestamp for the block must not be before the median timestamp of the
 341  	// last several blocks. Thus, choose the maximum between the current time and
 342  	// one second after the past median time. The current timestamp is truncated to
 343  	// a second boundary before comparison since a block timestamp does not
 344  	// supported a precision greater than one second.
 345  	newTimestamp := timeSource.AdjustedTime()
 346  	minTimestamp := MinimumMedianTime(chainState)
 347  	if newTimestamp.Before(minTimestamp) {
 348  		newTimestamp = minTimestamp
 349  	}
 350  	return newTimestamp
 351  }
 352  
 353  // NewBlkTmplGenerator returns a new block template generator for the given
 354  // policy using transactions from the provided transaction source. The
 355  // additional state-related fields are required in order to ensure the templates
 356  // are built on top of the current best chain and adhere to the consensus rules.
 357  func NewBlkTmplGenerator(
 358  	policy *Policy, params *chaincfg.Params,
 359  	txSource TxSource, chain *blockchain.BlockChain,
 360  	timeSource blockchain.MedianTimeSource,
 361  	sigCache *txscript.SigCache,
 362  	hashCache *txscript.HashCache,
 363  ) *BlkTmplGenerator {
 364  	return &BlkTmplGenerator{
 365  		Policy:      policy,
 366  		ChainParams: params,
 367  		TxSource:    txSource,
 368  		Chain:       chain,
 369  		TimeSource:  timeSource,
 370  		SigCache:    sigCache,
 371  		HashCache:   hashCache,
 372  	}
 373  }
 374  
 375  // NewBlockTemplate returns a new block template that is ready to be solved
 376  // using the transactions from the passed transaction source pool and a coinbase
 377  // that either pays to the passed address if it is not nil, or a coinbase that
 378  // is redeemable by anyone if the passed address is nil. The nil address
 379  // functionality is useful since there are cases such as the getblocktemplate
 380  // RPC where external mining software is responsible for creating their own
 381  // coinbase which will replace the one generated for the block template. Thus
 382  // the need to have configured address can be avoided.
 383  //
 384  // The transactions selected and included are prioritized according to several
 385  // factors. First, each transaction has a priority calculated based on its
 386  // value, age of inputs, and size.
 387  //
 388  // Transactions which consist of larger amounts, older inputs, and small txsizes
 389  // have the highest priority. Second, a fee per kilobyte is calculated for each
 390  // transaction. Transactions with a higher fee per kilobyte are preferred.
 391  // Finally, the block generation related policy settings are all taken into
 392  // account.
 393  //
 394  // Transactions which only spend outputs from other transactions already in the
 395  // block chain are immediately added to a priority queue which either
 396  // prioritizes based on the priority (then fee per kilobyte) or the fee per
 397  // kilobyte (then priority) depending on whether or not the BlockPrioritySize
 398  // policy setting allots space for high-priority transactions.
 399  //
 400  // Transactions which spend outputs from other transactions in the source pool
 401  // are added to a dependency map so they can be added to the priority queue once
 402  // the transactions they depend on have been included. Once the high-priority
 403  // area (if configured) has been filled with transactions, or the priority falls
 404  // below what is considered high-priority, the priority queue is updated to
 405  // prioritize by fees per kilobyte (then priority).
 406  //
 407  // When the fees per kilobyte drop below the TxMinFreeFee policy setting, the
 408  // transaction will be skipped unless the BlockMinSize policy setting is
 409  // nonzero, in which case the block will be filled with the low-fee/free
 410  // transactions until the block size reaches that minimum size. Any transactions
 411  // which would cause the block to exceed the BlockMaxSize policy setting, exceed
 412  // the maximum allowed signature operations per block, or otherwise cause the
 413  // block to be invalid are skipped.
 414  //
 415  // Given the above, a block generated by this function is of the following form:
 416  //
 417  //   -----------------------------------  --  --
 418  //  |      Coinbase Transaction         |   |   |
 419  //  |-----------------------------------|   |   |
 420  //  |                                   |   |   | ----- policy.BlockPrioritySize
 421  //  |   High-priority Transactions      |   |   |
 422  //  |                                   |   |   |
 423  //  |-----------------------------------|   | --
 424  //  |                                   |   |
 425  //  |                                   |   |
 426  //  |                                   |   |--- policy.BlockMaxSize
 427  //  |  Transactions prioritized by fee  |   |
 428  //  |  until <= policy.TxMinFreeFee     |   |
 429  //  |                                   |   |
 430  //  |                                   |   |
 431  //  |                                   |   |
 432  //  |-----------------------------------|   |
 433  //  |  Low-fee/Non high-priority (free) |   |
 434  //  |  transactions (while block size   |   |
 435  //  |  <= policy.BlockMinSize)          |   |
 436  //   -----------------------------------  --
 437  func (g *BlkTmplGenerator) NewBlockTemplate(payToAddress btcaddr.Address, algo string,) (*BlockTemplate, error) {
 438  	T.Ln("NewBlockTemplate", algo)
 439  	if algo == "" {
 440  		algo = "random"
 441  	}
 442  	// Extend the most recently known best block.
 443  	best := g.Chain.BestSnapshot()
 444  	nextBlockHeight := best.Height + 1
 445  	// sanitise the version number
 446  	vers := fork.GetAlgoVer(algo, nextBlockHeight)
 447  	algo = fork.GetAlgoName(vers, nextBlockHeight)
 448  	// Create a standard coinbase transaction paying to the provided address.
 449  	//
 450  	// NOTE: The coinbase value will be updated to include the fees from the
 451  	// selected transactions later after they have actually been selected. It is
 452  	// created here to detect any errors early before potentially doing a lot of
 453  	// work below. The extra nonce helps ensure the transaction is not a duplicate
 454  	// transaction (paying the same value to the same public key address would
 455  	// otherwise be an identical transaction for block version 1).
 456  	rand.Seed(time.Now().UnixNano())
 457  	extraNonce := rand.Uint64()
 458  	var e error
 459  	var coinbaseScript []byte
 460  	if coinbaseScript, e = standardCoinbaseScript(nextBlockHeight, extraNonce); E.Chk(e) {
 461  		return nil, e
 462  	}
 463  	var coinbaseTx *util.Tx
 464  	if coinbaseTx, e = createCoinbaseTx(
 465  		g.ChainParams, coinbaseScript, nextBlockHeight, payToAddress,
 466  		vers,
 467  	); E.Chk(e) {
 468  		return nil, e
 469  	}
 470  	coinbaseSigOpCost := int64(blockchain.CountSigOps(coinbaseTx))
 471  	// Get the current source transactions and create a priority queue to hold the
 472  	// transactions which are ready for inclusion into a block along with some
 473  	// priority related and fee metadata. Reserve the same number of items that are
 474  	// available for the priority queue. Also, choose the initial txsort order for the
 475  	// priority queue based on whether or not there is an area allocated for
 476  	// high-priority transactions.
 477  	sourceTxns := g.TxSource.MiningDescs()
 478  	sortedByFee := g.Policy.BlockPrioritySize == 0
 479  	priorityQueue := newTxPriorityQueue(len(sourceTxns), sortedByFee)
 480  	// Create a slice to hold the transactions to be included in the generated block
 481  	// with reserved space. Also create a utxo view to house all of the input
 482  	// transactions so multiple lookups can be avoided.
 483  	blockTxns := make([]*util.Tx, 0, len(sourceTxns))
 484  	blockTxns = append(blockTxns, coinbaseTx)
 485  	blockUtxos := blockchain.NewUtxoViewpoint()
 486  	// dependers is used to track transactions which depend on another transaction
 487  	// in the source pool. This, in conjunction with the dependsOn map kept with
 488  	// each dependent transaction helps quickly determine which dependent
 489  	// transactions are now eligible for inclusion in the block once each
 490  	// transaction has been included.
 491  	dependers := make(map[chainhash.Hash]map[chainhash.Hash]*txPrioItem)
 492  	// Create slices to hold the fees and number of signature operations for each of
 493  	// the selected transactions and add an entry for the coinbase. This allows the
 494  	// code below to simply append details about a transaction as it is selected for
 495  	// inclusion in the final block. However, since the total fees aren't known yet,
 496  	// use a dummy value for the coinbase fee which will be updated later.
 497  	txFees := make([]int64, 0, len(sourceTxns))
 498  	txSigOpCosts := make([]int64, 0, len(sourceTxns))
 499  	txFees = append(txFees, -1) // Updated once known
 500  	txSigOpCosts = append(txSigOpCosts, coinbaseSigOpCost)
 501  	T.F("considering %d transactions for inclusion to new block", len(sourceTxns))
 502  mempoolLoop:
 503  	for _, txDesc := range sourceTxns {
 504  		// A block can't have more than one coinbase or contain non-finalized
 505  		// transactions.
 506  		tx := txDesc.Tx
 507  		if blockchain.IsCoinBase(tx) {
 508  			T.C(
 509  				func() string {
 510  					return fmt.Sprintf("skipping coinbase tx %s", tx.Hash())
 511  				},
 512  			)
 513  			continue
 514  		}
 515  		if !blockchain.IsFinalizedTransaction(
 516  			tx, nextBlockHeight,
 517  			g.TimeSource.AdjustedTime(),
 518  		) {
 519  			T.C(
 520  				func() string {
 521  					return "skipping non-finalized tx " + tx.Hash().String()
 522  				},
 523  			)
 524  			continue
 525  		}
 526  		// Fetch all of the utxos referenced by the this transaction.
 527  		//
 528  		// NOTE: This intentionally does not fetch inputs from the mempool since a
 529  		// transaction which depends on other transactions in the mempool must come
 530  		// after those dependencies in the final generated block.
 531  		var utxos *blockchain.UtxoViewpoint
 532  		utxos, e = g.Chain.FetchUtxoView(tx)
 533  		if e != nil {
 534  			W.C(
 535  				func() string {
 536  					return "unable to fetch utxo view for tx " + tx.Hash().String() + ": " + e.Error()
 537  				},
 538  			)
 539  			continue
 540  		}
 541  		// Setup dependencies for any transactions which reference other transactions in
 542  		// the mempool so they can be properly ordered below.
 543  		prioItem := &txPrioItem{tx: tx}
 544  		for _, txIn := range tx.MsgTx().TxIn {
 545  			originHash := &txIn.PreviousOutPoint.Hash
 546  			entry := utxos.LookupEntry(txIn.PreviousOutPoint)
 547  			if entry == nil || entry.IsSpent() {
 548  				if !g.TxSource.HaveTransaction(originHash) {
 549  					T.C(
 550  						func() string {
 551  							return "skipping tx %s because it references unspent output %s which is not available" +
 552  								tx.Hash().String() +
 553  								txIn.PreviousOutPoint.String()
 554  						},
 555  					)
 556  					continue mempoolLoop
 557  				}
 558  				// The transaction is referencing another transaction in the source pool, so
 559  				// setup an ordering dependency.
 560  				deps, exists := dependers[*originHash]
 561  				if !exists {
 562  					deps = make(map[chainhash.Hash]*txPrioItem)
 563  					dependers[*originHash] = deps
 564  				}
 565  				deps[*prioItem.tx.Hash()] = prioItem
 566  				if prioItem.dependsOn == nil {
 567  					prioItem.dependsOn = make(
 568  						map[chainhash.Hash]struct{},
 569  					)
 570  				}
 571  				prioItem.dependsOn[*originHash] = struct{}{}
 572  				// Skip the check below. We already know the referenced transaction is
 573  				// available.
 574  				continue
 575  			}
 576  		}
 577  		// Calculate the final transaction priority using the input value age sum as
 578  		// well as the adjusted transaction size. The formula is: sum (inputValue *
 579  		// inputAge) / adjustedTxSize
 580  		prioItem.priority = CalcPriority(
 581  			tx.MsgTx(), utxos,
 582  			nextBlockHeight,
 583  		)
 584  		// Calculate the fee in Satoshi/kB.
 585  		prioItem.feePerKB = txDesc.FeePerKB
 586  		prioItem.fee = txDesc.Fee
 587  		// Add the transaction to the priority queue to mark it ready for inclusion in
 588  		// the block unless it has dependencies.
 589  		if prioItem.dependsOn == nil {
 590  			heap.Push(priorityQueue, prioItem)
 591  		}
 592  		// Merge the referenced outputs from the input transactions to this transaction
 593  		// into the block utxo view. This allows the code below to avoid a second
 594  		// lookup.
 595  		mergeUtxoView(blockUtxos, utxos)
 596  	}
 597  	// The starting block size is the size of the block header plus the max possible
 598  	// transaction count size, plus the size of the coinbase transaction.
 599  	blockWeight := uint32((blockHeaderOverhead) + blockchain.GetTransactionWeight(coinbaseTx))
 600  	blockSigOpCost := coinbaseSigOpCost
 601  	totalFees := int64(0)
 602  	// // Query the version bits state to see if segwit has been activated, if so then
 603  	// // this means that we'll include any transactions with witness data in the
 604  	// // mempool, and also add the witness commitment as an OP_RETURN output in the
 605  	// // coinbase transaction.
 606  	// var segwitState blockchain.ThresholdState
 607  	// segwitState, e = g.Chain.ThresholdState(chaincfg.DeploymentSegwit)
 608  	// if e != nil  {
 609  	// 		// 	return nil, e
 610  	// }
 611  	// segwitActive := segwitState == blockchain.ThresholdActive
 612  	// witnessIncluded := false
 613  	// Choose which transactions make it into the block.
 614  	for priorityQueue.Len() > 0 {
 615  		// Grab the highest priority (or highest fee per kilobyte depending on the txsort
 616  		// order) transaction.
 617  		prioItem := heap.Pop(priorityQueue).(*txPrioItem)
 618  		tx := prioItem.tx
 619  		// switch {
 620  		// // If segregated witness has not been activated yet, then we shouldn't include
 621  		// // any witness transactions in the block.
 622  		// case !segwitActive && tx.HasWitness():
 623  		// 	continue
 624  		// 	// Otherwise, Keep track of if we've included a transaction with witness data or not. If so, then we'll need
 625  		// 	// to include the witness commitment as the last output in the coinbase transaction.
 626  		// case segwitActive && !witnessIncluded && tx.HasWitness():
 627  		// 	// If we're about to include a transaction bearing witness data, then we'll also
 628  		// 	// need to include a witness commitment in the coinbase transaction. Therefore,
 629  		// 	// we account for the additional weight within the block with a model coinbase
 630  		// 	// tx with a witness commitment.
 631  		// 	coinbaseCopy := util.NewTx(coinbaseTx.MsgTx().Copy())
 632  		// 	coinbaseCopy.MsgTx().TxIn[0].Witness = [][]byte{
 633  		// 		bytes.Repeat(
 634  		// 			[]byte("a"),
 635  		// 			blockchain.CoinbaseWitnessDataLen,
 636  		// 		),
 637  		// 	}
 638  		// 	coinbaseCopy.MsgTx().AddTxOut(
 639  		// 		&wire.TxOut{
 640  		// 			PkScript: bytes.Repeat(
 641  		// 				[]byte("a"),
 642  		// 				blockchain.CoinbaseWitnessPkScriptLength,
 643  		// 			),
 644  		// 		},
 645  		// 	)
 646  		// 	// In order to accurately account for the weight addition due to this coinbase
 647  		// 	// transaction, we'll add the difference of the transaction before and after the
 648  		// 	// addition of the commitment to the block weight.
 649  		// 	weightDiff := blockchain.GetTransactionWeight(coinbaseCopy) -
 650  		// 		blockchain.GetTransactionWeight(coinbaseTx)
 651  		// 	blockWeight += uint32(weightDiff)
 652  		// 	witnessIncluded = true
 653  		// }
 654  		// Grab any transactions which depend on this one.
 655  		deps := dependers[*tx.Hash()]
 656  		// Enforce maximum block size.  Also check for overflow.
 657  		txWeight := uint32(blockchain.GetTransactionWeight(tx))
 658  		blockPlusTxWeight := blockWeight + txWeight
 659  		if blockPlusTxWeight < blockWeight ||
 660  			blockPlusTxWeight >= g.Policy.BlockMaxWeight {
 661  			T.F("skipping tx %s because it would exceed the max block weight", tx.Hash())
 662  			logSkippedDeps(tx, deps)
 663  			continue
 664  		}
 665  		// Enforce maximum signature operation cost per block. Also check for overflow.
 666  		var sigOpCost int
 667  		sigOpCost, e = blockchain.GetSigOpCost(tx, false, blockUtxos, true)
 668  		if e != nil {
 669  			T.C(
 670  				func() string {
 671  					return "skipping tx " + tx.Hash().String() +
 672  						"due to error in GetSigOpCost: " + e.Error()
 673  				},
 674  			)
 675  			logSkippedDeps(tx, deps)
 676  			continue
 677  		}
 678  		if blockSigOpCost+int64(sigOpCost) < blockSigOpCost ||
 679  			blockSigOpCost+int64(sigOpCost) > blockchain.MaxBlockSigOpsCost {
 680  			T.C(
 681  				func() string {
 682  					return "skipping tx " + tx.Hash().String() +
 683  						" because it would exceed the maximum sigops per block"
 684  				},
 685  			)
 686  			logSkippedDeps(tx, deps)
 687  			continue
 688  		}
 689  		// Skip free transactions once the block is larger than the minimum block size.
 690  		if sortedByFee &&
 691  			prioItem.feePerKB < int64(g.Policy.TxMinFreeFee) &&
 692  			blockPlusTxWeight >= g.Policy.BlockMinWeight {
 693  			T.C(
 694  				func() string {
 695  					return fmt.Sprintf(
 696  						"skipping tx %v with feePerKB %v < TxMinFreeFee %v and block weight %v >= minBlockWeight %v",
 697  						tx.Hash(),
 698  						prioItem.feePerKB,
 699  						g.Policy.TxMinFreeFee,
 700  						blockPlusTxWeight,
 701  						g.Policy.BlockMinWeight,
 702  					)
 703  				},
 704  			)
 705  			logSkippedDeps(tx, deps)
 706  			continue
 707  		}
 708  		// Prioritize by fee per kilobyte once the block is larger than the priority
 709  		// size or there are no more high-priority transactions.
 710  		if !sortedByFee && (blockPlusTxWeight >= g.Policy.BlockPrioritySize ||
 711  			prioItem.priority <= MinHighPriority.ToDUO()) {
 712  			T.F(
 713  				"switching to txsort by fees per kilobyte blockSize %d"+
 714  					" >= BlockPrioritySize %d || priority %.2f <= minHighPriority %.2f",
 715  				blockPlusTxWeight,
 716  				g.Policy.BlockPrioritySize,
 717  				prioItem.priority,
 718  				MinHighPriority,
 719  			)
 720  			sortedByFee = true
 721  			priorityQueue.SetLessFunc(txPQByFee)
 722  		}
 723  		// Put the transaction back into the priority queue and skip it so it is
 724  		// re-prioritized by fees if it won't fit into the high-priority section or the
 725  		// priority is too low. Otherwise this transaction will be the final one in the
 726  		// high-priority section, so just fall though to the code below so it is added
 727  		// now.
 728  		if blockPlusTxWeight > g.Policy.BlockPrioritySize ||
 729  			prioItem.priority < MinHighPriority.ToDUO() {
 730  			heap.Push(priorityQueue, prioItem)
 731  			continue
 732  		}
 733  		
 734  		// Ensure the transaction inputs pass all of the necessary preconditions before
 735  		// allowing it to be added to the block.
 736  		_, e = blockchain.CheckTransactionInputs(
 737  			tx, nextBlockHeight,
 738  			blockUtxos, g.ChainParams,
 739  		)
 740  		if e != nil {
 741  			T.F(
 742  				"skipping tx %s due to error in CheckTransactionInputs: %v",
 743  				tx.Hash(), e,
 744  			)
 745  			logSkippedDeps(tx, deps)
 746  			continue
 747  		}
 748  		if e = blockchain.ValidateTransactionScripts(
 749  			g.Chain, tx, blockUtxos,
 750  			txscript.StandardVerifyFlags, g.SigCache,
 751  			g.HashCache,
 752  		); E.Chk(e) {
 753  			T.F(
 754  				"skipping tx %s due to error in ValidateTransactionScripts: %v",
 755  				tx.Hash(), e,
 756  			)
 757  			logSkippedDeps(tx, deps)
 758  			continue
 759  		}
 760  		
 761  		// Spend the transaction inputs in the block utxo view and add an entry for it
 762  		// to ensure any transactions which reference this one have it available as an
 763  		// input and can ensure they aren't double spending.
 764  		if e = spendTransaction(blockUtxos, tx, nextBlockHeight); E.Chk(e) {
 765  		}
 766  		// Add the transaction to the block, increment counters, and save the fees and
 767  		// signature operation counts to the block template.
 768  		blockTxns = append(blockTxns, tx)
 769  		blockWeight += txWeight
 770  		blockSigOpCost += int64(sigOpCost)
 771  		totalFees += prioItem.fee
 772  		txFees = append(txFees, prioItem.fee)
 773  		txSigOpCosts = append(txSigOpCosts, int64(sigOpCost))
 774  		T.F(
 775  			"adding tx %s (priority %.2f, feePerKB %.2f)",
 776  			prioItem.tx.Hash(),
 777  			prioItem.priority,
 778  			prioItem.feePerKB,
 779  		)
 780  		// Add transactions which depend on this one (and also do not have any other
 781  		// unsatisfied dependencies) to the priority queue.
 782  		for _, item := range deps {
 783  			// Add the transaction to the priority queue if there are no more dependencies
 784  			// after this one.
 785  			delete(item.dependsOn, *tx.Hash())
 786  			if len(item.dependsOn) == 0 {
 787  				heap.Push(priorityQueue, item)
 788  			}
 789  		}
 790  	}
 791  	// Now that the actual transactions have been selected, update the block weight
 792  	// for the real transaction count and coinbase value with the total fees
 793  	// accordingly.
 794  	blockWeight -= wire.MaxVarIntPayload -
 795  		(uint32(wire.VarIntSerializeSize(uint64(len(blockTxns)))))
 796  	coinbaseTx.MsgTx().TxOut[0].Value += totalFees
 797  	txFees[0] = -totalFees
 798  	// If segwit is active and we included transactions with witness data, then
 799  	// // we'll need to include a commitment to the witness data in an OP_RETURN output
 800  	// // within the coinbase transaction.
 801  	// var witnessCommitment []byte
 802  	// if witnessIncluded {
 803  	// 	// The witness of the coinbase transaction MUST be exactly 32-bytes of all
 804  	// 	// zeroes.
 805  	// 	var witnessNonce [blockchain.CoinbaseWitnessDataLen]byte
 806  	// 	coinbaseTx.MsgTx().TxIn[0].Witness = wire.TxWitness{witnessNonce[:]}
 807  	// 	// Next, obtain the merkle root of a tree which consists of the wtxid of all
 808  	// 	// transactions in the block. The coinbase transaction will have a special wtxid
 809  	// 	// of all zeroes.
 810  	// 	witnessMerkleTree := blockchain.BuildMerkleTreeStore(
 811  	// 		blockTxns,
 812  	// 		true,
 813  	// 	)
 814  	// 	witnessMerkleRoot := witnessMerkleTree.GetRoot()
 815  	// 	// The preimage to the witness commitment is: witnessRoot || coinbaseWitness
 816  	// 	var witnessPreimage [64]byte
 817  	// 	copy(witnessPreimage[:32], witnessMerkleRoot[:])
 818  	// 	copy(witnessPreimage[32:], witnessNonce[:])
 819  	// 	// The witness commitment itself is the double-sha256 of the witness preimage
 820  	// 	// generated above. With the commitment generated, the witness script for the
 821  	// 	// output is: OP_RETURN OP_DATA_36 {0xaa21a9ed || witnessCommitment}. The
 822  	// 	// leading prefix is referred to as the "witness magic bytes".
 823  	// 	witnessCommitment = chainhash.DoubleHashB(witnessPreimage[:])
 824  	// 	witnessScript := append(blockchain.WitnessMagicBytes, witnessCommitment...)
 825  	// 	// Finally, create the OP_RETURN carrying witness commitment output as an
 826  	// 	// additional output within the coinbase.
 827  	// 	commitmentOutput := &wire.TxOut{
 828  	// 		Value:    0,
 829  	// 		PkScript: witnessScript,
 830  	// 	}
 831  	// 	coinbaseTx.MsgTx().TxOut = append(
 832  	// 		coinbaseTx.MsgTx().TxOut,
 833  	// 		commitmentOutput,
 834  	// 	)
 835  	// }
 836  	// Calculate the required difficulty for the block. The timestamp is potentially
 837  	// adjusted to ensure it comes after the median time of the last several blocks
 838  	// per the chain consensus rules.
 839  	ts := medianAdjustedTime(best, g.TimeSource)
 840  	T.Ln("legacy ts", ts)
 841  	if fork.GetCurrent(nextBlockHeight) > 0 {
 842  		ots := g.Chain.BestChain.NodeByHeight(best.Height).Header().Timestamp.Truncate(time.Second).Add(time.Second)
 843  		D.Ln("prev timestamp+1", ots)
 844  		tn := time.Now().Truncate(time.Second)
 845  		if tn.After(ots) {
 846  			ts = tn
 847  		} else {
 848  			ts = ots
 849  		}
 850  		T.Ln("plan9 ts", ts)
 851  	}
 852  	// T.Ln("algo ", ts, " ", algo)
 853  	var reqDifficulty uint32
 854  	if reqDifficulty, e = g.Chain.CalcNextRequiredDifficulty(algo); E.Chk(e) {
 855  		return nil, e
 856  	}
 857  	T.F("reqDifficulty %d %08x %064x", vers, reqDifficulty, bits.CompactToBig(reqDifficulty))
 858  	// Create a new block ready to be solved.
 859  	merkles := blockchain.BuildMerkleTreeStore(blockTxns, false)
 860  	var msgBlock wire.Block
 861  	msgBlock.Header = wire.BlockHeader{
 862  		Version:    vers,
 863  		PrevBlock:  best.Hash,
 864  		MerkleRoot: *merkles.GetRoot(),
 865  		Timestamp:  ts,
 866  		Bits:       reqDifficulty,
 867  	}
 868  	for _, tx := range blockTxns {
 869  		if e = msgBlock.AddTransaction(tx.MsgTx()); E.Chk(e) {
 870  			return nil, e
 871  		}
 872  	}
 873  	// Finally, perform a full check on the created block against the chain
 874  	// consensus rules to ensure it properly connects to the current best chain with
 875  	// no issues.
 876  	block := block2.NewBlock(&msgBlock)
 877  	block.SetHeight(nextBlockHeight)
 878  	e = g.Chain.CheckConnectBlockTemplate(block)
 879  	if e != nil {
 880  		D.Ln("checkconnectblocktemplate err:", e)
 881  		return nil, e
 882  	}
 883  	bh := msgBlock.Header.BlockHash()
 884  	D.C(
 885  		func() string {
 886  			return fmt.Sprintf(
 887  				"created new block template (height %d algo %s, %d transactions, "+
 888  					"%d in fees, %d signature operations cost, %d weight, "+
 889  					"target difficulty %064x prevblockhash %064x %064x subsidy %d)",
 890  				nextBlockHeight,
 891  				algo,
 892  				len(msgBlock.Transactions),
 893  				totalFees,
 894  				blockSigOpCost,
 895  				blockWeight,
 896  				bits.CompactToBig(msgBlock.Header.Bits),
 897  				msgBlock.Header.PrevBlock.CloneBytes(),
 898  				bh.CloneBytes(),
 899  				msgBlock.Transactions[0].TxOut[0].Value,
 900  			)
 901  		},
 902  	)
 903  	// D.S(msgBlock.Header)
 904  	// Tracec(func() string { return spew.Sdump(msgBlock) })
 905  	return &BlockTemplate{
 906  		Block:           &msgBlock,
 907  		Fees:            txFees,
 908  		SigOpCosts:      txSigOpCosts,
 909  		Height:          nextBlockHeight,
 910  		ValidPayAddress: payToAddress != nil,
 911  	}, nil
 912  }
 913  
 914  // UpdateBlockTime updates the timestamp in the header of the passed block to
 915  // the current time while taking into account the median time of the last
 916  // several blocks to ensure the new time is after that time per the chain
 917  // consensus rules.
 918  //
 919  // Finally, it will update the target difficulty if needed based on the new time
 920  // for the test networks since their target difficulty can change based upon
 921  // time.
 922  func (g *BlkTmplGenerator) UpdateBlockTime(
 923  	workerNumber uint32, msgBlock *wire.
 924  Block,
 925  ) (e error) {
 926  	// The new timestamp is potentially adjusted to ensure it comes after the median
 927  	// time of the last several blocks per the chain consensus rules.
 928  	newTime := medianAdjustedTime(g.Chain.BestSnapshot(), g.TimeSource)
 929  	msgBlock.Header.Timestamp = newTime
 930  	// Recalculate the difficulty if running on a network that requires it.
 931  	if g.ChainParams.ReduceMinDifficulty {
 932  		var difficulty uint32
 933  		var e error
 934  		if difficulty, e = g.Chain.CalcNextRequiredDifficulty(
 935  			fork.GetAlgoName(
 936  				msgBlock.Header.Version,
 937  				g.BestSnapshot().Height,
 938  			),
 939  		); E.Chk(e) {
 940  			return e
 941  		}
 942  		msgBlock.Header.Bits = difficulty
 943  	}
 944  	return nil
 945  }
 946  
 947  // UpdateExtraNonce updates the extra nonce in the coinbase script of the passed
 948  // block by regenerating the coinbase script with the passed value and block
 949  // height.
 950  //
 951  // It also recalculates and updates the new merkle root that results from
 952  // changing the coinbase script.
 953  func (g *BlkTmplGenerator) UpdateExtraNonce(
 954  	msgBlock *wire.Block,
 955  	blockHeight int32, extraNonce uint64,
 956  ) (e error) {
 957  	var coinbaseScript []byte
 958  	if coinbaseScript, e = standardCoinbaseScript(blockHeight, extraNonce); E.Chk(e) {
 959  		return e
 960  	}
 961  	if len(coinbaseScript) > blockchain.MaxCoinbaseScriptLen {
 962  		return fmt.Errorf(
 963  			"coinbase transaction script length of %d is out of range (min: %d, max: %d)",
 964  			len(coinbaseScript), blockchain.MinCoinbaseScriptLen,
 965  			blockchain.MaxCoinbaseScriptLen,
 966  		)
 967  	}
 968  	msgBlock.Transactions[0].TxIn[0].SignatureScript = coinbaseScript
 969  	// TODO(davec): A util.Block should use saved in the state to avoid
 970  	// 	recalculating all of the other transaction hashes.
 971  	// 	block.Transactions[0].InvalidateCache()
 972  	// Recalculate the merkle root with the updated extra nonce.
 973  	block := block2.NewBlock(msgBlock)
 974  	merkles := blockchain.BuildMerkleTreeStore(block.Transactions(), false)
 975  	msgBlock.Header.MerkleRoot = *merkles.GetRoot()
 976  	return nil
 977  }
 978  
 979  // BestSnapshot returns information about the current best chain block and
 980  // related state as of the current point in time using the chain instance
 981  // associated with the block template generator. The returned state must be
 982  // treated as immutable since it is shared by all callers. This function is safe
 983  // for concurrent access.
 984  func (g *BlkTmplGenerator) BestSnapshot() *blockchain.BestState {
 985  	return g.Chain.BestSnapshot()
 986  }
 987  
 988  // GetTxSource returns the associated transaction source.
 989  //
 990  // This function is safe for concurrent access.
 991  func (g *BlkTmplGenerator) GetTxSource() TxSource {
 992  	return g.TxSource
 993  }
 994