github.com/decred/dcrlnd@v0.7.6/invoices/resolution.go (about)

     1  package invoices
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/decred/dcrlnd/channeldb"
     7  	"github.com/decred/dcrlnd/lntypes"
     8  )
     9  
    10  // HtlcResolution describes how an htlc should be resolved.
    11  type HtlcResolution interface {
    12  	// CircuitKey returns the circuit key for the htlc that we have a
    13  	// resolution for.
    14  	CircuitKey() channeldb.CircuitKey
    15  }
    16  
    17  // HtlcFailResolution is an implementation of the HtlcResolution interface
    18  // which is returned when a htlc is failed.
    19  type HtlcFailResolution struct {
    20  	// circuitKey is the key of the htlc for which we have a resolution.
    21  	circuitKey channeldb.CircuitKey
    22  
    23  	// AcceptHeight is the original height at which the htlc was accepted.
    24  	AcceptHeight int32
    25  
    26  	// Outcome indicates the outcome of the invoice registry update.
    27  	Outcome FailResolutionResult
    28  }
    29  
    30  // NewFailResolution returns a htlc failure resolution.
    31  func NewFailResolution(key channeldb.CircuitKey,
    32  	acceptHeight int32, outcome FailResolutionResult) *HtlcFailResolution {
    33  
    34  	return &HtlcFailResolution{
    35  		circuitKey:   key,
    36  		AcceptHeight: acceptHeight,
    37  		Outcome:      outcome,
    38  	}
    39  }
    40  
    41  // CircuitKey returns the circuit key for the htlc that we have a
    42  // resolution for.
    43  //
    44  // Note: it is part of the HtlcResolution interface.
    45  func (f *HtlcFailResolution) CircuitKey() channeldb.CircuitKey {
    46  	return f.circuitKey
    47  }
    48  
    49  // HtlcSettleResolution is an implementation of the HtlcResolution interface
    50  // which is returned when a htlc is settled.
    51  type HtlcSettleResolution struct {
    52  	// Preimage is the htlc preimage. Its value is nil in case of a cancel.
    53  	Preimage lntypes.Preimage
    54  
    55  	// circuitKey is the key of the htlc for which we have a resolution.
    56  	circuitKey channeldb.CircuitKey
    57  
    58  	// acceptHeight is the original height at which the htlc was accepted.
    59  	AcceptHeight int32
    60  
    61  	// Outcome indicates the outcome of the invoice registry update.
    62  	Outcome SettleResolutionResult
    63  }
    64  
    65  // NewSettleResolution returns a htlc resolution which is associated with a
    66  // settle.
    67  func NewSettleResolution(preimage lntypes.Preimage,
    68  	key channeldb.CircuitKey, acceptHeight int32,
    69  	outcome SettleResolutionResult) *HtlcSettleResolution {
    70  
    71  	return &HtlcSettleResolution{
    72  		Preimage:     preimage,
    73  		circuitKey:   key,
    74  		AcceptHeight: acceptHeight,
    75  		Outcome:      outcome,
    76  	}
    77  }
    78  
    79  // CircuitKey returns the circuit key for the htlc that we have a
    80  // resolution for.
    81  //
    82  // Note: it is part of the HtlcResolution interface.
    83  func (s *HtlcSettleResolution) CircuitKey() channeldb.CircuitKey {
    84  	return s.circuitKey
    85  }
    86  
    87  // htlcAcceptResolution is an implementation of the HtlcResolution interface
    88  // which is returned when a htlc is accepted. This struct is not exported
    89  // because the codebase uses a nil resolution to indicate that a htlc was
    90  // accepted. This struct is used internally in the invoice registry to
    91  // surface accept resolution results. When an invoice update returns an
    92  // acceptResolution, a nil resolution should be surfaced.
    93  type htlcAcceptResolution struct {
    94  	// circuitKey is the key of the htlc for which we have a resolution.
    95  	circuitKey channeldb.CircuitKey
    96  
    97  	// autoRelease signals that the htlc should be automatically released
    98  	// after a timeout.
    99  	autoRelease bool
   100  
   101  	// acceptTime is the time at which this htlc was accepted.
   102  	acceptTime time.Time
   103  
   104  	// outcome indicates the outcome of the invoice registry update.
   105  	outcome acceptResolutionResult
   106  }
   107  
   108  // newAcceptResolution returns a htlc resolution which is associated with a
   109  // htlc accept.
   110  func newAcceptResolution(key channeldb.CircuitKey,
   111  	outcome acceptResolutionResult) *htlcAcceptResolution {
   112  
   113  	return &htlcAcceptResolution{
   114  		circuitKey: key,
   115  		outcome:    outcome,
   116  	}
   117  }
   118  
   119  // CircuitKey returns the circuit key for the htlc that we have a
   120  // resolution for.
   121  //
   122  // Note: it is part of the HtlcResolution interface.
   123  func (a *htlcAcceptResolution) CircuitKey() channeldb.CircuitKey {
   124  	return a.circuitKey
   125  }