diff options
| author | Xe Iaso <me@xeiaso.net> | 2025-04-23 23:48:16 -0400 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2025-04-23 23:48:16 -0400 |
| commit | 3d0a5c2d8791764f15cd41459696722c22bcd312 (patch) | |
| tree | a92f1f27a162b7c2f945f12a07643ae5a1c023f7 /lib/verifier.go | |
| parent | ea321b7f13e59120a9995284ee0d1f9c7498c06b (diff) | |
| download | anubis-wasm.tar.xz anubis-wasm.zip | |
feat(lib): limit concurrency of wasm-based verifierswasm
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'lib/verifier.go')
| -rw-r--r-- | lib/verifier.go | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/verifier.go b/lib/verifier.go index 3c28c14..c41cb93 100644 --- a/lib/verifier.go +++ b/lib/verifier.go @@ -6,6 +6,8 @@ import ( "crypto/subtle" "errors" "fmt" + + "golang.org/x/sync/semaphore" ) var ( @@ -68,3 +70,23 @@ func hasLeadingZeroNibbles(data []byte, n uint32) bool { } return count >= n } + +type ConcurrentVerifier struct { + Verifier + sem *semaphore.Weighted +} + +func NewConcurrentVerifier(v Verifier, maxConcurrent int64) *ConcurrentVerifier { + return &ConcurrentVerifier{ + Verifier: v, + sem: semaphore.NewWeighted(maxConcurrent), + } +} + +func (cv *ConcurrentVerifier) Verify(ctx context.Context, challenge, verify []byte, nonce, difficulty uint32) (bool, error) { + if err := cv.sem.Acquire(ctx, 1); err != nil { + return false, fmt.Errorf("can't verify solution: %w", err) + } + + return cv.Verifier.Verify(ctx, challenge, verify, nonce, difficulty) +} |
