Browse Source

elements: Consolidate weight computation to be handled by wally

Signed-off-by: Christian Decker <decker.christian@gmail.com>
travis-debug
Christian Decker 5 years ago
committed by Rusty Russell
parent
commit
557f6063a7
  1. 47
      bitcoin/tx.c
  2. 2
      bitcoin/tx.h
  3. 2
      lightningd/closing_control.c
  4. 2
      lightningd/onchain_control.c
  5. 2
      onchaind/onchaind.c

47
bitcoin/tx.c

@ -293,32 +293,6 @@ static bool uses_witness(const struct bitcoin_tx *tx)
return false;
}
/* BIP 141: The witness is a serialization of all witness data of the
* transaction. Each txin is associated with a witness field. A
* witness field starts with a var_int to indicate the number of stack
* items for the txin. */
static void push_witnesses(const struct bitcoin_tx *tx,
void (*push)(const void *, size_t, void *), void *pushp)
{
for (size_t i = 0; i < tx->wtx->num_inputs; i++) {
struct wally_tx_witness_stack *witness = tx->wtx->inputs[i].witness;
/* Not every input needs a witness. */
if (!witness) {
push_varint(0, push, pushp);
continue;
}
push_varint(witness->num_items, push, pushp);
for (size_t j = 0; j < witness->num_items; j++) {
size_t witlen = witness->items[j].witness_len;
const u8 *wit = witness->items[j].witness;
push_varint(witlen, push, pushp);
push(wit, witlen, pushp);
}
}
}
/* For signing, we ignore input scripts on other inputs, and pretend
* the current input has a certain script: this is indicated by a
* non-NULL override_script.
@ -377,23 +351,12 @@ u8 *linearize_tx(const tal_t *ctx, const struct bitcoin_tx *tx)
return arr;
}
static void push_measure(const void *data UNUSED, size_t len, void *lenp)
{
*(size_t *)lenp += len;
}
size_t measure_tx_weight(const struct bitcoin_tx *tx)
size_t bitcoin_tx_weight(const struct bitcoin_tx *tx)
{
size_t non_witness_len = 0, witness_len = 0;
push_tx(tx, NULL, 0, push_measure, &non_witness_len, false);
if (uses_witness(tx)) {
push_witnesses(tx, push_measure, &witness_len);
/* Include BIP 144 marker and flag bytes in witness length */
witness_len += 2;
}
/* Normal bytes weigh 4 times more than Witness bytes */
return non_witness_len * 4 + witness_len;
size_t weight;
int ret = wally_tx_get_weight(tx->wtx, &weight);
assert(ret == WALLY_OK);
return weight;
}
void bitcoin_txid(const struct bitcoin_tx *tx, struct bitcoin_txid *txid)

2
bitcoin/tx.h

@ -51,7 +51,7 @@ void bitcoin_txid(const struct bitcoin_tx *tx, struct bitcoin_txid *txid);
u8 *linearize_tx(const tal_t *ctx, const struct bitcoin_tx *tx);
/* Get weight of tx in Sipa. */
size_t measure_tx_weight(const struct bitcoin_tx *tx);
size_t bitcoin_tx_weight(const struct bitcoin_tx *tx);
/* Allocate a tx: you just need to fill in inputs and outputs (they're
* zeroed with inputs' sequence_number set to FFFFFFFF) */

2
lightningd/closing_control.c

@ -64,7 +64,7 @@ static bool better_closing_fee(struct lightningd *ld,
type_to_string(tmpctx, struct amount_sat, &last_fee));
/* Weight once we add in sigs. */
weight = measure_tx_weight(tx) + 74 * 2;
weight = bitcoin_tx_weight(tx) + 74 * 2;
/* If we don't have a feerate estimate, this gives feerate_floor */
min_feerate = feerate_min(ld, &feerate_unknown);

2
lightningd/onchain_control.c

@ -504,7 +504,7 @@ enum watch_result onchaind_funding_spent(struct channel *channel,
return KEEP_WATCHING;
}
feerate = fee.satoshis / measure_tx_weight(tx); /* Raw: reverse feerate extraction */
feerate = fee.satoshis / bitcoin_tx_weight(tx); /* Raw: reverse feerate extraction */
if (feerate < feerate_floor())
feerate = feerate_floor();
}

2
onchaind/onchaind.c

@ -328,7 +328,7 @@ static struct bitcoin_tx *tx_to_us(const tal_t *ctx,
tx, scriptpubkey_p2wpkh(tx, &our_wallet_pubkey), out->sat);
/* Worst-case sig is 73 bytes */
weight = measure_tx_weight(tx) + 1 + 3 + 73 + 0 + tal_count(wscript);
weight = bitcoin_tx_weight(tx) + 1 + 3 + 73 + 0 + tal_count(wscript);
fee = amount_tx_fee(feerate_per_kw, weight);
/* Result is trivial? Spend with small feerate, but don't wait

Loading…
Cancel
Save