summaryrefslogtreecommitdiff
path: root/web/source/nollamasworker
diff options
context:
space:
mode:
authorLibravatar kim <grufwub@gmail.com>2025-05-26 11:57:50 +0200
committerLibravatar tobi <kipvandenbos@noreply.codeberg.org>2025-05-26 11:57:50 +0200
commit326e04283a2536d64a7055bfef184f5817b691d6 (patch)
treeaf3f86c2ce3a0ce430825a84f653f8a1e2c40b03 /web/source/nollamasworker
parent[chore] update dependencies (#4188) (diff)
downloadgotosocial-326e04283a2536d64a7055bfef184f5817b691d6.tar.xz
[feature] update proof-of-work to allow setting required rounds (#4186)
# Description This updates our proof-of-work middleware, NoLLaMas, to work on a more easily configurable algorithm (thank you f0x for bringing this to my attention!). Instead of requiring that a solution with pre-determined number of '0' chars be found, it now pre-computes a result with a pre-determined nonce value that it expects the client to iterate up-to. (though with some level of jitter applied, to prevent it being too-easily gamed). This allows the user to configure roughly how many hash-encode rounds they want their clients to have to complete. ## Checklist - [x] I/we have read the [GoToSocial contribution guidelines](https://codeberg.org/superseriousbusiness/gotosocial/src/branch/main/CONTRIBUTING.md). - [x] I/we have discussed the proposed changes already, either in an issue on the repository, or in the Matrix chat. - [x] I/we have not leveraged AI to create the proposed changes. - [x] I/we have performed a self-review of added code. - [x] I/we have written code that is legible and maintainable by others. - [x] I/we have commented the added code, particularly in hard-to-understand areas. - [x] I/we have made any necessary changes to documentation. - [ ] I/we have added tests that cover new code. - [x] I/we have run tests and they pass locally with the changes. - [x] I/we have run `go fmt ./...` and `golangci-lint run`. Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4186 Co-authored-by: kim <grufwub@gmail.com> Co-committed-by: kim <grufwub@gmail.com>
Diffstat (limited to 'web/source/nollamasworker')
-rw-r--r--web/source/nollamasworker/index.js33
1 files changed, 10 insertions, 23 deletions
diff --git a/web/source/nollamasworker/index.js b/web/source/nollamasworker/index.js
index 2762b125e..3c9b043c2 100644
--- a/web/source/nollamasworker/index.js
+++ b/web/source/nollamasworker/index.js
@@ -19,32 +19,22 @@
import sha256 from "./sha256";
-let compute = async function(challengeStr, diffStr) {
+let compute = async function(seedStr, challengeStr) {
const textEncoder = new TextEncoder();
- // Get difficulty1 as number and generate
- // expected zero ASCII prefix to check for.
- const diff1 = parseInt(diffStr, 10);
- const zeros = "0".repeat(diff1);
-
- // Calculate hex encoded prefix required to check solution, where we
- // need diff1 no. chars in hex, and hex encoding doubles input length.
- const prefixLen = diff1 / 2 + (diff1 % 2 != 0 ? 2 : 0);
-
let nonce = 0;
while (true) { // eslint-disable-line no-constant-condition
// Create possible solution string from challenge string + nonce.
- const solution = textEncoder.encode(challengeStr + nonce.toString());
+ const solution = textEncoder.encode(seedStr + nonce.toString());
- // Generate SHA256 hashsum of solution string, and hex encode the
- // necessary prefix length we need to check for a valid solution.
- const prefixArray = Array.from(sha256(solution).slice(0, prefixLen));
- const prefixHex = prefixArray.map(b => b.toString(16).padStart(2, "0")).join("");
+ // Generate hex encoded SHA256 hashsum of solution.
+ const hashArray = Array.from(sha256(solution));
+ const hashAsHex = hashArray.map(b => b.toString(16).padStart(2, "0")).join("");
- // Check if the hex encoded hash has
- // difficulty defined zeroes prefix.
- if (prefixHex.startsWith(zeros)) {
+ // Check whether hex encoded
+ // solution matches challenge.
+ if (hashAsHex == challengeStr) {
return nonce;
}
@@ -56,11 +46,8 @@ let compute = async function(challengeStr, diffStr) {
onmessage = async function(e) {
console.log('worker started'); // eslint-disable-line no-console
- const challenge = e.data.challenge;
- const difficulty = e.data.difficulty;
-
- // Compute the nonce that produces solution with args.
- let nonce = await compute(challenge, difficulty);
+ // Compute nonce value that produces 'challenge' for seed.
+ let nonce = await compute(e.data.seed, e.data.challenge);
// Post the solution nonce back to caller.
postMessage({ nonce: nonce, done: true });