mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 07:04:22 +01:00
psbt: psbt_has_serial_id -> psbt_find_serial_id
Cleans up some awkward spots in the code, makes the footprint a bit neater Suggested-By: @rustyrussell
This commit is contained in:
@@ -416,28 +416,28 @@ void psbt_output_add_serial_id(struct wally_psbt_output *output,
|
|||||||
psbt_output_add_unknown(output, key, &bev, sizeof(bev));
|
psbt_output_add_unknown(output, key, &bev, sizeof(bev));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool psbt_has_serial_input(struct wally_psbt *psbt, u16 serial_id)
|
int psbt_find_serial_input(struct wally_psbt *psbt, u16 serial_id)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < psbt->num_inputs; i++) {
|
for (size_t i = 0; i < psbt->num_inputs; i++) {
|
||||||
u16 in_serial;
|
u16 in_serial;
|
||||||
if (!psbt_get_serial_id(&psbt->inputs[i].unknowns, &in_serial))
|
if (!psbt_get_serial_id(&psbt->inputs[i].unknowns, &in_serial))
|
||||||
continue;
|
continue;
|
||||||
if (in_serial == serial_id)
|
if (in_serial == serial_id)
|
||||||
return true;
|
return i;
|
||||||
}
|
}
|
||||||
return false;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool psbt_has_serial_output(struct wally_psbt *psbt, u16 serial_id)
|
int psbt_find_serial_output(struct wally_psbt *psbt, u16 serial_id)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < psbt->num_outputs; i++) {
|
for (size_t i = 0; i < psbt->num_outputs; i++) {
|
||||||
u16 out_serial;
|
u16 out_serial;
|
||||||
if (!psbt_get_serial_id(&psbt->outputs[i].unknowns, &out_serial))
|
if (!psbt_get_serial_id(&psbt->outputs[i].unknowns, &out_serial))
|
||||||
continue;
|
continue;
|
||||||
if (out_serial == serial_id)
|
if (out_serial == serial_id)
|
||||||
return true;
|
return i;
|
||||||
}
|
}
|
||||||
return false;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void psbt_input_add_max_witness_len(struct wally_psbt_input *input,
|
void psbt_input_add_max_witness_len(struct wally_psbt_input *input,
|
||||||
|
|||||||
@@ -106,23 +106,23 @@ void psbt_output_add_serial_id(struct wally_psbt_output *output,
|
|||||||
*/
|
*/
|
||||||
void psbt_sort_by_serial_id(struct wally_psbt *psbt);
|
void psbt_sort_by_serial_id(struct wally_psbt *psbt);
|
||||||
|
|
||||||
/* psbt_has_serial_input - Checks inputs for provided serial_id
|
/* psbt_find_serial_input - Checks inputs for provided serial_id
|
||||||
*
|
*
|
||||||
* @psbt - psbt's inputs to check
|
* @psbt - psbt's inputs to check
|
||||||
* @serial_id - id to look for
|
* @serial_id - id to look for
|
||||||
* Returns true if serial_id found.
|
*
|
||||||
|
* Returns index of input with matching serial if found or -1
|
||||||
*/
|
*/
|
||||||
WARN_UNUSED_RESULT bool
|
int psbt_find_serial_input(struct wally_psbt *psbt, u16 serial_id);
|
||||||
psbt_has_serial_input(struct wally_psbt *psbt, u16 serial_id);
|
|
||||||
|
|
||||||
/* psbt_has_serial_output - Checks outputs for provided serial_id
|
/* psbt_find_serial_output - Checks outputs for provided serial_id
|
||||||
*
|
*
|
||||||
* @psbt - psbt's outputs to check
|
* @psbt - psbt's outputs to check
|
||||||
* @serial_id - id to look for
|
* @serial_id - id to look for
|
||||||
* Returns true if serial_id found.
|
*
|
||||||
|
* Returns index of output with matching serial if found or -1
|
||||||
*/
|
*/
|
||||||
WARN_UNUSED_RESULT bool
|
int psbt_find_serial_output(struct wally_psbt *psbt, u16 serial_id);
|
||||||
psbt_has_serial_output(struct wally_psbt *psbt, u16 serial_id);
|
|
||||||
|
|
||||||
/* psbt_input_add_max_witness_len - Put a max witness len on a thing
|
/* psbt_input_add_max_witness_len - Put a max witness len on a thing
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -428,7 +428,7 @@ static void psbt_add_serials(struct wally_psbt *psbt, enum side opener)
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
while ((serial_id = pseudorand(serial_space)) % 2 != opener ||
|
while ((serial_id = pseudorand(serial_space)) % 2 != opener ||
|
||||||
psbt_has_serial_input(psbt, serial_id)) {
|
psbt_find_serial_input(psbt, serial_id) != -1) {
|
||||||
/* keep going; */
|
/* keep going; */
|
||||||
}
|
}
|
||||||
psbt_input_add_serial_id(&psbt->inputs[i], serial_id);
|
psbt_input_add_serial_id(&psbt->inputs[i], serial_id);
|
||||||
@@ -439,7 +439,7 @@ static void psbt_add_serials(struct wally_psbt *psbt, enum side opener)
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
while ((serial_id = pseudorand(serial_space)) % 2 != opener ||
|
while ((serial_id = pseudorand(serial_space)) % 2 != opener ||
|
||||||
psbt_has_serial_output(psbt, serial_id)) {
|
psbt_find_serial_output(psbt, serial_id) != -1) {
|
||||||
/* keep going; */
|
/* keep going; */
|
||||||
}
|
}
|
||||||
psbt_output_add_serial_id(&psbt->outputs[i], serial_id);
|
psbt_output_add_serial_id(&psbt->outputs[i], serial_id);
|
||||||
|
|||||||
@@ -629,7 +629,7 @@ static bool run_tx_interactive(struct state *state, struct wally_psbt **orig_psb
|
|||||||
* ...
|
* ...
|
||||||
* - it recieves a duplicate `serial_id`
|
* - it recieves a duplicate `serial_id`
|
||||||
*/
|
*/
|
||||||
if (psbt_has_serial_input(psbt, serial_id))
|
if (psbt_find_serial_input(psbt, serial_id) != -1)
|
||||||
peer_failed(state->pps, &state->channel_id,
|
peer_failed(state->pps, &state->channel_id,
|
||||||
"Duplicate serial_id rcvd. %u", serial_id);
|
"Duplicate serial_id rcvd. %u", serial_id);
|
||||||
|
|
||||||
@@ -703,7 +703,7 @@ static bool run_tx_interactive(struct state *state, struct wally_psbt **orig_psb
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case WIRE_TX_REMOVE_INPUT: {
|
case WIRE_TX_REMOVE_INPUT: {
|
||||||
bool input_found = false;
|
int input_index;
|
||||||
|
|
||||||
if (!fromwire_tx_remove_input(msg, &cid, &serial_id))
|
if (!fromwire_tx_remove_input(msg, &cid, &serial_id))
|
||||||
peer_failed(state->pps, &state->channel_id,
|
peer_failed(state->pps, &state->channel_id,
|
||||||
@@ -712,24 +712,21 @@ static bool run_tx_interactive(struct state *state, struct wally_psbt **orig_psb
|
|||||||
|
|
||||||
check_channel_id(state, &cid, &state->channel_id);
|
check_channel_id(state, &cid, &state->channel_id);
|
||||||
|
|
||||||
for (size_t i = 0; i < psbt->num_inputs; i++) {
|
/* BOLT-fe0351ca2cea3105c4f2eb18c571afca9d21c85b #2
|
||||||
u16 input_serial;
|
* The sending node:
|
||||||
if (!psbt_get_serial_id(&psbt->inputs[i].unknowns,
|
* - MUST NOT send a `tx_remove_input` for an
|
||||||
&input_serial)) {
|
* input which is not theirs */
|
||||||
peer_failed(state->pps, &state->channel_id,
|
if (serial_id % 2 != 0)
|
||||||
"No input added with serial_id %u",
|
peer_failed(state->pps, &state->channel_id,
|
||||||
serial_id);
|
"Invalid serial_id rcvd. %u", serial_id);
|
||||||
}
|
|
||||||
if (input_serial == serial_id) {
|
input_index = psbt_find_serial_input(psbt, serial_id);
|
||||||
psbt_rm_input(psbt, i);
|
if (input_index == -1)
|
||||||
input_found = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!input_found)
|
|
||||||
peer_failed(state->pps, &state->channel_id,
|
peer_failed(state->pps, &state->channel_id,
|
||||||
"No input added with serial_id %u",
|
"No input added with serial_id %u",
|
||||||
serial_id);
|
serial_id);
|
||||||
|
|
||||||
|
psbt_rm_input(psbt, input_index);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case WIRE_TX_ADD_OUTPUT: {
|
case WIRE_TX_ADD_OUTPUT: {
|
||||||
@@ -745,7 +742,18 @@ static bool run_tx_interactive(struct state *state, struct wally_psbt **orig_psb
|
|||||||
tal_hex(tmpctx, msg));
|
tal_hex(tmpctx, msg));
|
||||||
check_channel_id(state, &cid, &state->channel_id);
|
check_channel_id(state, &cid, &state->channel_id);
|
||||||
|
|
||||||
if (psbt_has_serial_output(psbt, serial_id))
|
/* BOLT-fe0351ca2cea3105c4f2eb18c571afca9d21c85b #2
|
||||||
|
* The receiving node:
|
||||||
|
* ...
|
||||||
|
* - MUST fail the transaction collaboration if:
|
||||||
|
* ...
|
||||||
|
* - it receives a `serial_id` from the peer with the
|
||||||
|
* incorrect parity */
|
||||||
|
if (serial_id % 2 != 0)
|
||||||
|
peer_failed(state->pps, &state->channel_id,
|
||||||
|
"Invalid serial_id rcvd. %u", serial_id);
|
||||||
|
|
||||||
|
if (psbt_find_serial_output(psbt, serial_id) != -1)
|
||||||
peer_failed(state->pps, &state->channel_id,
|
peer_failed(state->pps, &state->channel_id,
|
||||||
"Duplicate serial_id rcvd. %u", serial_id);
|
"Duplicate serial_id rcvd. %u", serial_id);
|
||||||
amt = amount_sat(value);
|
amt = amount_sat(value);
|
||||||
@@ -754,7 +762,7 @@ static bool run_tx_interactive(struct state *state, struct wally_psbt **orig_psb
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case WIRE_TX_REMOVE_OUTPUT: {
|
case WIRE_TX_REMOVE_OUTPUT: {
|
||||||
bool out_found = false;
|
int output_index;
|
||||||
|
|
||||||
if (!fromwire_tx_remove_output(msg, &cid, &serial_id))
|
if (!fromwire_tx_remove_output(msg, &cid, &serial_id))
|
||||||
peer_failed(state->pps, &state->channel_id,
|
peer_failed(state->pps, &state->channel_id,
|
||||||
@@ -763,24 +771,20 @@ static bool run_tx_interactive(struct state *state, struct wally_psbt **orig_psb
|
|||||||
|
|
||||||
check_channel_id(state, &cid, &state->channel_id);
|
check_channel_id(state, &cid, &state->channel_id);
|
||||||
|
|
||||||
for (size_t i = 0; i < psbt->num_outputs; i++) {
|
/* BOLT-fe0351ca2cea3105c4f2eb18c571afca9d21c85b #2
|
||||||
u16 output_serial;
|
* The sending node:
|
||||||
if (!psbt_get_serial_id(&psbt->outputs[i].unknowns,
|
* - MUST NOT send a `tx_remove_ouput` for an
|
||||||
&output_serial)) {
|
* input which is not theirs */
|
||||||
peer_failed(state->pps, &state->channel_id,
|
if (serial_id % 2 != 0)
|
||||||
"No output added with serial_id %u",
|
peer_failed(state->pps, &state->channel_id,
|
||||||
serial_id);
|
"Invalid serial_id rcvd. %u", serial_id);
|
||||||
}
|
|
||||||
if (output_serial == serial_id) {
|
output_index = psbt_find_serial_output(psbt, serial_id);
|
||||||
psbt_rm_output(psbt, i);
|
if (output_index == -1)
|
||||||
out_found = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!out_found)
|
|
||||||
peer_failed(state->pps, &state->channel_id,
|
peer_failed(state->pps, &state->channel_id,
|
||||||
"No output added with serial_id %u",
|
"No output added with serial_id %u",
|
||||||
serial_id);
|
serial_id);
|
||||||
|
psbt_rm_output(psbt, output_index);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case WIRE_TX_COMPLETE:
|
case WIRE_TX_COMPLETE:
|
||||||
|
|||||||
Reference in New Issue
Block a user