Skip to content

feat(frontend): implement pow worker and service layer #5887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 56 commits into
base: main
Choose a base branch
from

Conversation

DecentAgeCoder
Copy link
Collaborator

@DecentAgeCoder DecentAgeCoder commented Apr 16, 2025

Motivation

This change introduces the foundational components required to support a Proof-of-Work (PoW)–protected allowSigning operation. Specifically, it prepares a scheduler that will execute the requestSignerCycles function within a Web Worker context.

Note: The PoW worker is defined but not yet initialized via initPowProtectorWorker(..). This will be completed in a subsequent pull request.

Changes

  • Implemented a Scheduler-compliant scheduler using the existing SchedulerTimer to support timed execution of PoW jobs.
  • Introduced a PoW service layer responsible for initializing the PowProtectorWorker via the initPowProtectorWorker function.
  • Added the core PoW Protection Web Worker (PowProtectorWorker).
  • Defined PowProtectorWorkerInitResult to describe the worker's initialization return structure, and introduced the new PowProtectorWorker type.
  • Added a message handler onPowProtectionMessage for processing postMessage communications from the Web Worker on the application side.
  • Registered onPowProtectionMessage(msg) to the the global onmessage handler within the web application to handle messages dispatched by the PoW worker.

DecentAgeCoder and others added 30 commits April 16, 2025 09:12
…ation of the allowSigning function."

This reverts commit 0f19642.
…nto feat/frontend/pow/protect-allow-signing-4
…signing-4' into feat/frontend/pow/protect-allow-signing-4
…signing-4' into feat/frontend/pow/protect-allow-signing-4
…tect-allow-signing-4

# Conflicts:
#	src/frontend/src/tests/lib/canisters/backend.canister.spec.ts
@DecentAgeCoder DecentAgeCoder marked this pull request as ready for review April 23, 2025 14:09
@DecentAgeCoder DecentAgeCoder requested a review from a team as a code owner April 23, 2025 14:09
@DecentAgeCoder DecentAgeCoder enabled auto-merge (squash) April 23, 2025 14:23

// Otherwise, increment the nonce (bigint increment) and try again
nonce++;
}
Copy link
Collaborator

@AntonioVentilii AntonioVentilii Apr 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe make it functional? maybe with self-called functions?

const nonce = ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do that but It could reduce readability. Since this is a critical code a more 'procedural' presentation could therefore be desired here. I will give it a try.

Copy link
Collaborator

@AntonioVentilii AntonioVentilii Apr 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you! in any case, if you prefer this implementation, maybe preferable to use while (prefix > target) and refactor the logic?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A do-while-loop and a conditional-loop will be less readable since they duplicate the condition.

Copy link
Collaborator

@AntonioVentilii AntonioVentilii Apr 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, my idea was something like

const target = BigInt(Math.floor(0xffffffff / difficulty));

const solvePowChallengeRecursive = async (
  timestamp: bigint,
  nonce: bigint = 0n
): Promise<bigint> => {
  const challengeStr = `${timestamp}.${nonce}`;
  const hashHex = await hashText(challengeStr);
  const prefix = BigInt(parseInt(hashHex.slice(0, 8), 16));

  return prefix <= target
    ? nonce
    : solveChallengeRecursive(timestamp, nonce + 1n);
}

const nonce = await solvePowChallengeRecursive(...)

But anyway, let's proceed with the while loop for readability. May you please change it to have a condition instead of while(true)

Copy link
Collaborator

@AntonioVentilii AntonioVentilii Apr 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! and for safety, let's put an hard coded limit of N iterations that raise an error

P.S. and tests

difficulty
}: {
timestamp: bigint; // The unique timestamp for the challenge
difficulty: number; // The difficulty level
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's use the docstring like the other functions, so that you define the params and the results directly in it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I will add a complete docstring here.

Copy link
Collaborator

@AntonioVentilii AntonioVentilii Apr 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove them from here?

Copy link
Collaborator Author

@DecentAgeCoder DecentAgeCoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added feedback on comments

difficulty
}: {
timestamp: bigint; // The unique timestamp for the challenge
difficulty: number; // The difficulty level
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I will add a complete docstring here.


// Otherwise, increment the nonce (bigint increment) and try again
nonce++;
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do that but It could reduce readability. Since this is a critical code a more 'procedural' presentation could therefore be desired here. I will give it a try.


// Otherwise, increment the nonce (bigint increment) and try again
nonce++;
}
Copy link
Collaborator

@AntonioVentilii AntonioVentilii Apr 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! and for safety, let's put an hard coded limit of N iterations that raise an error

P.S. and tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants