state: add async anchor creation.

Actually generating the anchor transaction in my implementation
requires interaction with bitcoind, which we want to be async.  So add
a callback and a new state to wait for it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2016-01-22 06:41:47 +10:30
parent 0db3c03ed1
commit ebf2bc57d8
4 changed files with 69 additions and 10 deletions

22
state.c
View File

@@ -149,9 +149,9 @@ enum command_status state(const tal_t *ctx,
complete_cmd(peer, &cstatus, CMD_FAIL);
goto err_close_nocleanup;
}
queue_pkt(out, pkt_anchor(ctx, peer));
bitcoin_create_anchor(peer, BITCOIN_ANCHOR_CREATED);
return next_state(peer, cstatus,
STATE_OPEN_WAIT_FOR_COMMIT_SIG);
STATE_OPEN_WAIT_FOR_ANCHOR_CREATE);
} else if (input_is(input, CMD_CLOSE)) {
complete_cmd(peer, &cstatus, CMD_FAIL);
goto instant_close;
@@ -160,6 +160,21 @@ enum command_status state(const tal_t *ctx,
goto unexpected_pkt_nocleanup;
}
break;
case STATE_OPEN_WAIT_FOR_ANCHOR_CREATE:
if (input_is(input, BITCOIN_ANCHOR_CREATED)) {
queue_pkt(out, pkt_anchor(ctx, peer));
return next_state(peer, cstatus,
STATE_OPEN_WAIT_FOR_COMMIT_SIG);
} else if (input_is(input, CMD_CLOSE)) {
bitcoin_release_anchor(peer, BITCOIN_ANCHOR_CREATED);
complete_cmd(peer, &cstatus, CMD_FAIL);
goto instant_close;
} else if (input_is_pkt(input)) {
bitcoin_release_anchor(peer, BITCOIN_ANCHOR_CREATED);
complete_cmd(peer, &cstatus, CMD_FAIL);
goto unexpected_pkt_nocleanup;
}
break;
case STATE_OPEN_WAIT_FOR_ANCHOR:
if (input_is(input, PKT_OPEN_ANCHOR)) {
err = accept_pkt_anchor(ctx, peer, idata->pkt);
@@ -190,6 +205,7 @@ enum command_status state(const tal_t *ctx,
if (input_is(input, PKT_OPEN_COMMIT_SIG)) {
err = accept_pkt_open_commit_sig(ctx, peer, idata->pkt);
if (err) {
bitcoin_release_anchor(peer, INPUT_NONE);
complete_cmd(peer, &cstatus, CMD_FAIL);
goto err_start_unilateral_close;
}
@@ -203,9 +219,11 @@ enum command_status state(const tal_t *ctx,
return next_state(peer, cstatus,
STATE_OPEN_WAITING_OURANCHOR);
} else if (input_is(input, CMD_CLOSE)) {
bitcoin_release_anchor(peer, INPUT_NONE);
complete_cmd(peer, &cstatus, CMD_FAIL);
goto instant_close;
} else if (input_is_pkt(input)) {
bitcoin_release_anchor(peer, INPUT_NONE);
complete_cmd(peer, &cstatus, CMD_FAIL);
goto unexpected_pkt_nocleanup;
}