wally: Migrate main daemon to use wally transactions

Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
Christian Decker
2019-03-25 11:35:56 +01:00
committed by Rusty Russell
parent c39963b8b7
commit d651ce6f3b
13 changed files with 73 additions and 69 deletions

View File

@@ -144,7 +144,7 @@ bool check_tx_sig(const struct bitcoin_tx *tx, size_t input_num,
if (sig->sighash_type != (SIGHASH_SINGLE|SIGHASH_ANYONECANPAY)) if (sig->sighash_type != (SIGHASH_SINGLE|SIGHASH_ANYONECANPAY))
return false; return false;
} }
assert(input_num < tal_count(tx->input)); assert(input_num < tx->wtx->num_inputs);
wally_tx_get_btc_signature_hash( wally_tx_get_btc_signature_hash(
tx->wtx, input_num, script, tal_bytelen(script), tx->wtx, input_num, script, tal_bytelen(script),

View File

@@ -164,23 +164,14 @@ void bitcoin_tx_input_get_txid(const struct bitcoin_tx *tx, int innum,
memcpy(out, tx->wtx->inputs[innum].txhash, sizeof(struct bitcoin_txid)); memcpy(out, tx->wtx->inputs[innum].txhash, sizeof(struct bitcoin_txid));
} }
/* BIP 141:
* It is followed by stack items, with each item starts with a var_int
* to indicate the length. */
static void push_witness(const u8 *witness,
void (*push)(const void *, size_t, void *), void *pushp)
{
push_varint_blob(witness, push, pushp);
}
/* BIP144: /* BIP144:
* If the witness is empty, the old serialization format should be used. */ * If the witness is empty, the old serialization format should be used. */
static bool uses_witness(const struct bitcoin_tx *tx) static bool uses_witness(const struct bitcoin_tx *tx)
{ {
size_t i; size_t i;
for (i = 0; i < tal_count(tx->input); i++) { for (i = 0; i < tx->wtx->num_inputs; i++) {
if (tx->input[i].witness) if (tx->wtx->inputs[i].witness)
return true; return true;
} }
return false; return false;
@@ -193,22 +184,21 @@ static bool uses_witness(const struct bitcoin_tx *tx)
static void push_witnesses(const struct bitcoin_tx *tx, static void push_witnesses(const struct bitcoin_tx *tx,
void (*push)(const void *, size_t, void *), void *pushp) void (*push)(const void *, size_t, void *), void *pushp)
{ {
size_t i; for (size_t i = 0; i < tx->wtx->num_inputs; i++) {
for (i = 0; i < tal_count(tx->input); i++) { struct wally_tx_witness_stack *witness = tx->wtx->inputs[i].witness;
size_t j, elements;
/* Not every input needs a witness. */ /* Not every input needs a witness. */
if (!tx->input[i].witness) { if (!witness) {
push_varint(0, push, pushp); push_varint(0, push, pushp);
continue; continue;
} }
elements = tal_count(tx->input[i].witness);
push_varint(elements, push, pushp); push_varint(witness->num_items, push, pushp);
for (j = 0; for (size_t j = 0; j < witness->num_items; j++) {
j < tal_count(tx->input[i].witness); size_t witlen = witness->items[j].witness_len;
j++) { const u8 *wit = witness->items[j].witness;
push_witness(tx->input[i].witness[j], push_varint(witlen, push, pushp);
push, pushp); push(wit, witlen, pushp);
} }
} }
} }

View File

@@ -340,7 +340,7 @@ static void report(struct bitcoin_tx *tx,
type_to_string(tmpctx, struct bitcoin_signature, &localsig)); type_to_string(tmpctx, struct bitcoin_signature, &localsig));
witness = witness =
bitcoin_witness_2of2(tx->input, &localsig, &remotesig, bitcoin_witness_2of2(tx, &localsig, &remotesig,
local_funding_pubkey, remote_funding_pubkey); local_funding_pubkey, remote_funding_pubkey);
bitcoin_tx_input_set_witness(tx, 0, witness); bitcoin_tx_input_set_witness(tx, 0, witness);
txhex = tal_hex(tmpctx, linearize_tx(tx, tx)); txhex = tal_hex(tmpctx, linearize_tx(tx, tx));
@@ -846,8 +846,8 @@ int main(void)
"to_local_msat: %"PRIu64"\n" "to_local_msat: %"PRIu64"\n"
"to_remote_msat: %"PRIu64"\n" "to_remote_msat: %"PRIu64"\n"
"local_feerate_per_kw: %u\n", "local_feerate_per_kw: %u\n",
tal_count(tx->output), tx->wtx->num_outputs,
tal_count(tx->output) > 1 ? "s" : "", tx->wtx->num_outputs > 1 ? "s" : "",
to_local.millisatoshis, to_remote.millisatoshis, feerate_per_kw-1); to_local.millisatoshis, to_remote.millisatoshis, feerate_per_kw-1);
/* Recalc with verbosity on */ /* Recalc with verbosity on */
print_superverbose = true; print_superverbose = true;
@@ -882,8 +882,8 @@ int main(void)
"to_local_msat: %"PRIu64"\n" "to_local_msat: %"PRIu64"\n"
"to_remote_msat: %"PRIu64"\n" "to_remote_msat: %"PRIu64"\n"
"local_feerate_per_kw: %u\n", "local_feerate_per_kw: %u\n",
tal_count(newtx->output), newtx->wtx->num_outputs,
tal_count(newtx->output) > 1 ? "s" : "", newtx->wtx->num_outputs > 1 ? "s" : "",
to_local.millisatoshis, to_remote.millisatoshis, feerate_per_kw); to_local.millisatoshis, to_remote.millisatoshis, feerate_per_kw);
/* Recalc with verbosity on */ /* Recalc with verbosity on */
print_superverbose = true; print_superverbose = true;
@@ -913,11 +913,11 @@ int main(void)
feerate_per_kw, feerate_per_kw,
htlc_map); htlc_map);
assert(tal_count(newtx->output) != tal_count(tx->output)); assert(newtx->wtx->num_outputs != tx->wtx->num_outputs);
tal_free(tx); tal_free(tx);
tx = newtx; tx = newtx;
} while (tal_count(tx->output) > 1); } while (tx->wtx->num_outputs > 1);
/* Now make sure we cover case where funder can't afford the fee; /* Now make sure we cover case where funder can't afford the fee;
* its output cannot go negative! */ * its output cannot go negative! */

View File

@@ -104,7 +104,7 @@ void htlc_success_tx_add_witness(struct bitcoin_tx *htlc_success,
localhtlckey, remotehtlckey, localhtlckey, remotehtlckey,
&hash, revocationkey); &hash, revocationkey);
witness = bitcoin_witness_htlc_success_tx(htlc_success->input, witness = bitcoin_witness_htlc_success_tx(htlc_success,
localhtlcsig, remotehtlcsig, localhtlcsig, remotehtlcsig,
payment_preimage, wscript); payment_preimage, wscript);
bitcoin_tx_input_set_witness(htlc_success, 0, witness); bitcoin_tx_input_set_witness(htlc_success, 0, witness);
@@ -145,8 +145,8 @@ void htlc_timeout_tx_add_witness(struct bitcoin_tx *htlc_timeout,
localhtlckey, remotehtlckey, localhtlckey, remotehtlckey,
payment_hash, revocationkey); payment_hash, revocationkey);
witness = bitcoin_witness_htlc_timeout_tx( witness = bitcoin_witness_htlc_timeout_tx(htlc_timeout, localhtlcsig,
htlc_timeout->input, localhtlcsig, remotehtlcsig, wscript); remotehtlcsig, wscript);
bitcoin_tx_input_set_witness(htlc_timeout, 0, witness); bitcoin_tx_input_set_witness(htlc_timeout, 0, witness);
tal_free(wscript); tal_free(wscript);
} }

View File

@@ -197,7 +197,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
n++; n++;
} }
assert(n <= tal_count(tx->output)); assert(n <= tx->wtx->num_outputs);
tal_resize(&tx->output, n); tal_resize(&tx->output, n);
/* BOLT #3: /* BOLT #3:

View File

@@ -100,6 +100,7 @@ int main(void)
u8 *subscript, *script; u8 *subscript, *script;
struct bitcoin_signature sig; struct bitcoin_signature sig;
struct bitcoin_address addr; struct bitcoin_address addr;
struct amount_sat tmpamt;
secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY
| SECP256K1_CONTEXT_SIGN); | SECP256K1_CONTEXT_SIGN);
@@ -170,9 +171,11 @@ int main(void)
&inputkey, NULL); &inputkey, NULL);
printf("# fee: %s\n", printf("# fee: %s\n",
type_to_string(tmpctx, struct amount_sat, &fee)); type_to_string(tmpctx, struct amount_sat, &fee));
tmpamt = bitcoin_tx_output_get_amount(funding, !funding_outnum);
printf("change: %s\n", printf("change: %s\n",
type_to_string(tmpctx, struct amount_sat, type_to_string(tmpctx, struct amount_sat,
&funding->output[!funding_outnum].amount)); &tmpamt));
printf("funding output: %u\n", funding_outnum); printf("funding output: %u\n", funding_outnum);

View File

@@ -69,11 +69,11 @@ static void filter_block_txs(struct chain_topology *topo, struct block *b)
size_t j; size_t j;
/* Tell them if it spends a txo we care about. */ /* Tell them if it spends a txo we care about. */
for (j = 0; j < tal_count(tx->input); j++) { for (j = 0; j < tx->wtx->num_inputs; j++) {
struct txwatch_output out; struct txwatch_output out;
struct txowatch *txo; struct txowatch *txo;
out.txid = tx->input[j].txid; bitcoin_tx_input_get_txid(tx, j, &out.txid);
out.index = tx->input[j].index; out.index = tx->wtx->inputs[j].index;
txo = txowatch_hash_get(&topo->txowatches, &out); txo = txowatch_hash_get(&topo->txowatches, &out);
if (txo) { if (txo) {
@@ -571,10 +571,13 @@ static void topo_update_spends(struct chain_topology *topo, struct block *b)
const struct short_channel_id *scid; const struct short_channel_id *scid;
for (size_t i = 0; i < tal_count(b->full_txs); i++) { for (size_t i = 0; i < tal_count(b->full_txs); i++) {
const struct bitcoin_tx *tx = b->full_txs[i]; const struct bitcoin_tx *tx = b->full_txs[i];
for (size_t j = 0; j < tal_count(tx->input); j++) { for (size_t j = 0; j < tx->wtx->num_inputs; j++) {
const struct bitcoin_tx_input *input = &tx->input[j]; const struct wally_tx_input *input = &tx->wtx->inputs[j];
struct bitcoin_txid txid;
bitcoin_tx_input_get_txid(tx, j, &txid);
scid = wallet_outpoint_spend(topo->ld->wallet, tmpctx, scid = wallet_outpoint_spend(topo->ld->wallet, tmpctx,
b->height, &input->txid, b->height, &txid,
input->index); input->index);
if (scid) { if (scid) {
gossipd_notify_spend(topo->bitcoind->ld, scid); gossipd_notify_spend(topo->bitcoind->ld, scid);
@@ -588,12 +591,14 @@ static void topo_add_utxos(struct chain_topology *topo, struct block *b)
{ {
for (size_t i = 0; i < tal_count(b->full_txs); i++) { for (size_t i = 0; i < tal_count(b->full_txs); i++) {
const struct bitcoin_tx *tx = b->full_txs[i]; const struct bitcoin_tx *tx = b->full_txs[i];
for (size_t j = 0; j < tal_count(tx->output); j++) { for (size_t j = 0; j < tx->wtx->num_outputs; j++) {
const struct bitcoin_tx_output *output = &tx->output[j]; const u8 *script = bitcoin_tx_output_get_script(tmpctx, tx, j);
if (is_p2wsh(output->script, NULL)) { struct amount_sat amt = bitcoin_tx_output_get_amount(tx, j);
if (is_p2wsh(script, NULL)) {
wallet_utxoset_add(topo->ld->wallet, tx, j, wallet_utxoset_add(topo->ld->wallet, tx, j,
b->height, i, output->script, b->height, i, script,
output->amount); amt);
} }
} }
} }

View File

@@ -20,9 +20,10 @@
static struct amount_sat calc_tx_fee(struct amount_sat sat_in, static struct amount_sat calc_tx_fee(struct amount_sat sat_in,
const struct bitcoin_tx *tx) const struct bitcoin_tx *tx)
{ {
struct amount_sat fee = sat_in; struct amount_sat amt, fee = sat_in;
for (size_t i = 0; i < tal_count(tx->output); i++) { for (size_t i = 0; i < tx->wtx->num_outputs; i++) {
if (!amount_sat_sub(&fee, fee, tx->output[i].amount)) amt = bitcoin_tx_output_get_amount(tx, i);
if (!amount_sat_sub(&fee, fee, amt))
fatal("Tx spends more than input %s? %s", fatal("Tx spends more than input %s? %s",
type_to_string(tmpctx, struct amount_sat, &sat_in), type_to_string(tmpctx, struct amount_sat, &sat_in),
type_to_string(tmpctx, struct bitcoin_tx, tx)); type_to_string(tmpctx, struct bitcoin_tx, tx));

View File

@@ -157,7 +157,7 @@ static void watch_tx_and_outputs(struct channel *channel,
txw = watch_tx(channel->owner, ld->topology, channel, tx, txw = watch_tx(channel->owner, ld->topology, channel, tx,
onchain_tx_watched); onchain_tx_watched);
for (size_t i = 0; i < tal_count(tx->output); i++) for (size_t i = 0; i < tx->wtx->num_outputs; i++)
watch_txo(txw, ld->topology, channel, &txid, i, watch_txo(txw, ld->topology, channel, &txid, i,
onchain_txo_watched); onchain_txo_watched);
} }
@@ -450,9 +450,10 @@ enum watch_result onchaind_funding_spent(struct channel *channel,
if (!feerate) { if (!feerate) {
/* We have at least one data point: the last tx's feerate. */ /* We have at least one data point: the last tx's feerate. */
struct amount_sat fee = channel->funding; struct amount_sat fee = channel->funding;
for (size_t i = 0; i < tal_count(channel->last_tx->output); i++) for (size_t i = 0; i < channel->last_tx->wtx->num_outputs; i++)
if (!amount_sat_sub(&fee, fee, if (!amount_sat_sub(&fee, fee,
channel->last_tx->output[i].amount)) { bitcoin_tx_output_get_amount(
channel->last_tx, i))) {
log_broken(channel->log, "Could not get fee" log_broken(channel->log, "Could not get fee"
" funding %s tx %s", " funding %s tx %s",
type_to_string(tmpctx, type_to_string(tmpctx,

View File

@@ -343,17 +343,19 @@ static void opening_funder_finished(struct subd *openingd, const u8 *resp,
ld->wallet->bip32_base); ld->wallet->bip32_base);
log_debug(fc->uc->log, "Funding tx has %zi inputs, %zu outputs:", log_debug(fc->uc->log, "Funding tx has %zi inputs, %zu outputs:",
tal_count(fundingtx->input), fundingtx->wtx->num_inputs,
tal_count(fundingtx->output)); fundingtx->wtx->num_outputs);
for (size_t i = 0; i < tal_count(fundingtx->input); i++) { for (size_t i = 0; i < fundingtx->wtx->num_inputs; i++) {
struct bitcoin_txid tmptxid;
bitcoin_tx_input_get_txid(fundingtx, i, &tmptxid);
log_debug(fc->uc->log, "%zi: %s (%s) %s\n", log_debug(fc->uc->log, "%zi: %s (%s) %s\n",
i, i,
type_to_string(tmpctx, struct amount_sat, type_to_string(tmpctx, struct amount_sat,
&fc->wtx.utxos[i]->amount), &fc->wtx.utxos[i]->amount),
fc->wtx.utxos[i]->is_p2sh ? "P2SH" : "SEGWIT", fc->wtx.utxos[i]->is_p2sh ? "P2SH" : "SEGWIT",
type_to_string(tmpctx, struct bitcoin_txid, type_to_string(tmpctx, struct bitcoin_txid,
&fundingtx->input[i].txid)); &tmptxid));
} }
bitcoin_txid(fundingtx, &funding_txid); bitcoin_txid(fundingtx, &funding_txid);
@@ -433,8 +435,8 @@ static void opening_funder_finished(struct subd *openingd, const u8 *resp,
/* Make sure we recognize our change output by its scriptpubkey in /* Make sure we recognize our change output by its scriptpubkey in
* future. This assumes that we have only two outputs, may not be true * future. This assumes that we have only two outputs, may not be true
* if we add support for multifundchannel */ * if we add support for multifundchannel */
if (tal_count(fundingtx->output) == 2) if (fundingtx->wtx->num_outputs == 2)
txfilter_add_scriptpubkey(ld->owned_txfilter, fundingtx->output[!funding_outnum].script); txfilter_add_scriptpubkey(ld->owned_txfilter, bitcoin_tx_output_get_script(tmpctx, fundingtx, !funding_outnum));
/* We need these to compose cmd's response in funding_broadcast_success */ /* We need these to compose cmd's response in funding_broadcast_success */
fc->hextx = tal_hex(fc, linearize_tx(fc->cmd, fundingtx)); fc->hextx = tal_hex(fc, linearize_tx(fc->cmd, fundingtx));

View File

@@ -200,7 +200,7 @@ static void sign_last_tx(struct channel *channel)
struct bitcoin_signature sig; struct bitcoin_signature sig;
u8 *msg, **witness; u8 *msg, **witness;
assert(!channel->last_tx->input[0].witness); assert(!channel->last_tx->wtx->inputs[0].witness);
msg = towire_hsm_sign_commitment_tx(tmpctx, msg = towire_hsm_sign_commitment_tx(tmpctx,
&channel->peer->id, &channel->peer->id,
@@ -219,7 +219,7 @@ static void sign_last_tx(struct channel *channel)
tal_hex(tmpctx, msg)); tal_hex(tmpctx, msg));
witness = witness =
bitcoin_witness_2of2(channel->last_tx->input, &channel->last_sig, bitcoin_witness_2of2(channel->last_tx, &channel->last_sig,
&sig, &channel->channel_info.remote_fundingkey, &sig, &channel->channel_info.remote_fundingkey,
&channel->local_funding_pubkey); &channel->local_funding_pubkey);
@@ -1532,7 +1532,7 @@ static struct command_result *json_sign_last_tx(struct command *cmd,
response = json_stream_success(cmd); response = json_stream_success(cmd);
log_debug(channel->log, "dev-sign-last-tx: signing tx with %zu outputs", log_debug(channel->log, "dev-sign-last-tx: signing tx with %zu outputs",
tal_count(channel->last_tx->output)); channel->last_tx->wtx->num_outputs);
sign_last_tx(channel); sign_last_tx(channel);
linear = linearize_tx(cmd, channel->last_tx); linear = linearize_tx(cmd, channel->last_tx);
remove_sig(channel->last_tx); remove_sig(channel->last_tx);

View File

@@ -73,8 +73,8 @@ void txfilter_add_derkey(struct txfilter *filter,
bool txfilter_match(const struct txfilter *filter, const struct bitcoin_tx *tx) bool txfilter_match(const struct txfilter *filter, const struct bitcoin_tx *tx)
{ {
for (size_t i = 0; i < tal_count(tx->output); i++) { for (size_t i = 0; i < tx->wtx->num_outputs; i++) {
u8 *oscript = tx->output[i].script; const u8 *oscript = bitcoin_tx_output_get_script(tmpctx, tx, i);
for (size_t j = 0; j < tal_count(filter->scriptpubkeys); j++) { for (size_t j = 0; j < tal_count(filter->scriptpubkeys); j++) {
if (scripteq(oscript, filter->scriptpubkeys[j])) if (scripteq(oscript, filter->scriptpubkeys[j]))

View File

@@ -1163,19 +1163,21 @@ int wallet_extract_owned_outputs(struct wallet *w, const struct bitcoin_tx *tx,
int num_utxos = 0; int num_utxos = 0;
*total = AMOUNT_SAT(0); *total = AMOUNT_SAT(0);
for (size_t output = 0; output < tal_count(tx->output); output++) { for (size_t output = 0; output < tx->wtx->num_outputs; output++) {
struct utxo *utxo; struct utxo *utxo;
u32 index; u32 index;
bool is_p2sh; bool is_p2sh;
const u8 *script = bitcoin_tx_output_get_script(tmpctx, tx, output);
if (!wallet_can_spend(w, tx->output[output].script, &index,
if (!wallet_can_spend(w, script, &index,
&is_p2sh)) &is_p2sh))
continue; continue;
utxo = tal(w, struct utxo); utxo = tal(w, struct utxo);
utxo->keyindex = index; utxo->keyindex = index;
utxo->is_p2sh = is_p2sh; utxo->is_p2sh = is_p2sh;
utxo->amount = tx->output[output].amount; utxo->amount = bitcoin_tx_output_get_amount(tx, output);
utxo->status = output_state_available; utxo->status = output_state_available;
bitcoin_txid(tx, &utxo->txid); bitcoin_txid(tx, &utxo->txid);
utxo->outnum = output; utxo->outnum = output;
@@ -1183,12 +1185,12 @@ int wallet_extract_owned_outputs(struct wallet *w, const struct bitcoin_tx *tx,
utxo->blockheight = blockheight ? blockheight : NULL; utxo->blockheight = blockheight ? blockheight : NULL;
utxo->spendheight = NULL; utxo->spendheight = NULL;
utxo->scriptPubkey = tx->output[output].script; utxo->scriptPubkey = tal_dup_arr(utxo, u8, script, tal_bytelen(script), 0);
log_debug(w->log, "Owning output %zu %s (%s) txid %s%s", log_debug(w->log, "Owning output %zu %s (%s) txid %s%s",
output, output,
type_to_string(tmpctx, struct amount_sat, type_to_string(tmpctx, struct amount_sat,
&tx->output[output].amount), &utxo->amount),
is_p2sh ? "P2SH" : "SEGWIT", is_p2sh ? "P2SH" : "SEGWIT",
type_to_string(tmpctx, struct bitcoin_txid, type_to_string(tmpctx, struct bitcoin_txid,
&utxo->txid), blockheight ? " CONFIRMED" : ""); &utxo->txid), blockheight ? " CONFIRMED" : "");
@@ -1208,7 +1210,7 @@ int wallet_extract_owned_outputs(struct wallet *w, const struct bitcoin_tx *tx,
if (!amount_sat_add(total, *total, utxo->amount)) if (!amount_sat_add(total, *total, utxo->amount))
fatal("Cannot add utxo output %zu/%zu %s + %s", fatal("Cannot add utxo output %zu/%zu %s + %s",
output, tal_count(tx->output), output, tx->wtx->num_outputs,
type_to_string(tmpctx, struct amount_sat, total), type_to_string(tmpctx, struct amount_sat, total),
type_to_string(tmpctx, struct amount_sat, type_to_string(tmpctx, struct amount_sat,
&utxo->amount)); &utxo->amount));