mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-03-01 09:40:19 +01:00
state: return tx to be queued directly.
Instead of effect->broadcast_tx. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
e984df486d
commit
1a20e8094f
3 changed files with 30 additions and 25 deletions
41
state.c
41
state.c
|
@ -91,11 +91,20 @@ static void queue_pkt(Pkt **out, Pkt *pkt)
|
|||
*out = pkt;
|
||||
}
|
||||
|
||||
static void queue_tx_broadcast(struct bitcoin_tx **broadcast,
|
||||
struct bitcoin_tx *tx)
|
||||
{
|
||||
assert(!*broadcast);
|
||||
assert(tx);
|
||||
*broadcast = tx;
|
||||
}
|
||||
|
||||
enum command_status state(const tal_t *ctx,
|
||||
struct peer *peer,
|
||||
const enum state_input input,
|
||||
const union input *idata,
|
||||
Pkt **out,
|
||||
struct bitcoin_tx **broadcast,
|
||||
struct state_effect **effect)
|
||||
{
|
||||
Pkt *decline;
|
||||
|
@ -105,6 +114,7 @@ enum command_status state(const tal_t *ctx,
|
|||
enum command_status cstatus = CMD_NONE;
|
||||
|
||||
*out = NULL;
|
||||
*broadcast = NULL;
|
||||
|
||||
/* NULL-terminated linked list. */
|
||||
*effect = NULL;
|
||||
|
@ -199,8 +209,7 @@ enum command_status state(const tal_t *ctx,
|
|||
complete_cmd(peer, &cstatus, CMD_FAIL);
|
||||
goto err_start_unilateral_close;
|
||||
}
|
||||
add_effect(effect, broadcast_tx,
|
||||
bitcoin_anchor(ctx, peer));
|
||||
queue_tx_broadcast(broadcast, bitcoin_anchor(ctx, peer));
|
||||
add_effect(effect, watch,
|
||||
bitcoin_watch_anchor(ctx, peer,
|
||||
BITCOIN_ANCHOR_DEPTHOK,
|
||||
|
@ -617,8 +626,7 @@ enum command_status state(const tal_t *ctx,
|
|||
goto err_start_unilateral_close_already_closing;
|
||||
queue_pkt(out,
|
||||
pkt_close_ack(ctx, peer));
|
||||
add_effect(effect, broadcast_tx,
|
||||
bitcoin_close(ctx, peer));
|
||||
queue_tx_broadcast(broadcast, bitcoin_close(ctx, peer));
|
||||
change_peer_cond(peer, PEER_CLOSING, PEER_CLOSED);
|
||||
return next_state(peer, cstatus, STATE_CLOSE_WAIT_CLOSE);
|
||||
} else if (input_is(input, PKT_CLOSE)) {
|
||||
|
@ -630,8 +638,7 @@ enum command_status state(const tal_t *ctx,
|
|||
goto err_start_unilateral_close_already_closing;
|
||||
queue_pkt(out,
|
||||
pkt_close_ack(ctx, peer));
|
||||
add_effect(effect, broadcast_tx,
|
||||
bitcoin_close(ctx, peer));
|
||||
queue_tx_broadcast(broadcast, bitcoin_close(ctx, peer));
|
||||
set_peer_cond(peer, PEER_CLOSED);
|
||||
return next_state(peer, cstatus, STATE_CLOSE_WAIT_CLOSE);
|
||||
} else if (input_is(input, PKT_ERROR)) {
|
||||
|
@ -756,7 +763,7 @@ enum command_status state(const tal_t *ctx,
|
|||
& STATE_CLOSE_OURCOMMIT_BIT));
|
||||
tx = bitcoin_spend_ours(ctx, peer);
|
||||
/* Now we need to wait for our commit to be done. */
|
||||
add_effect(effect, broadcast_tx, tx);
|
||||
queue_tx_broadcast(broadcast, tx);
|
||||
add_effect(effect, watch,
|
||||
bitcoin_watch(ctx, tx,
|
||||
BITCOIN_SPEND_OURS_DONE));
|
||||
|
@ -793,7 +800,7 @@ enum command_status state(const tal_t *ctx,
|
|||
peer,
|
||||
idata->htlc);
|
||||
/* HTLC timed out, spend it back to us. */
|
||||
add_effect(effect, broadcast_tx, tx);
|
||||
queue_tx_broadcast(broadcast, tx);
|
||||
/* Don't unwatch yet; they could yet
|
||||
* try to spend, revealing rvalue. */
|
||||
|
||||
|
@ -809,7 +816,7 @@ enum command_status state(const tal_t *ctx,
|
|||
idata->htlc);
|
||||
|
||||
/* Spend it... */
|
||||
add_effect(effect, broadcast_tx, tx);
|
||||
queue_tx_broadcast(broadcast, tx);
|
||||
/* We're done when it gets buried. */
|
||||
add_effect(effect, watch_htlc_spend,
|
||||
htlc_spend_watch(ctx,
|
||||
|
@ -863,7 +870,7 @@ enum command_status state(const tal_t *ctx,
|
|||
*/
|
||||
if (input_is(input, BITCOIN_ANCHOR_THEIRSPEND)) {
|
||||
tx = bitcoin_spend_theirs(ctx, peer, idata->btc);
|
||||
add_effect(effect, broadcast_tx, tx);
|
||||
queue_tx_broadcast(broadcast, tx);
|
||||
add_effect(effect, watch,
|
||||
bitcoin_watch(ctx, tx,
|
||||
BITCOIN_SPEND_THEIRS_DONE));
|
||||
|
@ -885,7 +892,7 @@ enum command_status state(const tal_t *ctx,
|
|||
if (!tx)
|
||||
return next_state(peer, cstatus,
|
||||
STATE_ERR_INFORMATION_LEAK);
|
||||
add_effect(effect, broadcast_tx, tx);
|
||||
queue_tx_broadcast(broadcast, tx);
|
||||
add_effect(effect, watch,
|
||||
bitcoin_watch(ctx, tx,
|
||||
BITCOIN_STEAL_DONE));
|
||||
|
@ -973,7 +980,7 @@ start_unilateral_close:
|
|||
/* No more inputs, no more commands. */
|
||||
set_peer_cond(peer, PEER_CLOSED);
|
||||
tx = bitcoin_commit(ctx, peer);
|
||||
add_effect(effect, broadcast_tx, tx);
|
||||
queue_tx_broadcast(broadcast, tx);
|
||||
add_effect(effect, watch,
|
||||
bitcoin_watch_delayed(ctx, tx,
|
||||
BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED));
|
||||
|
@ -1003,7 +1010,7 @@ start_unilateral_close_already_closing:
|
|||
/* No more inputs, no more commands. */
|
||||
set_peer_cond(peer, PEER_CLOSED);
|
||||
tx = bitcoin_commit(ctx, peer);
|
||||
add_effect(effect, broadcast_tx, tx);
|
||||
queue_tx_broadcast(broadcast, tx);
|
||||
add_effect(effect, watch,
|
||||
bitcoin_watch_delayed(ctx, tx,
|
||||
BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED));
|
||||
|
@ -1025,7 +1032,7 @@ them_unilateral:
|
|||
/* No more inputs, no more commands. */
|
||||
set_peer_cond(peer, PEER_CLOSED);
|
||||
tx = bitcoin_spend_theirs(ctx, peer, idata->btc);
|
||||
add_effect(effect, broadcast_tx, tx);
|
||||
queue_tx_broadcast(broadcast, tx);
|
||||
add_effect(effect, watch,
|
||||
bitcoin_watch(ctx, tx,
|
||||
BITCOIN_SPEND_THEIRS_DONE));
|
||||
|
@ -1143,7 +1150,7 @@ fail_during_close:
|
|||
} else if (input_is(input, BITCOIN_ANCHOR_THEIRSPEND)) {
|
||||
/* A reorganization could make this happen. */
|
||||
tx = bitcoin_spend_theirs(ctx, peer, idata->btc);
|
||||
add_effect(effect, broadcast_tx, tx);
|
||||
queue_tx_broadcast(broadcast, tx);
|
||||
add_effect(effect, watch,
|
||||
bitcoin_watch(ctx, tx,
|
||||
BITCOIN_SPEND_THEIRS_DONE));
|
||||
|
@ -1166,7 +1173,7 @@ fail_during_close:
|
|||
if (!tx)
|
||||
return next_state(peer, cstatus,
|
||||
STATE_ERR_INFORMATION_LEAK);
|
||||
add_effect(effect, broadcast_tx, tx);
|
||||
queue_tx_broadcast(broadcast, tx);
|
||||
add_effect(effect, watch,
|
||||
bitcoin_watch(ctx, tx, BITCOIN_STEAL_DONE));
|
||||
/* Expect either close or steal to complete */
|
||||
|
@ -1191,7 +1198,7 @@ old_commit_spotted:
|
|||
if (!tx)
|
||||
return next_state(peer, cstatus,
|
||||
STATE_ERR_INFORMATION_LEAK);
|
||||
add_effect(effect, broadcast_tx, tx);
|
||||
queue_tx_broadcast(broadcast, tx);
|
||||
add_effect(effect, watch,
|
||||
bitcoin_watch(ctx, tx, BITCOIN_STEAL_DONE));
|
||||
return next_state(peer, cstatus, STATE_CLOSE_WAIT_STEAL);
|
||||
|
|
6
state.h
6
state.h
|
@ -7,7 +7,6 @@
|
|||
#include <stdbool.h>
|
||||
|
||||
enum state_effect_type {
|
||||
STATE_EFFECT_broadcast_tx,
|
||||
STATE_EFFECT_watch,
|
||||
STATE_EFFECT_unwatch,
|
||||
/* FIXME: Use a watch for this?. */
|
||||
|
@ -30,9 +29,6 @@ struct state_effect {
|
|||
|
||||
enum state_effect_type etype;
|
||||
union {
|
||||
/* Transaction to broadcast. */
|
||||
struct bitcoin_tx *broadcast_tx;
|
||||
|
||||
/* Event to watch for. */
|
||||
struct watch *watch;
|
||||
|
||||
|
@ -62,6 +58,7 @@ static inline bool state_is_error(enum state s)
|
|||
}
|
||||
|
||||
struct peer;
|
||||
struct bitcoin_tx;
|
||||
|
||||
static inline bool input_is_pkt(enum state_input input)
|
||||
{
|
||||
|
@ -81,6 +78,7 @@ enum command_status state(const tal_t *ctx,
|
|||
const enum state_input input,
|
||||
const union input *idata,
|
||||
Pkt **out,
|
||||
struct bitcoin_tx **broadcast,
|
||||
struct state_effect **effect);
|
||||
|
||||
/* Any CMD_SEND_HTLC_* */
|
||||
|
|
|
@ -1494,8 +1494,6 @@ static const char *apply_effects(struct peer *peer,
|
|||
*effects |= (1ULL << effect->etype);
|
||||
|
||||
switch (effect->etype) {
|
||||
case STATE_EFFECT_broadcast_tx:
|
||||
break;
|
||||
case STATE_EFFECT_watch:
|
||||
/* We can have multiple steals or spendtheirs
|
||||
in flight, so make exceptions for
|
||||
|
@ -1925,6 +1923,7 @@ static void try_input(const struct peer *peer,
|
|||
struct state_effect *effect;
|
||||
const char *problem;
|
||||
Pkt *output;
|
||||
struct bitcoin_tx *broadcast;
|
||||
const tal_t *ctx = tal(NULL, char);
|
||||
enum command_status cstatus;
|
||||
|
||||
|
@ -1936,7 +1935,7 @@ static void try_input(const struct peer *peer,
|
|||
copy.trail = &t;
|
||||
|
||||
eliminate_input(&hist->inputs_per_state[copy.state], i);
|
||||
cstatus = state(ctx, ©, i, idata, &output, &effect);
|
||||
cstatus = state(ctx, ©, i, idata, &output, &broadcast, &effect);
|
||||
|
||||
normalpath &= normal_path(i, peer->state, copy.state);
|
||||
errorpath |= error_path(i, peer->state, copy.state);
|
||||
|
@ -2350,11 +2349,12 @@ static enum state_input **map_inputs(void)
|
|||
if (!state_is_error(i)) {
|
||||
struct peer dummy;
|
||||
struct state_effect *effect;
|
||||
struct bitcoin_tx *dummy_tx;
|
||||
Pkt *dummy_pkt;
|
||||
memset(&dummy, 0, sizeof(dummy));
|
||||
dummy.state = i;
|
||||
state(ctx, &dummy, INPUT_NONE, NULL, &dummy_pkt,
|
||||
&effect);
|
||||
&dummy_tx, &effect);
|
||||
}
|
||||
inps[i] = mapping_inputs;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue