@@ -42,11 +42,16 @@ struct commando {
42
42
const char * json_id ;
43
43
};
44
44
45
+ struct blacklist {
46
+ u64 start , end ;
47
+ };
48
+
45
49
static struct plugin * plugin ;
46
50
static struct commando * * outgoing_commands ;
47
51
static struct commando * * incoming_commands ;
48
52
static u64 * rune_counter ;
49
53
static struct rune * master_rune ;
54
+ static struct blacklist * blacklist ;
50
55
51
56
struct usage {
52
57
/* 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,
1034
1039
return command_finished (cmd , js );
1035
1040
}
1036
1041
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
+
1037
1130
static struct command_result * json_commando_listrunes (struct command * cmd ,
1038
1131
const char * buffer ,
1039
1132
const jsmntok_t * params )
@@ -1061,6 +1154,7 @@ static void memleak_mark_globals(struct plugin *p, struct htable *memtable)
1061
1154
memleak_scan_obj (memtable , incoming_commands );
1062
1155
memleak_scan_obj (memtable , master_rune );
1063
1156
memleak_scan_htable (memtable , & usage_table -> raw );
1157
+ memleak_scan_obj (memtable , blacklist );
1064
1158
if (rune_counter )
1065
1159
memleak_scan_obj (memtable , rune_counter );
1066
1160
}
@@ -1135,7 +1229,14 @@ static const struct plugin_command commands[] = { {
1135
1229
"List runes we have created earlier" ,
1136
1230
"Takes an optional {rune} and returns list of {rune}" ,
1137
1231
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
+ },
1139
1240
};
1140
1241
1141
1242
int main (int argc , char * argv [])
0 commit comments