Browse Source

bitcoin/preimage: struct preimage.

We had a hack for 'struct rval' in protobuf_convert.h; make an
explicit header and put it in bitcoin/preimage.h.  It's not really
bitcoin-specific, but it's better than having bitcoin/script depend on
an external header.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 8 years ago
parent
commit
c6997f15c7
  1. 1
      Makefile
  2. 9
      bitcoin/preimage.h
  3. 10
      daemon/db.c
  4. 2
      daemon/db.h
  5. 1
      daemon/htlc.c
  6. 2
      daemon/htlc.h
  7. 2
      daemon/invoice.c
  8. 6
      daemon/invoice.h
  9. 7
      daemon/packets.c
  10. 7
      daemon/packets.h
  11. 11
      daemon/pay.c
  12. 5
      daemon/pay.h
  13. 12
      daemon/peer.c
  14. 72
      lightning.pb-c.c
  15. 44
      lightning.pb-c.h
  16. 6
      lightning.proto
  17. 12
      protobuf_convert.c
  18. 8
      protobuf_convert.h
  19. 2
      type_to_string.h

1
Makefile

@ -169,6 +169,7 @@ BITCOIN_HEADERS := bitcoin/address.h \
bitcoin/base58.h \
bitcoin/block.h \
bitcoin/locktime.h \
bitcoin/preimage.h \
bitcoin/privkey.h \
bitcoin/pubkey.h \
bitcoin/pullpush.h \

9
bitcoin/preimage.h

@ -0,0 +1,9 @@
#ifndef LIGHTNING_BITCOIN_PREIMAGE_H
#define LIGHTNING_BITCOIN_PREIMAGE_H
#include "config.h"
#include <ccan/short_types/short_types.h>
struct preimage {
u8 r[32];
};
#endif /* LIGHTNING_BITCOIN_PREIMAGE_H */

10
daemon/db.c

@ -575,7 +575,7 @@ static void load_peer_htlcs(struct peer *peer)
hstate);
if (sqlite3_column_type(stmt, 6) != SQLITE_NULL) {
htlc->r = tal(htlc, struct rval);
htlc->r = tal(htlc, struct preimage);
from_sql_blob(stmt, 6, htlc->r, sizeof(*htlc->r));
}
if (sqlite3_column_type(stmt, 10) != SQLITE_NULL) {
@ -1048,7 +1048,7 @@ static void db_load_pay(struct lightningd_state *dstate)
struct pubkey *peer_id;
u64 htlc_id, msatoshi;
struct pubkey *ids;
struct rval *r;
struct preimage *r;
void *fail;
if (err != SQLITE_ROW)
@ -1074,7 +1074,7 @@ static void db_load_pay(struct lightningd_state *dstate)
if (sqlite3_column_type(stmt, 5) == SQLITE_NULL)
r = NULL;
else {
r = tal(ctx, struct rval);
r = tal(ctx, struct preimage);
from_sql_blob(stmt, 5, r, sizeof(*r));
}
fail = tal_sql_blob(ctx, stmt, 6);
@ -1113,7 +1113,7 @@ static void db_load_invoice(struct lightningd_state *dstate)
sqlite3_errstr(err), sqlite3_errmsg(dstate->db->sql));
while ((err = sqlite3_step(stmt)) != SQLITE_DONE) {
struct rval r;
struct preimage r;
u64 msatoshi, paid_num;
const char *label;
@ -1928,7 +1928,7 @@ void db_complete_pay_command(struct lightningd_state *dstate,
bool db_new_invoice(struct lightningd_state *dstate,
u64 msatoshi,
const char *label,
const struct rval *r)
const struct preimage *r)
{
const tal_t *ctx = tal_tmpctx(dstate);
bool ok;

2
daemon/db.h

@ -31,7 +31,7 @@ bool db_replace_pay_command(struct lightningd_state *dstate,
bool db_new_invoice(struct lightningd_state *dstate,
u64 msatoshi,
const char *label,
const struct rval *r);
const struct preimage *r);
bool db_remove_invoice(struct lightningd_state *dstate,
const char *label);

1
daemon/htlc.c

@ -4,6 +4,7 @@
#include "peer.h"
#include "type_to_string.h"
#include "gen_htlc_state_names.h"
#include <bitcoin/preimage.h>
#include <ccan/array_size/array_size.h>
#include <ccan/tal/str/str.h>
#include <inttypes.h>

2
daemon/htlc.h

@ -57,7 +57,7 @@ struct htlc {
/* The hash of the preimage which can redeem this HTLC */
struct sha256 rhash;
/* The preimage which hashes to rhash (if known) */
struct rval *r;
struct preimage *r;
/* FIXME: We could union these together: */
/* Routing information sent with this HTLC. */

2
daemon/invoice.c

@ -57,7 +57,7 @@ static struct invoice *find_invoice_by_label(const struct list_head *list,
}
void invoice_add(struct invoices *invs,
const struct rval *r,
const struct preimage *r,
u64 msatoshi,
const char *label,
u64 paid_num)

6
daemon/invoice.h

@ -1,7 +1,7 @@
#ifndef LIGHTNING_DAEMON_INVOICE_H
#define LIGHTNING_DAEMON_INVOICE_H
#include "config.h"
#include "protobuf_convert.h"
#include <bitcoin/preimage.h>
struct invoices;
struct lightningd_state;
@ -10,7 +10,7 @@ struct invoice {
struct list_node list;
const char *label;
u64 msatoshi;
struct rval r;
struct preimage r;
struct sha256 rhash;
u64 paid_num;
};
@ -19,7 +19,7 @@ struct invoice {
/* From database */
void invoice_add(struct invoices *i,
const struct rval *r,
const struct preimage *r,
u64 msatoshi,
const char *label,
u64 complete);

7
daemon/packets.c

@ -1,3 +1,4 @@
#include "bitcoin/preimage.h"
#include "bitcoin/script.h"
#include "bitcoin/tx.h"
#include "chaintopology.h"
@ -151,7 +152,7 @@ void queue_pkt_htlc_fulfill(struct peer *peer, struct htlc *htlc)
update_fulfill_htlc__init(f);
f->id = htlc->id;
f->r = rval_to_proto(f, htlc->r);
f->r = preimage_to_proto(f, htlc->r);
queue_pkt(peer, PKT__PKT_UPDATE_FULFILL_HTLC, f);
}
@ -476,7 +477,7 @@ Pkt *accept_pkt_htlc_fail(struct peer *peer, const Pkt *pkt, struct htlc **h,
}
Pkt *accept_pkt_htlc_fulfill(struct peer *peer, const Pkt *pkt, struct htlc **h,
struct rval *r)
struct preimage *r)
{
const UpdateFulfillHtlc *f = pkt->update_fulfill_htlc;
struct sha256 rhash;
@ -487,7 +488,7 @@ Pkt *accept_pkt_htlc_fulfill(struct peer *peer, const Pkt *pkt, struct htlc **h,
return err;
/* Now, it must solve the HTLC rhash puzzle. */
proto_to_rval(f->r, r);
proto_to_preimage(f->r, r);
sha256(&rhash, r, sizeof(*r));
if (!structeq(&rhash, &(*h)->rhash))

7
daemon/packets.h

@ -3,10 +3,11 @@
#include "config.h"
#include "lightning.pb-c.h"
struct peer;
struct commit_info;
struct htlc;
struct peer;
struct preimage;
struct sha256;
struct commit_info;
/* Send various kinds of packets */
void queue_pkt_open(struct peer *peer, bool offer_anchor);
@ -48,7 +49,7 @@ Pkt *accept_pkt_htlc_fail(struct peer *peer, const Pkt *pkt, struct htlc **h,
u8 **fail);
Pkt *accept_pkt_htlc_fulfill(struct peer *peer, const Pkt *pkt, struct htlc **h,
struct rval *r);
struct preimage *r);
Pkt *accept_pkt_update_fee(struct peer *peer, const Pkt *pkt, u64 *feerate);

11
daemon/pay.c

@ -8,6 +8,7 @@
#include "peer.h"
#include "routing.h"
#include "sphinx.h"
#include <bitcoin/preimage.h>
#include <ccan/str/hex/hex.h>
#include <ccan/structeq/structeq.h>
#include <inttypes.h>
@ -22,10 +23,10 @@ struct pay_command {
/* Set if this is in progress. */
struct htlc *htlc;
/* Preimage if this succeeded. */
const struct rval *rval;
const struct preimage *rval;
struct command *cmd;
};
static void json_pay_success(struct command *cmd, const struct rval *rval)
static void json_pay_success(struct command *cmd, const struct preimage *rval)
{
struct json_result *response;
@ -106,7 +107,7 @@ void complete_pay_command(struct lightningd_state *dstate,
db_complete_pay_command(dstate, htlc);
if (htlc->r)
i->rval = tal_dup(i, struct rval, htlc->r);
i->rval = tal_dup(i, struct preimage, htlc->r);
else {
f = failinfo_unwrap(i->cmd, htlc->fail,
tal_count(htlc->fail));
@ -162,7 +163,7 @@ bool pay_add(struct lightningd_state *dstate,
const struct pubkey *ids,
struct htlc *htlc,
const u8 *fail UNNEEDED,
const struct rval *r)
const struct preimage *r)
{
struct pay_command *pc;
@ -175,7 +176,7 @@ bool pay_add(struct lightningd_state *dstate,
pc->ids = tal_dup_arr(pc, struct pubkey, ids, tal_count(ids), 0);
pc->htlc = htlc;
if (r)
pc->rval = tal_dup(pc, struct rval, r);
pc->rval = tal_dup(pc, struct preimage, r);
else
pc->rval = NULL;
pc->cmd = NULL;

5
daemon/pay.h

@ -2,8 +2,9 @@
#define LIGHTNING_DAEMON_PAY_H
#include "config.h"
struct lightningd_state;
struct htlc;
struct lightningd_state;
struct preimage;
void complete_pay_command(struct lightningd_state *dstate,
const struct htlc *htlc);
@ -14,5 +15,5 @@ bool pay_add(struct lightningd_state *dstate,
const struct pubkey *ids,
struct htlc *htlc,
const u8 *fail,
const struct rval *r);
const struct preimage *r);
#endif /* LIGHTNING_DAEMON_PAY_H */

12
daemon/peer.c

@ -766,11 +766,11 @@ static bool open_wait_pkt_in(struct peer *peer, const Pkt *pkt)
}
static void set_htlc_rval(struct peer *peer,
struct htlc *htlc, const struct rval *rval)
struct htlc *htlc, const struct preimage *rval)
{
assert(!htlc->r);
assert(!htlc->fail);
htlc->r = tal_dup(htlc, struct rval, rval);
htlc->r = tal_dup(htlc, struct preimage, rval);
db_htlc_fulfilled(peer, htlc);
}
@ -1509,7 +1509,7 @@ static Pkt *handle_pkt_htlc_fulfill(struct peer *peer, const Pkt *pkt)
{
struct htlc *htlc;
Pkt *err;
struct rval r;
struct preimage r;
err = accept_pkt_htlc_fulfill(peer, pkt, &htlc, &r);
if (err)
@ -3745,7 +3745,7 @@ static enum watch_result our_htlc_spent(struct peer *peer,
struct htlc *h)
{
struct sha256 sha;
struct rval preimage;
struct preimage preimage;
/* FIXME-OLD #onchain:
*
@ -3776,7 +3776,7 @@ static enum watch_result our_htlc_spent(struct peer *peer,
log_unusual(peer->log, "Peer redeemed HTLC %"PRIu64" on-chain",
h->id);
log_add_struct(peer->log, " using rvalue %s", struct rval, &preimage);
log_add_struct(peer->log, " using rvalue %s", struct preimage, &preimage);
set_htlc_rval(peer, h, &preimage);
our_htlc_fulfilled(peer, h);
@ -4748,7 +4748,7 @@ static void json_fulfillhtlc(struct command *cmd,
u64 id;
struct htlc *htlc;
struct sha256 rhash;
struct rval r;
struct preimage r;
if (!json_get_params(buffer, params,
"peerid", &peeridtok,

72
lightning.pb-c.c

@ -50,47 +50,47 @@ void sha256_hash__free_unpacked
assert(message->base.descriptor == &sha256_hash__descriptor);
protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator);
}
void rval__init
(Rval *message)
void preimage__init
(Preimage *message)
{
static Rval init_value = RVAL__INIT;
static Preimage init_value = PREIMAGE__INIT;
*message = init_value;
}
size_t rval__get_packed_size
(const Rval *message)
size_t preimage__get_packed_size
(const Preimage *message)
{
assert(message->base.descriptor == &rval__descriptor);
assert(message->base.descriptor == &preimage__descriptor);
return protobuf_c_message_get_packed_size ((const ProtobufCMessage*)(message));
}
size_t rval__pack
(const Rval *message,
size_t preimage__pack
(const Preimage *message,
uint8_t *out)
{
assert(message->base.descriptor == &rval__descriptor);
assert(message->base.descriptor == &preimage__descriptor);
return protobuf_c_message_pack ((const ProtobufCMessage*)message, out);
}
size_t rval__pack_to_buffer
(const Rval *message,
size_t preimage__pack_to_buffer
(const Preimage *message,
ProtobufCBuffer *buffer)
{
assert(message->base.descriptor == &rval__descriptor);
assert(message->base.descriptor == &preimage__descriptor);
return protobuf_c_message_pack_to_buffer ((const ProtobufCMessage*)message, buffer);
}
Rval *
rval__unpack
Preimage *
preimage__unpack
(ProtobufCAllocator *allocator,
size_t len,
const uint8_t *data)
{
return (Rval *)
protobuf_c_message_unpack (&rval__descriptor,
return (Preimage *)
protobuf_c_message_unpack (&preimage__descriptor,
allocator, len, data);
}
void rval__free_unpacked
(Rval *message,
void preimage__free_unpacked
(Preimage *message,
ProtobufCAllocator *allocator)
{
assert(message->base.descriptor == &rval__descriptor);
assert(message->base.descriptor == &preimage__descriptor);
protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator);
}
void signature__init
@ -1288,7 +1288,7 @@ const ProtobufCMessageDescriptor sha256_hash__descriptor =
(ProtobufCMessageInit) sha256_hash__init,
NULL,NULL,NULL /* reserved[123] */
};
static const ProtobufCFieldDescriptor rval__field_descriptors[4] =
static const ProtobufCFieldDescriptor preimage__field_descriptors[4] =
{
{
"a",
@ -1296,7 +1296,7 @@ static const ProtobufCFieldDescriptor rval__field_descriptors[4] =
PROTOBUF_C_LABEL_REQUIRED,
PROTOBUF_C_TYPE_FIXED64,
0, /* quantifier_offset */
offsetof(Rval, a),
offsetof(Preimage, a),
NULL,
NULL,
0, /* flags */
@ -1308,7 +1308,7 @@ static const ProtobufCFieldDescriptor rval__field_descriptors[4] =
PROTOBUF_C_LABEL_REQUIRED,
PROTOBUF_C_TYPE_FIXED64,
0, /* quantifier_offset */
offsetof(Rval, b),
offsetof(Preimage, b),
NULL,
NULL,
0, /* flags */
@ -1320,7 +1320,7 @@ static const ProtobufCFieldDescriptor rval__field_descriptors[4] =
PROTOBUF_C_LABEL_REQUIRED,
PROTOBUF_C_TYPE_FIXED64,
0, /* quantifier_offset */
offsetof(Rval, c),
offsetof(Preimage, c),
NULL,
NULL,
0, /* flags */
@ -1332,37 +1332,37 @@ static const ProtobufCFieldDescriptor rval__field_descriptors[4] =
PROTOBUF_C_LABEL_REQUIRED,
PROTOBUF_C_TYPE_FIXED64,
0, /* quantifier_offset */
offsetof(Rval, d),
offsetof(Preimage, d),
NULL,
NULL,
0, /* flags */
0,NULL,NULL /* reserved1,reserved2, etc */
},
};
static const unsigned rval__field_indices_by_name[] = {
static const unsigned preimage__field_indices_by_name[] = {
0, /* field[0] = a */
1, /* field[1] = b */
2, /* field[2] = c */
3, /* field[3] = d */
};
static const ProtobufCIntRange rval__number_ranges[1 + 1] =
static const ProtobufCIntRange preimage__number_ranges[1 + 1] =
{
{ 1, 0 },
{ 0, 4 }
};
const ProtobufCMessageDescriptor rval__descriptor =
const ProtobufCMessageDescriptor preimage__descriptor =
{
PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC,
"rval",
"Rval",
"Rval",
"preimage",
"Preimage",
"Preimage",
"",
sizeof(Rval),
sizeof(Preimage),
4,
rval__field_descriptors,
rval__field_indices_by_name,
1, rval__number_ranges,
(ProtobufCMessageInit) rval__init,
preimage__field_descriptors,
preimage__field_indices_by_name,
1, preimage__number_ranges,
(ProtobufCMessageInit) preimage__init,
NULL,NULL,NULL /* reserved[123] */
};
static const ProtobufCFieldDescriptor signature__field_descriptors[8] =
@ -2288,7 +2288,7 @@ static const ProtobufCFieldDescriptor update_fulfill_htlc__field_descriptors[2]
PROTOBUF_C_TYPE_MESSAGE,
0, /* quantifier_offset */
offsetof(UpdateFulfillHtlc, r),
&rval__descriptor,
&preimage__descriptor,
NULL,
0, /* flags */
0,NULL,NULL /* reserved1,reserved2, etc */

44
lightning.pb-c.h

@ -16,7 +16,7 @@ PROTOBUF_C__BEGIN_DECLS
typedef struct _Sha256Hash Sha256Hash;
typedef struct _Rval Rval;
typedef struct _Preimage Preimage;
typedef struct _Signature Signature;
typedef struct _Locktime Locktime;
typedef struct _BitcoinPubkey BitcoinPubkey;
@ -77,7 +77,7 @@ struct _Sha256Hash
, 0, 0, 0, 0 }
struct _Rval
struct _Preimage
{
ProtobufCMessage base;
uint64_t a;
@ -85,8 +85,8 @@ struct _Rval
uint64_t c;
uint64_t d;
};
#define RVAL__INIT \
{ PROTOBUF_C_MESSAGE_INIT (&rval__descriptor) \
#define PREIMAGE__INIT \
{ PROTOBUF_C_MESSAGE_INIT (&preimage__descriptor) \
, 0, 0, 0, 0 }
@ -401,9 +401,9 @@ struct _UpdateFulfillHtlc
*/
uint64_t id;
/*
* HTLC R value.
* HTLC payment_preimage.
*/
Rval *r;
Preimage *r;
};
#define UPDATE_FULFILL_HTLC__INIT \
{ PROTOBUF_C_MESSAGE_INIT (&update_fulfill_htlc__descriptor) \
@ -649,24 +649,24 @@ Sha256Hash *
void sha256_hash__free_unpacked
(Sha256Hash *message,
ProtobufCAllocator *allocator);
/* Rval methods */
void rval__init
(Rval *message);
size_t rval__get_packed_size
(const Rval *message);
size_t rval__pack
(const Rval *message,
/* Preimage methods */
void preimage__init
(Preimage *message);
size_t preimage__get_packed_size
(const Preimage *message);
size_t preimage__pack
(const Preimage *message,
uint8_t *out);
size_t rval__pack_to_buffer
(const Rval *message,
size_t preimage__pack_to_buffer
(const Preimage *message,
ProtobufCBuffer *buffer);
Rval *
rval__unpack
Preimage *
preimage__unpack
(ProtobufCAllocator *allocator,
size_t len,
const uint8_t *data);
void rval__free_unpacked
(Rval *message,
void preimage__free_unpacked
(Preimage *message,
ProtobufCAllocator *allocator);
/* Signature methods */
void signature__init
@ -1167,8 +1167,8 @@ void pkt__free_unpacked
typedef void (*Sha256Hash_Closure)
(const Sha256Hash *message,
void *closure_data);
typedef void (*Rval_Closure)
(const Rval *message,
typedef void (*Preimage_Closure)
(const Preimage *message,
void *closure_data);
typedef void (*Signature_Closure)
(const Signature *message,
@ -1255,7 +1255,7 @@ typedef void (*Pkt_Closure)
/* --- descriptors --- */
extern const ProtobufCMessageDescriptor sha256_hash__descriptor;
extern const ProtobufCMessageDescriptor rval__descriptor;
extern const ProtobufCMessageDescriptor preimage__descriptor;
extern const ProtobufCMessageDescriptor signature__descriptor;
extern const ProtobufCMessageDescriptor locktime__descriptor;
extern const ProtobufCMessageDescriptor bitcoin_pubkey__descriptor;

6
lightning.proto

@ -15,7 +15,7 @@ message sha256_hash {
required fixed64 d = 4;
}
message rval {
message preimage {
required fixed64 a = 1;
required fixed64 b = 2;
required fixed64 c = 3;
@ -164,8 +164,8 @@ message update_add_htlc {
message update_fulfill_htlc {
// Which HTLC
required uint64 id = 1;
// HTLC R value.
required rval r = 2;
// HTLC payment_preimage.
required preimage r = 2;
}
// This is encrypted in fail_reason.

12
protobuf_convert.c

@ -1,4 +1,5 @@
#include "bitcoin/locktime.h"
#include "bitcoin/preimage.h"
#include "bitcoin/pubkey.h"
#include "bitcoin/signature.h"
#include "protobuf_convert.h"
@ -92,10 +93,10 @@ void proto_to_sha256(const Sha256Hash *pb, struct sha256 *hash)
memcpy(hash->u.u8 + 24, &pb->d, 8);
}
Rval *rval_to_proto(const tal_t *ctx, const struct rval *r)
Preimage *preimage_to_proto(const tal_t *ctx, const struct preimage *r)
{
Rval *pb = tal(ctx, Rval);
rval__init(pb);
Preimage *pb = tal(ctx, Preimage);
preimage__init(pb);
/* Kill me now... */
memcpy(&pb->a, r->r, 8);
@ -105,7 +106,7 @@ Rval *rval_to_proto(const tal_t *ctx, const struct rval *r)
return pb;
}
void proto_to_rval(const Rval *pb, struct rval *r)
void proto_to_preimage(const Preimage *pb, struct preimage *r)
{
/* Kill me again. */
memcpy(r->r, &pb->a, 8);
@ -202,5 +203,4 @@ void steal_from_prototal(const tal_t *ctx, struct ProtobufCAllocator *prototal,
tal_steal(pb, prototal->allocator_data);
tal_free(prototal);
}
REGISTER_TYPE_TO_HEXSTR(rval);
REGISTER_TYPE_TO_HEXSTR(preimage);

8
protobuf_convert.h

@ -23,11 +23,9 @@ struct sha256;
Sha256Hash *sha256_to_proto(const tal_t *ctx, const struct sha256 *hash);
void proto_to_sha256(const Sha256Hash *pb, struct sha256 *hash);
struct rval {
u8 r[32];
};
Rval *rval_to_proto(const tal_t *ctx, const struct rval *r);
void proto_to_rval(const Rval *pb, struct rval *r);
struct preimage;
Preimage *preimage_to_proto(const tal_t *ctx, const struct preimage *r);
void proto_to_preimage(const Preimage *pb, struct preimage *r);
struct rel_locktime;
struct abs_locktime;

2
type_to_string.h

@ -14,7 +14,7 @@ union printable_types {
const struct abs_locktime *abs_locktime;
const struct bitcoin_tx *bitcoin_tx;
const struct htlc *htlc;
const struct rval *rval;
const struct preimage *preimage;
const struct channel_state *channel_state;
const struct channel_oneside *channel_oneside;
const struct netaddr *netaddr;

Loading…
Cancel
Save