mirror of
https://github.com/aljazceru/lightning.git
synced 2026-02-04 05:34:22 +01:00
protocol: add commitment fee logic.
Both sides elect a commitment fee, and the lowest is chosen. That means you can't game the other side (but if you offer too low, then can error out of course). Fees are split 50-50 if possible: originally the whole fee has to be paid by the (single) funder. Neither side can withdraw funds which would make them unable to pay fees. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
@@ -68,7 +68,8 @@ 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, &our_amount, &their_amount))
|
||||
if (!initial_funding(o1, o2, a, commit_fee(o1, o2),
|
||||
&our_amount, &their_amount))
|
||||
errx(1, "Invalid open combination (need 1 anchor offer)");
|
||||
|
||||
/* Now create our commitment tx. */
|
||||
|
||||
@@ -57,7 +57,7 @@ int main(int argc, char *argv[])
|
||||
if (!testnet)
|
||||
errx(1, "Private key '%s' not on testnet!", argv[4]);
|
||||
|
||||
gather_updates(o1, o2, a, argv + 5, &our_amount, &their_amount,
|
||||
gather_updates(o1, o2, a, 0, argv + 5, &our_amount, &their_amount,
|
||||
NULL, NULL, NULL);
|
||||
|
||||
/* Get pubkeys */
|
||||
|
||||
@@ -56,7 +56,7 @@ int main(int argc, char *argv[])
|
||||
errx(1, "Invalid o2 commit_key");
|
||||
|
||||
/* Get delta by accumulting all the updates. */
|
||||
gather_updates(o1, o2, a, argv + 6, &our_amount, &their_amount,
|
||||
gather_updates(o1, o2, a, 0, argv + 6, &our_amount, &their_amount,
|
||||
NULL, NULL, NULL);
|
||||
|
||||
/* This is what the anchor pays to; figure out which output. */
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "find_p2sh_out.h"
|
||||
#include "protobuf_convert.h"
|
||||
#include "test-cli/gather_updates.h"
|
||||
#include "funding.h"
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
@@ -81,7 +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, argv + 7, &our_amount, &their_amount,
|
||||
gather_updates(o1, o2, a, commit_fee(o1, o2), argv + 7,
|
||||
&our_amount, &their_amount,
|
||||
&rhash, NULL, NULL);
|
||||
|
||||
/* Create redeem script */
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include "find_p2sh_out.h"
|
||||
#include "protobuf_convert.h"
|
||||
#include "gather_updates.h"
|
||||
#include "funding.h"
|
||||
#include <unistd.h>
|
||||
|
||||
/* FIXME: this code doesn't work if we're not the ones proposing the delta */
|
||||
@@ -65,7 +66,8 @@ int main(int argc, char *argv[])
|
||||
|
||||
sig2.stype = SIGHASH_ALL;
|
||||
|
||||
gather_updates(o1, o2, a, argv + 5, &our_amount, &their_amount,
|
||||
gather_updates(o1, o2, a, commit_fee(o1, o2), argv + 5,
|
||||
&our_amount, &their_amount,
|
||||
&rhash, NULL, &sig2.sig);
|
||||
|
||||
redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2);
|
||||
|
||||
@@ -33,7 +33,7 @@ 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,
|
||||
const OpenAnchor *oa,
|
||||
const OpenAnchor *oa, uint64_t fee,
|
||||
char **argv,
|
||||
uint64_t *our_amount, uint64_t *their_amount,
|
||||
struct sha256 *our_rhash,
|
||||
@@ -46,7 +46,7 @@ uint64_t gather_updates(const OpenChannel *o1, const OpenChannel *o2,
|
||||
struct sha256 old_our_rhash, old_their_rhash;
|
||||
|
||||
/* Start sanity check. */
|
||||
if (!initial_funding(o1, o2, oa, our_amount, their_amount))
|
||||
if (!initial_funding(o1, o2, oa, fee, our_amount, their_amount))
|
||||
errx(1, "Invalid open combination (need 1 anchor offer)");
|
||||
|
||||
if (our_rhash)
|
||||
@@ -88,7 +88,7 @@ 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, &cdelta, delta,
|
||||
if (!funding_delta(o1, o2, oa, fee, &cdelta, delta,
|
||||
our_amount, their_amount))
|
||||
errx(1, "Impossible funding update %lli %s",
|
||||
(long long)delta, *argv);
|
||||
|
||||
@@ -6,7 +6,7 @@ struct signature;
|
||||
struct sha256;
|
||||
|
||||
uint64_t gather_updates(const OpenChannel *o1, const OpenChannel *o2,
|
||||
const OpenAnchor *oa,
|
||||
const OpenAnchor *oa, uint64_t fee,
|
||||
char **argv,
|
||||
uint64_t *our_amount, uint64_t *their_amount,
|
||||
struct sha256 *our_rhash,
|
||||
|
||||
@@ -29,6 +29,7 @@ int main(int argc, char *argv[])
|
||||
struct pkt *pkt;
|
||||
const tal_t *ctx = tal_arr(NULL, char, 0);
|
||||
unsigned int locktime_seconds, min_confirms;
|
||||
u64 commit_tx_fee;
|
||||
bool offer_anchor = false;
|
||||
struct pubkey commitkey, finalkey;
|
||||
|
||||
@@ -38,6 +39,8 @@ int main(int argc, char *argv[])
|
||||
locktime_seconds = LOCKTIME_MIN + 24 * 60 * 60;
|
||||
/* Zero, unless they set --offer-anchor or --min-anchor-confirms */
|
||||
min_confirms = 0;
|
||||
/* We only need this for involuntary close, so make it larger. */
|
||||
commit_tx_fee = 100000;
|
||||
|
||||
opt_register_noarg("--help|-h", opt_usage_and_exit,
|
||||
"<seed> <commitpubkey> <finalpubkey>\n"
|
||||
@@ -52,6 +55,9 @@ int main(int argc, char *argv[])
|
||||
opt_register_noarg("--offer-anchor",
|
||||
opt_set_bool, &offer_anchor,
|
||||
"Offer to create anchor transaction");
|
||||
opt_register_arg("--commitment-fee=<bits>",
|
||||
opt_set_bits, opt_show_bits, &commit_tx_fee,
|
||||
"100's of satoshi to pay for commitment");
|
||||
|
||||
opt_parse(&argc, argv, opt_log_stderr_exit);
|
||||
|
||||
@@ -76,7 +82,8 @@ int main(int argc, char *argv[])
|
||||
revocation_hash.u.u8, sizeof(revocation_hash.u.u8));
|
||||
|
||||
pkt = open_channel_pkt(ctx, &revocation_hash, &commitkey, &finalkey,
|
||||
locktime_seconds, offer_anchor, min_confirms);
|
||||
locktime_seconds, offer_anchor, min_confirms,
|
||||
commit_tx_fee);
|
||||
|
||||
if (!write_all(STDOUT_FILENO, pkt, pkt_totlen(pkt)))
|
||||
err(1, "Writing out packet");
|
||||
|
||||
@@ -55,7 +55,7 @@ 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, &to_us, &to_them))
|
||||
if (!initial_funding(o1, o2, a, commit_fee(o1, o2), &to_us, &to_them))
|
||||
errx(1, "Invalid open combination (need 1 anchor offer)");
|
||||
|
||||
proto_to_sha256(o2->revocation_hash, &rhash);
|
||||
|
||||
@@ -114,8 +114,8 @@ B_UPDATE_PKTS="-- +B-commit-sig.pb"
|
||||
$PREFIX ./create-commit-tx A-open.pb B-open.pb A-anchor.pb $A_TMPKEY $A_UPDATE_PKTS > A-commit-0.tx
|
||||
$PREFIX ./create-commit-tx B-open.pb A-open.pb A-anchor.pb $B_TMPKEY $B_UPDATE_PKTS > B-commit-0.tx
|
||||
|
||||
# Now, update the channel, so I pay you 500 satoshi.
|
||||
$PREFIX ./update-channel --to-them=500 $A_SEED 1 > A-update-1.pb
|
||||
# Now, update the channel, so I pay you 60000 satoshi (covers 50000 fee)
|
||||
$PREFIX ./update-channel --to-them=60000 $A_SEED 1 > A-update-1.pb
|
||||
A_UPDATE_PKTS="$A_UPDATE_PKTS +A-update-1.pb"
|
||||
B_UPDATE_PKTS="$B_UPDATE_PKTS -A-update-1.pb"
|
||||
|
||||
|
||||
@@ -17,6 +17,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[])
|
||||
@@ -60,7 +61,7 @@ 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, argv + 6,
|
||||
num_updates = gather_updates(o1, o2, a, commit_fee(o1, o2), argv + 6,
|
||||
&our_amount, &their_amount,
|
||||
NULL, &their_rhash, NULL);
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ int main(int argc, char *argv[])
|
||||
sig.stype = SIGHASH_ALL;
|
||||
|
||||
/* This also checks that preimage is correct! */
|
||||
num_updates = gather_updates(o1, o2, a, argv + 5,
|
||||
num_updates = gather_updates(o1, o2, a, commit_fee(o1, o2), argv + 5,
|
||||
&our_amount, &their_amount,
|
||||
&our_rhash, &their_rhash, &sig.sig);
|
||||
if (num_updates < 1)
|
||||
|
||||
@@ -17,6 +17,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[])
|
||||
@@ -62,7 +63,7 @@ int main(int argc, char *argv[])
|
||||
sig.stype = SIGHASH_ALL;
|
||||
|
||||
/* Figure out cumulative delta since anchor. */
|
||||
num_updates = gather_updates(o1, o2, a, argv + 6,
|
||||
num_updates = gather_updates(o1, o2, a, commit_fee(o1, o2), argv + 6,
|
||||
&our_amount, &their_amount,
|
||||
&our_rhash, &their_rhash, &sig.sig);
|
||||
if (num_updates < 1)
|
||||
|
||||
Reference in New Issue
Block a user