Skip to content

Commit 7ee8ebf

Browse files
wip: funding transaction in rgb
Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent be39e1f commit 7ee8ebf

File tree

3 files changed

+94
-3
lines changed

3 files changed

+94
-3
lines changed

lightningd/opening_control.c

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ static void openchannel_hook_serialize(struct openchannel_hook_payload *payload,
674674

675675
/* openingd dies? Remove openingd ptr from payload */
676676
static void openchannel_payload_remove_openingd(struct subd *openingd,
677-
struct openchannel_hook_payload *payload)
677+
struct openchannel_hook_payload *payload)
678678
{
679679
assert(payload->openingd == openingd);
680680
payload->openingd = NULL;
@@ -858,6 +858,74 @@ static void opening_got_offer(struct subd *openingd,
858858
plugin_hook_call_openchannel(openingd->ld, NULL, payload);
859859
}
860860

861+
struct onfunding_channel_tx_hook_payload {
862+
struct subd *openingd;
863+
struct bitcoin_tx *tx;
864+
};
865+
866+
static void onfunding_channel_tx_hook_final(struct onfunding_channel_tx_hook_payload *payload STEALS)
867+
{
868+
struct subd *openingd = payload->openingd;
869+
// FIXME(bitfinix): manage the errors!
870+
subd_send_msg(openingd,
871+
take(towire_openingd_on_funding_tx_reply(NULL, payload->tx)));
872+
}
873+
874+
static void onfunding_channel_tx_hook_serialize(struct onfunding_channel_tx_hook_payload *payload,
875+
struct json_stream *stream,
876+
struct plugin *plugin)
877+
{
878+
json_object_start(stream, "onfunding_channel_tx");
879+
json_add_tx(stream, "tx", payload->tx);
880+
json_object_end(stream);
881+
}
882+
883+
static bool onfunding_channel_tx_hook_deserialize(struct onfunding_channel_tx_hook_payload *payload,
884+
const char *buffer,
885+
const jsmntok_t *toks)
886+
{
887+
const jsmntok_t *result_tok, *error_tok, *tx_tok;
888+
889+
if ((error_tok = json_get_member(buffer, toks, "error")) != NULL)
890+
/*FIXME(bitfinix): please return an error to openingd*/
891+
return false;
892+
893+
if ((result_tok = json_get_member(buffer, toks, "result")) == NULL)
894+
fatal("Plugin returned an invalid response to the"
895+
" onfunding_channel_tx hook: %.*s",
896+
toks[0].end - toks[0].start, buffer + toks[0].start);
897+
898+
if ((tx_tok = json_get_member(buffer, result_tok, "tx")) == NULL)
899+
fatal("Plugin returned an invalid response to the"
900+
" onfunding_channel_tx hook: %.*s",
901+
toks[0].end - toks[0].start, buffer + toks[0].start);
902+
903+
if (!json_to_tx(buffer, tx_tok, payload->tx))
904+
return false;
905+
return true;
906+
}
907+
908+
REGISTER_PLUGIN_HOOK(onfunding_channel_tx,
909+
onfunding_channel_tx_hook_deserialize,
910+
onfunding_channel_tx_hook_final,
911+
onfunding_channel_tx_hook_serialize,
912+
struct onfunding_channel_tx_hook_payload *);
913+
914+
915+
static char *opening_on_funding_tx(struct subd *openingd, const u8 *msg)
916+
{
917+
struct onfunding_channel_tx_hook_payload *payload;
918+
payload = tal(openingd, struct onfunding_channel_tx_hook_payload);
919+
payload->openingd = openingd;
920+
921+
if (!fromwire_openingd_on_funding_tx(msg, msg, &payload->tx))
922+
return tal_fmt(tmpctx, "Unexpected encoding of openingd_on_funding_tx msg: %s",
923+
tal_hex(tmpctx, msg));
924+
assert(plugin_hook_call_onfunding_channel_tx(openingd->ld, NULL, payload));
925+
// FIXME(bitfinix): return an error
926+
return NULL;
927+
}
928+
861929
static unsigned int openingd_msg(struct subd *openingd,
862930
const u8 *msg, const int *fds)
863931
{
@@ -898,13 +966,20 @@ static unsigned int openingd_msg(struct subd *openingd,
898966
case WIRE_OPENINGD_GOT_OFFER:
899967
opening_got_offer(openingd, msg, uc);
900968
return 0;
901-
969+
case WIRE_OPENINGD_ON_FUNDING_TX:
970+
const char *err;
971+
if ((err = opening_on_funding_tx(openingd, msg)) != NULL) {
972+
log_debug(openingd->log, "Unexpected error while handling on funding tx: %s", err);
973+
return 1;
974+
}
975+
return 0;
902976
/* We send these! */
903977
case WIRE_OPENINGD_INIT:
904978
case WIRE_OPENINGD_FUNDER_START:
905979
case WIRE_OPENINGD_FUNDER_COMPLETE:
906980
case WIRE_OPENINGD_FUNDER_CANCEL:
907981
case WIRE_OPENINGD_GOT_OFFER_REPLY:
982+
case WIRE_OPENINGD_ON_FUNDING_TX_REPLY:
908983
case WIRE_OPENINGD_DEV_MEMLEAK:
909984
/* Replies never get here */
910985
case WIRE_OPENINGD_DEV_MEMLEAK_REPLY:

openingd/openingd.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,12 @@ static bool funder_finalize_channel_setup(struct state *state,
703703
else
704704
*pbase = NULL;
705705

706-
// TODO(bitfinix): Send the commitment transaction to master and generate an hook
706+
msg = towire_openingd_on_funding_tx(tmpctx, *tx);
707+
wire_sync_write(REQ_FD, msg);
708+
msg = wire_sync_read(tmpctx, REQ_FD);
709+
if (!fromwire_openingd_on_funding_tx_reply(msg, msg, tx))
710+
status_failed(STATUS_FAIL_MASTER_IO, "Bad onfunding_tx %s",
711+
tal_hex(tmpctx, msg));
707712

708713
/* We ask the HSM to sign their commitment transaction for us: it knows
709714
* our funding key, it just needs the remote funding key to create the
@@ -817,6 +822,7 @@ static bool funder_finalize_channel_setup(struct state *state,
817822
goto fail;
818823
}
819824

825+
// TODO(bitfixnix): RGB for initial tx channel?
820826
validate_initial_commitment_signature(HSM_FD, *tx, sig);
821827

822828
if (!check_tx_sig(*tx, 0, NULL, wscript, &state->their_funding_pubkey, sig)) {
@@ -1503,6 +1509,8 @@ static u8 *handle_master_in(struct state *state)
15031509
case WIRE_OPENINGD_FAILED:
15041510
case WIRE_OPENINGD_GOT_OFFER:
15051511
case WIRE_OPENINGD_GOT_OFFER_REPLY:
1512+
case WIRE_OPENINGD_ON_FUNDING_TX:
1513+
case WIRE_OPENINGD_ON_FUNDING_TX_REPLY:
15061514
break;
15071515
}
15081516

openingd/openingd_wire.csv

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ msgdata,openingd_fundee,remote_shutdown_len,u16,
140140
msgdata,openingd_fundee,remote_shutdown_scriptpubkey,u8,remote_shutdown_len
141141
msgdata,openingd_fundee,channel_type,channel_type,
142142

143+
# Communicate the Funding Tx to master
144+
msgtype,openingd_on_funding_tx,6045
145+
msgdata,openingd_on_funding_tx,tx,bitcoin_tx,
146+
147+
# Get a reply from master -> openingd
148+
msgtype,openingd_on_funding_tx_reply,6145
149+
msgdata,openingd_on_funding_tx_reply,tx,bitcoin_tx,
150+
143151
# master -> openingd: do you have a memleak?
144152
msgtype,openingd_dev_memleak,6033
145153

0 commit comments

Comments
 (0)