@@ -539,6 +539,86 @@ func TestTransactionHandling(t *testing.T) {
539539 require .EqualValues (t , mockProof .Blob , dbProof )
540540}
541541
542+ // TestTransactionConfirmedOnly tests that the custodian only starts the proof
543+ // courier once a transaction has been confirmed. We also test that it correctly
544+ // re-tries fetching proofs using a proof courier after it has been restarted.
545+ func TestTransactionConfirmedOnly (t * testing.T ) {
546+ t .Parallel ()
547+
548+ runTransactionConfirmedOnlyTest (t , false )
549+ runTransactionConfirmedOnlyTest (t , true )
550+ }
551+
552+ // runTransactionConfirmedOnlyTest runs the transaction confirmed only test,
553+ // optionally restarting the custodian in the middle.
554+ func runTransactionConfirmedOnlyTest (t * testing.T , withRestart bool ) {
555+ h := newHarness (t , nil )
556+
557+ // Before we start the custodian, we create a few random addresses.
558+ ctx := context .Background ()
559+
560+ const numAddrs = 5
561+ addrs := make ([]* address.AddrWithKeyInfo , numAddrs )
562+ genesis := make ([]* asset.Genesis , numAddrs )
563+ for i := 0 ; i < numAddrs ; i ++ {
564+ addrs [i ], genesis [i ] = randAddr (h )
565+ err := h .tapdbBook .InsertAddrs (ctx , * addrs [i ])
566+ require .NoError (t , err )
567+ }
568+
569+ // We start the custodian and make sure it's started up correctly. This
570+ // should add pending events for each of the addresses.
571+ require .NoError (t , h .c .Start ())
572+ t .Cleanup (func () {
573+ require .NoError (t , h .c .Stop ())
574+ })
575+ h .assertStartup ()
576+
577+ // We expect all addresses to be watched by the wallet now.
578+ h .assertAddrsRegistered (addrs ... )
579+
580+ // To make sure the custodian adds address events for each address, we
581+ // need to signal an unconfirmed transaction for each of them now.
582+ outputIndexes := make ([]int , numAddrs )
583+ transactions := make ([]* lndclient.Transaction , numAddrs )
584+ for idx := range addrs {
585+ outputIndex , tx := randWalletTx (addrs [idx ])
586+ outputIndexes [idx ] = outputIndex
587+ transactions [idx ] = tx
588+ h .walletAnchor .SubscribeTx <- * tx
589+
590+ // We also simulate that the proof courier has all the proofs
591+ // it needs.
592+ mockProof := randProof (
593+ t , outputIndexes [idx ], tx .Tx , genesis [idx ], addrs [idx ],
594+ )
595+ _ = h .courier .DeliverProof (nil , mockProof )
596+ }
597+
598+ // We want events to be created for each address, they should be in the
599+ // state where they detected a transaction.
600+ h .assertEventsPresent (numAddrs , address .StatusTransactionDetected )
601+
602+ // In case we're testing with a restart, we now restart the custodian.
603+ if withRestart {
604+ require .NoError (t , h .c .Stop ())
605+
606+ h .c = tapgarden .NewCustodian (h .cfg )
607+ require .NoError (t , h .c .Start ())
608+ h .assertStartup ()
609+ }
610+
611+ // Now we confirm the transactions. This should trigger the custodian to
612+ // fetch the proof for each of the addresses.
613+ for idx := range transactions {
614+ tx := transactions [idx ]
615+ tx .Confirmations = 1
616+ h .walletAnchor .SubscribeTx <- * tx
617+ }
618+
619+ h .assertEventsPresent (numAddrs , address .StatusCompleted )
620+ }
621+
542622func mustMakeAddr (t * testing.T ,
543623 gen asset.Genesis , groupKey * btcec.PublicKey ,
544624 groupWitness wire.TxWitness , scriptKey btcec.PublicKey ) * address.Tap {
0 commit comments