decred.org/dcrwallet/v3@v3.1.0/wallet/network.go (about)

     1  // Copyright (c) 2017-2019 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package wallet
     6  
     7  import (
     8  	"context"
     9  
    10  	"decred.org/dcrwallet/v3/errors"
    11  	"github.com/decred/dcrd/chaincfg/chainhash"
    12  	"github.com/decred/dcrd/dcrutil/v4"
    13  	"github.com/decred/dcrd/gcs/v4"
    14  	"github.com/decred/dcrd/txscript/v4/stdaddr"
    15  	"github.com/decred/dcrd/wire"
    16  )
    17  
    18  // FilterProof specifies cfilterv2 data of an individual block during a
    19  // Peer.CFiltersV2 call.
    20  //
    21  // Note: This is a type alias of an anonymous struct rather than a regular
    22  // struct due to the packages that fulfill the Peer interface having a
    23  // dependency graph (spv -> wallet -> rpc/client/dcrd) that prevents directly
    24  // returning a struct.
    25  type FilterProof = struct {
    26  	Filter     *gcs.FilterV2
    27  	ProofIndex uint32
    28  	Proof      []chainhash.Hash
    29  }
    30  
    31  // Peer provides wallets with a subset of Decred network functionality available
    32  // to a single peer.
    33  type Peer interface {
    34  	Blocks(ctx context.Context, blockHashes []*chainhash.Hash) ([]*wire.MsgBlock, error)
    35  	CFiltersV2(ctx context.Context, blockHashes []*chainhash.Hash) ([]FilterProof, error)
    36  	Headers(ctx context.Context, blockLocators []*chainhash.Hash, hashStop *chainhash.Hash) ([]*wire.BlockHeader, error)
    37  	PublishTransactions(ctx context.Context, txs ...*wire.MsgTx) error
    38  }
    39  
    40  // NetworkBackend provides wallets with Decred network functionality.  Some
    41  // wallet operations require the wallet to be associated with a network backend
    42  // to complete.
    43  //
    44  // NetworkBackend expands on the Peer interface to provide additional
    45  // functionality for rescanning and filtering.
    46  type NetworkBackend interface {
    47  	Peer
    48  	LoadTxFilter(ctx context.Context, reload bool, addrs []stdaddr.Address, outpoints []wire.OutPoint) error
    49  	Rescan(ctx context.Context, blocks []chainhash.Hash, save func(block *chainhash.Hash, txs []*wire.MsgTx) error) error
    50  
    51  	// This is impossible to determine over the wire protocol, and will always
    52  	// error.  Use Wallet.NextStakeDifficulty to calculate the next ticket price
    53  	// when the DCP0001 deployment is known to be active.
    54  	StakeDifficulty(ctx context.Context) (dcrutil.Amount, error)
    55  }
    56  
    57  // NetworkBackend returns the currently associated network backend of the
    58  // wallet, or an error if the no backend is currently set.
    59  func (w *Wallet) NetworkBackend() (NetworkBackend, error) {
    60  	const op errors.Op = "wallet.NetworkBackend"
    61  
    62  	w.networkBackendMu.Lock()
    63  	n := w.networkBackend
    64  	w.networkBackendMu.Unlock()
    65  	if n == nil {
    66  		return nil, errors.E(op, errors.NoPeers)
    67  	}
    68  	return n, nil
    69  }
    70  
    71  // SetNetworkBackend sets the network backend used by various functions of the
    72  // wallet.
    73  func (w *Wallet) SetNetworkBackend(n NetworkBackend) {
    74  	w.networkBackendMu.Lock()
    75  	w.networkBackend = n
    76  	w.networkBackendMu.Unlock()
    77  }
    78  
    79  // Caller provides a client interface to perform remote procedure calls.
    80  // Serialization and calling conventions are implementation-specific.
    81  type Caller interface {
    82  	// Call performs the remote procedure call defined by method and
    83  	// waits for a response or a broken client connection.
    84  	// Args provides positional parameters for the call.
    85  	// Res must be a pointer to a struct, slice, or map type to unmarshal
    86  	// a result (if any), or nil if no result is needed.
    87  	Call(ctx context.Context, method string, res interface{}, args ...interface{}) error
    88  }