本文整理汇总了Golang中github.com/conformal/btcwire.MsgBlock.AddTransaction方法的典型用法代码示例。如果您正苦于以下问题:Golang MsgBlock.AddTransaction方法的具体用法?Golang MsgBlock.AddTransaction怎么用?Golang MsgBlock.AddTransaction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/conformal/btcwire.MsgBlock
的用法示例。
在下文中一共展示了MsgBlock.AddTransaction方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: NewBlockTemplate
//.........这里部分代码省略.........
minrLog.Tracef("Skipping tx %s due to error in "+
"ValidateTransactionScripts: %v", tx.Sha(), err)
logSkippedDeps(tx, deps)
continue
}
// Spend the transaction inputs in the block transaction store
// and add an entry for it to ensure any transactions which
// reference this one have it available as an input and can
// ensure they aren't double spending.
spendTransaction(blockTxStore, tx, nextBlockHeight)
// Add the transaction to the block, increment counters, and
// save the fees and signature operation counts to the block
// template.
blockTxns = append(blockTxns, tx)
blockSize += txSize
blockSigOps += numSigOps
totalFees += prioItem.fee
txFees = append(txFees, prioItem.fee)
txSigOpCounts = append(txSigOpCounts, numSigOps)
minrLog.Tracef("Adding tx %s (priority %.2f, feePerKB %.2f)",
prioItem.tx.Sha(), prioItem.priority, prioItem.feePerKB)
// Add transactions which depend on this one (and also do not
// have any other unsatisified dependencies) to the priority
// queue.
if deps != nil {
for e := deps.Front(); e != nil; e = e.Next() {
// Add the transaction to the priority queue if
// there are no more dependencies after this
// one.
item := e.Value.(*txPrioItem)
delete(item.dependsOn, *tx.Sha())
if len(item.dependsOn) == 0 {
heap.Push(priorityQueue, item)
}
}
}
}
// Now that the actual transactions have been selected, update the
// block size for the real transaction count and coinbase value with
// the total fees accordingly.
blockSize -= btcwire.MaxVarIntPayload -
uint32(btcwire.VarIntSerializeSize(uint64(len(blockTxns))))
coinbaseTx.MsgTx().TxOut[0].Value += totalFees
txFees[0] = -totalFees
// Calculate the required difficulty for the block. The timestamp
// is potentially adjusted to ensure it comes after the median time of
// the last several blocks per the chain consensus rules.
ts, err := medianAdjustedTime(chainState)
if err != nil {
return nil, err
}
requiredDifficulty, err := blockManager.CalcNextRequiredDifficulty(ts)
if err != nil {
return nil, err
}
// Create a new block ready to be solved.
merkles := btcchain.BuildMerkleTreeStore(blockTxns)
var msgBlock btcwire.MsgBlock
msgBlock.Header = btcwire.BlockHeader{
Version: generatedBlockVersion,
PrevBlock: *prevHash,
MerkleRoot: *merkles[len(merkles)-1],
Timestamp: ts,
Bits: requiredDifficulty,
}
for _, tx := range blockTxns {
if err := msgBlock.AddTransaction(tx.MsgTx()); err != nil {
return nil, err
}
}
// Finally, perform a full check on the created block against the chain
// consensus rules to ensure it properly connects to the current best
// chain with no issues.
block := btcutil.NewBlock(&msgBlock)
block.SetHeight(nextBlockHeight)
if err := blockManager.CheckConnectBlock(block); err != nil {
return nil, err
}
minrLog.Debugf("Created new block template (%d transactions, %d in "+
"fees, %d signature operations, %d bytes, target difficulty "+
"%064x)", len(msgBlock.Transactions), totalFees, blockSigOps,
blockSize, btcchain.CompactToBig(msgBlock.Header.Bits))
return &BlockTemplate{
block: &msgBlock,
fees: txFees,
sigOpCounts: txSigOpCounts,
height: nextBlockHeight,
validPayAddress: payToAddress != nil,
}, nil
}