mirror of
https://github.com/ElementsProject/lightning.git
synced 2024-11-19 18:11:28 +01:00
6d6abd57e7
Use our own structure with the information we need about HTLCs, and remove protobufs from the API. The is_funder() helper goes inside gather_updates.h. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
106 lines
3.3 KiB
C
106 lines
3.3 KiB
C
#include "bitcoin/locktime.h"
|
|
#include "bitcoin/pubkey.h"
|
|
#include "bitcoin/script.h"
|
|
#include "bitcoin/shadouble.h"
|
|
#include "bitcoin/tx.h"
|
|
#include "commit_tx.h"
|
|
#include "funding.h"
|
|
#include "overflows.h"
|
|
#include "permute_tx.h"
|
|
#include <assert.h>
|
|
|
|
static bool add_htlc(struct bitcoin_tx *tx, size_t n,
|
|
const struct channel_htlc *h,
|
|
const struct pubkey *ourkey,
|
|
const struct pubkey *theirkey,
|
|
const struct sha256 *rhash,
|
|
const struct rel_locktime *locktime,
|
|
u8 *(*scriptpubkeyfn)(const tal_t *,
|
|
const struct pubkey *,
|
|
const struct pubkey *,
|
|
const struct abs_locktime *,
|
|
const struct rel_locktime *,
|
|
const struct sha256 *,
|
|
const struct sha256 *))
|
|
{
|
|
assert(!tx->output[n].script);
|
|
|
|
tx->output[n].script = scriptpubkey_p2sh(tx,
|
|
scriptpubkeyfn(tx, ourkey, theirkey,
|
|
&h->expiry, locktime, rhash,
|
|
&h->rhash));
|
|
tx->output[n].script_length = tal_count(tx->output[n].script);
|
|
tx->output[n].amount = h->msatoshis / 1000;
|
|
return true;
|
|
}
|
|
|
|
struct bitcoin_tx *create_commit_tx(const tal_t *ctx,
|
|
const struct pubkey *our_final,
|
|
const struct pubkey *their_final,
|
|
const struct rel_locktime *their_locktime,
|
|
const struct sha256_double *anchor_txid,
|
|
unsigned int anchor_index,
|
|
u64 anchor_satoshis,
|
|
const struct sha256 *rhash,
|
|
const struct channel_state *cstate)
|
|
{
|
|
struct bitcoin_tx *tx;
|
|
const u8 *redeemscript;
|
|
size_t i, num;
|
|
uint64_t total;
|
|
|
|
/* Now create commitment tx: one input, two outputs (plus htlcs) */
|
|
tx = bitcoin_tx(ctx, 1, 2 + tal_count(cstate->a.htlcs)
|
|
+ tal_count(cstate->b.htlcs));
|
|
|
|
/* Our input spends the anchor tx output. */
|
|
tx->input[0].txid = *anchor_txid;
|
|
tx->input[0].index = anchor_index;
|
|
tx->input[0].input_amount = anchor_satoshis;
|
|
|
|
/* First output is a P2SH to a complex redeem script (usu. for me) */
|
|
redeemscript = bitcoin_redeem_secret_or_delay(tx, our_final,
|
|
their_locktime,
|
|
their_final,
|
|
rhash);
|
|
tx->output[0].script = scriptpubkey_p2sh(tx, redeemscript);
|
|
tx->output[0].script_length = tal_count(tx->output[0].script);
|
|
tx->output[0].amount = cstate->a.pay_msat / 1000;
|
|
|
|
/* Second output is a P2SH payment to them. */
|
|
tx->output[1].script = scriptpubkey_p2sh(tx,
|
|
bitcoin_redeem_single(tx,
|
|
their_final));
|
|
tx->output[1].script_length = tal_count(tx->output[1].script);
|
|
tx->output[1].amount = cstate->b.pay_msat / 1000;
|
|
|
|
/* First two outputs done, now for the HTLCs. */
|
|
total = tx->output[0].amount + tx->output[1].amount;
|
|
num = 2;
|
|
|
|
/* HTLCs we've sent. */
|
|
for (i = 0; i < tal_count(cstate->a.htlcs); i++) {
|
|
if (!add_htlc(tx, num, &cstate->a.htlcs[i],
|
|
our_final, their_final,
|
|
rhash, their_locktime, scriptpubkey_htlc_send))
|
|
return tal_free(tx);
|
|
total += tx->output[num++].amount;
|
|
}
|
|
/* HTLCs we've received. */
|
|
for (i = 0; i < tal_count(cstate->b.htlcs); i++) {
|
|
if (!add_htlc(tx, num, &cstate->b.htlcs[i],
|
|
our_final, their_final,
|
|
rhash, their_locktime, scriptpubkey_htlc_recv))
|
|
return tal_free(tx);
|
|
total += tx->output[num++].amount;
|
|
}
|
|
assert(num == tx->output_count);
|
|
|
|
/* Calculate fee; difference of inputs and outputs. */
|
|
assert(total <= tx->input[0].input_amount);
|
|
tx->fee = tx->input[0].input_amount - total;
|
|
|
|
permute_outputs(tx->output, tx->output_count, NULL);
|
|
return tx;
|
|
}
|