| 104 | } |
| 105 | |
| 106 | struct rebalances *init_rebalances(const tal_t *ctx, |
| 107 | struct command *init_cmd) |
| 108 | { |
| 109 | struct json_out *params = json_out_new(tmpctx); |
| 110 | const jsmntok_t *result; |
| 111 | const char *buf; |
| 112 | const jsmntok_t *datastore, *t; |
| 113 | size_t i; |
| 114 | |
| 115 | struct rebalances *r = tal(ctx, struct rebalances); |
| 116 | r->pairs = tal(r, struct rebalance_htable); |
| 117 | rebalance_htable_init(r->pairs); |
| 118 | memleak_add_helper(r->pairs, memleak_scan_rebalance_htable); |
| 119 | |
| 120 | /* Query all keys under bookkeeper/rebalances */ |
| 121 | json_out_start(params, NULL, '{'); |
| 122 | json_out_start(params, "key", '['); |
| 123 | json_out_addstr(params, NULL, "bookkeeper"); |
| 124 | json_out_addstr(params, NULL, "rebalances"); |
| 125 | json_out_end(params, ']'); |
| 126 | json_out_end(params, '}'); |
| 127 | |
| 128 | result = jsonrpc_request_sync(tmpctx, init_cmd, |
| 129 | "listdatastore", params, &buf); |
| 130 | |
| 131 | datastore = json_get_member(buf, result, "datastore"); |
| 132 | json_for_each_arr(i, t, datastore) { |
| 133 | const jsmntok_t *keytok = json_get_member(buf, t, "key"); |
| 134 | jsmntok_t lessertok, greatertok; |
| 135 | u64 lesser, greater; |
| 136 | |
| 137 | if (keytok->size != 3) |
| 138 | goto weird; |
| 139 | |
| 140 | /* key = ["bookkeeper", "rebalances", "<lesser>-<greater>"] */ |
| 141 | if (!split_tok(buf, keytok + 3, '-', &lessertok, &greatertok)) |
| 142 | goto weird; |
| 143 | |
| 144 | if (!json_to_u64(buf, &lessertok, &lesser) |
| 145 | || !json_to_u64(buf, &greatertok, &greater)) |
| 146 | goto weird; |
| 147 | |
| 148 | new_rebalance_pair(r, lesser, greater); |
| 149 | continue; |
| 150 | |
| 151 | weird: |
| 152 | plugin_log(init_cmd->plugin, LOG_BROKEN, "Unparsable datastore %.*s", |
| 153 | json_tok_full_len(keytok), |
| 154 | json_tok_full(buf, keytok)); |
| 155 | } |
| 156 | |
| 157 | return r; |
| 158 | } |
no test coverage detected