From ac731b8e5216de0eb037b4c77cab6abf05977ab9 Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Wed, 30 Oct 2019 16:32:46 -0700 Subject: [PATCH] wtxmgr: add TestInsertMempoolTxAndConfirm This test ensures that there aren't any lingering unconfirmed records for a transaction that existed within the store as unconfirmed before becoming confirmed. At the moment, this is currently failing due to a gap when moving a transaction from unconfirmed to confirmed within the store. This will be resolved in a subsequent commit. --- wtxmgr/tx_test.go | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/wtxmgr/tx_test.go b/wtxmgr/tx_test.go index 8914e1a..6f8f72e 100644 --- a/wtxmgr/tx_test.go +++ b/wtxmgr/tx_test.go @@ -2119,3 +2119,71 @@ func TestAddDuplicateCreditAfterConfirm(t *testing.T) { } }) } + +// TestInsertMempoolTxAndConfirm ensures that there aren't any lingering +// unconfirmed records for a transaction that existed within the store as +// unconfirmed before becoming confirmed. +func TestInsertMempoolTxAndConfirm(t *testing.T) { + t.Parallel() + + store, db, teardown, err := testStore() + if err != nil { + t.Fatal(err) + } + defer teardown() + + // Create a transaction which we'll insert into the store as + // unconfirmed. + tx := newCoinBase(1e8) + txRec, err := NewTxRecordFromMsgTx(tx, time.Now()) + if err != nil { + t.Fatal(err) + } + commitDBTx(t, store, db, func(ns walletdb.ReadWriteBucket) { + if err := store.InsertTx(ns, txRec, nil); err != nil { + t.Fatal(err) + } + err := store.AddCredit(ns, txRec, nil, 0, false) + if err != nil { + t.Fatal(err) + } + }) + + // Then, proceed to confirm it. + commitDBTx(t, store, db, func(ns walletdb.ReadWriteBucket) { + block := &BlockMeta{ + Block: Block{Height: 1337}, + Time: time.Now(), + } + if err := store.InsertTx(ns, txRec, block); err != nil { + t.Fatal(err) + } + err := store.AddCredit(ns, txRec, block, 0, false) + if err != nil { + t.Fatal(err) + } + }) + + // We should not see any lingering unconfirmed records for it once it's + // been confirmed. + commitDBTx(t, store, db, func(ns walletdb.ReadWriteBucket) { + for _, input := range tx.TxIn { + prevOut := input.PreviousOutPoint + k := canonicalOutPoint(&prevOut.Hash, prevOut.Index) + if existsRawUnminedInput(ns, k) != nil { + t.Fatalf("found transaction input %v as "+ + "unconfirmed", prevOut) + } + } + if existsRawUnmined(ns, txRec.Hash[:]) != nil { + t.Fatal("found transaction as unconfirmed") + } + for i := range tx.TxOut { + k := canonicalOutPoint(&txRec.Hash, uint32(i)) + if existsRawUnminedCredit(ns, k) != nil { + t.Fatalf("found transaction output %v as "+ + "unconfirmed", i) + } + } + }) +}