github.com/decred/dcrlnd@v0.7.6/lntest/mock/chainnotifier.go (about) 1 package mock 2 3 import ( 4 "sync" 5 6 "github.com/decred/dcrd/chaincfg/chainhash" 7 "github.com/decred/dcrd/wire" 8 "golang.org/x/exp/slices" 9 10 "github.com/decred/dcrlnd/chainntnfs" 11 ) 12 13 // SpendNtnfRequest tracks calls made to the mock notifier RegisterSpendNtfn. 14 // 15 // Note: this exists only in dcrlnd. 16 type SpendNtnfRequest struct { 17 Outpoint wire.OutPoint 18 PkScript []byte 19 } 20 21 // ChainNotifier is a mock implementation of the ChainNotifier interface. 22 type ChainNotifier struct { 23 SpendChan chan *chainntnfs.SpendDetail 24 EpochChan chan *chainntnfs.BlockEpoch 25 ConfChan chan *chainntnfs.TxConfirmation 26 27 mtx sync.Mutex 28 spendNtfns []SpendNtnfRequest 29 } 30 31 // RegisteredSpendNtfns returns registered spend notifications. This is only 32 // part of the mock interface. 33 func (c *ChainNotifier) RegisteredSpendNtfns() []SpendNtnfRequest { 34 c.mtx.Lock() 35 res := slices.Clone(c.spendNtfns) 36 c.mtx.Unlock() 37 return res 38 } 39 40 // RegisterConfirmationsNtfn returns a ConfirmationEvent that contains a channel 41 // that the tx confirmation will go over. 42 func (c *ChainNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash, 43 pkScript []byte, numConfs, heightHint uint32) (*chainntnfs.ConfirmationEvent, 44 error) { 45 46 return &chainntnfs.ConfirmationEvent{ 47 Confirmed: c.ConfChan, 48 Cancel: func() {}, 49 }, nil 50 } 51 52 // RegisterSpendNtfn returns a SpendEvent that contains a channel that the spend 53 // details will go over. 54 func (c *ChainNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint, 55 pkScript []byte, heightHint uint32) (*chainntnfs.SpendEvent, error) { 56 57 c.mtx.Lock() 58 c.spendNtfns = append(c.spendNtfns, SpendNtnfRequest{ 59 Outpoint: *outpoint, 60 PkScript: pkScript, 61 }) 62 c.mtx.Unlock() 63 64 return &chainntnfs.SpendEvent{ 65 Spend: c.SpendChan, 66 Cancel: func() {}, 67 }, nil 68 } 69 70 // RegisterBlockEpochNtfn returns a BlockEpochEvent that contains a channel that 71 // block epochs will go over. 72 func (c *ChainNotifier) RegisterBlockEpochNtfn(blockEpoch *chainntnfs.BlockEpoch) ( 73 *chainntnfs.BlockEpochEvent, error) { 74 75 return &chainntnfs.BlockEpochEvent{ 76 Epochs: c.EpochChan, 77 Cancel: func() {}, 78 }, nil 79 } 80 81 // Start currently returns a dummy value. 82 func (c *ChainNotifier) Start() error { 83 return nil 84 } 85 86 // Started currently returns a dummy value. 87 func (c *ChainNotifier) Started() bool { 88 return true 89 } 90 91 // Stop currently returns a dummy value. 92 func (c *ChainNotifier) Stop() error { 93 return nil 94 }