Note: returns estimates in perkb, caller converts! */
| 189 | |
| 190 | /* Note: returns estimates in perkb, caller converts! */ |
| 191 | static struct feerate_est *parse_feerate_ranges(const tal_t *ctx, |
| 192 | struct bitcoind *bitcoind, |
| 193 | const char *buf, |
| 194 | const jsmntok_t *floortok, |
| 195 | const jsmntok_t *feerates, |
| 196 | u32 *floor) |
| 197 | { |
| 198 | size_t i; |
| 199 | const jsmntok_t *t; |
| 200 | struct feerate_est *rates = tal_arr(ctx, struct feerate_est, 0); |
| 201 | |
| 202 | if (!json_to_u32(buf, floortok, floor)) |
| 203 | bitcoin_plugin_error(bitcoind, buf, floortok, |
| 204 | "estimatefees.feerate_floor", "Not a u32?"); |
| 205 | |
| 206 | json_for_each_arr(i, t, feerates) { |
| 207 | struct feerate_est rate; |
| 208 | const char *err; |
| 209 | |
| 210 | err = json_scan(tmpctx, buf, t, "{blocks:%,feerate:%}", |
| 211 | JSON_SCAN(json_to_u32, &rate.blockcount), |
| 212 | JSON_SCAN(json_to_u32, &rate.rate)); |
| 213 | if (err) |
| 214 | bitcoin_plugin_error(bitcoind, buf, t, |
| 215 | "estimatefees.feerates", err); |
| 216 | |
| 217 | /* Block count must be in order. If rates go up somehow, we |
| 218 | * reduce to prev. */ |
| 219 | if (tal_count(rates) != 0) { |
| 220 | const struct feerate_est *prev = &rates[tal_count(rates)-1]; |
| 221 | if (rate.blockcount <= prev->blockcount) |
| 222 | bitcoin_plugin_error(bitcoind, buf, feerates, |
| 223 | "estimatefees.feerates", |
| 224 | "Blocks must be ascending" |
| 225 | " order: %u <= %u!", |
| 226 | rate.blockcount, |
| 227 | prev->blockcount); |
| 228 | if (rate.rate > prev->rate) { |
| 229 | log_unusual(bitcoind->log, |
| 230 | "Feerate for %u blocks (%u) is > rate" |
| 231 | " for %u blocks (%u)!", |
| 232 | rate.blockcount, rate.rate, |
| 233 | prev->blockcount, prev->rate); |
| 234 | rate.rate = prev->rate; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | tal_arr_expand(&rates, rate); |
| 239 | } |
| 240 | |
| 241 | if (tal_count(rates) == 0) { |
| 242 | if (chainparams->testnet) |
| 243 | log_debug(bitcoind->log, "Unable to estimate any fees"); |
| 244 | else |
| 245 | log_unusual(bitcoind->log, "Unable to estimate any fees"); |
| 246 | } |
| 247 | |
| 248 | return rates; |
no test coverage detected