Skip to content

Commit

Permalink
json: fix up msat amounts in non-_msat fields.
Browse files Browse the repository at this point in the history
We had json_add_amount_msat_only(), which was designed to be used to
print out msat fields, if we had sats.

However, we misused it, so split it into the three different cases:
1. json_add_amount_sat_msat: We are using it correctly, with a field called
   xxx_msat.
2. json_add_amount_sats_deprecated: We were using it wrong, so deprecate
   the old field and create a new one which does end in _msat.
3. json_add_sats: we were using it to hand sats as a JSON parameter to an
   interface, where "XXXsat".

Signed-off-by: Rusty Russell <[email protected]>
Changelog-Deprecated: Plugins: `rbf_channel` and `openchannel2` hooks `their_funding` (use `their_funding_msat`)
Changelog-Deprecated: Plugins: `openchannel2` hook `dust_limit_satoshis` (use `dust_limit_msat`)
Changelog-Deprecated: Plugins: `openchannel` hook `funding_satoshis` (use `funding_msat`)
Changelog-Deprecated: Plugins: `openchannel` hook `dust_limit_satoshis` (use `dust_limit_msat`)
Changelog-Deprecated: Plugins: `openchannel` hook `channel_reserve_satoshis` (use `channel_reserve_msat`)
Changelog-Deprecated: Plugins: `channel_opened` notification `amount` (use `funding_msat`)
Changelog-Deprecated: JSON-RPC: `listtransactions` `msat` (use `amount_msat`)
Changelog-Deprecated: Plugins: `htlc_accepted` `forward_amount` (use `forward_msat`)
  • Loading branch information
rustyrussell committed Jun 20, 2022
1 parent 6e2a775 commit 36a2491
Show file tree
Hide file tree
Showing 42 changed files with 164 additions and 82 deletions.
1 change: 1 addition & 0 deletions .msggen.json
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,7 @@
"ListTransactions.transactions[].inputs[].type": 4
},
"ListtransactionsTransactionsOutputs": {
"ListTransactions.transactions[].outputs[].amount_msat": 6,
"ListTransactions.transactions[].outputs[].channel": 5,
"ListTransactions.transactions[].outputs[].index": 1,
"ListTransactions.transactions[].outputs[].msat": 2,
Expand Down
2 changes: 1 addition & 1 deletion cln-grpc/proto/node.proto
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ message ListtransactionsTransactionsOutputs {
CHANNEL_UNILATERAL_CHEAT = 10;
}
uint32 index = 1;
Amount msat = 2;
Amount amount_msat = 6;
bytes scriptPubKey = 3;
optional ListtransactionsTransactionsOutputsType item_type = 4;
optional string channel = 5;
Expand Down
2 changes: 1 addition & 1 deletion cln-grpc/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ impl From<&responses::ListtransactionsTransactionsOutputs> for pb::Listtransacti
fn from(c: &responses::ListtransactionsTransactionsOutputs) -> Self {
Self {
index: c.index.clone(), // Rule #2 for type u32
msat: Some(c.msat.into()), // Rule #2 for type msat
amount_msat: Some(c.amount_msat.into()), // Rule #2 for type msat
script_pub_key: hex::decode(&c.script_pub_key).unwrap(), // Rule #2 for type hex
item_type: c.item_type.map(|v| v as i32),
channel: c.channel.as_ref().map(|v| v.to_string()), // Rule #2 for type short_channel_id?
Expand Down
4 changes: 2 additions & 2 deletions cln-rpc/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1999,8 +1999,8 @@ pub mod responses {
pub struct ListtransactionsTransactionsOutputs {
#[serde(alias = "index")]
pub index: u32,
#[serde(alias = "msat")]
pub msat: Amount,
#[serde(alias = "amount_msat")]
pub amount_msat: Amount,
#[serde(alias = "scriptPubKey")]
pub script_pub_key: String,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down
2 changes: 1 addition & 1 deletion common/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ const char *json_scanv(const tal_t *ctx,
/* '"fieldname" : "value"' or '"value"' if fieldname is NULL. Turns
* any non-printable chars into JSON escapes, but leaves existing escapes alone.
*/
void json_add_string(struct json_stream *result, const char *fieldname, const char *value);
void json_add_string(struct json_stream *result, const char *fieldname, const char *value TAKES);

/* '"fieldname" : "value[:value_len]"' or '"value[:value_len]"' if
* fieldname is NULL. Turns any non-printable chars into JSON
Expand Down
36 changes: 31 additions & 5 deletions common/json_helpers.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "config.h"
#include <arpa/inet.h>
#include <assert.h>
#include <bitcoin/psbt.h>
#include <ccan/ccan/str/hex/hex.h>
#include <common/configdir.h>
#include <common/json_helpers.h>
#include <common/json_stream.h>
#include <common/type_to_string.h>
Expand Down Expand Up @@ -417,19 +419,43 @@ void json_add_amount_sat_compat(struct json_stream *result,
const char *msatfieldname)
{
json_add_u64(result, rawfieldname, sat.satoshis); /* Raw: low-level helper */
json_add_amount_sat_only(result, msatfieldname, sat);
json_add_amount_sat_msat(result, msatfieldname, sat);
}

void json_add_amount_sat_only(struct json_stream *result,
const char *msatfieldname,
struct amount_sat sat)
void json_add_amount_sat_msat(struct json_stream *result,
const char *msatfieldname,
struct amount_sat sat)
{
struct amount_msat msat;
assert(strends(msatfieldname, "_msat"));
if (amount_sat_to_msat(&msat, sat))
json_add_string(result, msatfieldname,
type_to_string(tmpctx, struct amount_msat, &msat));
}

/* When I noticed that we were adding "XXXmsat" fields *not* ending in _msat */
void json_add_amount_sats_deprecated(struct json_stream *result,
const char *fieldname,
const char *msatfieldname,
struct amount_sat sat)
{
if (deprecated_apis) {
struct amount_msat msat;
assert(!strends(fieldname, "_msat"));
if (amount_sat_to_msat(&msat, sat))
json_add_string(result, fieldname,
take(fmt_amount_msat(NULL, msat)));
}
json_add_amount_sat_msat(result, msatfieldname, sat);
}

void json_add_sats(struct json_stream *result,
const char *fieldname,
struct amount_sat sat)
{
json_add_string(result, fieldname, take(fmt_amount_sat(NULL, sat)));
}

void json_add_secret(struct json_stream *response, const char *fieldname,
const struct secret *secret)
{
Expand All @@ -451,7 +477,7 @@ void json_add_preimage(struct json_stream *result, const char *fieldname,
void json_add_lease_rates(struct json_stream *result,
const struct lease_rates *rates)
{
json_add_amount_sat_only(result, "lease_fee_base_msat",
json_add_amount_sat_msat(result, "lease_fee_base_msat",
amount_sat(rates->lease_fee_base_sat));
json_add_num(result, "lease_fee_basis", rates->lease_fee_basis);
json_add_num(result, "funding_weight", rates->funding_weight);
Expand Down
19 changes: 16 additions & 3 deletions common/json_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,24 @@ void json_add_amount_msat_only(struct json_stream *result,
NO_NULL_ARGS;

/* Adds an 'msat' field */
void json_add_amount_sat_only(struct json_stream *result,
const char *msatfieldname,
struct amount_sat sat)
void json_add_amount_sat_msat(struct json_stream *result,
const char *msatfieldname,
struct amount_sat sat)
NO_NULL_ARGS;

/* Adds an 'msat' field, and an older deprecated field. */
void json_add_amount_sats_deprecated(struct json_stream *result,
const char *fieldname,
const char *msatfieldname,
struct amount_sat sat)
NO_NULL_ARGS;

/* This is used to create requests, *never* for output (output is always
* msat!) */
void json_add_sats(struct json_stream *result,
const char *fieldname,
struct amount_sat sat);

void json_add_sha256(struct json_stream *result, const char *fieldname,
const struct sha256 *hash);

Expand Down
2 changes: 2 additions & 0 deletions common/test/run-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <stdio.h>

/* AUTOGENERATED MOCKS START */
/* Generated stub for deprecated_apis */
bool deprecated_apis;
/* Generated stub for fromwire_tlv */
bool fromwire_tlv(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
const struct tlv_record_type *types UNNEEDED, size_t num_types UNNEEDED,
Expand Down
8 changes: 8 additions & 0 deletions common/test/run-route_blinding_override_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ struct amount_asset amount_sat_to_asset(struct amount_sat *sat UNNEEDED, const u
/* Generated stub for amount_tx_fee */
struct amount_sat amount_tx_fee(u32 fee_per_kw UNNEEDED, size_t weight UNNEEDED)
{ fprintf(stderr, "amount_tx_fee called!\n"); abort(); }
/* Generated stub for deprecated_apis */
bool deprecated_apis;
/* Generated stub for fmt_amount_msat */
const char *fmt_amount_msat(const tal_t *ctx UNNEEDED, struct amount_msat msat UNNEEDED)
{ fprintf(stderr, "fmt_amount_msat called!\n"); abort(); }
/* Generated stub for fmt_amount_sat */
const char *fmt_amount_sat(const tal_t *ctx UNNEEDED, struct amount_sat sat UNNEEDED)
{ fprintf(stderr, "fmt_amount_sat called!\n"); abort(); }
/* Generated stub for fromwire_amount_msat */
struct amount_msat fromwire_amount_msat(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_amount_msat called!\n"); abort(); }
Expand Down
8 changes: 8 additions & 0 deletions common/test/run-route_blinding_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ struct amount_asset amount_sat_to_asset(struct amount_sat *sat UNNEEDED, const u
/* Generated stub for amount_tx_fee */
struct amount_sat amount_tx_fee(u32 fee_per_kw UNNEEDED, size_t weight UNNEEDED)
{ fprintf(stderr, "amount_tx_fee called!\n"); abort(); }
/* Generated stub for deprecated_apis */
bool deprecated_apis;
/* Generated stub for fmt_amount_msat */
const char *fmt_amount_msat(const tal_t *ctx UNNEEDED, struct amount_msat msat UNNEEDED)
{ fprintf(stderr, "fmt_amount_msat called!\n"); abort(); }
/* Generated stub for fmt_amount_sat */
const char *fmt_amount_sat(const tal_t *ctx UNNEEDED, struct amount_sat sat UNNEEDED)
{ fprintf(stderr, "fmt_amount_sat called!\n"); abort(); }
/* Generated stub for fmt_wireaddr_without_port */
char *fmt_wireaddr_without_port(const tal_t *ctx UNNEEDED, const struct wireaddr *a UNNEEDED)
{ fprintf(stderr, "fmt_wireaddr_without_port called!\n"); abort(); }
Expand Down
1 change: 1 addition & 0 deletions devtools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ DEVTOOLS_COMMON_OBJS := \
common/bolt11.o \
common/blockheight_states.o \
common/channel_id.o \
common/configdir.o \
common/decode_array.o \
common/features.o \
common/fee_states.o \
Expand Down
3 changes: 2 additions & 1 deletion devtools/bolt12-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <ccan/tal/str/str.h>
#include <common/bech32_util.h>
#include <common/bolt12_merkle.h>
#include <common/configdir.h>
#include <common/features.h>
#include <common/iso4217.h>
#include <common/setup.h>
Expand All @@ -20,7 +21,6 @@
#define ERROR_USAGE 3

static bool well_formed = true;
bool deprecated_apis = true;

/* Tal wrappers for opt. */
static void *opt_allocfn(size_t size)
Expand Down Expand Up @@ -463,6 +463,7 @@ int main(int argc, char *argv[])
char *fail;

common_setup(argv[0]);
deprecated_apis = true;

opt_set_alloc(opt_allocfn, tal_reallocfn, tal_freefn);
opt_register_noarg("--help|-h", opt_usage_and_exit,
Expand Down
10 changes: 5 additions & 5 deletions doc/PLUGINS.md
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ if the funding transaction has been included into a block.
{
"channel_opened": {
"id": "03864ef025fde8fb587d989186ce6a4a186895ee44a926bfc370e2c366597a3f8f",
"funding_satoshis": "100000000msat",
"funding_msat": "100000000msat",
"funding_txid": "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
"funding_locked": false
}
Expand Down Expand Up @@ -721,7 +721,7 @@ i.e. only definitively resolved HTLCs or confirmed bitcoin transactions.
"part_id": 0, // (`channel_mvt` only, optional)
"credit":"2000000000msat",
"debit":"0msat",
"output_value": "2000000000msat", // ('chain_mvt' only)
"output_msat": "2000000000msat", // ('chain_mvt' only)
"output_count": 2, // ('chain_mvt' only, typically only channel closes)
"fees": "382msat", // ('channel_mvt' only)
"tags": ["deposit"],
Expand Down Expand Up @@ -1185,8 +1185,8 @@ the v2 protocol, and it has passed basic sanity checks:
"openchannel2": {
"id": "03864ef025fde8fb587d989186ce6a4a186895ee44a926bfc370e2c366597a3f8f",
"channel_id": "252d1b0a1e57895e84137f28cf19ab2c35847e284c112fefdecc7afeaa5c1de7",
"their_funding": "100000000msat",
"dust_limit_satoshis": "546000msat",
"their_funding_msat": "100000000msat",
"dust_limit_msat": "546000msat",
"max_htlc_value_in_flight_msat": "18446744073709551615msat",
"htlc_minimum_msat": "0msat",
"funding_feerate_per_kw": 7500,
Expand Down Expand Up @@ -1323,7 +1323,7 @@ requests an RBF for a channel funding transaction.
"rbf_channel": {
"id": "03864ef025fde8fb587d989186ce6a4a186895ee44a926bfc370e2c366597a3f8f",
"channel_id": "252d1b0a1e57895e84137f28cf19ab2c35847e284c112fefdecc7afeaa5c1de7",
"their_funding": "100000000msat",
"their_funding_msat": "100000000msat",
"funding_feerate_per_kw": 7500,
"feerate_our_max": 10000,
"feerate_our_min": 253,
Expand Down
4 changes: 2 additions & 2 deletions doc/lightning-listtransactions.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ On success, an object containing **transactions** is returned. It is an array o
- **channel** (short_channel_id, optional): the channel this input is associated with (*EXPERIMENTAL_FEATURES* only)
- **outputs** (array of objects): Each output, in order:
- **index** (u32): the 0-based output number
- **msat** (msat): the amount of the output
- **amount_msat** (msat): the amount of the output
- **scriptPubKey** (hex): the scriptPubKey
- **type** (string, optional): the purpose of this output (*EXPERIMENTAL_FEATURES* only) (one of "theirs", "deposit", "withdraw", "channel_funding", "channel_mutual_close", "channel_unilateral_close", "channel_sweep", "channel_htlc_success", "channel_htlc_timeout", "channel_penalty", "channel_unilateral_cheat")
- **channel** (short_channel_id, optional): the channel this output is associated with (*EXPERIMENTAL_FEATURES* only)
Expand Down Expand Up @@ -104,4 +104,4 @@ RESOURCES
---------

Main web site: <https://github.com/ElementsProject/lightning>
[comment]: # ( SHA256STAMP:bd9c33dd27be0f25b0212b4115714768ffbec2ff6e72f083613a4464a3f98bc0)
[comment]: # ( SHA256STAMP:74408f25dcf548f1389ab41123748297ba3116ad75afb41b86ecca453b95fec8)
7 changes: 5 additions & 2 deletions doc/schemas/listtransactions.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,21 @@
"additionalProperties": false,
"required": [
"index",
"msat",
"amount_msat",
"scriptPubKey"
],
"properties": {
"index": {
"type": "u32",
"description": "the 0-based output number"
},
"msat": {
"amount_msat": {
"type": "msat",
"description": "the amount of the output"
},
"msat": {
"deprecated": true
},
"scriptPubKey": {
"type": "hex",
"description": "the scriptPubKey"
Expand Down
2 changes: 2 additions & 0 deletions gossipd/test/run-check_channel_announcement.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ bool cupdate_different(struct gossip_store *gs UNNEEDED,
const struct half_chan *hc UNNEEDED,
const u8 *cupdate UNNEEDED)
{ fprintf(stderr, "cupdate_different called!\n"); abort(); }
/* Generated stub for deprecated_apis */
bool deprecated_apis;
/* Generated stub for ecdh */
void ecdh(const struct pubkey *point UNNEEDED, struct secret *ss UNNEEDED)
{ fprintf(stderr, "ecdh called!\n"); abort(); }
Expand Down
2 changes: 2 additions & 0 deletions gossipd/test/run-check_node_announcement.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ bool blinding_next_pubkey(const struct pubkey *pk UNNEEDED,
/* Generated stub for daemon_conn_send */
void daemon_conn_send(struct daemon_conn *dc UNNEEDED, const u8 *msg UNNEEDED)
{ fprintf(stderr, "daemon_conn_send called!\n"); abort(); }
/* Generated stub for deprecated_apis */
bool deprecated_apis;
/* Generated stub for ecdh */
void ecdh(const struct pubkey *point UNNEEDED, struct secret *ss UNNEEDED)
{ fprintf(stderr, "ecdh called!\n"); abort(); }
Expand Down
2 changes: 2 additions & 0 deletions gossipd/test/run-crc32_of_update.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ bigsize_t *decode_scid_query_flags(const tal_t *ctx UNNEEDED,
/* Generated stub for decode_short_ids */
struct short_channel_id *decode_short_ids(const tal_t *ctx UNNEEDED, const u8 *encoded UNNEEDED)
{ fprintf(stderr, "decode_short_ids called!\n"); abort(); }
/* Generated stub for deprecated_apis */
bool deprecated_apis;
/* Generated stub for ecdh */
void ecdh(const struct pubkey *point UNNEEDED, struct secret *ss UNNEEDED)
{ fprintf(stderr, "ecdh called!\n"); abort(); }
Expand Down
2 changes: 2 additions & 0 deletions gossipd/test/run-extended-info.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ bigsize_t *decode_scid_query_flags(const tal_t *ctx UNNEEDED,
/* Generated stub for decode_short_ids */
struct short_channel_id *decode_short_ids(const tal_t *ctx UNNEEDED, const u8 *encoded UNNEEDED)
{ fprintf(stderr, "decode_short_ids called!\n"); abort(); }
/* Generated stub for deprecated_apis */
bool deprecated_apis;
/* Generated stub for ecdh */
void ecdh(const struct pubkey *point UNNEEDED, struct secret *ss UNNEEDED)
{ fprintf(stderr, "ecdh called!\n"); abort(); }
Expand Down
2 changes: 2 additions & 0 deletions gossipd/test/run-next_block_range.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ bool blinding_next_pubkey(const struct pubkey *pk UNNEEDED,
const struct sha256 *h UNNEEDED,
struct pubkey *next UNNEEDED)
{ fprintf(stderr, "blinding_next_pubkey called!\n"); abort(); }
/* Generated stub for deprecated_apis */
bool deprecated_apis;
/* Generated stub for ecdh */
void ecdh(const struct pubkey *point UNNEEDED, struct secret *ss UNNEEDED)
{ fprintf(stderr, "ecdh called!\n"); abort(); }
Expand Down
2 changes: 2 additions & 0 deletions gossipd/test/run-txout_failure.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ bool cupdate_different(struct gossip_store *gs UNNEEDED,
const struct half_chan *hc UNNEEDED,
const u8 *cupdate UNNEEDED)
{ fprintf(stderr, "cupdate_different called!\n"); abort(); }
/* Generated stub for deprecated_apis */
bool deprecated_apis;
/* Generated stub for ecdh */
void ecdh(const struct pubkey *point UNNEEDED, struct secret *ss UNNEEDED)
{ fprintf(stderr, "ecdh called!\n"); abort(); }
Expand Down
Loading

0 comments on commit 36a2491

Please sign in to comment.