github.com/decred/dcrlnd@v0.7.6/chainntnfs/remotedcrwnotify/remotedcrw.go (about)

     1  package remotedcrwnotify
     2  
     3  import (
     4  	"context"
     5  
     6  	"decred.org/dcrwallet/v4/rpc/walletrpc"
     7  	"github.com/decred/dcrd/chaincfg/chainhash"
     8  	"github.com/decred/dcrd/chaincfg/v3"
     9  	"github.com/decred/dcrd/wire"
    10  	"github.com/decred/dcrlnd/blockcache"
    11  	"github.com/decred/dcrlnd/chainntnfs"
    12  	csnotify "github.com/decred/dcrlnd/chainntnfs/chainscannotify"
    13  	"github.com/decred/dcrlnd/chainscan/csdrivers"
    14  	"google.golang.org/grpc"
    15  )
    16  
    17  type RemoteWalletChainSource struct {
    18  	*csdrivers.RemoteWalletCSDriver
    19  	wsvc walletrpc.WalletServiceClient
    20  }
    21  
    22  func (s *RemoteWalletChainSource) GetBlockHash(ctx context.Context, height int32) (*chainhash.Hash, error) {
    23  	req := &walletrpc.BlockInfoRequest{
    24  		BlockHeight: height,
    25  	}
    26  	resp, err := s.wsvc.BlockInfo(ctx, req)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	return chainhash.NewHash(resp.BlockHash)
    31  }
    32  
    33  func (s *RemoteWalletChainSource) GetBlockHeader(ctx context.Context, hash *chainhash.Hash) (*wire.BlockHeader, error) {
    34  	req := &walletrpc.BlockInfoRequest{
    35  		BlockHash: hash[:],
    36  	}
    37  	resp, err := s.wsvc.BlockInfo(ctx, req)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  
    42  	var header wire.BlockHeader
    43  	err = header.FromBytes(resp.BlockHeader)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	return &header, err
    49  }
    50  
    51  func (s *RemoteWalletChainSource) StoresReorgedHeaders() bool {
    52  	return true
    53  }
    54  
    55  func NewRemoteWalletChainSource(conn *grpc.ClientConn, bCache *blockcache.BlockCache) *RemoteWalletChainSource {
    56  	wsvc := walletrpc.NewWalletServiceClient(conn)
    57  	nsvc := walletrpc.NewNetworkServiceClient(conn)
    58  	cs := csdrivers.NewRemoteWalletCSDriver(wsvc, nsvc, bCache)
    59  	return &RemoteWalletChainSource{
    60  		wsvc:                 wsvc,
    61  		RemoteWalletCSDriver: cs,
    62  	}
    63  }
    64  
    65  var _ csnotify.ChainSource = (*RemoteWalletChainSource)(nil)
    66  
    67  // New returns a new DcrdNotifier instance. This function assumes the dcrd node
    68  // detailed in the passed configuration is already running, and willing to
    69  // accept new websockets clients.
    70  func New(conn *grpc.ClientConn, chainParams *chaincfg.Params,
    71  	spendHintCache chainntnfs.SpendHintCache,
    72  	confirmHintCache chainntnfs.ConfirmHintCache,
    73  	blockCache *blockcache.BlockCache) (*csnotify.ChainscanNotifier, error) {
    74  
    75  	src := NewRemoteWalletChainSource(conn, blockCache)
    76  	return csnotify.New(src, chainParams, spendHintCache, confirmHintCache)
    77  }