2015-05-01 08:28:01 +02:00
|
|
|
// Copyright (c) 2013-2014 The btcsuite developers
|
2013-05-29 02:07:21 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-01-27 18:45:10 +01:00
|
|
|
package database
|
2013-05-29 02:07:21 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2014-07-03 02:47:24 +02:00
|
|
|
|
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-03-04 04:25:38 +01:00
|
|
|
"github.com/btcsuite/golangcrypto/ripemd160"
|
2013-05-29 02:07:21 +02:00
|
|
|
)
|
|
|
|
|
2013-05-31 21:07:48 +02:00
|
|
|
// Errors that the various database functions may return.
|
2013-05-29 02:07:21 +02:00
|
|
|
var (
|
2015-03-19 14:56:06 +01:00
|
|
|
ErrAddrIndexDoesNotExist = errors.New("address index hasn't been built or is an older version")
|
2014-12-25 00:55:14 +01:00
|
|
|
ErrUnsupportedAddressType = errors.New("address type is not supported " +
|
|
|
|
"by the address-index")
|
2014-09-16 20:43:09 +02:00
|
|
|
ErrPrevShaMissing = errors.New("previous sha missing from database")
|
|
|
|
ErrTxShaMissing = errors.New("requested transaction does not exist")
|
|
|
|
ErrBlockShaMissing = errors.New("requested block does not exist")
|
|
|
|
ErrDuplicateSha = errors.New("duplicate insert attempted")
|
|
|
|
ErrDbDoesNotExist = errors.New("non-existent database")
|
|
|
|
ErrDbUnknownType = errors.New("non-existent database type")
|
2014-12-25 00:55:14 +01:00
|
|
|
ErrNotImplemented = errors.New("method has not yet been implemented")
|
2013-05-29 02:07:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// AllShas is a special value that can be used as the final sha when requesting
|
|
|
|
// a range of shas by height to request them all.
|
2015-08-08 04:20:49 +02:00
|
|
|
const AllShas = int32(^uint32(0) >> 1)
|
2013-05-29 02:07:21 +02:00
|
|
|
|
2013-05-31 22:12:20 +02:00
|
|
|
// Db defines a generic interface that is used to request and insert data into
|
|
|
|
// the bitcoin block chain. This interface is intended to be agnostic to actual
|
|
|
|
// mechanism used for backend data storage. The AddDBDriver function can be
|
|
|
|
// used to add a new backend data storage method.
|
2013-05-29 02:07:21 +02:00
|
|
|
type Db interface {
|
|
|
|
// Close cleanly shuts down the database and syncs all data.
|
2014-07-07 16:50:50 +02:00
|
|
|
Close() (err error)
|
2013-05-29 02:07:21 +02:00
|
|
|
|
|
|
|
// DropAfterBlockBySha will remove any blocks from the database after
|
|
|
|
// the given block. It terminates any existing transaction and performs
|
|
|
|
// its operations in an atomic transaction which is commited before
|
|
|
|
// the function returns.
|
2015-02-05 22:16:39 +01:00
|
|
|
DropAfterBlockBySha(*wire.ShaHash) (err error)
|
2013-05-29 02:07:21 +02:00
|
|
|
|
|
|
|
// ExistsSha returns whether or not the given block hash is present in
|
|
|
|
// the database.
|
2015-02-05 22:16:39 +01:00
|
|
|
ExistsSha(sha *wire.ShaHash) (exists bool, err error)
|
2013-05-29 02:07:21 +02:00
|
|
|
|
|
|
|
// FetchBlockBySha returns a btcutil Block. The implementation may
|
2013-05-31 20:40:38 +02:00
|
|
|
// cache the underlying data if desired.
|
2015-02-05 22:16:39 +01:00
|
|
|
FetchBlockBySha(sha *wire.ShaHash) (blk *btcutil.Block, err error)
|
2013-05-29 02:07:21 +02:00
|
|
|
|
2014-01-19 03:59:53 +01:00
|
|
|
// FetchBlockHeightBySha returns the block height for the given hash.
|
2015-08-08 04:20:49 +02:00
|
|
|
FetchBlockHeightBySha(sha *wire.ShaHash) (height int32, err error)
|
2014-01-19 03:59:53 +01:00
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
// FetchBlockHeaderBySha returns a wire.BlockHeader for the given
|
2014-01-19 03:59:53 +01:00
|
|
|
// sha. The implementation may cache the underlying data if desired.
|
2015-02-05 22:16:39 +01:00
|
|
|
FetchBlockHeaderBySha(sha *wire.ShaHash) (bh *wire.BlockHeader, err error)
|
2014-01-19 03:59:53 +01:00
|
|
|
|
2013-05-31 00:27:39 +02:00
|
|
|
// FetchBlockShaByHeight returns a block hash based on its height in the
|
|
|
|
// block chain.
|
2015-08-08 04:20:49 +02:00
|
|
|
FetchBlockShaByHeight(height int32) (sha *wire.ShaHash, err error)
|
2013-05-29 02:07:21 +02:00
|
|
|
|
2013-05-31 00:27:39 +02:00
|
|
|
// FetchHeightRange looks up a range of blocks by the start and ending
|
2013-05-31 00:18:13 +02:00
|
|
|
// 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-08-08 04:20:49 +02:00
|
|
|
FetchHeightRange(startHeight, endHeight int32) (rshalist []wire.ShaHash, err error)
|
2013-05-29 02:07:21 +02:00
|
|
|
|
2013-07-10 01:11:02 +02:00
|
|
|
// ExistsTxSha returns whether or not the given tx hash is present in
|
|
|
|
// the database
|
2015-02-05 22:16:39 +01:00
|
|
|
ExistsTxSha(sha *wire.ShaHash) (exists bool, err error)
|
2013-07-10 01:11:02 +02:00
|
|
|
|
2013-05-31 20:40:38 +02:00
|
|
|
// FetchTxBySha returns some data for the given transaction hash. The
|
|
|
|
// implementation may cache the underlying data if desired.
|
2015-02-05 22:16:39 +01:00
|
|
|
FetchTxBySha(txsha *wire.ShaHash) ([]*TxListReply, error)
|
2013-05-29 02:07:21 +02:00
|
|
|
|
2013-05-31 00:18:13 +02:00
|
|
|
// FetchTxByShaList returns a TxListReply given an array of transaction
|
2013-05-31 20:40:38 +02:00
|
|
|
// hashes. The implementation may cache the underlying data if desired.
|
2013-10-13 17:08:21 +02:00
|
|
|
// This differs from FetchUnSpentTxByShaList in that it will return
|
|
|
|
// the most recent known Tx, if it is fully spent or not.
|
2014-07-07 16:50:50 +02:00
|
|
|
//
|
|
|
|
// NOTE: This function does not return an error directly since it MUST
|
|
|
|
// return at least one TxListReply instance for each requested
|
|
|
|
// transaction. Each TxListReply instance then contains an Err field
|
|
|
|
// which can be used to detect errors.
|
2015-02-05 22:16:39 +01:00
|
|
|
FetchTxByShaList(txShaList []*wire.ShaHash) []*TxListReply
|
2013-10-13 17:08:21 +02:00
|
|
|
|
|
|
|
// FetchUnSpentTxByShaList returns a TxListReply given an array of
|
|
|
|
// transaction hashes. The implementation may cache the underlying
|
|
|
|
// data if desired. Fully spent transactions will not normally not
|
|
|
|
// be returned in this operation.
|
2014-07-07 16:50:50 +02:00
|
|
|
//
|
|
|
|
// NOTE: This function does not return an error directly since it MUST
|
|
|
|
// return at least one TxListReply instance for each requested
|
|
|
|
// transaction. Each TxListReply instance then contains an Err field
|
|
|
|
// which can be used to detect errors.
|
2015-02-05 22:16:39 +01:00
|
|
|
FetchUnSpentTxByShaList(txShaList []*wire.ShaHash) []*TxListReply
|
2013-05-29 02:07:21 +02:00
|
|
|
|
2013-05-31 00:18:13 +02:00
|
|
|
// InsertBlock inserts raw block and transaction data from a block
|
2013-07-25 23:44:18 +02:00
|
|
|
// into the database. The first block inserted into the database
|
|
|
|
// will be treated as the genesis block. Every subsequent block insert
|
|
|
|
// requires the referenced parent block to already exist.
|
2015-08-08 04:20:49 +02:00
|
|
|
InsertBlock(block *btcutil.Block) (height int32, err error)
|
2013-05-29 02:07:21 +02:00
|
|
|
|
2013-07-25 23:44:18 +02:00
|
|
|
// 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-08-08 04:20:49 +02:00
|
|
|
NewestSha() (sha *wire.ShaHash, height int32, err error)
|
2013-05-29 02:07:21 +02:00
|
|
|
|
2014-12-25 00:55:14 +01:00
|
|
|
// FetchAddrIndexTip returns the hash and block height of the most recent
|
|
|
|
// block which has had its address index populated. It will return
|
|
|
|
// ErrAddrIndexDoesNotExist along with a zero hash, and -1 if the
|
|
|
|
// addrindex hasn't yet been built up.
|
2015-08-08 04:20:49 +02:00
|
|
|
FetchAddrIndexTip() (sha *wire.ShaHash, height int32, err error)
|
2014-12-25 00:55:14 +01:00
|
|
|
|
|
|
|
// UpdateAddrIndexForBlock updates the stored addrindex with passed
|
|
|
|
// index information for a particular block height. Additionally, it
|
|
|
|
// will update the stored meta-data related to the curent tip of the
|
|
|
|
// addr index. These two operations are performed in an atomic
|
|
|
|
// transaction which is commited before the function returns.
|
|
|
|
// Addresses are indexed by the raw bytes of their base58 decoded
|
|
|
|
// hash160.
|
2015-08-08 04:20:49 +02:00
|
|
|
UpdateAddrIndexForBlock(blkSha *wire.ShaHash, height int32,
|
2014-12-25 00:55:14 +01:00
|
|
|
addrIndex BlockAddrIndex) error
|
|
|
|
|
|
|
|
// FetchTxsForAddr looks up and returns all transactions which either
|
|
|
|
// spend a previously created output of the passed address, or create
|
|
|
|
// a new output locked to the passed address. The, `limit` parameter
|
|
|
|
// should be the max number of transactions to be returned.
|
|
|
|
// Additionally, if the caller wishes to skip forward in the results
|
|
|
|
// some amount, the 'seek' represents how many results to skip.
|
|
|
|
// NOTE: Values for both `seek` and `limit` MUST be positive.
|
2015-08-28 13:58:18 +02:00
|
|
|
// It will return the array of fetched transactions, along with the amount
|
|
|
|
// of transactions that were actually skipped.
|
|
|
|
FetchTxsForAddr(addr btcutil.Address, skip int, limit int) ([]*TxListReply, int, error)
|
2014-12-25 00:55:14 +01:00
|
|
|
|
|
|
|
// DeleteAddrIndex deletes the entire addrindex stored within the DB.
|
|
|
|
DeleteAddrIndex() error
|
|
|
|
|
2013-05-29 02:07:21 +02:00
|
|
|
// RollbackClose discards the recent database changes to the previously
|
|
|
|
// saved data at last Sync and closes the database.
|
2014-07-07 16:50:50 +02:00
|
|
|
RollbackClose() (err error)
|
2013-05-29 02:07:21 +02:00
|
|
|
|
|
|
|
// Sync verifies that the database is coherent on disk and no
|
|
|
|
// outstanding transactions are in flight.
|
2014-07-07 16:50:50 +02:00
|
|
|
Sync() (err error)
|
2013-05-29 02:07:21 +02:00
|
|
|
}
|
|
|
|
|
2013-05-31 22:19:34 +02:00
|
|
|
// DriverDB defines a structure for backend drivers to use when they registered
|
|
|
|
// themselves as a backend which implements the Db interface.
|
2013-05-29 02:07:21 +02:00
|
|
|
type DriverDB struct {
|
2014-01-20 03:01:31 +01:00
|
|
|
DbType string
|
|
|
|
CreateDB func(args ...interface{}) (pbdb Db, err error)
|
|
|
|
OpenDB func(args ...interface{}) (pbdb Db, err error)
|
2013-05-29 02:07:21 +02:00
|
|
|
}
|
|
|
|
|
2013-05-31 22:19:34 +02:00
|
|
|
// TxListReply is used to return individual transaction information when
|
|
|
|
// data about multiple transactions is requested in a single call.
|
2013-05-29 02:07:21 +02:00
|
|
|
type TxListReply struct {
|
2015-02-05 22:16:39 +01:00
|
|
|
Sha *wire.ShaHash
|
|
|
|
Tx *wire.MsgTx
|
|
|
|
BlkSha *wire.ShaHash
|
2015-08-08 04:20:49 +02:00
|
|
|
Height int32
|
2013-07-11 22:35:50 +02:00
|
|
|
TxSpent []bool
|
|
|
|
Err error
|
2013-05-29 02:07:21 +02:00
|
|
|
}
|
|
|
|
|
2014-12-25 00:55:14 +01:00
|
|
|
// AddrIndexKeySize is the number of bytes used by keys into the BlockAddrIndex.
|
|
|
|
const AddrIndexKeySize = ripemd160.Size
|
|
|
|
|
|
|
|
// BlockAddrIndex represents the indexing structure for addresses.
|
|
|
|
// It maps a hash160 to a list of transaction locations within a block that
|
|
|
|
// either pays to or spends from the passed UTXO for the hash160.
|
2015-02-05 22:16:39 +01:00
|
|
|
type BlockAddrIndex map[[AddrIndexKeySize]byte][]*wire.TxLoc
|
2014-12-25 00:55:14 +01:00
|
|
|
|
2013-05-29 02:07:21 +02:00
|
|
|
// driverList holds all of the registered database backends.
|
|
|
|
var driverList []DriverDB
|
|
|
|
|
|
|
|
// AddDBDriver adds a back end database driver to available interfaces.
|
|
|
|
func AddDBDriver(instance DriverDB) {
|
|
|
|
// TODO(drahn) Does this really need to check for duplicate names ?
|
|
|
|
for _, drv := range driverList {
|
|
|
|
// TODO(drahn) should duplicates be an error?
|
|
|
|
if drv.DbType == instance.DbType {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
driverList = append(driverList, instance)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateDB intializes and opens a database.
|
2014-01-20 03:01:31 +01:00
|
|
|
func CreateDB(dbtype string, args ...interface{}) (pbdb Db, err error) {
|
2013-05-29 02:07:21 +02:00
|
|
|
for _, drv := range driverList {
|
|
|
|
if drv.DbType == dbtype {
|
2014-01-20 03:01:31 +01:00
|
|
|
return drv.CreateDB(args...)
|
2013-05-29 02:07:21 +02:00
|
|
|
}
|
|
|
|
}
|
2014-09-16 20:43:09 +02:00
|
|
|
return nil, ErrDbUnknownType
|
2013-05-29 02:07:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// OpenDB opens an existing database.
|
2014-01-20 03:01:31 +01:00
|
|
|
func OpenDB(dbtype string, args ...interface{}) (pbdb Db, err error) {
|
2013-05-29 02:07:21 +02:00
|
|
|
for _, drv := range driverList {
|
|
|
|
if drv.DbType == dbtype {
|
2014-01-20 03:01:31 +01:00
|
|
|
return drv.OpenDB(args...)
|
2013-05-29 02:07:21 +02:00
|
|
|
}
|
|
|
|
}
|
2014-09-16 20:43:09 +02:00
|
|
|
return nil, ErrDbUnknownType
|
2013-05-29 02:07:21 +02:00
|
|
|
}
|
2013-09-15 22:18:46 +02:00
|
|
|
|
|
|
|
// SupportedDBs returns a slice of strings that represent the database drivers
|
|
|
|
// that have been registered and are therefore supported.
|
|
|
|
func SupportedDBs() []string {
|
|
|
|
var supportedDBs []string
|
|
|
|
for _, drv := range driverList {
|
|
|
|
supportedDBs = append(supportedDBs, drv.DbType)
|
|
|
|
}
|
|
|
|
return supportedDBs
|
|
|
|
}
|