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