From b349e2884b34e77feceb4d4c70aa5d51e5b1bbe6 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 8 Nov 2016 22:03:55 +1030 Subject: [PATCH] state: move anchor-depth-ok code into peer.c This is the beginning of removing state.c altogether. Signed-off-by: Rusty Russell --- daemon/db.c | 3 +-- daemon/peer.c | 44 +++++++++++++++++++++++++++++++++++++++++--- state.c | 20 +------------------- state.h | 4 +--- state_types.h | 2 -- 5 files changed, 44 insertions(+), 29 deletions(-) diff --git a/daemon/db.c b/daemon/db.c index 71e38ffc4..373207d5f 100644 --- a/daemon/db.c +++ b/daemon/db.c @@ -306,8 +306,7 @@ static void load_peer_anchor(struct peer *peer) /* FIXME: Do timeout! */ peer_watch_anchor(peer, - sqlite3_column_int(stmt, 4), - BITCOIN_ANCHOR_DEPTHOK, INPUT_NONE); + sqlite3_column_int(stmt, 4), INPUT_NONE); peer->anchor.min_depth = sqlite3_column_int(stmt, 5); anchor_set = true; } diff --git a/daemon/peer.c b/daemon/peer.c index 9d240444e..f46bcf6c3 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -3026,6 +3026,46 @@ static void check_htlc_expiry(struct peer *peer) peer_fail(peer, __func__); } +static void peer_depth_ok(struct peer *peer) +{ + queue_pkt_open_complete(peer); + + db_start_transaction(peer); + + switch (peer->state) { + case STATE_OPEN_WAITING_OURANCHOR: + set_peer_state(peer, STATE_OPEN_WAIT_FOR_COMPLETE_OURANCHOR, + __func__, false); + break; + case STATE_OPEN_WAITING_THEIRANCHOR: + set_peer_state(peer, STATE_OPEN_WAIT_FOR_COMPLETE_THEIRANCHOR, + __func__, false); + break; + case STATE_OPEN_WAITING_OURANCHOR_THEYCOMPLETED: + case STATE_OPEN_WAITING_THEIRANCHOR_THEYCOMPLETED: + assert(!peer->nc); + /* We're connected, so record it. */ + peer->nc = add_connection(peer->dstate, + &peer->dstate->id, peer->id, + peer->dstate->config.fee_base, + peer->dstate->config.fee_per_satoshi, + peer->dstate->config.min_htlc_expiry, + peer->dstate->config.min_htlc_expiry); + peer_open_complete(peer, NULL); + set_peer_state(peer, STATE_NORMAL, __func__, true); + break; + default: + log_broken(peer->log, "%s: state %s", + __func__, state_name(peer->state)); + peer_fail(peer, __func__); + io_wake(peer); + break; + } + + if (db_commit_transaction(peer)) + peer_comms_err(peer, pkt_err(peer, "Database error")); +} + static enum watch_result anchor_depthchange(struct peer *peer, unsigned int depth, const struct sha256_double *txid, @@ -3039,7 +3079,7 @@ static enum watch_result anchor_depthchange(struct peer *peer, peer->anchor.ok_depth); /* We can see a run of blocks all at once, so may be > depth */ if ((int)depth >= peer->anchor.ok_depth) { - state_event(peer, BITCOIN_ANCHOR_DEPTHOK, NULL); + peer_depth_ok(peer); peer->anchor.ok_depth = -1; } } else if (depth == 0) @@ -3968,7 +4008,6 @@ static void anchor_timeout(struct peer *peer) void peer_watch_anchor(struct peer *peer, int depth, - enum state_input depthok, enum state_input timeout) { log_debug_struct(peer->log, "watching for anchor %s", @@ -3976,7 +4015,6 @@ void peer_watch_anchor(struct peer *peer, log_add(peer->log, " to hit depth %i", depth); /* We assume this. */ - assert(depthok == BITCOIN_ANCHOR_DEPTHOK); assert(timeout == BITCOIN_ANCHOR_TIMEOUT || timeout == INPUT_NONE); peer->anchor.ok_depth = depth; diff --git a/state.c b/state.c index ff95d2a9c..96c4e5385 100644 --- a/state.c +++ b/state.c @@ -168,7 +168,6 @@ enum state state(struct peer *peer, queue_pkt_open_commit_sig(peer); peer_watch_anchor(peer, peer->local.mindepth, - BITCOIN_ANCHOR_DEPTHOK, BITCOIN_ANCHOR_TIMEOUT); return next_state(peer, STATE_OPEN_WAITING_THEIRANCHOR); @@ -217,7 +216,6 @@ enum state state(struct peer *peer, queue_tx_broadcast(broadcast, bitcoin_anchor(peer)); peer_watch_anchor(peer, peer->local.mindepth, - BITCOIN_ANCHOR_DEPTHOK, INPUT_NONE); return next_state(peer, STATE_OPEN_WAITING_OURANCHOR); } else if (input_is_pkt(input)) { @@ -238,15 +236,7 @@ enum state state(struct peer *peer, } /* Fall thru */ case STATE_OPEN_WAITING_OURANCHOR_THEYCOMPLETED: - if (input_is(input, BITCOIN_ANCHOR_DEPTHOK)) { - queue_pkt_open_complete(peer); - if (peer->state == STATE_OPEN_WAITING_OURANCHOR_THEYCOMPLETED) { - peer_open_complete(peer, NULL); - return next_state(peer, STATE_NORMAL); - } - return next_state(peer, - STATE_OPEN_WAIT_FOR_COMPLETE_OURANCHOR); - } else if (input_is(input, PKT_CLOSE_SHUTDOWN)) { + if (input_is(input, PKT_CLOSE_SHUTDOWN)) { peer_open_complete(peer, "Received PKT_CLOSE_SHUTDOWN"); goto accept_shutdown; } else if (input_is_pkt(input)) { @@ -270,14 +260,6 @@ enum state state(struct peer *peer, /* Anchor didn't reach blockchain in reasonable time. */ queue_pkt_err(peer, pkt_err(peer, "Anchor timed out")); return next_state(peer, STATE_ERR_ANCHOR_TIMEOUT); - } else if (input_is(input, BITCOIN_ANCHOR_DEPTHOK)) { - queue_pkt_open_complete(peer); - if (peer->state == STATE_OPEN_WAITING_THEIRANCHOR_THEYCOMPLETED) { - peer_open_complete(peer, NULL); - return next_state(peer, STATE_NORMAL); - } - return next_state(peer, - STATE_OPEN_WAIT_FOR_COMPLETE_THEIRANCHOR); } else if (input_is(input, PKT_CLOSE_SHUTDOWN)) { peer_open_complete(peer, "Received PKT_CLOSE_SHUTDOWN"); goto accept_shutdown; diff --git a/state.h b/state.h index 80ebbdad5..72d1b4fc4 100644 --- a/state.h +++ b/state.h @@ -103,15 +103,13 @@ static inline bool input_is(enum state_input a, enum state_input b) * peer_watch_anchor: create a watch for the anchor transaction. * @peer: the state data for this peer. * @depth: depth at which to fire @depthok. - * @depthok: the input to give when anchor reaches expected depth. * @timeout: the input to give if anchor doesn't reach depth in time. * - * @depthok can be INPUT_NONE if it's our anchor (we don't time + * @timeout can be INPUT_NONE if it's our anchor (we don't time * ourselves out). */ void peer_watch_anchor(struct peer *peer, int depth, - enum state_input depthok, enum state_input timeout); /* Start creation of the bitcoin anchor tx. */ diff --git a/state_types.h b/state_types.h index c4c21a163..95251d657 100644 --- a/state_types.h +++ b/state_types.h @@ -92,8 +92,6 @@ enum state_input { /* * Bitcoin events */ - /* It reached the required depth. */ - BITCOIN_ANCHOR_DEPTHOK, /* It didn't reach the required depth in time. */ BITCOIN_ANCHOR_TIMEOUT, /* No more HTLCs in either commitment tx. */