github.com/decred/dcrlnd@v0.7.6/chainreg/no_chain_backend.go (about)

     1  package chainreg
     2  
     3  import (
     4  	"errors"
     5  	"time"
     6  
     7  	"github.com/decred/dcrd/chaincfg/chainhash"
     8  	"github.com/decred/dcrd/txscript/v4/stdaddr"
     9  	"github.com/decred/dcrd/wire"
    10  	"github.com/decred/dcrlnd/chainntnfs"
    11  	"github.com/decred/dcrlnd/channeldb"
    12  	"github.com/decred/dcrlnd/lnwallet/chainfee"
    13  	"github.com/decred/dcrlnd/routing/chainview"
    14  )
    15  
    16  var (
    17  	// defaultFee is the fee that is returned by NoChainBackend.
    18  	defaultFee = chainfee.FeePerKBFloor
    19  
    20  	// noChainBackendName is the backend name returned by NoChainBackend.
    21  	noChainBackendName = "nochainbackend"
    22  
    23  	// errNotImplemented is the error that is returned by NoChainBackend for
    24  	// any operation that is not supported by it. Such paths should in
    25  	// practice never been hit, so seeing this error either means a remote
    26  	// signing instance was used for an unsupported purpose or a previously
    27  	// forgotten edge case path was hit.
    28  	errNotImplemented = errors.New("not implemented in nochainbackend " +
    29  		"mode")
    30  
    31  	// noChainBackendBestHash is the chain hash of the chain tip that is
    32  	// returned by NoChainBackend.
    33  	noChainBackendBestHash = &chainhash.Hash{0x01}
    34  
    35  	// noChainBackendBestHeight is the best height that is returned by
    36  	// NoChainBackend.
    37  	noChainBackendBestHeight int32 = 1
    38  )
    39  
    40  // NoChainBackend is a mock implementation of the following interfaces:
    41  //   - chainview.FilteredChainView
    42  //   - chainntnfs.ChainNotifier
    43  //   - chainfee.Estimator
    44  type NoChainBackend struct {
    45  }
    46  
    47  func (n *NoChainBackend) EstimateFeePerKB(uint32) (chainfee.AtomPerKByte,
    48  	error) {
    49  
    50  	return defaultFee, nil
    51  }
    52  
    53  func (n *NoChainBackend) RelayFeePerKB() chainfee.AtomPerKByte {
    54  	return defaultFee
    55  }
    56  
    57  func (n *NoChainBackend) RegisterConfirmationsNtfn(*chainhash.Hash, []byte,
    58  	uint32, uint32) (*chainntnfs.ConfirmationEvent, error) {
    59  
    60  	return nil, errNotImplemented
    61  }
    62  
    63  func (n *NoChainBackend) RegisterSpendNtfn(*wire.OutPoint, []byte,
    64  	uint32) (*chainntnfs.SpendEvent, error) {
    65  
    66  	return nil, errNotImplemented
    67  }
    68  
    69  func (n *NoChainBackend) RegisterBlockEpochNtfn(
    70  	*chainntnfs.BlockEpoch) (*chainntnfs.BlockEpochEvent, error) {
    71  
    72  	epochChan := make(chan *chainntnfs.BlockEpoch)
    73  	return &chainntnfs.BlockEpochEvent{
    74  		Epochs: epochChan,
    75  		Cancel: func() {
    76  			close(epochChan)
    77  		},
    78  	}, nil
    79  }
    80  
    81  func (n *NoChainBackend) Started() bool {
    82  	return true
    83  }
    84  
    85  func (n *NoChainBackend) FilteredBlocks() <-chan *chainview.FilteredBlock {
    86  	return make(chan *chainview.FilteredBlock)
    87  }
    88  
    89  func (n *NoChainBackend) DisconnectedBlocks() <-chan *chainview.FilteredBlock {
    90  	return make(chan *chainview.FilteredBlock)
    91  }
    92  
    93  func (n *NoChainBackend) UpdateFilter([]channeldb.EdgePoint, int64) error {
    94  	return nil
    95  }
    96  
    97  func (n *NoChainBackend) FilterBlock(*chainhash.Hash) (*chainview.FilteredBlock,
    98  	error) {
    99  
   100  	return nil, errNotImplemented
   101  }
   102  
   103  func (n *NoChainBackend) Start() error {
   104  	return nil
   105  }
   106  
   107  func (n *NoChainBackend) Stop() error {
   108  	return nil
   109  }
   110  
   111  var _ chainview.FilteredChainView = (*NoChainBackend)(nil)
   112  var _ chainntnfs.ChainNotifier = (*NoChainBackend)(nil)
   113  var _ chainfee.Estimator = (*NoChainBackend)(nil)
   114  
   115  // NoChainSource is a mock implementation of chain.Interface.
   116  // The mock is designed to return static values where necessary to make any
   117  // caller believe the chain is fully synced to virtual block height 1 (hash
   118  // 0x0000..0001). That should avoid calls to other methods completely since they
   119  // are only used for advancing the chain forward.
   120  type NoChainSource struct {
   121  	notifChan chan interface{}
   122  
   123  	BestBlockTime time.Time
   124  }
   125  
   126  func (n *NoChainSource) Start() error {
   127  	n.notifChan = make(chan interface{})
   128  
   129  	return nil
   130  }
   131  
   132  func (n *NoChainSource) Stop() {
   133  }
   134  
   135  func (n *NoChainSource) WaitForShutdown() {
   136  }
   137  
   138  func (n *NoChainSource) GetBestBlock() (*chainhash.Hash, int32, error) {
   139  	return noChainBackendBestHash, noChainBackendBestHeight, nil
   140  }
   141  
   142  func (n *NoChainSource) GetBlock(*chainhash.Hash) (*wire.MsgBlock, error) {
   143  	return &wire.MsgBlock{
   144  		Header: wire.BlockHeader{
   145  			Timestamp: n.BestBlockTime,
   146  		},
   147  		Transactions: []*wire.MsgTx{},
   148  	}, nil
   149  }
   150  
   151  func (n *NoChainSource) GetBlockHash(int64) (*chainhash.Hash, error) {
   152  	return noChainBackendBestHash, nil
   153  }
   154  
   155  func (n *NoChainSource) GetBlockHeader(*chainhash.Hash) (*wire.BlockHeader,
   156  	error) {
   157  
   158  	return &wire.BlockHeader{
   159  		Timestamp: n.BestBlockTime,
   160  	}, nil
   161  }
   162  
   163  func (n *NoChainSource) IsCurrent() bool {
   164  	return true
   165  }
   166  
   167  func (n *NoChainSource) SendRawTransaction(*wire.MsgTx, bool) (*chainhash.Hash,
   168  	error) {
   169  
   170  	return nil, errNotImplemented
   171  }
   172  
   173  func (n *NoChainSource) Rescan(*chainhash.Hash, []stdaddr.Address,
   174  	map[wire.OutPoint]stdaddr.Address) error {
   175  
   176  	return nil
   177  }
   178  
   179  func (n *NoChainSource) NotifyReceived([]stdaddr.Address) error {
   180  	return nil
   181  }
   182  
   183  func (n *NoChainSource) NotifyBlocks() error {
   184  	return nil
   185  }
   186  
   187  func (n *NoChainSource) Notifications() <-chan interface{} {
   188  	return n.notifChan
   189  }
   190  
   191  func (n *NoChainSource) BackEnd() string {
   192  	return noChainBackendName
   193  }