mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-23 00:54:20 +01:00
tx: split 'compute fee' into two, with one that takes an input value
Transactions that we 'get' from bitciond don't have the input values available on the transaction; for these cases we'll sum up the inputs amounts using a different data source than the transaction's `input_amounts`. So we need to expose it here.
This commit is contained in:
committed by
Rusty Russell
parent
3d241bc261
commit
824e8fa72b
39
bitcoin/tx.c
39
bitcoin/tx.c
@@ -70,35 +70,46 @@ bool elements_tx_output_is_fee(const struct bitcoin_tx *tx, int outnum)
|
|||||||
tx->wtx->outputs[outnum].script_len == 0;
|
tx->wtx->outputs[outnum].script_len == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
struct amount_sat bitcoin_tx_compute_fee_w_inputs(const struct bitcoin_tx *tx,
|
||||||
* Compute how much fee we are actually sending with this transaction.
|
struct amount_sat input_val)
|
||||||
*/
|
|
||||||
static struct amount_sat bitcoin_tx_compute_fee(const struct bitcoin_tx *tx)
|
|
||||||
{
|
{
|
||||||
struct amount_sat fee = AMOUNT_SAT(0), value;
|
|
||||||
struct amount_asset asset;
|
struct amount_asset asset;
|
||||||
bool ok;
|
bool ok;
|
||||||
|
|
||||||
for (size_t i = 0; i < tal_count(tx->input_amounts); i++) {
|
|
||||||
value.satoshis = tx->input_amounts[i]->satoshis; /* Raw: fee computation */
|
|
||||||
ok = amount_sat_add(&fee, fee, value);
|
|
||||||
assert(ok);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (size_t i = 0; i < tx->wtx->num_outputs; i++) {
|
for (size_t i = 0; i < tx->wtx->num_outputs; i++) {
|
||||||
asset = bitcoin_tx_output_get_amount(tx, i);
|
asset = bitcoin_tx_output_get_amount(tx, i);
|
||||||
if (elements_tx_output_is_fee(tx, i) ||
|
if (elements_tx_output_is_fee(tx, i) ||
|
||||||
!amount_asset_is_main(&asset))
|
!amount_asset_is_main(&asset))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
value = amount_asset_to_sat(&asset);
|
ok = amount_sat_sub(&input_val, input_val,
|
||||||
ok = amount_sat_sub(&fee, fee, value);
|
amount_asset_to_sat(&asset));
|
||||||
assert(ok);
|
assert(ok);
|
||||||
}
|
}
|
||||||
return fee;
|
return input_val;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Compute how much fee we are actually sending with this transaction.
|
||||||
|
* Note that using this with a transaction without the input_amounts
|
||||||
|
* initialized/populated is an error.
|
||||||
|
*/
|
||||||
|
struct amount_sat bitcoin_tx_compute_fee(const struct bitcoin_tx *tx)
|
||||||
|
{
|
||||||
|
struct amount_sat input_total = AMOUNT_SAT(0);
|
||||||
|
bool ok;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < tal_count(tx->input_amounts); i++) {
|
||||||
|
assert(tx->input_amounts[i]);
|
||||||
|
ok = amount_sat_add(&input_total, input_total,
|
||||||
|
*tx->input_amounts[i]);
|
||||||
|
assert(ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
return bitcoin_tx_compute_fee_w_inputs(tx, input_total);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
* Add an explicit fee output if necessary.
|
* Add an explicit fee output if necessary.
|
||||||
*
|
*
|
||||||
* An explicit fee output is only necessary if we are using an elements
|
* An explicit fee output is only necessary if we are using an elements
|
||||||
|
|||||||
14
bitcoin/tx.h
14
bitcoin/tx.h
@@ -177,4 +177,18 @@ void bitcoin_tx_finalize(struct bitcoin_tx *tx);
|
|||||||
*/
|
*/
|
||||||
bool elements_tx_output_is_fee(const struct bitcoin_tx *tx, int outnum);
|
bool elements_tx_output_is_fee(const struct bitcoin_tx *tx, int outnum);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate the fees for this transaction
|
||||||
|
*/
|
||||||
|
struct amount_sat bitcoin_tx_compute_fee(const struct bitcoin_tx *tx);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Calculate the fees for this transaction, given a pre-computed input balance.
|
||||||
|
*
|
||||||
|
* This is needed for cases where the input_amounts aren't properly initialized,
|
||||||
|
* typically due to being passed across the wire.
|
||||||
|
*/
|
||||||
|
struct amount_sat bitcoin_tx_compute_fee_w_inputs(const struct bitcoin_tx *tx,
|
||||||
|
struct amount_sat input_val);
|
||||||
|
|
||||||
#endif /* LIGHTNING_BITCOIN_TX_H */
|
#endif /* LIGHTNING_BITCOIN_TX_H */
|
||||||
|
|||||||
Reference in New Issue
Block a user