mirror of
https://github.com/aljazceru/lightning.git
synced 2026-01-13 11:04:29 +01:00
channel_state: encapsulate funding of channel in one place.
This shows where funds are going at any time (fees vs to each side). funding.c is mainly rewritten, and should be clearer now. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
@@ -29,7 +29,7 @@ int main(int argc, char *argv[])
|
||||
struct privkey privkey;
|
||||
bool testnet;
|
||||
struct sha256 rhash;
|
||||
u64 our_amount, their_amount;
|
||||
struct channel_state *cstate;
|
||||
|
||||
err_set_progname(argv[0]);
|
||||
|
||||
@@ -68,14 +68,13 @@ int main(int argc, char *argv[])
|
||||
if (!proto_to_pubkey(o2->commit_key, &pubkey2))
|
||||
errx(1, "Invalid o2 commit_key");
|
||||
|
||||
if (!initial_funding(o1, o2, a, commit_fee(o1, o2),
|
||||
&our_amount, &their_amount))
|
||||
cstate = initial_funding(ctx, o1, o2, a, commit_fee(o1, o2));
|
||||
if (!cstate)
|
||||
errx(1, "Invalid open combination (need 1 anchor offer)");
|
||||
|
||||
/* Now create our commitment tx. */
|
||||
proto_to_sha256(o1->revocation_hash, &rhash);
|
||||
commit = create_commit_tx(ctx, o1, o2, a, &rhash,
|
||||
our_amount, their_amount);
|
||||
commit = create_commit_tx(ctx, o1, o2, a, &rhash, cstate);
|
||||
|
||||
/* Check signature. */
|
||||
subscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2);
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "bitcoin/script.h"
|
||||
#include "permute_tx.h"
|
||||
#include "funding.h"
|
||||
#include "commit_tx.h"
|
||||
#include "bitcoin/signature.h"
|
||||
#include "bitcoin/pubkey.h"
|
||||
#include "bitcoin/privkey.h"
|
||||
@@ -33,9 +34,9 @@ int main(int argc, char *argv[])
|
||||
bool testnet;
|
||||
struct pubkey pubkey1, pubkey2;
|
||||
u8 *redeemscript;
|
||||
uint64_t our_amount, their_amount;
|
||||
char *close_file = NULL;
|
||||
u64 close_fee = 10000;
|
||||
struct channel_state *cstate;
|
||||
|
||||
err_set_progname(argv[0]);
|
||||
|
||||
@@ -70,8 +71,7 @@ int main(int argc, char *argv[])
|
||||
close_fee = c->close_fee;
|
||||
}
|
||||
|
||||
gather_updates(o1, o2, a, close_fee, argv + 5,
|
||||
&our_amount, &their_amount,
|
||||
cstate = gather_updates(ctx, o1, o2, a, close_fee, argv + 5, NULL,
|
||||
NULL, NULL, NULL);
|
||||
|
||||
/* Get pubkeys */
|
||||
@@ -86,7 +86,7 @@ int main(int argc, char *argv[])
|
||||
/* This is what the anchor pays to. */
|
||||
redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2);
|
||||
|
||||
close_tx = create_close_tx(ctx, o1, o2, a, our_amount, their_amount);
|
||||
close_tx = create_close_tx(ctx, o1, o2, a, cstate->a.pay, cstate->b.pay);
|
||||
|
||||
/* Sign it for them. */
|
||||
sign_tx_input(ctx, close_tx, 0, redeemscript, tal_count(redeemscript),
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "find_p2sh_out.h"
|
||||
#include "protobuf_convert.h"
|
||||
#include "gather_updates.h"
|
||||
#include "funding.h"
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
@@ -28,7 +29,7 @@ int main(int argc, char *argv[])
|
||||
u8 *redeemscript;
|
||||
CloseChannel *close;
|
||||
CloseChannelComplete *closecomplete;
|
||||
uint64_t our_amount, their_amount;
|
||||
struct channel_state *cstate;
|
||||
|
||||
err_set_progname(argv[0]);
|
||||
|
||||
@@ -56,15 +57,14 @@ int main(int argc, char *argv[])
|
||||
errx(1, "Invalid o2 commit_key");
|
||||
|
||||
/* Get delta by accumulting all the updates. */
|
||||
gather_updates(o1, o2, a, close->close_fee, argv + 6,
|
||||
&our_amount, &their_amount,
|
||||
NULL, NULL, NULL);
|
||||
cstate = gather_updates(ctx, o1, o2, a, close->close_fee, argv + 6,
|
||||
NULL, NULL, NULL, NULL);
|
||||
|
||||
/* This is what the anchor pays to; figure out which output. */
|
||||
redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2);
|
||||
|
||||
/* Now create the close tx to spend 2/2 output of anchor. */
|
||||
close_tx = create_close_tx(ctx, o1, o2, a, our_amount, their_amount);
|
||||
close_tx = create_close_tx(ctx, o1, o2, a, cstate->a.pay, cstate->b.pay);
|
||||
|
||||
/* Signatures well-formed? */
|
||||
sig1.stype = sig2.stype = SIGHASH_ALL;
|
||||
|
||||
@@ -35,7 +35,7 @@ int main(int argc, char *argv[])
|
||||
u8 *redeemscript;
|
||||
struct sha256 rhash;
|
||||
size_t p2sh_out;
|
||||
u64 fee = 10000, our_amount, their_amount;
|
||||
u64 fee = 10000;
|
||||
u32 locktime;
|
||||
|
||||
err_set_progname(argv[0]);
|
||||
@@ -82,9 +82,8 @@ int main(int argc, char *argv[])
|
||||
errx(1, "Invalid o2 final pubkey");
|
||||
|
||||
/* We use this simply to get final revocation hash. */
|
||||
gather_updates(o1, o2, a, commit_fee(o1, o2), argv + 7,
|
||||
&our_amount, &their_amount,
|
||||
&rhash, NULL, NULL);
|
||||
gather_updates(ctx, o1, o2, a, commit_fee(o1, o2), argv + 7,
|
||||
NULL, &rhash, NULL, NULL);
|
||||
|
||||
/* Create redeem script */
|
||||
redeemscript = bitcoin_redeem_secret_or_delay(ctx, &pubkey1, locktime,
|
||||
|
||||
@@ -31,8 +31,8 @@ int main(int argc, char *argv[])
|
||||
struct bitcoin_signature sig1, sig2;
|
||||
struct pubkey pubkey1, pubkey2;
|
||||
u8 *redeemscript;
|
||||
uint64_t our_amount, their_amount;
|
||||
struct sha256 rhash;
|
||||
struct channel_state *cstate;
|
||||
|
||||
err_set_progname(argv[0]);
|
||||
|
||||
@@ -66,15 +66,13 @@ int main(int argc, char *argv[])
|
||||
|
||||
sig2.stype = SIGHASH_ALL;
|
||||
|
||||
gather_updates(o1, o2, a, commit_fee(o1, o2), argv + 5,
|
||||
&our_amount, &their_amount,
|
||||
&rhash, NULL, &sig2.sig);
|
||||
cstate = gather_updates(ctx, o1, o2, a, commit_fee(o1, o2), argv + 5,
|
||||
NULL, &rhash, NULL, &sig2.sig);
|
||||
|
||||
redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2);
|
||||
|
||||
/* Now create commitment tx to spend 2/2 output of anchor. */
|
||||
commit = create_commit_tx(ctx, o1, o2, a, &rhash,
|
||||
our_amount, their_amount);
|
||||
commit = create_commit_tx(ctx, o1, o2, a, &rhash, cstate);
|
||||
|
||||
/* This only fails on malformed packets */
|
||||
if (!commit)
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <ccan/crypto/sha256/sha256.h>
|
||||
#include <ccan/structeq/structeq.h>
|
||||
#include "test-cli/gather_updates.h"
|
||||
#include "commit_tx.h"
|
||||
#include "funding.h"
|
||||
#include "pkt.h"
|
||||
#include "protobuf_convert.h"
|
||||
@@ -32,21 +33,22 @@ static void get_rhash(const Sha256Hash *rhash, struct sha256 *old,
|
||||
}
|
||||
|
||||
/* Takes complete update history, gets summary of last state. */
|
||||
uint64_t gather_updates(const OpenChannel *o1, const OpenChannel *o2,
|
||||
struct channel_state *gather_updates(const tal_t *ctx,
|
||||
const OpenChannel *o1, const OpenChannel *o2,
|
||||
const OpenAnchor *oa, uint64_t fee,
|
||||
char **argv,
|
||||
uint64_t *our_amount, uint64_t *their_amount,
|
||||
size_t *num_updates,
|
||||
struct sha256 *our_rhash,
|
||||
struct sha256 *their_rhash,
|
||||
struct signature *their_commit_sig)
|
||||
{
|
||||
uint64_t cdelta = 0;
|
||||
uint64_t num_updates = 0;
|
||||
Signature *sig = NULL;
|
||||
struct sha256 old_our_rhash, old_their_rhash;
|
||||
struct channel_state *cstate;
|
||||
|
||||
/* Start sanity check. */
|
||||
if (!initial_funding(o1, o2, oa, fee, our_amount, their_amount))
|
||||
cstate = initial_funding(NULL, o1, o2, oa, fee);
|
||||
if (!cstate)
|
||||
errx(1, "Invalid open combination (need 1 anchor offer)");
|
||||
|
||||
if (our_rhash)
|
||||
@@ -59,6 +61,8 @@ uint64_t gather_updates(const OpenChannel *o1, const OpenChannel *o2,
|
||||
if (o2->anch == OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR)
|
||||
sig = oa->commit_sig;
|
||||
|
||||
if (num_updates)
|
||||
*num_updates = 0;
|
||||
while (*argv) {
|
||||
int64_t delta;
|
||||
bool received;
|
||||
@@ -88,11 +92,12 @@ uint64_t gather_updates(const OpenChannel *o1, const OpenChannel *o2,
|
||||
get_rhash(pkt->update->revocation_hash,
|
||||
&old_our_rhash, our_rhash);
|
||||
}
|
||||
if (!funding_delta(o1, o2, oa, fee, &cdelta, delta,
|
||||
our_amount, their_amount))
|
||||
if (!funding_delta(o1, o2, oa, delta,
|
||||
&cstate->a, &cstate->b))
|
||||
errx(1, "Impossible funding update %lli %s",
|
||||
(long long)delta, *argv);
|
||||
num_updates++;
|
||||
if (num_updates)
|
||||
(*num_updates)++;
|
||||
break;
|
||||
}
|
||||
case PKT__PKT_UPDATE_ACCEPT:
|
||||
@@ -145,5 +150,5 @@ uint64_t gather_updates(const OpenChannel *o1, const OpenChannel *o2,
|
||||
errx(1, "Invalid signature");
|
||||
}
|
||||
|
||||
return num_updates;
|
||||
return cstate;
|
||||
}
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
#ifndef GATHER_UPDATES_H
|
||||
#define GATHER_UPDATES_H
|
||||
#include <ccan/tal/tal.h>
|
||||
#include "lightning.pb-c.h"
|
||||
|
||||
struct signature;
|
||||
struct sha256;
|
||||
struct channel_state;
|
||||
|
||||
uint64_t gather_updates(const OpenChannel *o1, const OpenChannel *o2,
|
||||
struct channel_state *gather_updates(const tal_t *ctx,
|
||||
const OpenChannel *o1, const OpenChannel *o2,
|
||||
const OpenAnchor *oa, uint64_t fee,
|
||||
char **argv,
|
||||
uint64_t *our_amount, uint64_t *their_amount,
|
||||
size_t *num_updates,
|
||||
struct sha256 *our_rhash,
|
||||
struct sha256 *their_rhash,
|
||||
struct signature *their_commit_sig);
|
||||
|
||||
#endif /* GATHER_UPDATES_H */
|
||||
|
||||
@@ -31,12 +31,12 @@ int main(int argc, char *argv[])
|
||||
struct sha256_double txid;
|
||||
struct sha256 rhash;
|
||||
struct pkt *pkt;
|
||||
uint64_t to_them, to_us;
|
||||
struct pubkey pubkey1, pubkey2;
|
||||
struct privkey privkey;
|
||||
struct signature sig;
|
||||
bool testnet;
|
||||
u8 *redeemscript;
|
||||
struct channel_state *cstate;
|
||||
|
||||
err_set_progname(argv[0]);
|
||||
|
||||
@@ -70,12 +70,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. */
|
||||
if (!initial_funding(o1, o2, &oa, commit_fee(o1, o2), &to_us, &to_them))
|
||||
cstate = initial_funding(ctx, o1, o2, &oa, commit_fee(o1, o2));
|
||||
if (!cstate)
|
||||
errx(1, "Invalid open combination (need 1 anchor offer)");
|
||||
|
||||
/* Now, create signature for their commitment tx. */
|
||||
proto_to_sha256(o2->revocation_hash, &rhash);
|
||||
commit = create_commit_tx(ctx, o2, o1, &oa, &rhash, to_them, to_us);
|
||||
invert_cstate(cstate);
|
||||
commit = create_commit_tx(ctx, o2, o1, &oa, &rhash, cstate);
|
||||
|
||||
sign_tx_input(ctx, commit, 0, redeemscript, tal_count(redeemscript),
|
||||
&privkey, &pubkey1, &sig);
|
||||
|
||||
@@ -31,7 +31,7 @@ int main(int argc, char *argv[])
|
||||
struct pubkey pubkey1, pubkey2;
|
||||
u8 *subscript;
|
||||
struct sha256 rhash;
|
||||
uint64_t to_them, to_us;
|
||||
struct channel_state *cstate;
|
||||
|
||||
err_set_progname(argv[0]);
|
||||
|
||||
@@ -55,11 +55,13 @@ int main(int argc, char *argv[])
|
||||
errx(1, "Private key '%s' not on testnet!", argv[4]);
|
||||
|
||||
/* Now create THEIR commitment tx to spend 2/2 output of anchor. */
|
||||
if (!initial_funding(o1, o2, a, commit_fee(o1, o2), &to_us, &to_them))
|
||||
cstate = initial_funding(ctx, o1, o2, a, commit_fee(o1, o2));
|
||||
if (!cstate)
|
||||
errx(1, "Invalid open combination (need 1 anchor offer)");
|
||||
|
||||
proto_to_sha256(o2->revocation_hash, &rhash);
|
||||
commit = create_commit_tx(ctx, o2, o1, a, &rhash, to_them, to_us);
|
||||
invert_cstate(cstate);
|
||||
commit = create_commit_tx(ctx, o2, o1, a, &rhash, cstate);
|
||||
|
||||
/* If contributions don't exceed fees, this fails. */
|
||||
if (!commit)
|
||||
|
||||
@@ -31,10 +31,10 @@ int main(int argc, char *argv[])
|
||||
struct bitcoin_signature sig;
|
||||
struct privkey privkey;
|
||||
bool testnet;
|
||||
uint64_t num_updates;
|
||||
size_t num_updates;
|
||||
struct pubkey pubkey1, pubkey2;
|
||||
u8 *redeemscript;
|
||||
uint64_t our_amount, their_amount;
|
||||
struct channel_state *cstate;
|
||||
|
||||
err_set_progname(argv[0]);
|
||||
|
||||
@@ -61,9 +61,8 @@ int main(int argc, char *argv[])
|
||||
errx(1, "Private key '%s' not on testnet!", argv[5]);
|
||||
|
||||
/* Figure out cumulative delta since anchor. */
|
||||
num_updates = gather_updates(o1, o2, a, commit_fee(o1, o2), argv + 6,
|
||||
&our_amount, &their_amount,
|
||||
NULL, &their_rhash, NULL);
|
||||
cstate = gather_updates(ctx, o1, o2, a, commit_fee(o1, o2), argv + 6,
|
||||
&num_updates, NULL, &their_rhash, NULL);
|
||||
|
||||
/* Get next revocation hash. */
|
||||
shachain_from_seed(&seed, num_updates, &revocation_hash);
|
||||
@@ -83,8 +82,8 @@ int main(int argc, char *argv[])
|
||||
redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2);
|
||||
|
||||
/* Now create THEIR new commitment tx to spend 2/2 output of anchor. */
|
||||
commit = create_commit_tx(ctx, o2, o1, a, &their_rhash,
|
||||
their_amount, our_amount);
|
||||
invert_cstate(cstate);
|
||||
commit = create_commit_tx(ctx, o2, o1, a, &their_rhash, cstate);
|
||||
|
||||
/* If contributions don't exceed fees, this fails. */
|
||||
if (!commit)
|
||||
|
||||
@@ -31,8 +31,8 @@ int main(int argc, char *argv[])
|
||||
struct pubkey pubkey1, pubkey2;
|
||||
size_t num_updates;
|
||||
struct bitcoin_signature sig;
|
||||
uint64_t our_amount, their_amount;
|
||||
u8 *redeemscript;
|
||||
struct channel_state *cstate;
|
||||
|
||||
err_set_progname(argv[0]);
|
||||
|
||||
@@ -56,9 +56,9 @@ int main(int argc, char *argv[])
|
||||
sig.stype = SIGHASH_ALL;
|
||||
|
||||
/* This also checks that preimage is correct! */
|
||||
num_updates = gather_updates(o1, o2, a, commit_fee(o1, o2), argv + 5,
|
||||
&our_amount, &their_amount,
|
||||
&our_rhash, &their_rhash, &sig.sig);
|
||||
cstate = gather_updates(ctx, o1, o2, a, commit_fee(o1, o2), argv + 5,
|
||||
&num_updates,
|
||||
&our_rhash, &their_rhash, &sig.sig);
|
||||
if (num_updates < 1)
|
||||
errx(1, "Expected at least one update!");
|
||||
|
||||
@@ -72,8 +72,7 @@ int main(int argc, char *argv[])
|
||||
redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2);
|
||||
|
||||
/* Check their signature signs our new commit tx correctly. */
|
||||
commit = create_commit_tx(ctx, o1, o2, a, &our_rhash,
|
||||
our_amount, their_amount);
|
||||
commit = create_commit_tx(ctx, o1, o2, a, &our_rhash, cstate);
|
||||
if (!commit)
|
||||
errx(1, "Delta too large");
|
||||
|
||||
|
||||
@@ -33,8 +33,8 @@ int main(int argc, char *argv[])
|
||||
bool testnet;
|
||||
struct pubkey pubkey1, pubkey2;
|
||||
u8 *redeemscript;
|
||||
uint64_t our_amount, their_amount;
|
||||
uint64_t num_updates;
|
||||
size_t num_updates;
|
||||
struct channel_state *cstate;
|
||||
|
||||
err_set_progname(argv[0]);
|
||||
|
||||
@@ -63,9 +63,9 @@ int main(int argc, char *argv[])
|
||||
sig.stype = SIGHASH_ALL;
|
||||
|
||||
/* Figure out cumulative delta since anchor. */
|
||||
num_updates = gather_updates(o1, o2, a, commit_fee(o1, o2), argv + 6,
|
||||
&our_amount, &their_amount,
|
||||
&our_rhash, &their_rhash, &sig.sig);
|
||||
cstate = gather_updates(ctx, o1, o2, a, commit_fee(o1, o2), argv + 6,
|
||||
&num_updates,
|
||||
&our_rhash, &their_rhash, &sig.sig);
|
||||
if (num_updates < 1)
|
||||
errx(1, "Expected at least one update!");
|
||||
|
||||
@@ -85,8 +85,7 @@ int main(int argc, char *argv[])
|
||||
redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2);
|
||||
|
||||
/* Check our new commit is signed correctly by them. */
|
||||
commit = create_commit_tx(ctx, o1, o2, a, &our_rhash,
|
||||
our_amount, their_amount);
|
||||
commit = create_commit_tx(ctx, o1, o2, a, &our_rhash, cstate);
|
||||
if (!commit)
|
||||
errx(1, "Invalid packets");
|
||||
|
||||
@@ -96,8 +95,8 @@ int main(int argc, char *argv[])
|
||||
errx(1, "Invalid signature.");
|
||||
|
||||
/* Now create THEIR new commitment tx to spend 2/2 output of anchor. */
|
||||
commit = create_commit_tx(ctx, o2, o1, a, &their_rhash,
|
||||
their_amount, our_amount);
|
||||
invert_cstate(cstate);
|
||||
commit = create_commit_tx(ctx, o2, o1, a, &their_rhash, cstate);
|
||||
if (!commit)
|
||||
errx(1, "Invalid packets");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user