routerrpc: refactor hop unmarshall code
Remove code duplication in preparation for additional unmarshalling.
This commit is contained in:
parent
5d4ceca038
commit
0abc054eb0
@ -364,90 +364,64 @@ func (r *RouterBackend) MarshallRoute(route *route.Route) (*lnrpc.Route, error)
|
|||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshallHopByChannelLookup unmarshalls an rpc hop for which the pub key is
|
// UnmarshallHopWithPubkey unmarshalls an rpc hop for which the pubkey has
|
||||||
// not known. This function will query the channel graph with channel id to
|
// already been extracted.
|
||||||
// retrieve both endpoints and determine the hop pubkey using the previous hop
|
func UnmarshallHopWithPubkey(rpcHop *lnrpc.Hop, pubkey route.Vertex) (*route.Hop,
|
||||||
// pubkey. If the channel is unknown, an error is returned.
|
error) {
|
||||||
func (r *RouterBackend) UnmarshallHopByChannelLookup(hop *lnrpc.Hop,
|
|
||||||
prevPubKeyBytes [33]byte) (*route.Hop, error) {
|
|
||||||
|
|
||||||
// Discard edge policies, because they may be nil.
|
|
||||||
node1, node2, err := r.FetchChannelEndpoints(hop.ChanId)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var pubKeyBytes [33]byte
|
|
||||||
switch {
|
|
||||||
case prevPubKeyBytes == node1:
|
|
||||||
pubKeyBytes = node2
|
|
||||||
case prevPubKeyBytes == node2:
|
|
||||||
pubKeyBytes = node1
|
|
||||||
default:
|
|
||||||
return nil, fmt.Errorf("channel edge does not match expected node")
|
|
||||||
}
|
|
||||||
|
|
||||||
var tlvRecords []tlv.Record
|
var tlvRecords []tlv.Record
|
||||||
|
|
||||||
mpp, err := UnmarshalMPP(hop.MppRecord)
|
mpp, err := UnmarshalMPP(rpcHop.MppRecord)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &route.Hop{
|
return &route.Hop{
|
||||||
OutgoingTimeLock: hop.Expiry,
|
OutgoingTimeLock: rpcHop.Expiry,
|
||||||
AmtToForward: lnwire.MilliSatoshi(hop.AmtToForwardMsat),
|
AmtToForward: lnwire.MilliSatoshi(rpcHop.AmtToForwardMsat),
|
||||||
PubKeyBytes: pubKeyBytes,
|
PubKeyBytes: pubkey,
|
||||||
ChannelID: hop.ChanId,
|
ChannelID: rpcHop.ChanId,
|
||||||
TLVRecords: tlvRecords,
|
TLVRecords: tlvRecords,
|
||||||
LegacyPayload: !hop.TlvPayload,
|
LegacyPayload: !rpcHop.TlvPayload,
|
||||||
MPP: mpp,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnmarshallKnownPubkeyHop unmarshalls an rpc hop that contains the hop pubkey.
|
|
||||||
// The channel graph doesn't need to be queried because all information required
|
|
||||||
// for sending the payment is present.
|
|
||||||
func UnmarshallKnownPubkeyHop(hop *lnrpc.Hop) (*route.Hop, error) {
|
|
||||||
pubKey, err := hex.DecodeString(hop.PubKey)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("cannot decode pubkey %s", hop.PubKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
var pubKeyBytes [33]byte
|
|
||||||
copy(pubKeyBytes[:], pubKey)
|
|
||||||
|
|
||||||
var tlvRecords []tlv.Record
|
|
||||||
|
|
||||||
mpp, err := UnmarshalMPP(hop.MppRecord)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &route.Hop{
|
|
||||||
OutgoingTimeLock: hop.Expiry,
|
|
||||||
AmtToForward: lnwire.MilliSatoshi(hop.AmtToForwardMsat),
|
|
||||||
PubKeyBytes: pubKeyBytes,
|
|
||||||
ChannelID: hop.ChanId,
|
|
||||||
TLVRecords: tlvRecords,
|
|
||||||
LegacyPayload: !hop.TlvPayload,
|
|
||||||
MPP: mpp,
|
MPP: mpp,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshallHop unmarshalls an rpc hop that may or may not contain a node
|
// UnmarshallHop unmarshalls an rpc hop that may or may not contain a node
|
||||||
// pubkey.
|
// pubkey.
|
||||||
func (r *RouterBackend) UnmarshallHop(hop *lnrpc.Hop,
|
func (r *RouterBackend) UnmarshallHop(rpcHop *lnrpc.Hop,
|
||||||
prevNodePubKey [33]byte) (*route.Hop, error) {
|
prevNodePubKey [33]byte) (*route.Hop, error) {
|
||||||
|
|
||||||
if hop.PubKey == "" {
|
var pubKeyBytes [33]byte
|
||||||
// If no pub key is given of the hop, the local channel
|
if rpcHop.PubKey != "" {
|
||||||
// graph needs to be queried to complete the information
|
// Unmarshall the provided hop pubkey.
|
||||||
// necessary for routing.
|
pubKey, err := hex.DecodeString(rpcHop.PubKey)
|
||||||
return r.UnmarshallHopByChannelLookup(hop, prevNodePubKey)
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("cannot decode pubkey %s",
|
||||||
|
rpcHop.PubKey)
|
||||||
|
}
|
||||||
|
copy(pubKeyBytes[:], pubKey)
|
||||||
|
} else {
|
||||||
|
// If no pub key is given of the hop, the local channel graph
|
||||||
|
// needs to be queried to complete the information necessary for
|
||||||
|
// routing. Discard edge policies, because they may be nil.
|
||||||
|
node1, node2, err := r.FetchChannelEndpoints(rpcHop.ChanId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case prevNodePubKey == node1:
|
||||||
|
pubKeyBytes = node2
|
||||||
|
case prevNodePubKey == node2:
|
||||||
|
pubKeyBytes = node1
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("channel edge does not match " +
|
||||||
|
"expected node")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return UnmarshallKnownPubkeyHop(hop)
|
return UnmarshallHopWithPubkey(rpcHop, pubKeyBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshallRoute unmarshalls an rpc route. For hops that don't specify a
|
// UnmarshallRoute unmarshalls an rpc route. For hops that don't specify a
|
||||||
|
Loading…
Reference in New Issue
Block a user