/ chanacceptor / interface.go
interface.go
  1  package chanacceptor
  2  
  3  import (
  4  	"errors"
  5  
  6  	"github.com/btcsuite/btcd/btcec/v2"
  7  	"github.com/btcsuite/btcd/btcutil"
  8  	"github.com/lightningnetwork/lnd/lnwire"
  9  )
 10  
 11  var (
 12  	// errChannelRejected is returned when the rpc channel acceptor rejects
 13  	// a channel due to acceptor timeout, shutdown, or because no custom
 14  	// error value is available when the channel was rejected.
 15  	errChannelRejected = errors.New("channel rejected")
 16  )
 17  
 18  // ChannelAcceptRequest is a struct containing the requesting node's public key
 19  // along with the lnwire.OpenChannel message that they sent when requesting an
 20  // inbound channel. This information is provided to each acceptor so that they
 21  // can each leverage their own decision-making with this information.
 22  type ChannelAcceptRequest struct {
 23  	// Node is the public key of the node requesting to open a channel.
 24  	Node *btcec.PublicKey
 25  
 26  	// OpenChanMsg is the actual OpenChannel protocol message that the peer
 27  	// sent to us.
 28  	OpenChanMsg *lnwire.OpenChannel
 29  }
 30  
 31  // ChannelAcceptResponse is a struct containing the response to a request to
 32  // open an inbound channel. Note that fields added to this struct must be added
 33  // to the mergeResponse function to allow combining of responses from different
 34  // acceptors.
 35  type ChannelAcceptResponse struct {
 36  	// ChanAcceptError the error returned by the channel acceptor. If the
 37  	// channel was accepted, this value will be nil.
 38  	ChanAcceptError
 39  
 40  	// UpfrontShutdown is the address that we will set as our upfront
 41  	// shutdown address.
 42  	UpfrontShutdown lnwire.DeliveryAddress
 43  
 44  	// CSVDelay is the csv delay we require for the remote peer.
 45  	CSVDelay uint16
 46  
 47  	// Reserve is the amount that require the remote peer hold in reserve
 48  	// on the channel.
 49  	Reserve btcutil.Amount
 50  
 51  	// InFlightTotal is the maximum amount that we allow the remote peer to
 52  	// hold in outstanding htlcs.
 53  	InFlightTotal lnwire.MilliSatoshi
 54  
 55  	// HtlcLimit is the maximum number of htlcs that we allow the remote
 56  	// peer to offer us.
 57  	HtlcLimit uint16
 58  
 59  	// MinHtlcIn is the minimum incoming htlc value allowed on the channel.
 60  	MinHtlcIn lnwire.MilliSatoshi
 61  
 62  	// MinAcceptDepth is the minimum depth that the initiator of the
 63  	// channel should wait before considering the channel open.
 64  	MinAcceptDepth uint16
 65  
 66  	// ZeroConf indicates that the fundee wishes to send min_depth = 0 and
 67  	// request a zero-conf channel with the counter-party.
 68  	ZeroConf bool
 69  }
 70  
 71  // NewChannelAcceptResponse is a constructor for a channel accept response,
 72  // which creates a response with an appropriately wrapped error (in the case of
 73  // a rejection) so that the error will be whitelisted and delivered to the
 74  // initiating peer. Accepted channels simply return a response containing a nil
 75  // error.
 76  func NewChannelAcceptResponse(accept bool, acceptErr error,
 77  	upfrontShutdown lnwire.DeliveryAddress, csvDelay, htlcLimit,
 78  	minDepth uint16, reserve btcutil.Amount, inFlight,
 79  	minHtlcIn lnwire.MilliSatoshi, zeroConf bool) *ChannelAcceptResponse {
 80  
 81  	resp := &ChannelAcceptResponse{
 82  		UpfrontShutdown: upfrontShutdown,
 83  		CSVDelay:        csvDelay,
 84  		Reserve:         reserve,
 85  		InFlightTotal:   inFlight,
 86  		HtlcLimit:       htlcLimit,
 87  		MinHtlcIn:       minHtlcIn,
 88  		MinAcceptDepth:  minDepth,
 89  		ZeroConf:        zeroConf,
 90  	}
 91  
 92  	// If we want to accept the channel, we return a response with a nil
 93  	// error.
 94  	if accept {
 95  		return resp
 96  	}
 97  
 98  	// Use a generic error when no custom error is provided.
 99  	if acceptErr == nil {
100  		acceptErr = errChannelRejected
101  	}
102  
103  	resp.ChanAcceptError = ChanAcceptError{
104  		error: acceptErr,
105  	}
106  
107  	return resp
108  }
109  
110  // RejectChannel returns a boolean that indicates whether we should reject the
111  // channel.
112  func (c *ChannelAcceptResponse) RejectChannel() bool {
113  	return c.error != nil
114  }
115  
116  // ChannelAcceptor is an interface that represents  a predicate on the data
117  // contained in ChannelAcceptRequest.
118  type ChannelAcceptor interface {
119  	Accept(req *ChannelAcceptRequest) *ChannelAcceptResponse
120  }
121  
122  // MultiplexAcceptor is an interface that abstracts the ability of a
123  // ChannelAcceptor to contain sub-ChannelAcceptors.
124  type MultiplexAcceptor interface {
125  	// Embed the ChannelAcceptor.
126  	ChannelAcceptor
127  
128  	// AddAcceptor nests a ChannelAcceptor inside the MultiplexAcceptor.
129  	AddAcceptor(acceptor ChannelAcceptor) uint64
130  
131  	// Remove a sub-ChannelAcceptor.
132  	RemoveAcceptor(id uint64)
133  }