renepay: put the entire hash in the key struct.

As recommended by your TODO, a bit simpler: we also make the hash function
return a ptr rather than the (now rather large) struct.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2023-08-10 09:31:18 +09:30
parent 135180c1a0
commit e8cfb40709
4 changed files with 25 additions and 24 deletions

View file

@ -1292,11 +1292,10 @@ static struct pay_flow *pay_flow_from_notification(const char *buf,
/* Single part payment? No partid */
key.partid = 0;
key.payment_hash = tal(tmpctx, struct sha256);
err = json_scan(tmpctx, buf, obj, "{partid?:%,groupid:%,payment_hash:%}",
JSON_SCAN(json_to_u64, &key.partid),
JSON_SCAN(json_to_u64, &key.groupid),
JSON_SCAN(json_to_sha256, key.payment_hash));
JSON_SCAN(json_to_sha256, &key.payment_hash));
if (err) {
plugin_err(pay_plugin->plugin,
"Missing fields (%s) in notification: %.*s",
@ -1305,7 +1304,7 @@ static struct pay_flow *pay_flow_from_notification(const char *buf,
json_tok_full(buf, obj));
}
return payflow_map_get(pay_plugin->payflow_map, key);
return payflow_map_get(pay_plugin->payflow_map, &key);
}
// TODO(eduardo): if I subscribe to a shutdown notification, the plugin takes

View file

@ -220,7 +220,7 @@ static struct pay_flow **flows_to_pay_flows(struct payment *payment,
pf->payment = payment;
pf->key.partid = (*next_partid)++;
pf->key.groupid = payment->groupid;
pf->key.payment_hash = &payment->payment_hash;
pf->key.payment_hash = payment->payment_hash;
/* Convert gossmap_chan into scids and nodes */
pf->path_scids = tal_arr(pf, struct short_channel_id, plen);

View file

@ -17,8 +17,7 @@ struct pay_flow {
/* Information to link this flow to a unique sendpay. */
struct payflow_key
{
// TODO(eduardo): pointer or value?
struct sha256 *payment_hash;
struct sha256 payment_hash;
u64 groupid;
u64 partid;
} key;
@ -36,9 +35,9 @@ struct pay_flow {
};
static inline struct payflow_key
payflow_key(struct sha256 *hash, u64 groupid, u64 partid)
payflow_key(const struct sha256 *hash, u64 groupid, u64 partid)
{
struct payflow_key k= {hash,groupid,partid};
struct payflow_key k= {*hash,groupid,partid};
return k;
}
@ -50,27 +49,27 @@ static inline const char* fmt_payflow_key(
ctx,
"key: groupid=%"PRIu64", partid=%"PRIu64", payment_hash=%s",
k->groupid,k->partid,
type_to_string(ctx,struct sha256,k->payment_hash));
type_to_string(ctx,struct sha256,&k->payment_hash));
return str;
}
static inline const struct payflow_key
static inline const struct payflow_key *
payflow_get_key(const struct pay_flow * pf)
{
return pf->key;
return &pf->key;
}
static inline size_t payflow_key_hash(const struct payflow_key k)
static inline size_t payflow_key_hash(const struct payflow_key *k)
{
return k.payment_hash->u.u32[0] ^ (k.groupid << 32) ^ k.partid;
return k->payment_hash.u.u32[0] ^ (k->groupid << 32) ^ k->partid;
}
static inline bool payflow_key_equal(const struct pay_flow *pf,
const struct payflow_key k)
const struct payflow_key *k)
{
return pf->key.partid==k.partid && pf->key.groupid==k.groupid
&& sha256_eq(pf->key.payment_hash,k.payment_hash);
return pf->key.partid==k->partid && pf->key.groupid==k->groupid
&& sha256_eq(&pf->key.payment_hash, &k->payment_hash);
}
HTABLE_DEFINE_TYPE(struct pay_flow,

View file

@ -31,14 +31,14 @@ static void destroy_payflow(
}
static struct pay_flow* new_payflow(
const tal_t *ctx,
struct sha256 * payment_hash,
const struct sha256 * payment_hash,
u64 gid,
u64 pid)
{
struct pay_flow *p = tal(ctx,struct pay_flow);
p->payment=NULL;
p->key.payment_hash=payment_hash;
p->key.payment_hash = *payment_hash;
p->key.groupid = gid;
p->key.partid = pid;
@ -61,6 +61,7 @@ static void valgrind_ok1(void)
{
tal_t *local_ctx = tal(this_ctx,tal_t);
struct payflow_key key;
struct pay_flow *p1 = new_payflow(local_ctx,
&hash,1,1);
@ -69,17 +70,19 @@ static void valgrind_ok1(void)
printf("key1 = %s\n",fmt_payflow_key(local_ctx,&p1->key));
printf("key1 = %s\n",fmt_payflow_key(local_ctx,&p2->key));
printf("key hash 1 = %zu\n",payflow_key_hash(p1->key));
printf("key hash 2 = %zu\n",payflow_key_hash(p2->key));
printf("key hash 1 = %zu\n",payflow_key_hash(&p1->key));
printf("key hash 2 = %zu\n",payflow_key_hash(&p2->key));
payflow_map_add(map,p1); tal_add_destructor2(p1,destroy_payflow,map);
payflow_map_add(map,p2); tal_add_destructor2(p2,destroy_payflow,map);
struct pay_flow *q1 = payflow_map_get(map,payflow_key(&hash,1,1));
struct pay_flow *q2 = payflow_map_get(map,payflow_key(&hash,2,3));
key = payflow_key(&hash,1,1);
struct pay_flow *q1 = payflow_map_get(map, &key);
key = payflow_key(&hash,2,3);
struct pay_flow *q2 = payflow_map_get(map, &key);
assert(payflow_key_hash(q1->key)==payflow_key_hash(p1->key));
assert(payflow_key_hash(q2->key)==payflow_key_hash(p2->key));
assert(payflow_key_hash(&q1->key)==payflow_key_hash(&p1->key));
assert(payflow_key_hash(&q2->key)==payflow_key_hash(&p2->key));
tal_free(local_ctx);
}