本文整理汇总了Golang中github.com/decred/dcrutil.Block类的典型用法代码示例。如果您正苦于以下问题:Golang Block类的具体用法?Golang Block怎么用?Golang Block使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Block类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: submitBlock
// submitBlock submits the passed block to network after ensuring it passes all
// of the consensus validation rules.
func (m *CPUMiner) submitBlock(block *dcrutil.Block) bool {
m.submitBlockLock.Lock()
defer m.submitBlockLock.Unlock()
_, latestHeight := m.server.blockManager.chainState.Best()
// Be sure to set this so ProcessBlock doesn't fail! - Decred
block.SetHeight(latestHeight + 1)
// Process this block using the same rules as blocks coming from other
// nodes. This will in turn relay it to the network like normal.
isOrphan, err := m.server.blockManager.ProcessBlock(block, blockchain.BFNone)
if err != nil {
// Anything other than a rule violation is an unexpected error,
// so log that error as an internal error.
if rErr, ok := err.(blockchain.RuleError); !ok {
minrLog.Errorf("Unexpected error while processing "+
"block submitted via CPU miner: %v", err)
return false
} else {
// Occasionally errors are given out for timing errors with
// ResetMinDifficulty and high block works that is above
// the target. Feed these to debug.
if m.server.chainParams.ResetMinDifficulty &&
rErr.ErrorCode == blockchain.ErrHighHash {
minrLog.Debugf("Block submitted via CPU miner rejected "+
"because of ResetMinDifficulty time sync failure: %v",
err)
return false
} else {
// Other rule errors should be reported.
minrLog.Errorf("Block submitted via CPU miner rejected: %v", err)
return false
}
}
}
if isOrphan {
minrLog.Errorf("Block submitted via CPU miner is an orphan building "+
"on parent %v", block.MsgBlock().Header.PrevBlock)
return false
}
// The block was accepted.
coinbaseTxOuts := block.MsgBlock().Transactions[0].TxOut
coinbaseTxGenerated := int64(0)
for _, out := range coinbaseTxOuts {
coinbaseTxGenerated += out.Value
}
minrLog.Infof("Block submitted via CPU miner accepted (hash %s, "+
"height %v, amount %v)",
block.Sha(),
block.Height(),
dcrutil.Amount(coinbaseTxGenerated))
return true
}
示例2: ticketsRevokedInBlock
// ticketsRevokedInBlock fetches a list of tickets that were revoked in the
// block.
func ticketsRevokedInBlock(bl *dcrutil.Block) []chainhash.Hash {
var tickets []chainhash.Hash
for _, stx := range bl.MsgBlock().STransactions {
if stake.DetermineTxType(stx) == stake.TxTypeSSRtx {
tickets = append(tickets, stx.TxIn[0].PreviousOutPoint.Hash)
}
}
return tickets
}
示例3: ticketsSpentInBlock
// ticketsSpentInBlock finds all the tickets spent in the block.
func ticketsSpentInBlock(bl *dcrutil.Block) []chainhash.Hash {
tickets := make([]chainhash.Hash, 0)
for _, stx := range bl.STransactions() {
if DetermineTxType(stx.MsgTx()) == TxTypeSSGen {
tickets = append(tickets, stx.MsgTx().TxIn[1].PreviousOutPoint.Hash)
}
}
return tickets
}
示例4: voteVersionsInBlock
// voteVersionsInBlock returns all versions in a block.
func voteVersionsInBlock(bl *dcrutil.Block, params *chaincfg.Params) []uint32 {
versions := make([]uint32, 0, params.TicketsPerBlock)
for _, stx := range bl.MsgBlock().STransactions {
if is, _ := stake.IsSSGen(stx); !is {
continue
}
versions = append(versions, stake.SSGenVersion(stx))
}
return versions
}
示例5: ticketsInBlock
// ticketsInBlock finds all the new tickets in the block.
func ticketsInBlock(bl *dcrutil.Block) []chainhash.Hash {
tickets := make([]chainhash.Hash, 0)
for _, stx := range bl.STransactions() {
if DetermineTxType(stx.MsgTx()) == TxTypeSStx {
h := stx.Sha()
tickets = append(tickets, *h)
}
}
return tickets
}
示例6: unspendStakeTxTree
// unspendStakeTxTree returns all outpoints spent before this one
// in the block's tx tree stake. used for unspending the stake tx
// tree to evaluate tx tree regular of prev block.
func unspendStakeTxTree(block *dcrutil.Block) map[wire.OutPoint]struct{} {
unspentOps := make(map[wire.OutPoint]struct{})
for _, tx := range block.STransactions() {
for _, txIn := range tx.MsgTx().TxIn {
unspentOps[txIn.PreviousOutPoint] = struct{}{}
}
}
return unspentOps
}
示例7: votesInBlock
// votesInBlock finds all the votes in the block.
func votesInBlock(bl *dcrutil.Block) []chainhash.Hash {
votes := make([]chainhash.Hash, 0)
for _, stx := range bl.STransactions() {
if DetermineTxType(stx.MsgTx()) == TxTypeSSGen {
h := stx.Sha()
votes = append(votes, *h)
}
}
return votes
}
示例8: dbIndexDisconnectBlock
// dbIndexDisconnectBlock removes all of the index entries associated with the
// given block using the provided indexer and updates the tip of the indexer
// accordingly. An error will be returned if the current tip for the indexer is
// not the passed block.
func dbIndexDisconnectBlock(dbTx database.Tx, indexer Indexer, block, parent *dcrutil.Block, view *blockchain.UtxoViewpoint) error {
// Assert that the block being disconnected is the current tip of the
// index.
idxKey := indexer.Key()
curTipHash, _, err := dbFetchIndexerTip(dbTx, idxKey)
if err != nil {
return err
}
if !curTipHash.IsEqual(block.Sha()) {
return AssertError(fmt.Sprintf("dbIndexDisconnectBlock must "+
"be called with the block at the current index tip "+
"(%s, tip %s, block %s)", indexer.Name(),
curTipHash, block.Sha()))
}
// Notify the indexer with the disconnected block so it can remove all
// of the appropriate entries.
if err := indexer.DisconnectBlock(dbTx, block, parent, view); err != nil {
return err
}
// Update the current index tip.
prevHash := &block.MsgBlock().Header.PrevBlock
return dbPutIndexerTip(dbTx, idxKey, prevHash, uint32(block.Height())-1)
}
示例9: checkBlockContext
// checkBlockContext peforms several validation checks on the block which depend
// on its position within the block chain.
//
// The flags modify the behavior of this function as follows:
// - BFFastAdd: The transaction are not checked to see if they are finalized
// and the somewhat expensive duplication transaction check is not performed.
//
// The flags are also passed to checkBlockHeaderContext. See its documentation
// for how the flags modify its behavior.
func (b *BlockChain) checkBlockContext(block *dcrutil.Block, prevNode *blockNode,
flags BehaviorFlags) error {
// The genesis block is valid by definition.
if prevNode == nil {
return nil
}
// Perform all block header related validation checks.
header := &block.MsgBlock().Header
err := b.checkBlockHeaderContext(header, prevNode, flags)
if err != nil {
return err
}
fastAdd := flags&BFFastAdd == BFFastAdd
if !fastAdd {
// The height of this block is one more than the referenced
// previous block.
blockHeight := prevNode.height + 1
// Ensure all transactions in the block are finalized.
for _, tx := range block.Transactions() {
if !IsFinalizedTransaction(tx, blockHeight,
header.Timestamp) {
str := fmt.Sprintf("block contains unfinalized regular "+
"transaction %v", tx.Sha())
return ruleError(ErrUnfinalizedTx, str)
}
}
for _, stx := range block.STransactions() {
if !IsFinalizedTransaction(stx, blockHeight,
header.Timestamp) {
str := fmt.Sprintf("block contains unfinalized stake "+
"transaction %v", stx.Sha())
return ruleError(ErrUnfinalizedTx, str)
}
}
// Check that the node is at the correct height in the blockchain,
// as specified in the block header.
if blockHeight != int64(block.MsgBlock().Header.Height) {
errStr := fmt.Sprintf("Block header height invalid; expected %v"+
" but %v was found", blockHeight, header.Height)
return ruleError(ErrBadBlockHeight, errStr)
}
// Check that the coinbase contains at minimum the block
// height in output 1.
if blockHeight > 1 {
err := checkCoinbaseUniqueHeight(blockHeight, block)
if err != nil {
return err
}
}
}
return nil
}
示例10: SubmitBlockAsync
// SubmitBlockAsync returns an instance of a type that can be used to get the
// result of the RPC at some future time by invoking the Receive function on the
// returned instance.
//
// See SubmitBlock for the blocking version and more details.
func (c *Client) SubmitBlockAsync(block *dcrutil.Block, options *dcrjson.SubmitBlockOptions) FutureSubmitBlockResult {
blockHex := ""
if block != nil {
blockBytes, err := block.Bytes()
if err != nil {
return newFutureError(err)
}
blockHex = hex.EncodeToString(blockBytes)
}
cmd := dcrjson.NewSubmitBlockCmd(blockHex, options)
return c.sendCmd(cmd)
}
示例11: InsertBlock
// InsertBlock synchronously queues a newly solved block to have its
// transactions indexed by address.
func (a *addrIndexer) InsertBlock(block *dcrutil.Block, parent *dcrutil.Block) error {
addrIndex, err := a.indexBlockAddrs(block, parent)
if err != nil {
return fmt.Errorf("Unable to index transactions of"+
" block: %v", err)
}
err = a.server.db.UpdateAddrIndexForBlock(block.Sha(),
block.Height(),
addrIndex)
if err != nil {
return fmt.Errorf("Unable to insert block: %v", err.Error())
}
return nil
}
示例12: DisconnectBlock
// DisconnectBlock is invoked by the index manager when a block has been
// disconnected from the main chain. This indexer removes the
// hash-to-transaction mapping for every transaction in the block.
//
// This is part of the Indexer interface.
func (idx *TxIndex) DisconnectBlock(dbTx database.Tx, block, parent *dcrutil.Block, view *blockchain.UtxoViewpoint) error {
// Remove all of the transactions in the block from the index.
if err := dbRemoveTxIndexEntries(dbTx, block, parent); err != nil {
return err
}
// Remove the block ID index entry for the block being disconnected and
// decrement the current internal block ID to account for it.
blockSha := block.Sha()
if err := dbRemoveBlockIDIndexEntry(dbTx, *blockSha); err != nil {
return err
}
idx.curBlockID--
return nil
}
示例13: unspendInflightTxTree
// unspendInflightTxTree returns all outpoints spent that reference internal
// transactions in a TxTreeRegular.
func unspendInflightTxTree(block *dcrutil.Block) map[wire.OutPoint]struct{} {
unspentOps := make(map[wire.OutPoint]struct{})
allTxHashes := make(map[chainhash.Hash]struct{})
for _, tx := range block.Transactions() {
h := tx.Sha()
allTxHashes[*h] = struct{}{}
}
for _, tx := range block.Transactions() {
for _, txIn := range tx.MsgTx().TxIn {
if _, isLocal := allTxHashes[txIn.PreviousOutPoint.Hash]; isLocal {
unspentOps[txIn.PreviousOutPoint] = struct{}{}
}
}
}
return unspentOps
}
示例14: ConnectBlock
// ConnectBlock is invoked by the index manager when a new block has been
// connected to the main chain. This indexer adds a hash-to-transaction mapping
// for every transaction in the passed block.
//
// This is part of the Indexer interface.
func (idx *TxIndex) ConnectBlock(dbTx database.Tx, block, parent *dcrutil.Block, view *blockchain.UtxoViewpoint) error {
// Increment the internal block ID to use for the block being connected
// and add all of the transactions in the block to the index.
newBlockID := idx.curBlockID + 1
if err := dbAddTxIndexEntries(dbTx, block, parent, newBlockID); err != nil {
return err
}
// Add the new block ID index entry for the block being connected and
// update the current internal block ID accordingly.
blockSha := block.Sha()
err := dbPutBlockIDIndexEntry(dbTx, *blockSha, newBlockID)
if err != nil {
return err
}
idx.curBlockID = newBlockID
return nil
}
示例15: checkBlockScripts
// checkBlockScripts executes and validates the scripts for all transactions in
// the passed block using multiple goroutines.
// txTree = true is TxTreeRegular, txTree = false is TxTreeStake.
func checkBlockScripts(block *dcrutil.Block, utxoView *UtxoViewpoint, txTree bool,
scriptFlags txscript.ScriptFlags, sigCache *txscript.SigCache) error {
// Collect all of the transaction inputs and required information for
// validation for all transactions in the block into a single slice.
numInputs := 0
var txs []*dcrutil.Tx
// TxTreeRegular handling.
if txTree {
txs = block.Transactions()
} else { // TxTreeStake
txs = block.STransactions()
}
for _, tx := range txs {
numInputs += len(tx.MsgTx().TxIn)
}
txValItems := make([]*txValidateItem, 0, numInputs)
for _, tx := range txs {
for txInIdx, txIn := range tx.MsgTx().TxIn {
// Skip coinbases.
if txIn.PreviousOutPoint.Index == math.MaxUint32 {
continue
}
txVI := &txValidateItem{
txInIndex: txInIdx,
txIn: txIn,
tx: tx,
}
txValItems = append(txValItems, txVI)
}
}
// Validate all of the inputs.
validator := newTxValidator(utxoView, scriptFlags, sigCache)
if err := validator.Validate(txValItems); err != nil {
return err
}
return nil
}