From a53062b84a5a482a0fc32dbdc96bd4a628021f43 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Thu, 5 May 2022 16:50:08 -0700 Subject: [PATCH] nip13: commit to difficulty Signed-off-by: William Casarin --- nostril.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/nostril.c b/nostril.c index cbf49cf..4c34d92 100644 --- a/nostril.c +++ b/nostril.c @@ -348,20 +348,30 @@ static int parse_num(const char *arg, uint64_t *t) return errno != EINVAL; } -static int nostr_add_tag(struct nostr_event *ev, const char *t1, const char *t2) +static int nostr_add_tag_n(struct nostr_event *ev, const char **ts, int n_ts) { + int i; struct nostr_tag *tag; if (ev->num_tags + 1 > MAX_TAGS) return 0; tag = &ev->tags[ev->num_tags++]; - tag->strs[0] = t1; - tag->strs[1] = t2; - tag->num_elems = 2; + + tag->num_elems = n_ts; + for (i = 0; i < n_ts; i++) { + tag->strs[i] = ts[i]; + } + return 1; } +static int nostr_add_tag(struct nostr_event *ev, const char *t1, const char *t2) +{ + const char *ts[] = {t1, t2}; + return nostr_add_tag_n(ev, ts, 2); +} + static int parse_args(int argc, const char *argv[], struct args *args, struct nostr_event *ev) { @@ -508,8 +518,9 @@ static int copyx(unsigned char *output, const unsigned char *x32, const unsigned return 1; } -static int ensure_nonce_tag(struct nostr_event *ev, int *index) +static int ensure_nonce_tag(struct nostr_event *ev, int target, int *index) { + char *str_target = malloc(8); struct nostr_tag *tag; int i; @@ -522,7 +533,11 @@ static int ensure_nonce_tag(struct nostr_event *ev, int *index) } *index = ev->num_tags; - return nostr_add_tag(ev, "nonce", "0"); + + snprintf(str_target, 7, "%d", target); + const char *ts[] = { "nonce", "0", str_target }; + + return nostr_add_tag_n(ev, ts, 3); } static int mine_event(struct nostr_event *ev, int difficulty) @@ -532,11 +547,11 @@ static int mine_event(struct nostr_event *ev, int difficulty) uint64_t nonce; int index, res; - if (!ensure_nonce_tag(ev, &index)) + if (!ensure_nonce_tag(ev, difficulty, &index)) return 0; tag = &ev->tags[index]; - assert(tag->num_elems == 2); + assert(tag->num_elems == 3); assert(!strcmp(tag->strs[0], "nonce")); tag->strs[1] = strnonce;