// LastBlock returns the last block hash, height and time.
337626
func (exp *explorerUI) LastBlock() (lastBlockHash string, lastBlock int64, lastBlockTime int64) {
	exp.pageData.RLock()
	defer exp.pageData.RUnlock()

	if exp.pageData.BlockInfo.BlockBasic == nil {
		// If exp.pageData.BlockInfo.BlockBasic has not yet been set return:
		lastBlock, lastBlockTime = -1, -1
		return
	}

	lastBlock = exp.pageData.BlockInfo.Height
	lastBlockTime = exp.pageData.BlockInfo.BlockTime.UNIX()
	lastBlockHash = exp.pageData.BlockInfo.Hash
	return
}
1174911
func (p *Peer) UpdateLastAnnouncedBlock(blkHash *chainhash.Hash) {
	log.Tracef("Updating last blk for peer %v, %v", p.addr, blkHash)

	p.statsMtx.Lock()
	p.lastAnnouncedBlock = blkHash
	p.statsMtx.Unlock()
}
1236418
func (db *DB) RetrieveHighestBlockHeight() (int64, error) {
	var blockHeight int64
	err := db.QueryRow(db.getHighestBlockHeightSQL).Scan(&blockHeight)
	return blockHeight, err
}
10135
func DeleteBlock(db ethdb.Writer, hash common.Hash, number uint64) {
	DeleteReceipts(db, hash, number)
	DeleteHeader(db, hash, number)
	DeleteBody(db, hash, number)
	DeleteTd(db, hash, number)
}
930086
func (p *MempoolMonitor) LastBlockHash() chainhash.Hash {
	p.mtx.RLock()
	defer p.mtx.RUnlock()
	return p.lastBlock.Hash
}
379806
func (pq *BlockPriorityQueue) UpdateBlock(b *CachedBlock, summary *BlockDataBasic, stakeInfo *StakeInfoExtended) {
	if b != nil {
		heightAdded, hash, mismatch := checkBlockHeightHash(&cachedBlockData{
			blockSummary: summary,
			stakeInfo:    stakeInfo,
		})
		if mismatch {
			fmt.Println("Cannot UpdateBlock!")
			return
		}
		heightRemoved := b.height

		b.height = heightAdded
		b.hash = hash
		b.summary = summary
		b.stakeInfo = stakeInfo
		b.accesses = 0
		b.Access()
		heap.Fix(pq, b.heapIdx)
		pq.RescanMinMaxForUpdate(heightAdded, heightRemoved)
		pq.lastAccess = time.Unix(0, b.accessTime)
	}
}
917583
func scanForLastCompleteBlock(rootDir string, fileNum int, startingOffset int64) ([]byte, int64, int, error) {
	//scan the passed file number suffix starting from the passed offset to find the last completed block
	numBlocks := 0
	var lastBlockBytes []byte
	blockStream, errOpen := newBlockfileStream(rootDir, fileNum, startingOffset)
	if errOpen != nil {
		return nil, 0, 0, errOpen
	}
	defer blockStream.close()
	var errRead error
	var blockBytes []byte
	for {
		blockBytes, errRead = blockStream.nextBlockBytes()
		if blockBytes == nil || errRead != nil {
			break
		}
		lastBlockBytes = blockBytes
		numBlocks++
	}
	if errRead == ErrUnexpectedEndOfBlockfile {
		logger.Debugf(`Error:%s
		The error may happen if a crash has happened during block appending.
		Resetting error to nil and returning current offset as a last complete block's end offset`, errRead)
		errRead = nil
	}
	logger.Debugf("scanForLastCompleteBlock(): last complete block ends at offset=[%d]", blockStream.currentOffset)
	return lastBlockBytes, blockStream.currentOffset, numBlocks, errRead
}
1064969
func DeleteBlocks(ctx context.Context, N int64, db *sql.DB) (res []dbtypes.DeletionSummary, height int64, hash string, err error) {
	// If N is less than 1, get the current best block height and hash, then
	// return.
	if N < 1 {
		height, hash, err = RetrieveBestBlock(ctx, db)
		return
	}

	for i := int64(0); i < N; i++ {
		var resi dbtypes.DeletionSummary
		resi, height, hash, err = DeleteBestBlock(ctx, db)
		if err != nil {
			return
		}
		res = append(res, resi)
		if hash == "" {
			break
		}
		if (i%100 == 0 && i > 0) || i == N-1 {
			log.Debugf("Removed data for %d blocks.", i+1)
		}
	}

	return
}
281531
func (ed *Dispatcher) updateLastBlockNum(blockNum uint64) error {
	// The Deliver Service shouldn't be sending blocks out of order.
	// Log an error if we detect this happening.
	lastBlockNum := atomic.LoadUint64(&ed.lastBlockNum)
	if lastBlockNum == math.MaxUint64 || blockNum > lastBlockNum {
		atomic.StoreUint64(&ed.lastBlockNum, blockNum)
		logger.Debugf("Updated last block received to %d", blockNum)
		return nil
	}
	return errors.Errorf("Expecting a block number greater than %d but received block number %d", lastBlockNum, blockNum)
}
1123176
func (apic *APICache) GetCachedBlockByHeight(height int64) *CachedBlock {
	apic.mtx.Lock()
	defer apic.mtx.Unlock()
	hash, ok := apic.MainchainBlocks[height]
	if !ok {
		return nil
	}
	return apic.getCachedBlockByHash(hash)
}
145090
func (db *DB) DeleteBlock(blockhash string) (NSummaryRows, NStakeInfoRows int64, err error) {
	// Attempt to purge the block data.
	NSummaryRows, NStakeInfoRows, err = db.deleteBlock(blockhash)
	if err != nil {
		return
	}

	err = db.updateHeights()
	return
}
5526
func (cs *ChainSupport) Block(number uint64) *cb.Block {
	if cs.Height() <= number {
		return nil
	}
	return blockledger.GetBlock(cs.Reader(), number)
}
1335061
func MakeBlock(height int64, txs []Tx, lastCommit *Commit, evidence []Evidence) *Block {
	block := &Block{
		Header: Header{
			Height: height,
			NumTxs: int64(len(txs)),
		},
		Data: Data{
			Txs: txs,
		},
		Evidence:   EvidenceData{Evidence: evidence},
		LastCommit: lastCommit,
	}
	block.fillHeader()
	return block
}
1127448
func (adapter *SQLiteAdapter) LastBlockIDQuery() string {
	query := `
		WITH ll AS (
			SELECT MAX(%s) AS %s FROM %s
		)
		SELECT COALESCE(%s, '0') AS %s
			FROM ll LEFT OUTER JOIN %s log ON (ll.%s = log.%s);`

	return Cleanf(query,
		types.SQLColumnLabelId,                         // max
		types.SQLColumnLabelId,                         // as
		types.SQLLogTableName,                          // from
		types.SQLColumnLabelHeight,                     // coalesce
		types.SQLColumnLabelHeight,                     // as
		types.SQLLogTableName,                          // from
		types.SQLColumnLabelId, types.SQLColumnLabelId) // on
}
779420
func NewBlock(msgBlock *wire.MsgBlock) *Block {
	return &Block{
		msgBlock:    msgBlock,
		blockHeight: BlockHeightUnknown,
	}
}
1235859
func NewBlockData(height uint64) *BlockData {
	data := types.EventData{
		Tables:      make(map[string]types.EventDataTable),
		BlockHeight: height,
	}

	return &BlockData{
		Data: data,
	}
}
366334
func UpdateLastBlockValid(db *sql.DB, blockDbID uint64, isValid bool) error {
	numRows, err := sqlExec(db, internal.UpdateLastBlockValid,
		"failed to update last block validity: ", blockDbID, isValid)
	if err != nil {
		return err
	}
	if numRows != 1 {
		return fmt.Errorf("UpdateLastBlockValid failed to update exactly 1 row"+
			"(%d)", numRows)
	}
	return nil
}