1515package transactions_test
1616
1717import (
18+ "os"
1819 "testing"
1920
2021 "github.com/btcsuite/btcd/chaincfg"
@@ -40,34 +41,23 @@ import (
4041 "github.com/stretchr/testify/suite"
4142)
4243
44+ func TestMain (m * testing.M ) {
45+ test .TstSetupLogging ()
46+ os .Exit (m .Run ())
47+ }
48+
4349type BlockchainMock struct {
4450 blockchainMock.Interface
45- transactions map [chainhash.Hash ]* wire.MsgTx
46- transactionGetCallbacks map [chainhash.Hash ][]func ()
51+ transactions map [chainhash.Hash ]* wire.MsgTx
4752}
4853
4954func NewBlockchainMock () * BlockchainMock {
5055 blockchainMock := & BlockchainMock {
51- transactions : map [chainhash.Hash ]* wire.MsgTx {},
52- transactionGetCallbacks : map [chainhash.Hash ][]func (){},
56+ transactions : map [chainhash.Hash ]* wire.MsgTx {},
5357 }
5458 return blockchainMock
5559}
5660
57- func (blockchain * BlockchainMock ) CallTransactionGetCallbacks (txHash chainhash.Hash ) {
58- callbacks := blockchain .transactionGetCallbacks [txHash ]
59- delete (blockchain .transactionGetCallbacks , txHash )
60- for _ , callback := range callbacks {
61- callback ()
62- }
63- }
64-
65- func (blockchain * BlockchainMock ) CallAllTransactionGetCallbacks () {
66- for txHash := range blockchain .transactionGetCallbacks {
67- blockchain .CallTransactionGetCallbacks (txHash )
68- }
69- }
70-
7161func (blockchain * BlockchainMock ) RegisterTxs (txs ... * wire.MsgTx ) {
7262 for _ , tx := range txs {
7363 blockchain .transactions [tx .TxHash ()] = tx
@@ -84,17 +74,7 @@ func (blockchain *BlockchainMock) TransactionGet(
8474 if ! ok {
8575 panic ("you need to first register the transaction with the mock backend" )
8676 }
87- callbacks , ok := blockchain .transactionGetCallbacks [txHash ]
88- if ! ok {
89- callbacks = []func (){}
90- }
91- blockchain .transactionGetCallbacks [txHash ] = append (callbacks ,
92- func () {
93- defer cleanup (nil )
94- if err := success (tx ); err != nil {
95- panic (err )
96- }
97- })
77+ go func () { _ = success (tx ) }()
9878}
9979
10080func (blockchain * BlockchainMock ) ConnectionStatus () blockchainpkg.Status {
@@ -152,7 +132,6 @@ func (s *transactionsSuite) updateAddressHistory(
152132 }
153133
154134 s .transactions .UpdateAddressHistory (address .PubkeyScriptHashHex (), txs )
155- s .blockchainMock .CallAllTransactionGetCallbacks ()
156135}
157136
158137func newTx (
@@ -170,50 +149,6 @@ func newTx(
170149 }
171150}
172151
173- // TestUpdateAddressHistorySyncStatus checks that the synchronizer is calling the
174- // syncStart/syncFinished callbacks once in the beginning and once after all transactions have
175- // processed.
176- func (s * transactionsSuite ) TestUpdateAddressHistorySyncStatus () {
177- addresses := s .addressChain .EnsureAddresses ()
178- address := addresses [0 ]
179- expectedAmount := btcutil .Amount (123 )
180- tx1 := newTx (chainhash .HashH (nil ), 0 , address , expectedAmount )
181- tx1Hash := tx1 .TxHash ()
182- tx2 := newTx (chainhash .HashH (nil ), 1 , address , expectedAmount )
183- tx2Hash := tx2 .TxHash ()
184- s .blockchainMock .RegisterTxs (tx1 , tx2 )
185- var syncStarted , syncFinished bool
186- onSyncStarted := func () {
187- if syncStarted {
188- require .FailNow (s .T (), "sync started twice" )
189- }
190- syncStarted = true
191- }
192- onSyncFinished := func () {
193- if syncFinished {
194- require .FailNow (s .T (), "sync finished twice" )
195- }
196- syncFinished = true
197- }
198- * s .synchronizer = * synchronizer .NewSynchronizer (onSyncStarted , onSyncFinished , s .log )
199- s .notifierMock .On ("Put" , tx1Hash [:]).Return (nil ).Once ()
200- s .notifierMock .On ("Put" , tx2Hash [:]).Return (nil ).Once ()
201- s .transactions .UpdateAddressHistory (address .PubkeyScriptHashHex (), []* blockchainpkg.TxInfo {
202- {TXHash : blockchainpkg .TXHash (tx1Hash ), Height : 10 },
203- {TXHash : blockchainpkg .TXHash (tx2Hash ), Height : 10 },
204- })
205- require .True (s .T (), syncStarted )
206- require .False (s .T (), syncFinished )
207- s .headersMock .On ("HeaderByHeight" , 10 ).Return (nil , nil ).Once ()
208- s .blockchainMock .CallTransactionGetCallbacks (tx1 .TxHash ())
209- require .True (s .T (), syncStarted )
210- require .False (s .T (), syncFinished )
211- s .headersMock .On ("HeaderByHeight" , 10 ).Return (nil , nil ).Once ()
212- s .blockchainMock .CallTransactionGetCallbacks (tx2 .TxHash ())
213- require .True (s .T (), syncStarted )
214- require .True (s .T (), syncFinished )
215- }
216-
217152func newBalance (available , incoming btcutil.Amount ) * accounts.Balance {
218153 return accounts .NewBalance (
219154 coin .NewAmountFromInt64 (int64 (available )),
@@ -253,35 +188,6 @@ func (s *transactionsSuite) TestUpdateAddressHistorySingleTxReceive() {
253188 require .Equal (s .T (), expectedHeight , transactions [0 ].Height )
254189}
255190
256- // TestUpdateAddressHistoryOppositeOrder checks that a spend is correctly recognized even if the
257- // transactions in the history of an address are processed in the wrong order. If the spending tx is
258- // processed before the funding tx, the output is unknown when processing the funds, but after the
259- // output has been added, the input spending it needs to be indexed correctly.
260- func (s * transactionsSuite ) TestUpdateAddressHistoryOppositeOrder () {
261- addresses := s .addressChain .EnsureAddresses ()
262- address := addresses [0 ]
263- address2 := addresses [1 ]
264- tx1 := newTx (chainhash .HashH (nil ), 0 , address , 123 )
265- tx1Hash := tx1 .TxHash ()
266- tx2 := newTx (tx1 .TxHash (), 0 , address2 , 123 )
267- tx2Hash := tx2 .TxHash ()
268- s .blockchainMock .RegisterTxs (tx1 , tx2 )
269- s .notifierMock .On ("Put" , tx1Hash [:]).Return (nil ).Once ()
270- s .notifierMock .On ("Put" , tx2Hash [:]).Return (nil ).Once ()
271- s .transactions .UpdateAddressHistory (address .PubkeyScriptHashHex (), []* blockchainpkg.TxInfo {
272- {TXHash : blockchainpkg .TXHash (tx1Hash ), Height : 0 },
273- {TXHash : blockchainpkg .TXHash (tx2Hash ), Height : 0 },
274- })
275- // Process tx2 (the spend) before tx1 (the funding). This should result in a zero balance, as
276- // the received funds are spent.
277- s .blockchainMock .CallTransactionGetCallbacks (tx2 .TxHash ())
278- s .blockchainMock .CallTransactionGetCallbacks (tx1 .TxHash ())
279- require .Equal (s .T (),
280- newBalance (0 , 0 ),
281- s .transactions .Balance (),
282- )
283- }
284-
285191// TestSpendableOutputs checks that the utxo set is correct. Only confirmed (or unconfirmed outputs
286192// we own) outputs can be spent.
287193func (s * transactionsSuite ) TestSpendableOutputs () {
@@ -453,23 +359,3 @@ func (s *transactionsSuite) TestRemoveTransaction() {
453359 s .transactions .Transactions (func (blockchainpkg.ScriptHashHex ) bool { return false }),
454360 2 )
455361}
456-
457- // TestRemoveTransactionPendingDownload tests that a tx can be removed from the address history
458- // while it is still pending to be indexed.
459- func (s * transactionsSuite ) TestRemoveTransactionPendingDownload () {
460- address := s .addressChain .EnsureAddresses ()[0 ]
461- tx := newTx (chainhash .HashH (nil ), 0 , address , 123 )
462- s .blockchainMock .RegisterTxs (tx )
463- s .transactions .UpdateAddressHistory (address .PubkeyScriptHashHex (), []* blockchainpkg.TxInfo {
464- {TXHash : blockchainpkg .TXHash (tx .TxHash ()), Height : 0 },
465- })
466- // Callback for processing the tx is not called yet. We remove the tx.
467- s .transactions .UpdateAddressHistory (address .PubkeyScriptHashHex (), []* blockchainpkg.TxInfo {})
468- // Process the tx now. It should not be indexed anymore.
469- s .blockchainMock .CallAllTransactionGetCallbacks ()
470- require .Equal (s .T (),
471- newBalance (0 , 0 ),
472- s .transactions .Balance ())
473- require .Empty (s .T (),
474- s .transactions .Transactions (func (blockchainpkg.ScriptHashHex ) bool { return false }))
475- }
0 commit comments