locktime: nice abstractions for absolute and relative locktimes.

I got confused navigating these, especially since Alpha and Bitcoin
have diverged (BIP68 was proposed after Elements Alpha).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2016-01-22 06:38:08 +10:30
parent 4c42930940
commit 4b02c6f558
13 changed files with 185 additions and 89 deletions

View File

@@ -21,6 +21,7 @@
#include "test-cli/gather_updates.h"
#include "funding.h"
#include "version.h"
#include "bitcoin/locktime.h"
#include <unistd.h>
int main(int argc, char *argv[])
@@ -37,7 +38,7 @@ int main(int argc, char *argv[])
struct sha256 rhash;
size_t p2sh_out;
u64 fee = 10000;
u32 locktime;
struct rel_locktime locktime;
err_set_progname(argv[0]);
@@ -87,7 +88,7 @@ int main(int argc, char *argv[])
NULL, &rhash, NULL, NULL);
/* Create redeem script */
redeemscript = bitcoin_redeem_secret_or_delay(ctx, &pubkey1, locktime,
redeemscript = bitcoin_redeem_secret_or_delay(ctx, &pubkey1, &locktime,
&pubkey2, &rhash);
/* Now, create transaction to spend it. */
@@ -98,7 +99,7 @@ int main(int argc, char *argv[])
tx->input[0].input_amount = commit->output[p2sh_out].amount;
tx->fee = fee;
tx->input[0].sequence_number = bitcoin_nsequence(locktime);
tx->input[0].sequence_number = bitcoin_nsequence(&locktime);
if (commit->output[p2sh_out].amount <= fee)
errx(1, "Amount of %llu won't exceed fee",

View File

@@ -16,6 +16,7 @@
#include "bitcoin/privkey.h"
#include "protobuf_convert.h"
#include "find_p2sh_out.h"
#include "bitcoin/locktime.h"
#include "version.h"
#include <unistd.h>
@@ -31,7 +32,8 @@ int main(int argc, char *argv[])
struct bitcoin_signature sig;
struct privkey privkey;
bool testnet;
u32 locktime, htlc_abstimeout;
struct rel_locktime locktime;
struct abs_locktime htlc_abstimeout;
char *rvalue = NULL, *preimage = NULL;
bool received, own_commit_tx;
Pkt *pkt;
@@ -114,13 +116,13 @@ int main(int argc, char *argv[])
if (received) {
redeemscript = scriptpubkey_htlc_recv(ctx, &pubkey1, &pubkey2,
htlc_abstimeout,
locktime, &revoke_hash,
&htlc_abstimeout,
&locktime, &revoke_hash,
&htlc_rhash);
} else {
redeemscript = scriptpubkey_htlc_send(ctx, &pubkey1, &pubkey2,
htlc_abstimeout,
locktime, &revoke_hash,
&htlc_abstimeout,
&locktime, &revoke_hash,
&htlc_rhash);
}
@@ -163,14 +165,14 @@ int main(int argc, char *argv[])
if (!secret_len) {
/* We must be relying on HTLC timeout. */
tx->lock_time = htlc_abstimeout;
tx->lock_time = htlc_abstimeout.locktime;
/* Locktime only applies if an input has seq != ffffffff... */
tx->input[0].sequence_number = 0;
}
/* If it's our own commit tx, we also need delay. */
if (own_commit_tx)
tx->input[0].sequence_number = bitcoin_nsequence(locktime);
tx->input[0].sequence_number = bitcoin_nsequence(&locktime);
/* Leave 10,000 satoshi as fee (if we can!). */
tx->fee = 10000;

View File

@@ -15,6 +15,7 @@
#include "bitcoin/privkey.h"
#include "protobuf_convert.h"
#include "version.h"
#include "bitcoin/locktime.h"
#include <unistd.h>
int main(int argc, char *argv[])
@@ -30,7 +31,7 @@ int main(int argc, char *argv[])
struct bitcoin_signature sig;
struct privkey privkey;
bool testnet;
u32 locktime_seconds;
struct rel_locktime locktime;
err_set_progname(argv[0]);
@@ -68,7 +69,7 @@ int main(int argc, char *argv[])
o1 = pkt_from_file(argv[4], PKT__PKT_OPEN)->open;
o2 = pkt_from_file(argv[5], PKT__PKT_OPEN)->open;
if (!proto_to_rel_locktime(o1->delay, &locktime_seconds))
if (!proto_to_rel_locktime(o1->delay, &locktime))
errx(1, "Invalid locktime in o2");
if (!pubkey_from_hexstr(argv[6], &outpubkey))
@@ -85,7 +86,7 @@ int main(int argc, char *argv[])
/* Now, which commit output? Match redeem script. */
sha256(&revoke_hash, &revoke_preimage, sizeof(revoke_preimage));
redeemscript = bitcoin_redeem_secret_or_delay(ctx, &pubkey2,
locktime_seconds,
&locktime,
&pubkey1, &revoke_hash);
p2sh = scriptpubkey_p2sh(ctx, redeemscript);