-
Notifications
You must be signed in to change notification settings - Fork 2.3k
htlcswitch/hop: new package for hop payload parsing/encapsulation #3466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
joostjager
merged 4 commits into
lightningnetwork:master
from
cfromknecht:htlcswitch-hop
Sep 5, 2019
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
83d2112
htlcswitch/hop: move NetworkHop to hop.Network
cfromknecht fc0e4be
htlcswitch+hop: move ForwardingInfo to hop.ForwaringInfo
cfromknecht 378e055
htlcswitch/hop: move hop.Exit and hop.Source to hop pkg
cfromknecht 138b079
htlcswitch/iterator: use hop.Payload constructors
cfromknecht File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package hop | ||
|
|
||
| import ( | ||
| "github.com/lightningnetwork/lnd/lnwire" | ||
| ) | ||
|
|
||
| // ForwardingInfo contains all the information that is necessary to forward and | ||
| // incoming HTLC to the next hop encoded within a valid HopIterator instance. | ||
| // Forwarding links are to use this information to authenticate the information | ||
| // received within the incoming HTLC, to ensure that the prior hop didn't | ||
| // tamper with the end-to-end routing information at all. | ||
| type ForwardingInfo struct { | ||
| // Network is the target blockchain network that the HTLC will travel | ||
| // over next. | ||
| Network Network | ||
|
|
||
| // NextHop is the channel ID of the next hop. The received HTLC should | ||
| // be forwarded to this particular channel in order to continue the | ||
| // end-to-end route. | ||
| NextHop lnwire.ShortChannelID | ||
|
|
||
| // AmountToForward is the amount of milli-satoshis that the receiving | ||
| // node should forward to the next hop. | ||
| AmountToForward lnwire.MilliSatoshi | ||
|
|
||
| // OutgoingCTLV is the specified value of the CTLV timelock to be used | ||
| // in the outgoing HTLC. | ||
| OutgoingCTLV uint32 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package hop | ||
|
|
||
| // Network indicates the blockchain network that is intended to be the next hop | ||
| // for a forwarded HTLC. The existence of this field within the ForwardingInfo | ||
| // struct enables the ability for HTLC to cross chain-boundaries at will. | ||
| type Network uint8 | ||
|
|
||
| const ( | ||
| // BitcoinNetwork denotes that an HTLC is to be forwarded along the | ||
| // Bitcoin link with the specified short channel ID. | ||
| BitcoinNetwork Network = iota | ||
|
|
||
| // LitecoinNetwork denotes that an HTLC is to be forwarded along the | ||
| // Litecoin link with the specified short channel ID. | ||
| LitecoinNetwork | ||
| ) | ||
|
|
||
| // String returns the string representation of the target Network. | ||
| func (c Network) String() string { | ||
| switch c { | ||
| case BitcoinNetwork: | ||
| return "Bitcoin" | ||
| case LitecoinNetwork: | ||
| return "Litecoin" | ||
| default: | ||
| return "Kekcoin" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package hop | ||
|
|
||
| import ( | ||
| "encoding/binary" | ||
| "io" | ||
|
|
||
| "github.com/lightningnetwork/lightning-onion" | ||
| "github.com/lightningnetwork/lnd/lnwire" | ||
| "github.com/lightningnetwork/lnd/record" | ||
| "github.com/lightningnetwork/lnd/tlv" | ||
| ) | ||
|
|
||
| // Payload encapsulates all information delivered to a hop in an onion payload. | ||
| // A Hop can represent either a TLV or legacy payload. The primary forwarding | ||
| // instruction can be accessed via ForwardingInfo, and additional records can be | ||
| // accessed by other member functions. | ||
| type Payload struct { | ||
| // FwdInfo holds the basic parameters required for HTLC forwarding, e.g. | ||
| // amount, cltv, and next hop. | ||
| FwdInfo ForwardingInfo | ||
| } | ||
|
|
||
| // NewLegacyPayload builds a Payload from the amount, cltv, and next hop | ||
| // parameters provided by leegacy onion payloads. | ||
| func NewLegacyPayload(f *sphinx.HopData) *Payload { | ||
| nextHop := binary.BigEndian.Uint64(f.NextAddress[:]) | ||
|
|
||
| return &Payload{ | ||
| FwdInfo: ForwardingInfo{ | ||
| Network: BitcoinNetwork, | ||
| NextHop: lnwire.NewShortChanIDFromInt(nextHop), | ||
| AmountToForward: lnwire.MilliSatoshi(f.ForwardAmount), | ||
| OutgoingCTLV: f.OutgoingCltv, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // NewPayloadFromReader builds a new Hop from the passed io.Reader. The reader | ||
| // should correspond to the bytes encapsulated in a TLV onion payload. | ||
| func NewPayloadFromReader(r io.Reader) (*Payload, error) { | ||
| var ( | ||
| cid uint64 | ||
| amt uint64 | ||
| cltv uint32 | ||
| ) | ||
|
|
||
| tlvStream, err := tlv.NewStream( | ||
| record.NewAmtToFwdRecord(&amt), | ||
| record.NewLockTimeRecord(&cltv), | ||
| record.NewNextHopIDRecord(&cid), | ||
| ) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| err = tlvStream.Decode(r) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &Payload{ | ||
| FwdInfo: ForwardingInfo{ | ||
| Network: BitcoinNetwork, | ||
| NextHop: lnwire.NewShortChanIDFromInt(cid), | ||
| AmountToForward: lnwire.MilliSatoshi(amt), | ||
| OutgoingCTLV: cltv, | ||
| }, | ||
| }, nil | ||
| } | ||
|
|
||
| // ForwardingInfo returns the basic parameters required for HTLC forwarding, | ||
| // e.g. amount, cltv, and next hop. | ||
| func (h *Payload) ForwardingInfo() ForwardingInfo { | ||
| return h.FwdInfo | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package hop | ||
|
|
||
| import "github.com/lightningnetwork/lnd/lnwire" | ||
|
|
||
| var ( | ||
| // Exit is a special "hop" denoting that an incoming HTLC is meant to | ||
| // pay finally to the receiving node. | ||
| Exit lnwire.ShortChannelID | ||
|
|
||
| // Source is a sentinel "hop" denoting that an incoming HTLC is | ||
| // initiated by our own switch. | ||
| Source lnwire.ShortChannelID | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.