Browse Source

channel_states: fold all the ONCHAIND states into one.

The billboard is now far more useful to tell what's going on, and this
gets us closer to a state == owner mapping.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 7 years ago
committed by Christian Decker
parent
commit
cccdb53bec
  1. 26
      README.md
  2. 13
      lightningd/channel.h
  3. 9
      lightningd/channel_state.h
  4. 17
      lightningd/onchain_control.c
  5. 5
      lightningd/peer_control.c
  6. 17
      onchaind/onchain.c
  7. 5
      onchaind/onchain_wire.csv
  8. 78
      tests/test_lightningd.py
  9. 2
      wallet/db.c

26
README.md

@ -111,16 +111,22 @@ You can check the status of the channel using `cli/lightning-cli listpeers`, whi
### Different states
States starting with `ONCHAIND` mean that the channel has been closed and an onchain transaction exists reflecting the resulting balances
* `ONCHAIND_OUR_UNILATERAL` > Closed by you without cooperation of the counterparty
* `ONCHAIND_THEIR_UNILATERAL` > Closed by the counterparty without your cooperation
* `ONCHAIND_MUTUAL` > Negotiated closing by both sides
States starting with `CHANNELD` mean that funds are not available onchain, and from that moment they can only be moved offchain, this is, through the Lightning Network
* `CHANNELD_AWAITING_LOCKIN` > Waiting for confirmation of the channel funding transaction
* `CHANNELD_NORMAL` > Channel is active
The `GOSSIPING` state means that you are connected to a peer but there is no payment channel yet.
* `GOSSIPING` means that you are connected to a peer but there is no
payment channel yet.
* `OPENINGD` means that `lightning_openingd` is negotiating channel opening.
* `CHANNELD_AWAITING_LOCKIN` means that `lightning_channeld` is waiting until
the minimum number of confirmation on the channel funding transaction.
* `CHANNELD_NORMAL` means your channel is operating normally.
* `CHANNELD_SHUTTING_DOWN` means one or both sides have asked to shut down the
channel, and we're waiting for existing HTLCs to clear.
* `CLOSINGD_SIGEXCHANGE` means we're trying to negotiate the fee for the mutual close transaction.
* `CLOSINGD_COMPLETE` means we've broadcast our mutual close
transaction (which spends the funding transaction) , but haven't seen it in a block yet.
* `FUNDING_SPEND_SEEN` means we've seen the funding transaction spent.
* `ONCHAIN` means that the `lightning_onchaind` is tracking the onchain closing of the channel.
All these states have more information about what's going on in the
`status` field in `listpeers`.
### Sending and receiving payments

13
lightningd/channel.h

@ -171,20 +171,9 @@ static inline bool channel_fees_can_change(const struct channel *channel)
|| channel->state == CHANNELD_SHUTTING_DOWN;
}
static inline bool channel_can_remove_htlc(const struct channel *channel)
{
return channel->state == CHANNELD_NORMAL
|| channel->state == CHANNELD_SHUTTING_DOWN
|| channel->state == ONCHAIND_THEIR_UNILATERAL
|| channel->state == ONCHAIND_OUR_UNILATERAL;
}
static inline bool channel_state_on_chain(enum channel_state state)
{
return state == ONCHAIND_CHEATED
|| state == ONCHAIND_THEIR_UNILATERAL
|| state == ONCHAIND_OUR_UNILATERAL
|| state == ONCHAIND_MUTUAL;
return state == ONCHAIN;
}
static inline bool channel_on_chain(const struct channel *channel)

9
lightningd/channel_state.h

@ -22,12 +22,9 @@ enum channel_state {
/* We've seen the funding spent, we're waiting for onchaind. */
FUNDING_SPEND_SEEN,
/* Various onchain states. */
ONCHAIND_CHEATED,
ONCHAIND_THEIR_UNILATERAL,
ONCHAIND_OUR_UNILATERAL,
ONCHAIND_MUTUAL
/* On chain */
ONCHAIN
};
#define CHANNEL_STATE_MAX ONCHAIND_MUTUAL
#define CHANNEL_STATE_MAX ONCHAIN
#endif /* LIGHTNING_LIGHTNINGD_CHANNEL_STATE_H */

17
lightningd/onchain_control.c

@ -48,21 +48,8 @@ static void onchaind_tell_fulfill(struct channel *channel)
static void handle_onchain_init_reply(struct channel *channel, const u8 *msg)
{
u8 state;
if (!fromwire_onchain_init_reply(msg, &state)) {
channel_internal_error(channel, "Invalid onchain_init_reply");
return;
}
if (!channel_state_on_chain(state)) {
channel_internal_error(channel,
"Invalid onchain_init_reply state %u (%s)",
state, channel_state_str(state));
return;
}
channel_set_state(channel, FUNDING_SPEND_SEEN, state);
/* FIXME: We may already be ONCHAIN state when we implement restart! */
channel_set_state(channel, FUNDING_SPEND_SEEN, ONCHAIN);
/* Tell it about any preimages we know. */
onchaind_tell_fulfill(channel);

5
lightningd/peer_control.c

@ -344,11 +344,8 @@ void peer_connected(struct lightningd *ld, const u8 *msg,
#endif
switch (channel->state) {
case ONCHAIND_CHEATED:
case ONCHAIND_THEIR_UNILATERAL:
case ONCHAIND_OUR_UNILATERAL:
case ONCHAIN:
case FUNDING_SPEND_SEEN:
case ONCHAIND_MUTUAL:
case CLOSINGD_COMPLETE:
/* Channel is active! */
abort();

17
onchaind/onchain.c

@ -1051,16 +1051,16 @@ static void wait_for_resolved(struct tracked_output **outs)
take(towire_onchain_all_irrevocably_resolved(outs)));
}
static void set_state(enum channel_state state)
static void init_reply(const char *what)
{
wire_sync_write(REQ_FD, take(towire_onchain_init_reply(NULL, state)));
peer_billboard(true, what);
wire_sync_write(REQ_FD, take(towire_onchain_init_reply(NULL)));
}
static void handle_mutual_close(const struct bitcoin_txid *txid,
struct tracked_output **outs)
{
set_state(ONCHAIND_MUTUAL);
peer_billboard(true, "Tracking mutual close transaction");
init_reply("Tracking mutual close transaction");
/* BOLT #5:
*
@ -1269,8 +1269,7 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx,
struct keyset *ks;
size_t i;
set_state(ONCHAIND_OUR_UNILATERAL);
peer_billboard(true, "Tracking our own unilateral close");
init_reply("Tracking our own unilateral close");
init_feerate_range(outs[0]->satoshi, tx);
@ -1571,8 +1570,7 @@ static void handle_their_cheat(const struct bitcoin_tx *tx,
struct privkey per_commitment_privkey;
struct pubkey per_commitment_point;
set_state(ONCHAIND_CHEATED);
peer_billboard(true, "Tracking their illegal close: taking all funds");
init_reply("Tracking their illegal close: taking all funds");
init_feerate_range(outs[0]->satoshi, tx);
@ -1832,8 +1830,7 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx,
struct keyset *ks;
size_t i;
set_state(ONCHAIND_THEIR_UNILATERAL);
peer_billboard(true, "Tracking their unilateral close");
init_reply("Tracking their unilateral close");
init_feerate_range(outs[0]->satoshi, tx);

5
onchaind/onchain_wire.csv

@ -40,9 +40,8 @@ onchain_htlc,,htlc,struct htlc_stub
onchain_htlc,,tell_if_missing,bool
onchain_htlc,,tell_immediately,bool
# This sets what the state is, depending on tx.
# This says we're ready; give us preimages.
onchain_init_reply,5101
onchain_init_reply,,state,u8
# onchaind->master: Send out a tx.
onchain_broadcast_tx,5003
@ -87,4 +86,4 @@ onchain_add_utxo,5012
onchain_add_utxo,,prev_out_tx,struct bitcoin_txid
onchain_add_utxo,,prev_out_index,u32
onchain_add_utxo,,per_commit_point,struct pubkey
onchain_add_utxo,,value,u64
onchain_add_utxo,,value,u64

Can't render this file because it has a wrong number of fields in line 2.

78
tests/test_lightningd.py

@ -1052,10 +1052,10 @@ class LightningDTests(BaseLightningDTests):
assert closetxid in set([o['txid'] for o in l1.rpc.listfunds()['outputs']])
assert closetxid in set([o['txid'] for o in l2.rpc.listfunds()['outputs']])
wait_for(lambda: l1.rpc.listpeers(l2.info['id'])['peers'][0]['channels'][0]['status'] == ['CLOSINGD_SIGEXCHANGE:We agreed on a closing fee of 5430 satoshi', 'ONCHAIND_MUTUAL:Tracking mutual close transaction', 'ONCHAIND_MUTUAL:All outputs resolved: waiting 99 more blocks before forgetting channel'])
wait_for(lambda: l1.rpc.listpeers(l2.info['id'])['peers'][0]['channels'][0]['status'] == ['CLOSINGD_SIGEXCHANGE:We agreed on a closing fee of 5430 satoshi', 'ONCHAIND:Tracking mutual close transaction', 'ONCHAIND_MUTUAL:All outputs resolved: waiting 99 more blocks before forgetting channel'])
l1.bitcoin.rpc.generate(9)
wait_for(lambda: l1.rpc.listpeers(l2.info['id'])['peers'][0]['channels'][0]['status'] == ['CLOSINGD_SIGEXCHANGE:We agreed on a closing fee of 5430 satoshi', 'ONCHAIND_MUTUAL:Tracking mutual close transaction', 'ONCHAIND_MUTUAL:All outputs resolved: waiting 90 more blocks before forgetting channel'])
wait_for(lambda: l1.rpc.listpeers(l2.info['id'])['peers'][0]['channels'][0]['status'] == ['CLOSINGD_SIGEXCHANGE:We agreed on a closing fee of 5430 satoshi', 'ONCHAIND:Tracking mutual close transaction', 'ONCHAIND_MUTUAL:All outputs resolved: waiting 90 more blocks before forgetting channel'])
# Make sure both have forgotten about it
l1.bitcoin.rpc.generate(90)
@ -1131,9 +1131,9 @@ class LightningDTests(BaseLightningDTests):
bitcoind.generate_block(1)
for p in peers:
p.daemon.wait_for_log(' to ONCHAIND_MUTUAL')
l1.daemon.wait_for_logs([' to ONCHAIND_MUTUAL'] * num_peers)
p.daemon.wait_for_log(' to ONCHAIN')
wait_for(lambda: p.rpc.listpeers(l1.info['id'])['peers'][0]['channels'][0]['status'][1] == 'ONCHAIND:Tracking mutual close transaction')
l1.daemon.wait_for_logs([' to ONCHAIN'] * num_peers)
@unittest.skipIf(not DEVELOPER, "needs DEVELOPER=1")
def test_permfail(self):
@ -1166,19 +1166,19 @@ class LightningDTests(BaseLightningDTests):
bitcoind.generate_block(1)
l1.daemon.wait_for_log('Their unilateral tx, old commit point')
l1.daemon.wait_for_log(' to ONCHAIND_THEIR_UNILATERAL')
l2.daemon.wait_for_log(' to ONCHAIND_OUR_UNILATERAL')
l1.daemon.wait_for_log(' to ONCHAIN')
l2.daemon.wait_for_log(' to ONCHAIN')
l2.daemon.wait_for_log('Propose handling OUR_UNILATERAL/DELAYED_OUTPUT_TO_US by OUR_DELAYED_RETURN_TO_WALLET (.*) in 5 blocks')
wait_for(lambda: l1.rpc.listpeers(l2.info['id'])['peers'][0]['channels'][0]['status']
== ['CHANNELD_NORMAL:Received error from peer: channel ALL: Internal error: Failing due to dev-fail command',
'ONCHAIND_THEIR_UNILATERAL:Tracking their unilateral close',
'ONCHAIND_THEIR_UNILATERAL:All outputs resolved: waiting 99 more blocks before forgetting channel'])
'ONCHAIND:Tracking their unilateral close',
'ONCHAIND:All outputs resolved: waiting 99 more blocks before forgetting channel'])
billboard = l2.rpc.listpeers(l1.info['id'])['peers'][0]['channels'][0]['status']
assert len(billboard) == 2
assert billboard[0] == 'ONCHAIND_OUR_UNILATERAL:Tracking our own unilateral close'
assert re.fullmatch('ONCHAIND_OUR_UNILATERAL:.* outputs unresolved: in 4 blocks will spend DELAYED_OUTPUT_TO_US \(.*:0\) using OUR_DELAYED_RETURN_TO_WALLET', billboard[1])
assert billboard[0] == 'ONCHAIND:Tracking our own unilateral close'
assert re.fullmatch('ONCHAIND:.* outputs unresolved: in 4 blocks will spend DELAYED_OUTPUT_TO_US \(.*:0\) using OUR_DELAYED_RETURN_TO_WALLET', billboard[1])
# Now, mine 4 blocks so it sends out the spending tx.
bitcoind.generate_block(4)
@ -1191,7 +1191,7 @@ class LightningDTests(BaseLightningDTests):
bitcoind.generate_block(95)
wait_forget_channels(l1)
wait_for(lambda: l2.rpc.listpeers(l1.info['id'])['peers'][0]['channels'][0]['status'] == ['ONCHAIND_OUR_UNILATERAL:Tracking our own unilateral close', 'ONCHAIND_OUR_UNILATERAL:All outputs resolved: waiting 5 more blocks before forgetting channel'], timeout=1)
wait_for(lambda: l2.rpc.listpeers(l1.info['id'])['peers'][0]['channels'][0]['status'] == ['ONCHAIND:Tracking our own unilateral close', 'ONCHAIND:All outputs resolved: waiting 5 more blocks before forgetting channel'], timeout=1)
# Now, 100 blocks l2 should be done.
bitcoind.generate_block(5)
@ -1226,8 +1226,8 @@ class LightningDTests(BaseLightningDTests):
l1.daemon.wait_for_log('permfail')
l1.daemon.wait_for_log('sendrawtx exit 0')
l1.bitcoin.generate_block(1)
l1.daemon.wait_for_log(' to ONCHAIND_OUR_UNILATERAL')
l2.daemon.wait_for_log(' to ONCHAIND_THEIR_UNILATERAL')
l1.daemon.wait_for_log(' to ONCHAIN')
l2.daemon.wait_for_log(' to ONCHAIN')
# 10 later, l1 should collect its to-self payment.
bitcoind.generate_block(10)
@ -1255,8 +1255,8 @@ class LightningDTests(BaseLightningDTests):
l1.daemon.wait_for_log('sendrawtx exit 0')
l1.bitcoin.generate_block(1)
l1.daemon.wait_for_log(' to ONCHAIND_OUR_UNILATERAL')
l2.daemon.wait_for_log(' to ONCHAIND_THEIR_UNILATERAL')
l1.daemon.wait_for_log(' to ONCHAIN')
l2.daemon.wait_for_log(' to ONCHAIN')
# 10 later, l1 should collect its to-self payment.
bitcoind.generate_block(10)
@ -1316,8 +1316,8 @@ class LightningDTests(BaseLightningDTests):
l1.daemon.wait_for_log('permfail')
l1.daemon.wait_for_log('sendrawtx exit 0')
l1.bitcoin.generate_block(1)
l1.daemon.wait_for_log(' to ONCHAIND_OUR_UNILATERAL')
l2.daemon.wait_for_log(' to ONCHAIND_THEIR_UNILATERAL')
l1.daemon.wait_for_log(' to ONCHAIN')
l2.daemon.wait_for_log(' to ONCHAIN')
# We use 3 blocks for "reasonable depth"
bitcoind.generate_block(3)
@ -1369,8 +1369,8 @@ class LightningDTests(BaseLightningDTests):
l1.daemon.wait_for_log('permfail')
l1.daemon.wait_for_log('sendrawtx exit 0')
l1.bitcoin.generate_block(1)
l1.daemon.wait_for_log(' to ONCHAIND_OUR_UNILATERAL')
l2.daemon.wait_for_log(' to ONCHAIND_THEIR_UNILATERAL')
l1.daemon.wait_for_log(' to ONCHAIN')
l2.daemon.wait_for_log(' to ONCHAIN')
# Wait for timeout.
l1.daemon.wait_for_log('Propose handling OUR_UNILATERAL/DELAYED_OUTPUT_TO_US by OUR_DELAYED_RETURN_TO_WALLET .* in 5 blocks')
@ -1448,8 +1448,8 @@ class LightningDTests(BaseLightningDTests):
# l2 will drop to chain.
l2.daemon.wait_for_log('sendrawtx exit 0')
l1.bitcoin.generate_block(1)
l2.daemon.wait_for_log(' to ONCHAIND_OUR_UNILATERAL')
l1.daemon.wait_for_log(' to ONCHAIND_THEIR_UNILATERAL')
l2.daemon.wait_for_log(' to ONCHAIN')
l1.daemon.wait_for_log(' to ONCHAIN')
l2.daemon.wait_for_log('OUR_UNILATERAL/THEIR_HTLC')
# l2 should fulfill HTLC onchain, and spend to-us (any order)
@ -1524,7 +1524,7 @@ class LightningDTests(BaseLightningDTests):
bitcoind.rpc.sendrawtransaction(tx)
bitcoind.generate_block(1)
l2.daemon.wait_for_log(' to ONCHAIND_CHEATED')
l2.daemon.wait_for_log(' to ONCHAIN')
# FIXME: l1 should try to stumble along!
wait_for(lambda: len(l2.getactivechannels()) == 0)
@ -1587,7 +1587,7 @@ class LightningDTests(BaseLightningDTests):
bitcoind.rpc.sendrawtransaction(tx)
bitcoind.generate_block(1)
l2.daemon.wait_for_log(' to ONCHAIND_CHEATED')
l2.daemon.wait_for_log(' to ONCHAIN')
# FIXME: l1 should try to stumble along!
# l2 should spend all of the outputs (except to-us).
@ -1623,8 +1623,8 @@ class LightningDTests(BaseLightningDTests):
l2.daemon.wait_for_log('sendrawtx exit 0')
bitcoind.generate_block(1)
l1.daemon.wait_for_log('Their unilateral tx, new commit point')
l1.daemon.wait_for_log(' to ONCHAIND_THEIR_UNILATERAL')
l2.daemon.wait_for_log(' to ONCHAIND_OUR_UNILATERAL')
l1.daemon.wait_for_log(' to ONCHAIN')
l2.daemon.wait_for_log(' to ONCHAIN')
l2.daemon.wait_for_log('Propose handling OUR_UNILATERAL/THEIR_HTLC by THEIR_HTLC_TIMEOUT_TO_THEM \\(IGNORING\\) in 5 blocks')
l1.daemon.wait_for_log('Propose handling THEIR_UNILATERAL/OUR_HTLC by OUR_HTLC_TIMEOUT_TO_US (.*) in 5 blocks')
@ -1659,8 +1659,8 @@ class LightningDTests(BaseLightningDTests):
l2.daemon.wait_for_log('sendrawtx exit 0')
bitcoind.generate_block(1)
l1.daemon.wait_for_log('Their unilateral tx, old commit point')
l1.daemon.wait_for_log(' to ONCHAIND_THEIR_UNILATERAL')
l2.daemon.wait_for_log(' to ONCHAIND_OUR_UNILATERAL')
l1.daemon.wait_for_log(' to ONCHAIN')
l2.daemon.wait_for_log(' to ONCHAIN')
l2.daemon.wait_for_log('Propose handling OUR_UNILATERAL/THEIR_HTLC by THEIR_HTLC_TIMEOUT_TO_THEM \\(IGNORING\\) in 5 blocks')
l1.daemon.wait_for_log('Propose handling THEIR_UNILATERAL/OUR_HTLC by OUR_HTLC_TIMEOUT_TO_US (.*) in 5 blocks')
# l2 then gets preimage, uses it instead of ignoring
@ -1701,8 +1701,8 @@ class LightningDTests(BaseLightningDTests):
l2.daemon.wait_for_log('sendrawtx exit 0')
bitcoind.generate_block(1)
l1.daemon.wait_for_log('Their unilateral tx, old commit point')
l1.daemon.wait_for_log(' to ONCHAIND_THEIR_UNILATERAL')
l2.daemon.wait_for_log(' to ONCHAIND_OUR_UNILATERAL')
l1.daemon.wait_for_log(' to ONCHAIN')
l2.daemon.wait_for_log(' to ONCHAIN')
l2.daemon.wait_for_logs(['Propose handling OUR_UNILATERAL/OUR_HTLC by OUR_HTLC_TIMEOUT_TX \\(.*\\) in 5 blocks',
'Propose handling OUR_UNILATERAL/DELAYED_OUTPUT_TO_US by OUR_DELAYED_RETURN_TO_WALLET .* in 5 blocks'])
@ -2277,8 +2277,8 @@ class LightningDTests(BaseLightningDTests):
l1.daemon.wait_for_log('Offered HTLC 0 SENT_ADD_ACK_REVOCATION cltv .* hit deadline')
l1.daemon.wait_for_log('sendrawtx exit 0')
l1.bitcoin.generate_block(1)
l1.daemon.wait_for_log(' to ONCHAIND_OUR_UNILATERAL')
l2.daemon.wait_for_log(' to ONCHAIND_THEIR_UNILATERAL')
l1.daemon.wait_for_log(' to ONCHAIN')
l2.daemon.wait_for_log(' to ONCHAIN')
# L1 will timeout HTLC immediately
l1.daemon.wait_for_logs(['Propose handling OUR_UNILATERAL/OUR_HTLC by OUR_HTLC_TIMEOUT_TX .* in 0 blocks',
@ -2332,8 +2332,8 @@ class LightningDTests(BaseLightningDTests):
l2.daemon.wait_for_log('Fulfilled HTLC 0 SENT_REMOVE_COMMIT cltv .* hit deadline')
l2.daemon.wait_for_log('sendrawtx exit 0')
l2.bitcoin.generate_block(1)
l2.daemon.wait_for_log(' to ONCHAIND_OUR_UNILATERAL')
l1.daemon.wait_for_log(' to ONCHAIND_THEIR_UNILATERAL')
l2.daemon.wait_for_log(' to ONCHAIN')
l1.daemon.wait_for_log(' to ONCHAIN')
# L2 will collect HTLC
l2.daemon.wait_for_log('Propose handling OUR_UNILATERAL/THEIR_HTLC by OUR_HTLC_SUCCESS_TX .* in 0 blocks')
@ -3016,7 +3016,7 @@ class LightningDTests(BaseLightningDTests):
bitcoind.generate_block(1)
# L1 must notice.
l1.daemon.wait_for_log(' to ONCHAIND_THEIR_UNILATERAL')
l1.daemon.wait_for_log(' to ONCHAIN')
@unittest.skipIf(not DEVELOPER, "needs DEVELOPER=1")
def test_payment_success_persistence(self):
@ -3294,8 +3294,8 @@ class LightningDTests(BaseLightningDTests):
l2.daemon.wait_for_log('sendrawtx exit 0')
bitcoind.generate_block(1)
l1.daemon.wait_for_log(' to ONCHAIND_MUTUAL')
l2.daemon.wait_for_log(' to ONCHAIND_MUTUAL')
l1.daemon.wait_for_log(' to ONCHAIN')
l2.daemon.wait_for_log(' to ONCHAIN')
bitcoind.generate_block(99)
l1.daemon.wait_for_log('onchaind complete, forgetting peer')
@ -3410,8 +3410,8 @@ class LightningDTests(BaseLightningDTests):
l2.daemon.wait_for_log('sendrawtx exit 0')
bitcoind.generate_block(1)
l1.daemon.wait_for_log(' to ONCHAIND_MUTUAL')
l2.daemon.wait_for_log(' to ONCHAIND_MUTUAL')
l1.daemon.wait_for_log(' to ONCHAIN')
l2.daemon.wait_for_log(' to ONCHAIN')
bitcoind.generate_block(99)
l1.daemon.wait_for_log('onchaind complete, forgetting peer')

2
wallet/db.c

@ -198,6 +198,8 @@ char *dbmigrations[] = {
"CREATE TABLE db_upgrades (upgrade_from INTEGER, lightning_version TEXT);",
/* We used not to clean up peers when their channels were gone. */
"DELETE FROM peers WHERE id NOT IN (SELECT peer_id FROM channels);",
/* The ONCHAIND_CHEATED/THEIR_UNILATERAL/OUR_UNILATERAL/MUTUAL are now one */
"UPDATE channels SET STATE = 8 WHERE state > 8;",
NULL,
};

Loading…
Cancel
Save