Skip to content

Commit 7ad04a9

Browse files
ShahanaFarooquirustyrussell
authored andcommitted
commando: Save blacklist runes to datastore
1 parent a4ed3ae commit 7ad04a9

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

plugins/commando.c

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "config.h"
22
#include <ccan/array_size/array_size.h>
3+
#include <ccan/cast/cast.h>
34
#include <ccan/crypto/siphash24/siphash24.h>
45
#include <ccan/htable/htable_type.h>
56
#include <ccan/json_escape/json_escape.h>
@@ -963,6 +964,30 @@ static struct command_result *save_rune(struct command *cmd,
963964
forward_error, rune);
964965
}
965966

967+
static void towire_blacklist(u8 **pptr, const struct blacklist *b)
968+
{
969+
for (size_t i = 0; i < tal_count(b); i++) {
970+
towire_u64(pptr, b[i].start);
971+
towire_u64(pptr, b[i].end);
972+
}
973+
}
974+
975+
static struct blacklist *fromwire_blacklist(const tal_t *ctx,
976+
const u8 **cursor,
977+
size_t *max)
978+
{
979+
struct blacklist *blist = tal_arr(ctx, struct blacklist, 0);
980+
while (*max > 0) {
981+
struct blacklist b;
982+
b.start = fromwire_u64(cursor, max);
983+
b.end = fromwire_u64(cursor, max);
984+
tal_arr_expand(&blist, b);
985+
}
986+
if (!*cursor) {
987+
return tal_free(blist);
988+
}
989+
return blist;
990+
}
966991

967992
static struct command_result *json_commando_rune(struct command *cmd,
968993
const char *buffer,
@@ -1092,11 +1117,20 @@ static struct command_result *list_blacklist(struct command *cmd)
10921117
return command_finished(cmd, js);
10931118
}
10941119

1120+
static struct command_result *blacklist_save_done(struct command *cmd,
1121+
const char *buf,
1122+
const jsmntok_t *result,
1123+
void *unused)
1124+
{
1125+
return list_blacklist(cmd);
1126+
}
1127+
10951128
static struct command_result *json_commando_blacklist(struct command *cmd,
10961129
const char *buffer,
10971130
const jsmntok_t *params)
10981131
{
10991132
u64 *start, *end;
1133+
u8 *bwire;
11001134
struct blacklist *entry, *newblacklist;
11011135

11021136
if (!param(cmd, buffer, params,
@@ -1145,7 +1179,9 @@ static struct command_result *json_commando_blacklist(struct command *cmd,
11451179
}
11461180
tal_free(blacklist);
11471181
blacklist = newblacklist;
1148-
return list_blacklist(cmd);
1182+
bwire = tal_arr(tmpctx, u8, 0);
1183+
towire_blacklist(&bwire, blacklist);
1184+
return jsonrpc_set_datastore_binary(cmd->plugin, cmd, "commando/blacklist", bwire, "create-or-replace", blacklist_save_done, NULL, NULL);
11491185
}
11501186

11511187
static struct command_result *json_commando_listrunes(struct command *cmd,
@@ -1186,7 +1222,19 @@ static const char *init(struct plugin *p,
11861222
{
11871223
struct secret rune_secret;
11881224
const char *err;
1189-
1225+
u8 *bwire;
1226+
1227+
if (rpc_scan_datastore_hex(tmpctx, p, "commando/blacklist",
1228+
JSON_SCAN_TAL(tmpctx, json_tok_bin_from_hex,
1229+
&bwire)) == NULL) {
1230+
size_t max = tal_bytelen(bwire);
1231+
blacklist = fromwire_blacklist(p, cast_const2(const u8 **,
1232+
&bwire),
1233+
&max);
1234+
if (blacklist == NULL) {
1235+
plugin_err(p, "Invalid commando/blacklist");
1236+
}
1237+
}
11901238
outgoing_commands = tal_arr(p, struct commando *, 0);
11911239
incoming_commands = tal_arr(p, struct commando *, 0);
11921240
usage_table = tal(p, struct usage_table);

0 commit comments

Comments
 (0)