2017-01-17 01:19:02 +01:00
|
|
|
// Copyright (c) 2013-2017 The btcsuite developers
|
|
|
|
// Copyright (c) 2015-2016 The Decred developers
|
2015-12-01 19:44:58 +01:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
2015-04-06 21:18:04 +02:00
|
|
|
|
|
|
|
package wtxmgr
|
|
|
|
|
|
|
|
import (
|
2018-05-15 07:11:11 +02:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcwallet/walletdb"
|
2015-04-06 21:18:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// insertMemPoolTx inserts the unmined transaction record. It also marks
|
|
|
|
// previous outputs referenced by the inputs as spent.
|
2017-01-17 01:19:02 +01:00
|
|
|
func (s *Store) insertMemPoolTx(ns walletdb.ReadWriteBucket, rec *TxRecord) error {
|
2019-07-11 00:54:28 +02:00
|
|
|
// Check whether the transaction has already been added to the store,
|
|
|
|
// regardless of whether is has confirmed or not. This ensures that we
|
|
|
|
// don't add it to the unconfirmed bucket again if it has already
|
|
|
|
// confirmed.
|
|
|
|
//
|
|
|
|
// TODO: compare serialized txs to ensure this isn't a hash
|
|
|
|
// collision?
|
|
|
|
if txDetails, _ := s.TxDetails(ns, &rec.Hash); txDetails != nil {
|
2021-05-28 14:29:48 +02:00
|
|
|
return ErrDuplicateTx
|
2015-04-06 21:18:04 +02:00
|
|
|
}
|
|
|
|
|
2018-08-30 00:28:40 +02:00
|
|
|
// Since transaction records within the store are keyed by their
|
|
|
|
// transaction _and_ block confirmation, we'll iterate through the
|
|
|
|
// transaction's outputs to determine if we've already seen them to
|
|
|
|
// prevent from adding this transaction to the unconfirmed bucket.
|
|
|
|
for i := range rec.MsgTx.TxOut {
|
|
|
|
k := canonicalOutPoint(&rec.Hash, uint32(i))
|
|
|
|
if existsRawUnspent(ns, k) != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-06 21:18:04 +02:00
|
|
|
log.Infof("Inserting unconfirmed transaction %v", rec.Hash)
|
|
|
|
v, err := valueTxRecord(rec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = putRawUnmined(ns, rec.Hash[:], v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, input := range rec.MsgTx.TxIn {
|
|
|
|
prevOut := &input.PreviousOutPoint
|
|
|
|
k := canonicalOutPoint(&prevOut.Hash, prevOut.Index)
|
|
|
|
err = putRawUnminedInput(ns, k, rec.Hash[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: increment credit amount for each credit (but those are unknown
|
|
|
|
// here currently).
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeDoubleSpends checks for any unmined transactions which would introduce
|
|
|
|
// a double spend if tx was added to the store (either as a confirmed or unmined
|
|
|
|
// transaction). Each conflicting transaction and all transactions which spend
|
|
|
|
// it are recursively removed.
|
2017-01-17 01:19:02 +01:00
|
|
|
func (s *Store) removeDoubleSpends(ns walletdb.ReadWriteBucket, rec *TxRecord) error {
|
2015-04-06 21:18:04 +02:00
|
|
|
for _, input := range rec.MsgTx.TxIn {
|
|
|
|
prevOut := &input.PreviousOutPoint
|
|
|
|
prevOutKey := canonicalOutPoint(&prevOut.Hash, prevOut.Index)
|
2018-06-09 03:59:57 +02:00
|
|
|
|
|
|
|
doubleSpendHashes := fetchUnminedInputSpendTxHashes(ns, prevOutKey)
|
|
|
|
for _, doubleSpendHash := range doubleSpendHashes {
|
2019-03-23 00:59:04 +01:00
|
|
|
// We'll make sure not to remove ourselves.
|
|
|
|
if rec.Hash == doubleSpendHash {
|
|
|
|
continue
|
|
|
|
}
|
2018-06-09 03:59:57 +02:00
|
|
|
|
|
|
|
// If the spending transaction spends multiple outputs
|
|
|
|
// from the same transaction, we'll find duplicate
|
|
|
|
// entries within the store, so it's possible we're
|
|
|
|
// unable to find it if the conflicts have already been
|
|
|
|
// removed in a previous iteration.
|
2019-03-23 00:59:04 +01:00
|
|
|
doubleSpendVal := existsRawUnmined(
|
|
|
|
ns, doubleSpendHash[:],
|
|
|
|
)
|
2018-06-09 03:59:57 +02:00
|
|
|
if doubleSpendVal == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-04-06 21:18:04 +02:00
|
|
|
var doubleSpend TxRecord
|
2018-06-09 03:59:57 +02:00
|
|
|
doubleSpend.Hash = doubleSpendHash
|
|
|
|
err := readRawTxRecord(
|
|
|
|
&doubleSpend.Hash, doubleSpendVal, &doubleSpend,
|
|
|
|
)
|
2015-04-06 21:18:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Removing double spending transaction %v",
|
|
|
|
doubleSpend.Hash)
|
2019-03-23 00:59:04 +01:00
|
|
|
|
2018-06-09 03:59:57 +02:00
|
|
|
if err := s.removeConflict(ns, &doubleSpend); err != nil {
|
2015-04-06 21:18:04 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-09 03:59:57 +02:00
|
|
|
|
2015-04-06 21:18:04 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeConflict removes an unmined transaction record and all spend chains
|
|
|
|
// deriving from it from the store. This is designed to remove transactions
|
|
|
|
// that would otherwise result in double spend conflicts if left in the store,
|
|
|
|
// and to remove transactions that spend coinbase transactions on reorgs.
|
2017-01-17 01:19:02 +01:00
|
|
|
func (s *Store) removeConflict(ns walletdb.ReadWriteBucket, rec *TxRecord) error {
|
2015-04-06 21:18:04 +02:00
|
|
|
// For each potential credit for this record, each spender (if any) must
|
|
|
|
// be recursively removed as well. Once the spenders are removed, the
|
|
|
|
// credit is deleted.
|
2018-06-09 03:59:57 +02:00
|
|
|
for i := range rec.MsgTx.TxOut {
|
|
|
|
k := canonicalOutPoint(&rec.Hash, uint32(i))
|
|
|
|
spenderHashes := fetchUnminedInputSpendTxHashes(ns, k)
|
|
|
|
for _, spenderHash := range spenderHashes {
|
|
|
|
// If the spending transaction spends multiple outputs
|
|
|
|
// from the same transaction, we'll find duplicate
|
|
|
|
// entries within the store, so it's possible we're
|
|
|
|
// unable to find it if the conflicts have already been
|
|
|
|
// removed in a previous iteration.
|
2019-03-23 00:59:04 +01:00
|
|
|
spenderVal := existsRawUnmined(ns, spenderHash[:])
|
2018-06-09 03:59:57 +02:00
|
|
|
if spenderVal == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-04-06 21:18:04 +02:00
|
|
|
var spender TxRecord
|
2018-06-09 03:59:57 +02:00
|
|
|
spender.Hash = spenderHash
|
2015-04-06 21:18:04 +02:00
|
|
|
err := readRawTxRecord(&spender.Hash, spenderVal, &spender)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Transaction %v is part of a removed conflict "+
|
|
|
|
"chain -- removing as well", spender.Hash)
|
2018-06-09 03:59:57 +02:00
|
|
|
if err := s.removeConflict(ns, &spender); err != nil {
|
2015-04-06 21:18:04 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-06-09 03:59:57 +02:00
|
|
|
if err := deleteRawUnminedCredit(ns, k); err != nil {
|
2015-04-06 21:18:04 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this tx spends any previous credits (either mined or unmined), set
|
|
|
|
// each unspent. Mined transactions are only marked spent by having the
|
|
|
|
// output in the unmined inputs bucket.
|
|
|
|
for _, input := range rec.MsgTx.TxIn {
|
|
|
|
prevOut := &input.PreviousOutPoint
|
|
|
|
k := canonicalOutPoint(&prevOut.Hash, prevOut.Index)
|
2019-03-23 00:59:04 +01:00
|
|
|
err := deleteRawUnminedInput(ns, k, rec.Hash)
|
|
|
|
if err != nil {
|
2015-04-06 21:18:04 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return deleteRawUnmined(ns, rec.Hash[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnminedTxs returns the underlying transactions for all unmined transactions
|
2016-03-17 00:07:38 +01:00
|
|
|
// which are not known to have been mined in a block. Transactions are
|
|
|
|
// guaranteed to be sorted by their dependency order.
|
2017-01-17 01:19:02 +01:00
|
|
|
func (s *Store) UnminedTxs(ns walletdb.ReadBucket) ([]*wire.MsgTx, error) {
|
|
|
|
recSet, err := s.unminedTxRecords(ns)
|
2016-03-17 00:07:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-01 01:41:19 +01:00
|
|
|
txSet := make(map[chainhash.Hash]*wire.MsgTx, len(recSet))
|
|
|
|
for txHash, txRec := range recSet {
|
|
|
|
txSet[txHash] = &txRec.MsgTx
|
2016-03-17 00:07:38 +01:00
|
|
|
}
|
2019-02-01 01:41:19 +01:00
|
|
|
|
2019-02-01 01:42:53 +01:00
|
|
|
return DependencySort(txSet), nil
|
2015-04-06 21:18:04 +02:00
|
|
|
}
|
|
|
|
|
2017-01-17 01:19:02 +01:00
|
|
|
func (s *Store) unminedTxRecords(ns walletdb.ReadBucket) (map[chainhash.Hash]*TxRecord, error) {
|
2016-08-08 21:49:09 +02:00
|
|
|
unmined := make(map[chainhash.Hash]*TxRecord)
|
2017-01-17 01:19:02 +01:00
|
|
|
err := ns.NestedReadBucket(bucketUnmined).ForEach(func(k, v []byte) error {
|
2016-08-08 21:49:09 +02:00
|
|
|
var txHash chainhash.Hash
|
2015-04-06 21:18:04 +02:00
|
|
|
err := readRawUnminedHash(k, &txHash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-03-17 00:07:38 +01:00
|
|
|
rec := new(TxRecord)
|
|
|
|
err = readRawTxRecord(&txHash, v, rec)
|
2015-04-06 21:18:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-17 00:07:38 +01:00
|
|
|
unmined[rec.Hash] = rec
|
2015-04-06 21:18:04 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return unmined, err
|
|
|
|
}
|
Modernize the RPC server.
This is a rather monolithic commit that moves the old RPC server to
its own package (rpc/legacyrpc), introduces a new RPC server using
gRPC (rpc/rpcserver), and provides the ability to defer wallet loading
until request at a later time by an RPC (--noinitialload).
The legacy RPC server remains the default for now while the new gRPC
server is not enabled by default. Enabling the new server requires
setting a listen address (--experimenalrpclisten). This experimental
flag is used to effectively feature gate the server until it is ready
to use as a default. Both RPC servers can be run at the same time,
but require binding to different listen addresses.
In theory, with the legacy RPC server now living in its own package it
should become much easier to unit test the handlers. This will be
useful for any future changes to the package, as compatibility with
Core's wallet is still desired.
Type safety has also been improved in the legacy RPC server. Multiple
handler types are now used for methods that do and do not require the
RPC client as a dependency. This can statically help prevent nil
pointer dereferences, and was very useful for catching bugs during
refactoring.
To synchronize the wallet loading process between the main package
(the default) and through the gRPC WalletLoader service (with the
--noinitialload option), as well as increasing the loose coupling of
packages, a new wallet.Loader type has been added. All creating and
loading of existing wallets is done through a single Loader instance,
and callbacks can be attached to the instance to run after the wallet
has been opened. This is how the legacy RPC server is associated with
a loaded wallet, even after the wallet is loaded by a gRPC method in a
completely unrelated package.
Documentation for the new RPC server has been added to the
rpc/documentation directory. The documentation includes a
specification for the new RPC API, addresses how to make changes to
the server implementation, and provides short example clients in
several different languages.
Some of the new RPC methods are not implementated exactly as described
by the specification. These are considered bugs with the
implementation, not the spec. Known bugs are commented as such.
2015-06-01 21:57:50 +02:00
|
|
|
|
|
|
|
// UnminedTxHashes returns the hashes of all transactions not known to have been
|
|
|
|
// mined in a block.
|
2017-01-17 01:19:02 +01:00
|
|
|
func (s *Store) UnminedTxHashes(ns walletdb.ReadBucket) ([]*chainhash.Hash, error) {
|
|
|
|
return s.unminedTxHashes(ns)
|
Modernize the RPC server.
This is a rather monolithic commit that moves the old RPC server to
its own package (rpc/legacyrpc), introduces a new RPC server using
gRPC (rpc/rpcserver), and provides the ability to defer wallet loading
until request at a later time by an RPC (--noinitialload).
The legacy RPC server remains the default for now while the new gRPC
server is not enabled by default. Enabling the new server requires
setting a listen address (--experimenalrpclisten). This experimental
flag is used to effectively feature gate the server until it is ready
to use as a default. Both RPC servers can be run at the same time,
but require binding to different listen addresses.
In theory, with the legacy RPC server now living in its own package it
should become much easier to unit test the handlers. This will be
useful for any future changes to the package, as compatibility with
Core's wallet is still desired.
Type safety has also been improved in the legacy RPC server. Multiple
handler types are now used for methods that do and do not require the
RPC client as a dependency. This can statically help prevent nil
pointer dereferences, and was very useful for catching bugs during
refactoring.
To synchronize the wallet loading process between the main package
(the default) and through the gRPC WalletLoader service (with the
--noinitialload option), as well as increasing the loose coupling of
packages, a new wallet.Loader type has been added. All creating and
loading of existing wallets is done through a single Loader instance,
and callbacks can be attached to the instance to run after the wallet
has been opened. This is how the legacy RPC server is associated with
a loaded wallet, even after the wallet is loaded by a gRPC method in a
completely unrelated package.
Documentation for the new RPC server has been added to the
rpc/documentation directory. The documentation includes a
specification for the new RPC API, addresses how to make changes to
the server implementation, and provides short example clients in
several different languages.
Some of the new RPC methods are not implementated exactly as described
by the specification. These are considered bugs with the
implementation, not the spec. Known bugs are commented as such.
2015-06-01 21:57:50 +02:00
|
|
|
}
|
|
|
|
|
2017-01-17 01:19:02 +01:00
|
|
|
func (s *Store) unminedTxHashes(ns walletdb.ReadBucket) ([]*chainhash.Hash, error) {
|
2016-08-08 21:49:09 +02:00
|
|
|
var hashes []*chainhash.Hash
|
2017-01-17 01:19:02 +01:00
|
|
|
err := ns.NestedReadBucket(bucketUnmined).ForEach(func(k, v []byte) error {
|
2016-08-08 21:49:09 +02:00
|
|
|
hash := new(chainhash.Hash)
|
Modernize the RPC server.
This is a rather monolithic commit that moves the old RPC server to
its own package (rpc/legacyrpc), introduces a new RPC server using
gRPC (rpc/rpcserver), and provides the ability to defer wallet loading
until request at a later time by an RPC (--noinitialload).
The legacy RPC server remains the default for now while the new gRPC
server is not enabled by default. Enabling the new server requires
setting a listen address (--experimenalrpclisten). This experimental
flag is used to effectively feature gate the server until it is ready
to use as a default. Both RPC servers can be run at the same time,
but require binding to different listen addresses.
In theory, with the legacy RPC server now living in its own package it
should become much easier to unit test the handlers. This will be
useful for any future changes to the package, as compatibility with
Core's wallet is still desired.
Type safety has also been improved in the legacy RPC server. Multiple
handler types are now used for methods that do and do not require the
RPC client as a dependency. This can statically help prevent nil
pointer dereferences, and was very useful for catching bugs during
refactoring.
To synchronize the wallet loading process between the main package
(the default) and through the gRPC WalletLoader service (with the
--noinitialload option), as well as increasing the loose coupling of
packages, a new wallet.Loader type has been added. All creating and
loading of existing wallets is done through a single Loader instance,
and callbacks can be attached to the instance to run after the wallet
has been opened. This is how the legacy RPC server is associated with
a loaded wallet, even after the wallet is loaded by a gRPC method in a
completely unrelated package.
Documentation for the new RPC server has been added to the
rpc/documentation directory. The documentation includes a
specification for the new RPC API, addresses how to make changes to
the server implementation, and provides short example clients in
several different languages.
Some of the new RPC methods are not implementated exactly as described
by the specification. These are considered bugs with the
implementation, not the spec. Known bugs are commented as such.
2015-06-01 21:57:50 +02:00
|
|
|
err := readRawUnminedHash(k, hash)
|
|
|
|
if err == nil {
|
|
|
|
hashes = append(hashes, hash)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
return hashes, err
|
|
|
|
}
|