Skip to content

Commit

Permalink
bitcoin: 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 bac9a59 commit 948ca47
Show file tree
Hide file tree
Showing 29 changed files with 157 additions and 122 deletions.
6 changes: 6 additions & 0 deletions bitcoin/pullpush.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ void push_le64(u64 v,
push(&l, sizeof(l), pushp);
}

void push_amount_sat(struct amount_sat v,
void (*push)(const void *, size_t, void *), void *pushp)
{
push_le64(v.satoshis, push, pushp);
}

void push_varint_blob(const tal_t *blob,
void (*push)(const void *, size_t, void *),
void *pushp)
Expand Down
3 changes: 3 additions & 0 deletions bitcoin/pullpush.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
#include "config.h"
#include "bitcoin/varint.h"
#include <ccan/tal/tal.h>
#include <common/amount.h>

void push_varint(varint_t v,
void (*push)(const void *, size_t, void *), void *pushp);
void push_le32(u32 v, void (*push)(const void *, size_t, void *), void *pushp);
void push_le64(u64 v, void (*push)(const void *, size_t, void *), void *pushp);
void push_amount_sat(struct amount_sat v,
void (*push)(const void *, size_t, void *), void *pushp);
void push_varint_blob(const tal_t *blob,
void (*push)(const void *, size_t, void *),
void *pushp);
Expand Down
16 changes: 12 additions & 4 deletions bitcoin/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static void push_tx_input(const struct bitcoin_tx_input *input,
static void push_tx_output(const struct bitcoin_tx_output *output,
void (*push)(const void *, size_t, void *), void *pushp)
{
push_le64(output->amount, push, pushp);
push_amount_sat(output->amount, push, pushp);
push_varint_blob(output->script, push, pushp);
}

Expand Down Expand Up @@ -204,7 +204,7 @@ static void hash_outputs(struct sha256_double *h, const struct bitcoin_tx *tx,
if (sighash_single(sighash_type) && i != input_num)
continue;

push_le64(tx->output[i].amount, push_sha, &ctx);
push_amount_sat(tx->output[i].amount, push_sha, &ctx);
push_varint_blob(tx->output[i].script, push_sha, &ctx);
}

Expand Down Expand Up @@ -243,7 +243,7 @@ static void hash_for_segwit(struct sha256_ctx *ctx,
push_varint_blob(witness_script, push_sha, ctx);

/* 6. value of the output spent by this input (8-byte little end) */
push_le64(*tx->input[input_num].amount, push_sha, ctx);
push_amount_sat(*tx->input[input_num].amount, push_sha, ctx);

/* 7. nSequence of the input (4-byte little endian) */
push_le32(tx->input[input_num].sequence_number, push_sha, ctx);
Expand Down Expand Up @@ -362,6 +362,14 @@ static u64 pull_value(const u8 **cursor, size_t *max)
return amount;
}

static struct amount_sat pull_amount_sat(const u8 **cursor, size_t *max)
{
struct amount_sat sat;

sat.satoshis = pull_value(cursor, max);
return sat;
}

/* Pulls a varint which specifies n items of mult size: ensures basic
* sanity to avoid trivial OOM */
static u64 pull_length(const u8 **cursor, size_t *max, size_t mult)
Expand Down Expand Up @@ -393,7 +401,7 @@ static void pull_input(const tal_t *ctx, const u8 **cursor, size_t *max,
static void pull_output(const tal_t *ctx, const u8 **cursor, size_t *max,
struct bitcoin_tx_output *output)
{
output->amount = pull_value(cursor, max);
output->amount = pull_amount_sat(cursor, max);
output->script = tal_arr(ctx, u8, pull_length(cursor, max, 1));
pull(cursor, max, output->script, tal_count(output->script));
}
Expand Down
5 changes: 3 additions & 2 deletions bitcoin/tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <ccan/short_types/short_types.h>
#include <ccan/structeq/structeq.h>
#include <ccan/tal/tal.h>
#include <common/amount.h>

struct bitcoin_txid {
struct sha256_double shad;
Expand All @@ -22,7 +23,7 @@ struct bitcoin_tx {
};

struct bitcoin_tx_output {
u64 amount;
struct amount_sat amount;
u8 *script;
};

Expand All @@ -33,7 +34,7 @@ struct bitcoin_tx_input {
u32 sequence_number;

/* Value of the output we're spending (NULL if unknown). */
u64 *amount;
struct amount_sat *amount;

/* Only if BIP141 used. */
u8 **witness;
Expand Down
5 changes: 2 additions & 3 deletions channeld/channeld.c
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ static secp256k1_ecdsa_signature *calc_commitsigs(const tal_t *ctx,

msg = towire_hsm_sign_remote_commitment_tx(NULL, txs[0],
&peer->channel->funding_pubkey[REMOTE],
(struct amount_sat){*txs[0]->input[0].amount});
*txs[0]->input[0].amount);

msg = hsm_req(tmpctx, take(msg));
if (!fromwire_hsm_sign_tx_reply(msg, commit_sig))
Expand Down Expand Up @@ -980,8 +980,7 @@ static secp256k1_ecdsa_signature *calc_commitsigs(const tal_t *ctx,
struct bitcoin_signature sig;
msg = towire_hsm_sign_remote_htlc_tx(NULL, txs[i + 1],
wscripts[i + 1],
(struct amount_sat)
{ *txs[i+1]->input[0].amount },
*txs[i+1]->input[0].amount,
&peer->remote_per_commit);

msg = hsm_req(tmpctx, take(msg));
Expand Down
34 changes: 21 additions & 13 deletions channeld/commit_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,13 @@ static void add_offered_htlc_out(struct bitcoin_tx *tx, size_t n,

ripemd160(&ripemd, htlc->rhash.u.u8, sizeof(htlc->rhash.u.u8));
wscript = htlc_offered_wscript(tx->output, &ripemd, keyset);
tx->output[n].amount = amount_msat_to_sat_round_down(htlc->amount).satoshis;
tx->output[n].amount = amount_msat_to_sat_round_down(htlc->amount);
tx->output[n].script = scriptpubkey_p2wsh(tx, wscript);
SUPERVERBOSE("# HTLC %"PRIu64" offered amount %"PRIu64" wscript %s\n",
htlc->id, tx->output[n].amount, tal_hex(wscript, wscript));
SUPERVERBOSE("# HTLC %"PRIu64" offered %s wscript %s\n",
htlc->id,
type_to_string(tmpctx, struct amount_sat,
&tx->output[n].amount),
tal_hex(wscript, wscript));
tal_free(wscript);
}

Expand All @@ -86,10 +89,13 @@ static void add_received_htlc_out(struct bitcoin_tx *tx, size_t n,

ripemd160(&ripemd, htlc->rhash.u.u8, sizeof(htlc->rhash.u.u8));
wscript = htlc_received_wscript(tx, &ripemd, &htlc->expiry, keyset);
tx->output[n].amount = amount_msat_to_sat_round_down(htlc->amount).satoshis;
tx->output[n].amount = amount_msat_to_sat_round_down(htlc->amount);
tx->output[n].script = scriptpubkey_p2wsh(tx->output, wscript);
SUPERVERBOSE("# HTLC %"PRIu64" received amount %"PRIu64" wscript %s\n",
htlc->id, tx->output[n].amount, tal_hex(wscript, wscript));
SUPERVERBOSE("# HTLC %"PRIu64" received %s wscript %s\n",
htlc->id,
type_to_string(tmpctx, struct amount_sat,
&tx->output[n].amount),
tal_hex(wscript, wscript));
tal_free(wscript);
}

Expand Down Expand Up @@ -217,13 +223,14 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
*/
if (amount_msat_greater_eq_sat(self_pay, dust_limit)) {
u8 *wscript = to_self_wscript(tmpctx, to_self_delay,keyset);
tx->output[n].amount = self_pay.millisatoshis / 1000;
tx->output[n].amount = amount_msat_to_sat_round_down(self_pay);
tx->output[n].script = scriptpubkey_p2wsh(tx, wscript);
(*htlcmap)[n] = NULL;
/* We don't assign cltvs[n]: if we use it, order doesn't matter.
* However, valgrind will warn us something wierd is happening */
SUPERVERBOSE("# to-local amount %"PRIu64" wscript %s\n",
tx->output[n].amount,
SUPERVERBOSE("# to-local amount %s wscript %s\n",
type_to_string(tmpctx, struct amount_sat,
&tx->output[n].amount),
tal_hex(tmpctx, wscript));
n++;
}
Expand All @@ -242,14 +249,15 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
* This output sends funds to the other peer and thus is a simple
* P2WPKH to `remotepubkey`.
*/
tx->output[n].amount = other_pay.millisatoshis / 1000;
tx->output[n].amount = amount_msat_to_sat_round_down(other_pay);
tx->output[n].script = scriptpubkey_p2wpkh(tx,
&keyset->other_payment_key);
(*htlcmap)[n] = NULL;
/* We don't assign cltvs[n]: if we use it, order doesn't matter.
* However, valgrind will warn us something wierd is happening */
SUPERVERBOSE("# to-remote amount %"PRIu64" P2WPKH(%s)\n",
tx->output[n].amount,
SUPERVERBOSE("# to-remote amount %s P2WPKH(%s)\n",
type_to_string(tmpctx, struct amount_sat,
&tx->output[n].amount),
type_to_string(tmpctx, struct pubkey,
&keyset->other_payment_key));
n++;
Expand Down Expand Up @@ -298,7 +306,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
= (0x80000000 | ((obscured_commitment_number>>24) & 0xFFFFFF));

/* Input amount needed for signature code. */
tx->input[0].amount = tal_dup(tx->input, u64, &funding.satoshis);
tx->input[0].amount = tal_dup(tx->input, struct amount_sat, &funding);

return tx;
}
6 changes: 6 additions & 0 deletions cli/test/run-large-input.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ int test_printf(const char *format, ...);
#undef main

/* AUTOGENERATED MOCKS START */
/* Generated stub for amount_sat_eq */
bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED)
{ fprintf(stderr, "amount_sat_eq called!\n"); abort(); }
/* Generated stub for amount_sat_less */
bool amount_sat_less(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED)
{ fprintf(stderr, "amount_sat_less called!\n"); abort(); }
/* Generated stub for version_and_exit */
char *version_and_exit(const void *unused UNNEEDED)
{ fprintf(stderr, "version_and_exit called!\n"); abort(); }
Expand Down
6 changes: 3 additions & 3 deletions common/close_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ struct bitcoin_tx *create_close_tx(const tal_t *ctx,
/* Our input spends the anchor tx output. */
tx->input[0].txid = *anchor_txid;
tx->input[0].index = anchor_index;
tx->input[0].amount = tal_dup(tx->input, u64, &funding.satoshis);
tx->input[0].amount = tal_dup(tx->input, struct amount_sat, &funding);

if (amount_sat_greater_eq(to_us, dust_limit)) {
/* One output is to us. */
tx->output[num_outputs].amount = to_us.satoshis;
tx->output[num_outputs].amount = to_us;
tx->output[num_outputs].script = tal_dup_arr(tx, u8,
our_script, tal_count(our_script), 0);
num_outputs++;
}

if (amount_sat_greater_eq(to_them, dust_limit)) {
/* Other output is to them. */
tx->output[num_outputs].amount = to_them.satoshis;
tx->output[num_outputs].amount = to_them;
tx->output[num_outputs].script = tal_dup_arr(tx, u8,
their_script, tal_count(their_script),
0);
Expand Down
13 changes: 7 additions & 6 deletions common/funding_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,32 @@
struct bitcoin_tx *funding_tx(const tal_t *ctx,
u16 *outnum,
const struct utxo **utxomap,
u64 funding_satoshis,
struct amount_sat funding,
const struct pubkey *local_fundingkey,
const struct pubkey *remote_fundingkey,
u64 change_satoshis,
struct amount_sat change,
const struct pubkey *changekey,
const struct ext_key *bip32_base)
{
u8 *wscript;
struct bitcoin_tx *tx;

tx = tx_spending_utxos(ctx, utxomap, bip32_base, change_satoshis != 0);
tx = tx_spending_utxos(ctx, utxomap, bip32_base,
!amount_sat_eq(change, AMOUNT_SAT(0)));

tx->output[0].amount = funding_satoshis;
tx->output[0].amount = funding;
wscript = bitcoin_redeem_2of2(tx, local_fundingkey, remote_fundingkey);
SUPERVERBOSE("# funding witness script = %s\n",
tal_hex(wscript, wscript));
tx->output[0].script = scriptpubkey_p2wsh(tx, wscript);
tal_free(wscript);

if (change_satoshis != 0) {
if (!amount_sat_eq(change, AMOUNT_SAT(0))) {
const void *map[2];
map[0] = int2ptr(0);
map[1] = int2ptr(1);
tx->output[1].script = scriptpubkey_p2wpkh(tx, changekey);
tx->output[1].amount = change_satoshis;
tx->output[1].amount = change;
permute_outputs(tx->output, NULL, map);
*outnum = (map[0] == int2ptr(0) ? 0 : 1);
} else {
Expand Down
9 changes: 5 additions & 4 deletions common/funding_tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "config.h"
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
#include <common/amount.h>

struct bitcoin_tx;
struct ext_key;
Expand All @@ -15,10 +16,10 @@ struct utxo;
* @ctx: context to tal from.
* @outnum: (out) txout (0 or 1) which is the funding output.
* @utxomap: (in/out) tal_arr of UTXO pointers to spend (permuted to match)
* @funding_satoshis: (in) satoshis to output.
* @funding: (in) satoshis to output.
* @local_fundingkey: (in) local key for 2of2 funding output.
* @remote_fundingkey: (in) remote key for 2of2 funding output.
* @change_satoshis: (in) amount to send as change.
* @change: (in) amount to send as change.
* @changekey: (in) key to send change to (only used if change_satoshis != 0).
* @bip32_base: (in) bip32 base for key derivation, or NULL.
*
Expand All @@ -34,10 +35,10 @@ struct utxo;
struct bitcoin_tx *funding_tx(const tal_t *ctx,
u16 *outnum,
const struct utxo **utxomap,
u64 funding_satoshis,
struct amount_sat funding,
const struct pubkey *local_fundingkey,
const struct pubkey *remote_fundingkey,
u64 change_satoshis,
struct amount_sat change,
const struct pubkey *changekey,
const struct ext_key *bip32_base);
#endif /* LIGHTNING_COMMON_FUNDING_TX_H */
7 changes: 3 additions & 4 deletions common/htlc_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static struct bitcoin_tx *htlc_tx(const tal_t *ctx,
{
struct bitcoin_tx *tx = bitcoin_tx(ctx, 1, 1);
u8 *wscript;
struct amount_sat amount, out_amount;
struct amount_sat amount;

/* BOLT #3:
*
Expand Down Expand Up @@ -49,7 +49,7 @@ static struct bitcoin_tx *htlc_tx(const tal_t *ctx,

/* We need amount for signing. */
amount = amount_msat_to_sat_round_down(msat);
tx->input[0].amount = tal_dup(tx, u64, &amount.satoshis);
tx->input[0].amount = tal_dup(tx, struct amount_sat, &amount);

/* BOLT #3:
* * `txin[0]` sequence: `0`
Expand All @@ -63,10 +63,9 @@ static struct bitcoin_tx *htlc_tx(const tal_t *ctx,
* * `txout[0]` script: version-0 P2WSH with witness script as shown
* below
*/
if (!amount_sat_sub(&out_amount, amount, htlc_fee))
if (!amount_sat_sub(&tx->output[0].amount, amount, htlc_fee))
abort();

tx->output[0].amount = out_amount.satoshis;
wscript = bitcoin_wscript_htlc_tx(tx, to_self_delay,
revocation_pubkey, local_delayedkey);
tx->output[0].script = scriptpubkey_p2wsh(tx, wscript);
Expand Down
6 changes: 3 additions & 3 deletions common/initial_commit_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
*/
if (amount_msat_greater_eq_sat(self_pay, dust_limit)) {
u8 *wscript = to_self_wscript(tmpctx, to_self_delay,keyset);
tx->output[n].amount = amount_msat_to_sat_round_down(self_pay).satoshis;
tx->output[n].amount = amount_msat_to_sat_round_down(self_pay);
tx->output[n].script = scriptpubkey_p2wsh(tx, wscript);
n++;
}
Expand All @@ -185,7 +185,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
* This output sends funds to the other peer and thus is a simple
* P2WPKH to `remotepubkey`.
*/
tx->output[n].amount = amount_msat_to_sat_round_down(other_pay).satoshis;
tx->output[n].amount = amount_msat_to_sat_round_down(other_pay);
tx->output[n].script = scriptpubkey_p2wpkh(tx,
&keyset->other_payment_key);
n++;
Expand Down Expand Up @@ -234,7 +234,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
= (0x80000000 | ((obscured_commitment_number>>24) & 0xFFFFFF));

/* Input amount needed for signature code. */
tx->input[0].amount = tal_dup(tx->input, u64, &funding.satoshis);
tx->input[0].amount = tal_dup(tx->input, struct amount_sat, &funding);

return tx;
}
4 changes: 2 additions & 2 deletions common/permute_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ static bool output_better(const struct bitcoin_tx_output *a,
size_t len, lena, lenb;
int ret;

if (a->amount != b->amount)
return a->amount < b->amount;
if (!amount_sat_eq(a->amount, b->amount))
return amount_sat_less(a->amount, b->amount);

/* Lexicographical sort. */
lena = tal_count(a->script);
Expand Down
Loading

0 comments on commit 948ca47

Please sign in to comment.