mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-02-24 14:50:40 +01:00
lnwallet: add function to convert paymentDescriptor to LogUpdate
Here we add a function that is capable of recovering LogUpdates from paymentDescriptors and we refactor the lnwallet code to use this rather than doing JIT inline construction of the LogUpdates.
This commit is contained in:
parent
9904af5d96
commit
d364b9b9f8
2 changed files with 83 additions and 205 deletions
|
@ -3371,12 +3371,6 @@ func genRemoteHtlcSigJobs(keyRing *CommitmentKeyRing,
|
||||||
func (lc *LightningChannel) createCommitDiff(newCommit *commitment,
|
func (lc *LightningChannel) createCommitDiff(newCommit *commitment,
|
||||||
commitSig lnwire.Sig, htlcSigs []lnwire.Sig) *channeldb.CommitDiff {
|
commitSig lnwire.Sig, htlcSigs []lnwire.Sig) *channeldb.CommitDiff {
|
||||||
|
|
||||||
// First, we need to convert the funding outpoint into the ID that's
|
|
||||||
// used on the wire to identify this channel. We'll use this shortly
|
|
||||||
// when recording the exact CommitSig message that we'll be sending
|
|
||||||
// out.
|
|
||||||
chanID := lnwire.NewChanIDFromOutPoint(lc.channelState.FundingOutpoint)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
logUpdates []channeldb.LogUpdate
|
logUpdates []channeldb.LogUpdate
|
||||||
ackAddRefs []channeldb.AddRef
|
ackAddRefs []channeldb.AddRef
|
||||||
|
@ -3401,91 +3395,42 @@ func (lc *LightningChannel) createCommitDiff(newCommit *commitment,
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Knowing that this update is a part of this new commitment,
|
|
||||||
// we'll create a log update and not its index in the log so
|
|
||||||
// we can later restore it properly if a restart occurs.
|
|
||||||
logUpdate := channeldb.LogUpdate{
|
|
||||||
LogIndex: pd.LogIndex,
|
|
||||||
}
|
|
||||||
|
|
||||||
// We'll map the type of the PaymentDescriptor to one of the
|
// We'll map the type of the PaymentDescriptor to one of the
|
||||||
// four messages that it corresponds to. With this set of
|
// four messages that it corresponds to. With this set of
|
||||||
// messages obtained, we can simply read from disk and re-send
|
// messages obtained, we can simply read from disk and re-send
|
||||||
// them in the case of a needed channel sync.
|
// them in the case of a needed channel sync.
|
||||||
switch pd.EntryType {
|
switch pd.EntryType {
|
||||||
case Add:
|
case Add:
|
||||||
htlc := &lnwire.UpdateAddHTLC{
|
|
||||||
ChanID: chanID,
|
|
||||||
ID: pd.HtlcIndex,
|
|
||||||
Amount: pd.Amount,
|
|
||||||
Expiry: pd.Timeout,
|
|
||||||
PaymentHash: pd.RHash,
|
|
||||||
OnionBlob: pd.OnionBlob,
|
|
||||||
BlindingPoint: pd.BlindingPoint,
|
|
||||||
CustomRecords: pd.CustomRecords.Copy(),
|
|
||||||
}
|
|
||||||
logUpdate.UpdateMsg = htlc
|
|
||||||
|
|
||||||
// Gather any references for circuits opened by this Add
|
// Gather any references for circuits opened by this Add
|
||||||
// HTLC.
|
// HTLC.
|
||||||
if pd.OpenCircuitKey != nil {
|
if pd.OpenCircuitKey != nil {
|
||||||
openCircuitKeys = append(openCircuitKeys,
|
openCircuitKeys = append(
|
||||||
*pd.OpenCircuitKey)
|
openCircuitKeys, *pd.OpenCircuitKey,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
logUpdates = append(logUpdates, logUpdate)
|
case Settle, Fail, MalformedFail:
|
||||||
|
// Gather the fwd pkg references from any settle or fail
|
||||||
// Short circuit here since an add should not have any
|
// packets, if they exist.
|
||||||
// of the references gathered in the case of settles,
|
if pd.SourceRef != nil {
|
||||||
// fails or malformed fails.
|
ackAddRefs = append(ackAddRefs, *pd.SourceRef)
|
||||||
continue
|
|
||||||
|
|
||||||
case Settle:
|
|
||||||
logUpdate.UpdateMsg = &lnwire.UpdateFulfillHTLC{
|
|
||||||
ChanID: chanID,
|
|
||||||
ID: pd.ParentIndex,
|
|
||||||
PaymentPreimage: pd.RPreimage,
|
|
||||||
}
|
}
|
||||||
|
if pd.DestRef != nil {
|
||||||
case Fail:
|
settleFailRefs = append(
|
||||||
logUpdate.UpdateMsg = &lnwire.UpdateFailHTLC{
|
settleFailRefs, *pd.DestRef,
|
||||||
ChanID: chanID,
|
)
|
||||||
ID: pd.ParentIndex,
|
|
||||||
Reason: pd.FailReason,
|
|
||||||
}
|
}
|
||||||
|
if pd.ClosedCircuitKey != nil {
|
||||||
case MalformedFail:
|
closedCircuitKeys = append(
|
||||||
logUpdate.UpdateMsg = &lnwire.UpdateFailMalformedHTLC{
|
closedCircuitKeys, *pd.ClosedCircuitKey,
|
||||||
ChanID: chanID,
|
)
|
||||||
ID: pd.ParentIndex,
|
|
||||||
ShaOnionBlob: pd.ShaOnionBlob,
|
|
||||||
FailureCode: pd.FailCode,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case FeeUpdate:
|
case FeeUpdate:
|
||||||
// The Amount field holds the feerate denominated in
|
// Nothing special to do.
|
||||||
// msat. Since feerates are only denominated in sat/kw,
|
|
||||||
// we can convert it without loss of precision.
|
|
||||||
logUpdate.UpdateMsg = &lnwire.UpdateFee{
|
|
||||||
ChanID: chanID,
|
|
||||||
FeePerKw: uint32(pd.Amount.ToSatoshis()),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gather the fwd pkg references from any settle or fail
|
logUpdates = append(logUpdates, pd.ToLogUpdate())
|
||||||
// packets, if they exist.
|
|
||||||
if pd.SourceRef != nil {
|
|
||||||
ackAddRefs = append(ackAddRefs, *pd.SourceRef)
|
|
||||||
}
|
|
||||||
if pd.DestRef != nil {
|
|
||||||
settleFailRefs = append(settleFailRefs, *pd.DestRef)
|
|
||||||
}
|
|
||||||
if pd.ClosedCircuitKey != nil {
|
|
||||||
closedCircuitKeys = append(closedCircuitKeys,
|
|
||||||
*pd.ClosedCircuitKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
logUpdates = append(logUpdates, logUpdate)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// With the set of log updates mapped into wire messages, we'll now
|
// With the set of log updates mapped into wire messages, we'll now
|
||||||
|
@ -3513,10 +3458,6 @@ func (lc *LightningChannel) createCommitDiff(newCommit *commitment,
|
||||||
// getUnsignedAckedUpdates returns all remote log updates that we haven't
|
// getUnsignedAckedUpdates returns all remote log updates that we haven't
|
||||||
// signed for yet ourselves.
|
// signed for yet ourselves.
|
||||||
func (lc *LightningChannel) getUnsignedAckedUpdates() []channeldb.LogUpdate {
|
func (lc *LightningChannel) getUnsignedAckedUpdates() []channeldb.LogUpdate {
|
||||||
// First, we need to convert the funding outpoint into the ID that's
|
|
||||||
// used on the wire to identify this channel.
|
|
||||||
chanID := lnwire.NewChanIDFromOutPoint(lc.channelState.FundingOutpoint)
|
|
||||||
|
|
||||||
// Fetch the last remote update that we have signed for.
|
// Fetch the last remote update that we have signed for.
|
||||||
lastRemoteCommitted :=
|
lastRemoteCommitted :=
|
||||||
lc.commitChains.Remote.tail().messageIndices.Remote
|
lc.commitChains.Remote.tail().messageIndices.Remote
|
||||||
|
@ -3547,60 +3488,9 @@ func (lc *LightningChannel) getUnsignedAckedUpdates() []channeldb.LogUpdate {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
logUpdate := channeldb.LogUpdate{
|
logUpdates = append(logUpdates, pd.ToLogUpdate())
|
||||||
LogIndex: pd.LogIndex,
|
|
||||||
}
|
|
||||||
|
|
||||||
// We'll map the type of the PaymentDescriptor to one of the
|
|
||||||
// four messages that it corresponds to.
|
|
||||||
switch pd.EntryType {
|
|
||||||
case Add:
|
|
||||||
htlc := &lnwire.UpdateAddHTLC{
|
|
||||||
ChanID: chanID,
|
|
||||||
ID: pd.HtlcIndex,
|
|
||||||
Amount: pd.Amount,
|
|
||||||
Expiry: pd.Timeout,
|
|
||||||
PaymentHash: pd.RHash,
|
|
||||||
OnionBlob: pd.OnionBlob,
|
|
||||||
BlindingPoint: pd.BlindingPoint,
|
|
||||||
CustomRecords: pd.CustomRecords.Copy(),
|
|
||||||
}
|
|
||||||
logUpdate.UpdateMsg = htlc
|
|
||||||
|
|
||||||
case Settle:
|
|
||||||
logUpdate.UpdateMsg = &lnwire.UpdateFulfillHTLC{
|
|
||||||
ChanID: chanID,
|
|
||||||
ID: pd.ParentIndex,
|
|
||||||
PaymentPreimage: pd.RPreimage,
|
|
||||||
}
|
|
||||||
|
|
||||||
case Fail:
|
|
||||||
logUpdate.UpdateMsg = &lnwire.UpdateFailHTLC{
|
|
||||||
ChanID: chanID,
|
|
||||||
ID: pd.ParentIndex,
|
|
||||||
Reason: pd.FailReason,
|
|
||||||
}
|
|
||||||
|
|
||||||
case MalformedFail:
|
|
||||||
logUpdate.UpdateMsg = &lnwire.UpdateFailMalformedHTLC{
|
|
||||||
ChanID: chanID,
|
|
||||||
ID: pd.ParentIndex,
|
|
||||||
ShaOnionBlob: pd.ShaOnionBlob,
|
|
||||||
FailureCode: pd.FailCode,
|
|
||||||
}
|
|
||||||
|
|
||||||
case FeeUpdate:
|
|
||||||
// The Amount field holds the feerate denominated in
|
|
||||||
// msat. Since feerates are only denominated in sat/kw,
|
|
||||||
// we can convert it without loss of precision.
|
|
||||||
logUpdate.UpdateMsg = &lnwire.UpdateFee{
|
|
||||||
ChanID: chanID,
|
|
||||||
FeePerKw: uint32(pd.Amount.ToSatoshis()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
logUpdates = append(logUpdates, logUpdate)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return logUpdates
|
return logUpdates
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5621,55 +5511,19 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) (
|
||||||
|
|
||||||
// If we've reached this point, this HTLC will be added to the
|
// If we've reached this point, this HTLC will be added to the
|
||||||
// forwarding package at the height of the remote commitment.
|
// forwarding package at the height of the remote commitment.
|
||||||
// All types of HTLCs will record their assigned log index.
|
// We'll map the type of the PaymentDescriptor to one of the
|
||||||
logUpdate := channeldb.LogUpdate{
|
// four messages that it corresponds to and separate the
|
||||||
LogIndex: pd.LogIndex,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Next, we'll map the type of the PaymentDescriptor to one of
|
|
||||||
// the four messages that it corresponds to and separate the
|
|
||||||
// updates into Adds and Settle/Fail/MalformedFail such that
|
// updates into Adds and Settle/Fail/MalformedFail such that
|
||||||
// they can be written in the forwarding package. Adds are
|
// they can be written in the forwarding package. Adds are
|
||||||
// aggregated separately from the other types of HTLCs.
|
// aggregated separately from the other types of HTLCs.
|
||||||
switch pd.EntryType {
|
switch pd.EntryType {
|
||||||
case Add:
|
case Add:
|
||||||
htlc := &lnwire.UpdateAddHTLC{
|
addUpdates = append(addUpdates, pd.ToLogUpdate())
|
||||||
ChanID: chanID,
|
|
||||||
ID: pd.HtlcIndex,
|
|
||||||
Amount: pd.Amount,
|
|
||||||
Expiry: pd.Timeout,
|
|
||||||
PaymentHash: pd.RHash,
|
|
||||||
OnionBlob: pd.OnionBlob,
|
|
||||||
BlindingPoint: pd.BlindingPoint,
|
|
||||||
CustomRecords: pd.CustomRecords.Copy(),
|
|
||||||
}
|
|
||||||
logUpdate.UpdateMsg = htlc
|
|
||||||
addUpdates = append(addUpdates, logUpdate)
|
|
||||||
|
|
||||||
case Settle:
|
case Settle, Fail, MalformedFail:
|
||||||
logUpdate.UpdateMsg = &lnwire.UpdateFulfillHTLC{
|
settleFailUpdates = append(
|
||||||
ChanID: chanID,
|
settleFailUpdates, pd.ToLogUpdate(),
|
||||||
ID: pd.ParentIndex,
|
)
|
||||||
PaymentPreimage: pd.RPreimage,
|
|
||||||
}
|
|
||||||
settleFailUpdates = append(settleFailUpdates, logUpdate)
|
|
||||||
|
|
||||||
case Fail:
|
|
||||||
logUpdate.UpdateMsg = &lnwire.UpdateFailHTLC{
|
|
||||||
ChanID: chanID,
|
|
||||||
ID: pd.ParentIndex,
|
|
||||||
Reason: pd.FailReason,
|
|
||||||
}
|
|
||||||
settleFailUpdates = append(settleFailUpdates, logUpdate)
|
|
||||||
|
|
||||||
case MalformedFail:
|
|
||||||
logUpdate.UpdateMsg = &lnwire.UpdateFailMalformedHTLC{
|
|
||||||
ChanID: chanID,
|
|
||||||
ID: pd.ParentIndex,
|
|
||||||
ShaOnionBlob: pd.ShaOnionBlob,
|
|
||||||
FailureCode: pd.FailCode,
|
|
||||||
}
|
|
||||||
settleFailUpdates = append(settleFailUpdates, logUpdate)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9005,38 +8859,9 @@ func (lc *LightningChannel) unsignedLocalUpdates(remoteMessageIndex,
|
||||||
// covered in the next commitment signature that the remote
|
// covered in the next commitment signature that the remote
|
||||||
// sends.
|
// sends.
|
||||||
if pd.LogIndex < remoteMessageIndex && pd.LogIndex >= localMessageIndex {
|
if pd.LogIndex < remoteMessageIndex && pd.LogIndex >= localMessageIndex {
|
||||||
logUpdate := channeldb.LogUpdate{
|
localPeerUpdates = append(
|
||||||
LogIndex: pd.LogIndex,
|
localPeerUpdates, pd.ToLogUpdate(),
|
||||||
}
|
)
|
||||||
|
|
||||||
switch pd.EntryType {
|
|
||||||
case FeeUpdate:
|
|
||||||
logUpdate.UpdateMsg = &lnwire.UpdateFee{
|
|
||||||
ChanID: chanID,
|
|
||||||
FeePerKw: uint32(pd.Amount.ToSatoshis()),
|
|
||||||
}
|
|
||||||
case Settle:
|
|
||||||
logUpdate.UpdateMsg = &lnwire.UpdateFulfillHTLC{
|
|
||||||
ChanID: chanID,
|
|
||||||
ID: pd.ParentIndex,
|
|
||||||
PaymentPreimage: pd.RPreimage,
|
|
||||||
}
|
|
||||||
case Fail:
|
|
||||||
logUpdate.UpdateMsg = &lnwire.UpdateFailHTLC{
|
|
||||||
ChanID: chanID,
|
|
||||||
ID: pd.ParentIndex,
|
|
||||||
Reason: pd.FailReason,
|
|
||||||
}
|
|
||||||
case MalformedFail:
|
|
||||||
logUpdate.UpdateMsg = &lnwire.UpdateFailMalformedHTLC{
|
|
||||||
ChanID: chanID,
|
|
||||||
ID: pd.ParentIndex,
|
|
||||||
ShaOnionBlob: pd.ShaOnionBlob,
|
|
||||||
FailureCode: pd.FailCode,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
localPeerUpdates = append(localPeerUpdates, logUpdate)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -231,3 +231,56 @@ type PaymentDescriptor struct {
|
||||||
// may have been attached to a sent HTLC.
|
// may have been attached to a sent HTLC.
|
||||||
CustomRecords lnwire.CustomRecords
|
CustomRecords lnwire.CustomRecords
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToLogUpdate recovers the underlying LogUpdate from the paymentDescriptor.
|
||||||
|
// This operation is lossy and will forget some extra information tracked by the
|
||||||
|
// paymentDescriptor but the function is total in that all paymentDescriptors
|
||||||
|
// can be converted back to LogUpdates.
|
||||||
|
func (pd *PaymentDescriptor) ToLogUpdate() channeldb.LogUpdate {
|
||||||
|
var msg lnwire.Message
|
||||||
|
switch pd.EntryType {
|
||||||
|
case Add:
|
||||||
|
msg = &lnwire.UpdateAddHTLC{
|
||||||
|
ChanID: pd.ChanID,
|
||||||
|
ID: pd.HtlcIndex,
|
||||||
|
Amount: pd.Amount,
|
||||||
|
PaymentHash: pd.RHash,
|
||||||
|
Expiry: pd.Timeout,
|
||||||
|
OnionBlob: pd.OnionBlob,
|
||||||
|
BlindingPoint: pd.BlindingPoint,
|
||||||
|
CustomRecords: pd.CustomRecords.Copy(),
|
||||||
|
}
|
||||||
|
case Settle:
|
||||||
|
msg = &lnwire.UpdateFulfillHTLC{
|
||||||
|
ChanID: pd.ChanID,
|
||||||
|
ID: pd.ParentIndex,
|
||||||
|
PaymentPreimage: pd.RPreimage,
|
||||||
|
}
|
||||||
|
case Fail:
|
||||||
|
msg = &lnwire.UpdateFailHTLC{
|
||||||
|
ChanID: pd.ChanID,
|
||||||
|
ID: pd.ParentIndex,
|
||||||
|
Reason: pd.FailReason,
|
||||||
|
}
|
||||||
|
case MalformedFail:
|
||||||
|
msg = &lnwire.UpdateFailMalformedHTLC{
|
||||||
|
ChanID: pd.ChanID,
|
||||||
|
ID: pd.ParentIndex,
|
||||||
|
ShaOnionBlob: pd.ShaOnionBlob,
|
||||||
|
FailureCode: pd.FailCode,
|
||||||
|
}
|
||||||
|
case FeeUpdate:
|
||||||
|
// The Amount field holds the feerate denominated in
|
||||||
|
// msat. Since feerates are only denominated in sat/kw,
|
||||||
|
// we can convert it without loss of precision.
|
||||||
|
msg = &lnwire.UpdateFee{
|
||||||
|
ChanID: pd.ChanID,
|
||||||
|
FeePerKw: uint32(pd.Amount.ToSatoshis()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return channeldb.LogUpdate{
|
||||||
|
LogIndex: pd.LogIndex,
|
||||||
|
UpdateMsg: msg,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue