diff --git a/contrib/pyln-testing/pyln/testing/utils.py b/contrib/pyln-testing/pyln/testing/utils.py index 5fca522c2829..c60a571d7d65 100644 --- a/contrib/pyln-testing/pyln/testing/utils.py +++ b/contrib/pyln-testing/pyln/testing/utils.py @@ -619,7 +619,7 @@ def getnewaddress(self): class ValidatingLightningSignerD(TailableProc): - def __init__(self, vlsd_dir, vlsd_port, node_id, network): + def __init__(self, vlsd_dir, vlsd_port, vlsd_rpc_port, node_id, network): TailableProc.__init__(self, vlsd_dir, verbose=True) self.executable = env("REMOTE_SIGNER_CMD", 'vlsd2') os.environ['ALLOWLIST'] = env( @@ -629,6 +629,7 @@ def __init__(self, vlsd_dir, vlsd_port, node_id, network): '--network={}'.format(network), '--datadir={}'.format(vlsd_dir), '--connect=http://localhost:{}'.format(vlsd_port), + '--rpc-server-port={}'.format(vlsd_rpc_port), '--integration-test', ] self.prefix = 'vlsd2-%d' % (node_id) @@ -676,6 +677,7 @@ def __init__( self.use_vlsd = False self.vlsd_dir = os.path.join(lightning_dir, "vlsd") self.vlsd_port = None + self.vlsd_rpc_server_port = None self.vlsd = None self.node_id = node_id @@ -794,6 +796,7 @@ def start(self, stdin=None, wait_for_initialized=True, stderr_redir=False): if self.use_vlsd: self.vlsd_port = reserve_unused_port() + self.vlsd_rpc_server_port = reserve_unused_port() # We can't do this in the constructor because we need a new port on each restart. self.env['VLS_PORT'] = str(self.vlsd_port) # Kill any previous vlsd (we may have been restarted) @@ -806,7 +809,7 @@ def start(self, stdin=None, wait_for_initialized=True, stderr_redir=False): if self.use_vlsd: # Start the remote signer first self.vlsd = ValidatingLightningSignerD( - self.vlsd_dir, self.vlsd_port, self.node_id, self.opts['network']) + self.vlsd_dir, self.vlsd_port, self.vlsd_rpc_server_port, self.node_id, self.opts['network']) self.vlsd.start( stdin, stdout_redir=True, stderr_redir=True, wait_for_initialized=wait_for_initialized) diff --git a/hsmd/hsmd.c b/hsmd/hsmd.c index 92c728efd1b4..29b4b066aeb7 100644 --- a/hsmd/hsmd.c +++ b/hsmd/hsmd.c @@ -647,6 +647,7 @@ static struct io_plan *handle_client(struct io_conn *conn, struct client *c) case WIRE_HSMD_SETUP_CHANNEL: case WIRE_HSMD_CHECK_OUTPOINT: case WIRE_HSMD_LOCK_OUTPOINT: + case WIRE_HSMD_FORGET_CHANNEL: case WIRE_HSMD_SIGN_COMMITMENT_TX: case WIRE_HSMD_VALIDATE_COMMITMENT_TX: case WIRE_HSMD_VALIDATE_REVOCATION: @@ -694,6 +695,7 @@ static struct io_plan *handle_client(struct io_conn *conn, struct client *c) case WIRE_HSMD_SETUP_CHANNEL_REPLY: case WIRE_HSMD_CHECK_OUTPOINT_REPLY: case WIRE_HSMD_LOCK_OUTPOINT_REPLY: + case WIRE_HSMD_FORGET_CHANNEL_REPLY: case WIRE_HSMD_NODE_ANNOUNCEMENT_SIG_REPLY: case WIRE_HSMD_SIGN_WITHDRAWAL_REPLY: case WIRE_HSMD_SIGN_INVOICE_REPLY: diff --git a/hsmd/hsmd_wire.csv b/hsmd/hsmd_wire.csv index d31287b32580..ca97f9c72a23 100644 --- a/hsmd/hsmd_wire.csv +++ b/hsmd/hsmd_wire.csv @@ -105,6 +105,14 @@ msgdata,hsmd_lock_outpoint,funding_txout,u16, # No value returned. msgtype,hsmd_lock_outpoint_reply,137 +# Forget channel. +msgtype,hsmd_forget_channel,34 +msgdata,hsmd_forget_channel,id,node_id, +msgdata,hsmd_forget_channel,dbid,u64, + +# No value returned. +msgtype,hsmd_forget_channel_reply,134 + # Return signature for a funding tx. #include diff --git a/hsmd/libhsmd.c b/hsmd/libhsmd.c index 0b7a9f5cba80..9ca742c73212 100644 --- a/hsmd/libhsmd.c +++ b/hsmd/libhsmd.c @@ -118,6 +118,7 @@ bool hsmd_check_client_capabilities(struct hsmd_client *client, case WIRE_HSMD_INIT: case WIRE_HSMD_NEW_CHANNEL: + case WIRE_HSMD_FORGET_CHANNEL: case WIRE_HSMD_CLIENT_HSMFD: case WIRE_HSMD_SIGN_WITHDRAWAL: case WIRE_HSMD_SIGN_INVOICE: @@ -150,6 +151,7 @@ bool hsmd_check_client_capabilities(struct hsmd_client *client, case WIRE_HSMD_SETUP_CHANNEL_REPLY: case WIRE_HSMD_CHECK_OUTPOINT_REPLY: case WIRE_HSMD_LOCK_OUTPOINT_REPLY: + case WIRE_HSMD_FORGET_CHANNEL_REPLY: case WIRE_HSMD_NODE_ANNOUNCEMENT_SIG_REPLY: case WIRE_HSMD_SIGN_WITHDRAWAL_REPLY: case WIRE_HSMD_SIGN_INVOICE_REPLY: @@ -382,6 +384,21 @@ static u8 *handle_setup_channel(struct hsmd_client *c, const u8 *msg_in) return towire_hsmd_setup_channel_reply(NULL); } +/* ~This stub implementation is overriden by fully validating signers + * that need to manage per-channel state. */ +static u8 *handle_forget_channel(struct hsmd_client *c, const u8 *msg_in) +{ + struct node_id peer_id; + u64 dbid; + + if (!fromwire_hsmd_forget_channel(msg_in, &peer_id, &dbid)) + return hsmd_status_malformed_request(c, msg_in); + + /* Stub implementation */ + + return towire_hsmd_forget_channel_reply(NULL); +} + /* ~This stub implementation is overriden by fully validating signers * to ensure they are caught up when outpoints are freshly buried */ static u8 *handle_check_outpoint(struct hsmd_client *c, const u8 *msg_in) @@ -1945,6 +1962,8 @@ u8 *hsmd_handle_client_message(const tal_t *ctx, struct hsmd_client *client, return handle_check_outpoint(client, msg); case WIRE_HSMD_LOCK_OUTPOINT: return handle_lock_outpoint(client, msg); + case WIRE_HSMD_FORGET_CHANNEL: + return handle_forget_channel(client, msg); case WIRE_HSMD_GET_OUTPUT_SCRIPTPUBKEY: return handle_get_output_scriptpubkey(client, msg); case WIRE_HSMD_CHECK_FUTURE_SECRET: @@ -2024,6 +2043,7 @@ u8 *hsmd_handle_client_message(const tal_t *ctx, struct hsmd_client *client, case WIRE_HSMD_SETUP_CHANNEL_REPLY: case WIRE_HSMD_CHECK_OUTPOINT_REPLY: case WIRE_HSMD_LOCK_OUTPOINT_REPLY: + case WIRE_HSMD_FORGET_CHANNEL_REPLY: case WIRE_HSMD_NODE_ANNOUNCEMENT_SIG_REPLY: case WIRE_HSMD_SIGN_WITHDRAWAL_REPLY: case WIRE_HSMD_SIGN_INVOICE_REPLY: @@ -2067,6 +2087,7 @@ u8 *hsmd_init(struct secret hsm_secret, WIRE_HSMD_SIGN_HTLC_TX_MINGLE, WIRE_HSMD_SIGN_SPLICE_TX, WIRE_HSMD_CHECK_OUTPOINT, + WIRE_HSMD_FORGET_CHANNEL, }; /*~ Don't swap this. */ diff --git a/lightningd/channel.c b/lightningd/channel.c index a20b3f5387c9..e9198d588242 100644 --- a/lightningd/channel.c +++ b/lightningd/channel.c @@ -89,9 +89,21 @@ static void destroy_channel(struct channel *channel) void delete_channel(struct channel *channel STEALS) { + const u8 *msg; + struct peer *peer = channel->peer; if (channel->dbid != 0) wallet_channel_close(channel->peer->ld->wallet, channel->dbid); + + /* Tell the hsm to forget the channel, needs to be after it's + * been forgotten here */ + if (hsm_capable(channel->peer->ld, WIRE_HSMD_FORGET_CHANNEL)) { + msg = towire_hsmd_forget_channel(NULL, &channel->peer->id, channel->dbid); + msg = hsm_sync_req(tmpctx, channel->peer->ld, take(msg)); + if (!fromwire_hsmd_forget_channel_reply(msg)) + fatal("HSM gave bad hsm_forget_channel_reply %s", tal_hex(msg, msg)); + } + tal_free(channel); maybe_delete_peer(peer); diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 42cec47f1ef0..41c994a741de 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -272,6 +272,9 @@ bool fromwire_hsmd_init_reply_v4(const tal_t *ctx UNNEEDED, const void *p UNNEED /* Generated stub for fromwire_hsmd_new_channel_reply */ bool fromwire_hsmd_new_channel_reply(const void *p UNNEEDED) { fprintf(stderr, "fromwire_hsmd_new_channel_reply called!\n"); abort(); } +/* Generated stub for fromwire_hsmd_forget_channel_reply */ +bool fromwire_hsmd_forget_channel_reply(const void *p UNNEEDED) +{ fprintf(stderr, "fromwire_hsmd_forget_channel_reply called!\n"); abort(); } /* Generated stub for fromwire_hsmd_sign_commitment_tx_reply */ bool fromwire_hsmd_sign_commitment_tx_reply(const void *p UNNEEDED, struct bitcoin_signature *sig UNNEEDED) { fprintf(stderr, "fromwire_hsmd_sign_commitment_tx_reply called!\n"); abort(); } @@ -1008,6 +1011,9 @@ u8 *towire_hsmd_init(const tal_t *ctx UNNEEDED, const struct bip32_key_version * /* Generated stub for towire_hsmd_new_channel */ u8 *towire_hsmd_new_channel(const tal_t *ctx UNNEEDED, const struct node_id *id UNNEEDED, u64 dbid UNNEEDED) { fprintf(stderr, "towire_hsmd_new_channel called!\n"); abort(); } +/* Generated stub for towire_hsmd_forget_channel */ +u8 *towire_hsmd_forget_channel(const tal_t *ctx UNNEEDED, const struct node_id *id UNNEEDED, u64 dbid UNNEEDED) +{ fprintf(stderr, "towire_hsmd_forget_channel called!\n"); abort(); } /* Generated stub for towire_hsmd_sign_commitment_tx */ u8 *towire_hsmd_sign_commitment_tx(const tal_t *ctx UNNEEDED, const struct node_id *peer_id UNNEEDED, u64 channel_dbid UNNEEDED, const struct bitcoin_tx *tx UNNEEDED, const struct pubkey *remote_funding_key UNNEEDED, u64 commit_num UNNEEDED) { fprintf(stderr, "towire_hsmd_sign_commitment_tx called!\n"); abort(); }