// BuildBlockHeaderVerbose creates a *dcrjson.GetBlockHeaderVerboseResult from
// an input *wire.BlockHeader and current best block height, which is used to
// compute confirmations.  The next block hash may optionally be provided.
7899
func BuildBlockHeaderVerbose(header *wire.BlockHeader, params *chaincfg.Params,
	currentHeight int64, nextHash ...string) *dcrjson.GetBlockHeaderVerboseResult {
	if header == nil {
		return nil
	}

	diffRatio := txhelpers.GetDifficultyRatio(header.Bits, params)

	var next string
	if len(nextHash) > 0 {
		next = nextHash[0]
	}

	blockHeaderResult := dcrjson.GetBlockHeaderVerboseResult{
		Hash:          header.BlockHash().String(),
		Confirmations: currentHeight - int64(header.Height),
		Version:       header.Version,
		PreviousHash:  header.PrevBlock.String(),
		MerkleRoot:    header.MerkleRoot.String(),
		StakeRoot:     header.StakeRoot.String(),
		VoteBits:      header.VoteBits,
		FinalState:    hex.EncodeToString(header.FinalState[:]),
		Voters:        header.Voters,
		FreshStake:    header.FreshStake,
		Revocations:   header.Revocations,
		PoolSize:      header.PoolSize,
		Bits:          strconv.FormatInt(int64(header.Bits), 16),
		SBits:         dcrutil.Amount(header.SBits).ToCoin(),
		Height:        header.Height,
		Size:          header.Size,
		Time:          header.Timestamp.Unix(),
		Nonce:         header.Nonce,
		Difficulty:    diffRatio,
		// Cannot get ChainWork from the wire.BlockHeader
		NextHash: next,
	}

	return &blockHeaderResult
}
20364
func NewBlockServerMemory(log logger.Logger) *BlockServerMemory {
	return &BlockServerMemory{
		log, sync.RWMutex{}, make(map[kbfsblock.ID]blockMemEntry),
	}
}
1063791
func (g *BlockGate) BestBlockHash() (chainhash.Hash, int64, error) {
	g.mtx.RLock()
	defer g.mtx.RUnlock()
	var err error
	hash, ok := g.hashAtHeight[g.height]
	if !ok {
		err = fmt.Errorf("hash of best block %d not found", g.height)
	}
	return hash, g.height, err
}
327246
func (r FutureGetBlockHeaderVerboseResult) Receive() (*btcjson.GetBlockHeaderVerboseResult, error) {
	res, err := receiveFuture(r)
	if err != nil {
		return nil, err
	}

	// Unmarshal result as a string.
	var bh btcjson.GetBlockHeaderVerboseResult
	err = json.Unmarshal(res, &bh)
	if err != nil {
		return nil, err
	}

	return &bh, nil
}
349402
func BuildB3SingleHeader(traceID, spanID, sampling, flags, parentSpanID string) string {
	if traceID == "" || spanID == "" {
		return ""
	}

	if sampling == "" && flags == "" {
		return traceID + "-" + spanID
	}

	samplingBit := "0"
	if flags == "1" {
		samplingBit = "d"
	} else if s, err := strconv.ParseBool(sampling); err == nil {
		if s {
			samplingBit = "1"
		}
	} else {
		return traceID + "-" + spanID
	}

	if parentSpanID == "" {
		return traceID + "-" + spanID + "-" + samplingBit
	}

	return traceID + "-" + spanID + "-" + samplingBit + "-" + parentSpanID
}
3541
func GetBlockHeaderVerboseByString(client BlockFetcher, hash string) *dcrjson.GetBlockHeaderVerboseResult {
	blockhash, err := chainhash.NewHashFromStr(hash)
	if err != nil {
		log.Errorf("Invalid block hash %s: %v", blockhash, err)
		return nil
	}

	blockHeaderVerbose, err := client.GetBlockHeaderVerbose(blockhash)
	if err != nil {
		log.Errorf("GetBlockHeaderVerbose(%v) failed: %v", blockhash, err)
		return nil
	}

	return blockHeaderVerbose
}
55575
func (g *BlockGate) Block(hash chainhash.Hash) (*dcrutil.Block, error) {
	// Try block cache first.
	block, err := g.CachedBlock(hash)
	if err == nil {
		return block, nil
	}

	// Cache miss. Retrieve from dcrd RPC.
	block, err = GetBlockByHash(&hash, g.client)
	if err != nil {
		return nil, fmt.Errorf("GetBlock (%v) failed: %v", hash, err)
	}

	g.mtx.RLock()
	fmt.Printf("Block cache miss: requested %d, cache capacity %d, tip %d.",
		block.Height(), g.expireQueue.cap, g.height)
	g.mtx.RUnlock()
	return block, nil
}
272293
func putBlockToServer(
	ctx context.Context, bserv BlockServer, tlfID tlf.ID,
	blockPtr data.BlockPointer, readyBlockData data.ReadyBlockData,
	cacheType DiskBlockCacheType) error {
	var err error
	if blockPtr.RefNonce == kbfsblock.ZeroRefNonce {
		err = bserv.Put(ctx, tlfID, blockPtr.ID, blockPtr.Context,
			readyBlockData.Buf, readyBlockData.ServerHalf, cacheType)
	} else {
		// non-zero block refnonce means this is a new reference to an
		// existing block.
		err = bserv.AddBlockReference(ctx, tlfID, blockPtr.ID,
			blockPtr.Context)
	}
	return err
}
710507
func (c *Client) GetBlockHeaderAsync(blockHash *chainhash.Hash) FutureGetBlockHeaderResult {
	hash := ""
	if blockHash != nil {
		hash = blockHash.String()
	}

	cmd := btcjson.NewGetBlockHeaderCmd(hash, btcjson.Bool(false))
	return c.sendCmd(cmd)
}
811379
func newBlockNode(blockHeader *wire.BlockHeader, parent *blockNode) *blockNode {
	var node blockNode
	initBlockNode(&node, blockHeader, parent)
	return &node
}
1157675
func (c *ConfigLocal) BlockCryptVersion() kbfscrypto.EncryptionVer {
	c.lock.RLock()
	defer c.lock.RUnlock()
	return c.blockCryptVersion
}
747989
func (bs *BlockStore) LoadBlock(height int64) *types.Block {
	var blockMeta = bs.LoadBlockMeta(height)
	if blockMeta == nil {
		return nil
	}

	var block = new(types.Block)
	buf := []byte{}
	for i := 0; i < blockMeta.BlockID.PartsHeader.Total; i++ {
		part := bs.LoadBlockPart(height, i)
		buf = append(buf, part.Bytes...)
	}
	err := cdc.UnmarshalBinaryLengthPrefixed(buf, block)
	if err != nil {
		// NOTE: The existence of meta should imply the existence of the
		// block. So, make sure meta is only saved after blocks are saved.
		panic(cmn.ErrorWrap(err, "Error reading block"))
	}
	return block
}
351205
func (c *BitcoindClient) GetBlockHeaderVerbose(
	hash *chainhash.Hash) (*btcjson.GetBlockHeaderVerboseResult, error) {

	return c.chainConn.client.GetBlockHeaderVerbose(hash)
}
604788
func (db *PublicRecord) InsertBlockHead(blk *btcutil.Block) (error, bool) {
	tx, err := db.conn.Begin()
	if err != nil {
		return err, false
	}

	err = db.insertBlockHead(tx, blk)
	if err != nil {
		return tx.Rollback(), false
	}

	if err = tx.Commit(); err != nil {
		return err, true
	}

	return nil, true
}
713662
func NewBlockGate(client BlockFetcher, capacity int) *BlockGate {
	return &BlockGate{
		client:        client,
		height:        -1,
		fetchToHeight: -1,
		hashAtHeight:  make(map[int64]chainhash.Hash),
		blockWithHash: make(map[chainhash.Hash]*dcrutil.Block),
		heightWaiters: make(map[int64][]chan chainhash.Hash),
		hashWaiters:   make(map[chainhash.Hash][]chan int64),
		expireQueue: heightHashQueue{
			cap: capacity,
		},
	}
}
372565
func (hc *HeaderChain) GetBlockNumber(hash common.Hash) *uint64 {
	if cached, ok := hc.numberCache.Get(hash); ok {
		number := cached.(uint64)
		return &number
	}
	number := rawdb.ReadHeaderNumber(hc.chainDb, hash)
	if number != nil {
		hc.numberCache.Add(hash, *number)
	}
	return number
}
714737
func (sp *serverPeer) newestBlock() (*chainhash.Hash, int32, error) {
	best := sp.server.chain.BestSnapshot()
	return &best.Hash, best.Height, nil
}