From 314cd98152a6a3941ce9123c0b6d825c6e56e36c Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Thu, 27 Aug 2020 21:14:55 +0200 Subject: [PATCH] wallet: extract addUtxo in create TX test --- wallet/createtx_test.go | 84 ++++++++++++++++++++++++----------------- 1 file changed, 49 insertions(+), 35 deletions(-) diff --git a/wallet/createtx_test.go b/wallet/createtx_test.go index 04a52c1..31a8753 100644 --- a/wallet/createtx_test.go +++ b/wallet/createtx_test.go @@ -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) + } +}