Skip to content

Commit fb86529

Browse files
ShahanaFarooquirustyrussell
authored andcommitted
commando: blacklist support
Does not yet persist the blacklist. Changelog-Added: Plugins: `commando-blacklist` command to disable select runes.
1 parent 183fbb4 commit fb86529

File tree

1 file changed

+102
-1
lines changed

1 file changed

+102
-1
lines changed

plugins/commando.c

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,16 @@ struct commando {
4242
const char *json_id;
4343
};
4444

45+
struct blacklist {
46+
u64 start, end;
47+
};
48+
4549
static struct plugin *plugin;
4650
static struct commando **outgoing_commands;
4751
static struct commando **incoming_commands;
4852
static u64 *rune_counter;
4953
static struct rune *master_rune;
54+
static struct blacklist *blacklist;
5055

5156
struct usage {
5257
/* If you really issue more than 2^32 runes, they'll share ratelimit buckets */
@@ -1034,6 +1039,94 @@ static struct command_result *listdatastore_done(struct command *cmd,
10341039
return command_finished(cmd, js);
10351040
}
10361041

1042+
static void blacklist_merge(struct blacklist *blacklist,
1043+
const struct blacklist *entry)
1044+
{
1045+
if (entry->start < blacklist->start) {
1046+
blacklist->start = entry->start;
1047+
}
1048+
if (entry->end > blacklist->end) {
1049+
blacklist->end = entry->end;
1050+
}
1051+
}
1052+
1053+
static bool blacklist_before(const struct blacklist *first,
1054+
const struct blacklist *second)
1055+
{
1056+
// Is it before with a gap
1057+
return (first->end + 1) < second->start;
1058+
}
1059+
1060+
static struct command_result *list_blacklist(struct command *cmd)
1061+
{
1062+
struct json_stream *js = jsonrpc_stream_success(cmd);
1063+
json_array_start(js, "blacklist");
1064+
for (size_t i = 0; i < tal_count(blacklist); i++) {
1065+
json_object_start(js, NULL);
1066+
json_add_u64(js, "start", blacklist[i].start);
1067+
json_add_u64(js, "end", blacklist[i].end);
1068+
json_object_end(js);
1069+
}
1070+
json_array_end(js);
1071+
return command_finished(cmd, js);
1072+
}
1073+
1074+
static struct command_result *json_commando_blacklist(struct command *cmd,
1075+
const char *buffer,
1076+
const jsmntok_t *params)
1077+
{
1078+
u64 *start, *end;
1079+
struct blacklist *entry, *newblacklist;
1080+
1081+
if (!param(cmd, buffer, params,
1082+
p_opt("start", param_u64, &start), p_opt("end", param_u64, &end), NULL))
1083+
return command_param_failed();
1084+
1085+
if (end && !start) {
1086+
return command_fail(cmd, JSONRPC2_INVALID_PARAMS, "Can not specify end without start");
1087+
}
1088+
if (!start) {
1089+
return list_blacklist(cmd);
1090+
}
1091+
if (!end) {
1092+
end = start;
1093+
}
1094+
entry = tal(cmd, struct blacklist);
1095+
entry->start = *start;
1096+
entry->end = *end;
1097+
1098+
newblacklist = tal_arr(cmd->plugin, struct blacklist, 0);
1099+
1100+
for (size_t i = 0; i < tal_count(blacklist); i++) {
1101+
/* if new entry if already merged just copy the old list */
1102+
if (entry == NULL) {
1103+
tal_arr_expand(&newblacklist, blacklist[i]);
1104+
continue;
1105+
}
1106+
/* old list has not reached the entry yet, so we are just copying it */
1107+
if (blacklist_before(&blacklist[i], entry)) {
1108+
tal_arr_expand(&newblacklist, blacklist[i]);
1109+
continue;
1110+
}
1111+
/* old list has passed the entry, time to put the entry in */
1112+
if (blacklist_before(entry, &blacklist[i])) {
1113+
tal_arr_expand(&newblacklist, *entry);
1114+
tal_arr_expand(&newblacklist, blacklist[i]);
1115+
// mark entry as copied
1116+
entry = NULL;
1117+
continue;
1118+
}
1119+
/* old list overlaps combined into the entry we are adding */
1120+
blacklist_merge(entry, &blacklist[i]);
1121+
}
1122+
if (entry != NULL) {
1123+
tal_arr_expand(&newblacklist, *entry);
1124+
}
1125+
tal_free(blacklist);
1126+
blacklist = newblacklist;
1127+
return list_blacklist(cmd);
1128+
}
1129+
10371130
static struct command_result *json_commando_listrunes(struct command *cmd,
10381131
const char *buffer,
10391132
const jsmntok_t *params)
@@ -1061,6 +1154,7 @@ static void memleak_mark_globals(struct plugin *p, struct htable *memtable)
10611154
memleak_scan_obj(memtable, incoming_commands);
10621155
memleak_scan_obj(memtable, master_rune);
10631156
memleak_scan_htable(memtable, &usage_table->raw);
1157+
memleak_scan_obj(memtable, blacklist);
10641158
if (rune_counter)
10651159
memleak_scan_obj(memtable, rune_counter);
10661160
}
@@ -1135,7 +1229,14 @@ static const struct plugin_command commands[] = { {
11351229
"List runes we have created earlier",
11361230
"Takes an optional {rune} and returns list of {rune}",
11371231
json_commando_listrunes,
1138-
}
1232+
},
1233+
{
1234+
"commando-blacklist",
1235+
"utility",
1236+
"Blacklist a rune or range of runes by unique id",
1237+
"Takes an optional {start} and an optional {end} and returns {blacklist} array containing {start}, {end}",
1238+
json_commando_blacklist,
1239+
},
11391240
};
11401241

11411242
int main(int argc, char *argv[])

0 commit comments

Comments
 (0)