Browse Source

daemon: struct rval to represent r values.

We've been stuffing these into sha256s, but they're actually nonces.
Create a new structure for that for clarity.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 9 years ago
parent
commit
156d1be9ed
  1. 9
      daemon/packets.c
  2. 9
      daemon/peer.c
  3. 5
      daemon/peer.h
  4. 122
      lightning.pb-c.c
  5. 39
      lightning.pb-c.h
  6. 9
      lightning.proto
  7. 23
      protobuf_convert.c
  8. 6
      protobuf_convert.h
  9. 4
      state.h

9
daemon/packets.c

@ -205,7 +205,7 @@ void queue_pkt_htlc_add(struct peer *peer, const struct channel_htlc *htlc)
queue_pkt(peer, PKT__PKT_UPDATE_ADD_HTLC, u); queue_pkt(peer, PKT__PKT_UPDATE_ADD_HTLC, u);
} }
void queue_pkt_htlc_fulfill(struct peer *peer, u64 id, const struct sha256 *r) void queue_pkt_htlc_fulfill(struct peer *peer, u64 id, const struct rval *r)
{ {
UpdateFulfillHtlc *f = tal(peer, UpdateFulfillHtlc); UpdateFulfillHtlc *f = tal(peer, UpdateFulfillHtlc);
size_t n; size_t n;
@ -213,7 +213,7 @@ void queue_pkt_htlc_fulfill(struct peer *peer, u64 id, const struct sha256 *r)
update_fulfill_htlc__init(f); update_fulfill_htlc__init(f);
f->id = id; f->id = id;
f->r = sha256_to_proto(f, r); f->r = rval_to_proto(f, r);
/* BOLT #2: /* BOLT #2:
* *
@ -724,7 +724,8 @@ Pkt *accept_pkt_htlc_fulfill(struct peer *peer, const Pkt *pkt)
{ {
const UpdateFulfillHtlc *f = pkt->update_fulfill_htlc; const UpdateFulfillHtlc *f = pkt->update_fulfill_htlc;
size_t n_local; size_t n_local;
struct sha256 r, rhash; struct sha256 rhash;
struct rval r;
Pkt *err; Pkt *err;
union htlc_staging stage; union htlc_staging stage;
@ -733,7 +734,7 @@ Pkt *accept_pkt_htlc_fulfill(struct peer *peer, const Pkt *pkt)
return err; return err;
/* Now, it must solve the HTLC rhash puzzle. */ /* Now, it must solve the HTLC rhash puzzle. */
proto_to_sha256(f->r, &r); proto_to_rval(f->r, &r);
sha256(&rhash, &r, sizeof(r)); sha256(&rhash, &r, sizeof(r));
if (!structeq(&rhash, &peer->local.staging_cstate->side[OURS].htlcs[n_local].rhash)) if (!structeq(&rhash, &peer->local.staging_cstate->side[OURS].htlcs[n_local].rhash))

9
daemon/peer.c

@ -546,7 +546,7 @@ static bool command_htlc_fail(struct peer *peer, u64 id)
static bool command_htlc_fulfill(struct peer *peer, static bool command_htlc_fulfill(struct peer *peer,
u64 id, u64 id,
const struct sha256 *r) const struct rval *r)
{ {
if (!state_can_remove_htlc(peer->state)) if (!state_can_remove_htlc(peer->state))
return false; return false;
@ -1310,7 +1310,8 @@ static void our_htlc_spent(struct peer *peer,
ptrint_t *pi) ptrint_t *pi)
{ {
struct channel_htlc *h; struct channel_htlc *h;
struct sha256 preimage, sha; struct sha256 sha;
struct rval preimage;
size_t i = ptr2int(pi); size_t i = ptr2int(pi);
/* It should be spending the HTLC we expect. */ /* It should be spending the HTLC we expect. */
@ -1468,7 +1469,7 @@ static void resolve_our_htlcs(struct peer *peer,
* preimage. Otherwise, the other node could spend it once it as *timed out* * preimage. Otherwise, the other node could spend it once it as *timed out*
* as above. * as above.
*/ */
bool resolve_one_htlc(struct peer *peer, u64 id, const struct sha256 *preimage) bool resolve_one_htlc(struct peer *peer, u64 id, const struct rval *preimage)
{ {
FIXME_STUB(peer); FIXME_STUB(peer);
} }
@ -2497,7 +2498,7 @@ static void json_fulfillhtlc(struct command *cmd,
{ {
struct peer *peer; struct peer *peer;
jsmntok_t *peeridtok, *rtok; jsmntok_t *peeridtok, *rtok;
struct sha256 r; struct rval r;
struct sha256 rhash; struct sha256 rhash;
size_t i; size_t i;
u64 id; u64 id;

5
daemon/peer.h

@ -9,6 +9,7 @@
#include "funding.h" #include "funding.h"
#include "lightning.pb-c.h" #include "lightning.pb-c.h"
#include "netaddr.h" #include "netaddr.h"
#include "protobuf_convert.h"
#include "state.h" #include "state.h"
#include <ccan/crypto/sha256/sha256.h> #include <ccan/crypto/sha256/sha256.h>
#include <ccan/crypto/shachain/shachain.h> #include <ccan/crypto/shachain/shachain.h>
@ -29,7 +30,7 @@ struct htlc_add {
struct htlc_fulfill { struct htlc_fulfill {
enum htlc_stage_type fulfill; enum htlc_stage_type fulfill;
u64 id; u64 id;
struct sha256 r; struct rval r;
}; };
struct htlc_fail { struct htlc_fail {
@ -241,5 +242,5 @@ struct bitcoin_tx *peer_create_close_tx(struct peer *peer, u64 fee);
uint64_t commit_tx_fee(const struct bitcoin_tx *commit, uint64_t commit_tx_fee(const struct bitcoin_tx *commit,
uint64_t anchor_satoshis); uint64_t anchor_satoshis);
bool resolve_one_htlc(struct peer *peer, u64 id, const struct sha256 *preimage); bool resolve_one_htlc(struct peer *peer, u64 id, const struct rval *preimage);
#endif /* LIGHTNING_DAEMON_PEER_H */ #endif /* LIGHTNING_DAEMON_PEER_H */

122
lightning.pb-c.c

@ -50,6 +50,49 @@ void sha256_hash__free_unpacked
assert(message->base.descriptor == &sha256_hash__descriptor); assert(message->base.descriptor == &sha256_hash__descriptor);
protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator); protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator);
} }
void rval__init
(Rval *message)
{
static Rval init_value = RVAL__INIT;
*message = init_value;
}
size_t rval__get_packed_size
(const Rval *message)
{
assert(message->base.descriptor == &rval__descriptor);
return protobuf_c_message_get_packed_size ((const ProtobufCMessage*)(message));
}
size_t rval__pack
(const Rval *message,
uint8_t *out)
{
assert(message->base.descriptor == &rval__descriptor);
return protobuf_c_message_pack ((const ProtobufCMessage*)message, out);
}
size_t rval__pack_to_buffer
(const Rval *message,
ProtobufCBuffer *buffer)
{
assert(message->base.descriptor == &rval__descriptor);
return protobuf_c_message_pack_to_buffer ((const ProtobufCMessage*)message, buffer);
}
Rval *
rval__unpack
(ProtobufCAllocator *allocator,
size_t len,
const uint8_t *data)
{
return (Rval *)
protobuf_c_message_unpack (&rval__descriptor,
allocator, len, data);
}
void rval__free_unpacked
(Rval *message,
ProtobufCAllocator *allocator)
{
assert(message->base.descriptor == &rval__descriptor);
protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator);
}
void signature__init void signature__init
(Signature *message) (Signature *message)
{ {
@ -987,6 +1030,83 @@ const ProtobufCMessageDescriptor sha256_hash__descriptor =
(ProtobufCMessageInit) sha256_hash__init, (ProtobufCMessageInit) sha256_hash__init,
NULL,NULL,NULL /* reserved[123] */ NULL,NULL,NULL /* reserved[123] */
}; };
static const ProtobufCFieldDescriptor rval__field_descriptors[4] =
{
{
"a",
1,
PROTOBUF_C_LABEL_REQUIRED,
PROTOBUF_C_TYPE_FIXED64,
0, /* quantifier_offset */
offsetof(Rval, a),
NULL,
NULL,
0, /* flags */
0,NULL,NULL /* reserved1,reserved2, etc */
},
{
"b",
2,
PROTOBUF_C_LABEL_REQUIRED,
PROTOBUF_C_TYPE_FIXED64,
0, /* quantifier_offset */
offsetof(Rval, b),
NULL,
NULL,
0, /* flags */
0,NULL,NULL /* reserved1,reserved2, etc */
},
{
"c",
3,
PROTOBUF_C_LABEL_REQUIRED,
PROTOBUF_C_TYPE_FIXED64,
0, /* quantifier_offset */
offsetof(Rval, c),
NULL,
NULL,
0, /* flags */
0,NULL,NULL /* reserved1,reserved2, etc */
},
{
"d",
4,
PROTOBUF_C_LABEL_REQUIRED,
PROTOBUF_C_TYPE_FIXED64,
0, /* quantifier_offset */
offsetof(Rval, d),
NULL,
NULL,
0, /* flags */
0,NULL,NULL /* reserved1,reserved2, etc */
},
};
static const unsigned rval__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] =
{
{ 1, 0 },
{ 0, 4 }
};
const ProtobufCMessageDescriptor rval__descriptor =
{
PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC,
"rval",
"Rval",
"Rval",
"",
sizeof(Rval),
4,
rval__field_descriptors,
rval__field_indices_by_name,
1, rval__number_ranges,
(ProtobufCMessageInit) rval__init,
NULL,NULL,NULL /* reserved[123] */
};
static const ProtobufCFieldDescriptor signature__field_descriptors[8] = static const ProtobufCFieldDescriptor signature__field_descriptors[8] =
{ {
{ {
@ -1770,7 +1890,7 @@ static const ProtobufCFieldDescriptor update_fulfill_htlc__field_descriptors[2]
PROTOBUF_C_TYPE_MESSAGE, PROTOBUF_C_TYPE_MESSAGE,
0, /* quantifier_offset */ 0, /* quantifier_offset */
offsetof(UpdateFulfillHtlc, r), offsetof(UpdateFulfillHtlc, r),
&sha256_hash__descriptor, &rval__descriptor,
NULL, NULL,
0, /* flags */ 0, /* flags */
0,NULL,NULL /* reserved1,reserved2, etc */ 0,NULL,NULL /* reserved1,reserved2, etc */

39
lightning.pb-c.h

@ -16,6 +16,7 @@ PROTOBUF_C__BEGIN_DECLS
typedef struct _Sha256Hash Sha256Hash; typedef struct _Sha256Hash Sha256Hash;
typedef struct _Rval Rval;
typedef struct _Signature Signature; typedef struct _Signature Signature;
typedef struct _Locktime Locktime; typedef struct _Locktime Locktime;
typedef struct _BitcoinPubkey BitcoinPubkey; typedef struct _BitcoinPubkey BitcoinPubkey;
@ -70,6 +71,19 @@ struct _Sha256Hash
, 0, 0, 0, 0 } , 0, 0, 0, 0 }
struct _Rval
{
ProtobufCMessage base;
uint64_t a;
uint64_t b;
uint64_t c;
uint64_t d;
};
#define RVAL__INIT \
{ PROTOBUF_C_MESSAGE_INIT (&rval__descriptor) \
, 0, 0, 0, 0 }
struct _Signature struct _Signature
{ {
ProtobufCMessage base; ProtobufCMessage base;
@ -326,7 +340,7 @@ struct _UpdateFulfillHtlc
/* /*
* HTLC R value. * HTLC R value.
*/ */
Sha256Hash *r; Rval *r;
}; };
#define UPDATE_FULFILL_HTLC__INIT \ #define UPDATE_FULFILL_HTLC__INIT \
{ PROTOBUF_C_MESSAGE_INIT (&update_fulfill_htlc__descriptor) \ { PROTOBUF_C_MESSAGE_INIT (&update_fulfill_htlc__descriptor) \
@ -524,6 +538,25 @@ Sha256Hash *
void sha256_hash__free_unpacked void sha256_hash__free_unpacked
(Sha256Hash *message, (Sha256Hash *message,
ProtobufCAllocator *allocator); 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,
uint8_t *out);
size_t rval__pack_to_buffer
(const Rval *message,
ProtobufCBuffer *buffer);
Rval *
rval__unpack
(ProtobufCAllocator *allocator,
size_t len,
const uint8_t *data);
void rval__free_unpacked
(Rval *message,
ProtobufCAllocator *allocator);
/* Signature methods */ /* Signature methods */
void signature__init void signature__init
(Signature *message); (Signature *message);
@ -909,6 +942,9 @@ void pkt__free_unpacked
typedef void (*Sha256Hash_Closure) typedef void (*Sha256Hash_Closure)
(const Sha256Hash *message, (const Sha256Hash *message,
void *closure_data); void *closure_data);
typedef void (*Rval_Closure)
(const Rval *message,
void *closure_data);
typedef void (*Signature_Closure) typedef void (*Signature_Closure)
(const Signature *message, (const Signature *message,
void *closure_data); void *closure_data);
@ -976,6 +1012,7 @@ typedef void (*Pkt_Closure)
/* --- descriptors --- */ /* --- descriptors --- */
extern const ProtobufCMessageDescriptor sha256_hash__descriptor; extern const ProtobufCMessageDescriptor sha256_hash__descriptor;
extern const ProtobufCMessageDescriptor rval__descriptor;
extern const ProtobufCMessageDescriptor signature__descriptor; extern const ProtobufCMessageDescriptor signature__descriptor;
extern const ProtobufCMessageDescriptor locktime__descriptor; extern const ProtobufCMessageDescriptor locktime__descriptor;
extern const ProtobufCMessageDescriptor bitcoin_pubkey__descriptor; extern const ProtobufCMessageDescriptor bitcoin_pubkey__descriptor;

9
lightning.proto

@ -15,6 +15,13 @@ message sha256_hash {
required fixed64 d = 4; required fixed64 d = 4;
} }
message rval {
required fixed64 a = 1;
required fixed64 b = 2;
required fixed64 c = 3;
required fixed64 d = 4;
}
message signature { message signature {
required fixed64 r1 = 1; required fixed64 r1 = 1;
required fixed64 r2 = 2; required fixed64 r2 = 2;
@ -135,7 +142,7 @@ message update_fulfill_htlc {
// Which HTLC // Which HTLC
required uint64 id = 1; required uint64 id = 1;
// HTLC R value. // HTLC R value.
required sha256_hash r = 2; required rval r = 2;
} }
// FIXME: Failure information. // FIXME: Failure information.

23
protobuf_convert.c

@ -89,6 +89,29 @@ void proto_to_sha256(const Sha256Hash *pb, struct sha256 *hash)
memcpy(hash->u.u8 + 24, &pb->d, 8); memcpy(hash->u.u8 + 24, &pb->d, 8);
} }
Rval *rval_to_proto(const tal_t *ctx, const struct rval *r)
{
Rval *pb = tal(ctx, Rval);
rval__init(pb);
/* Kill me now... */
memcpy(&pb->a, r->r, 8);
memcpy(&pb->b, r->r + 8, 8);
memcpy(&pb->c, r->r + 16, 8);
memcpy(&pb->d, r->r + 24, 8);
return pb;
}
void proto_to_rval(const Rval *pb, struct rval *r)
{
/* Kill me again. */
memcpy(r->r, &pb->a, 8);
memcpy(r->r + 8, &pb->b, 8);
memcpy(r->r + 16, &pb->c, 8);
memcpy(r->r + 24, &pb->d, 8);
}
bool proto_to_rel_locktime(const Locktime *l, struct rel_locktime *locktime) bool proto_to_rel_locktime(const Locktime *l, struct rel_locktime *locktime)
{ {
switch (l->locktime_case) { switch (l->locktime_case) {

6
protobuf_convert.h

@ -22,6 +22,12 @@ 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);
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 rel_locktime; struct rel_locktime;
struct abs_locktime; struct abs_locktime;
bool proto_to_rel_locktime(const Locktime *l, struct rel_locktime *locktime); bool proto_to_rel_locktime(const Locktime *l, struct rel_locktime *locktime);

4
state.h

@ -90,7 +90,7 @@ static inline bool input_is(enum state_input a, enum state_input b)
return a == b; return a == b;
} }
struct signature; struct rval;
/* Inform peer have an unexpected packet. */ /* Inform peer have an unexpected packet. */
void peer_unexpected_pkt(struct peer *peer, const Pkt *pkt); void peer_unexpected_pkt(struct peer *peer, const Pkt *pkt);
@ -102,7 +102,7 @@ void queue_pkt_open_commit_sig(struct peer *peer);
void queue_pkt_open_complete(struct peer *peer); void queue_pkt_open_complete(struct peer *peer);
void queue_pkt_htlc_add(struct peer *peer, void queue_pkt_htlc_add(struct peer *peer,
const struct channel_htlc *htlc); const struct channel_htlc *htlc);
void queue_pkt_htlc_fulfill(struct peer *peer, u64 id, const struct sha256 *r); void queue_pkt_htlc_fulfill(struct peer *peer, u64 id, const struct rval *r);
void queue_pkt_htlc_fail(struct peer *peer, u64 id); void queue_pkt_htlc_fail(struct peer *peer, u64 id);
void queue_pkt_commit(struct peer *peer); void queue_pkt_commit(struct peer *peer);
void queue_pkt_revocation(struct peer *peer); void queue_pkt_revocation(struct peer *peer);

Loading…
Cancel
Save