2015-05-01 08:28:01 +02:00
|
|
|
// Copyright (c) 2013-2014 The btcsuite developers
|
2013-10-13 08:59:55 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package memdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2014-07-03 02:47:24 +02:00
|
|
|
"math"
|
|
|
|
"sync"
|
|
|
|
|
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"
|
2013-10-13 08:59:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Errors that the various database functions may return.
|
|
|
|
var (
|
2014-09-16 20:43:09 +02:00
|
|
|
ErrDbClosed = errors.New("database is closed")
|
2013-10-13 08:59:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-02-05 22:16:39 +01:00
|
|
|
zeroHash = wire.ShaHash{}
|
2013-10-13 08:59:55 +02:00
|
|
|
|
|
|
|
// The following two hashes are ones that must be specially handled.
|
|
|
|
// See the comments where they're used for more details.
|
|
|
|
dupTxHash91842 = newShaHashFromStr("d5d27987d2a3dfc724e359870c6644b40e497bdc0589a033220fe15429d88599")
|
|
|
|
dupTxHash91880 = newShaHashFromStr("e3bf3d07d4b0375638d5f1db5255fe07ba2c4cb067cd81b84ee974b6585fb468")
|
|
|
|
)
|
|
|
|
|
|
|
|
// tTxInsertData holds information about the location and spent status of
|
|
|
|
// a transaction.
|
|
|
|
type tTxInsertData struct {
|
2015-08-08 04:20:49 +02:00
|
|
|
blockHeight int32
|
2013-10-13 08:59:55 +02:00
|
|
|
offset int
|
|
|
|
spentBuf []bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// newShaHashFromStr converts the passed big-endian hex string into a
|
2015-02-06 00:54:04 +01:00
|
|
|
// wire.ShaHash. It only differs from the one available in wire in that it
|
|
|
|
// ignores the error since it will only (and must only) be called with
|
2013-10-13 08:59:55 +02:00
|
|
|
// hard-coded, and therefore known good, hashes.
|
2015-02-05 22:16:39 +01:00
|
|
|
func newShaHashFromStr(hexStr string) *wire.ShaHash {
|
|
|
|
sha, _ := wire.NewShaHashFromStr(hexStr)
|
2013-10-13 08:59:55 +02:00
|
|
|
return sha
|
|
|
|
}
|
|
|
|
|
|
|
|
// isCoinbaseInput returns whether or not the passed transaction input is a
|
|
|
|
// coinbase input. A coinbase is a special transaction created by miners that
|
|
|
|
// has no inputs. This is represented in the block chain by a transaction with
|
|
|
|
// a single input that has a previous output transaction index set to the
|
|
|
|
// maximum value along with a zero hash.
|
2015-02-05 22:16:39 +01:00
|
|
|
func isCoinbaseInput(txIn *wire.TxIn) bool {
|
2014-10-01 14:52:19 +02:00
|
|
|
prevOut := &txIn.PreviousOutPoint
|
2013-10-13 08:59:55 +02:00
|
|
|
if prevOut.Index == math.MaxUint32 && prevOut.Hash.IsEqual(&zeroHash) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// isFullySpent returns whether or not a transaction represented by the passed
|
|
|
|
// transaction insert data is fully spent. A fully spent transaction is one
|
|
|
|
// where all outputs are spent.
|
|
|
|
func isFullySpent(txD *tTxInsertData) bool {
|
|
|
|
for _, spent := range txD.spentBuf {
|
|
|
|
if !spent {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-01-27 18:45:10 +01:00
|
|
|
// MemDb is a concrete implementation of the database.Db interface which provides
|
2013-10-13 08:59:55 +02:00
|
|
|
// a memory-only database. Since it is memory-only, it is obviously not
|
|
|
|
// persistent and is mostly only useful for testing purposes.
|
|
|
|
type MemDb struct {
|
|
|
|
// Embed a mutex for safe concurrent access.
|
|
|
|
sync.Mutex
|
|
|
|
|
|
|
|
// blocks holds all of the bitcoin blocks that will be in the memory
|
|
|
|
// database.
|
2015-02-05 22:16:39 +01:00
|
|
|
blocks []*wire.MsgBlock
|
2013-10-13 08:59:55 +02:00
|
|
|
|
|
|
|
// blocksBySha keeps track of block heights by hash. The height can
|
|
|
|
// be used as an index into the blocks slice.
|
2015-08-08 04:20:49 +02:00
|
|
|
blocksBySha map[wire.ShaHash]int32
|
2013-10-13 08:59:55 +02:00
|
|
|
|
|
|
|
// txns holds information about transactions such as which their
|
|
|
|
// block height and spent status of all their outputs.
|
2015-02-05 22:16:39 +01:00
|
|
|
txns map[wire.ShaHash][]*tTxInsertData
|
2013-10-13 08:59:55 +02:00
|
|
|
|
|
|
|
// closed indicates whether or not the database has been closed and is
|
|
|
|
// therefore invalidated.
|
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeTx removes the passed transaction including unspending it.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *MemDb) removeTx(msgTx *wire.MsgTx, txHash *wire.ShaHash) {
|
2013-10-13 08:59:55 +02:00
|
|
|
// Undo all of the spends for the transaction.
|
|
|
|
for _, txIn := range msgTx.TxIn {
|
|
|
|
if isCoinbaseInput(txIn) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-10-01 14:52:19 +02:00
|
|
|
prevOut := &txIn.PreviousOutPoint
|
2013-10-13 08:59:55 +02:00
|
|
|
originTxns, exists := db.txns[prevOut.Hash]
|
|
|
|
if !exists {
|
|
|
|
log.Warnf("Unable to find input transaction %s to "+
|
|
|
|
"unspend %s index %d", prevOut.Hash, txHash,
|
|
|
|
prevOut.Index)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
originTxD := originTxns[len(originTxns)-1]
|
|
|
|
originTxD.spentBuf[prevOut.Index] = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the info for the most recent version of the transaction.
|
|
|
|
txns := db.txns[*txHash]
|
|
|
|
lastIndex := len(txns) - 1
|
|
|
|
txns[lastIndex] = nil
|
|
|
|
txns = txns[:lastIndex]
|
|
|
|
db.txns[*txHash] = txns
|
|
|
|
|
|
|
|
// Remove the info entry from the map altogether if there not any older
|
|
|
|
// versions of the transaction.
|
|
|
|
if len(txns) == 0 {
|
|
|
|
delete(db.txns, *txHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-01-27 18:45:10 +01:00
|
|
|
// Close cleanly shuts down database. This is part of the database.Db interface
|
2013-10-13 08:59:55 +02:00
|
|
|
// implementation.
|
|
|
|
//
|
|
|
|
// All data is purged upon close with this implementation since it is a
|
|
|
|
// memory-only database.
|
2014-07-07 16:50:50 +02:00
|
|
|
func (db *MemDb) Close() error {
|
2013-10-13 08:59:55 +02:00
|
|
|
db.Lock()
|
|
|
|
defer db.Unlock()
|
|
|
|
|
2014-07-07 16:50:50 +02:00
|
|
|
if db.closed {
|
|
|
|
return ErrDbClosed
|
|
|
|
}
|
|
|
|
|
2013-10-13 08:59:55 +02:00
|
|
|
db.blocks = nil
|
|
|
|
db.blocksBySha = nil
|
|
|
|
db.txns = nil
|
|
|
|
db.closed = true
|
2014-07-07 16:50:50 +02:00
|
|
|
return nil
|
2013-10-13 08:59:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DropAfterBlockBySha removes any blocks from the database after the given
|
|
|
|
// block. This is different than a simple truncate since the spend information
|
2015-01-27 18:45:10 +01:00
|
|
|
// for each block must also be unwound. This is part of the database.Db interface
|
2013-10-13 08:59:55 +02:00
|
|
|
// implementation.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *MemDb) DropAfterBlockBySha(sha *wire.ShaHash) error {
|
2013-10-13 08:59:55 +02:00
|
|
|
db.Lock()
|
|
|
|
defer db.Unlock()
|
|
|
|
|
|
|
|
if db.closed {
|
|
|
|
return ErrDbClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Begin by attempting to find the height associated with the passed
|
|
|
|
// hash.
|
|
|
|
height, exists := db.blocksBySha[*sha]
|
|
|
|
if !exists {
|
|
|
|
return fmt.Errorf("block %v does not exist in the database",
|
|
|
|
sha)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The spend information has to be undone in reverse order, so loop
|
|
|
|
// backwards from the last block through the block just after the passed
|
|
|
|
// block. While doing this unspend all transactions in each block and
|
|
|
|
// remove the block.
|
2015-08-08 04:20:49 +02:00
|
|
|
endHeight := int32(len(db.blocks) - 1)
|
2013-10-13 08:59:55 +02:00
|
|
|
for i := endHeight; i > height; i-- {
|
|
|
|
// Unspend and remove each transaction in reverse order because
|
|
|
|
// later transactions in a block can reference earlier ones.
|
|
|
|
transactions := db.blocks[i].Transactions
|
|
|
|
for j := len(transactions) - 1; j >= 0; j-- {
|
|
|
|
tx := transactions[j]
|
2015-04-17 08:09:21 +02:00
|
|
|
txHash := tx.TxSha()
|
2013-10-13 08:59:55 +02:00
|
|
|
db.removeTx(tx, &txHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
db.blocks[i] = nil
|
|
|
|
db.blocks = db.blocks[:i]
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExistsSha returns whether or not the given block hash is present in the
|
2015-01-27 18:45:10 +01:00
|
|
|
// database. This is part of the database.Db interface implementation.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *MemDb) ExistsSha(sha *wire.ShaHash) (bool, error) {
|
2013-10-13 08:59:55 +02:00
|
|
|
db.Lock()
|
|
|
|
defer db.Unlock()
|
|
|
|
|
|
|
|
if db.closed {
|
2014-07-07 16:50:50 +02:00
|
|
|
return false, ErrDbClosed
|
2013-10-13 08:59:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, exists := db.blocksBySha[*sha]; exists {
|
2014-07-07 16:50:50 +02:00
|
|
|
return true, nil
|
2013-10-13 08:59:55 +02:00
|
|
|
}
|
|
|
|
|
2014-07-07 16:50:50 +02:00
|
|
|
return false, nil
|
2013-10-13 08:59:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FetchBlockBySha returns a btcutil.Block. The implementation may cache the
|
2015-01-27 18:45:10 +01:00
|
|
|
// underlying data if desired. This is part of the database.Db interface
|
2013-10-13 08:59:55 +02:00
|
|
|
// implementation.
|
|
|
|
//
|
|
|
|
// This implementation does not use any additional cache since the entire
|
|
|
|
// database is already in memory.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *MemDb) FetchBlockBySha(sha *wire.ShaHash) (*btcutil.Block, error) {
|
2013-10-13 08:59:55 +02:00
|
|
|
db.Lock()
|
|
|
|
defer db.Unlock()
|
|
|
|
|
|
|
|
if db.closed {
|
|
|
|
return nil, ErrDbClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
if blockHeight, exists := db.blocksBySha[*sha]; exists {
|
|
|
|
block := btcutil.NewBlock(db.blocks[int(blockHeight)])
|
|
|
|
block.SetHeight(blockHeight)
|
|
|
|
return block, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("block %v is not in database", sha)
|
|
|
|
}
|
|
|
|
|
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-08-08 04:20:49 +02:00
|
|
|
func (db *MemDb) FetchBlockHeightBySha(sha *wire.ShaHash) (int32, error) {
|
2014-01-19 03:59:53 +01:00
|
|
|
db.Lock()
|
|
|
|
defer db.Unlock()
|
|
|
|
|
|
|
|
if db.closed {
|
|
|
|
return 0, ErrDbClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
if blockHeight, exists := db.blocksBySha[*sha]; exists {
|
|
|
|
return blockHeight, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, fmt.Errorf("block %v is not in database", sha)
|
|
|
|
}
|
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
// FetchBlockHeaderBySha returns a wire.BlockHeader for the given sha. The
|
2014-01-19 03:59:53 +01:00
|
|
|
// implementation may cache the underlying data if desired. This is part of the
|
2015-01-27 18:45:10 +01:00
|
|
|
// database.Db interface implementation.
|
2014-01-19 03:59:53 +01:00
|
|
|
//
|
|
|
|
// This implementation does not use any additional cache since the entire
|
|
|
|
// database is already in memory.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *MemDb) FetchBlockHeaderBySha(sha *wire.ShaHash) (*wire.BlockHeader, error) {
|
2014-01-19 03:59:53 +01:00
|
|
|
db.Lock()
|
|
|
|
defer db.Unlock()
|
|
|
|
|
|
|
|
if db.closed {
|
|
|
|
return nil, ErrDbClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
if blockHeight, exists := db.blocksBySha[*sha]; exists {
|
|
|
|
return &db.blocks[int(blockHeight)].Header, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("block header %v is not in database", sha)
|
|
|
|
}
|
|
|
|
|
2013-10-13 08:59:55 +02:00
|
|
|
// FetchBlockShaByHeight returns a block hash based on its height in the block
|
2015-01-27 18:45:10 +01:00
|
|
|
// chain. This is part of the database.Db interface implementation.
|
2015-08-08 04:20:49 +02:00
|
|
|
func (db *MemDb) FetchBlockShaByHeight(height int32) (*wire.ShaHash, error) {
|
2013-10-13 08:59:55 +02:00
|
|
|
db.Lock()
|
|
|
|
defer db.Unlock()
|
|
|
|
|
|
|
|
if db.closed {
|
|
|
|
return nil, ErrDbClosed
|
|
|
|
}
|
|
|
|
|
2015-08-08 04:20:49 +02:00
|
|
|
numBlocks := int32(len(db.blocks))
|
2013-10-13 08:59:55 +02:00
|
|
|
if height < 0 || height > numBlocks-1 {
|
|
|
|
return nil, fmt.Errorf("unable to fetch block height %d since "+
|
|
|
|
"it is not within the valid range (%d-%d)", height, 0,
|
|
|
|
numBlocks-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
msgBlock := db.blocks[height]
|
2015-04-17 08:09:21 +02:00
|
|
|
blockHash := msgBlock.BlockSha()
|
2013-10-13 08:59:55 +02:00
|
|
|
return &blockHash, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2015-01-27 18:45:10 +01:00
|
|
|
// special id `AllShas'. This is part of the database.Db interface implementation.
|
2015-08-08 04:20:49 +02:00
|
|
|
func (db *MemDb) FetchHeightRange(startHeight, endHeight int32) ([]wire.ShaHash, error) {
|
2013-10-13 08:59:55 +02:00
|
|
|
db.Lock()
|
|
|
|
defer db.Unlock()
|
|
|
|
|
|
|
|
if db.closed {
|
|
|
|
return nil, ErrDbClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the user passes the special AllShas id, adjust the end height
|
|
|
|
// accordingly.
|
2015-01-27 18:45:10 +01:00
|
|
|
if endHeight == database.AllShas {
|
2015-08-08 04:20:49 +02:00
|
|
|
endHeight = int32(len(db.blocks))
|
2013-10-13 08:59:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure requested heights are sane.
|
|
|
|
if startHeight < 0 {
|
|
|
|
return nil, fmt.Errorf("start height of fetch range must not "+
|
|
|
|
"be less than zero - got %d", startHeight)
|
|
|
|
}
|
|
|
|
if endHeight < startHeight {
|
|
|
|
return nil, fmt.Errorf("end height of fetch range must not "+
|
|
|
|
"be less than the start height - got start %d, end %d",
|
|
|
|
startHeight, endHeight)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch as many as are availalbe within the specified range.
|
2015-08-08 04:20:49 +02:00
|
|
|
lastBlockIndex := int32(len(db.blocks) - 1)
|
2015-02-05 22:16:39 +01:00
|
|
|
hashList := make([]wire.ShaHash, 0, endHeight-startHeight)
|
2013-10-13 08:59:55 +02:00
|
|
|
for i := startHeight; i < endHeight; i++ {
|
|
|
|
if i > lastBlockIndex {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
msgBlock := db.blocks[i]
|
2015-04-17 08:09:21 +02:00
|
|
|
blockHash := msgBlock.BlockSha()
|
2013-10-13 08:59:55 +02:00
|
|
|
hashList = append(hashList, blockHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
return hashList, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExistsTxSha returns whether or not the given transaction hash is present in
|
2015-01-27 18:45:10 +01:00
|
|
|
// the database and is not fully spent. This is part of the database.Db interface
|
2013-10-13 08:59:55 +02:00
|
|
|
// implementation.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *MemDb) ExistsTxSha(sha *wire.ShaHash) (bool, error) {
|
2013-10-13 08:59:55 +02:00
|
|
|
db.Lock()
|
|
|
|
defer db.Unlock()
|
|
|
|
|
|
|
|
if db.closed {
|
2014-07-07 16:50:50 +02:00
|
|
|
return false, ErrDbClosed
|
2013-10-13 08:59:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if txns, exists := db.txns[*sha]; exists {
|
2014-07-07 16:50:50 +02:00
|
|
|
return !isFullySpent(txns[len(txns)-1]), nil
|
2013-10-13 08:59:55 +02:00
|
|
|
}
|
|
|
|
|
2014-07-07 16:50:50 +02:00
|
|
|
return false, nil
|
2013-10-13 08:59:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FetchTxBySha returns some data for the given transaction hash. The
|
|
|
|
// implementation may cache the underlying data if desired. This is part of the
|
2015-01-27 18:45:10 +01:00
|
|
|
// database.Db interface implementation.
|
2013-10-13 08:59:55 +02:00
|
|
|
//
|
|
|
|
// This implementation does not use any additional cache since the entire
|
|
|
|
// database is already in memory.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *MemDb) FetchTxBySha(txHash *wire.ShaHash) ([]*database.TxListReply, error) {
|
2013-10-13 08:59:55 +02:00
|
|
|
db.Lock()
|
|
|
|
defer db.Unlock()
|
|
|
|
|
|
|
|
if db.closed {
|
|
|
|
return nil, ErrDbClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
txns, exists := db.txns[*txHash]
|
|
|
|
if !exists {
|
|
|
|
log.Warnf("FetchTxBySha: requested hash of %s does not exist",
|
|
|
|
txHash)
|
2015-01-27 18:45:10 +01:00
|
|
|
return nil, database.ErrTxShaMissing
|
2013-10-13 08:59:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
txHashCopy := *txHash
|
2015-01-27 18:45:10 +01:00
|
|
|
replyList := make([]*database.TxListReply, len(txns))
|
2013-10-13 08:59:55 +02:00
|
|
|
for i, txD := range txns {
|
|
|
|
msgBlock := db.blocks[txD.blockHeight]
|
2015-04-17 08:09:21 +02:00
|
|
|
blockSha := msgBlock.BlockSha()
|
2013-10-13 08:59:55 +02:00
|
|
|
|
|
|
|
spentBuf := make([]bool, len(txD.spentBuf))
|
|
|
|
copy(spentBuf, txD.spentBuf)
|
2015-01-27 18:45:10 +01:00
|
|
|
reply := database.TxListReply{
|
2013-10-13 08:59:55 +02:00
|
|
|
Sha: &txHashCopy,
|
|
|
|
Tx: msgBlock.Transactions[txD.offset],
|
|
|
|
BlkSha: &blockSha,
|
|
|
|
Height: txD.blockHeight,
|
|
|
|
TxSpent: spentBuf,
|
|
|
|
Err: nil,
|
|
|
|
}
|
|
|
|
replyList[i] = &reply
|
|
|
|
}
|
|
|
|
|
|
|
|
return replyList, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetchTxByShaList fetches transactions and information about them given an
|
|
|
|
// array of transaction hashes. The result is a slice of of TxListReply objects
|
|
|
|
// which contain the transaction and information about it such as what block and
|
|
|
|
// block height it's contained in and which outputs are spent.
|
|
|
|
//
|
|
|
|
// The includeSpent flag indicates whether or not information about transactions
|
|
|
|
// which are fully spent should be returned. When the flag is not set, the
|
|
|
|
// corresponding entry in the TxListReply slice for fully spent transactions
|
|
|
|
// will indicate the transaction does not exist.
|
|
|
|
//
|
|
|
|
// This function must be called with the db lock held.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *MemDb) fetchTxByShaList(txShaList []*wire.ShaHash, includeSpent bool) []*database.TxListReply {
|
2015-01-27 18:45:10 +01:00
|
|
|
replyList := make([]*database.TxListReply, 0, len(txShaList))
|
2013-10-13 08:59:55 +02:00
|
|
|
for i, hash := range txShaList {
|
|
|
|
// Every requested entry needs a response, so start with nothing
|
|
|
|
// more than a response with the requested hash marked missing.
|
|
|
|
// The reply will be updated below with the appropriate
|
|
|
|
// information if the transaction exists.
|
2015-01-27 18:45:10 +01:00
|
|
|
reply := database.TxListReply{
|
2013-10-13 08:59:55 +02:00
|
|
|
Sha: txShaList[i],
|
2015-01-27 18:45:10 +01:00
|
|
|
Err: database.ErrTxShaMissing,
|
2013-10-13 08:59:55 +02:00
|
|
|
}
|
|
|
|
replyList = append(replyList, &reply)
|
|
|
|
|
|
|
|
if db.closed {
|
|
|
|
reply.Err = ErrDbClosed
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if txns, exists := db.txns[*hash]; exists {
|
|
|
|
// A given transaction may have duplicates so long as the
|
|
|
|
// previous one is fully spent. We are only interested
|
|
|
|
// in the most recent version of the transaction for
|
|
|
|
// this function. The FetchTxBySha function can be
|
|
|
|
// used to get all versions of a transaction.
|
|
|
|
txD := txns[len(txns)-1]
|
|
|
|
if !includeSpent && isFullySpent(txD) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look up the referenced block and get its hash. Set
|
|
|
|
// the reply error appropriately and go to the next
|
|
|
|
// requested transaction if anything goes wrong.
|
|
|
|
msgBlock := db.blocks[txD.blockHeight]
|
2015-04-17 08:09:21 +02:00
|
|
|
blockSha := msgBlock.BlockSha()
|
2013-10-13 08:59:55 +02:00
|
|
|
|
|
|
|
// Make a copy of the spent buf to return so the caller
|
|
|
|
// can't accidentally modify it.
|
|
|
|
spentBuf := make([]bool, len(txD.spentBuf))
|
|
|
|
copy(spentBuf, txD.spentBuf)
|
|
|
|
|
|
|
|
// Populate the reply.
|
|
|
|
reply.Tx = msgBlock.Transactions[txD.offset]
|
|
|
|
reply.BlkSha = &blockSha
|
|
|
|
reply.Height = txD.blockHeight
|
|
|
|
reply.TxSpent = spentBuf
|
|
|
|
reply.Err = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return replyList
|
|
|
|
}
|
|
|
|
|
|
|
|
// FetchTxByShaList returns a TxListReply given an array of transaction hashes.
|
|
|
|
// The implementation may cache the underlying data if desired. This is part of
|
2015-01-27 18:45:10 +01:00
|
|
|
// the database.Db interface implementation.
|
2013-10-13 08:59:55 +02:00
|
|
|
//
|
|
|
|
// This implementation does not use any additional cache since the entire
|
|
|
|
// database is already in memory.
|
|
|
|
|
|
|
|
// FetchTxByShaList returns a TxListReply given an array of transaction
|
|
|
|
// hashes. This function differs from FetchUnSpentTxByShaList in that it
|
|
|
|
// returns the most recent version of fully spent transactions. Due to the
|
|
|
|
// increased number of transaction fetches, this function is typically more
|
|
|
|
// expensive than the unspent counterpart, however the specific performance
|
|
|
|
// details depend on the concrete implementation. The implementation may cache
|
2015-01-27 18:45:10 +01:00
|
|
|
// the underlying data if desired. This is part of the database.Db interface
|
2013-10-13 08:59:55 +02:00
|
|
|
// implementation.
|
|
|
|
//
|
|
|
|
// To fetch all versions of a specific transaction, call FetchTxBySha.
|
|
|
|
//
|
|
|
|
// This implementation does not use any additional cache since the entire
|
|
|
|
// database is already in memory.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *MemDb) FetchTxByShaList(txShaList []*wire.ShaHash) []*database.TxListReply {
|
2013-10-13 08:59:55 +02:00
|
|
|
db.Lock()
|
|
|
|
defer db.Unlock()
|
|
|
|
|
|
|
|
return db.fetchTxByShaList(txShaList, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FetchUnSpentTxByShaList returns a TxListReply given an array of transaction
|
|
|
|
// hashes. Any transactions which are fully spent will indicate they do not
|
|
|
|
// exist by setting the Err field to TxShaMissing. The implementation may cache
|
2015-01-27 18:45:10 +01:00
|
|
|
// the underlying data if desired. This is part of the database.Db interface
|
2013-10-13 08:59:55 +02:00
|
|
|
// implementation.
|
|
|
|
//
|
|
|
|
// To obtain results which do contain the most recent version of a fully spent
|
|
|
|
// transactions, call FetchTxByShaList. To fetch all versions of a specific
|
|
|
|
// transaction, call FetchTxBySha.
|
|
|
|
//
|
|
|
|
// This implementation does not use any additional cache since the entire
|
|
|
|
// database is already in memory.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (db *MemDb) FetchUnSpentTxByShaList(txShaList []*wire.ShaHash) []*database.TxListReply {
|
2013-10-13 08:59:55 +02:00
|
|
|
db.Lock()
|
|
|
|
defer db.Unlock()
|
|
|
|
|
|
|
|
return db.fetchTxByShaList(txShaList, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InsertBlock inserts raw block and transaction data from a block 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
|
2015-01-27 18:45:10 +01:00
|
|
|
// block to already exist. This is part of the database.Db interface
|
2013-10-13 08:59:55 +02:00
|
|
|
// implementation.
|
2015-08-08 04:20:49 +02:00
|
|
|
func (db *MemDb) InsertBlock(block *btcutil.Block) (int32, error) {
|
2013-10-13 08:59:55 +02:00
|
|
|
db.Lock()
|
|
|
|
defer db.Unlock()
|
|
|
|
|
|
|
|
if db.closed {
|
|
|
|
return 0, ErrDbClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reject the insert if the previously reference block does not exist
|
|
|
|
// except in the case there are no blocks inserted yet where the first
|
|
|
|
// inserted block is assumed to be a genesis block.
|
|
|
|
msgBlock := block.MsgBlock()
|
|
|
|
if _, exists := db.blocksBySha[msgBlock.Header.PrevBlock]; !exists {
|
|
|
|
if len(db.blocks) > 0 {
|
2015-01-27 18:45:10 +01:00
|
|
|
return 0, database.ErrPrevShaMissing
|
2013-10-13 08:59:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build a map of in-flight transactions because some of the inputs in
|
|
|
|
// this block could be referencing other transactions earlier in this
|
|
|
|
// block which are not yet in the chain.
|
2015-02-05 22:16:39 +01:00
|
|
|
txInFlight := map[wire.ShaHash]int{}
|
2013-10-13 08:59:55 +02:00
|
|
|
transactions := block.Transactions()
|
|
|
|
for i, tx := range transactions {
|
|
|
|
txInFlight[*tx.Sha()] = i
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop through all transactions and inputs to ensure there are no error
|
|
|
|
// conditions that would prevent them from be inserted into the db.
|
|
|
|
// Although these checks could could be done in the loop below, checking
|
|
|
|
// for error conditions up front means the code below doesn't have to
|
|
|
|
// deal with rollback on errors.
|
2015-08-08 04:20:49 +02:00
|
|
|
newHeight := int32(len(db.blocks))
|
2013-10-13 08:59:55 +02:00
|
|
|
for i, tx := range transactions {
|
|
|
|
// Two old blocks contain duplicate transactions due to being
|
|
|
|
// mined by faulty miners and accepted by the origin Satoshi
|
|
|
|
// client. Rules have since been added to the ensure this
|
|
|
|
// problem can no longer happen, but the two duplicate
|
|
|
|
// transactions which were originally accepted are forever in
|
|
|
|
// the block chain history and must be dealth with specially.
|
|
|
|
// http://blockexplorer.com/b/91842
|
|
|
|
// http://blockexplorer.com/b/91880
|
|
|
|
if newHeight == 91842 && tx.Sha().IsEqual(dupTxHash91842) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if newHeight == 91880 && tx.Sha().IsEqual(dupTxHash91880) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, txIn := range tx.MsgTx().TxIn {
|
|
|
|
if isCoinbaseInput(txIn) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// It is acceptable for a transaction input to reference
|
|
|
|
// the output of another transaction in this block only
|
|
|
|
// if the referenced transaction comes before the
|
|
|
|
// current one in this block.
|
2014-10-01 14:52:19 +02:00
|
|
|
prevOut := &txIn.PreviousOutPoint
|
2013-10-13 08:59:55 +02:00
|
|
|
if inFlightIndex, ok := txInFlight[prevOut.Hash]; ok {
|
|
|
|
if i <= inFlightIndex {
|
|
|
|
log.Warnf("InsertBlock: requested hash "+
|
|
|
|
" of %s does not exist in-flight",
|
|
|
|
tx.Sha())
|
2015-01-27 18:45:10 +01:00
|
|
|
return 0, database.ErrTxShaMissing
|
2013-10-13 08:59:55 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
originTxns, exists := db.txns[prevOut.Hash]
|
|
|
|
if !exists {
|
|
|
|
log.Warnf("InsertBlock: requested hash "+
|
|
|
|
"of %s by %s does not exist",
|
|
|
|
prevOut.Hash, tx.Sha())
|
2015-01-27 18:45:10 +01:00
|
|
|
return 0, database.ErrTxShaMissing
|
2013-10-13 08:59:55 +02:00
|
|
|
}
|
|
|
|
originTxD := originTxns[len(originTxns)-1]
|
|
|
|
if prevOut.Index > uint32(len(originTxD.spentBuf)) {
|
|
|
|
log.Warnf("InsertBlock: requested hash "+
|
|
|
|
"of %s with index %d does not "+
|
|
|
|
"exist", tx.Sha(), prevOut.Index)
|
2015-01-27 18:45:10 +01:00
|
|
|
return 0, database.ErrTxShaMissing
|
2013-10-13 08:59:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent duplicate transactions in the same block.
|
|
|
|
if inFlightIndex, exists := txInFlight[*tx.Sha()]; exists &&
|
|
|
|
inFlightIndex < i {
|
|
|
|
log.Warnf("Block contains duplicate transaction %s",
|
|
|
|
tx.Sha())
|
2015-01-27 18:45:10 +01:00
|
|
|
return 0, database.ErrDuplicateSha
|
2013-10-13 08:59:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent duplicate transactions unless the old one is fully
|
|
|
|
// spent.
|
|
|
|
if txns, exists := db.txns[*tx.Sha()]; exists {
|
|
|
|
txD := txns[len(txns)-1]
|
|
|
|
if !isFullySpent(txD) {
|
|
|
|
log.Warnf("Attempt to insert duplicate "+
|
|
|
|
"transaction %s", tx.Sha())
|
2015-01-27 18:45:10 +01:00
|
|
|
return 0, database.ErrDuplicateSha
|
2013-10-13 08:59:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
db.blocks = append(db.blocks, msgBlock)
|
2015-04-17 07:44:15 +02:00
|
|
|
db.blocksBySha[*block.Sha()] = newHeight
|
2013-10-13 08:59:55 +02:00
|
|
|
|
|
|
|
// Insert information about eacj transaction and spend all of the
|
|
|
|
// outputs referenced by the inputs to the transactions.
|
|
|
|
for i, tx := range block.Transactions() {
|
|
|
|
// Insert the transaction data.
|
|
|
|
txD := tTxInsertData{
|
|
|
|
blockHeight: newHeight,
|
|
|
|
offset: i,
|
|
|
|
spentBuf: make([]bool, len(tx.MsgTx().TxOut)),
|
|
|
|
}
|
|
|
|
db.txns[*tx.Sha()] = append(db.txns[*tx.Sha()], &txD)
|
|
|
|
|
|
|
|
// Spend all of the inputs.
|
|
|
|
for _, txIn := range tx.MsgTx().TxIn {
|
|
|
|
// Coinbase transaction has no inputs.
|
|
|
|
if isCoinbaseInput(txIn) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Already checked for existing and valid ranges above.
|
2014-10-01 14:52:19 +02:00
|
|
|
prevOut := &txIn.PreviousOutPoint
|
2013-10-13 08:59:55 +02:00
|
|
|
originTxns := db.txns[prevOut.Hash]
|
|
|
|
originTxD := originTxns[len(originTxns)-1]
|
|
|
|
originTxD.spentBuf[prevOut.Index] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newHeight, 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. This is part
|
2015-01-27 18:45:10 +01:00
|
|
|
// of the database.Db interface implementation.
|
2015-08-08 04:20:49 +02:00
|
|
|
func (db *MemDb) NewestSha() (*wire.ShaHash, int32, error) {
|
2013-10-13 08:59:55 +02:00
|
|
|
db.Lock()
|
|
|
|
defer db.Unlock()
|
|
|
|
|
|
|
|
if db.closed {
|
|
|
|
return nil, 0, ErrDbClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the database has not had a genesis block inserted yet, return
|
|
|
|
// values specified by interface contract.
|
|
|
|
numBlocks := len(db.blocks)
|
|
|
|
if numBlocks == 0 {
|
|
|
|
return &zeroHash, -1, nil
|
|
|
|
}
|
|
|
|
|
2015-04-17 08:09:21 +02:00
|
|
|
blockSha := db.blocks[numBlocks-1].BlockSha()
|
2015-08-08 04:20:49 +02:00
|
|
|
return &blockSha, int32(numBlocks - 1), nil
|
2013-10-13 08:59:55 +02:00
|
|
|
}
|
|
|
|
|
2014-12-25 00:55:14 +01:00
|
|
|
// FetchAddrIndexTip isn't currently implemented. This is a part of the
|
2015-01-27 18:45:10 +01:00
|
|
|
// database.Db interface implementation.
|
2015-08-08 04:20:49 +02:00
|
|
|
func (db *MemDb) FetchAddrIndexTip() (*wire.ShaHash, int32, error) {
|
2015-01-27 18:45:10 +01:00
|
|
|
return nil, 0, database.ErrNotImplemented
|
2014-12-25 00:55:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateAddrIndexForBlock isn't currently implemented. This is a part of the
|
2015-01-27 18:45:10 +01:00
|
|
|
// database.Db interface implementation.
|
2015-08-08 04:20:49 +02:00
|
|
|
func (db *MemDb) UpdateAddrIndexForBlock(*wire.ShaHash, int32,
|
2015-01-27 18:45:10 +01:00
|
|
|
database.BlockAddrIndex) error {
|
|
|
|
return database.ErrNotImplemented
|
2014-12-25 00:55:14 +01:00
|
|
|
}
|
|
|
|
|
2015-01-27 18:45:10 +01:00
|
|
|
// FetchTxsForAddr isn't currently implemented. This is a part of the database.Db
|
2014-12-25 00:55:14 +01:00
|
|
|
// interface implementation.
|
2015-08-27 19:34:24 +02:00
|
|
|
func (db *MemDb) FetchTxsForAddr(btcutil.Address, int, int, bool) ([]*database.TxListReply, int, error) {
|
2015-08-28 13:58:18 +02:00
|
|
|
return nil, 0, database.ErrNotImplemented
|
2014-12-25 00:55:14 +01:00
|
|
|
}
|
|
|
|
|
2015-01-27 18:45:10 +01:00
|
|
|
// DeleteAddrIndex isn't currently implemented. This is a part of the database.Db
|
2014-12-25 00:55:14 +01:00
|
|
|
// interface implementation.
|
|
|
|
func (db *MemDb) DeleteAddrIndex() error {
|
2015-01-27 18:45:10 +01:00
|
|
|
return database.ErrNotImplemented
|
2014-12-25 00:55:14 +01:00
|
|
|
}
|
|
|
|
|
2013-10-13 08:59:55 +02:00
|
|
|
// RollbackClose discards the recent database changes to the previously saved
|
2015-01-27 18:45:10 +01:00
|
|
|
// data at last Sync and closes the database. This is part of the database.Db
|
2013-10-13 08:59:55 +02:00
|
|
|
// interface implementation.
|
|
|
|
//
|
|
|
|
// The database is completely purged on close with this implementation since the
|
|
|
|
// entire database is only in memory. As a result, this function behaves no
|
|
|
|
// differently than Close.
|
2014-07-07 16:50:50 +02:00
|
|
|
func (db *MemDb) RollbackClose() error {
|
2013-10-13 08:59:55 +02:00
|
|
|
// Rollback doesn't apply to a memory database, so just call Close.
|
|
|
|
// Close handles the mutex locks.
|
2014-07-07 16:50:50 +02:00
|
|
|
return db.Close()
|
2013-10-13 08:59:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sync verifies that the database is coherent on disk and no outstanding
|
2015-01-27 18:45:10 +01:00
|
|
|
// transactions are in flight. This is part of the database.Db interface
|
2013-10-13 08:59:55 +02:00
|
|
|
// implementation.
|
|
|
|
//
|
|
|
|
// This implementation does not write any data to disk, so this function only
|
|
|
|
// grabs a lock to ensure it doesn't return until other operations are complete.
|
2014-07-07 16:50:50 +02:00
|
|
|
func (db *MemDb) Sync() error {
|
2013-10-13 08:59:55 +02:00
|
|
|
db.Lock()
|
|
|
|
defer db.Unlock()
|
|
|
|
|
|
|
|
if db.closed {
|
2014-07-07 16:50:50 +02:00
|
|
|
return ErrDbClosed
|
2013-10-13 08:59:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// There is nothing extra to do to sync the memory database. However,
|
|
|
|
// the lock is still grabbed to ensure the function does not return
|
|
|
|
// until other operations are complete.
|
2014-07-07 16:50:50 +02:00
|
|
|
return nil
|
2013-10-13 08:59:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// newMemDb returns a new memory-only database ready for block inserts.
|
|
|
|
func newMemDb() *MemDb {
|
|
|
|
db := MemDb{
|
2015-02-05 22:16:39 +01:00
|
|
|
blocks: make([]*wire.MsgBlock, 0, 200000),
|
2015-08-08 04:20:49 +02:00
|
|
|
blocksBySha: make(map[wire.ShaHash]int32),
|
2015-02-05 22:16:39 +01:00
|
|
|
txns: make(map[wire.ShaHash][]*tTxInsertData),
|
2013-10-13 08:59:55 +02:00
|
|
|
}
|
|
|
|
return &db
|
|
|
|
}
|