github.com/decred/dcrlnd@v0.7.6/sweep/store_mock.go (about)

     1  package sweep
     2  
     3  import (
     4  	"github.com/decred/dcrd/chaincfg/chainhash"
     5  	"github.com/decred/dcrd/wire"
     6  )
     7  
     8  // MockSweeperStore is a mock implementation of sweeper store. This type is
     9  // exported, because it is currently used in nursery tests too.
    10  type MockSweeperStore struct {
    11  	lastTx  *wire.MsgTx
    12  	ourTxes map[chainhash.Hash]struct{}
    13  }
    14  
    15  // NewMockSweeperStore returns a new instance.
    16  func NewMockSweeperStore() *MockSweeperStore {
    17  	return &MockSweeperStore{
    18  		ourTxes: make(map[chainhash.Hash]struct{}),
    19  	}
    20  }
    21  
    22  // IsOurTx determines whether a tx is published by us, based on its
    23  // hash.
    24  func (s *MockSweeperStore) IsOurTx(hash chainhash.Hash) (bool, error) {
    25  	_, ok := s.ourTxes[hash]
    26  	return ok, nil
    27  }
    28  
    29  // NotifyPublishTx signals that we are about to publish a tx.
    30  func (s *MockSweeperStore) NotifyPublishTx(tx *wire.MsgTx) error {
    31  	if tx == nil {
    32  		return nil
    33  	}
    34  
    35  	txHash := tx.TxHash()
    36  	s.ourTxes[txHash] = struct{}{}
    37  	s.lastTx = tx
    38  
    39  	return nil
    40  }
    41  
    42  // GetLastPublishedTx returns the last tx that we called NotifyPublishTx
    43  // for.
    44  func (s *MockSweeperStore) GetLastPublishedTx() (*wire.MsgTx, error) {
    45  	return s.lastTx, nil
    46  }
    47  
    48  // ListSweeps lists all the sweeps we have successfully published.
    49  func (s *MockSweeperStore) ListSweeps() ([]chainhash.Hash, error) {
    50  	var txns []chainhash.Hash
    51  	for tx := range s.ourTxes {
    52  		txns = append(txns, tx)
    53  	}
    54  
    55  	return txns, nil
    56  }
    57  
    58  // Compile-time constraint to ensure MockSweeperStore implements SweeperStore.
    59  var _ SweeperStore = (*MockSweeperStore)(nil)