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

Merged
merged 63 commits into from
Apr 25, 2025

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

// 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

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


// 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

@AntonioVentilii AntonioVentilii enabled auto-merge (squash) April 25, 2025 06:53
@AntonioVentilii AntonioVentilii merged commit 8bf80b8 into main Apr 25, 2025
30 checks passed
@AntonioVentilii AntonioVentilii deleted the feat/frontend/pow/protect-allow-signing-4 branch April 25, 2025 07:10
daviddecentage pushed a commit that referenced this pull request May 22, 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.

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Antonio Ventilii <[email protected]>
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