Skip to content
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

Use context instead of an atomic variable #3599

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions crypto/merklesignature/keysBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package merklesignature

import (
"context"
"runtime"
"sync"
"sync/atomic"

"github.com/algorand/go-algorand/crypto"
)
Expand All @@ -28,7 +28,8 @@ import (
func KeysBuilder(numberOfKeys uint64) ([]crypto.FalconSigner, error) {
numOfKeysPerRoutine, _ := calculateRanges(numberOfKeys)

var terminate int64
ctx, ctxCancel := context.WithCancel(context.Background())
defer ctxCancel()

errors := make(chan error, 1)
defer close(errors)
Expand All @@ -48,13 +49,13 @@ func KeysBuilder(numberOfKeys uint64) ([]crypto.FalconSigner, error) {
wg.Add(1)
go func(startIdx, endIdx uint64, keys []crypto.FalconSigner) {
defer wg.Done()
if err := generateKeysForRange(startIdx, endIdx, &terminate, keys); err != nil {
if err := generateKeysForRange(ctx, startIdx, endIdx, keys); err != nil {
// write to the error channel, if it's not full already.
select {
case errors <- err:
default:
}
atomic.StoreInt64(&terminate, 1)
ctxCancel()
}
}(i, endIdx, keys)
}
Expand All @@ -80,10 +81,10 @@ func calculateRanges(numberOfKeys uint64) (numOfKeysPerRoutine uint64, numOfRout
return
}

func generateKeysForRange(startIdx uint64, endIdx uint64, terminate *int64, keys []crypto.FalconSigner) error {
func generateKeysForRange(ctx context.Context, startIdx uint64, endIdx uint64, keys []crypto.FalconSigner) error {
for k := startIdx; k < endIdx; k++ {
if atomic.LoadInt64(terminate) != 0 {
return nil
if ctx.Err() != nil {
break
}
sigAlgo, err := crypto.NewFalconSigner()
if err != nil {
Expand Down
24 changes: 12 additions & 12 deletions crypto/merklesignature/merkleSignatureScheme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package merklesignature

import (
"crypto/rand"
"errors"
"math"
"testing"

Expand Down Expand Up @@ -240,7 +241,7 @@ func TestSigning(t *testing.T) {

err = signer.GetVerifier().Verify(start+5, hashable, sig)
a.Error(err)
a.Contains(err.Error(), ErrSignatureSchemeVerificationFailed)
a.True(errors.Is(err, ErrSignatureSchemeVerificationFailed))
Copy link
Contributor

Choose a reason for hiding this comment

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


signer = generateTestSigner(50, 100, 12, a)
a.Equal(4, length(signer, a))
Expand Down Expand Up @@ -274,16 +275,16 @@ func TestBadRound(t *testing.T) {

err := signer.GetVerifier().Verify(start+1, hashable, sig)
a.Error(err)
a.Contains(err.Error(), ErrSignatureSchemeVerificationFailed)
a.True(errors.Is(err, ErrSignatureSchemeVerificationFailed))

hashable, sig = makeSig(signer, start+1, a)
err = signer.GetVerifier().Verify(start, hashable, sig)
a.Error(err)
a.Contains(err.Error(), ErrSignatureSchemeVerificationFailed)
a.True(errors.Is(err, ErrSignatureSchemeVerificationFailed))

err = signer.GetVerifier().Verify(start+2, hashable, sig)
a.Error(err)
a.Contains(err.Error(), ErrSignatureSchemeVerificationFailed)
a.True(errors.Is(err, ErrSignatureSchemeVerificationFailed))
}

func TestBadMerkleProofInSignature(t *testing.T) {
Expand All @@ -297,15 +298,15 @@ func TestBadMerkleProofInSignature(t *testing.T) {
sig2.Proof.Path = sig2.Proof.Path[:len(sig2.Proof.Path)-1]
err := signer.GetVerifier().Verify(start, hashable, sig2)
a.Error(err)
a.Contains(err.Error(), ErrSignatureSchemeVerificationFailed)
a.True(errors.Is(err, ErrSignatureSchemeVerificationFailed))

sig3 := copySig(sig)
someDigest := crypto.Digest{}
rand.Read(someDigest[:])
sig3.Proof.Path[0] = someDigest[:]
err = signer.GetVerifier().Verify(start, hashable, sig3)
a.Error(err)
a.Contains(err.Error(), ErrSignatureSchemeVerificationFailed)
a.True(errors.Is(err, ErrSignatureSchemeVerificationFailed))
}

func copySig(sig Signature) Signature {
Expand Down Expand Up @@ -334,7 +335,7 @@ func TestIncorrectByteSignature(t *testing.T) {

err := signer.GetVerifier().Verify(start, hashable, sig2)
a.Error(err)
a.Contains(err.Error(), ErrSignatureSchemeVerificationFailed)
a.True(errors.Is(err, ErrSignatureSchemeVerificationFailed))
}

func TestIncorrectMerkleIndex(t *testing.T) {
Expand All @@ -352,17 +353,16 @@ func TestIncorrectMerkleIndex(t *testing.T) {
sig.MerkleArrayIndex = 0
err = signer.GetVerifier().Verify(20, h, sig)
a.Error(err)
a.Contains(err.Error(), ErrSignatureSchemeVerificationFailed)
a.True(errors.Is(err, ErrSignatureSchemeVerificationFailed))

sig.MerkleArrayIndex = math.MaxUint64
err = signer.GetVerifier().Verify(20, h, sig)
a.Error(err)
a.Contains(err.Error(), ErrSignatureSchemeVerificationFailed)
a.True(errors.Is(err, ErrSignatureSchemeVerificationFailed))

err = signer.GetVerifier().Verify(20, h, sig)
a.Error(err)
a.Contains(err.Error(), ErrSignatureSchemeVerificationFailed)

a.True(errors.Is(err, ErrSignatureSchemeVerificationFailed))
}

func TestAttemptToUseDifferentKey(t *testing.T) {
Expand All @@ -382,7 +382,7 @@ func TestAttemptToUseDifferentKey(t *testing.T) {

err := signer.GetVerifier().Verify(start+1, hashable, sig2)
a.Error(err)
a.Contains(err.Error(), ErrSignatureSchemeVerificationFailed)
a.True(errors.Is(err, ErrSignatureSchemeVerificationFailed))
}

func TestMarshal(t *testing.T) {
Expand Down