Skip to content

Commit

Permalink
common: use bitcoin_outpoint.
Browse files Browse the repository at this point in the history
I started pulling this thread, and the entire codebase got unravelled.

Oh well, it's done now!

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed Oct 15, 2021
1 parent e7a8a0d commit c503232
Show file tree
Hide file tree
Showing 75 changed files with 1,061 additions and 1,156 deletions.
25 changes: 13 additions & 12 deletions bitcoin/psbt.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ struct wally_psbt_input *psbt_add_input(struct wally_psbt *psbt,
}

struct wally_psbt_input *psbt_append_input(struct wally_psbt *psbt,
const struct bitcoin_txid *txid,
u32 outnum, u32 sequence,
const struct bitcoin_outpoint *outpoint,
u32 sequence,
const u8 *scriptSig,
const u8 *input_wscript,
const u8 *redeemscript)
Expand All @@ -139,9 +139,10 @@ struct wally_psbt_input *psbt_append_input(struct wally_psbt *psbt,

tal_wally_start();
if (chainparams->is_elements) {
if (wally_tx_elements_input_init_alloc(txid->shad.sha.u.u8,
sizeof(txid->shad.sha.u.u8),
outnum, sequence, NULL, 0,
if (wally_tx_elements_input_init_alloc(outpoint->txid.shad.sha.u.u8,
sizeof(outpoint->txid.shad.sha.u.u8),
outpoint->n,
sequence, NULL, 0,
NULL,
NULL, 0,
NULL, 0, NULL, 0,
Expand All @@ -150,9 +151,10 @@ struct wally_psbt_input *psbt_append_input(struct wally_psbt *psbt,
&tx_in) != WALLY_OK)
abort();
} else {
if (wally_tx_input_init_alloc(txid->shad.sha.u.u8,
sizeof(txid->shad.sha.u.u8),
outnum, sequence, NULL, 0, NULL,
if (wally_tx_input_init_alloc(outpoint->txid.shad.sha.u.u8,
sizeof(outpoint->txid.shad.sha.u.u8),
outpoint->n,
sequence, NULL, 0, NULL,
&tx_in) != WALLY_OK)
abort();
}
Expand Down Expand Up @@ -412,18 +414,17 @@ void psbt_elements_normalize_fees(struct wally_psbt *psbt)
}

bool psbt_has_input(const struct wally_psbt *psbt,
const struct bitcoin_txid *txid,
u32 outnum)
const struct bitcoin_outpoint *outpoint)
{
for (size_t i = 0; i < psbt->num_inputs; i++) {
struct bitcoin_txid in_txid;
struct wally_tx_input *in = &psbt->tx->inputs[i];

if (outnum != in->index)
if (outpoint->n != in->index)
continue;

wally_tx_input_get_txid(in, &in_txid);
if (bitcoin_txid_eq(txid, &in_txid))
if (bitcoin_txid_eq(&outpoint->txid, &in_txid))
return true;
}
return false;
Expand Down
11 changes: 5 additions & 6 deletions bitcoin/psbt.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct wally_tx_output;
struct wally_map;
struct amount_asset;
struct amount_sat;
struct bitcoin_outpoint;
struct bitcoin_signature;
struct bitcoin_txid;
struct pubkey;
Expand Down Expand Up @@ -119,8 +120,8 @@ struct wally_psbt_input *psbt_add_input(struct wally_psbt *psbt,

/* One stop shop for adding an input + metadata to a PSBT */
struct wally_psbt_input *psbt_append_input(struct wally_psbt *psbt,
const struct bitcoin_txid *txid,
u32 outnum, u32 sequence,
const struct bitcoin_outpoint *outpoint,
u32 sequence,
const u8 *scriptSig,
const u8 *input_wscript,
const u8 *redeemscript);
Expand Down Expand Up @@ -253,12 +254,10 @@ struct amount_sat psbt_compute_fee(const struct wally_psbt *psbt);
/* psbt_has_input - Is this input present on this psbt
*
* @psbt - psbt
* @txid - txid of input
* @outnum - output index of input
* @outpoint - txid/index spent by input
*/
bool psbt_has_input(const struct wally_psbt *psbt,
const struct bitcoin_txid *txid,
u32 outnum);
const struct bitcoin_outpoint *outpoint);

struct wally_psbt *psbt_from_b64(const tal_t *ctx,
const char *b64,
Expand Down
44 changes: 31 additions & 13 deletions bitcoin/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,17 @@ void bitcoin_tx_set_locktime(struct bitcoin_tx *tx, u32 locktime)
tx->psbt->tx->locktime = locktime;
}

int bitcoin_tx_add_input(struct bitcoin_tx *tx, const struct bitcoin_txid *txid,
u32 outnum, u32 sequence, const u8 *scriptSig,
int bitcoin_tx_add_input(struct bitcoin_tx *tx,
const struct bitcoin_outpoint *outpoint,
u32 sequence, const u8 *scriptSig,
struct amount_sat amount, const u8 *scriptPubkey,
const u8 *input_wscript)
{
int wally_err;
int input_num = tx->wtx->num_inputs;

psbt_append_input(tx->psbt, txid, outnum, sequence, scriptSig,
psbt_append_input(tx->psbt, outpoint,
sequence, scriptSig,
input_wscript, NULL);

if (input_wscript) {
Expand Down Expand Up @@ -388,33 +390,50 @@ const u8 *bitcoin_tx_input_get_witness(const tal_t *ctx,
return witness_item;
}

/* FIXME: remove */
void bitcoin_tx_input_get_txid(const struct bitcoin_tx *tx, int innum,
struct bitcoin_txid *out)
{
assert(innum < tx->wtx->num_inputs);
wally_tx_input_get_txid(&tx->wtx->inputs[innum], out);
}

void bitcoin_tx_input_set_txid(struct bitcoin_tx *tx, int innum,
const struct bitcoin_txid *txid,
u32 index)
void bitcoin_tx_input_get_outpoint(const struct bitcoin_tx *tx,
int innum,
struct bitcoin_outpoint *outpoint)
{
assert(innum < tx->wtx->num_inputs);
wally_tx_input_get_outpoint(&tx->wtx->inputs[innum], outpoint);
}

void bitcoin_tx_input_set_outpoint(struct bitcoin_tx *tx, int innum,
const struct bitcoin_outpoint *outpoint)
{
struct wally_tx_input *in;
assert(innum < tx->wtx->num_inputs);

in = &tx->wtx->inputs[innum];
BUILD_ASSERT(sizeof(struct bitcoin_txid) == sizeof(in->txhash));
memcpy(in->txhash, txid, sizeof(struct bitcoin_txid));
in->index = index;
memcpy(in->txhash, &outpoint->txid, sizeof(struct bitcoin_txid));
in->index = outpoint->n;
}

/* FIXME: remove */
void wally_tx_input_get_txid(const struct wally_tx_input *in,
struct bitcoin_txid *txid)
{
BUILD_ASSERT(sizeof(struct bitcoin_txid) == sizeof(in->txhash));
memcpy(txid, in->txhash, sizeof(struct bitcoin_txid));
}

void wally_tx_input_get_outpoint(const struct wally_tx_input *in,
struct bitcoin_outpoint *outpoint)
{
BUILD_ASSERT(sizeof(struct bitcoin_txid) == sizeof(in->txhash));
memcpy(&outpoint->txid, in->txhash, sizeof(struct bitcoin_txid));
outpoint->n = in->index;
}

/* BIP144:
* If the witness is empty, the old serialization format should be used. */
static bool uses_witness(const struct wally_tx *wtx)
Expand Down Expand Up @@ -791,16 +810,15 @@ void towire_bitcoin_tx_output(u8 **pptr, const struct bitcoin_tx_output *output)
}

bool wally_tx_input_spends(const struct wally_tx_input *input,
const struct bitcoin_txid *txid,
int outnum)
const struct bitcoin_outpoint *outpoint)
{
/* Useful, as tx_part can have some NULL inputs */
if (!input)
return false;
BUILD_ASSERT(sizeof(*txid) == sizeof(input->txhash));
if (memcmp(txid, input->txhash, sizeof(*txid)) != 0)
BUILD_ASSERT(sizeof(outpoint->txid) == sizeof(input->txhash));
if (memcmp(&outpoint->txid, input->txhash, sizeof(outpoint->txid)) != 0)
return false;
return input->index == outnum;
return input->index == outpoint->n;
}

/* FIXME(cdecker) Make the caller pass in a reference to amount_asset, and
Expand Down
23 changes: 16 additions & 7 deletions bitcoin/tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ struct bitcoin_outpoint {
/* Define bitcoin_txid_eq */
STRUCTEQ_DEF(bitcoin_txid, 0, shad.sha.u);

/* Define bitcoin_outpoint_eq */
STRUCTEQ_DEF(bitcoin_outpoint, 0, txid.shad.sha.u, n);

struct bitcoin_tx {
struct wally_tx *wtx;

Expand Down Expand Up @@ -109,15 +112,15 @@ void bitcoin_tx_set_locktime(struct bitcoin_tx *tx, u32 locktime);
* Passing in just the {input_wscript}, we'll generate the scriptPubkey for you.
* In some cases we may not have the wscript, in which case the scriptPubkey
* should be provided. We'll check that it's P2WSH before saving it */
int bitcoin_tx_add_input(struct bitcoin_tx *tx, const struct bitcoin_txid *txid,
u32 outnum, u32 sequence, const u8 *scriptSig,
int bitcoin_tx_add_input(struct bitcoin_tx *tx,
const struct bitcoin_outpoint *outpoint,
u32 sequence, const u8 *scriptSig,
struct amount_sat amount, const u8 *scriptPubkey,
const u8 *input_wscript);

/* This helps is useful because wally uses a raw byte array for txids */
bool wally_tx_input_spends(const struct wally_tx_input *input,
const struct bitcoin_txid *txid,
int outnum);
const struct bitcoin_outpoint *outpoint);

struct amount_asset
wally_tx_output_get_amount(const struct wally_tx_output *output);
Expand Down Expand Up @@ -193,17 +196,23 @@ const u8 *bitcoin_tx_input_get_witness(const tal_t *ctx,
/**
* Wrap the raw txhash in the wally_tx_input into a bitcoin_txid
*/
void bitcoin_tx_input_get_outpoint(const struct bitcoin_tx *tx,
int innum,
struct bitcoin_outpoint *outpoint);

void bitcoin_tx_input_get_txid(const struct bitcoin_tx *tx, int innum,
struct bitcoin_txid *out);
void wally_tx_input_get_txid(const struct wally_tx_input *in,
struct bitcoin_txid *txid);

void wally_tx_input_get_outpoint(const struct wally_tx_input *in,
struct bitcoin_outpoint *outpoint);

/**
* Overwrite the txhash and index in the wally_tx_input
*/
void bitcoin_tx_input_set_txid(struct bitcoin_tx *tx, int innum,
const struct bitcoin_txid *txid,
u32 index);
void bitcoin_tx_input_set_outpoint(struct bitcoin_tx *tx, int innum,
const struct bitcoin_outpoint *outpoint);

/**
* Check a transaction for consistency.
Expand Down
19 changes: 9 additions & 10 deletions channeld/channeld.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,12 @@ static struct amount_msat advertized_htlc_max(const struct channel *channel)
struct amount_msat lower_bound_msat;

/* This shouldn't fail */
if (!amount_sat_sub(&lower_bound, channel->funding,
if (!amount_sat_sub(&lower_bound, channel->funding_sats,
channel->config[REMOTE].channel_reserve)) {
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"funding %s - remote reserve %s?",
type_to_string(tmpctx, struct amount_sat,
&channel->funding),
&channel->funding_sats),
type_to_string(tmpctx, struct amount_sat,
&channel->config[REMOTE]
.channel_reserve));
Expand Down Expand Up @@ -454,7 +454,8 @@ static void make_channel_local_active(struct peer *peer)

/* Tell gossipd about local channel. */
msg = towire_gossip_store_private_channel(NULL,
peer->channel->funding, ann);
peer->channel->funding_sats,
ann);
wire_sync_write(peer->pps->gossip_fd, take(msg));

/* Tell gossipd and the other side what parameters we expect should
Expand Down Expand Up @@ -3755,12 +3756,11 @@ static void req_in(struct peer *peer, const u8 *msg)
static void init_channel(struct peer *peer)
{
struct basepoints points[NUM_SIDES];
struct amount_sat funding;
u16 funding_txout;
struct amount_sat funding_sats;
struct amount_msat local_msat;
struct pubkey funding_pubkey[NUM_SIDES];
struct channel_config conf[NUM_SIDES];
struct bitcoin_txid funding_txid;
struct bitcoin_outpoint funding;
enum side opener;
struct existing_htlc **htlcs;
bool reconnected;
Expand All @@ -3786,8 +3786,8 @@ static void init_channel(struct peer *peer)
&chainparams,
&peer->our_features,
&peer->channel_id,
&funding_txid, &funding_txout,
&funding,
&funding_sats,
&minimum_depth,
&peer->our_blockheight,
&blockheight_states,
Expand Down Expand Up @@ -3899,12 +3899,11 @@ static void init_channel(struct peer *peer)
&peer->next_local_per_commit, NULL);

peer->channel = new_full_channel(peer, &peer->channel_id,
&funding_txid,
funding_txout,
&funding,
minimum_depth,
take(blockheight_states),
lease_expiry,
funding,
funding_sats,
local_msat,
take(fee_states),
&conf[LOCAL], &conf[REMOTE],
Expand Down
3 changes: 1 addition & 2 deletions channeld/channeld_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ msgtype,channeld_init,1000
msgdata,channeld_init,chainparams,chainparams,
msgdata,channeld_init,our_features,feature_set,
msgdata,channeld_init,channel_id,channel_id,
msgdata,channeld_init,funding_txid,bitcoin_txid,
msgdata,channeld_init,funding_txout,u16,
msgdata,channeld_init,funding,bitcoin_outpoint,
msgdata,channeld_init,funding_satoshi,amount_sat,
msgdata,channeld_init,minimum_depth,u32,
msgdata,channeld_init,our_blockheight,u32,
Expand Down
13 changes: 6 additions & 7 deletions channeld/commit_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ static void add_received_htlc_out(struct bitcoin_tx *tx, size_t n,
}

struct bitcoin_tx *commit_tx(const tal_t *ctx,
const struct bitcoin_txid *funding_txid,
unsigned int funding_txout,
struct amount_sat funding,
const struct bitcoin_outpoint *funding,
struct amount_sat funding_sats,
const struct pubkey *local_funding_key,
const struct pubkey *remote_funding_key,
enum side opener,
Expand Down Expand Up @@ -119,7 +118,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,

if (!amount_msat_add(&total_pay, self_pay, other_pay))
abort();
assert(!amount_msat_greater_sat(total_pay, funding));
assert(!amount_msat_greater_sat(total_pay, funding_sats));

/* BOLT #3:
*
Expand Down Expand Up @@ -176,7 +175,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
ok &= amount_sat_add(&out, out, amount_msat_to_sat_round_down(other_pay));
assert(ok);
SUPERVERBOSE("# actual commitment transaction fee = %"PRIu64"\n",
funding.satoshis - out.satoshis); /* Raw: test output */
funding_sats.satoshis - out.satoshis); /* Raw: test output */
}
#endif

Expand Down Expand Up @@ -390,8 +389,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
* * `txin[0]` sequence: upper 8 bits are 0x80, lower 24 bits are upper 24 bits of the obscured commitment number
*/
u32 sequence = (0x80000000 | ((obscured_commitment_number>>24) & 0xFFFFFF));
bitcoin_tx_add_input(tx, funding_txid, funding_txout,
sequence, NULL, funding, NULL, funding_wscript);
bitcoin_tx_add_input(tx, funding,
sequence, NULL, funding_sats, NULL, funding_wscript);

/* Identify the direct outputs (to_us, to_them). */
if (direct_outputs != NULL) {
Expand Down
7 changes: 3 additions & 4 deletions channeld/commit_tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ size_t commit_tx_num_untrimmed(const struct htlc **htlcs,
/**
* commit_tx: create (unsigned) commitment tx to spend the funding tx output
* @ctx: context to allocate transaction and @htlc_map from.
* @funding_txid, @funding_out, @funding: funding outpoint.
* @funding, @funding_sats: funding outpoint and amount
* @local_funding_key, @remote_funding_key: keys for funding input.
* @opener: is the LOCAL or REMOTE paying the fee?
* @keyset: keys derived for this commit tx.
Expand All @@ -48,9 +48,8 @@ size_t commit_tx_num_untrimmed(const struct htlc **htlcs,
* transaction, so we carefully use the terms "self" and "other" here.
*/
struct bitcoin_tx *commit_tx(const tal_t *ctx,
const struct bitcoin_txid *funding_txid,
unsigned int funding_txout,
struct amount_sat funding,
const struct bitcoin_outpoint *funding,
struct amount_sat funding_sats,
const struct pubkey *local_funding_key,
const struct pubkey *remote_funding_key,
enum side opener,
Expand Down
Loading

0 comments on commit c503232

Please sign in to comment.