Skip to content

Commit

Permalink
json: reverse parse a coin_mvt tag back into an enum
Browse files Browse the repository at this point in the history
  • Loading branch information
niftynei authored and rustyrussell committed Mar 5, 2022
1 parent 590f121 commit 7add7ca
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions common/coin_mvt.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ enum mvt_type {
CHANNEL_MVT = 1,
};

#define NUM_MVT_TAGS (LEASED + 1)
enum mvt_tag {
DEPOSIT = 0,
WITHDRAWAL = 1,
Expand Down
16 changes: 16 additions & 0 deletions common/json_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions common/json_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#define LIGHTNING_COMMON_JSON_HELPERS_H
#include "config.h"
#include <bitcoin/tx.h>
#include <common/coin_mvt.h>
#include <common/json.h>
#include <wire/wire.h>

Expand Down Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions common/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ 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: \
common/amount.o \
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 \
Expand Down
1 change: 1 addition & 0 deletions devtools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
1 change: 1 addition & 0 deletions gossipd/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
1 change: 1 addition & 0 deletions plugins/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down

0 comments on commit 7add7ca

Please sign in to comment.