Skip to content

Commit

Permalink
common: move json_add_channel_type from lightningd/
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Jan 29, 2024
1 parent 08e29ab commit 0b38b83
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 31 deletions.
16 changes: 16 additions & 0 deletions common/json_channel_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ struct command_result *param_channel_type(struct command *cmd,
return NULL;
}

void json_add_channel_type(struct json_stream *response,
const char *fieldname,
const struct channel_type *channel_type)
{
const char **fnames;

json_object_start(response, fieldname);
json_add_channel_type_arr(response, "bits", channel_type);

json_array_start(response, "names");
fnames = channel_type_name(tmpctx, channel_type);
for (size_t i = 0; i < tal_count(fnames); i++)
json_add_string(response, NULL, fnames[i]);
json_array_end(response);
json_object_end(response);
}
void json_add_channel_type_arr(struct json_stream *response,
const char *fieldname,
const struct channel_type *ctype)
Expand Down
6 changes: 6 additions & 0 deletions common/json_channel_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ struct command_result *param_channel_type(struct command *cmd,
void json_add_channel_type_arr(struct json_stream *response,
const char *fieldname,
const struct channel_type *ctype);


/* Add channel_type object, with array and names */
void json_add_channel_type(struct json_stream *response,
const char *fieldname,
const struct channel_type *channel_type);
#endif /* LIGHTNING_COMMON_JSON_CHANNEL_TYPE_H */
1 change: 1 addition & 0 deletions common/json_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct bip340sig;
struct secret;
struct node_id;
struct channel_id;
struct channel_type;
struct bitcoin_txid;
struct bitcoin_outpoint;
struct short_channel_id;
Expand Down
1 change: 1 addition & 0 deletions lightningd/closed_channel.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "config.h"
#include <common/json_channel_type.h>
#include <common/json_command.h>
#include <common/json_param.h>
#include <common/json_stream.h>
Expand Down
24 changes: 1 addition & 23 deletions lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <common/features.h>
#include <common/htlc_trim.h>
#include <common/initial_commit_tx.h>
#include <common/json_channel_type.h>
#include <common/json_command.h>
#include <common/json_param.h>
#include <common/jsonrpc_errors.h>
Expand Down Expand Up @@ -805,29 +806,6 @@ struct amount_msat channel_amount_receivable(const struct channel *channel)
return receivable;
}

void json_add_channel_type(struct json_stream *response,
const char *fieldname,
const struct channel_type *channel_type)
{
const char **fnames;

json_object_start(response, fieldname);
json_array_start(response, "bits");
for (size_t i = 0; i < tal_bytelen(channel_type->features) * CHAR_BIT; i++) {
if (!feature_is_set(channel_type->features, i))
continue;
json_add_u64(response, NULL, i);
}
json_array_end(response);

json_array_start(response, "names");
fnames = channel_type_name(tmpctx, channel_type);
for (size_t i = 0; i < tal_count(fnames); i++)
json_add_string(response, NULL, fnames[i]);
json_array_end(response);
json_object_end(response);
}

static void json_add_channel(struct lightningd *ld,
struct json_stream *response, const char *key,
const struct channel *channel,
Expand Down
5 changes: 0 additions & 5 deletions lightningd/peer_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,6 @@ command_find_channel(struct command *cmd,
const char *buffer, const jsmntok_t *tok,
struct channel **channel);

/* Add channel_type object */
void json_add_channel_type(struct json_stream *response,
const char *fieldname,
const struct channel_type *channel_type);

/* Ancient (0.7.0 and before) releases could create invalid commitment txs! */
bool invalid_last_tx(const struct bitcoin_tx *tx);

Expand Down
8 changes: 5 additions & 3 deletions lightningd/test/run-invoice-select-inchan.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ const char *channel_state_str(enum channel_state state UNNEEDED)
/* Generated stub for channel_type_has */
bool channel_type_has(const struct channel_type *type UNNEEDED, int feature UNNEEDED)
{ fprintf(stderr, "channel_type_has called!\n"); abort(); }
/* Generated stub for channel_type_name */
const char **channel_type_name(const tal_t *ctx UNNEEDED, const struct channel_type *t UNNEEDED)
{ fprintf(stderr, "channel_type_name called!\n"); abort(); }
/* Generated stub for channel_unsaved_close_conn */
void channel_unsaved_close_conn(struct channel *channel UNNEEDED, const char *why UNNEEDED)
{ fprintf(stderr, "channel_unsaved_close_conn called!\n"); abort(); }
Expand Down Expand Up @@ -480,6 +477,11 @@ void json_add_bolt11(struct json_stream *response UNNEEDED,
void json_add_bool(struct json_stream *result UNNEEDED, const char *fieldname UNNEEDED,
bool value UNNEEDED)
{ fprintf(stderr, "json_add_bool called!\n"); abort(); }
/* Generated stub for json_add_channel_type */
void json_add_channel_type(struct json_stream *response UNNEEDED,
const char *fieldname UNNEEDED,
const struct channel_type *channel_type UNNEEDED)
{ fprintf(stderr, "json_add_channel_type called!\n"); abort(); }
/* Generated stub for json_add_escaped_string */
void json_add_escaped_string(struct json_stream *result UNNEEDED,
const char *fieldname UNNEEDED,
Expand Down
5 changes: 5 additions & 0 deletions wallet/test/run-wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,11 @@ void json_add_channel_id(struct json_stream *response UNNEEDED,
const char *fieldname UNNEEDED,
const struct channel_id *cid UNNEEDED)
{ fprintf(stderr, "json_add_channel_id called!\n"); abort(); }
/* Generated stub for json_add_channel_type */
void json_add_channel_type(struct json_stream *response UNNEEDED,
const char *fieldname UNNEEDED,
const struct channel_type *channel_type UNNEEDED)
{ fprintf(stderr, "json_add_channel_type called!\n"); abort(); }
/* Generated stub for json_add_hex */
void json_add_hex(struct json_stream *result UNNEEDED, const char *fieldname UNNEEDED,
const void *data UNNEEDED, size_t len UNNEEDED)
Expand Down

0 comments on commit 0b38b83

Please sign in to comment.