diff options
| author | Francisco Calderón <fjcalderon@gmail.com> | 2024-11-04 15:39:21 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-04 15:39:21 -0300 |
| commit | 03f3bc39678262ecbd5d870c9da44723023557ff (patch) | |
| tree | e75ecf32d3bc906a8b26314488a1ae90996169c1 /13.md | |
| parent | f72a2f69ed93cf442e83bf9e7e16f6c06da40384 (diff) | |
| parent | 6bcd89c097e97e65dbc95e7c6b7b8348e8dd6b5c (diff) | |
Merge branch 'master' into p2p-nip
Diffstat (limited to '13.md')
| -rw-r--r-- | 13.md | 57 |
1 files changed, 20 insertions, 37 deletions
| @@ -48,37 +48,30 @@ Validating | |||
| 48 | Here is some reference C code for calculating the difficulty (aka number of leading zero bits) in a nostr event id: | 48 | Here 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> | 51 | int zero_bits(unsigned char b) |
| 52 | #include <stdlib.h> | 52 | { |
| 53 | #include <string.h> | 53 | int n = 0; |
| 54 | |||
| 55 | int 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 | ||
| 71 | int 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 */ |
| 65 | int 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 | ||
| 106 | Querying relays for PoW notes | ||
| 107 | ----------------------------- | ||
| 108 | |||
| 109 | If 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 | |||
| 116 | Delegated Proof of Work | 99 | Delegated Proof of Work |
| 117 | ----------------------- | 100 | ----------------------- |
| 118 | 101 | ||