Skip to content

Commit 4a6e014

Browse files
niftyneirustyrussell
authored andcommitted
bkpr: refactor out the add_events logic for json stuff
1 parent 64b7b98 commit 4a6e014

File tree

1 file changed

+59
-46
lines changed

1 file changed

+59
-46
lines changed

plugins/bkpr/bookkeeper.c

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,64 @@ static struct command_result *json_inspect(struct command *cmd,
354354
return command_finished(cmd, res);
355355
}
356356

357+
static void json_add_events(struct json_stream *res,
358+
struct channel_event **channel_events,
359+
struct chain_event **chain_events,
360+
struct onchain_fee **onchain_fees)
361+
{
362+
for (size_t i = 0, j = 0, k = 0;
363+
i < tal_count(chain_events)
364+
|| j < tal_count(channel_events)
365+
|| k < tal_count(onchain_fees);
366+
/* Incrementing happens inside loop */) {
367+
struct channel_event *chan;
368+
struct chain_event *chain;
369+
struct onchain_fee *fee;
370+
u64 lowest = 0;
371+
372+
if (i < tal_count(chain_events))
373+
chain = chain_events[i];
374+
else
375+
chain = NULL;
376+
if (j < tal_count(channel_events))
377+
chan = channel_events[j];
378+
else
379+
chan = NULL;
380+
if (k < tal_count(onchain_fees))
381+
fee = onchain_fees[k];
382+
else
383+
fee = NULL;
384+
385+
if (chain)
386+
lowest = chain->timestamp;
387+
388+
if (chan
389+
&& (lowest == 0 || lowest > chan->timestamp))
390+
lowest = chan->timestamp;
391+
392+
if (fee
393+
&& (lowest == 0 || lowest > fee->timestamp))
394+
lowest = fee->timestamp;
395+
396+
/* chain events first, then channel events, then fees. */
397+
if (chain && chain->timestamp == lowest) {
398+
json_add_chain_event(res, chain);
399+
i++;
400+
continue;
401+
}
402+
403+
if (chan && chan->timestamp == lowest) {
404+
json_add_channel_event(res, chan);
405+
j++;
406+
continue;
407+
}
408+
409+
/* Last thing left is the fee */
410+
json_add_onchain_fee(res, fee);
411+
k++;
412+
}
413+
}
414+
357415
/* Find all the events for this account, ordered by timestamp */
358416
static struct command_result *json_list_account_events(struct command *cmd,
359417
const char *buf,
@@ -416,57 +474,12 @@ static struct command_result *json_list_account_events(struct command *cmd,
416474

417475
res = jsonrpc_stream_success(cmd);
418476
json_array_start(res, "events");
419-
for (size_t i = 0, j = 0, k = 0;
420-
i < tal_count(chain_events)
421-
|| j < tal_count(channel_events)
422-
|| k < tal_count(onchain_fees);
423-
/* Incrementing happens inside loop */) {
424-
struct channel_event *chan;
425-
struct chain_event *chain;
426-
struct onchain_fee *fee;
427-
u64 lowest = 0;
428-
429-
if (i < tal_count(chain_events))
430-
chain = chain_events[i];
431-
else
432-
chain = NULL;
433-
if (j < tal_count(channel_events))
434-
chan = channel_events[j];
435-
else
436-
chan = NULL;
437-
if (k < tal_count(onchain_fees))
438-
fee = onchain_fees[k];
439-
else
440-
fee = NULL;
477+
json_add_events(res, channel_events, chain_events, onchain_fees);
441478

442-
if (chain)
443-
lowest = chain->timestamp;
444479

445-
if (chan
446-
&& (lowest == 0 || lowest > chan->timestamp))
447-
lowest = chan->timestamp;
448480

449-
if (fee
450-
&& (lowest == 0 || lowest > fee->timestamp))
451-
lowest = fee->timestamp;
452481

453-
/* chain events first, then channel events, then fees. */
454-
if (chain && chain->timestamp == lowest) {
455-
json_add_chain_event(res, chain);
456-
i++;
457-
continue;
458-
}
459-
460-
if (chan && chan->timestamp == lowest) {
461-
json_add_channel_event(res, chan);
462-
j++;
463-
continue;
464-
}
465482

466-
/* Last thing left is the fee */
467-
json_add_onchain_fee(res, fee);
468-
k++;
469-
}
470483
json_array_end(res);
471484
return command_finished(cmd, res);
472485
}

0 commit comments

Comments
 (0)