Browse Source

protocol: restore locktime in blocks option.

This could be used by an always-on node to agressively shorten their timeouts.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 10 years ago
parent
commit
6389dbd455
  1. 6
      commit_tx.c
  2. 40
      lightning.pb-c.c
  3. 17
      lightning.pb-c.h
  4. 5
      lightning.proto
  5. 1
      pkt.c
  6. 15
      protobuf_convert.c
  7. 2
      protobuf_convert.h
  8. 5
      test-cli/create-commit-spend-tx.c
  9. 5
      test-cli/create-steal-tx.c

6
commit_tx.c

@ -18,6 +18,7 @@ struct bitcoin_tx *create_commit_tx(const tal_t *ctx,
struct bitcoin_tx *tx; struct bitcoin_tx *tx;
const u8 *redeemscript; const u8 *redeemscript;
struct pubkey ourkey, theirkey, to_me; struct pubkey ourkey, theirkey, to_me;
u32 locktime;
/* Now create commitment tx: one input, two outputs. */ /* Now create commitment tx: one input, two outputs. */
tx = bitcoin_tx(ctx, 1, 2); tx = bitcoin_tx(ctx, 1, 2);
@ -32,9 +33,12 @@ struct bitcoin_tx *create_commit_tx(const tal_t *ctx,
if (!proto_to_pubkey(theirs->final, &theirkey)) if (!proto_to_pubkey(theirs->final, &theirkey))
return tal_free(tx); return tal_free(tx);
if (!proto_to_locktime(ours, &locktime))
return tal_free(tx);
/* First output is a P2SH to a complex redeem script (usu. for me) */ /* First output is a P2SH to a complex redeem script (usu. for me) */
redeemscript = bitcoin_redeem_revocable(tx, &ourkey, redeemscript = bitcoin_redeem_revocable(tx, &ourkey,
ours->locktime_seconds, locktime,
&theirkey, &theirkey,
rhash); rhash);
tx->output[0].script = scriptpubkey_p2sh(tx, redeemscript); tx->output[0].script = scriptpubkey_p2sh(tx, redeemscript);

40
lightning.pb-c.c

@ -1517,7 +1517,7 @@ const ProtobufCMessageDescriptor anchor__descriptor =
(ProtobufCMessageInit) anchor__init, (ProtobufCMessageInit) anchor__init,
NULL,NULL,NULL /* reserved[123] */ NULL,NULL,NULL /* reserved[123] */
}; };
static const ProtobufCFieldDescriptor open_channel__field_descriptors[7] = static const ProtobufCFieldDescriptor open_channel__field_descriptors[8] =
{ {
{ {
"seed", "seed",
@ -1534,13 +1534,25 @@ static const ProtobufCFieldDescriptor open_channel__field_descriptors[7] =
{ {
"locktime_seconds", "locktime_seconds",
2, 2,
PROTOBUF_C_LABEL_REQUIRED, PROTOBUF_C_LABEL_OPTIONAL,
PROTOBUF_C_TYPE_UINT32, PROTOBUF_C_TYPE_UINT32,
0, /* quantifier_offset */ offsetof(OpenChannel, locktime_case),
offsetof(OpenChannel, locktime_seconds), offsetof(OpenChannel, locktime_seconds),
NULL, NULL,
NULL, NULL,
0, /* flags */ 0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */
0,NULL,NULL /* reserved1,reserved2, etc */
},
{
"locktime_blocks",
3,
PROTOBUF_C_LABEL_OPTIONAL,
PROTOBUF_C_TYPE_UINT32,
offsetof(OpenChannel, locktime_case),
offsetof(OpenChannel, locktime_blocks),
NULL,
NULL,
0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */
0,NULL,NULL /* reserved1,reserved2, etc */ 0,NULL,NULL /* reserved1,reserved2, etc */
}, },
{ {
@ -1605,19 +1617,19 @@ static const ProtobufCFieldDescriptor open_channel__field_descriptors[7] =
}, },
}; };
static const unsigned open_channel__field_indices_by_name[] = { static const unsigned open_channel__field_indices_by_name[] = {
5, /* field[5] = anchor */ 6, /* field[6] = anchor */
4, /* field[4] = commitment_fee */ 5, /* field[5] = commitment_fee */
3, /* field[3] = final */ 4, /* field[4] = final */
2, /* field[2] = locktime_blocks */
1, /* field[1] = locktime_seconds */ 1, /* field[1] = locktime_seconds */
2, /* field[2] = revocation_hash */ 3, /* field[3] = revocation_hash */
0, /* field[0] = seed */ 0, /* field[0] = seed */
6, /* field[6] = tx_version */ 7, /* field[7] = tx_version */
}; };
static const ProtobufCIntRange open_channel__number_ranges[2 + 1] = static const ProtobufCIntRange open_channel__number_ranges[1 + 1] =
{ {
{ 1, 0 }, { 1, 0 },
{ 4, 2 }, { 0, 8 }
{ 0, 7 }
}; };
const ProtobufCMessageDescriptor open_channel__descriptor = const ProtobufCMessageDescriptor open_channel__descriptor =
{ {
@ -1627,10 +1639,10 @@ const ProtobufCMessageDescriptor open_channel__descriptor =
"OpenChannel", "OpenChannel",
"", "",
sizeof(OpenChannel), sizeof(OpenChannel),
7, 8,
open_channel__field_descriptors, open_channel__field_descriptors,
open_channel__field_indices_by_name, open_channel__field_indices_by_name,
2, open_channel__number_ranges, 1, open_channel__number_ranges,
(ProtobufCMessageInit) open_channel__init, (ProtobufCMessageInit) open_channel__init,
NULL,NULL,NULL /* reserved[123] */ NULL,NULL,NULL /* reserved[123] */
}; };

17
lightning.pb-c.h

@ -174,6 +174,12 @@ struct _Anchor
, 0,NULL, NULL, NULL, 0, 0, 0 } , 0,NULL, NULL, NULL, 0, 0, 0 }
typedef enum {
OPEN_CHANNEL__LOCKTIME__NOT_SET = 0,
OPEN_CHANNEL__LOCKTIME_LOCKTIME_SECONDS = 2,
OPEN_CHANNEL__LOCKTIME_LOCKTIME_BLOCKS = 3,
} OpenChannel__LocktimeCase;
/* /*
* Set channel params. * Set channel params.
*/ */
@ -184,10 +190,6 @@ struct _OpenChannel
* Seed which sets order we create outputs for all transactions. * Seed which sets order we create outputs for all transactions.
*/ */
uint64_t seed; uint64_t seed;
/*
* Relative locktime for outputs going to us.
*/
uint32_t locktime_seconds;
/* /*
* Hash seed for revoking commitment transactions. * Hash seed for revoking commitment transactions.
*/ */
@ -208,10 +210,15 @@ struct _OpenChannel
* Maximum transaction version we support. * Maximum transaction version we support.
*/ */
uint32_t tx_version; uint32_t tx_version;
OpenChannel__LocktimeCase locktime_case;
union {
uint32_t locktime_seconds;
uint32_t locktime_blocks;
};
}; };
#define OPEN_CHANNEL__INIT \ #define OPEN_CHANNEL__INIT \
{ PROTOBUF_C_MESSAGE_INIT (&open_channel__descriptor) \ { PROTOBUF_C_MESSAGE_INIT (&open_channel__descriptor) \
, 0, 0, NULL, NULL, 0, NULL, 0 } , 0, NULL, NULL, 0, NULL, 0, OPEN_CHANNEL__LOCKTIME__NOT_SET, {} }
/* /*

5
lightning.proto

@ -73,7 +73,10 @@ message open_channel {
// Seed which sets order we create outputs for all transactions. // Seed which sets order we create outputs for all transactions.
required uint64 seed = 1; required uint64 seed = 1;
// Relative locktime for outputs going to us. // Relative locktime for outputs going to us.
required uint32 locktime_seconds = 2; oneof locktime {
uint32 locktime_seconds = 2;
uint32 locktime_blocks = 3;
}
// Hash seed for revoking commitment transactions. // Hash seed for revoking commitment transactions.
required sha256_hash revocation_hash = 4; required sha256_hash revocation_hash = 4;
// How to pay money to us from commit_tx. // How to pay money to us from commit_tx.

1
pkt.c

@ -45,6 +45,7 @@ struct pkt *openchannel_pkt(const tal_t *ctx,
o.final = pubkey_to_proto(ctx, to_me); o.final = pubkey_to_proto(ctx, to_me);
o.commitment_fee = commitment_fee; o.commitment_fee = commitment_fee;
o.anchor = anchor; o.anchor = anchor;
o.locktime_case = OPEN_CHANNEL__LOCKTIME_LOCKTIME_SECONDS;
o.locktime_seconds = rel_locktime_seconds; o.locktime_seconds = rel_locktime_seconds;
o.tx_version = BITCOIN_TX_VERSION; o.tx_version = BITCOIN_TX_VERSION;

15
protobuf_convert.c

@ -81,3 +81,18 @@ void proto_to_sha256(const Sha256Hash *pb, struct sha256 *hash)
memcpy(hash->u.u8 + 16, &pb->c, 8); memcpy(hash->u.u8 + 16, &pb->c, 8);
memcpy(hash->u.u8 + 24, &pb->d, 8); memcpy(hash->u.u8 + 24, &pb->d, 8);
} }
bool proto_to_locktime(const OpenChannel *o, uint32_t *locktime)
{
switch (o->locktime_case) {
case OPEN_CHANNEL__LOCKTIME_LOCKTIME_SECONDS:
*locktime = 500000000 + o->locktime_seconds;
break;
case OPEN_CHANNEL__LOCKTIME_LOCKTIME_BLOCKS:
*locktime = o->locktime_blocks;
break;
default:
return false;
}
return true;
}

2
protobuf_convert.h

@ -18,4 +18,6 @@ bool proto_to_pubkey(const BitcoinPubkey *pb, struct pubkey *key);
struct sha256; struct sha256;
Sha256Hash *sha256_to_proto(const tal_t *ctx, const struct sha256 *hash); Sha256Hash *sha256_to_proto(const tal_t *ctx, const struct sha256 *hash);
void proto_to_sha256(const Sha256Hash *pb, struct sha256 *hash); void proto_to_sha256(const Sha256Hash *pb, struct sha256 *hash);
bool proto_to_locktime(const OpenChannel *o, uint32_t *locktime);
#endif /* LIGHTNING_PROTOBUF_CONVERT_H */ #endif /* LIGHTNING_PROTOBUF_CONVERT_H */

5
test-cli/create-commit-spend-tx.c

@ -39,6 +39,7 @@ int main(int argc, char *argv[])
struct sha256 rhash; struct sha256 rhash;
size_t p2sh_out; size_t p2sh_out;
u64 fee = 10000; u64 fee = 10000;
u32 locktime_seconds;
err_set_progname(argv[0]); err_set_progname(argv[0]);
@ -61,6 +62,8 @@ int main(int argc, char *argv[])
o1 = pkt_from_file(argv[2], PKT__PKT_OPEN)->open; o1 = pkt_from_file(argv[2], PKT__PKT_OPEN)->open;
o2 = pkt_from_file(argv[3], PKT__PKT_OPEN)->open; o2 = pkt_from_file(argv[3], PKT__PKT_OPEN)->open;
if (!proto_to_locktime(o2, &locktime_seconds))
errx(1, "Invalid locktime in o2");
/* We need our private key to spend commit output. */ /* We need our private key to spend commit output. */
privkey = key_from_base58(argv[4], strlen(argv[4]), &testnet, &pubkey1); privkey = key_from_base58(argv[4], strlen(argv[4]), &testnet, &pubkey1);
@ -86,7 +89,7 @@ int main(int argc, char *argv[])
/* Create redeem script */ /* Create redeem script */
redeemscript = bitcoin_redeem_revocable(ctx, &pubkey1, redeemscript = bitcoin_redeem_revocable(ctx, &pubkey1,
o2->locktime_seconds, locktime_seconds,
&pubkey2, &rhash); &pubkey2, &rhash);
/* Now, create transaction to spend it. */ /* Now, create transaction to spend it. */

5
test-cli/create-steal-tx.c

@ -36,6 +36,7 @@ int main(int argc, char *argv[])
char *tx_hex; char *tx_hex;
EC_KEY *privkey; EC_KEY *privkey;
bool testnet; bool testnet;
u32 locktime_seconds;
err_set_progname(argv[0]); err_set_progname(argv[0]);
@ -73,6 +74,8 @@ int main(int argc, char *argv[])
o1 = pkt_from_file(argv[4], PKT__PKT_OPEN)->open; o1 = pkt_from_file(argv[4], PKT__PKT_OPEN)->open;
o2 = pkt_from_file(argv[5], PKT__PKT_OPEN)->open; o2 = pkt_from_file(argv[5], PKT__PKT_OPEN)->open;
if (!proto_to_locktime(o2, &locktime_seconds))
errx(1, "Invalid locktime in o2");
if (!pubkey_from_hexstr(argv[6], &outpubkey)) if (!pubkey_from_hexstr(argv[6], &outpubkey))
errx(1, "Invalid bitcoin pubkey '%s'", argv[6]); errx(1, "Invalid bitcoin pubkey '%s'", argv[6]);
@ -89,7 +92,7 @@ int main(int argc, char *argv[])
/* Now, which commit output? Match redeem script. */ /* Now, which commit output? Match redeem script. */
sha256(&revoke_hash, &revoke_preimage, sizeof(revoke_preimage)); sha256(&revoke_hash, &revoke_preimage, sizeof(revoke_preimage));
redeemscript = bitcoin_redeem_revocable(ctx, &pubkey2, redeemscript = bitcoin_redeem_revocable(ctx, &pubkey2,
o2->locktime_seconds, locktime_seconds,
&pubkey1, &revoke_hash); &pubkey1, &revoke_hash);
p2sh = scriptpubkey_p2sh(ctx, redeemscript); p2sh = scriptpubkey_p2sh(ctx, redeemscript);

Loading…
Cancel
Save