github.com/decred/dcrlnd@v0.7.6/lnwallet/chanfunding/assembler.go (about)

     1  package chanfunding
     2  
     3  import (
     4  	"github.com/decred/dcrd/dcrutil/v4"
     5  	"github.com/decred/dcrd/txscript/v4/stdaddr"
     6  	"github.com/decred/dcrd/wire"
     7  	"github.com/decred/dcrlnd/lnwallet/chainfee"
     8  )
     9  
    10  // CoinSource is an interface that allows a caller to access a source of UTXOs
    11  // to use when attempting to fund a new channel.
    12  type CoinSource interface {
    13  	// ListCoins returns all UTXOs from the source that have between
    14  	// minConfs and maxConfs number of confirmations.
    15  	ListCoins(minConfs, maxConfs int32) ([]Coin, error)
    16  
    17  	// CoinFromOutPoint attempts to locate details pertaining to a coin
    18  	// based on its outpoint. If the coin isn't under the control of the
    19  	// backing CoinSource, then an error should be returned.
    20  	CoinFromOutPoint(wire.OutPoint) (*Coin, error)
    21  }
    22  
    23  // CoinSelectionLocker is an interface that allows the caller to perform an
    24  // operation, which is synchronized with all coin selection attempts. This can
    25  // be used when an operation requires that all coin selection operations cease
    26  // forward progress. Think of this as an exclusive lock on coin selection
    27  // operations.
    28  type CoinSelectionLocker interface {
    29  	// WithCoinSelectLock will execute the passed function closure in a
    30  	// synchronized manner preventing any coin selection operations from
    31  	// proceeding while the closure is executing. This can be seen as the
    32  	// ability to execute a function closure under an exclusive coin
    33  	// selection lock.
    34  	WithCoinSelectLock(func() error) error
    35  }
    36  
    37  // OutpointLocker allows a caller to lock/unlock an outpoint. When locked, the
    38  // outpoints shouldn't be used for any sort of channel funding of coin
    39  // selection. Locked outpoints are not expected to be persisted between
    40  // restarts.
    41  type OutpointLocker interface {
    42  	// LockOutpoint locks a target outpoint, rendering it unusable for coin
    43  	// selection.
    44  	LockOutpoint(o wire.OutPoint)
    45  
    46  	// UnlockOutpoint unlocks a target outpoint, allowing it to be used for
    47  	// coin selection once again.
    48  	UnlockOutpoint(o wire.OutPoint)
    49  }
    50  
    51  // Request is a new request for funding a channel. The items in the struct
    52  // governs how the final channel point will be provisioned by the target
    53  // Assembler.
    54  type Request struct {
    55  	// LocalAmt is the amount of coins we're placing into the funding
    56  	// output.
    57  	LocalAmt dcrutil.Amount
    58  
    59  	// RemoteAmt is the amount of coins the remote party is contributing to
    60  	// the funding output.
    61  	RemoteAmt dcrutil.Amount
    62  
    63  	// MinConfs controls how many confirmations a coin need to be eligible
    64  	// to be used as an input to the funding transaction. If this value is
    65  	// set to zero, then zero conf outputs may be spent.
    66  	MinConfs int32
    67  
    68  	// SubtractFees should be set if we intend to spend exactly LocalAmt
    69  	// when opening the channel, subtracting the fees from the funding
    70  	// output. This can be used for instance to use all our remaining funds
    71  	// to open the channel, since it will take fees into
    72  	// account.
    73  	SubtractFees bool
    74  
    75  	// FeeRate is the fee rate in sat/kw that the funding transaction
    76  	// should carry.
    77  	FeeRate chainfee.AtomPerKByte
    78  
    79  	// ChangeAddr is a closure that will provide the Assembler with a
    80  	// change address for the funding transaction if needed.
    81  	ChangeAddr func() (stdaddr.Address, error)
    82  }
    83  
    84  // Intent is returned by an Assembler and represents the base functionality the
    85  // caller needs to proceed with channel funding on a higher level. If the
    86  // Cancel method is called, then all resources assembled to fund the channel
    87  // will be released back to the eligible pool.
    88  type Intent interface {
    89  	// FundingOutput returns the witness script, and the output that
    90  	// creates the funding output.
    91  	FundingOutput() ([]byte, *wire.TxOut, error)
    92  
    93  	// ChanPoint returns the final outpoint that will create the funding
    94  	// output described above.
    95  	ChanPoint() (*wire.OutPoint, error)
    96  
    97  	// RemoteFundingAmt is the amount the remote party put into the
    98  	// channel.
    99  	RemoteFundingAmt() dcrutil.Amount
   100  
   101  	// LocalFundingAmt is the amount we put into the channel. This may
   102  	// differ from the local amount requested, as depending on coin
   103  	// selection, we may bleed from of that LocalAmt into fees to minimize
   104  	// change.
   105  	LocalFundingAmt() dcrutil.Amount
   106  
   107  	// Inputs returns all inputs to the final funding transaction that we
   108  	// know about. Note that there might be more, but we are not (yet)
   109  	// aware of.
   110  	Inputs() []wire.OutPoint
   111  
   112  	// Outputs returns all outputs of the final funding transaction that we
   113  	// know about. Note that there might be more, but we are not (yet)
   114  	// aware of.
   115  	Outputs() []*wire.TxOut
   116  
   117  	// Cancel allows the caller to cancel a funding Intent at any time.
   118  	// This will return any resources such as coins back to the eligible
   119  	// pool to be used in order channel fundings.
   120  	Cancel()
   121  }
   122  
   123  // Assembler is an abstract object that is capable of assembling everything
   124  // needed to create a new funding output. As an example, this assembler may be
   125  // our core backing wallet, an interactive PSBT based assembler, an assembler
   126  // than can aggregate multiple intents into a single funding transaction, or an
   127  // external protocol that creates a funding output out-of-band such as channel
   128  // factories.
   129  type Assembler interface {
   130  	// ProvisionChannel returns a populated Intent that can be used to
   131  	// further the channel funding workflow. Depending on the
   132  	// implementation of Assembler, additional state machine (Intent)
   133  	// actions may be required before the FundingOutput and ChanPoint are
   134  	// made available to the caller.
   135  	ProvisionChannel(*Request) (Intent, error)
   136  }
   137  
   138  // FundingTxAssembler is a super-set of the regular Assembler interface that's
   139  // also able to provide a fully populated funding transaction via the intents
   140  // that it produces.
   141  type FundingTxAssembler interface {
   142  	Assembler
   143  
   144  	// FundingTxAvailable is an empty method that an assembler can
   145  	// implement to signal to callers that its able to provide the funding
   146  	// transaction for the channel via the intent it returns.
   147  	FundingTxAvailable()
   148  }
   149  
   150  // ConditionalPublishAssembler is an assembler that can dynamically define if
   151  // the funding transaction should be published after channel negotiations or
   152  // not. Not publishing the transaction is only useful if the particular channel
   153  // the assembler is in charge of is part of a batch of channels. In that case
   154  // it is only safe to wait for all channel negotiations of the batch to complete
   155  // before publishing the batch transaction.
   156  type ConditionalPublishAssembler interface {
   157  	Assembler
   158  
   159  	// ShouldPublishFundingTx is a method of the assembler that signals if
   160  	// the funding transaction should be published after the channel
   161  	// negotiations are completed with the remote peer.
   162  	ShouldPublishFundingTx() bool
   163  }