lnwallet/channel: concrete HTLC errors and ack AddRefs

This commit is contained in:
Conner Fromknecht 2018-07-27 03:22:15 -07:00
parent 0dca373bcd
commit 05308ec22c
No known key found for this signature in database
GPG Key ID: E7D737B67FA592C7

@ -4433,6 +4433,22 @@ func (lc *LightningChannel) LoadFwdPkgs() ([]*channeldb.FwdPkg, error) {
return lc.channelState.LoadFwdPkgs() return lc.channelState.LoadFwdPkgs()
} }
// AckAddHtlcs sets a bit in the FwdFilter of a forwarding package belonging to
// this channel, that corresponds to the given AddRef. This method also succeeds
// if no forwarding package is found.
func (lc *LightningChannel) AckAddHtlcs(addRef channeldb.AddRef) error {
return lc.channelState.AckAddHtlcs(addRef)
}
// AckSettleFails sets a bit in the SettleFailFilter of a forwarding package
// belonging to this channel, that corresponds to the given SettleFailRef. This
// method also succeeds if no forwarding package is found.
func (lc *LightningChannel) AckSettleFails(
settleFailRefs ...channeldb.SettleFailRef) error {
return lc.channelState.AckSettleFails(settleFailRefs...)
}
// SetFwdFilter writes the forwarding decision for a given remote commitment // SetFwdFilter writes the forwarding decision for a given remote commitment
// height. // height.
func (lc *LightningChannel) SetFwdFilter(height uint64, func (lc *LightningChannel) SetFwdFilter(height uint64,
@ -4572,21 +4588,18 @@ func (lc *LightningChannel) SettleHTLC(preimage [32]byte,
htlc := lc.remoteUpdateLog.lookupHtlc(htlcIndex) htlc := lc.remoteUpdateLog.lookupHtlc(htlcIndex)
if htlc == nil { if htlc == nil {
return fmt.Errorf("No HTLC with ID %d in channel %v", htlcIndex, return ErrUnknownHtlcIndex{lc.ShortChanID(), htlcIndex}
lc.ShortChanID())
} }
// Now that we know the HTLC exists, before checking to see if the // Now that we know the HTLC exists, before checking to see if the
// preimage matches, we'll ensure that we haven't already attempted to // preimage matches, we'll ensure that we haven't already attempted to
// modify the HTLC. // modify the HTLC.
if lc.remoteUpdateLog.htlcHasModification(htlcIndex) { if lc.remoteUpdateLog.htlcHasModification(htlcIndex) {
return fmt.Errorf("HTLC with ID %d has already been settled", return ErrHtlcIndexAlreadySettled(htlcIndex)
htlcIndex)
} }
if htlc.RHash != sha256.Sum256(preimage[:]) { if htlc.RHash != sha256.Sum256(preimage[:]) {
return fmt.Errorf("Invalid payment preimage %x for hash %x", return ErrInvalidSettlePreimage{preimage[:], htlc.RHash[:]}
preimage[:], htlc.RHash[:])
} }
pd := &PaymentDescriptor{ pd := &PaymentDescriptor{
@ -4620,21 +4633,18 @@ func (lc *LightningChannel) ReceiveHTLCSettle(preimage [32]byte, htlcIndex uint6
htlc := lc.localUpdateLog.lookupHtlc(htlcIndex) htlc := lc.localUpdateLog.lookupHtlc(htlcIndex)
if htlc == nil { if htlc == nil {
return fmt.Errorf("No HTLC with ID %d in channel %v", htlcIndex, return ErrUnknownHtlcIndex{lc.ShortChanID(), htlcIndex}
lc.ShortChanID())
} }
// Now that we know the HTLC exists, before checking to see if the // Now that we know the HTLC exists, before checking to see if the
// preimage matches, we'll ensure that they haven't already attempted // preimage matches, we'll ensure that they haven't already attempted
// to modify the HTLC. // to modify the HTLC.
if lc.localUpdateLog.htlcHasModification(htlcIndex) { if lc.localUpdateLog.htlcHasModification(htlcIndex) {
return fmt.Errorf("HTLC with ID %d has already been settled", return ErrHtlcIndexAlreadySettled(htlcIndex)
htlcIndex)
} }
if htlc.RHash != sha256.Sum256(preimage[:]) { if htlc.RHash != sha256.Sum256(preimage[:]) {
return fmt.Errorf("Invalid payment preimage %x for hash %x", return ErrInvalidSettlePreimage{preimage[:], htlc.RHash[:]}
preimage[:], htlc.RHash[:])
} }
pd := &PaymentDescriptor{ pd := &PaymentDescriptor{
@ -4688,15 +4698,13 @@ func (lc *LightningChannel) FailHTLC(htlcIndex uint64, reason []byte,
htlc := lc.remoteUpdateLog.lookupHtlc(htlcIndex) htlc := lc.remoteUpdateLog.lookupHtlc(htlcIndex)
if htlc == nil { if htlc == nil {
return fmt.Errorf("No HTLC with ID %d in channel %v", htlcIndex, return ErrUnknownHtlcIndex{lc.ShortChanID(), htlcIndex}
lc.ShortChanID())
} }
// Now that we know the HTLC exists, we'll ensure that we haven't // Now that we know the HTLC exists, we'll ensure that we haven't
// already attempted to fail the HTLC. // already attempted to fail the HTLC.
if lc.remoteUpdateLog.htlcHasModification(htlcIndex) { if lc.remoteUpdateLog.htlcHasModification(htlcIndex) {
return fmt.Errorf("HTLC with ID %d has already been failed", return ErrHtlcIndexAlreadyFailed(htlcIndex)
htlcIndex)
} }
pd := &PaymentDescriptor{ pd := &PaymentDescriptor{
@ -4740,15 +4748,13 @@ func (lc *LightningChannel) MalformedFailHTLC(htlcIndex uint64,
htlc := lc.remoteUpdateLog.lookupHtlc(htlcIndex) htlc := lc.remoteUpdateLog.lookupHtlc(htlcIndex)
if htlc == nil { if htlc == nil {
return fmt.Errorf("No HTLC with ID %d in channel %v", htlcIndex, return ErrUnknownHtlcIndex{lc.ShortChanID(), htlcIndex}
lc.ShortChanID())
} }
// Now that we know the HTLC exists, we'll ensure that we haven't // Now that we know the HTLC exists, we'll ensure that we haven't
// already attempted to fail the HTLC. // already attempted to fail the HTLC.
if lc.remoteUpdateLog.htlcHasModification(htlcIndex) { if lc.remoteUpdateLog.htlcHasModification(htlcIndex) {
return fmt.Errorf("HTLC with ID %d has already been failed", return ErrHtlcIndexAlreadyFailed(htlcIndex)
htlcIndex)
} }
pd := &PaymentDescriptor{ pd := &PaymentDescriptor{
@ -4785,15 +4791,13 @@ func (lc *LightningChannel) ReceiveFailHTLC(htlcIndex uint64, reason []byte,
htlc := lc.localUpdateLog.lookupHtlc(htlcIndex) htlc := lc.localUpdateLog.lookupHtlc(htlcIndex)
if htlc == nil { if htlc == nil {
return fmt.Errorf("No HTLC with ID %d in channel %v", htlcIndex, return ErrUnknownHtlcIndex{lc.ShortChanID(), htlcIndex}
lc.ShortChanID())
} }
// Now that we know the HTLC exists, we'll ensure that they haven't // Now that we know the HTLC exists, we'll ensure that they haven't
// already attempted to fail the HTLC. // already attempted to fail the HTLC.
if lc.localUpdateLog.htlcHasModification(htlcIndex) { if lc.localUpdateLog.htlcHasModification(htlcIndex) {
return fmt.Errorf("HTLC with ID %d has already been failed", return ErrHtlcIndexAlreadyFailed(htlcIndex)
htlcIndex)
} }
pd := &PaymentDescriptor{ pd := &PaymentDescriptor{