mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-17 19:03:42 +01:00
protocol: add close fee logic.
The closer proposes the fee. This is simple, at least. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
3260fb2ed1
commit
de051f0df9
@ -1399,7 +1399,7 @@ const ProtobufCMessageDescriptor update_complete__descriptor =
|
||||
(ProtobufCMessageInit) update_complete__init,
|
||||
NULL,NULL,NULL /* reserved[123] */
|
||||
};
|
||||
static const ProtobufCFieldDescriptor close_channel__field_descriptors[1] =
|
||||
static const ProtobufCFieldDescriptor close_channel__field_descriptors[2] =
|
||||
{
|
||||
{
|
||||
"sig",
|
||||
@ -1413,14 +1413,27 @@ static const ProtobufCFieldDescriptor close_channel__field_descriptors[1] =
|
||||
0, /* flags */
|
||||
0,NULL,NULL /* reserved1,reserved2, etc */
|
||||
},
|
||||
{
|
||||
"close_fee",
|
||||
2,
|
||||
PROTOBUF_C_LABEL_REQUIRED,
|
||||
PROTOBUF_C_TYPE_UINT64,
|
||||
0, /* quantifier_offset */
|
||||
offsetof(CloseChannel, close_fee),
|
||||
NULL,
|
||||
NULL,
|
||||
0, /* flags */
|
||||
0,NULL,NULL /* reserved1,reserved2, etc */
|
||||
},
|
||||
};
|
||||
static const unsigned close_channel__field_indices_by_name[] = {
|
||||
1, /* field[1] = close_fee */
|
||||
0, /* field[0] = sig */
|
||||
};
|
||||
static const ProtobufCIntRange close_channel__number_ranges[1 + 1] =
|
||||
{
|
||||
{ 1, 0 },
|
||||
{ 0, 1 }
|
||||
{ 0, 2 }
|
||||
};
|
||||
const ProtobufCMessageDescriptor close_channel__descriptor =
|
||||
{
|
||||
@ -1430,7 +1443,7 @@ const ProtobufCMessageDescriptor close_channel__descriptor =
|
||||
"CloseChannel",
|
||||
"",
|
||||
sizeof(CloseChannel),
|
||||
1,
|
||||
2,
|
||||
close_channel__field_descriptors,
|
||||
close_channel__field_indices_by_name,
|
||||
1, close_channel__number_ranges,
|
||||
|
@ -293,10 +293,14 @@ struct _CloseChannel
|
||||
* as per the last commit tx.
|
||||
*/
|
||||
Signature *sig;
|
||||
/*
|
||||
* Fee to pay for close transaction.
|
||||
*/
|
||||
uint64_t close_fee;
|
||||
};
|
||||
#define CLOSE_CHANNEL__INIT \
|
||||
{ PROTOBUF_C_MESSAGE_INIT (&close_channel__descriptor) \
|
||||
, NULL }
|
||||
, NULL, 0 }
|
||||
|
||||
|
||||
/*
|
||||
|
@ -125,6 +125,8 @@ message close_channel {
|
||||
// output to my open->final and your open->final,
|
||||
// as per the last commit tx.
|
||||
required signature sig = 1;
|
||||
// Fee to pay for close transaction.
|
||||
required uint64 close_fee = 2;
|
||||
}
|
||||
|
||||
// OK, here's my sig so you can broadcast it too. We're done.
|
||||
|
5
pkt.c
5
pkt.c
@ -110,9 +110,12 @@ struct pkt *open_commit_sig_pkt(const tal_t *ctx, const struct signature *sig)
|
||||
return to_pkt(ctx, PKT__PKT_OPEN_COMMIT_SIG, &o);
|
||||
}
|
||||
|
||||
struct pkt *close_channel_pkt(const tal_t *ctx, const struct signature *sig)
|
||||
struct pkt *close_channel_pkt(const tal_t *ctx,
|
||||
uint64_t fee,
|
||||
const struct signature *sig)
|
||||
{
|
||||
CloseChannel c = CLOSE_CHANNEL__INIT;
|
||||
c.close_fee = fee;
|
||||
c.sig = signature_to_proto(ctx, sig);
|
||||
return to_pkt(ctx, PKT__PKT_CLOSE, &c);
|
||||
}
|
||||
|
5
pkt.h
5
pkt.h
@ -65,9 +65,12 @@ struct pkt *open_commit_sig_pkt(const tal_t *ctx, const struct signature *sig);
|
||||
/**
|
||||
* close_channel_pkt - create an close_channel message
|
||||
* @ctx: tal context to allocate off.
|
||||
* @fee: the fee for the transaction.
|
||||
* @sig: the signature for the close transaction input.
|
||||
*/
|
||||
struct pkt *close_channel_pkt(const tal_t *ctx, const struct signature *sig);
|
||||
struct pkt *close_channel_pkt(const tal_t *ctx,
|
||||
uint64_t fee,
|
||||
const struct signature *sig);
|
||||
|
||||
/**
|
||||
* close_channel_complete_pkt - create an close_channel_complete message
|
||||
|
@ -18,6 +18,7 @@
|
||||
#include "find_p2sh_out.h"
|
||||
#include "protobuf_convert.h"
|
||||
#include "gather_updates.h"
|
||||
#include "opt_bits.h"
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
@ -29,19 +30,25 @@ int main(int argc, char *argv[])
|
||||
struct pkt *pkt;
|
||||
struct signature sig;
|
||||
struct privkey privkey;
|
||||
bool testnet, complete = false;
|
||||
bool testnet;
|
||||
struct pubkey pubkey1, pubkey2;
|
||||
u8 *redeemscript;
|
||||
uint64_t our_amount, their_amount;
|
||||
char *close_file = NULL;
|
||||
u64 close_fee = 10000;
|
||||
|
||||
err_set_progname(argv[0]);
|
||||
|
||||
opt_register_noarg("--complete", opt_set_bool, &complete,
|
||||
"Create a close_transaction_complete msg instead");
|
||||
opt_register_arg("--complete=<close-msg-file>",
|
||||
opt_set_charp, NULL, &close_file,
|
||||
"Create a close_transaction_complete msg instead");
|
||||
opt_register_noarg("--help|-h", opt_usage_and_exit,
|
||||
"<open-channel-file1> <open-channel-file2> <anchor-file> <commit-privkey> [{+/-}update-protobuf]...\n"
|
||||
"Create the signature needed for the close transaction",
|
||||
"Print this message.");
|
||||
opt_register_arg("--close-fee=<bits>",
|
||||
opt_set_bits, opt_show_bits, &close_fee,
|
||||
"100's of satoshi to pay for close tx");
|
||||
|
||||
opt_parse(&argc, argv, opt_log_stderr_exit);
|
||||
|
||||
@ -57,7 +64,14 @@ int main(int argc, char *argv[])
|
||||
if (!testnet)
|
||||
errx(1, "Private key '%s' not on testnet!", argv[4]);
|
||||
|
||||
gather_updates(o1, o2, a, 0, argv + 5, &our_amount, &their_amount,
|
||||
if (close_file) {
|
||||
CloseChannel *c;
|
||||
c = pkt_from_file(close_file, PKT__PKT_CLOSE)->close;
|
||||
close_fee = c->close_fee;
|
||||
}
|
||||
|
||||
gather_updates(o1, o2, a, close_fee, argv + 5,
|
||||
&our_amount, &their_amount,
|
||||
NULL, NULL, NULL);
|
||||
|
||||
/* Get pubkeys */
|
||||
@ -72,17 +86,16 @@ int main(int argc, char *argv[])
|
||||
/* This is what the anchor pays to. */
|
||||
redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2);
|
||||
|
||||
/* FIXME: Add fee! */
|
||||
close_tx = create_close_tx(ctx, o1, o2, a, our_amount, their_amount);
|
||||
|
||||
/* Sign it for them. */
|
||||
sign_tx_input(ctx, close_tx, 0, redeemscript, tal_count(redeemscript),
|
||||
&privkey, &pubkey1, &sig);
|
||||
|
||||
if (complete)
|
||||
if (close_file)
|
||||
pkt = close_channel_complete_pkt(ctx, &sig);
|
||||
else
|
||||
pkt = close_channel_pkt(ctx, &sig);
|
||||
pkt = close_channel_pkt(ctx, close_fee, &sig);
|
||||
if (!write_all(STDOUT_FILENO, pkt, pkt_totlen(pkt)))
|
||||
err(1, "Writing out packet");
|
||||
|
||||
|
@ -56,7 +56,8 @@ int main(int argc, char *argv[])
|
||||
errx(1, "Invalid o2 commit_key");
|
||||
|
||||
/* Get delta by accumulting all the updates. */
|
||||
gather_updates(o1, o2, a, 0, argv + 6, &our_amount, &their_amount,
|
||||
gather_updates(o1, o2, a, close->close_fee, argv + 6,
|
||||
&our_amount, &their_amount,
|
||||
NULL, NULL, NULL);
|
||||
|
||||
/* This is what the anchor pays to; figure out which output. */
|
||||
|
@ -176,7 +176,7 @@ fi
|
||||
|
||||
# Now close channel by mutual consent.
|
||||
$PREFIX ./close-channel A-open.pb B-open.pb A-anchor.pb $A_TMPKEY $A_UPDATE_PKTS > A-close.pb
|
||||
$PREFIX ./close-channel --complete B-open.pb A-open.pb A-anchor.pb $B_TMPKEY $B_UPDATE_PKTS > B-close-complete.pb
|
||||
$PREFIX ./close-channel --complete=A-close.pb B-open.pb A-open.pb A-anchor.pb $B_TMPKEY $B_UPDATE_PKTS > B-close-complete.pb
|
||||
$PREFIX ./create-close-tx A-open.pb B-open.pb A-anchor.pb A-close.pb B-close-complete.pb $A_UPDATE_PKTS > A-close.tx
|
||||
|
||||
$CLI sendrawtransaction `cut -d: -f1 A-close.tx` > close.txid
|
||||
|
Loading…
Reference in New Issue
Block a user