github.com/decred/dcrlnd@v0.7.6/chainntnfs/dcrdnotify/driver.go (about)

     1  package dcrdnotify
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/decred/dcrd/chaincfg/v3"
     8  	"github.com/decred/dcrd/rpcclient/v8"
     9  	"github.com/decred/dcrlnd/blockcache"
    10  	"github.com/decred/dcrlnd/chainntnfs"
    11  )
    12  
    13  // createNewNotifier creates a new instance of the ChainNotifier interface
    14  // implemented by DcrdNotifier.
    15  func createNewNotifier(args ...interface{}) (chainntnfs.ChainNotifier, error) {
    16  	if len(args) != 5 {
    17  		return nil, fmt.Errorf("incorrect number of arguments to "+
    18  			".New(...), expected 5, instead passed %v", len(args))
    19  	}
    20  
    21  	config, ok := args[0].(*rpcclient.ConnConfig)
    22  	if !ok {
    23  		return nil, errors.New("first argument to dcrdnotifier.New " +
    24  			"is incorrect, expected a *rpcclient.ConnConfig")
    25  	}
    26  
    27  	chainParams, ok := args[1].(*chaincfg.Params)
    28  	if !ok {
    29  		return nil, errors.New("second argument to dcrdnotifier.New " +
    30  			"is incorrect, expected a *chaincfg.Params")
    31  	}
    32  
    33  	spendHintCache, ok := args[2].(chainntnfs.SpendHintCache)
    34  	if !ok {
    35  		return nil, errors.New("third argument to dcrdnotifier.New " +
    36  			"is incorrect, expected a chainntnfs.SpendHintCache")
    37  	}
    38  
    39  	confirmHintCache, ok := args[3].(chainntnfs.ConfirmHintCache)
    40  	if !ok {
    41  		return nil, errors.New("third argument to dcrdnotifier.New " +
    42  			"is incorrect, expected a chainntnfs.ConfirmHintCache")
    43  	}
    44  
    45  	blockCache, ok := args[4].(*blockcache.BlockCache)
    46  	if !ok {
    47  		return nil, errors.New("third argument to dcrdnotifier.New " +
    48  			"is incorrect, expected a chainntnfs.ConfirmHintCache")
    49  	}
    50  
    51  	return New(config, chainParams, spendHintCache, confirmHintCache, blockCache)
    52  }
    53  
    54  // init registers a driver for the DcrdNotifier concrete implementation of the
    55  // chainntnfs.ChainNotifier interface.
    56  func init() {
    57  	// Register the driver.
    58  	notifier := &chainntnfs.NotifierDriver{
    59  		NotifierType: notifierType,
    60  		New:          createNewNotifier,
    61  	}
    62  
    63  	if err := chainntnfs.RegisterNotifier(notifier); err != nil {
    64  		panic(fmt.Sprintf("failed to register notifier driver '%s': %v",
    65  			notifierType, err))
    66  	}
    67  }