Skip to content

Commit b5f4c78

Browse files
committed
df-accepter: accepter logic
Implements accepter side of df
1 parent df57686 commit b5f4c78

File tree

5 files changed

+652
-85
lines changed

5 files changed

+652
-85
lines changed

channeld/channeld.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2944,7 +2944,7 @@ static void init_channel(struct peer *peer)
29442944
enum side *failed_sides;
29452945
struct added_htlc *htlcs;
29462946
bool reconnected;
2947-
u8 *funding_signed;
2947+
u8 *sigs_msg;
29482948
const u8 *msg;
29492949
u32 feerate_per_kw[NUM_SIDES];
29502950
u32 minimum_depth, failheight;
@@ -3004,7 +3004,7 @@ static void init_channel(struct peer *peer)
30043004
&peer->shutdown_sent[REMOTE],
30053005
&peer->final_scriptpubkey,
30063006
&peer->channel_flags,
3007-
&funding_signed,
3007+
&sigs_msg,
30083008
&peer->announce_depth_reached,
30093009
&last_remote_per_commit_secret,
30103010
&peer->localfeatures,
@@ -3111,9 +3111,9 @@ static void init_channel(struct peer *peer)
31113111
if (reconnected)
31123112
peer_reconnect(peer, &last_remote_per_commit_secret);
31133113

3114-
/* If we have a funding_signed message, send that immediately */
3115-
if (funding_signed)
3116-
sync_crypto_write(peer->pps, take(funding_signed));
3114+
/* If we have a final sigs message, send that immediately */
3115+
if (sigs_msg)
3116+
sync_crypto_write(peer->pps, take(sigs_msg));
31173117

31183118
/* Reenable channel */
31193119
channel_announcement_negotiate(peer);

lightningd/channel_control.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ static unsigned channel_msg(struct subd *sd, const u8 *msg, const int *fds)
324324

325325
void peer_start_channeld(struct channel *channel,
326326
struct per_peer_state *pps,
327-
const u8 *funding_signed,
327+
const u8 *sigs_msg,
328328
bool reconnected)
329329
{
330330
u8 *initmsg;
@@ -468,7 +468,7 @@ void peer_start_channeld(struct channel *channel,
468468
channel->shutdown_scriptpubkey[REMOTE] != NULL,
469469
channel->shutdown_scriptpubkey[LOCAL],
470470
channel->channel_flags,
471-
funding_signed,
471+
sigs_msg,
472472
reached_announce_depth,
473473
&last_remote_per_commit_secret,
474474
channel->peer->localfeatures,

lightningd/opening_control.c

Lines changed: 138 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#include "bitcoin/feerate.h"
22
#include <bitcoin/privkey.h>
33
#include <bitcoin/script.h>
4+
#include <ccan/cast/cast.h>
45
#include <ccan/tal/str/str.h>
56
#include <common/addr.h>
67
#include <common/channel_config.h>
78
#include <common/features.h>
89
#include <common/funding_tx.h>
910
#include <common/json_command.h>
11+
#include <common/json_helpers.h>
1012
#include <common/jsonrpc_errors.h>
1113
#include <common/key_derive.h>
1214
#include <common/param.h>
@@ -679,20 +681,23 @@ static void opening_fundee_finished(struct subd *openingd,
679681
const int *fds,
680682
struct uncommitted_channel *uc)
681683
{
682-
u8 *funding_signed;
684+
u8 *final_peer_msg;
683685
struct channel_info channel_info;
684686
struct bitcoin_signature remote_commit_sig;
685687
struct bitcoin_tx *remote_commit;
686688
struct lightningd *ld = openingd->ld;
687689
struct bitcoin_txid funding_txid;
688690
u16 funding_outnum;
689-
struct amount_sat funding;
691+
struct amount_sat opener_funding;
692+
struct amount_sat accepter_funding;
693+
struct amount_sat total_funding;
690694
struct amount_msat push;
691695
u32 feerate;
692696
u8 channel_flags;
693697
struct channel *channel;
694698
u8 *remote_upfront_shutdown_script;
695699
struct per_peer_state *pps;
700+
struct utxo **utxos;
696701

697702
log_debug(uc->log, "Got opening_fundee_finish_response");
698703

@@ -712,12 +717,12 @@ static void opening_fundee_finished(struct subd *openingd,
712717
&channel_info.remote_fundingkey,
713718
&funding_txid,
714719
&funding_outnum,
715-
&funding,
716-
&AMOUNT_SAT(0),
720+
&opener_funding,
721+
&accepter_funding,
717722
&push,
718723
&channel_flags,
719724
&feerate,
720-
&funding_signed,
725+
&final_peer_msg,
721726
&uc->our_config.channel_reserve,
722727
&remote_upfront_shutdown_script)) {
723728
log_broken(uc->log, "bad OPENING_FUNDEE_REPLY %s",
@@ -736,14 +741,23 @@ static void opening_fundee_finished(struct subd *openingd,
736741
goto failed;
737742
}
738743

744+
if (!amount_sat_add(&total_funding, opener_funding, accepter_funding))
745+
abort();
746+
747+
if (uc->pf)
748+
/* We steal here, because otherwise gets eaten below */
749+
utxos = tal_steal(tmpctx, uc->pf->utxos);
750+
else
751+
utxos = NULL;
752+
739753
/* Consumes uc */
740754
channel = wallet_commit_channel(ld, uc,
741755
remote_commit,
742756
&remote_commit_sig,
743757
&funding_txid,
744758
funding_outnum,
745-
funding,
746-
AMOUNT_SAT(0),
759+
total_funding,
760+
accepter_funding,
747761
push,
748762
REMOTE,
749763
channel_flags,
@@ -759,14 +773,19 @@ static void opening_fundee_finished(struct subd *openingd,
759773
type_to_string(reply, struct bitcoin_txid,
760774
&channel->funding_txid));
761775

776+
if (utxos) {
777+
// TODO: mark utxos as shared
778+
tal_free(utxos);
779+
}
780+
762781
channel_watch_funding(ld, channel);
763782

764783
/* Tell plugins about the success */
765784
notify_channel_opened(ld, &channel->peer->id, &channel->funding,
766785
&channel->funding_txid, &channel->remote_funding_locked);
767786

768787
/* On to normal operation! */
769-
peer_start_channeld(channel, pps, funding_signed, false);
788+
peer_start_channeld(channel, pps, final_peer_msg, false);
770789

771790
subd_release_channel(openingd, uc);
772791
uc->openingd = NULL;
@@ -778,6 +797,7 @@ static void opening_fundee_finished(struct subd *openingd,
778797
close(fds[1]);
779798
close(fds[3]);
780799
tal_free(uc);
800+
tal_free(utxos);
781801
}
782802

783803
static void opening_funder_failed(struct subd *openingd, const u8 *msg,
@@ -940,9 +960,9 @@ static void channel_config(struct lightningd *ld,
940960

941961
struct openchannel_hook_payload {
942962
struct subd *openingd;
943-
struct amount_sat funding_satoshis;
944963
/* Is this a v2 openchannel call? */
945964
bool is_v2;
965+
struct amount_sat opener_satoshis;
946966
struct amount_msat push_msat;
947967
struct amount_sat dust_limit_satoshis;
948968
struct amount_msat max_htlc_value_in_flight_msat;
@@ -965,7 +985,7 @@ openchannel_hook_serialize(struct openchannel_hook_payload *payload,
965985
json_object_start(stream, "openchannel");
966986
json_add_node_id(stream, "id", &uc->peer->id);
967987
json_add_amount_sat_only(stream, "funding_satoshis",
968-
payload->funding_satoshis);
988+
payload->opener_satoshis);
969989
json_add_amount_msat_only(stream, "push_msat", payload->push_msat);
970990
json_add_amount_sat_only(stream, "dust_limit_satoshis",
971991
payload->dust_limit_satoshis);
@@ -994,11 +1014,16 @@ static void openchannel_payload_remove_openingd(struct subd *openingd,
9941014
}
9951015

9961016
static void openchannel_hook_cb(struct openchannel_hook_payload *payload,
997-
const char *buffer,
998-
const jsmntok_t *toks)
1017+
const char *buffer,
1018+
const jsmntok_t *toks)
9991019
{
10001020
struct subd *openingd = payload->openingd;
1021+
struct uncommitted_channel *uc = openingd->channel;
1022+
struct peer_funding *pf = uc->pf;
10011023
const char *errmsg = NULL;
1024+
struct amount_sat change;
1025+
struct bitcoin_tx_output **outputs;
1026+
bool has_change;
10021027

10031028
/* We want to free this, whatever happens. */
10041029
tal_steal(tmpctx, payload);
@@ -1009,6 +1034,7 @@ static void openchannel_hook_cb(struct openchannel_hook_payload *payload,
10091034

10101035
tal_del_destructor2(openingd, openchannel_payload_remove_openingd, payload);
10111036

1037+
pf->accepter_funding = AMOUNT_SAT(0);
10121038
/* If we had a hook, check what it says */
10131039
if (buffer) {
10141040
const jsmntok_t *t = json_get_member(buffer, toks, "result");
@@ -1031,10 +1057,107 @@ static void openchannel_hook_cb(struct openchannel_hook_payload *payload,
10311057
fatal("Plugin returned an invalid result for the "
10321058
"openchannel hook: %.*s",
10331059
t->end - t->start, buffer + t->start);
1060+
1061+
if (payload->is_v2 && !errmsg) {
1062+
const jsmntok_t *funding_sats = json_get_member(buffer,
1063+
toks,
1064+
"funding_sats");
1065+
if (funding_sats)
1066+
json_to_sat(buffer, funding_sats, &pf->accepter_funding);
1067+
}
1068+
}
1069+
1070+
if (!payload->is_v2 || errmsg) {
1071+
subd_send_msg(openingd,
1072+
take(towire_opening_got_offer_reply(NULL, errmsg)));
1073+
return;
1074+
}
1075+
1076+
/* Find utxos for funding this */
1077+
if (amount_sat_greater(pf->accepter_funding, AMOUNT_SAT(0))) {
1078+
/* Print a warning if we're trying to fund a channel with more
1079+
* than we said that we'd be allowed to */
1080+
if (amount_sat_greater(pf->accepter_funding, payload->available_funds))
1081+
log_info(openingd->log,
1082+
"Attempting to fund channel for %s when max was set to %s",
1083+
type_to_string(
1084+
tmpctx, struct amount_sat, &pf->accepter_funding),
1085+
type_to_string(
1086+
tmpctx, struct amount_sat, &payload->available_funds));
1087+
1088+
struct amount_sat fee_estimate UNUSED;
1089+
pf->utxos = (struct utxo **)wallet_select_coins(pf, openingd->ld->wallet,
1090+
true, pf->accepter_funding,
1091+
0, 0,
1092+
UINT32_MAX, /* minconf 1 */
1093+
&fee_estimate, &change);
1094+
1095+
/* Verify that we're still under the max remote that is allowed */
1096+
if (tal_count(pf->utxos) > REMOTE_CONTRIB_LIMIT) {
1097+
log_info(openingd->log,
1098+
"Too many utxos selected (%ld), only %"PRIu16" allowed",
1099+
tal_count(pf->utxos), REMOTE_CONTRIB_LIMIT - 1);
1100+
pf->utxos = tal_free(pf->utxos);
1101+
change = AMOUNT_SAT(0);
1102+
}
1103+
} else {
1104+
change = AMOUNT_SAT(0);
1105+
pf->utxos = NULL;
1106+
}
1107+
1108+
/* Either no utxos were found, or we weren't funding it anyway */
1109+
if (!pf->utxos) {
1110+
if (amount_sat_greater(pf->accepter_funding, AMOUNT_SAT(0)))
1111+
/* FIXME: send notification to the plugin that
1112+
* we weren't able to fund this channel as they
1113+
* requested; utxo set has changed since hook was called */
1114+
log_unusual(openingd->log,
1115+
"Unable to fund channel with %s; utxos unavailable",
1116+
type_to_string(tmpctx, struct amount_sat,
1117+
&pf->accepter_funding));
1118+
1119+
pf->accepter_funding = AMOUNT_SAT(0);
1120+
change = AMOUNT_SAT(0);
1121+
}
1122+
1123+
has_change = amount_sat_greater(change, AMOUNT_SAT(0));
1124+
outputs = tal_arr(tmpctx, struct bitcoin_tx_output *, has_change ? 1 : 0);
1125+
if (has_change) {
1126+
struct bitcoin_tx_output *output;
1127+
struct pubkey *changekey;
1128+
s64 change_keyindex;
1129+
1130+
output = tal(outputs, struct bitcoin_tx_output);
1131+
output->amount = change;
1132+
1133+
change_keyindex = wallet_get_newindex(openingd->ld);
1134+
changekey = tal(tmpctx, struct pubkey);
1135+
if (!bip32_pubkey(openingd->ld->wallet->bip32_base,
1136+
changekey, change_keyindex)) {
1137+
fatal("Error deriving change key %lu", change_keyindex);
1138+
}
1139+
output->script = scriptpubkey_p2wpkh(output, changekey);
1140+
outputs[0] = output;
1141+
}
1142+
1143+
/* We need to supply the scriptSig to our peer, for P2SH wrapped inputs */
1144+
for (size_t i = 0; i < tal_count(pf->utxos); i++) {
1145+
if (pf->utxos[i]->is_p2sh)
1146+
pf->utxos[i]->scriptSig =
1147+
derive_redeem_scriptsig(pf->utxos[i],
1148+
openingd->ld->wallet,
1149+
pf->utxos[i]->keyindex);
1150+
else
1151+
pf->utxos[i]->scriptSig = NULL;
1152+
10341153
}
10351154

10361155
subd_send_msg(openingd,
1037-
take(towire_opening_got_offer_reply(NULL, errmsg)));
1156+
take(towire_opening_got_offer_reply_fund(
1157+
NULL, pf->accepter_funding,
1158+
cast_const2(const struct utxo **, pf->utxos),
1159+
cast_const2(const struct bitcoin_tx_output **,
1160+
outputs))));
10381161
}
10391162

10401163
REGISTER_PLUGIN_HOOK(openchannel,
@@ -1061,7 +1184,7 @@ static void opening_got_offer(struct subd *openingd,
10611184
payload->openingd = openingd;
10621185
if (!fromwire_opening_got_offer(payload, msg,
10631186
&payload->is_v2,
1064-
&payload->funding_satoshis,
1187+
&payload->opener_satoshis,
10651188
&payload->push_msat,
10661189
&payload->dust_limit_satoshis,
10671190
&payload->max_htlc_value_in_flight_msat,
@@ -1157,6 +1280,7 @@ static unsigned int openingd_msg(struct subd *openingd,
11571280
case WIRE_OPENING_FUNDER_COMPLETE:
11581281
case WIRE_OPENING_FUNDER_CANCEL:
11591282
case WIRE_OPENING_GOT_OFFER_REPLY:
1283+
case WIRE_OPENING_GOT_OFFER_REPLY_FUND:
11601284
case WIRE_OPENING_DEV_MEMLEAK:
11611285
/* Replies never get here */
11621286
case WIRE_OPENING_DEV_MEMLEAK_REPLY:

openingd/opening_wire.csv

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ msgdata,opening_got_offer,shutdown_scriptpubkey,u8,shutdown_len
4646
msgtype,opening_got_offer_reply,6105
4747
msgdata,opening_got_offer_reply,rejection,?wirestring,
4848

49+
# master->openingd: we'd like to fund this too, maybe
50+
msgtype,opening_got_offer_reply_fund,6107
51+
msgdata,opening_got_offer_reply_fund,our_funding,amount_sat,
52+
msgdata,opening_got_offer_reply_fund,num_inputs,u16,
53+
msgdata,opening_got_offer_reply_fund,utxos,utxo,num_inputs
54+
msgdata,opening_got_offer_reply_fund,num_outputs,u16,
55+
msgdata,opening_got_offer_reply_fund,outputs,bitcoin_tx_output,num_outputs
56+
4957
# Openingd->master: we've successfully offered channel.
5058
# This gives their sig, means we can broadcast tx: we're done.
5159
msgtype,opening_funder_reply,6101

0 commit comments

Comments
 (0)