mirror of
https://github.com/jb55/nostril.git
synced 2024-11-24 17:09:07 -05:00
nip13: commit to difficulty
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
parent
6ecdc257ba
commit
a53062b84a
31
nostril.c
31
nostril.c
|
@ -348,20 +348,30 @@ static int parse_num(const char *arg, uint64_t *t)
|
||||||
return errno != EINVAL;
|
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;
|
struct nostr_tag *tag;
|
||||||
|
|
||||||
if (ev->num_tags + 1 > MAX_TAGS)
|
if (ev->num_tags + 1 > MAX_TAGS)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
tag = &ev->tags[ev->num_tags++];
|
tag = &ev->tags[ev->num_tags++];
|
||||||
tag->strs[0] = t1;
|
|
||||||
tag->strs[1] = t2;
|
tag->num_elems = n_ts;
|
||||||
tag->num_elems = 2;
|
for (i = 0; i < n_ts; i++) {
|
||||||
|
tag->strs[i] = ts[i];
|
||||||
|
}
|
||||||
|
|
||||||
return 1;
|
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)
|
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;
|
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;
|
struct nostr_tag *tag;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
@ -522,7 +533,11 @@ static int ensure_nonce_tag(struct nostr_event *ev, int *index)
|
||||||
}
|
}
|
||||||
|
|
||||||
*index = ev->num_tags;
|
*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)
|
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;
|
uint64_t nonce;
|
||||||
int index, res;
|
int index, res;
|
||||||
|
|
||||||
if (!ensure_nonce_tag(ev, &index))
|
if (!ensure_nonce_tag(ev, difficulty, &index))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
tag = &ev->tags[index];
|
tag = &ev->tags[index];
|
||||||
assert(tag->num_elems == 2);
|
assert(tag->num_elems == 3);
|
||||||
assert(!strcmp(tag->strs[0], "nonce"));
|
assert(!strcmp(tag->strs[0], "nonce"));
|
||||||
tag->strs[1] = strnonce;
|
tag->strs[1] = strnonce;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user