upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/13.md
diff options
context:
space:
mode:
authorKieran <kieran@harkin.me>2024-10-15 11:15:54 +0100
committerGitHub <noreply@github.com>2024-10-15 11:15:54 +0100
commit1e2f19863ca56754daa2466881eb22087a71b17d (patch)
tree4f7be759dce290fea1d3ae10c403260b466ee16c /13.md
parent53afaaece61f02e92b5ef9c3e9c32945c7ebf522 (diff)
parente381b577c997b849fa544eea7dc9f08b360b4a33 (diff)
Merge branch 'master' into nip71-imeta
Diffstat (limited to '13.md')
-rw-r--r--13.md57
1 files changed, 20 insertions, 37 deletions
diff --git a/13.md b/13.md
index 99289c2..cf5b1ac 100644
--- a/13.md
+++ b/13.md
@@ -48,37 +48,30 @@ Validating
48Here is some reference C code for calculating the difficulty (aka number of leading zero bits) in a nostr event id: 48Here is some reference C code for calculating the difficulty (aka number of leading zero bits) in a nostr event id:
49 49
50```c 50```c
51#include <stdio.h> 51int zero_bits(unsigned char b)
52#include <stdlib.h> 52{
53#include <string.h> 53 int n = 0;
54
55int countLeadingZeroes(const char *hex) {
56 int count = 0;
57
58 for (int i = 0; i < strlen(hex); i++) {
59 int nibble = (int)strtol((char[]){hex[i], '\0'}, NULL, 16);
60 if (nibble == 0) {
61 count += 4;
62 } else {
63 count += __builtin_clz(nibble) - 28;
64 break;
65 }
66 }
67 54
68 return count; 55 if (b == 0)
69} 56 return 8;
70 57
71int main(int argc, char *argv[]) { 58 while (b >>= 1)
72 if (argc != 2) { 59 n++;
73 fprintf(stderr, "Usage: %s <hex_string>\n", argv[0]);
74 return 1;
75 }
76 60
77 const char *hex_string = argv[1]; 61 return 7-n;
78 int result = countLeadingZeroes(hex_string); 62}
79 printf("Leading zeroes in hex string %s: %d\n", hex_string, result);
80 63
81 return 0; 64/* find the number of leading zero bits in a hash */
65int count_leading_zero_bits(unsigned char *hash)
66{
67 int bits, total, i;
68 for (i = 0, total = 0; i < 32; i++) {
69 bits = zero_bits(hash[i]);
70 total += bits;
71 if (bits != 8)
72 break;
73 }
74 return total;
82} 75}
83``` 76```
84 77
@@ -103,16 +96,6 @@ function countLeadingZeroes(hex) {
103} 96}
104``` 97```
105 98
106Querying relays for PoW notes
107-----------------------------
108
109If relays allow searching on prefixes, you can use this as a way to filter notes of a certain difficulty:
110
111```
112$ echo '["REQ", "subid", {"ids": ["000000000"]}]' | websocat wss://some-relay.com | jq -c '.[2]'
113{"id":"000000000121637feeb68a06c8fa7abd25774bdedfa9b6ef648386fb3b70c387", ...}
114```
115
116Delegated Proof of Work 99Delegated Proof of Work
117----------------------- 100-----------------------
118 101