11#include "config.h"
2+ #include <bitcoin/chainparams.h>
23#include <ccan/array_size/array_size.h>
34#include <ccan/cast/cast.h>
45#include <ccan/json_escape/json_escape.h>
@@ -556,7 +557,6 @@ static struct command_result *json_list_balances(struct command *cmd,
556557{
557558 struct json_stream * res ;
558559 struct account * * accts ;
559- char * err ;
560560
561561 if (!param (cmd , buf , params , NULL ))
562562 return command_param_failed ();
@@ -568,18 +568,19 @@ static struct command_result *json_list_balances(struct command *cmd,
568568
569569 json_array_start (res , "accounts" );
570570 for (size_t i = 0 ; i < tal_count (accts ); i ++ ) {
571- struct acct_balance * * balances ;
572-
573- err = account_get_balance (cmd , db ,
574- accts [i ]-> name ,
575- true,
576- & balances );
571+ struct amount_msat credit , debit , balance ;
572+ bool has_events ;
577573
578- if (err )
574+ has_events = account_get_credit_debit (cmd -> plugin , db ,
575+ accts [i ]-> name ,
576+ & credit , & debit );
577+ if (!amount_msat_sub (& balance , credit , debit )) {
579578 plugin_err (cmd -> plugin ,
580- "Get account balance returned err"
581- " for account %s: %s" ,
582- accts [i ]-> name , err );
579+ "Account balance underflow for account %s (credit %s, debit %s)" ,
580+ accts [i ]-> name ,
581+ fmt_amount_msat (tmpctx , credit ),
582+ fmt_amount_msat (tmpctx , debit ));
583+ }
583584
584585 /* Skip the external acct balance, it's effectively
585586 * meaningless */
@@ -602,13 +603,15 @@ static struct command_result *json_list_balances(struct command *cmd,
602603 accts [i ]-> onchain_resolved_block );
603604 }
604605
606+ /* FIXME: This API is now overkill! */
605607 json_array_start (res , "balances" );
606- for (size_t j = 0 ; j < tal_count (balances ); j ++ ) {
608+ /* We expect no entry if account is not used. */
609+ for (size_t j = 0 ; j < has_events ; j ++ ) {
607610 json_object_start (res , NULL );
608611 json_add_amount_msat (res , "balance_msat" ,
609- balances [ j ] -> balance );
612+ balance );
610613 json_add_string (res , "coin_type" ,
611- balances [ j ] -> currency );
614+ chainparams -> lightning_hrp );
612615 json_object_end (res );
613616 }
614617 json_array_end (res );
@@ -951,8 +954,7 @@ static struct command_result *listpeerchannels_multi_done(struct command *cmd,
951954 /* Let's register all these accounts! */
952955 for (size_t i = 0 ; i < tal_count (new_accts ); i ++ ) {
953956 struct new_account_info * info = new_accts [i ];
954- struct acct_balance * * balances , * bal ;
955- struct amount_msat credit_diff , debit_diff ;
957+ struct amount_msat credit , debit , credit_diff , debit_diff ;
956958 char * err ;
957959
958960 if (!new_missed_channel_account (cmd , buf , result ,
@@ -966,25 +968,14 @@ static struct command_result *listpeerchannels_multi_done(struct command *cmd,
966968 }
967969
968970 db_begin_transaction (db );
969- err = account_get_balance (tmpctx , db , info -> acct -> name ,
970- false, & balances );
971+ account_get_credit_debit (cmd -> plugin , db ,
972+ info -> acct -> name ,
973+ & credit , & debit );
971974 db_commit_transaction (db );
972975
973- if (err )
974- plugin_err (cmd -> plugin , "%s" , err );
975-
976- /* FIXME: multiple currencies */
977- if (tal_count (balances ) > 0 )
978- bal = balances [0 ];
979- else {
980- bal = tal (tmpctx , struct acct_balance );
981- bal -> credit = AMOUNT_MSAT (0 );
982- bal -> debit = AMOUNT_MSAT (0 );
983- }
984-
985976 err = msat_find_diff (info -> curr_bal ,
986- bal -> credit ,
987- bal -> debit ,
977+ credit ,
978+ debit ,
988979 & credit_diff , & debit_diff );
989980 if (err )
990981 plugin_err (cmd -> plugin , "%s" , err );
@@ -1075,9 +1066,8 @@ static struct command_result *json_balance_snapshot(struct command *cmd,
10751066
10761067 db_begin_transaction (db );
10771068 json_for_each_arr (i , acct_tok , accounts_tok ) {
1078- struct acct_balance * * balances , * bal ;
10791069 struct account * acct ;
1080- struct amount_msat snap_balance , credit_diff , debit_diff ;
1070+ struct amount_msat snap_balance , credit , debit , credit_diff , debit_diff ;
10811071 char * acct_name , * currency ;
10821072 bool existed ;
10831073
@@ -1101,34 +1091,13 @@ static struct command_result *json_balance_snapshot(struct command *cmd,
11011091 fmt_amount_msat (tmpctx , snap_balance ));
11021092
11031093 /* Find the account balances */
1104- err = account_get_balance (cmd , db , acct_name ,
1105- /* Don't error if negative */
1106- false,
1107- & balances );
1108-
1109- if (err )
1110- plugin_err (cmd -> plugin ,
1111- "Get account balance returned err"
1112- " for account %s: %s" ,
1113- acct_name , err );
1114-
1115- /* multiple currency balances! */
1116- bal = NULL ;
1117- for (size_t j = 0 ; j < tal_count (balances ); j ++ ) {
1118- if (streq (balances [j ]-> currency , currency ))
1119- bal = balances [j ];
1120- }
1121-
1122- if (!bal ) {
1123- bal = tal (balances , struct acct_balance );
1124- bal -> credit = AMOUNT_MSAT (0 );
1125- bal -> debit = AMOUNT_MSAT (0 );
1126- }
1094+ account_get_credit_debit (cmd -> plugin , db , acct_name ,
1095+ & credit , & debit );
11271096
11281097 /* Figure out what the net diff is btw reported & actual */
11291098 err = msat_find_diff (snap_balance ,
1130- bal -> credit ,
1131- bal -> debit ,
1099+ credit ,
1100+ debit ,
11321101 & credit_diff , & debit_diff );
11331102 if (err )
11341103 plugin_err (cmd -> plugin ,
@@ -1398,37 +1367,23 @@ listpeerchannels_done(struct command *cmd,
13981367 const jsmntok_t * result ,
13991368 struct event_info * info )
14001369{
1401- struct acct_balance * * balances , * bal ;
1402- struct amount_msat credit_diff , debit_diff ;
1370+ struct amount_msat credit , debit , credit_diff , debit_diff ;
14031371 const char * err ;
14041372
14051373 if (new_missed_channel_account (cmd , buf , result ,
14061374 info -> acct ,
14071375 info -> ev -> currency ,
14081376 info -> ev -> timestamp )) {
14091377 db_begin_transaction (db );
1410- err = account_get_balance ( tmpctx , db , info -> acct -> name ,
1411- false , & balances );
1378+ account_get_credit_debit ( cmd -> plugin , db , info -> acct -> name ,
1379+ & credit , & debit );
14121380 db_commit_transaction (db );
14131381
1414- if (err )
1415- plugin_err (cmd -> plugin , "%s" , err );
1416-
1417- /* FIXME: multiple currencies per account? */
1418- if (tal_count (balances ) > 0 )
1419- bal = balances [0 ];
1420- else {
1421- bal = tal (balances , struct acct_balance );
1422- bal -> credit = AMOUNT_MSAT (0 );
1423- bal -> debit = AMOUNT_MSAT (0 );
1424- }
1425- assert (tal_count (balances ) == 1 );
1426-
14271382 /* The expected current balance is zero, since
14281383 * we just got the channel close event */
14291384 err = msat_find_diff (AMOUNT_MSAT (0 ),
1430- bal -> credit ,
1431- bal -> debit ,
1385+ credit ,
1386+ debit ,
14321387 & credit_diff , & debit_diff );
14331388 if (err )
14341389 plugin_err (cmd -> plugin , "%s" , err );
0 commit comments