state: use htlc_onchain structure

Turns out that we want to pass information about the commit info, the
HTLC number and (sometimes) the R value, so create a struct for that.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2016-05-03 11:04:20 +09:30
parent bccd10c6d8
commit c4713a6ed5
3 changed files with 45 additions and 30 deletions

View File

@ -1094,7 +1094,7 @@ bool peer_watch_their_htlc_outputs(struct peer *peer,
FIXME_STUB(peer);
}
void peer_unwatch_htlc_output(struct peer *peer,
const struct htlc *htlc,
const struct htlc_onchain *htlc_onchain,
enum state_input all_done)
{
FIXME_STUB(peer);
@ -1105,13 +1105,13 @@ void peer_unwatch_all_htlc_outputs(struct peer *peer)
}
void peer_watch_htlc_spend(struct peer *peer,
const struct bitcoin_tx *tx,
const struct htlc *htlc,
const struct htlc_onchain *htlc_onchain,
enum state_input done)
{
/* FIXME! */
}
void peer_unwatch_htlc_spend(struct peer *peer,
const struct htlc *htlc,
const struct htlc_onchain *htlc_onchain,
enum state_input all_done)
{
FIXME_STUB(peer);
@ -1122,8 +1122,7 @@ void peer_unexpected_pkt(struct peer *peer, const Pkt *pkt)
}
/* An on-chain transaction revealed an R value. */
const struct htlc *peer_tx_revealed_r_value(struct peer *peer,
const struct bitcoin_event *btc)
void peer_tx_revealed_r_value(struct peer *peer, const struct htlc_onchain *htlc_onchain)
{
FIXME_STUB(peer);
}
@ -1261,14 +1260,14 @@ const struct bitcoin_tx *bitcoin_commit(struct peer *peer)
/* Create a HTLC refund collection */
const struct bitcoin_tx *bitcoin_htlc_timeout(const struct peer *peer,
const struct htlc *htlc)
const struct htlc_onchain *htlc_onchain)
{
FIXME_STUB(peer);
}
/* Create a HTLC collection */
const struct bitcoin_tx *bitcoin_htlc_spend(const struct peer *peer,
const struct htlc *htlc)
const struct htlc_onchain *htlc_onchain)
{
FIXME_STUB(peer);
}

33
state.c
View File

@ -661,58 +661,67 @@ enum command_status state(struct peer *peer,
return next_state(peer, cstatus,
BITS_TO_STATE(bits));
} else if (input_is(input, BITCOIN_HTLC_TOTHEM_SPENT)) {
const struct htlc *htlc;
/* They revealed R value. */
htlc = peer_tx_revealed_r_value(peer, idata->btc);
peer_tx_revealed_r_value(peer,
idata->htlc_onchain);
/* We don't care any more. */
peer_unwatch_htlc_output(peer, htlc,
peer_unwatch_htlc_output(peer,
idata->htlc_onchain,
INPUT_NO_MORE_HTLCS);
return unchanged_state(cstatus);
} else if (input_is(input, BITCOIN_HTLC_TOTHEM_TIMEOUT)){
tx = bitcoin_htlc_timeout(peer, idata->htlc);
tx = bitcoin_htlc_timeout(peer,
idata->htlc_onchain);
/* HTLC timed out, spend it back to us. */
queue_tx_broadcast(broadcast, tx);
/* Don't unwatch yet; they could yet
* try to spend, revealing rvalue. */
/* We're done when that gets buried. */
peer_watch_htlc_spend(peer, tx, idata->htlc,
peer_watch_htlc_spend(peer, tx,
idata->htlc_onchain,
BITCOIN_HTLC_RETURN_SPEND_DONE);
return unchanged_state(cstatus);
} else if (input_is(input, INPUT_RVALUE)) {
tx = bitcoin_htlc_spend(peer,
idata->htlc);
idata->htlc_onchain);
/* Spend it... */
queue_tx_broadcast(broadcast, tx);
/* We're done when it gets buried. */
peer_watch_htlc_spend(peer, tx, idata->htlc,
peer_watch_htlc_spend(peer, tx,
idata->htlc_onchain,
BITCOIN_HTLC_FULFILL_SPEND_DONE);
/* Don't care about this one any more. */
peer_unwatch_htlc_output(peer, idata->htlc,
peer_unwatch_htlc_output(peer,
idata->htlc_onchain,
INPUT_NO_MORE_HTLCS);
return unchanged_state(cstatus);
} else if (input_is(input, BITCOIN_HTLC_FULFILL_SPEND_DONE)) {
/* Stop watching spend, send
* INPUT_NO_MORE_HTLCS when done. */
peer_unwatch_htlc_spend(peer, idata->htlc,
peer_unwatch_htlc_spend(peer,
idata->htlc_onchain,
INPUT_NO_MORE_HTLCS);
return unchanged_state(cstatus);
} else if (input_is(input, BITCOIN_HTLC_RETURN_SPEND_DONE)) {
/* Stop watching spend, send
* INPUT_NO_MORE_HTLCS when done. */
peer_unwatch_htlc_spend(peer, idata->htlc,
peer_unwatch_htlc_spend(peer,
idata->htlc_onchain,
INPUT_NO_MORE_HTLCS);
/* Don't need to watch the HTLC output any more,
* either. */
peer_unwatch_htlc_output(peer, idata->htlc,
peer_unwatch_htlc_output(peer,
idata->htlc_onchain,
INPUT_NO_MORE_HTLCS);
return unchanged_state(cstatus);
} else if (input_is(input, BITCOIN_HTLC_TOUS_TIMEOUT)) {
/* They can spend, we no longer care
* about this HTLC. */
peer_unwatch_htlc_output(peer, idata->htlc,
peer_unwatch_htlc_output(peer,
idata->htlc_onchain,
INPUT_NO_MORE_HTLCS);
return unchanged_state(cstatus);
}

29
state.h
View File

@ -31,8 +31,15 @@ union input {
Pkt *pkt;
struct command *cmd;
struct bitcoin_event *btc;
struct htlc *htlc;
struct htlc_progress *htlc_prog;
struct htlc_onchain {
/* Which commitment we using. */
struct commit_info *ci;
/* Which HTLC. */
size_t i;
/* The rvalue (or NULL). */
u8 *r;
} *htlc_onchain;
};
enum command_status state(struct peer *peer,
@ -66,8 +73,8 @@ struct signature;
void peer_unexpected_pkt(struct peer *peer, const Pkt *pkt);
/* An on-chain transaction revealed an R value. */
const struct htlc *peer_tx_revealed_r_value(struct peer *peer,
const struct bitcoin_event *btc);
void peer_tx_revealed_r_value(struct peer *peer,
const struct htlc_onchain *htlc_onchain);
/* Send various kinds of packets */
void queue_pkt_open(struct peer *peer, OpenChannel__AnchorOffer anchor);
@ -246,11 +253,11 @@ bool peer_watch_their_htlc_outputs(struct peer *peer,
/**
* peer_unwatch_htlc_output: stop watching an HTLC
* @peer: the state data for this peer.
* @htlc: the htlc to stop watching
* @htlc_onchain: the htlc to stop watching
* @all_done: input to give if we're not watching any outputs anymore.
*/
void peer_unwatch_htlc_output(struct peer *peer,
const struct htlc *htlc,
const struct htlc_onchain *htlc_onchain,
enum state_input all_done);
/**
@ -263,22 +270,22 @@ void peer_unwatch_all_htlc_outputs(struct peer *peer);
* peer_watch_htlc_spend: watch our spend of an HTLC output
* @peer: the state data for this peer.
* @tx: the commitment tx
* @htlc: the htlc the tx is spending an output of
* @htlc_onchain: the htlc the tx is spending an output of
* @done: input to give when it's completely buried.
*/
void peer_watch_htlc_spend(struct peer *peer,
const struct bitcoin_tx *tx,
const struct htlc *htlc,
const struct htlc_onchain *htlc_onchain,
enum state_input done);
/**
* peer_unwatch_htlc_spend: stop watching our HTLC spend
* @peer: the state data for this peer.
* @htlc: the htlc to stop watching the spend for.
* @htlc_onchain: the htlc to stop watching the spend for.
* @all_done: input to give if we're not watching anything anymore.
*/
void peer_unwatch_htlc_spend(struct peer *peer,
const struct htlc *htlc,
const struct htlc_onchain *htlc_onchain,
enum state_input all_done);
/**
@ -324,10 +331,10 @@ const struct bitcoin_tx *bitcoin_commit(struct peer *peer);
/* Create a HTLC refund collection */
const struct bitcoin_tx *bitcoin_htlc_timeout(const struct peer *peer,
const struct htlc *htlc);
const struct htlc_onchain *htlc_onchain);
/* Create a HTLC collection */
const struct bitcoin_tx *bitcoin_htlc_spend(const struct peer *peer,
const struct htlc *htlc);
const struct htlc_onchain *htlc_onchain);
#endif /* LIGHTNING_STATE_H */