本文整理匯總了Golang中github.com/btcsuite/btcd/wire.MsgBlock.AddTransaction方法的典型用法代碼示例。如果您正苦於以下問題:Golang MsgBlock.AddTransaction方法的具體用法?Golang MsgBlock.AddTransaction怎麽用?Golang MsgBlock.AddTransaction使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/btcsuite/btcd/wire.MsgBlock
的用法示例。
在下文中一共展示了MsgBlock.AddTransaction方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: createBlock
// createBlock creates a new block building from the previous block.
func createBlock(prevBlock *btcutil.Block, inclusionTxs []*btcutil.Tx,
blockVersion int32, blockTime time.Time,
miningAddr btcutil.Address, net *chaincfg.Params) (*btcutil.Block, error) {
prevHash := prevBlock.Hash()
blockHeight := prevBlock.Height() + 1
// If a target block time was specified, then use that as the header's
// timestamp. Otherwise, add one second to the previous block unless
// it's the genesis block in which case use the current time.
var ts time.Time
switch {
case !blockTime.IsZero():
ts = blockTime
default:
ts = prevBlock.MsgBlock().Header.Timestamp.Add(time.Second)
}
extraNonce := uint64(0)
coinbaseScript, err := standardCoinbaseScript(blockHeight, extraNonce)
if err != nil {
return nil, err
}
coinbaseTx, err := createCoinbaseTx(coinbaseScript, blockHeight,
miningAddr, net)
if err != nil {
return nil, err
}
// Create a new block ready to be solved.
blockTxns := []*btcutil.Tx{coinbaseTx}
if inclusionTxs != nil {
blockTxns = append(blockTxns, inclusionTxs...)
}
merkles := blockchain.BuildMerkleTreeStore(blockTxns)
var block wire.MsgBlock
block.Header = wire.BlockHeader{
Version: blockVersion,
PrevBlock: *prevHash,
MerkleRoot: *merkles[len(merkles)-1],
Timestamp: ts,
Bits: net.PowLimitBits,
}
for _, tx := range blockTxns {
if err := block.AddTransaction(tx.MsgTx()); err != nil {
return nil, err
}
}
found := solveBlock(&block.Header, net.PowLimit)
if !found {
return nil, errors.New("Unable to solve block")
}
utilBlock := btcutil.NewBlock(&block)
utilBlock.SetHeight(blockHeight)
return utilBlock, nil
}
示例2: 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 -= wire.MaxVarIntPayload -
uint32(wire.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, timeSource)
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 := blockchain.BuildMerkleTreeStore(blockTxns)
var msgBlock wire.MsgBlock
msgBlock.Header = wire.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, blockchain.CompactToBig(msgBlock.Header.Bits))
return &BlockTemplate{
block: &msgBlock,
fees: txFees,
sigOpCounts: txSigOpCounts,
height: nextBlockHeight,
validPayAddress: payToAddress != nil,
}, nil
}