state: move anchor-depth-ok code into peer.c

This is the beginning of removing state.c altogether.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2016-11-08 22:03:55 +10:30
parent e21b161ed9
commit b349e2884b
5 changed files with 44 additions and 29 deletions

View File

@ -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;
}

View File

@ -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;

20
state.c
View File

@ -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;

View File

@ -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. */

View File

@ -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. */