From 7add7ca199e19a486884f3fd6d80c562586e1749 Mon Sep 17 00:00:00 2001 From: niftynei Date: Wed, 16 Feb 2022 15:36:47 -0600 Subject: [PATCH] json: reverse parse a coin_mvt tag back into an enum --- common/coin_mvt.h | 1 + common/json_helpers.c | 16 ++++++++++++++++ common/json_helpers.h | 5 +++++ common/test/Makefile | 5 +++-- devtools/Makefile | 1 + gossipd/test/Makefile | 1 + plugins/Makefile | 1 + 7 files changed, 28 insertions(+), 2 deletions(-) diff --git a/common/coin_mvt.h b/common/coin_mvt.h index 1f0aa9a581d0..0995c9edaff0 100644 --- a/common/coin_mvt.h +++ b/common/coin_mvt.h @@ -14,6 +14,7 @@ enum mvt_type { CHANNEL_MVT = 1, }; +#define NUM_MVT_TAGS (LEASED + 1) enum mvt_tag { DEPOSIT = 0, WITHDRAWAL = 1, diff --git a/common/json_helpers.c b/common/json_helpers.c index 8d9cb35224c4..af06c2036a2e 100644 --- a/common/json_helpers.c +++ b/common/json_helpers.c @@ -108,6 +108,22 @@ bool json_to_channel_id(const char *buffer, const jsmntok_t *tok, cid, sizeof(*cid)); } + +bool json_to_coin_mvt_tag(const char *buffer, const jsmntok_t *tok, + enum mvt_tag *tag) +{ + enum mvt_tag i_tag; + for (size_t i = 0; i < NUM_MVT_TAGS; i++) { + i_tag = (enum mvt_tag) i; + if (json_tok_streq(buffer, tok, mvt_tag_str(i_tag))) { + *tag = i_tag; + return true; + } + } + + return false; +} + bool split_tok(const char *buffer, const jsmntok_t *tok, char split, jsmntok_t *a, diff --git a/common/json_helpers.h b/common/json_helpers.h index e37cb95e835e..1f5fe7fe3239 100644 --- a/common/json_helpers.h +++ b/common/json_helpers.h @@ -3,6 +3,7 @@ #define LIGHTNING_COMMON_JSON_HELPERS_H #include "config.h" #include +#include #include #include @@ -73,6 +74,10 @@ bool json_to_outpoint(const char *buffer, const jsmntok_t *tok, bool json_to_channel_id(const char *buffer, const jsmntok_t *tok, struct channel_id *cid); +/* Extract a coin movement 'tag' from this */ +bool json_to_coin_mvt_tag(const char *buffer, const jsmntok_t *tok, + enum mvt_tag *tag); + /* Split a json token into 2 tokens given a splitting character */ bool split_tok(const char *buffer, const jsmntok_t *tok, char split, diff --git a/common/test/Makefile b/common/test/Makefile index c3959b52af9d..ae2803cf5efa 100644 --- a/common/test/Makefile +++ b/common/test/Makefile @@ -21,8 +21,8 @@ ALL_TEST_PROGRAMS += $(COMMON_TEST_PROGRAMS) # Sphinx test wants to decode TLVs. common/test/run-sphinx: wire/onion$(EXP)_wiregen.o wire/towire.o wire/fromwire.o common/test/run-blindedpath_enctlv common/test/run-blindedpath_onion: common/base32.o common/wireaddr.o wire/onion$(EXP)_wiregen.o wire/peer$(EXP)_wiregen.o wire/towire.o wire/fromwire.o wire/tlvstream.o -common/test/run-route_blinding_test: wire/onion$(EXP)_wiregen.o wire/peer$(EXP)_wiregen.o wire/towire.o wire/fromwire.o wire/tlvstream.o common/json.o common/json_helpers.o -common/test/run-route_blinding_override_test: common/base32.o common/wireaddr.o wire/onion$(EXP)_wiregen.o wire/peer$(EXP)_wiregen.o wire/towire.o wire/fromwire.o wire/tlvstream.o common/json.o common/json_helpers.o +common/test/run-route_blinding_test: wire/onion$(EXP)_wiregen.o wire/peer$(EXP)_wiregen.o wire/towire.o wire/fromwire.o wire/tlvstream.o common/json.o common/json_helpers.o common/coin_mvt.o +common/test/run-route_blinding_override_test: common/base32.o common/wireaddr.o wire/onion$(EXP)_wiregen.o wire/peer$(EXP)_wiregen.o wire/towire.o wire/fromwire.o wire/tlvstream.o common/json.o common/json_helpers.o common/coin_mvt.o common/test/run-param \ common/test/run-json: \ @@ -30,6 +30,7 @@ common/test/run-json: \ common/base32.o \ common/bigsize.o \ common/channel_id.o \ + common/coin_mvt.o \ common/json.o \ common/json_stream.o \ common/lease_rates.o \ diff --git a/devtools/Makefile b/devtools/Makefile index 5498db4684ce..90dd7c7b45a5 100644 --- a/devtools/Makefile +++ b/devtools/Makefile @@ -14,6 +14,7 @@ ALL_PROGRAMS += $(DEVTOOLS) DEVTOOLS_COMMON_OBJS := \ common/amount.o \ common/autodata.o \ + common/coin_mvt.o \ common/base32.o \ common/bech32.o \ common/bech32_util.o \ diff --git a/gossipd/test/Makefile b/gossipd/test/Makefile index 6c4f150b894f..e91b5c2ce322 100644 --- a/gossipd/test/Makefile +++ b/gossipd/test/Makefile @@ -9,6 +9,7 @@ GOSSIPD_TEST_PROGRAMS := $(GOSSIPD_TEST_OBJS:.o=) GOSSIPD_TEST_COMMON_OBJS := \ common/amount.o \ common/autodata.o \ + common/coin_mvt.o \ common/bigsize.o \ common/blindedpath.o \ common/channel_id.o \ diff --git a/plugins/Makefile b/plugins/Makefile index 3562243cd8c4..3707e7688c69 100644 --- a/plugins/Makefile +++ b/plugins/Makefile @@ -108,6 +108,7 @@ PLUGIN_COMMON_OBJS := \ bitcoin/varint.o \ common/amount.o \ common/autodata.o \ + common/coin_mvt.o \ common/base32.o \ common/bech32.o \ common/bech32_util.o \