diff --git a/common/test/run-funding_tx.c b/common/test/run-funding_tx.c index b21b1fb41..76bdac98c 100644 --- a/common/test/run-funding_tx.c +++ b/common/test/run-funding_tx.c @@ -17,6 +17,9 @@ #include "../utxo.c" /* AUTOGENERATED MOCKS START */ +/* Generated stub for fromwire_amount_sat */ +struct amount_sat fromwire_amount_sat(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) +{ fprintf(stderr, "fromwire_amount_sat called!\n"); abort(); } /* Generated stub for fromwire_bitcoin_txid */ void fromwire_bitcoin_txid(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct bitcoin_txid *txid UNNEEDED) @@ -33,6 +36,9 @@ u32 fromwire_u32(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) /* Generated stub for fromwire_u64 */ u64 fromwire_u64(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) { fprintf(stderr, "fromwire_u64 called!\n"); abort(); } +/* Generated stub for towire_amount_sat */ +void towire_amount_sat(u8 **pptr UNNEEDED, const struct amount_sat sat UNNEEDED) +{ fprintf(stderr, "towire_amount_sat called!\n"); abort(); } /* Generated stub for towire_bitcoin_txid */ void towire_bitcoin_txid(u8 **pptr UNNEEDED, const struct bitcoin_txid *txid UNNEEDED) { fprintf(stderr, "towire_bitcoin_txid called!\n"); abort(); } @@ -136,7 +142,7 @@ int main(void) bitcoin_txid(input, &utxo.txid); utxo.outnum = 0; - utxo.amount = 5000000000; + utxo.amount = AMOUNT_SAT(5000000000); utxo.is_p2sh = false; utxo.close_info = NULL; funding_satoshis = 10000000; @@ -145,7 +151,8 @@ int main(void) printf("input[0] txid: %s\n", tal_hexstr(tmpctx, &utxo.txid, sizeof(utxo.txid))); printf("input[0] input: %u\n", utxo.outnum); - printf("input[0] satoshis: %"PRIu64"\n", utxo.amount); + printf("input[0] satoshis: %s\n", + type_to_string(tmpctx, struct amount_sat, &utxo.amount)); printf("funding satoshis: %"PRIu64"\n", funding_satoshis); utxomap = tal_arr(tmpctx, const struct utxo *, 1); @@ -154,7 +161,7 @@ int main(void) funding_satoshis, &local_funding_pubkey, &remote_funding_pubkey, - utxo.amount - fee - funding_satoshis, + utxo.amount.satoshis - fee - funding_satoshis, &inputkey, NULL); printf("# fee: %"PRIu64"\n", fee); printf("change satoshis: %"PRIu64"\n", diff --git a/common/utxo.c b/common/utxo.c index 0132bd727..7cb609a3c 100644 --- a/common/utxo.c +++ b/common/utxo.c @@ -10,7 +10,7 @@ void towire_utxo(u8 **pptr, const struct utxo *utxo) bool is_unilateral_close = utxo->close_info != NULL; towire_bitcoin_txid(pptr, &utxo->txid); towire_u32(pptr, utxo->outnum); - towire_u64(pptr, utxo->amount); + towire_amount_sat(pptr, utxo->amount); towire_u32(pptr, utxo->keyindex); towire_bool(pptr, utxo->is_p2sh); @@ -28,7 +28,7 @@ struct utxo *fromwire_utxo(const tal_t *ctx, const u8 **ptr, size_t *max) fromwire_bitcoin_txid(ptr, max, &utxo->txid); utxo->outnum = fromwire_u32(ptr, max); - utxo->amount = fromwire_u64(ptr, max); + utxo->amount = fromwire_amount_sat(ptr, max); utxo->keyindex = fromwire_u32(ptr, max); utxo->is_p2sh = fromwire_bool(ptr, max); if (fromwire_bool(ptr, max)) { @@ -53,7 +53,7 @@ struct bitcoin_tx *tx_spending_utxos(const tal_t *ctx, for (size_t i = 0; i < tal_count(utxos); i++) { tx->input[i].txid = utxos[i]->txid; tx->input[i].index = utxos[i]->outnum; - tx->input[i].amount = tal_dup(tx, u64, &utxos[i]->amount); + tx->input[i].amount = tal_dup(tx, u64, &utxos[i]->amount.satoshis); if (utxos[i]->is_p2sh && bip32_base) { struct pubkey key; bip32_pubkey(bip32_base, &key, utxos[i]->keyindex); diff --git a/common/utxo.h b/common/utxo.h index 58a82d454..2cb3e5bb1 100644 --- a/common/utxo.h +++ b/common/utxo.h @@ -6,6 +6,7 @@ #include #include #include +#include #include struct ext_key; @@ -20,7 +21,7 @@ struct unilateral_close_info { struct utxo { struct bitcoin_txid txid; u32 outnum; - u64 amount; + struct amount_sat amount; u32 keyindex; bool is_p2sh; u8 status; diff --git a/lightningd/invoice.c b/lightningd/invoice.c index ddac0cd95..9731d6fee 100644 --- a/lightningd/invoice.c +++ b/lightningd/invoice.c @@ -47,12 +47,15 @@ static void json_add_invoice(struct json_stream *response, json_add_string(response, "bolt11", inv->bolt11); json_add_hex(response, "payment_hash", &inv->rhash, sizeof(inv->rhash)); if (inv->msatoshi) - json_add_u64(response, "msatoshi", *inv->msatoshi); + json_add_amount_msat(response, + (struct amount_msat){*inv->msatoshi}, + "msatoshi", "amount_msat"); json_add_string(response, "status", invoice_status_str(inv)); if (inv->state == PAID) { json_add_u64(response, "pay_index", inv->pay_index); - json_add_u64(response, "msatoshi_received", - inv->msatoshi_received); + json_add_amount_msat(response, + (struct amount_msat){inv->msatoshi_received}, + "msatoshi_received", "amount_received_msat"); json_add_u64(response, "paid_at", inv->paid_timestamp); } diff --git a/lightningd/onchain_control.c b/lightningd/onchain_control.c index 4f70a1577..7bd4c6b06 100644 --- a/lightningd/onchain_control.c +++ b/lightningd/onchain_control.c @@ -275,7 +275,7 @@ static void onchain_add_utxo(struct channel *channel, const u8 *msg) if (!fromwire_onchain_add_utxo(msg, &u->txid, &u->outnum, &u->close_info->commitment_point, - &u->amount, &blockheight)) { + &u->amount.satoshis, &blockheight)) { fatal("onchaind gave invalid add_utxo message: %s", tal_hex(msg, msg)); } u->blockheight = blockheight>0?&blockheight:NULL; diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index 0b4340d36..2191ed39a 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -334,8 +334,10 @@ static void opening_funder_finished(struct subd *openingd, const u8 *resp, tal_count(fundingtx->output)); for (size_t i = 0; i < tal_count(fundingtx->input); i++) { - log_debug(fc->uc->log, "%zi: %"PRIu64" satoshi (%s) %s\n", - i, fc->wtx.utxos[i]->amount, + log_debug(fc->uc->log, "%zi: %s (%s) %s\n", + i, + type_to_string(tmpctx, struct amount_sat, + &fc->wtx.utxos[i]->amount), fc->wtx.utxos[i]->is_p2sh ? "P2SH" : "SEGWIT", type_to_string(tmpctx, struct bitcoin_txid, &fundingtx->input[i].txid)); diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 5bd16fbc4..833f40d50 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -1270,8 +1270,9 @@ static struct command_result *json_getinfo(struct command *cmd, json_add_string(response, "version", version()); json_add_num(response, "blockheight", get_block_height(cmd->ld->topology)); json_add_string(response, "network", get_chainparams(cmd->ld)->network_name); - json_add_u64(response, "msatoshi_fees_collected", - wallet_total_forward_fees(cmd->ld->wallet)); + json_add_amount_msat(response, + (struct amount_msat){ wallet_total_forward_fees(cmd->ld->wallet) }, + "msatoshi_fees_collected", "fees_collected_msat"); json_object_end(response); return command_success(cmd, response); } diff --git a/plugins/pay.c b/plugins/pay.c index e1fdb06fd..bee92a7b4 100644 --- a/plugins/pay.c +++ b/plugins/pay.c @@ -634,10 +634,11 @@ static struct command_result *add_shadow_route(struct command *cmd, u32 cltv, best_cltv; json_for_each_arr(i, chan, channels) { - u64 sats, v; + struct amount_sat sats; + u64 v; - json_to_u64(buf, json_get_member(buf, chan, "satoshis"), &sats); - if (sats * 1000 < pc->msatoshi) + json_to_sat(buf, json_get_member(buf, chan, "satoshis"), &sats); + if (sats.satoshis * 1000 < pc->msatoshi) continue; /* Don't use if total would exceed 1/4 of our time allowance. */ diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 27d121176..57eef91f3 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -680,7 +680,7 @@ static bool test_wallet_outputs(struct lightningd *ld, const tal_t *ctx) CHECK(w); memset(&u, 0, sizeof(u)); - u.amount = 1; + u.amount = AMOUNT_SAT(1); pubkey_from_der(tal_hexdata(w, "02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc", 66), 33, &pk); db_begin_transaction(w->db); diff --git a/wallet/wallet.c b/wallet/wallet.c index d1ddd36b6..8fc8ba13f 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -78,7 +78,7 @@ bool wallet_add_utxo(struct wallet *w, struct utxo *utxo, ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"); sqlite3_bind_blob(stmt, 1, &utxo->txid, sizeof(utxo->txid), SQLITE_TRANSIENT); sqlite3_bind_int(stmt, 2, utxo->outnum); - sqlite3_bind_int64(stmt, 3, utxo->amount); + sqlite3_bind_amount_sat(stmt, 3, utxo->amount); sqlite3_bind_int(stmt, 4, wallet_output_type_in_db(type)); sqlite3_bind_int(stmt, 5, output_state_available); sqlite3_bind_int(stmt, 6, utxo->keyindex); @@ -116,7 +116,7 @@ static struct utxo *wallet_stmt2output(const tal_t *ctx, sqlite3_stmt *stmt) u32 *blockheight, *spendheight; sqlite3_column_sha256_double(stmt, 0, &utxo->txid.shad); utxo->outnum = sqlite3_column_int(stmt, 1); - utxo->amount = sqlite3_column_int64(stmt, 2); + utxo->amount = sqlite3_column_amount_sat(stmt, 2); utxo->is_p2sh = sqlite3_column_int(stmt, 3) == p2sh_wpkh; utxo->status = sqlite3_column_int(stmt, 4); utxo->keyindex = sqlite3_column_int(stmt, 5); @@ -305,7 +305,7 @@ static const struct utxo **wallet_select(const tal_t *ctx, struct wallet *w, weight += input_weight; *fee_estimate = weight * feerate_per_kw / 1000; - *satoshi_in += utxos[i]->amount; + *satoshi_in += utxos[i]->amount.satoshis; if (*satoshi_in >= *fee_estimate + value) break; } @@ -1123,7 +1123,7 @@ int wallet_extract_owned_outputs(struct wallet *w, const struct bitcoin_tx *tx, utxo = tal(w, struct utxo); utxo->keyindex = index; utxo->is_p2sh = is_p2sh; - utxo->amount = tx->output[output].amount; + utxo->amount.satoshis = tx->output[output].amount; utxo->status = output_state_available; bitcoin_txid(tx, &utxo->txid); utxo->outnum = output; @@ -1151,7 +1151,7 @@ int wallet_extract_owned_outputs(struct wallet *w, const struct bitcoin_tx *tx, } outpointfilter_add(w->owned_outpoints, &utxo->txid, utxo->outnum); - *total_satoshi += utxo->amount; + *total_satoshi += utxo->amount.satoshis; tal_free(utxo); num_utxos++; } diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index 0dee0038c..be1b91efe 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -422,7 +422,8 @@ static struct command_result *json_listfunds(struct command *cmd, json_object_start(response, NULL); json_add_txid(response, "txid", &utxos[i]->txid); json_add_num(response, "output", utxos[i]->outnum); - json_add_u64(response, "value", utxos[i]->amount); + json_add_amount_sat(response, utxos[i]->amount, + "value", "amount_msat"); /* @close_info is for outputs that are not yet claimable */ if (utxos[i]->close_info == NULL) { @@ -461,11 +462,12 @@ static struct command_result *json_listfunds(struct command *cmd, "short_channel_id", c->scid); - /* Poor man's rounding to satoshis to match the unit for outputs */ - json_add_u64(response, "channel_sat", - (c->our_msatoshi + 500)/1000); - json_add_u64(response, "channel_total_sat", - c->funding_satoshi); + json_add_amount_sat(response, + amount_msat_to_sat_round_down((struct amount_msat){c->our_msatoshi}), + "channel_sat", "our_amount_msat"); + json_add_amount_sat(response, + (struct amount_sat){c->funding_satoshi}, + "channel_total_sat", "amount_msat"); json_add_txid(response, "funding_txid", &c->funding_txid); json_object_end(response);