github.com/decred/dcrlnd@v0.7.6/sweep/store_test.go (about) 1 package sweep 2 3 import ( 4 "testing" 5 6 "github.com/decred/dcrd/chaincfg/chainhash" 7 "github.com/decred/dcrd/wire" 8 "github.com/decred/dcrlnd/channeldb" 9 ) 10 11 // TestStore asserts that the store persists the presented data to disk and is 12 // able to retrieve it again. 13 func TestStore(t *testing.T) { 14 t.Run("bbolt", func(t *testing.T) { 15 16 // Create new store. 17 cdb, cleanUp, err := channeldb.MakeTestDB() 18 if err != nil { 19 t.Fatalf("unable to open channel db: %v", err) 20 } 21 defer cleanUp() 22 23 if err != nil { 24 t.Fatal(err) 25 } 26 27 testStore(t, func() (SweeperStore, error) { 28 var chain chainhash.Hash 29 return NewSweeperStore(cdb, &chain) 30 }) 31 }) 32 t.Run("mock", func(t *testing.T) { 33 store := NewMockSweeperStore() 34 35 testStore(t, func() (SweeperStore, error) { 36 // Return same store, because the mock has no real 37 // persistence. 38 return store, nil 39 }) 40 }) 41 } 42 43 func testStore(t *testing.T, createStore func() (SweeperStore, error)) { 44 store, err := createStore() 45 if err != nil { 46 t.Fatal(err) 47 } 48 49 // Initially we expect the store not to have a last published tx. 50 retrievedTx, err := store.GetLastPublishedTx() 51 if err != nil { 52 t.Fatal(err) 53 } 54 if retrievedTx != nil { 55 t.Fatal("expected no last published tx") 56 } 57 58 // Notify publication of tx1 59 tx1 := wire.MsgTx{} 60 tx1.AddTxIn(&wire.TxIn{ 61 PreviousOutPoint: wire.OutPoint{ 62 Index: 1, 63 }, 64 }) 65 66 err = store.NotifyPublishTx(&tx1) 67 if err != nil { 68 t.Fatal(err) 69 } 70 71 // Notify publication of tx2 72 tx2 := wire.MsgTx{} 73 tx2.AddTxIn(&wire.TxIn{ 74 PreviousOutPoint: wire.OutPoint{ 75 Index: 2, 76 }, 77 }) 78 79 err = store.NotifyPublishTx(&tx2) 80 if err != nil { 81 t.Fatal(err) 82 } 83 84 // Recreate the sweeper store 85 store, err = createStore() 86 if err != nil { 87 t.Fatal(err) 88 } 89 90 // Assert that last published tx2 is present. 91 retrievedTx, err = store.GetLastPublishedTx() 92 if err != nil { 93 t.Fatal(err) 94 } 95 96 if tx2.TxHash() != retrievedTx.TxHash() { 97 t.Fatal("txes do not match") 98 } 99 100 // Assert that both txes are recognized as our own. 101 ours, err := store.IsOurTx(tx1.TxHash()) 102 if err != nil { 103 t.Fatal(err) 104 } 105 if !ours { 106 t.Fatal("expected tx to be ours") 107 } 108 109 ours, err = store.IsOurTx(tx2.TxHash()) 110 if err != nil { 111 t.Fatal(err) 112 } 113 if !ours { 114 t.Fatal("expected tx to be ours") 115 } 116 117 // An different hash should be reported as not being ours. 118 var unknownHash chainhash.Hash 119 ours, err = store.IsOurTx(unknownHash) 120 if err != nil { 121 t.Fatal(err) 122 } 123 if ours { 124 t.Fatal("expected tx to be not ours") 125 } 126 127 txns, err := store.ListSweeps() 128 if err != nil { 129 t.Fatalf("unexpected error: %v", err) 130 } 131 132 // Create a map containing the sweeps we expect to be returned by list 133 // sweeps. 134 expected := map[chainhash.Hash]bool{ 135 tx1.TxHash(): true, 136 tx2.TxHash(): true, 137 } 138 139 if len(txns) != len(expected) { 140 t.Fatalf("expected: %v sweeps, got: %v", len(expected), 141 len(txns)) 142 } 143 144 for _, tx := range txns { 145 _, ok := expected[tx] 146 if !ok { 147 t.Fatalf("unexpected tx: %v", tx) 148 } 149 } 150 }