| 99 | } |
| 100 | |
| 101 | struct blockheights *init_blockheights(const tal_t *ctx, |
| 102 | struct command *init_cmd) |
| 103 | { |
| 104 | struct json_out *params = json_out_new(tmpctx); |
| 105 | const jsmntok_t *result; |
| 106 | const char *buf; |
| 107 | const jsmntok_t *datastore, *t; |
| 108 | size_t i; |
| 109 | |
| 110 | struct blockheights *bh = tal(ctx, struct blockheights); |
| 111 | bh->map = tal(bh, struct blockheight_htable); |
| 112 | blockheight_htable_init(bh->map); |
| 113 | memleak_add_helper(bh->map, memleak_scan_blockheight_htable); |
| 114 | |
| 115 | /* Query all keys under bookkeeper/blockheights */ |
| 116 | json_out_start(params, NULL, '{'); |
| 117 | json_out_start(params, "key", '['); |
| 118 | json_out_addstr(params, NULL, "bookkeeper"); |
| 119 | json_out_addstr(params, NULL, "blockheights"); |
| 120 | json_out_end(params, ']'); |
| 121 | json_out_end(params, '}'); |
| 122 | |
| 123 | result = jsonrpc_request_sync(tmpctx, init_cmd, |
| 124 | "listdatastore", params, &buf); |
| 125 | |
| 126 | datastore = json_get_member(buf, result, "datastore"); |
| 127 | json_for_each_arr(i, t, datastore) { |
| 128 | const jsmntok_t *keytok = json_get_member(buf, t, "key"); |
| 129 | const jsmntok_t *hextok = json_get_member(buf, t, "hex"); |
| 130 | struct blockheight_entry *e; |
| 131 | struct bitcoin_txid txid; |
| 132 | be32 be_blockheight; |
| 133 | |
| 134 | /* Expect: ["bookkeeper","blockheights","<txid>"] */ |
| 135 | if (keytok->size != 3) |
| 136 | goto weird; |
| 137 | |
| 138 | if (!json_to_txid(buf, keytok + 3, &txid)) |
| 139 | goto weird; |
| 140 | if (!json_hex_to_be32(buf, hextok, &be_blockheight)) |
| 141 | goto weird; |
| 142 | |
| 143 | /* Insert into map */ |
| 144 | e = tal(bh->map, struct blockheight_entry); |
| 145 | e->txid = txid; |
| 146 | e->height = be32_to_cpu(be_blockheight); |
| 147 | blockheight_htable_add(bh->map, e); |
| 148 | continue; |
| 149 | |
| 150 | weird: |
| 151 | plugin_log(init_cmd->plugin, LOG_BROKEN, |
| 152 | "Unparsable blockheight datastore entry: %.*s", |
| 153 | json_tok_full_len(t), json_tok_full(buf, t)); |
| 154 | } |
| 155 | |
| 156 | return bh; |
| 157 | } |
no test coverage detected