2014-01-09 06:54:52 +01:00
|
|
|
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
2013-08-03 17:20:05 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package ldb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
2014-07-03 02:47:24 +02:00
|
|
|
|
2015-01-27 18:45:10 +01:00
|
|
|
"github.com/btcsuite/btcd/database"
|
2015-02-05 22:16:39 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2015-01-15 17:33:06 +01:00
|
|
|
"github.com/btcsuite/btcutil"
|
2015-01-15 09:47:40 +01:00
|
|
|
"github.com/btcsuite/goleveldb/leveldb"
|
2013-08-03 17:20:05 +02:00
|
|
|
)
|
|
|
|
|
2013-09-30 23:44:26 +02:00
|
|
|
// FetchBlockBySha - return a btcutil Block
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *LevelDb) FetchBlockBySha(sha *wire.ShaHash) (blk *btcutil.Block, err error) {
|
2013-09-30 23:44:26 +02:00
|
|
|
db.dbLock.Lock()
|
|
|
|
defer db.dbLock.Unlock()
|
|
|
|
return db.fetchBlockBySha(sha)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetchBlockBySha - return a btcutil Block
|
|
|
|
// Must be called with db lock held.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *LevelDb) fetchBlockBySha(sha *wire.ShaHash) (blk *btcutil.Block, err error) {
|
2013-09-30 23:44:26 +02:00
|
|
|
|
|
|
|
buf, height, err := db.fetchSha(sha)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
blk, err = btcutil.NewBlockFromBytes(buf)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
blk.SetHeight(height)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-01-19 03:59:53 +01:00
|
|
|
// FetchBlockHeightBySha returns the block height for the given hash. This is
|
2015-01-27 18:45:10 +01:00
|
|
|
// part of the database.Db interface implementation.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *LevelDb) FetchBlockHeightBySha(sha *wire.ShaHash) (int64, error) {
|
2014-01-19 03:59:53 +01:00
|
|
|
db.dbLock.Lock()
|
|
|
|
defer db.dbLock.Unlock()
|
|
|
|
|
|
|
|
return db.getBlkLoc(sha)
|
|
|
|
}
|
|
|
|
|
2015-02-06 00:54:04 +01:00
|
|
|
// FetchBlockHeaderBySha - return a ShaHash
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *LevelDb) FetchBlockHeaderBySha(sha *wire.ShaHash) (bh *wire.BlockHeader, err error) {
|
2014-01-19 03:59:53 +01:00
|
|
|
db.dbLock.Lock()
|
|
|
|
defer db.dbLock.Unlock()
|
|
|
|
|
|
|
|
// Read the raw block from the database.
|
|
|
|
buf, _, err := db.fetchSha(sha)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only deserialize the header portion and ensure the transaction count
|
|
|
|
// is zero since this is a standalone header.
|
2015-02-05 22:16:39 +01:00
|
|
|
var blockHeader wire.BlockHeader
|
2014-06-05 19:45:43 +02:00
|
|
|
err = blockHeader.Deserialize(bytes.NewReader(buf))
|
2014-01-19 03:59:53 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
bh = &blockHeader
|
|
|
|
|
|
|
|
return bh, err
|
|
|
|
}
|
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *LevelDb) getBlkLoc(sha *wire.ShaHash) (int64, error) {
|
2013-08-22 17:40:59 +02:00
|
|
|
key := shaBlkToKey(sha)
|
2013-08-03 17:20:05 +02:00
|
|
|
|
2013-08-22 17:40:59 +02:00
|
|
|
data, err := db.lDb.Get(key, db.ro)
|
2013-08-03 17:20:05 +02:00
|
|
|
if err != nil {
|
2014-07-17 16:48:03 +02:00
|
|
|
if err == leveldb.ErrNotFound {
|
2015-01-27 18:45:10 +01:00
|
|
|
err = database.ErrBlockShaMissing
|
2014-07-17 16:48:03 +02:00
|
|
|
}
|
2013-08-03 17:20:05 +02:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// deserialize
|
2014-06-04 20:55:48 +02:00
|
|
|
blkHeight := binary.LittleEndian.Uint64(data)
|
|
|
|
|
|
|
|
return int64(blkHeight), nil
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *LevelDb) getBlkByHeight(blkHeight int64) (rsha *wire.ShaHash, rbuf []byte, err error) {
|
2013-08-03 17:20:05 +02:00
|
|
|
var blkVal []byte
|
|
|
|
|
|
|
|
key := int64ToKey(blkHeight)
|
|
|
|
|
2013-08-22 17:40:59 +02:00
|
|
|
blkVal, err = db.lDb.Get(key, db.ro)
|
2013-08-03 17:20:05 +02:00
|
|
|
if err != nil {
|
2013-08-22 17:40:59 +02:00
|
|
|
log.Tracef("failed to find height %v", blkHeight)
|
2013-08-03 17:20:05 +02:00
|
|
|
return // exists ???
|
|
|
|
}
|
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
var sha wire.ShaHash
|
2013-08-03 17:20:05 +02:00
|
|
|
|
|
|
|
sha.SetBytes(blkVal[0:32])
|
|
|
|
|
|
|
|
blockdata := make([]byte, len(blkVal[32:]))
|
|
|
|
copy(blockdata[:], blkVal[32:])
|
|
|
|
|
|
|
|
return &sha, blockdata, nil
|
|
|
|
}
|
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *LevelDb) getBlk(sha *wire.ShaHash) (rblkHeight int64, rbuf []byte, err error) {
|
2013-08-03 17:20:05 +02:00
|
|
|
var blkHeight int64
|
|
|
|
|
|
|
|
blkHeight, err = db.getBlkLoc(sha)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf []byte
|
|
|
|
|
|
|
|
_, buf, err = db.getBlkByHeight(blkHeight)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return blkHeight, buf, nil
|
|
|
|
}
|
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *LevelDb) setBlk(sha *wire.ShaHash, blkHeight int64, buf []byte) {
|
2013-08-03 17:20:05 +02:00
|
|
|
// serialize
|
2014-06-04 20:55:48 +02:00
|
|
|
var lw [8]byte
|
2014-06-05 19:45:43 +02:00
|
|
|
binary.LittleEndian.PutUint64(lw[0:8], uint64(blkHeight))
|
2013-08-03 17:20:05 +02:00
|
|
|
|
2014-06-04 20:55:48 +02:00
|
|
|
shaKey := shaBlkToKey(sha)
|
2013-08-03 17:20:05 +02:00
|
|
|
blkKey := int64ToKey(blkHeight)
|
|
|
|
|
|
|
|
shaB := sha.Bytes()
|
|
|
|
blkVal := make([]byte, len(shaB)+len(buf))
|
|
|
|
copy(blkVal[0:], shaB)
|
|
|
|
copy(blkVal[len(shaB):], buf)
|
|
|
|
|
2014-06-04 20:55:48 +02:00
|
|
|
db.lBatch().Put(shaKey, lw[:])
|
2013-08-22 17:40:59 +02:00
|
|
|
db.lBatch().Put(blkKey, blkVal)
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// insertSha stores a block hash and its associated data block with a
|
2013-10-03 21:21:45 +02:00
|
|
|
// previous sha of `prevSha'.
|
2013-08-03 17:20:05 +02:00
|
|
|
// insertSha shall be called with db lock held
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *LevelDb) insertBlockData(sha *wire.ShaHash, prevSha *wire.ShaHash, buf []byte) (int64, error) {
|
2014-06-04 20:55:48 +02:00
|
|
|
oBlkHeight, err := db.getBlkLoc(prevSha)
|
2013-08-03 17:20:05 +02:00
|
|
|
if err != nil {
|
|
|
|
// check current block count
|
|
|
|
// if count != 0 {
|
2015-01-27 18:45:10 +01:00
|
|
|
// err = database.PrevShaMissing
|
2013-08-03 17:20:05 +02:00
|
|
|
// return
|
|
|
|
// }
|
|
|
|
oBlkHeight = -1
|
2013-08-22 17:40:59 +02:00
|
|
|
if db.nextBlock != 0 {
|
|
|
|
return 0, err
|
|
|
|
}
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(drahn) check curfile filesize, increment curfile if this puts it over
|
|
|
|
blkHeight := oBlkHeight + 1
|
|
|
|
|
2014-06-04 20:55:48 +02:00
|
|
|
db.setBlk(sha, blkHeight, buf)
|
2013-08-03 17:20:05 +02:00
|
|
|
|
|
|
|
// update the last block cache
|
|
|
|
db.lastBlkShaCached = true
|
|
|
|
db.lastBlkSha = *sha
|
|
|
|
db.lastBlkIdx = blkHeight
|
|
|
|
db.nextBlock = blkHeight + 1
|
|
|
|
|
|
|
|
return blkHeight, nil
|
|
|
|
}
|
|
|
|
|
2013-10-03 21:21:45 +02:00
|
|
|
// fetchSha returns the datablock for the given ShaHash.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *LevelDb) fetchSha(sha *wire.ShaHash) (rbuf []byte,
|
2013-08-03 17:20:05 +02:00
|
|
|
rblkHeight int64, err error) {
|
|
|
|
var blkHeight int64
|
|
|
|
var buf []byte
|
|
|
|
|
|
|
|
blkHeight, buf, err = db.getBlk(sha)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, blkHeight, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExistsSha looks up the given block hash
|
|
|
|
// returns true if it is present in the database.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *LevelDb) ExistsSha(sha *wire.ShaHash) (bool, error) {
|
2013-08-03 17:20:05 +02:00
|
|
|
db.dbLock.Lock()
|
|
|
|
defer db.dbLock.Unlock()
|
|
|
|
|
|
|
|
// not in cache, try database
|
2014-07-07 16:50:50 +02:00
|
|
|
return db.blkExistsSha(sha)
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// blkExistsSha looks up the given block hash
|
|
|
|
// returns true if it is present in the database.
|
|
|
|
// CALLED WITH LOCK HELD
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *LevelDb) blkExistsSha(sha *wire.ShaHash) (bool, error) {
|
2015-01-31 01:38:26 +01:00
|
|
|
key := shaBlkToKey(sha)
|
|
|
|
|
|
|
|
return db.lDb.Has(key, db.ro)
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FetchBlockShaByHeight returns a block hash based on its height in the
|
|
|
|
// block chain.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *LevelDb) FetchBlockShaByHeight(height int64) (sha *wire.ShaHash, err error) {
|
2013-08-03 17:20:05 +02:00
|
|
|
db.dbLock.Lock()
|
|
|
|
defer db.dbLock.Unlock()
|
|
|
|
|
|
|
|
return db.fetchBlockShaByHeight(height)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetchBlockShaByHeight returns a block hash based on its height in the
|
|
|
|
// block chain.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *LevelDb) fetchBlockShaByHeight(height int64) (rsha *wire.ShaHash, err error) {
|
2014-03-20 16:07:40 +01:00
|
|
|
key := int64ToKey(height)
|
|
|
|
|
|
|
|
blkVal, err := db.lDb.Get(key, db.ro)
|
2013-08-03 17:20:05 +02:00
|
|
|
if err != nil {
|
2014-03-20 16:07:40 +01:00
|
|
|
log.Tracef("failed to find height %v", height)
|
|
|
|
return // exists ???
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
var sha wire.ShaHash
|
2014-03-20 16:07:40 +01:00
|
|
|
sha.SetBytes(blkVal[0:32])
|
|
|
|
|
|
|
|
return &sha, nil
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FetchHeightRange looks up a range of blocks by the start and ending
|
|
|
|
// heights. Fetch is inclusive of the start height and exclusive of the
|
|
|
|
// ending height. To fetch all hashes from the start height until no
|
|
|
|
// more are present, use the special id `AllShas'.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *LevelDb) FetchHeightRange(startHeight, endHeight int64) (rshalist []wire.ShaHash, err error) {
|
2013-08-03 17:20:05 +02:00
|
|
|
db.dbLock.Lock()
|
|
|
|
defer db.dbLock.Unlock()
|
|
|
|
|
|
|
|
var endidx int64
|
2015-01-27 18:45:10 +01:00
|
|
|
if endHeight == database.AllShas {
|
2013-08-03 17:20:05 +02:00
|
|
|
endidx = startHeight + 500
|
|
|
|
} else {
|
|
|
|
endidx = endHeight
|
|
|
|
}
|
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
shalist := make([]wire.ShaHash, 0, endidx-startHeight)
|
2013-08-03 17:20:05 +02:00
|
|
|
for height := startHeight; height < endidx; height++ {
|
|
|
|
// TODO(drahn) fix blkFile from height
|
|
|
|
|
|
|
|
key := int64ToKey(height)
|
2013-08-22 17:40:59 +02:00
|
|
|
blkVal, lerr := db.lDb.Get(key, db.ro)
|
2013-08-03 17:20:05 +02:00
|
|
|
if lerr != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
var sha wire.ShaHash
|
2013-08-03 17:20:05 +02:00
|
|
|
sha.SetBytes(blkVal[0:32])
|
|
|
|
shalist = append(shalist, sha)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
//log.Tracef("FetchIdxRange idx %v %v returned %v shas err %v", startHeight, endHeight, len(shalist), err)
|
|
|
|
|
|
|
|
return shalist, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewestSha returns the hash and block height of the most recent (end) block of
|
|
|
|
// the block chain. It will return the zero hash, -1 for the block height, and
|
|
|
|
// no error (nil) if there are not any blocks in the database yet.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *LevelDb) NewestSha() (rsha *wire.ShaHash, rblkid int64, err error) {
|
2013-08-03 17:20:05 +02:00
|
|
|
db.dbLock.Lock()
|
|
|
|
defer db.dbLock.Unlock()
|
|
|
|
|
|
|
|
if db.lastBlkIdx == -1 {
|
2015-02-05 22:16:39 +01:00
|
|
|
return &wire.ShaHash{}, -1, nil
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
|
|
|
sha := db.lastBlkSha
|
|
|
|
|
|
|
|
return &sha, db.lastBlkIdx, nil
|
|
|
|
}
|
2014-12-25 00:55:14 +01:00
|
|
|
|
|
|
|
// fetchAddrIndexTip returns the last block height and block sha to be indexed.
|
|
|
|
// Meta-data about the address tip is currently cached in memory, and will be
|
|
|
|
// updated accordingly by functions that modify the state. This function is
|
|
|
|
// used on start up to load the info into memory. Callers will use the public
|
|
|
|
// version of this function below, which returns our cached copy.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *LevelDb) fetchAddrIndexTip() (*wire.ShaHash, int64, error) {
|
2014-12-25 00:55:14 +01:00
|
|
|
db.dbLock.Lock()
|
|
|
|
defer db.dbLock.Unlock()
|
|
|
|
|
|
|
|
data, err := db.lDb.Get(addrIndexMetaDataKey, db.ro)
|
|
|
|
if err != nil {
|
2015-02-05 22:16:39 +01:00
|
|
|
return &wire.ShaHash{}, -1, database.ErrAddrIndexDoesNotExist
|
2014-12-25 00:55:14 +01:00
|
|
|
}
|
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
var blkSha wire.ShaHash
|
2014-12-25 00:55:14 +01:00
|
|
|
blkSha.SetBytes(data[0:32])
|
|
|
|
|
|
|
|
blkHeight := binary.LittleEndian.Uint64(data[32:])
|
|
|
|
|
|
|
|
return &blkSha, int64(blkHeight), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FetchAddrIndexTip returns the hash and block height of the most recent
|
|
|
|
// block whose transactions have been indexed by address. It will return
|
|
|
|
// ErrAddrIndexDoesNotExist along with a zero hash, and -1 if the
|
|
|
|
// addrindex hasn't yet been built up.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *LevelDb) FetchAddrIndexTip() (*wire.ShaHash, int64, error) {
|
2014-12-25 00:55:14 +01:00
|
|
|
db.dbLock.Lock()
|
|
|
|
defer db.dbLock.Unlock()
|
|
|
|
|
|
|
|
if db.lastAddrIndexBlkIdx == -1 {
|
2015-02-05 22:16:39 +01:00
|
|
|
return &wire.ShaHash{}, -1, database.ErrAddrIndexDoesNotExist
|
2014-12-25 00:55:14 +01:00
|
|
|
}
|
|
|
|
sha := db.lastAddrIndexBlkSha
|
|
|
|
|
|
|
|
return &sha, db.lastAddrIndexBlkIdx, nil
|
|
|
|
}
|