From 454900210593384bcca35338b5819ad493d969c0 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 20 Mar 2023 10:49:27 +1030 Subject: [PATCH] common: expose routine to map channel_type to feature names. Signed-off-by: Rusty Russell --- common/channel_type.c | 14 ++++ common/channel_type.h | 3 + common/test/run-channel_type.c | 130 +++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 common/test/run-channel_type.c diff --git a/common/channel_type.c b/common/channel_type.c index 3a2574a03..920aeb99c 100644 --- a/common/channel_type.c +++ b/common/channel_type.c @@ -166,3 +166,17 @@ struct channel_type *channel_type_accept(const tal_t *ctx, return NULL; } + +/* Return an array of feature strings indicating channel type. */ +const char **channel_type_name(const tal_t *ctx, const struct channel_type *t) +{ + const char **names = tal_arr(ctx, const char *, 0); + + for (size_t i = 0; i < tal_bytelen(t->features) * CHAR_BIT; i++) { + if (!feature_is_set(t->features, i)) + continue; + tal_arr_expand(&names, + feature_name(names, i) + strlen("option_")); + } + return names; +} diff --git a/common/channel_type.h b/common/channel_type.h index 858dc6471..2f59df10b 100644 --- a/common/channel_type.h +++ b/common/channel_type.h @@ -35,4 +35,7 @@ struct channel_type *channel_type_accept(const tal_t *ctx, const u8 *t, const struct feature_set *our_features, const u8 *their_features); + +/* Return an array of feature strings indicating channel type. */ +const char **channel_type_name(const tal_t *ctx, const struct channel_type *t); #endif /* LIGHTNING_COMMON_CHANNEL_TYPE_H */ diff --git a/common/test/run-channel_type.c b/common/test/run-channel_type.c new file mode 100644 index 000000000..24e95686f --- /dev/null +++ b/common/test/run-channel_type.c @@ -0,0 +1,130 @@ +#include "config.h" +#include "../channel_type.c" +#include "../features.c" +#include +#include +#include + +/* AUTOGENERATED MOCKS START */ +/* Generated stub for amount_asset_is_main */ +bool amount_asset_is_main(struct amount_asset *asset UNNEEDED) +{ fprintf(stderr, "amount_asset_is_main called!\n"); abort(); } +/* Generated stub for amount_asset_to_sat */ +struct amount_sat amount_asset_to_sat(struct amount_asset *asset UNNEEDED) +{ fprintf(stderr, "amount_asset_to_sat called!\n"); abort(); } +/* Generated stub for amount_sat */ +struct amount_sat amount_sat(u64 satoshis UNNEEDED) +{ fprintf(stderr, "amount_sat called!\n"); abort(); } +/* Generated stub for amount_sat_add */ + bool amount_sat_add(struct amount_sat *val UNNEEDED, + struct amount_sat a UNNEEDED, + struct amount_sat b UNNEEDED) +{ fprintf(stderr, "amount_sat_add called!\n"); abort(); } +/* Generated stub for amount_sat_eq */ +bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) +{ fprintf(stderr, "amount_sat_eq called!\n"); abort(); } +/* Generated stub for amount_sat_greater_eq */ +bool amount_sat_greater_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) +{ fprintf(stderr, "amount_sat_greater_eq called!\n"); abort(); } +/* Generated stub for amount_sat_sub */ + bool amount_sat_sub(struct amount_sat *val UNNEEDED, + struct amount_sat a UNNEEDED, + struct amount_sat b UNNEEDED) +{ fprintf(stderr, "amount_sat_sub called!\n"); abort(); } +/* Generated stub for amount_sat_to_asset */ +struct amount_asset amount_sat_to_asset(struct amount_sat *sat UNNEEDED, const u8 *asset UNNEEDED) +{ fprintf(stderr, "amount_sat_to_asset called!\n"); abort(); } +/* Generated stub for amount_tx_fee */ +struct amount_sat amount_tx_fee(u32 fee_per_kw UNNEEDED, size_t weight UNNEEDED) +{ fprintf(stderr, "amount_tx_fee called!\n"); abort(); } +/* Generated stub for fromwire */ +const u8 *fromwire(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, void *copy UNNEEDED, size_t n UNNEEDED) +{ fprintf(stderr, "fromwire called!\n"); abort(); } +/* Generated stub for fromwire_bool */ +bool fromwire_bool(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) +{ fprintf(stderr, "fromwire_bool called!\n"); abort(); } +/* Generated stub for fromwire_fail */ +void *fromwire_fail(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) +{ fprintf(stderr, "fromwire_fail called!\n"); abort(); } +/* Generated stub for fromwire_secp256k1_ecdsa_signature */ +void fromwire_secp256k1_ecdsa_signature(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, + secp256k1_ecdsa_signature *signature UNNEEDED) +{ fprintf(stderr, "fromwire_secp256k1_ecdsa_signature called!\n"); abort(); } +/* Generated stub for fromwire_sha256 */ +void fromwire_sha256(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct sha256 *sha256 UNNEEDED) +{ fprintf(stderr, "fromwire_sha256 called!\n"); abort(); } +/* Generated stub for fromwire_tal_arrn */ +u8 *fromwire_tal_arrn(const tal_t *ctx UNNEEDED, + const u8 **cursor UNNEEDED, size_t *max UNNEEDED, size_t num UNNEEDED) +{ fprintf(stderr, "fromwire_tal_arrn called!\n"); abort(); } +/* Generated stub for fromwire_u16 */ +u16 fromwire_u16(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) +{ fprintf(stderr, "fromwire_u16 called!\n"); abort(); } +/* Generated stub for fromwire_u32 */ +u32 fromwire_u32(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) +{ fprintf(stderr, "fromwire_u32 called!\n"); abort(); } +/* Generated stub for fromwire_u64 */ +u64 fromwire_u64(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) +{ fprintf(stderr, "fromwire_u64 called!\n"); abort(); } +/* Generated stub for fromwire_u8 */ +u8 fromwire_u8(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) +{ fprintf(stderr, "fromwire_u8 called!\n"); abort(); } +/* Generated stub for fromwire_u8_array */ +void fromwire_u8_array(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, u8 *arr UNNEEDED, size_t num UNNEEDED) +{ fprintf(stderr, "fromwire_u8_array called!\n"); abort(); } +/* Generated stub for towire */ +void towire(u8 **pptr UNNEEDED, const void *data UNNEEDED, size_t len UNNEEDED) +{ fprintf(stderr, "towire called!\n"); abort(); } +/* Generated stub for towire_bool */ +void towire_bool(u8 **pptr UNNEEDED, bool v UNNEEDED) +{ fprintf(stderr, "towire_bool called!\n"); abort(); } +/* Generated stub for towire_secp256k1_ecdsa_signature */ +void towire_secp256k1_ecdsa_signature(u8 **pptr UNNEEDED, + const secp256k1_ecdsa_signature *signature UNNEEDED) +{ fprintf(stderr, "towire_secp256k1_ecdsa_signature called!\n"); abort(); } +/* Generated stub for towire_sha256 */ +void towire_sha256(u8 **pptr UNNEEDED, const struct sha256 *sha256 UNNEEDED) +{ fprintf(stderr, "towire_sha256 called!\n"); abort(); } +/* Generated stub for towire_u16 */ +void towire_u16(u8 **pptr UNNEEDED, u16 v UNNEEDED) +{ fprintf(stderr, "towire_u16 called!\n"); abort(); } +/* Generated stub for towire_u32 */ +void towire_u32(u8 **pptr UNNEEDED, u32 v UNNEEDED) +{ fprintf(stderr, "towire_u32 called!\n"); abort(); } +/* Generated stub for towire_u64 */ +void towire_u64(u8 **pptr UNNEEDED, u64 v UNNEEDED) +{ fprintf(stderr, "towire_u64 called!\n"); abort(); } +/* Generated stub for towire_u8 */ +void towire_u8(u8 **pptr UNNEEDED, u8 v UNNEEDED) +{ fprintf(stderr, "towire_u8 called!\n"); abort(); } +/* Generated stub for towire_u8_array */ +void towire_u8_array(u8 **pptr UNNEEDED, const u8 *arr UNNEEDED, size_t num UNNEEDED) +{ fprintf(stderr, "towire_u8_array called!\n"); abort(); } +/* AUTOGENERATED MOCKS END */ + +static void assert_names_eq(const char **names, const char *expected) +{ + char **expected_names = tal_strsplit(tmpctx, expected, " ", STR_EMPTY_OK); + + assert(tal_count(expected_names) == tal_count(names) + 1); + for (size_t i = 0; i < tal_count(names); i++) + assert(streq(expected_names[i], names[i])); +} + +int main(int argc, char *argv[]) +{ + struct channel_type t; + + common_setup(argv[0]); + + assert_names_eq(channel_type_name(tmpctx, channel_type_none(tmpctx)), ""); + assert_names_eq(channel_type_name(tmpctx, channel_type_static_remotekey(tmpctx)), + "static_remotekey/even"); + assert_names_eq(channel_type_name(tmpctx, channel_type_anchor_outputs(tmpctx)), + "static_remotekey/even anchor_outputs/even"); + + t.features = tal_arr(tmpctx, u8, 0); + set_feature_bit(&t.features, 1000); + assert_names_eq(channel_type_name(tmpctx, &t), "unknown_1000/even"); + common_shutdown(); +}