sphinx: Decode payload and place shortcuts in the route-step

We'll need to pass them around anyway, so just make them easier to access by
doing a bit more to `process_onionpacket`.
This commit is contained in:
Christian Decker
2019-11-19 12:51:24 +01:00
committed by Rusty Russell
parent d69a43780c
commit baffa84291
3 changed files with 56 additions and 0 deletions

View File

@@ -683,6 +683,52 @@ struct onionpacket *create_onionpacket(
return packet;
}
/**
* Helper to extract fields from the legacy or tlv payload into the top-level
* struct.
*/
static void route_step_decode(struct route_step *rs)
{
switch (rs->type) {
case SPHINX_V0_PAYLOAD:
rs->amt_to_forward = &rs->payload.v0.amt_forward;
rs->outgoing_cltv = &rs->payload.v0.outgoing_cltv;
if (rs->nextcase == ONION_FORWARD) {
rs->forward_channel = &rs->payload.v0.channel_id;
} else {
rs->forward_channel = NULL;
}
break;
case SPHINX_TLV_PAYLOAD:
if (rs->payload.tlv->amt_to_forward) {
rs->amt_to_forward = tal(rs, struct amount_msat);
amount_msat_from_u64(
rs->amt_to_forward,
rs->payload.tlv->amt_to_forward->amt_to_forward);
} else {
rs->amt_to_forward = NULL;
}
if (rs->payload.tlv->outgoing_cltv_value) {
rs->outgoing_cltv =
&rs->payload.tlv->outgoing_cltv_value
->outgoing_cltv_value;
} else {
rs->outgoing_cltv = NULL;
}
if (rs->payload.tlv->short_channel_id)
rs->forward_channel = &rs->payload.tlv->short_channel_id
->short_channel_id;
else
rs->forward_channel = NULL;
break;
case SPHINX_INVALID_PAYLOAD:
case SPHINX_RAW_PAYLOAD:
abort();
}
}
/*
* Given an onionpacket msg extract the information for the current
* node and unwrap the remainder so that the node can forward it.
@@ -754,6 +800,8 @@ struct route_step *process_onionpacket(
step->nextcase = ONION_FORWARD;
}
route_step_decode(step);
return step;
}