Skip to content

Commit

Permalink
wallet: use amount_sat/amount_msat.
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Feb 21, 2019
1 parent 3ac0e81 commit bac9a59
Show file tree
Hide file tree
Showing 14 changed files with 154 additions and 119 deletions.
8 changes: 4 additions & 4 deletions common/wallet_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ struct command_result *wtx_select_utxos(struct wallet_tx *tx,
size_t out_len)
{
struct command_result *res;
u64 fee_estimate;
struct amount_sat fee_estimate;

if (tx->all_funds) {
struct amount_sat amount;
tx->utxos = wallet_select_all(tx->cmd, tx->cmd->ld->wallet,
fee_rate_per_kw, out_len,
&amount.satoshis,
&amount,
&fee_estimate);
res = check_amount(tx, amount);
if (res)
Expand All @@ -86,9 +86,9 @@ struct command_result *wtx_select_utxos(struct wallet_tx *tx,
}

tx->utxos = wallet_select_coins(tx->cmd, tx->cmd->ld->wallet,
tx->amount.satoshis,
tx->amount,
fee_rate_per_kw, out_len,
&fee_estimate, &tx->change.satoshis);
&fee_estimate, &tx->change);
res = check_amount(tx, tx->amount);
if (res)
return res;
Expand Down
10 changes: 5 additions & 5 deletions lightningd/chaintopology.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static bool we_broadcast(const struct chain_topology *topo,
static void filter_block_txs(struct chain_topology *topo, struct block *b)
{
size_t i;
u64 satoshi_owned;
struct amount_sat owned;

/* Now we see if any of those txs are interesting. */
for (i = 0; i < tal_count(b->full_txs); i++) {
Expand All @@ -83,17 +83,17 @@ static void filter_block_txs(struct chain_topology *topo, struct block *b)
}
}

satoshi_owned = 0;
owned = AMOUNT_SAT(0);
if (txfilter_match(topo->bitcoind->ld->owned_txfilter, tx)) {
wallet_extract_owned_outputs(topo->bitcoind->ld->wallet,
tx, &b->height,
&satoshi_owned);
&owned);
}

/* We did spends first, in case that tells us to watch tx. */
bitcoin_txid(tx, &txid);
if (watching_txid(topo, &txid) || we_broadcast(topo, &txid) ||
satoshi_owned != 0) {
!amount_sat_eq(owned, AMOUNT_SAT(0))) {
wallet_transaction_add(topo->ld->wallet,
tx, b->height, i);
}
Expand Down Expand Up @@ -593,7 +593,7 @@ static void topo_add_utxos(struct chain_topology *topo, struct block *b)
if (is_p2wsh(output->script, NULL)) {
wallet_utxoset_add(topo->ld->wallet, tx, j,
b->height, i, output->script,
output->amount);
(struct amount_sat){ output->amount });
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions lightningd/invoice.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,13 @@ static void json_add_invoice(struct json_stream *response,
json_add_escaped_string(response, "label", inv->label);
json_add_string(response, "bolt11", inv->bolt11);
json_add_hex(response, "payment_hash", &inv->rhash, sizeof(inv->rhash));
if (inv->msatoshi)
json_add_amount_msat(response,
(struct amount_msat){*inv->msatoshi},
if (inv->msat)
json_add_amount_msat(response, *inv->msat,
"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_amount_msat(response,
(struct amount_msat){inv->msatoshi_received},
json_add_amount_msat(response, inv->received,
"msatoshi_received", "amount_received_msat");
json_add_u64(response, "paid_at", inv->paid_timestamp);
}
Expand Down
4 changes: 2 additions & 2 deletions lightningd/opening_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ static void opening_funder_finished(struct subd *openingd, const u8 *resp,
struct bitcoin_tx *remote_commit;
u16 funding_outnum;
u32 feerate;
u64 change_satoshi;
struct amount_sat change;
struct channel *channel;
struct lightningd *ld = openingd->ld;

Expand Down Expand Up @@ -426,7 +426,7 @@ static void opening_funder_finished(struct subd *openingd, const u8 *resp,
tal_hex(msg, resp));

/* Extract the change output and add it to the DB */
wallet_extract_owned_outputs(ld->wallet, fundingtx, NULL, &change_satoshi);
wallet_extract_owned_outputs(ld->wallet, fundingtx, NULL, &change);

/* Make sure we recognize our change output by its scriptpubkey in
* future. This assumes that we have only two outputs, may not be true
Expand Down
26 changes: 13 additions & 13 deletions lightningd/pay.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,9 @@ json_add_payment_fields(struct json_stream *response,
json_add_u64(response, "id", t->id);
json_add_hex(response, "payment_hash", &t->payment_hash, sizeof(t->payment_hash));
json_add_pubkey(response, "destination", &t->destination);
json_add_amount_msat(response,
((struct amount_msat){ t->msatoshi }),
json_add_amount_msat(response, t->msatoshi,
"msatoshi", "amount_msat");
json_add_amount_msat(response,
((struct amount_msat){ t->msatoshi_sent }),
json_add_amount_msat(response, t->msatoshi_sent,
"msatoshi_sent", "amount_sent_msat");
json_add_u64(response, "created_at", t->timestamp);

Expand Down Expand Up @@ -581,7 +579,7 @@ send_payment(struct lightningd *ld,
struct command *cmd,
const struct sha256 *rhash,
const struct route_hop *route,
u64 msatoshi,
struct amount_msat msat,
const char *description TAKES)
{
const u8 *onion;
Expand Down Expand Up @@ -633,11 +631,13 @@ send_payment(struct lightningd *ld,
if (payment->status == PAYMENT_COMPLETE) {
log_add(ld->log, "... succeeded");
/* Must match successful payment parameters. */
if (payment->msatoshi != msatoshi) {
if (!amount_msat_eq(payment->msatoshi, msat)) {
return command_fail(cmd, PAY_RHASH_ALREADY_USED,
"Already succeeded "
"with amount %"PRIu64,
payment->msatoshi);
"with amount %s",
type_to_string(tmpctx,
struct amount_msat,
&payment->msatoshi));
}
if (!pubkey_eq(&payment->destination, &ids[n_hops-1])) {
return command_fail(cmd, PAY_RHASH_ALREADY_USED,
Expand Down Expand Up @@ -671,9 +671,9 @@ send_payment(struct lightningd *ld,
sizeof(struct sha256), &path_secrets);
onion = serialize_onionpacket(tmpctx, packet);

log_info(ld->log, "Sending %s over %zu hops to deliver %"PRIu64"",
log_info(ld->log, "Sending %s over %zu hops to deliver %s",
type_to_string(tmpctx, struct amount_msat, &route[0].amount),
n_hops, msatoshi);
n_hops, type_to_string(tmpctx, struct amount_msat, &msat));

failcode = send_htlc_out(channel, route[0].amount,
base_expiry + route[0].delay,
Expand Down Expand Up @@ -709,8 +709,8 @@ send_payment(struct lightningd *ld,
payment->payment_hash = *rhash;
payment->destination = ids[n_hops - 1];
payment->status = PAYMENT_PENDING;
payment->msatoshi = msatoshi;
payment->msatoshi_sent = route[0].amount.millisatoshis;
payment->msatoshi = msat;
payment->msatoshi_sent = route[0].amount;
payment->timestamp = time_now().ts.tv_sec;
payment->payment_preimage = NULL;
payment->path_secrets = tal_steal(payment, path_secrets);
Expand Down Expand Up @@ -836,7 +836,7 @@ static struct command_result *json_sendpay(struct command *cmd,
}

res = send_payment(cmd->ld, cmd, rhash, route,
msat ? msat->millisatoshis : route[routetok->size-1].amount.millisatoshis,
msat ? *msat : route[routetok->size-1].amount,
description);
if (res)
return res;
Expand Down
2 changes: 1 addition & 1 deletion lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -1324,7 +1324,7 @@ static struct command_result *json_getinfo(struct command *cmd,
json_add_num(response, "blockheight", get_block_height(cmd->ld->topology));
json_add_string(response, "network", get_chainparams(cmd->ld)->network_name);
json_add_amount_msat(response,
(struct amount_msat){ wallet_total_forward_fees(cmd->ld->wallet) },
wallet_total_forward_fees(cmd->ld->wallet),
"msatoshi_fees_collected", "fees_collected_msat");
json_object_end(response);
return command_success(cmd, response);
Expand Down
41 changes: 24 additions & 17 deletions lightningd/peer_htlcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,19 +302,26 @@ static void handle_localpay(struct htlc_in *hin,
* - if the amount paid is less than the amount expected:
* - MUST fail the HTLC.
*/
if (details->msatoshi != NULL && hin->msat.millisatoshis < *details->msatoshi) {
failcode = WIRE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS;
goto fail;
} else if (details->msatoshi != NULL && hin->msat.millisatoshis > *details->msatoshi * 2) {
/* FIXME: bolt update fixes this quote! */
/* BOLT #4:
*
* - if the amount paid is more than twice the amount expected:
* - SHOULD fail the HTLC.
* - SHOULD return an `incorrect_payment_amount` error.
*/
failcode = WIRE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS;
goto fail;
if (details->msat != NULL) {
struct amount_msat twice;

if (amount_msat_less(hin->msat, *details->msat)) {
failcode = WIRE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS;
goto fail;
}

if (amount_msat_add(&twice, *details->msat, *details->msat)
&& amount_msat_greater(hin->msat, twice)) {
/* FIXME: bolt update fixes this quote! */
/* BOLT #4:
*
* - if the amount paid is more than twice the amount expected:
* - SHOULD fail the HTLC.
* - SHOULD return an `incorrect_payment_amount` error.
*/
failcode = WIRE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS;
goto fail;
}
}

/* BOLT #4:
Expand All @@ -341,7 +348,7 @@ static void handle_localpay(struct htlc_in *hin,
type_to_string(tmpctx, struct amount_msat, &hin->msat),
cltv_expiry);
fulfill_htlc(hin, &details->r);
wallet_invoice_resolve(ld->wallet, invoice, hin->msat.millisatoshis);
wallet_invoice_resolve(ld->wallet, invoice, hin->msat);

return;

Expand Down Expand Up @@ -1886,13 +1893,13 @@ static void listforwardings_add_forwardings(struct json_stream *response, struct
json_add_short_channel_id(response, "in_channel", &cur->channel_in);
json_add_short_channel_id(response, "out_channel", &cur->channel_out);
json_add_amount_msat(response,
(struct amount_msat) { cur->msatoshi_in },
cur->msat_in,
"in_msatoshi", "in_msat");
json_add_amount_msat(response,
(struct amount_msat) { cur->msatoshi_out },
cur->msat_out,
"out_msatoshi", "out_msat");
json_add_amount_msat(response,
(struct amount_msat) { cur->fee },
cur->fee,
"fee", "fee_msat");
json_add_string(response, "status", forward_status_name(cur->status));
json_object_end(response);
Expand Down
2 changes: 1 addition & 1 deletion lightningd/test/run-invoice-select-inchan.c
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ void wallet_invoice_waitone(const tal_t *ctx UNNEEDED,
void wallet_peer_delete(struct wallet *w UNNEEDED, u64 peer_dbid UNNEEDED)
{ fprintf(stderr, "wallet_peer_delete called!\n"); abort(); }
/* Generated stub for wallet_total_forward_fees */
u64 wallet_total_forward_fees(struct wallet *w UNNEEDED)
struct amount_msat wallet_total_forward_fees(struct wallet *w UNNEEDED)
{ fprintf(stderr, "wallet_total_forward_fees called!\n"); abort(); }
/* Generated stub for wallet_transaction_locate */
struct txlocator *wallet_transaction_locate(const tal_t *ctx UNNEEDED, struct wallet *w UNNEEDED,
Expand Down
14 changes: 7 additions & 7 deletions wallet/invoices.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,17 @@ static struct invoice_details *wallet_stmt2invoice_details(const tal_t *ctx,
dtl->label = sqlite3_column_json_escaped(dtl, stmt, 3);

if (sqlite3_column_type(stmt, 4) != SQLITE_NULL) {
dtl->msatoshi = tal(dtl, u64);
*dtl->msatoshi = sqlite3_column_int64(stmt, 4);
dtl->msat = tal(dtl, struct amount_msat);
*dtl->msat = sqlite3_column_amount_msat(stmt, 4);
} else {
dtl->msatoshi = NULL;
dtl->msat = NULL;
}

dtl->expiry_time = sqlite3_column_int64(stmt, 5);

if (dtl->state == PAID) {
dtl->pay_index = sqlite3_column_int64(stmt, 6);
dtl->msatoshi_received = sqlite3_column_int64(stmt, 7);
dtl->received = sqlite3_column_amount_msat(stmt, 7);
dtl->paid_timestamp = sqlite3_column_int64(stmt, 8);
}

Expand Down Expand Up @@ -303,7 +303,7 @@ bool invoices_create(struct invoices *invoices,
sqlite3_bind_blob(stmt, 2, r, sizeof(struct preimage), SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, 3, UNPAID);
if (msat)
sqlite3_bind_int64(stmt, 4, msat->millisatoshis);
sqlite3_bind_amount_msat(stmt, 4, *msat);
else
sqlite3_bind_null(stmt, 4);
sqlite3_bind_json_escaped(stmt, 5, label);
Expand Down Expand Up @@ -508,7 +508,7 @@ static s64 get_next_pay_index(struct db *db)

void invoices_resolve(struct invoices *invoices,
struct invoice invoice,
u64 msatoshi_received)
struct amount_msat received)
{
sqlite3_stmt *stmt;
s64 pay_index;
Expand All @@ -528,7 +528,7 @@ void invoices_resolve(struct invoices *invoices,
" WHERE id=?;");
sqlite3_bind_int(stmt, 1, PAID);
sqlite3_bind_int64(stmt, 2, pay_index);
sqlite3_bind_int64(stmt, 3, msatoshi_received);
sqlite3_bind_amount_msat(stmt, 3, received);
sqlite3_bind_int64(stmt, 4, paid_timestamp);
sqlite3_bind_int64(stmt, 5, invoice.id);
db_exec_prepared(invoices->db, stmt);
Expand Down
4 changes: 2 additions & 2 deletions wallet/invoices.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,14 @@ const struct invoice_details *invoices_iterator_deref(
*
* @invoices - the invoice handler.
* @invoice - the invoice to mark as paid.
* @msatoshi_received - the actual amount received.
* @received - the actual amount received.
*
* Precondition: the invoice must not yet be expired (invoices
* does not check).
*/
void invoices_resolve(struct invoices *invoices,
struct invoice invoice,
u64 msatoshi_received);
struct amount_msat received);

/**
* invoices_waitany - Wait for any invoice to be paid.
Expand Down
18 changes: 9 additions & 9 deletions wallet/test/run-wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ struct invoices *invoices_new(const tal_t *ctx UNNEEDED,
/* Generated stub for invoices_resolve */
void invoices_resolve(struct invoices *invoices UNNEEDED,
struct invoice invoice UNNEEDED,
u64 msatoshi_received UNNEEDED)
struct amount_msat received UNNEEDED)
{ fprintf(stderr, "invoices_resolve called!\n"); abort(); }
/* Generated stub for invoices_waitany */
void invoices_waitany(const tal_t *ctx UNNEEDED,
Expand Down Expand Up @@ -683,7 +683,7 @@ static bool test_wallet_outputs(struct lightningd *ld, const tal_t *ctx)
struct wallet *w = create_test_wallet(ld, ctx);
struct utxo u;
struct pubkey pk;
u64 fee_estimate, change_satoshis;
struct amount_sat fee_estimate, change_satoshis;
const struct utxo **utxos;
CHECK(w);

Expand Down Expand Up @@ -711,7 +711,7 @@ static bool test_wallet_outputs(struct lightningd *ld, const tal_t *ctx)
"wallet_add_utxo with close_info");

/* Now select them */
utxos = wallet_select_coins(w, w, 2, 0, 21, &fee_estimate, &change_satoshis);
utxos = wallet_select_coins(w, w, AMOUNT_SAT(2), 0, 21, &fee_estimate, &change_satoshis);
CHECK(utxos && tal_count(utxos) == 2);

u = *utxos[1];
Expand Down Expand Up @@ -1106,8 +1106,8 @@ static bool test_payment_crud(struct lightningd *ld, const tal_t *ctx)
memset(&t->destination, 1, sizeof(t->destination));

t->id = 0;
t->msatoshi = 100;
t->msatoshi_sent = 101;
t->msatoshi = AMOUNT_MSAT(100);
t->msatoshi_sent = AMOUNT_MSAT(101);
t->status = PAYMENT_PENDING;
t->payment_preimage = NULL;
memset(&t->payment_hash, 1, sizeof(t->payment_hash));
Expand All @@ -1119,8 +1119,8 @@ static bool test_payment_crud(struct lightningd *ld, const tal_t *ctx)
CHECK(t2 != NULL);
CHECK(t2->status == t->status);
CHECK(pubkey_cmp(&t2->destination, &t->destination) == 0);
CHECK(t2->msatoshi == t->msatoshi);
CHECK(t2->msatoshi_sent == t->msatoshi_sent);
CHECK(amount_msat_eq(t2->msatoshi, t->msatoshi));
CHECK(amount_msat_eq(t2->msatoshi_sent, t->msatoshi_sent));
CHECK(!t2->payment_preimage);

t->status = PAYMENT_COMPLETE;
Expand All @@ -1132,8 +1132,8 @@ static bool test_payment_crud(struct lightningd *ld, const tal_t *ctx)
CHECK(t2 != NULL);
CHECK(t2->status == t->status);
CHECK(pubkey_cmp(&t2->destination, &t->destination) == 0);
CHECK(t2->msatoshi == t->msatoshi);
CHECK(t2->msatoshi_sent == t->msatoshi_sent);
CHECK(amount_msat_eq(t2->msatoshi, t->msatoshi));
CHECK(amount_msat_eq(t2->msatoshi_sent, t->msatoshi_sent));
CHECK(preimage_eq(t->payment_preimage, t2->payment_preimage));

db_commit_transaction(w->db);
Expand Down
Loading

0 comments on commit bac9a59

Please sign in to comment.