invoices+contractcourt: add HtlcResolution constructor
This commit adds a constructor for HtlcResolution creation to enforce provision of all relevant values when an event is created. A custom construstor which also takes a preimage is added for settle events.
This commit is contained in:
2 files changed
+67
-51
No files matched your search
@@ -21,10 +21,11 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
testResPreimage = lntypes.Preimage{1, 2, 3}
|
testResPreimage = lntypes.Preimage{1, 2, 3}
|
||||||
testResHash = testResPreimage.Hash()
|
testResHash = testResPreimage.Hash()
|
||||||
testResCircuitKey = channeldb.CircuitKey{}
|
testResCircuitKey = channeldb.CircuitKey{}
|
||||||
testOnionBlob = []byte{4, 5, 6}
|
testOnionBlob = []byte{4, 5, 6}
|
||||||
|
testAcceptHeight int32 = 1234
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestHtlcIncomingResolverFwdPreimageKnown tests resolution of a forwarded htlc
|
// TestHtlcIncomingResolverFwdPreimageKnown tests resolution of a forwarded htlc
|
||||||
@@ -96,10 +97,10 @@ func TestHtlcIncomingResolverExitSettle(t *testing.T) {
|
|||||||
defer timeout(t)()
|
defer timeout(t)()
|
||||||
|
|
||||||
ctx := newIncomingResolverTestContext(t)
|
ctx := newIncomingResolverTestContext(t)
|
||||||
ctx.registry.notifyResolution = &invoices.HtlcResolution{
|
ctx.registry.notifyResolution = invoices.NewSettleResolution(
|
||||||
CircuitKey: testResCircuitKey,
|
testResPreimage, testResCircuitKey, testAcceptHeight,
|
||||||
Preimage: &testResPreimage,
|
)
|
||||||
}
|
|
||||||
ctx.resolve()
|
ctx.resolve()
|
||||||
|
|
||||||
data := <-ctx.registry.notifyChan
|
data := <-ctx.registry.notifyChan
|
||||||
@@ -126,9 +127,10 @@ func TestHtlcIncomingResolverExitCancel(t *testing.T) {
|
|||||||
defer timeout(t)()
|
defer timeout(t)()
|
||||||
|
|
||||||
ctx := newIncomingResolverTestContext(t)
|
ctx := newIncomingResolverTestContext(t)
|
||||||
ctx.registry.notifyResolution = &invoices.HtlcResolution{
|
ctx.registry.notifyResolution = invoices.NewFailureResolution(
|
||||||
CircuitKey: testResCircuitKey,
|
testResCircuitKey, testAcceptHeight,
|
||||||
}
|
)
|
||||||
|
|
||||||
ctx.resolve()
|
ctx.resolve()
|
||||||
ctx.waitForResult(false)
|
ctx.waitForResult(false)
|
||||||
}
|
}
|
||||||
@@ -143,10 +145,9 @@ func TestHtlcIncomingResolverExitSettleHodl(t *testing.T) {
|
|||||||
ctx.resolve()
|
ctx.resolve()
|
||||||
|
|
||||||
notifyData := <-ctx.registry.notifyChan
|
notifyData := <-ctx.registry.notifyChan
|
||||||
notifyData.hodlChan <- invoices.HtlcResolution{
|
notifyData.hodlChan <- *invoices.NewSettleResolution(
|
||||||
CircuitKey: testResCircuitKey,
|
testResPreimage, testResCircuitKey, testAcceptHeight,
|
||||||
Preimage: &testResPreimage,
|
)
|
||||||
}
|
|
||||||
|
|
||||||
ctx.waitForResult(true)
|
ctx.waitForResult(true)
|
||||||
}
|
}
|
||||||
@@ -172,9 +173,10 @@ func TestHtlcIncomingResolverExitCancelHodl(t *testing.T) {
|
|||||||
ctx := newIncomingResolverTestContext(t)
|
ctx := newIncomingResolverTestContext(t)
|
||||||
ctx.resolve()
|
ctx.resolve()
|
||||||
notifyData := <-ctx.registry.notifyChan
|
notifyData := <-ctx.registry.notifyChan
|
||||||
notifyData.hodlChan <- invoices.HtlcResolution{
|
notifyData.hodlChan <- *invoices.NewFailureResolution(
|
||||||
CircuitKey: testResCircuitKey,
|
testResCircuitKey, testAcceptHeight,
|
||||||
}
|
)
|
||||||
|
|
||||||
ctx.waitForResult(false)
|
ctx.waitForResult(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+47
-33
@@ -50,6 +50,28 @@ type HtlcResolution struct {
|
|||||||
AcceptHeight int32
|
AcceptHeight int32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewFailureResolution returns a htlc failure resolution.
|
||||||
|
func NewFailureResolution(key channeldb.CircuitKey,
|
||||||
|
acceptHeight int32) *HtlcResolution {
|
||||||
|
|
||||||
|
return &HtlcResolution{
|
||||||
|
CircuitKey: key,
|
||||||
|
AcceptHeight: acceptHeight,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewSettleResolution returns a htlc resolution which is associated with a
|
||||||
|
// settle.
|
||||||
|
func NewSettleResolution(preimage lntypes.Preimage, key channeldb.CircuitKey,
|
||||||
|
acceptHeight int32) *HtlcResolution {
|
||||||
|
|
||||||
|
return &HtlcResolution{
|
||||||
|
Preimage: &preimage,
|
||||||
|
CircuitKey: key,
|
||||||
|
AcceptHeight: acceptHeight,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// RegistryConfig contains the configuration parameters for invoice registry.
|
// RegistryConfig contains the configuration parameters for invoice registry.
|
||||||
type RegistryConfig struct {
|
type RegistryConfig struct {
|
||||||
// FinalCltvRejectDelta defines the number of blocks before the expiry
|
// FinalCltvRejectDelta defines the number of blocks before the expiry
|
||||||
@@ -652,11 +674,9 @@ func (i *InvoiceRegistry) cancelSingleHtlc(hash lntypes.Hash,
|
|||||||
return fmt.Errorf("htlc %v not found", key)
|
return fmt.Errorf("htlc %v not found", key)
|
||||||
}
|
}
|
||||||
if htlc.State == channeldb.HtlcStateCanceled {
|
if htlc.State == channeldb.HtlcStateCanceled {
|
||||||
i.notifyHodlSubscribers(HtlcResolution{
|
i.notifyHodlSubscribers(
|
||||||
CircuitKey: key,
|
*NewFailureResolution(key, int32(htlc.AcceptHeight)),
|
||||||
AcceptHeight: int32(htlc.AcceptHeight),
|
)
|
||||||
Preimage: nil,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -745,10 +765,7 @@ func (i *InvoiceRegistry) NotifyExitHopHtlc(rHash lntypes.Hash,
|
|||||||
|
|
||||||
// If it isn't recorded, cancel htlc.
|
// If it isn't recorded, cancel htlc.
|
||||||
if !ok {
|
if !ok {
|
||||||
return &HtlcResolution{
|
return NewFailureResolution(circuitKey, currentHeight), nil
|
||||||
CircuitKey: circuitKey,
|
|
||||||
AcceptHeight: currentHeight,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine accepted height of this htlc. If the htlc reached the
|
// Determine accepted height of this htlc. If the htlc reached the
|
||||||
@@ -759,10 +776,7 @@ func (i *InvoiceRegistry) NotifyExitHopHtlc(rHash lntypes.Hash,
|
|||||||
|
|
||||||
switch invoiceHtlc.State {
|
switch invoiceHtlc.State {
|
||||||
case channeldb.HtlcStateCanceled:
|
case channeldb.HtlcStateCanceled:
|
||||||
return &HtlcResolution{
|
return NewFailureResolution(circuitKey, acceptHeight), nil
|
||||||
CircuitKey: circuitKey,
|
|
||||||
AcceptHeight: acceptHeight,
|
|
||||||
}, nil
|
|
||||||
|
|
||||||
case channeldb.HtlcStateSettled:
|
case channeldb.HtlcStateSettled:
|
||||||
// Also settle any previously accepted htlcs. The invoice state
|
// Also settle any previously accepted htlcs. The invoice state
|
||||||
@@ -773,18 +787,17 @@ func (i *InvoiceRegistry) NotifyExitHopHtlc(rHash lntypes.Hash,
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
i.notifyHodlSubscribers(HtlcResolution{
|
resolution := *NewSettleResolution(
|
||||||
CircuitKey: key,
|
invoice.Terms.PaymentPreimage, key,
|
||||||
Preimage: &invoice.Terms.PaymentPreimage,
|
acceptHeight,
|
||||||
AcceptHeight: int32(htlc.AcceptHeight),
|
)
|
||||||
})
|
|
||||||
|
i.notifyHodlSubscribers(resolution)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &HtlcResolution{
|
return NewSettleResolution(
|
||||||
CircuitKey: circuitKey,
|
invoice.Terms.PaymentPreimage, circuitKey, acceptHeight,
|
||||||
Preimage: &invoice.Terms.PaymentPreimage,
|
), nil
|
||||||
AcceptHeight: acceptHeight,
|
|
||||||
}, nil
|
|
||||||
|
|
||||||
case channeldb.HtlcStateAccepted:
|
case channeldb.HtlcStateAccepted:
|
||||||
// (Re)start the htlc timer if the invoice is still open. It can
|
// (Re)start the htlc timer if the invoice is still open. It can
|
||||||
@@ -836,7 +849,9 @@ func (i *InvoiceRegistry) SettleHodlInvoice(preimage lntypes.Preimage) error {
|
|||||||
hash := preimage.Hash()
|
hash := preimage.Hash()
|
||||||
invoice, err := i.cdb.UpdateInvoice(hash, updateInvoice)
|
invoice, err := i.cdb.UpdateInvoice(hash, updateInvoice)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("SettleHodlInvoice with preimage %v: %v", preimage, err)
|
log.Errorf("SettleHodlInvoice with preimage %v: %v",
|
||||||
|
preimage, err)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -854,11 +869,11 @@ func (i *InvoiceRegistry) SettleHodlInvoice(preimage lntypes.Preimage) error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
i.notifyHodlSubscribers(HtlcResolution{
|
resolution := *NewSettleResolution(
|
||||||
CircuitKey: key,
|
preimage, key, int32(htlc.AcceptHeight),
|
||||||
Preimage: &preimage,
|
)
|
||||||
AcceptHeight: int32(htlc.AcceptHeight),
|
|
||||||
})
|
i.notifyHodlSubscribers(resolution)
|
||||||
}
|
}
|
||||||
i.notifyClients(hash, invoice, invoice.State)
|
i.notifyClients(hash, invoice, invoice.State)
|
||||||
|
|
||||||
@@ -932,10 +947,9 @@ func (i *InvoiceRegistry) cancelInvoiceImpl(payHash lntypes.Hash,
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
i.notifyHodlSubscribers(HtlcResolution{
|
i.notifyHodlSubscribers(
|
||||||
CircuitKey: key,
|
*NewFailureResolution(key, int32(htlc.AcceptHeight)),
|
||||||
AcceptHeight: int32(htlc.AcceptHeight),
|
)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
i.notifyClients(payHash, invoice, channeldb.ContractCanceled)
|
i.notifyClients(payHash, invoice, channeldb.ContractCanceled)
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user