funding: remove protobufs.

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>
This commit is contained in:
Rusty Russell
2016-01-22 06:41:47 +10:30
parent d95d8a99c2
commit 6d6abd57e7
13 changed files with 188 additions and 106 deletions

View File

@@ -15,6 +15,7 @@
#include "bitcoin/privkey.h"
#include "protobuf_convert.h"
#include "funding.h"
#include "gather_updates.h"
#include "version.h"
#include <unistd.h>
@@ -70,9 +71,14 @@ int main(int argc, char *argv[])
if (!proto_to_pubkey(o2->commit_key, &pubkey2))
errx(1, "Invalid o2 commit_key");
cstate = initial_funding(ctx, o1, o2, a, commit_fee(o1, o2));
if (is_funder(o1) == is_funder(o2))
errx(1, "Must be exactly one funder");
cstate = initial_funding(ctx, is_funder(o1), a->amount,
commit_fee(o1->commitment_fee,
o2->commitment_fee));
if (!cstate)
errx(1, "Invalid open combination (need 1 anchor offer)");
errx(1, "Invalid open combination (need to cover fees)");
/* Now create our commitment tx. */
proto_to_sha256(o1->revocation_hash, &rhash);

View File

@@ -85,7 +85,9 @@ int main(int argc, char *argv[])
errx(1, "Invalid o2 final pubkey");
/* We use this simply to get final revocation hash. */
gather_updates(ctx, o1, o2, a, commit_fee(o1, o2), argv + 7,
gather_updates(ctx, o1, o2, a,
commit_fee(o1->commitment_fee, o2->commitment_fee),
argv + 7,
NULL, &rhash, NULL, NULL);
/* Create redeem script */

View File

@@ -67,7 +67,10 @@ int main(int argc, char *argv[])
sig2.stype = SIGHASH_ALL;
cstate = gather_updates(ctx, o1, o2, a, commit_fee(o1, o2), argv + 5,
cstate = gather_updates(ctx, o1, o2, a,
commit_fee(o1->commitment_fee,
o2->commitment_fee),
argv + 5,
NULL, &rhash, NULL, &sig2.sig);
redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2);

View File

@@ -28,28 +28,32 @@ static size_t find_htlc(struct channel_oneside *oneside,
const Sha256Hash *rhash)
{
size_t i, n;
struct sha256 h;
proto_to_sha256(rhash, &h);
n = tal_count(oneside->htlcs);
for (i = 0; i < n; i++) {
if (oneside->htlcs[i]->r_hash->a == rhash->a
&& oneside->htlcs[i]->r_hash->b == rhash->b
&& oneside->htlcs[i]->r_hash->c == rhash->c
&& oneside->htlcs[i]->r_hash->d == rhash->d)
if (structeq(&oneside->htlcs[i].rhash, &h))
break;
}
return i;
}
static void add_htlc(struct channel_oneside *oneside, UpdateAddHtlc *ah,
static void add_htlc(struct channel_oneside *oneside,
const UpdateAddHtlc *ah,
const char *file)
{
size_t num = tal_count(oneside->htlcs);
struct sha256 rhash;
struct abs_locktime expiry;
if (find_htlc(oneside, ah->r_hash) != num)
errx(1, "Duplicate R hash in %s", file);
tal_resize(&oneside->htlcs, num+1);
oneside->htlcs[num] = ah;
proto_to_sha256(ah->r_hash, &rhash);
proto_to_abs_locktime(ah->expiry, &expiry);
funding_add_htlc(oneside, ah->amount_msat, &expiry, &rhash);
}
static void remove_htlc(struct channel_oneside *oneside, size_t n)
@@ -85,13 +89,13 @@ static void update_rhash(const Sha256Hash *rhash,
(*num_updates)++;
}
static uint32_t htlcs_total(UpdateAddHtlc *const *htlcs)
static uint32_t htlcs_total(const struct channel_htlc *htlcs)
{
size_t i, n = tal_count(htlcs);
uint32_t total = 0;
for (i = 0; i < n; i++)
total += htlcs[i]->amount_msat;
total += htlcs[i].msatoshis;
return total;
}
@@ -110,9 +114,12 @@ struct channel_state *gather_updates(const tal_t *ctx,
struct channel_state *cstate;
/* Start sanity check. */
cstate = initial_funding(NULL, o1, o2, oa, fee);
if (is_funder(o1) == is_funder(o2))
errx(1, "Must be exactly one funder");
cstate = initial_funding(NULL, is_funder(o1), oa->amount, fee);
if (!cstate)
errx(1, "Invalid open combination (need 1 anchor offer)");
errx(1, "Invalid open combination (need to cover fees)");
/* If they don't want these, use dummy ones. */
if (!our_rhash)
@@ -156,14 +163,16 @@ struct channel_state *gather_updates(const tal_t *ctx,
case PKT__PKT_UPDATE_ADD_HTLC:
amount = pkt->update_add_htlc->amount_msat;
if (received) {
if (!funding_delta(o2, o1, oa, 0, amount,
if (!funding_delta(is_funder(o2), oa->amount,
0, amount,
&cstate->b, &cstate->a))
errx(1, "Impossible htlc %llu %s",
(long long)amount, *argv);
add_htlc(&cstate->b, pkt->update_add_htlc,
*argv);
} else {
if (!funding_delta(o1, o2, oa, 0, amount,
if (!funding_delta(is_funder(o1), oa->amount,
0, amount,
&cstate->a, &cstate->b))
errx(1, "Impossible htlc %llu %s",
(long long)amount, *argv);
@@ -183,8 +192,9 @@ struct channel_state *gather_updates(const tal_t *ctx,
pkt->update_timedout_htlc->r_hash);
if (n == tal_count(cstate->b.htlcs))
errx(1, "Unknown R hash in %s", *argv);
amount = cstate->b.htlcs[n]->amount_msat;
if (!funding_delta(o2, o1, oa, 0, -amount,
amount = cstate->b.htlcs[n].msatoshis;
if (!funding_delta(is_funder(o2), oa->amount,
0, -amount,
&cstate->b, &cstate->a))
errx(1, "Impossible htlc %llu %s",
(long long)amount, *argv);
@@ -194,8 +204,9 @@ struct channel_state *gather_updates(const tal_t *ctx,
pkt->update_timedout_htlc->r_hash);
if (n == tal_count(cstate->a.htlcs))
errx(1, "Unknown R hash in %s", *argv);
amount = cstate->a.htlcs[n]->amount_msat;
if (!funding_delta(o1, o2, oa, 0, -amount,
amount = cstate->a.htlcs[n].msatoshis;
if (!funding_delta(is_funder(o1), oa->amount,
0, -amount,
&cstate->a, &cstate->b))
errx(1, "Impossible htlc %llu %s",
(long long)amount, *argv);
@@ -214,8 +225,9 @@ struct channel_state *gather_updates(const tal_t *ctx,
pkt->update_routefail_htlc->r_hash);
if (n == tal_count(cstate->a.htlcs))
errx(1, "Unknown R hash in %s", *argv);
amount = cstate->a.htlcs[n]->amount_msat;
if (!funding_delta(o1, o2, oa, 0, -amount,
amount = cstate->a.htlcs[n].msatoshis;
if (!funding_delta(is_funder(o1), oa->amount,
0, -amount,
&cstate->a, &cstate->b))
errx(1, "Impossible htlc %llu %s",
(long long)amount, *argv);
@@ -225,8 +237,9 @@ struct channel_state *gather_updates(const tal_t *ctx,
pkt->update_routefail_htlc->r_hash);
if (n == tal_count(cstate->b.htlcs))
errx(1, "Unknown R hash in %s", *argv);
amount = cstate->b.htlcs[n]->amount_msat;
if (!funding_delta(o2, o1, oa, 0, -amount,
amount = cstate->b.htlcs[n].msatoshis;
if (!funding_delta(is_funder(o2), oa->amount,
0, -amount,
&cstate->b, &cstate->a))
errx(1, "Impossible htlc %llu %s",
(long long)amount, *argv);
@@ -253,8 +266,9 @@ struct channel_state *gather_updates(const tal_t *ctx,
n = find_htlc(&cstate->a, rh);
if (n == tal_count(cstate->a.htlcs))
errx(1, "Unknown R hash in %s", *argv);
amount = cstate->a.htlcs[n]->amount_msat;
if (!funding_delta(o1, o2, oa, -amount, -amount,
amount = cstate->a.htlcs[n].msatoshis;
if (!funding_delta(is_funder(o1), oa->amount,
-amount, -amount,
&cstate->a, &cstate->b))
errx(1, "Impossible htlc %llu %s",
(long long)amount, *argv);
@@ -266,8 +280,9 @@ struct channel_state *gather_updates(const tal_t *ctx,
n = find_htlc(&cstate->b, rh);
if (n == tal_count(cstate->b.htlcs))
errx(1, "Unknown R hash in %s", *argv);
amount = cstate->b.htlcs[n]->amount_msat;
if (!funding_delta(o2, o1, oa, -amount, -amount,
amount = cstate->b.htlcs[n].msatoshis;
if (!funding_delta(is_funder(o2), oa->amount,
-amount, -amount,
&cstate->b, &cstate->a))
errx(1, "Impossible htlc %llu %s",
(long long)amount, *argv);
@@ -285,7 +300,7 @@ struct channel_state *gather_updates(const tal_t *ctx,
delta = -pkt->update->delta_msat;
else
delta = pkt->update->delta_msat;
if (!funding_delta(o1, o2, oa, delta, 0,
if (!funding_delta(is_funder(o1), oa->amount, delta, 0,
&cstate->a, &cstate->b))
errx(1, "Impossible funding update %lli %s",
(long long)delta, *argv);

View File

@@ -15,4 +15,13 @@ struct channel_state *gather_updates(const tal_t *ctx,
struct sha256 *our_rhash,
struct sha256 *their_rhash,
struct signature *their_commit_sig);
/**
* is_funder: helper to tell you if this is offering to fund channel.
* @o: the openchannel packet.
*/
static inline bool is_funder(const OpenChannel *o)
{
return o->anch == OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR;
}
#endif /* GATHER_UPDATES_H */

View File

@@ -9,6 +9,7 @@
#include "bitcoin/base58.h"
#include "pkt.h"
#include "funding.h"
#include "gather_updates.h"
#include "bitcoin/script.h"
#include "bitcoin/address.h"
#include "bitcoin/tx.h"
@@ -73,9 +74,14 @@ int main(int argc, char *argv[])
oa.amount = anchor->output[oa.output_index].amount;
/* Figure out initial how much to us, how much to them. */
cstate = initial_funding(ctx, o1, o2, &oa, commit_fee(o1, o2));
if (is_funder(o1) == is_funder(o2))
errx(1, "Must be exactly one funder");
cstate = initial_funding(ctx, is_funder(o1), oa.amount,
commit_fee(o1->commitment_fee,
o2->commitment_fee));
if (!cstate)
errx(1, "Invalid open combination (need 1 anchor offer)");
errx(1, "Invalid open combination (need to cover fees)");
/* Now, create signature for their commitment tx. */
proto_to_sha256(o2->revocation_hash, &rhash);

View File

@@ -16,6 +16,7 @@
#include "bitcoin/privkey.h"
#include "protobuf_convert.h"
#include "funding.h"
#include "gather_updates.h"
#include "version.h"
#include <unistd.h>
@@ -56,13 +57,17 @@ int main(int argc, char *argv[])
if (!testnet)
errx(1, "Private key '%s' not on testnet!", argv[4]);
if (is_funder(o1) == is_funder(o2))
errx(1, "Must be exactly one funder");
/* Now create THEIR commitment tx to spend 2/2 output of anchor. */
cstate = initial_funding(ctx, o1, o2, a, commit_fee(o1, o2));
cstate = initial_funding(ctx, is_funder(o2), a->amount,
commit_fee(o2->commitment_fee,
o1->commitment_fee));
if (!cstate)
errx(1, "Invalid open combination (need 1 anchor offer)");
errx(1, "Invalid open combination (too low for fees)");
proto_to_sha256(o2->revocation_hash, &rhash);
invert_cstate(cstate);
commit = commit_tx_from_pkts(ctx, o2, o1, a, &rhash, cstate);
/* If contributions don't exceed fees, this fails. */

View File

@@ -63,7 +63,10 @@ int main(int argc, char *argv[])
errx(1, "Private key '%s' not on testnet!", argv[5]);
/* Figure out cumulative delta since anchor. */
cstate = gather_updates(ctx, o1, o2, a, commit_fee(o1, o2), argv + 6,
cstate = gather_updates(ctx, o1, o2, a,
commit_fee(o1->commitment_fee,
o2->commitment_fee),
argv + 6,
&num_updates, NULL, &their_rhash, NULL);
/* Get next revocation hash. */

View File

@@ -58,7 +58,10 @@ int main(int argc, char *argv[])
sig.stype = SIGHASH_ALL;
/* This also checks that preimage is correct! */
cstate = gather_updates(ctx, o1, o2, a, commit_fee(o1, o2), argv + 5,
cstate = gather_updates(ctx, o1, o2, a,
commit_fee(o1->commitment_fee,
o2->commitment_fee),
argv + 5,
&num_updates,
&our_rhash, &their_rhash, &sig.sig);
if (num_updates < 1)

View File

@@ -65,7 +65,10 @@ int main(int argc, char *argv[])
sig.stype = SIGHASH_ALL;
/* Figure out cumulative delta since anchor. */
cstate = gather_updates(ctx, o1, o2, a, commit_fee(o1, o2), argv + 6,
cstate = gather_updates(ctx, o1, o2, a,
commit_fee(o1->commitment_fee,
o2->commitment_fee),
argv + 6,
&num_updates,
&our_rhash, &their_rhash, &sig.sig);
if (num_updates < 1)