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>
This commit is contained in:
Rusty Russell
2017-02-02 14:35:45 +10:30
parent 279f216208
commit c6997f15c7
19 changed files with 117 additions and 104 deletions

View File

@@ -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;

View File

@@ -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);

View File

@@ -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>

View File

@@ -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. */

View File

@@ -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)

View File

@@ -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);

View File

@@ -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))

View File

@@ -3,10 +3,11 @@
#include "config.h"
#include "lightning.pb-c.h"
struct peer;
struct htlc;
struct sha256;
struct commit_info;
struct htlc;
struct peer;
struct preimage;
struct sha256;
/* 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);

View File

@@ -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;

View File

@@ -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 */

View File

@@ -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,