Browse Source

tx: Use the context chainparams to determine the fee asset

This is the main reason we started weaving the chainparams everywhere: being
able to compare the asset type with the fee paying asset tag, thus determining
the value of the asset correctly (we still treat any non-fee-paying assets as
having value 0).

Signed-off-by: Christian Decker <decker.christian@gmail.com>
travis-debug
Christian Decker 5 years ago
committed by Rusty Russell
parent
commit
fad9a74662
  1. 31
      bitcoin/tx.c
  2. 3
      onchaind/test/run-grind_feerate-bug.c
  3. 1
      onchaind/test/run-grind_feerate.c

31
bitcoin/tx.c

@ -14,13 +14,6 @@
#define SEGREGATED_WITNESS_FLAG 0x1 #define SEGREGATED_WITNESS_FLAG 0x1
static u8 bitcoin_asset[] =
{
0x01, 0x5c, 0xe7, 0xb9, 0x63, 0xd3, 0x7f, 0x8f, 0x2d, 0x51, 0xca,
0xfb, 0xba, 0x92, 0x8a, 0xaa, 0x9e, 0x22, 0x0b, 0x8b, 0xbc, 0x66,
0x05, 0x71, 0x49, 0x9c, 0x03, 0x62, 0x8a, 0x38, 0x51, 0xb8, 0xce,
};
int bitcoin_tx_add_output(struct bitcoin_tx *tx, const u8 *script, int bitcoin_tx_add_output(struct bitcoin_tx *tx, const u8 *script,
struct amount_sat amount) struct amount_sat amount)
{ {
@ -28,9 +21,11 @@ int bitcoin_tx_add_output(struct bitcoin_tx *tx, const u8 *script,
struct wally_tx_output *output; struct wally_tx_output *output;
int ret; int ret;
u64 satoshis = amount.satoshis; /* Raw: low-level helper */ u64 satoshis = amount.satoshis; /* Raw: low-level helper */
const struct chainparams *chainparams = tx->chainparams;
assert(i < tx->wtx->outputs_allocation_len); assert(i < tx->wtx->outputs_allocation_len);
assert(tx->wtx != NULL); assert(tx->wtx != NULL);
assert(chainparams);
if (is_elements) { if (is_elements) {
u8 value[9]; u8 value[9];
@ -38,9 +33,8 @@ int bitcoin_tx_add_output(struct bitcoin_tx *tx, const u8 *script,
sizeof(value)); sizeof(value));
assert(ret == WALLY_OK); assert(ret == WALLY_OK);
ret = wally_tx_elements_output_init_alloc( ret = wally_tx_elements_output_init_alloc(
script, tal_bytelen(script), bitcoin_asset, script, tal_bytelen(script), chainparams->fee_asset_tag, 33,
sizeof(bitcoin_asset), value, sizeof(value), NULL, 0, NULL, value, sizeof(value), NULL, 0, NULL, 0, NULL, 0, &output);
0, NULL, 0, &output);
assert(ret == WALLY_OK); assert(ret == WALLY_OK);
/* Cheat a bit by also setting the numeric satoshi value, /* Cheat a bit by also setting the numeric satoshi value,
* otherwise we end up converting a number of times */ * otherwise we end up converting a number of times */
@ -212,18 +206,23 @@ struct amount_sat bitcoin_tx_output_get_amount(const struct bitcoin_tx *tx,
struct amount_sat amount; struct amount_sat amount;
struct wally_tx_output *output; struct wally_tx_output *output;
u64 satoshis; u64 satoshis;
const u8 *fee_asset_tag;
assert(tx->chainparams);
assert(outnum < tx->wtx->num_outputs); assert(outnum < tx->wtx->num_outputs);
output = &tx->wtx->outputs[outnum]; output = &tx->wtx->outputs[outnum];
fee_asset_tag = tx->chainparams->fee_asset_tag;
if (is_elements && !memeq(output->asset, output->asset_len, if (fee_asset_tag) {
bitcoin_asset, sizeof(bitcoin_asset))) { if (memeq(fee_asset_tag, 33, output->asset, output->asset_len)) {
/* If this is an asset based tx, and we don't know the asset
* type, i.e., it's not bitcoin, return a 0 amount */
satoshis = 0;
} else if (is_elements) {
be64 raw; be64 raw;
memcpy(&raw, output->value + 1, sizeof(raw)); memcpy(&raw, output->value + 1, sizeof(raw));
satoshis = be64_to_cpu(raw); satoshis = be64_to_cpu(raw);
} else {
/* If this is an asset based tx, and we don't know the
* asset type, i.e., it's not bitcoin, return a 0
* amount */
satoshis = 0;
}
} else { } else {
satoshis = tx->wtx->outputs[outnum].satoshi; satoshis = tx->wtx->outputs[outnum].satoshi;
} }

3
onchaind/test/run-grind_feerate-bug.c

@ -207,7 +207,7 @@ static void signature_from_hex(const char *hex, struct bitcoin_signature *sig)
/* We don't have enough info to make this from first principles, but we have /* We don't have enough info to make this from first principles, but we have
* an example tx, so just mangle that. */ * an example tx, so just mangle that. */
struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx, struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx,
const struct chainparams *chainparams UNNEEDED, const struct chainparams *chainparams,
const struct bitcoin_txid *commit_txid UNNEEDED, const struct bitcoin_txid *commit_txid UNNEEDED,
unsigned int commit_output_number UNNEEDED, unsigned int commit_output_number UNNEEDED,
struct amount_msat htlc_msatoshi, struct amount_msat htlc_msatoshi,
@ -225,6 +225,7 @@ struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx,
in_amount = amount_msat_to_sat_round_down(htlc_msatoshi); in_amount = amount_msat_to_sat_round_down(htlc_msatoshi);
tx->input_amounts[0] = tal_dup(tx, struct amount_sat, &in_amount); tx->input_amounts[0] = tal_dup(tx, struct amount_sat, &in_amount);
tx->chainparams = chainparams;
tx->wtx->locktime = cltv_expiry; tx->wtx->locktime = cltv_expiry;
return tx; return tx;

1
onchaind/test/run-grind_feerate.c

@ -203,6 +203,7 @@ int main(int argc, char *argv[])
setup_tmpctx(); setup_tmpctx();
tx = bitcoin_tx_from_hex(tmpctx, "0200000001e1ebca08cf1c301ac563580a1126d5c8fcb0e5e2043230b852c726553caf1e1d0000000000000000000160ae0a000000000022002082e03c5a9cb79c82cd5a0572dc175290bc044609aabe9cc852d61927436041796d000000", tx = bitcoin_tx_from_hex(tmpctx, "0200000001e1ebca08cf1c301ac563580a1126d5c8fcb0e5e2043230b852c726553caf1e1d0000000000000000000160ae0a000000000022002082e03c5a9cb79c82cd5a0572dc175290bc044609aabe9cc852d61927436041796d000000",
strlen("0200000001e1ebca08cf1c301ac563580a1126d5c8fcb0e5e2043230b852c726553caf1e1d0000000000000000000160ae0a000000000022002082e03c5a9cb79c82cd5a0572dc175290bc044609aabe9cc852d61927436041796d000000")); strlen("0200000001e1ebca08cf1c301ac563580a1126d5c8fcb0e5e2043230b852c726553caf1e1d0000000000000000000160ae0a000000000022002082e03c5a9cb79c82cd5a0572dc175290bc044609aabe9cc852d61927436041796d000000"));
tx->chainparams = chainparams_for_network("regtest");
tx->input_amounts[0] = tal(tx, struct amount_sat); tx->input_amounts[0] = tal(tx, struct amount_sat);
*tx->input_amounts[0] = AMOUNT_SAT(700000); *tx->input_amounts[0] = AMOUNT_SAT(700000);
tx->chainparams = chainparams_for_network("bitcoin"); tx->chainparams = chainparams_for_network("bitcoin");

Loading…
Cancel
Save