diff --git a/common/type_to_string.h b/common/type_to_string.h index 357d965b1..d0b6b12ea 100644 --- a/common/type_to_string.h +++ b/common/type_to_string.h @@ -17,7 +17,6 @@ union printable_types { const struct bitcoin_tx *bitcoin_tx; const struct htlc *htlc; const struct preimage *preimage; - const struct channel_state *channel_state; const struct channel_oneside *channel_oneside; const struct wireaddr *wireaddr; const secp256k1_pubkey *secp256k1_pubkey; diff --git a/lightningd/Makefile b/lightningd/Makefile index 4280a6434..0a474c3c4 100644 --- a/lightningd/Makefile +++ b/lightningd/Makefile @@ -83,12 +83,12 @@ ALL_OBJS += $(LIGHTNINGD_OBJS) # We accumulate all lightningd/ headers in these three: LIGHTNINGD_HEADERS_NOGEN = \ $(LIGHTNINGD_SRC:.c=.h) \ - lightningd/peer_state.h \ + lightningd/channel_state.h \ lightningd/jsonrpc_errors.h # Generated headers LIGHTNINGD_HEADERS_GEN = \ - lightningd/gen_peer_state_names.h + lightningd/gen_channel_state_names.h ALL_GEN_HEADERS += $(LIGHTNINGD_HEADERS_GEN) @@ -97,8 +97,8 @@ LIGHTNINGD_HEADERS = $(LIGHTNINGD_HEADERS_NOGEN) $(LIGHTNINGD_HEADERS_GEN) $(EXT $(LIGHTNINGD_OBJS): $(LIGHTNINGD_HEADERS) -lightningd/gen_peer_state_names.h: lightningd/peer_state.h ccan/ccan/cdump/tools/cdump-enumstr - ccan/ccan/cdump/tools/cdump-enumstr lightningd/peer_state.h > $@ +lightningd/gen_channel_state_names.h: lightningd/channel_state.h ccan/ccan/cdump/tools/cdump-enumstr + ccan/ccan/cdump/tools/cdump-enumstr lightningd/channel_state.h > $@ check-source: $(LIGHTNINGD_SRC:%=check-src-include-order/%) $(LIGHTNINGD_SRC_NOHDR:%=check-src-include-order/%) check-source: $(LIGHTNINGD_HEADERS_NOGEN:%=check-hdr-include-order/%) diff --git a/lightningd/channel.c b/lightningd/channel.c index 76922564f..3b3a06da4 100644 --- a/lightningd/channel.c +++ b/lightningd/channel.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -99,7 +100,7 @@ void derive_channel_seed(struct lightningd *ld, struct privkey *seed, struct channel *new_channel(struct peer *peer, u64 dbid, /* NULL or stolen */ struct wallet_shachain *their_shachain, - enum peer_state state, + enum channel_state state, enum side funder, /* NULL or stolen */ struct log *log, @@ -190,7 +191,15 @@ struct channel *new_channel(struct peer *peer, u64 dbid, const char *channel_state_name(const struct channel *channel) { - return peer_state_name(channel->state); + return channel_state_str(channel->state); +} + +const char *channel_state_str(enum channel_state state) +{ + for (size_t i = 0; enum_channel_state_names[i].name; i++) + if (enum_channel_state_names[i].v == state) + return enum_channel_state_names[i].name; + return "unknown"; } struct channel *peer_active_channel(struct peer *peer) @@ -230,22 +239,19 @@ void channel_set_last_tx(struct channel *channel, } void channel_set_state(struct channel *channel, - enum peer_state old_state, - enum peer_state state) + enum channel_state old_state, + enum channel_state state) { log_info(channel->log, "State changed from %s to %s", - channel_state_name(channel), peer_state_name(state)); + channel_state_name(channel), channel_state_str(state)); if (channel->state != old_state) fatal("channel state %s should be %s", - channel_state_name(channel), peer_state_name(old_state)); + channel_state_name(channel), channel_state_str(old_state)); channel->state = state; - /* We only persist channels/peers that have reached the opening state */ - if (channel_persists(channel)) { - /* TODO(cdecker) Selectively save updated fields to DB */ - wallet_channel_save(channel->peer->ld->wallet, channel); - } + /* TODO(cdecker) Selectively save updated fields to DB */ + wallet_channel_save(channel->peer->ld->wallet, channel); } void channel_fail_permanent(struct channel *channel, const char *fmt, ...) @@ -284,11 +290,8 @@ void channel_fail_permanent(struct channel *channel, const char *fmt, ...) } channel_set_owner(channel, NULL); - if (channel_persists(channel)) { - drop_to_chain(ld, channel); - tal_free(why); - } else - delete_channel(channel, why); + drop_to_chain(ld, channel); + tal_free(why); } void channel_internal_error(struct channel *channel, const char *fmt, ...) @@ -314,10 +317,10 @@ void channel_fail_transient(struct channel *channel, const char *fmt, ...) va_end(ap); log_info(channel->log, "Peer transient failure in %s: %s", channel_state_name(channel), why); + tal_free(why); #if DEVELOPER if (dev_disconnect_permanent(channel->peer->ld)) { - tal_free(why); channel_internal_error(channel, "dev_disconnect permfail"); return; } @@ -325,15 +328,6 @@ void channel_fail_transient(struct channel *channel, const char *fmt, ...) channel_set_owner(channel, NULL); - /* If we haven't reached awaiting locked, we don't need to reconnect */ - if (!channel_persists(channel)) { - log_info(channel->log, "Only reached state %s: forgetting", - channel_state_name(channel)); - delete_channel(channel, why); - return; - } - tal_free(why); - /* Reconnect unless we've dropped/are dropping to chain. */ if (channel_active(channel)) { struct lightningd *ld = channel->peer->ld; diff --git a/lightningd/channel.h b/lightningd/channel.h index b82b5aa42..e672dff66 100644 --- a/lightningd/channel.h +++ b/lightningd/channel.h @@ -2,7 +2,7 @@ #define LIGHTNING_LIGHTNINGD_CHANNEL_H #include "config.h" #include -#include +#include #include #include @@ -25,7 +25,7 @@ struct channel { struct wallet_shachain their_shachain; /* What's happening. */ - enum peer_state state; + enum channel_state state; /* Which side offered channel? */ enum side funder; @@ -88,7 +88,7 @@ struct channel { struct channel *new_channel(struct peer *peer, u64 dbid, /* NULL or stolen */ struct wallet_shachain *their_shachain, - enum peer_state state, + enum channel_state state, enum side funder, /* NULL or stolen */ struct log *log, @@ -126,6 +126,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid, void delete_channel(struct channel *channel, const char *why); const char *channel_state_name(const struct channel *channel); +const char *channel_state_str(enum channel_state state); void channel_set_owner(struct channel *channel, struct subd *owner); @@ -138,8 +139,8 @@ void channel_fail_permanent(struct channel *channel, const char *fmt, ...); void channel_internal_error(struct channel *channel, const char *fmt, ...); void channel_set_state(struct channel *channel, - enum peer_state old_state, - enum peer_state state); + enum channel_state old_state, + enum channel_state state); /* Find a channel which is not onchain, if any */ struct channel *peer_active_channel(struct peer *peer); @@ -172,7 +173,7 @@ static inline bool channel_can_remove_htlc(const struct channel *channel) || channel->state == ONCHAIND_OUR_UNILATERAL; } -static inline bool channel_state_on_chain(enum peer_state state) +static inline bool channel_state_on_chain(enum channel_state state) { return state == ONCHAIND_CHEATED || state == ONCHAIND_THEIR_UNILATERAL @@ -194,23 +195,7 @@ static inline bool channel_active(const struct channel *channel) static inline bool channel_wants_reconnect(const struct channel *channel) { - return channel->state >= CHANNELD_AWAITING_LOCKIN - && channel->state <= CLOSINGD_COMPLETE; -} - -/* BOLT #2: - * - * On disconnection, the funder MUST remember the channel for - * reconnection if it has broadcast the funding transaction, otherwise it - * SHOULD NOT. - * - * On disconnection, the non-funding node MUST remember the channel for - * reconnection if it has sent the `funding_signed` message, otherwise - * it SHOULD NOT. - */ -static inline bool channel_persists(const struct channel *channel) -{ - return channel->state >= CHANNELD_AWAITING_LOCKIN; + return channel->state <= CLOSINGD_COMPLETE; } void derive_channel_seed(struct lightningd *ld, struct privkey *seed, diff --git a/lightningd/peer_state.h b/lightningd/channel_state.h similarity index 65% rename from lightningd/peer_state.h rename to lightningd/channel_state.h index 9a28b86ad..de8bc82d6 100644 --- a/lightningd/peer_state.h +++ b/lightningd/channel_state.h @@ -1,16 +1,11 @@ -#ifndef LIGHTNING_LIGHTNINGD_PEER_STATE_H -#define LIGHTNING_LIGHTNINGD_PEER_STATE_H +#ifndef LIGHTNING_LIGHTNINGD_CHANNEL_STATE_H +#define LIGHTNING_LIGHTNINGD_CHANNEL_STATE_H #include "config.h" -/* FIXME: rename channel_state! */ -enum peer_state { - UNINITIALIZED, - - /* Negotiating channel opening: in opening daemon */ - OPENINGD, - +/* These are in the database, so don't renumber them! */ +enum channel_state { /* In channeld, still waiting for lockin. */ - CHANNELD_AWAITING_LOCKIN, + CHANNELD_AWAITING_LOCKIN = 2, /* Normal operating state. */ CHANNELD_NORMAL, @@ -35,4 +30,4 @@ enum peer_state { }; #define CHANNEL_STATE_MAX ONCHAIND_MUTUAL -#endif /* LIGHTNING_LIGHTNINGD_PEER_STATE_H */ +#endif /* LIGHTNING_LIGHTNINGD_CHANNEL_STATE_H */ diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 4215c688f..8460f3486 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -31,7 +31,6 @@ #include #include #include -#include #include #include #include @@ -463,16 +462,6 @@ void peer_connected(struct lightningd *ld, const u8 *msg, #endif switch (channel->state) { - /* This can't happen. */ - case UNINITIALIZED: - abort(); - - /* Reconnect: discard old one. */ - case OPENINGD: - delete_channel(channel, "peer reconnected"); - channel = NULL; - goto return_to_gossipd; - case ONCHAIND_CHEATED: case ONCHAIND_THEIR_UNILATERAL: case ONCHAIND_OUR_UNILATERAL: @@ -1029,7 +1018,7 @@ static void handle_onchain_init_reply(struct channel *channel, const u8 *msg) if (!channel_state_on_chain(state)) { channel_internal_error(channel, "Invalid onchain_init_reply state %u (%s)", - state, peer_state_name(state)); + state, channel_state_str(state)); return; } @@ -2879,17 +2868,6 @@ static const struct json_command close_command = { }; AUTODATA(json_command, &close_command); - -const char *peer_state_name(enum peer_state state) -{ - size_t i; - - for (i = 0; enum_peer_state_names[i].name; i++) - if (enum_peer_state_names[i].v == state) - return enum_peer_state_names[i].name; - return "unknown"; -} - static void activate_peer(struct peer *peer) { u8 *msg; diff --git a/lightningd/peer_control.h b/lightningd/peer_control.h index 6f1f03320..d50bc5d58 100644 --- a/lightningd/peer_control.h +++ b/lightningd/peer_control.h @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include #include @@ -93,7 +93,6 @@ void peer_sent_nongossip(struct lightningd *ld, /* Peer has failed to open; return to gossipd. */ void opening_failed(struct peer *peer, const u8 *msg TAKES); -const char *peer_state_name(enum peer_state state); void setup_listeners(struct lightningd *ld); /* We've loaded peers from database, set them going. */ diff --git a/onchaind/onchain.c b/onchaind/onchain.c index c5d12506f..3cd6fad2a 100644 --- a/onchaind/onchain.c +++ b/onchaind/onchain.c @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include #include @@ -966,7 +966,7 @@ static void wait_for_resolved(struct tracked_output **outs) take(towire_onchain_all_irrevocably_resolved(outs))); } -static void set_state(enum peer_state state) +static void set_state(enum channel_state state) { wire_sync_write(REQ_FD, take(towire_onchain_init_reply(NULL, state))); } diff --git a/wallet/db.c b/wallet/db.c index 1b1963fd0..6639ad66d 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -191,6 +191,8 @@ char *dbmigrations[] = { "ALTER TABLE payments ADD COLUMN route_channels TEXT;", "CREATE TABLE htlc_sigs (channelid INTEGER REFERENCES channels(id) ON DELETE CASCADE, signature BLOB);", "CREATE INDEX channel_idx ON htlc_sigs (channelid)", + /* Get rid of OPENINGD entries; we don't put them in db any more */ + "DELETE FROM channels WHERE state=1", NULL, }; diff --git a/wallet/wallet.c b/wallet/wallet.c index fa33ef96f..693484a24 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -625,11 +625,6 @@ bool wallet_channels_load_active(const tal_t *ctx, struct wallet *w) bool ok = true; sqlite3_stmt *stmt; - /* Get rid of OPENINGD entries; they don't last across reconnects */ - stmt = db_prepare(w->db, "DELETE FROM channels WHERE state=?"); - sqlite3_bind_int64(stmt, 1, OPENINGD); - db_exec_prepared(w->db, stmt); - /* We load all channels */ stmt = db_query( __func__, w->db, "SELECT %s FROM channels;",