mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-03-03 18:57:06 +01:00
wallet: use amount_msat / amount_sat.
We change struct utxo to use amount_sat, and paper over the JSON APIs. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
83adb94583
commit
b5dcb93e5f
11 changed files with 47 additions and 30 deletions
|
@ -17,6 +17,9 @@
|
||||||
#include "../utxo.c"
|
#include "../utxo.c"
|
||||||
|
|
||||||
/* AUTOGENERATED MOCKS START */
|
/* 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 */
|
/* Generated stub for fromwire_bitcoin_txid */
|
||||||
void fromwire_bitcoin_txid(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
|
void fromwire_bitcoin_txid(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
|
||||||
struct bitcoin_txid *txid 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 */
|
/* Generated stub for fromwire_u64 */
|
||||||
u64 fromwire_u64(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
|
u64 fromwire_u64(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
|
||||||
{ fprintf(stderr, "fromwire_u64 called!\n"); abort(); }
|
{ 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 */
|
/* Generated stub for towire_bitcoin_txid */
|
||||||
void towire_bitcoin_txid(u8 **pptr UNNEEDED, const struct bitcoin_txid *txid UNNEEDED)
|
void towire_bitcoin_txid(u8 **pptr UNNEEDED, const struct bitcoin_txid *txid UNNEEDED)
|
||||||
{ fprintf(stderr, "towire_bitcoin_txid called!\n"); abort(); }
|
{ fprintf(stderr, "towire_bitcoin_txid called!\n"); abort(); }
|
||||||
|
@ -136,7 +142,7 @@ int main(void)
|
||||||
|
|
||||||
bitcoin_txid(input, &utxo.txid);
|
bitcoin_txid(input, &utxo.txid);
|
||||||
utxo.outnum = 0;
|
utxo.outnum = 0;
|
||||||
utxo.amount = 5000000000;
|
utxo.amount = AMOUNT_SAT(5000000000);
|
||||||
utxo.is_p2sh = false;
|
utxo.is_p2sh = false;
|
||||||
utxo.close_info = NULL;
|
utxo.close_info = NULL;
|
||||||
funding_satoshis = 10000000;
|
funding_satoshis = 10000000;
|
||||||
|
@ -145,7 +151,8 @@ int main(void)
|
||||||
printf("input[0] txid: %s\n",
|
printf("input[0] txid: %s\n",
|
||||||
tal_hexstr(tmpctx, &utxo.txid, sizeof(utxo.txid)));
|
tal_hexstr(tmpctx, &utxo.txid, sizeof(utxo.txid)));
|
||||||
printf("input[0] input: %u\n", utxo.outnum);
|
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);
|
printf("funding satoshis: %"PRIu64"\n", funding_satoshis);
|
||||||
|
|
||||||
utxomap = tal_arr(tmpctx, const struct utxo *, 1);
|
utxomap = tal_arr(tmpctx, const struct utxo *, 1);
|
||||||
|
@ -154,7 +161,7 @@ int main(void)
|
||||||
funding_satoshis,
|
funding_satoshis,
|
||||||
&local_funding_pubkey,
|
&local_funding_pubkey,
|
||||||
&remote_funding_pubkey,
|
&remote_funding_pubkey,
|
||||||
utxo.amount - fee - funding_satoshis,
|
utxo.amount.satoshis - fee - funding_satoshis,
|
||||||
&inputkey, NULL);
|
&inputkey, NULL);
|
||||||
printf("# fee: %"PRIu64"\n", fee);
|
printf("# fee: %"PRIu64"\n", fee);
|
||||||
printf("change satoshis: %"PRIu64"\n",
|
printf("change satoshis: %"PRIu64"\n",
|
||||||
|
|
|
@ -10,7 +10,7 @@ void towire_utxo(u8 **pptr, const struct utxo *utxo)
|
||||||
bool is_unilateral_close = utxo->close_info != NULL;
|
bool is_unilateral_close = utxo->close_info != NULL;
|
||||||
towire_bitcoin_txid(pptr, &utxo->txid);
|
towire_bitcoin_txid(pptr, &utxo->txid);
|
||||||
towire_u32(pptr, utxo->outnum);
|
towire_u32(pptr, utxo->outnum);
|
||||||
towire_u64(pptr, utxo->amount);
|
towire_amount_sat(pptr, utxo->amount);
|
||||||
towire_u32(pptr, utxo->keyindex);
|
towire_u32(pptr, utxo->keyindex);
|
||||||
towire_bool(pptr, utxo->is_p2sh);
|
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);
|
fromwire_bitcoin_txid(ptr, max, &utxo->txid);
|
||||||
utxo->outnum = fromwire_u32(ptr, max);
|
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->keyindex = fromwire_u32(ptr, max);
|
||||||
utxo->is_p2sh = fromwire_bool(ptr, max);
|
utxo->is_p2sh = fromwire_bool(ptr, max);
|
||||||
if (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++) {
|
for (size_t i = 0; i < tal_count(utxos); i++) {
|
||||||
tx->input[i].txid = utxos[i]->txid;
|
tx->input[i].txid = utxos[i]->txid;
|
||||||
tx->input[i].index = utxos[i]->outnum;
|
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) {
|
if (utxos[i]->is_p2sh && bip32_base) {
|
||||||
struct pubkey key;
|
struct pubkey key;
|
||||||
bip32_pubkey(bip32_base, &key, utxos[i]->keyindex);
|
bip32_pubkey(bip32_base, &key, utxos[i]->keyindex);
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
#include <bitcoin/tx.h>
|
#include <bitcoin/tx.h>
|
||||||
#include <ccan/short_types/short_types.h>
|
#include <ccan/short_types/short_types.h>
|
||||||
#include <ccan/tal/tal.h>
|
#include <ccan/tal/tal.h>
|
||||||
|
#include <common/amount.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
struct ext_key;
|
struct ext_key;
|
||||||
|
@ -20,7 +21,7 @@ struct unilateral_close_info {
|
||||||
struct utxo {
|
struct utxo {
|
||||||
struct bitcoin_txid txid;
|
struct bitcoin_txid txid;
|
||||||
u32 outnum;
|
u32 outnum;
|
||||||
u64 amount;
|
struct amount_sat amount;
|
||||||
u32 keyindex;
|
u32 keyindex;
|
||||||
bool is_p2sh;
|
bool is_p2sh;
|
||||||
u8 status;
|
u8 status;
|
||||||
|
|
|
@ -47,12 +47,15 @@ static void json_add_invoice(struct json_stream *response,
|
||||||
json_add_string(response, "bolt11", inv->bolt11);
|
json_add_string(response, "bolt11", inv->bolt11);
|
||||||
json_add_hex(response, "payment_hash", &inv->rhash, sizeof(inv->rhash));
|
json_add_hex(response, "payment_hash", &inv->rhash, sizeof(inv->rhash));
|
||||||
if (inv->msatoshi)
|
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));
|
json_add_string(response, "status", invoice_status_str(inv));
|
||||||
if (inv->state == PAID) {
|
if (inv->state == PAID) {
|
||||||
json_add_u64(response, "pay_index", inv->pay_index);
|
json_add_u64(response, "pay_index", inv->pay_index);
|
||||||
json_add_u64(response, "msatoshi_received",
|
json_add_amount_msat(response,
|
||||||
inv->msatoshi_received);
|
(struct amount_msat){inv->msatoshi_received},
|
||||||
|
"msatoshi_received", "amount_received_msat");
|
||||||
json_add_u64(response, "paid_at", inv->paid_timestamp);
|
json_add_u64(response, "paid_at", inv->paid_timestamp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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,
|
if (!fromwire_onchain_add_utxo(msg, &u->txid, &u->outnum,
|
||||||
&u->close_info->commitment_point,
|
&u->close_info->commitment_point,
|
||||||
&u->amount, &blockheight)) {
|
&u->amount.satoshis, &blockheight)) {
|
||||||
fatal("onchaind gave invalid add_utxo message: %s", tal_hex(msg, msg));
|
fatal("onchaind gave invalid add_utxo message: %s", tal_hex(msg, msg));
|
||||||
}
|
}
|
||||||
u->blockheight = blockheight>0?&blockheight:NULL;
|
u->blockheight = blockheight>0?&blockheight:NULL;
|
||||||
|
|
|
@ -334,8 +334,10 @@ static void opening_funder_finished(struct subd *openingd, const u8 *resp,
|
||||||
tal_count(fundingtx->output));
|
tal_count(fundingtx->output));
|
||||||
|
|
||||||
for (size_t i = 0; i < tal_count(fundingtx->input); i++) {
|
for (size_t i = 0; i < tal_count(fundingtx->input); i++) {
|
||||||
log_debug(fc->uc->log, "%zi: %"PRIu64" satoshi (%s) %s\n",
|
log_debug(fc->uc->log, "%zi: %s (%s) %s\n",
|
||||||
i, fc->wtx.utxos[i]->amount,
|
i,
|
||||||
|
type_to_string(tmpctx, struct amount_sat,
|
||||||
|
&fc->wtx.utxos[i]->amount),
|
||||||
fc->wtx.utxos[i]->is_p2sh ? "P2SH" : "SEGWIT",
|
fc->wtx.utxos[i]->is_p2sh ? "P2SH" : "SEGWIT",
|
||||||
type_to_string(tmpctx, struct bitcoin_txid,
|
type_to_string(tmpctx, struct bitcoin_txid,
|
||||||
&fundingtx->input[i].txid));
|
&fundingtx->input[i].txid));
|
||||||
|
|
|
@ -1270,8 +1270,9 @@ static struct command_result *json_getinfo(struct command *cmd,
|
||||||
json_add_string(response, "version", version());
|
json_add_string(response, "version", version());
|
||||||
json_add_num(response, "blockheight", get_block_height(cmd->ld->topology));
|
json_add_num(response, "blockheight", get_block_height(cmd->ld->topology));
|
||||||
json_add_string(response, "network", get_chainparams(cmd->ld)->network_name);
|
json_add_string(response, "network", get_chainparams(cmd->ld)->network_name);
|
||||||
json_add_u64(response, "msatoshi_fees_collected",
|
json_add_amount_msat(response,
|
||||||
wallet_total_forward_fees(cmd->ld->wallet));
|
(struct amount_msat){ wallet_total_forward_fees(cmd->ld->wallet) },
|
||||||
|
"msatoshi_fees_collected", "fees_collected_msat");
|
||||||
json_object_end(response);
|
json_object_end(response);
|
||||||
return command_success(cmd, response);
|
return command_success(cmd, response);
|
||||||
}
|
}
|
||||||
|
|
|
@ -634,10 +634,11 @@ static struct command_result *add_shadow_route(struct command *cmd,
|
||||||
u32 cltv, best_cltv;
|
u32 cltv, best_cltv;
|
||||||
|
|
||||||
json_for_each_arr(i, chan, channels) {
|
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);
|
json_to_sat(buf, json_get_member(buf, chan, "satoshis"), &sats);
|
||||||
if (sats * 1000 < pc->msatoshi)
|
if (sats.satoshis * 1000 < pc->msatoshi)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
/* Don't use if total would exceed 1/4 of our time allowance. */
|
/* Don't use if total would exceed 1/4 of our time allowance. */
|
||||||
|
|
|
@ -680,7 +680,7 @@ static bool test_wallet_outputs(struct lightningd *ld, const tal_t *ctx)
|
||||||
CHECK(w);
|
CHECK(w);
|
||||||
|
|
||||||
memset(&u, 0, sizeof(u));
|
memset(&u, 0, sizeof(u));
|
||||||
u.amount = 1;
|
u.amount = AMOUNT_SAT(1);
|
||||||
pubkey_from_der(tal_hexdata(w, "02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc", 66), 33, &pk);
|
pubkey_from_der(tal_hexdata(w, "02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc", 66), 33, &pk);
|
||||||
|
|
||||||
db_begin_transaction(w->db);
|
db_begin_transaction(w->db);
|
||||||
|
|
|
@ -78,7 +78,7 @@ bool wallet_add_utxo(struct wallet *w, struct utxo *utxo,
|
||||||
") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
|
") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
|
||||||
sqlite3_bind_blob(stmt, 1, &utxo->txid, sizeof(utxo->txid), SQLITE_TRANSIENT);
|
sqlite3_bind_blob(stmt, 1, &utxo->txid, sizeof(utxo->txid), SQLITE_TRANSIENT);
|
||||||
sqlite3_bind_int(stmt, 2, utxo->outnum);
|
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, 4, wallet_output_type_in_db(type));
|
||||||
sqlite3_bind_int(stmt, 5, output_state_available);
|
sqlite3_bind_int(stmt, 5, output_state_available);
|
||||||
sqlite3_bind_int(stmt, 6, utxo->keyindex);
|
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;
|
u32 *blockheight, *spendheight;
|
||||||
sqlite3_column_sha256_double(stmt, 0, &utxo->txid.shad);
|
sqlite3_column_sha256_double(stmt, 0, &utxo->txid.shad);
|
||||||
utxo->outnum = sqlite3_column_int(stmt, 1);
|
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->is_p2sh = sqlite3_column_int(stmt, 3) == p2sh_wpkh;
|
||||||
utxo->status = sqlite3_column_int(stmt, 4);
|
utxo->status = sqlite3_column_int(stmt, 4);
|
||||||
utxo->keyindex = sqlite3_column_int(stmt, 5);
|
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;
|
weight += input_weight;
|
||||||
|
|
||||||
*fee_estimate = weight * feerate_per_kw / 1000;
|
*fee_estimate = weight * feerate_per_kw / 1000;
|
||||||
*satoshi_in += utxos[i]->amount;
|
*satoshi_in += utxos[i]->amount.satoshis;
|
||||||
if (*satoshi_in >= *fee_estimate + value)
|
if (*satoshi_in >= *fee_estimate + value)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -1123,7 +1123,7 @@ int wallet_extract_owned_outputs(struct wallet *w, const struct bitcoin_tx *tx,
|
||||||
utxo = tal(w, struct utxo);
|
utxo = tal(w, struct utxo);
|
||||||
utxo->keyindex = index;
|
utxo->keyindex = index;
|
||||||
utxo->is_p2sh = is_p2sh;
|
utxo->is_p2sh = is_p2sh;
|
||||||
utxo->amount = tx->output[output].amount;
|
utxo->amount.satoshis = tx->output[output].amount;
|
||||||
utxo->status = output_state_available;
|
utxo->status = output_state_available;
|
||||||
bitcoin_txid(tx, &utxo->txid);
|
bitcoin_txid(tx, &utxo->txid);
|
||||||
utxo->outnum = output;
|
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);
|
outpointfilter_add(w->owned_outpoints, &utxo->txid, utxo->outnum);
|
||||||
|
|
||||||
*total_satoshi += utxo->amount;
|
*total_satoshi += utxo->amount.satoshis;
|
||||||
tal_free(utxo);
|
tal_free(utxo);
|
||||||
num_utxos++;
|
num_utxos++;
|
||||||
}
|
}
|
||||||
|
|
|
@ -422,7 +422,8 @@ static struct command_result *json_listfunds(struct command *cmd,
|
||||||
json_object_start(response, NULL);
|
json_object_start(response, NULL);
|
||||||
json_add_txid(response, "txid", &utxos[i]->txid);
|
json_add_txid(response, "txid", &utxos[i]->txid);
|
||||||
json_add_num(response, "output", utxos[i]->outnum);
|
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 */
|
/* @close_info is for outputs that are not yet claimable */
|
||||||
if (utxos[i]->close_info == NULL) {
|
if (utxos[i]->close_info == NULL) {
|
||||||
|
@ -461,11 +462,12 @@ static struct command_result *json_listfunds(struct command *cmd,
|
||||||
"short_channel_id",
|
"short_channel_id",
|
||||||
c->scid);
|
c->scid);
|
||||||
|
|
||||||
/* Poor man's rounding to satoshis to match the unit for outputs */
|
json_add_amount_sat(response,
|
||||||
json_add_u64(response, "channel_sat",
|
amount_msat_to_sat_round_down((struct amount_msat){c->our_msatoshi}),
|
||||||
(c->our_msatoshi + 500)/1000);
|
"channel_sat", "our_amount_msat");
|
||||||
json_add_u64(response, "channel_total_sat",
|
json_add_amount_sat(response,
|
||||||
c->funding_satoshi);
|
(struct amount_sat){c->funding_satoshi},
|
||||||
|
"channel_total_sat", "amount_msat");
|
||||||
json_add_txid(response, "funding_txid",
|
json_add_txid(response, "funding_txid",
|
||||||
&c->funding_txid);
|
&c->funding_txid);
|
||||||
json_object_end(response);
|
json_object_end(response);
|
||||||
|
|
Loading…
Add table
Reference in a new issue