4040// TestLoopOutStore tests all the basic functionality of the current bbolt
4141// swap store.
4242func TestLoopOutStore (t * testing.T ) {
43- tempDirName , err := ioutil .TempDir ("" , "clientstore" )
44- if err != nil {
45- t .Fatal (err )
46- }
47- defer os .RemoveAll (tempDirName )
48-
49- store , err := NewBoltSwapStore (tempDirName , & chaincfg .MainNetParams )
50- if err != nil {
51- t .Fatal (err )
52- }
53-
54- // First, verify that an empty database has no active swaps.
55- swaps , err := store .FetchLoopOutSwaps ()
56- if err != nil {
57- t .Fatal (err )
58- }
59- if len (swaps ) != 0 {
60- t .Fatal ("expected empty store" )
61- }
62-
6343 destAddr := test .GetDestAddr (t , 0 )
64- hash := sha256 .Sum256 (testPreimage [:])
6544 initiationTime := time .Date (2018 , 11 , 1 , 0 , 0 , 0 , 0 , time .UTC )
6645
6746 // Next, we'll make a new pending swap that we'll insert into the
@@ -92,6 +71,32 @@ func TestLoopOutStore(t *testing.T) {
9271 SwapPublicationDeadline : time .Unix (0 , initiationTime .UnixNano ()),
9372 }
9473
74+ testLoopOutStore (t , & pendingSwap )
75+ }
76+
77+ // testLoopOutStore tests the basic functionality of the current bbolt
78+ // swap store for specific swap parameters.
79+ func testLoopOutStore (t * testing.T , pendingSwap * LoopOutContract ) {
80+ tempDirName , err := ioutil .TempDir ("" , "clientstore" )
81+ if err != nil {
82+ t .Fatal (err )
83+ }
84+ defer os .RemoveAll (tempDirName )
85+
86+ store , err := NewBoltSwapStore (tempDirName , & chaincfg .MainNetParams )
87+ if err != nil {
88+ t .Fatal (err )
89+ }
90+
91+ // First, verify that an empty database has no active swaps.
92+ swaps , err := store .FetchLoopOutSwaps ()
93+ if err != nil {
94+ t .Fatal (err )
95+ }
96+ if len (swaps ) != 0 {
97+ t .Fatal ("expected empty store" )
98+ }
99+
95100 // checkSwap is a test helper function that'll assert the state of a
96101 // swap.
97102 checkSwap := func (expectedState SwapState ) {
@@ -107,7 +112,7 @@ func TestLoopOutStore(t *testing.T) {
107112 }
108113
109114 swap := swaps [0 ].Contract
110- if ! reflect .DeepEqual (swap , & pendingSwap ) {
115+ if ! reflect .DeepEqual (swap , pendingSwap ) {
111116 t .Fatal ("invalid pending swap data" )
112117 }
113118
@@ -118,15 +123,17 @@ func TestLoopOutStore(t *testing.T) {
118123 }
119124 }
120125
126+ hash := pendingSwap .Preimage .Hash ()
127+
121128 // If we create a new swap, then it should show up as being initialized
122129 // right after.
123- if err := store .CreateLoopOut (hash , & pendingSwap ); err != nil {
130+ if err := store .CreateLoopOut (hash , pendingSwap ); err != nil {
124131 t .Fatal (err )
125132 }
126133 checkSwap (StateInitiated )
127134
128135 // Trying to make the same swap again should result in an error.
129- if err := store .CreateLoopOut (hash , & pendingSwap ); err == nil {
136+ if err := store .CreateLoopOut (hash , pendingSwap ); err == nil {
130137 t .Fatal ("expected error on storing duplicate" )
131138 }
132139 checkSwap (StateInitiated )
0 commit comments