Skip to content

Commit a870529

Browse files
committed
wtxmgr: add method OutputsToWatch
This commit adds a new method `OutputsToWatch`, which returns a list of outpoints the rescan process should be watching for during the startup.
1 parent 1045730 commit a870529

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

wtxmgr/tx.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,95 @@ func (s *Store) rollback(ns walletdb.ReadWriteBucket, height int32) error {
805805
return putMinedBalance(ns, minedBalance)
806806
}
807807

808+
// OutputsToWatch returns a list of outputs to monitor during the wallet's
809+
// startup. The returned items are similar to UnspentOutputs, exccept the
810+
// locked outputs and unmined credits are also returned here. In addition, we
811+
// only set the field `OutPoint` and `PkScript` for the `Credit`, as these are
812+
// the only fields used during the rescan.
813+
func (s *Store) OutputsToWatch(ns walletdb.ReadBucket) ([]Credit, error) {
814+
var (
815+
unspent []Credit
816+
op wire.OutPoint
817+
block Block
818+
)
819+
820+
err := ns.NestedReadBucket(bucketUnspent).ForEach(func(k,
821+
v []byte) error {
822+
823+
err := readCanonicalOutPoint(k, &op)
824+
if err != nil {
825+
return err
826+
}
827+
828+
err = readUnspentBlock(v, &block)
829+
if err != nil {
830+
return err
831+
}
832+
833+
// TODO(jrick): reading the entire transaction should be
834+
// avoidable. Creating the credit only requires the output
835+
// amount and pkScript.
836+
rec, err := fetchTxRecord(ns, &op.Hash, &block)
837+
if err != nil {
838+
return fmt.Errorf("unable to retrieve tx %v: %w",
839+
op.Hash, err)
840+
}
841+
842+
txOut := rec.MsgTx.TxOut[op.Index]
843+
cred := Credit{
844+
OutPoint: op,
845+
PkScript: txOut.PkScript,
846+
}
847+
unspent = append(unspent, cred)
848+
849+
return nil
850+
})
851+
if err != nil {
852+
if _, ok := err.(Error); ok {
853+
return nil, err
854+
}
855+
str := "failed iterating unspent bucket"
856+
return nil, storeError(ErrDatabase, str, err)
857+
}
858+
859+
err = ns.NestedReadBucket(bucketUnminedCredits).ForEach(func(k,
860+
v []byte) error {
861+
862+
if err := readCanonicalOutPoint(k, &op); err != nil {
863+
return err
864+
}
865+
866+
// TODO(jrick): Reading/parsing the entire transaction record
867+
// just for the output amount and script can be avoided.
868+
recVal := existsRawUnmined(ns, op.Hash[:])
869+
870+
var rec TxRecord
871+
err = readRawTxRecord(&op.Hash, recVal, &rec)
872+
if err != nil {
873+
return fmt.Errorf("unable to retrieve raw tx%v: %w",
874+
op.Hash, err)
875+
}
876+
877+
txOut := rec.MsgTx.TxOut[op.Index]
878+
cred := Credit{
879+
OutPoint: op,
880+
PkScript: txOut.PkScript,
881+
}
882+
unspent = append(unspent, cred)
883+
884+
return nil
885+
})
886+
if err != nil {
887+
if _, ok := err.(Error); ok {
888+
return nil, err
889+
}
890+
str := "failed iterating unmined credits bucket"
891+
return nil, storeError(ErrDatabase, str, err)
892+
}
893+
894+
return unspent, nil
895+
}
896+
808897
// UnspentOutputs returns all unspent received transaction outputs.
809898
// The order is undefined.
810899
func (s *Store) UnspentOutputs(ns walletdb.ReadBucket) ([]Credit, error) {

0 commit comments

Comments
 (0)