mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-17 19:03:42 +01:00
close_tx: trim based on dust level.
Saves the caller doing it (though legacy doesn't trim at all!). Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
6fa90c926a
commit
728593702f
31
close_tx.c
31
close_tx.c
@ -10,9 +10,13 @@ struct bitcoin_tx *create_close_tx(const tal_t *ctx,
|
|||||||
const struct sha256_double *anchor_txid,
|
const struct sha256_double *anchor_txid,
|
||||||
unsigned int anchor_index,
|
unsigned int anchor_index,
|
||||||
u64 anchor_satoshis,
|
u64 anchor_satoshis,
|
||||||
uint64_t to_us, uint64_t to_them)
|
uint64_t to_us, uint64_t to_them,
|
||||||
|
uint64_t dust_limit)
|
||||||
{
|
{
|
||||||
struct bitcoin_tx *tx;
|
struct bitcoin_tx *tx;
|
||||||
|
size_t num_outputs = 0;
|
||||||
|
|
||||||
|
assert(to_us + to_them <= anchor_satoshis);
|
||||||
|
|
||||||
/* Now create close tx: one input, two outputs. */
|
/* Now create close tx: one input, two outputs. */
|
||||||
tx = bitcoin_tx(ctx, 1, 2);
|
tx = bitcoin_tx(ctx, 1, 2);
|
||||||
@ -22,19 +26,28 @@ struct bitcoin_tx *create_close_tx(const tal_t *ctx,
|
|||||||
tx->input[0].index = anchor_index;
|
tx->input[0].index = anchor_index;
|
||||||
tx->input[0].amount = tal_dup(tx->input, u64, &anchor_satoshis);
|
tx->input[0].amount = tal_dup(tx->input, u64, &anchor_satoshis);
|
||||||
|
|
||||||
/* One output is to us. */
|
if (to_us >= dust_limit) {
|
||||||
tx->output[0].amount = to_us;
|
/* One output is to us. */
|
||||||
tx->output[0].script = tal_dup_arr(tx, u8,
|
tx->output[num_outputs].amount = to_us;
|
||||||
|
tx->output[num_outputs].script = tal_dup_arr(tx, u8,
|
||||||
our_script, tal_count(our_script), 0);
|
our_script, tal_count(our_script), 0);
|
||||||
|
num_outputs++;
|
||||||
|
}
|
||||||
|
|
||||||
/* Other output is to them. */
|
if (to_them >= dust_limit) {
|
||||||
tx->output[1].amount = to_them;
|
/* Other output is to them. */
|
||||||
tx->output[1].script = tal_dup_arr(tx, u8,
|
tx->output[num_outputs].amount = to_them;
|
||||||
|
tx->output[num_outputs].script = tal_dup_arr(tx, u8,
|
||||||
their_script, tal_count(their_script),
|
their_script, tal_count(their_script),
|
||||||
0);
|
0);
|
||||||
|
num_outputs++;
|
||||||
|
}
|
||||||
|
|
||||||
assert(tx->output[0].amount + tx->output[1].amount <= anchor_satoshis);
|
/* Can't have no outputs at all! */
|
||||||
|
if (num_outputs == 0)
|
||||||
|
return tal_free(tx);
|
||||||
|
tal_resize(&tx->output, num_outputs);
|
||||||
|
|
||||||
permute_outputs(tx->output, 2, NULL);
|
permute_outputs(tx->output, num_outputs, NULL);
|
||||||
return tx;
|
return tx;
|
||||||
}
|
}
|
||||||
|
@ -15,5 +15,6 @@ struct bitcoin_tx *create_close_tx(const tal_t *ctx,
|
|||||||
const struct sha256_double *anchor_txid,
|
const struct sha256_double *anchor_txid,
|
||||||
unsigned int anchor_index,
|
unsigned int anchor_index,
|
||||||
u64 anchor_satoshis,
|
u64 anchor_satoshis,
|
||||||
uint64_t to_us, uint64_t to_them);
|
uint64_t to_us, uint64_t to_them,
|
||||||
|
uint64_t dust_limit);
|
||||||
#endif
|
#endif
|
||||||
|
@ -4365,7 +4365,7 @@ struct bitcoin_tx *peer_create_close_tx(const tal_t *ctx,
|
|||||||
peer->anchor.index,
|
peer->anchor.index,
|
||||||
peer->anchor.satoshis,
|
peer->anchor.satoshis,
|
||||||
cstate.side[LOCAL].pay_msat / 1000,
|
cstate.side[LOCAL].pay_msat / 1000,
|
||||||
cstate.side[REMOTE].pay_msat / 1000);
|
cstate.side[REMOTE].pay_msat / 1000, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Sets up the initial cstate and commit tx for both nodes: false if
|
/* Sets up the initial cstate and commit tx for both nodes: false if
|
||||||
|
Loading…
Reference in New Issue
Block a user