wallet: extract addUtxo in create TX test

This commit is contained in:
Oliver Gugger 2020-08-27 21:14:55 +02:00
parent 66edcae704
commit 314cd98152
No known key found for this signature in database
GPG key ID: 8E4256593F177720

View file

@ -18,6 +18,14 @@ import (
"github.com/btcsuite/btcwallet/wtxmgr"
)
var (
testBlockHash, _ = chainhash.NewHashFromStr(
"00000000000000017188b968a371bab95aa43522665353b646e41865abae" +
"02a4",
)
testBlockHeight int32 = 276425
)
// TestTxToOutput checks that no new address is added to he database if we
// request a dry run of the txToOutputs call. It also makes sure a subsequent
// non-dry run call produces a similar transaction to the dry-run.
@ -45,41 +53,7 @@ func TestTxToOutputsDryRun(t *testing.T) {
txOut,
},
}
var b bytes.Buffer
if err := incomingTx.Serialize(&b); err != nil {
t.Fatalf("unable to serialize tx: %v", err)
}
txBytes := b.Bytes()
rec, err := wtxmgr.NewTxRecord(txBytes, time.Now())
if err != nil {
t.Fatalf("unable to create tx record: %v", err)
}
// The block meta will be inserted to tell the wallet this is a
// confirmed transaction.
blockHash, _ := chainhash.NewHashFromStr(
"00000000000000017188b968a371bab95aa43522665353b646e41865abae02a4")
block := &wtxmgr.BlockMeta{
Block: wtxmgr.Block{Hash: *blockHash, Height: 276425},
Time: time.Unix(1387737310, 0),
}
if err := walletdb.Update(w.db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(wtxmgrNamespaceKey)
err = w.TxStore.InsertTx(ns, rec, block)
if err != nil {
return err
}
err = w.TxStore.AddCredit(ns, rec, block, 0, false)
if err != nil {
return err
}
return nil
}); err != nil {
t.Fatalf("failed inserting tx: %v", err)
}
addUtxo(t, w, incomingTx)
// Now tell the wallet to create a transaction paying to the specified
// outputs.
@ -175,3 +149,43 @@ func TestTxToOutputsDryRun(t *testing.T) {
"than wet run")
}
}
// addUtxo add the given transaction to the wallet's database marked as a
// confirmed UTXO .
func addUtxo(t *testing.T, w *Wallet, incomingTx *wire.MsgTx) {
var b bytes.Buffer
if err := incomingTx.Serialize(&b); err != nil {
t.Fatalf("unable to serialize tx: %v", err)
}
txBytes := b.Bytes()
rec, err := wtxmgr.NewTxRecord(txBytes, time.Now())
if err != nil {
t.Fatalf("unable to create tx record: %v", err)
}
// The block meta will be inserted to tell the wallet this is a
// confirmed transaction.
block := &wtxmgr.BlockMeta{
Block: wtxmgr.Block{
Hash: *testBlockHash,
Height: testBlockHeight,
},
Time: time.Unix(1387737310, 0),
}
if err := walletdb.Update(w.db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(wtxmgrNamespaceKey)
err = w.TxStore.InsertTx(ns, rec, block)
if err != nil {
return err
}
err = w.TxStore.AddCredit(ns, rec, block, 0, false)
if err != nil {
return err
}
return nil
}); err != nil {
t.Fatalf("failed inserting tx: %v", err)
}
}