From 28ad74a02efbc29bd52107feab00785306c2f585 Mon Sep 17 00:00:00 2001 From: Javad Date: Mon, 23 Sep 2024 16:34:01 +0330 Subject: [PATCH] fix: linter errors --- cmd/cmd.go | 6 +-- sortition/vrf.go | 12 +++--- sortition/vrf_test.go | 8 ++-- util/encoding/encoding_test.go | 18 ++++----- util/io.go | 8 ++-- util/testsuite/testsuite.go | 70 +++++++++++++++++----------------- util/utils.go | 28 +++++++------- 7 files changed, 75 insertions(+), 75 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index dab7a1ec4..7aad0a63b 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -150,7 +150,7 @@ func PromptInputWithSuggestion(label, suggestion string) string { } // PromptInputWithRange prompts the user for an input integer within a specified range. -func PromptInputWithRange(label string, def, minVal, maxVal int) int { +func PromptInputWithRange(label string, def, min, max int) int { prompt := promptui.Prompt{ Label: label, Default: fmt.Sprintf("%v", def), @@ -161,8 +161,8 @@ func PromptInputWithRange(label string, def, minVal, maxVal int) int { if err != nil { return err } - if num < minVal || num > maxVal { - return fmt.Errorf("enter a number between %v and %v", minVal, maxVal) + if num < min || num > max { + return fmt.Errorf("enter a number between %v and %v", min, max) } return nil diff --git a/sortition/vrf.go b/sortition/vrf.go index 8d2154b55..fb3ad434a 100644 --- a/sortition/vrf.go +++ b/sortition/vrf.go @@ -17,7 +17,7 @@ func init() { // Evaluate returns a provable random number between [0, max) along with a proof. // It returns the random number and the proof that can regenerate the random number using // the public key of the signer, without revealing the private key. -func Evaluate(seed VerifiableSeed, prv *bls.PrivateKey, maxVal uint64) (uint64, Proof) { +func Evaluate(seed VerifiableSeed, prv *bls.PrivateKey, max uint64) (uint64, Proof) { signData := make([]byte, 0, bls.SignatureSize+bls.PublicKeySize) signData = append(signData, seed[:]...) signData = append(signData, prv.PublicKey().Bytes()...) @@ -25,7 +25,7 @@ func Evaluate(seed VerifiableSeed, prv *bls.PrivateKey, maxVal uint64) (uint64, sig := prv.Sign(signData) proof, _ := ProofFromBytes(sig.Bytes()) - index := GetIndex(proof, maxVal) + index := GetIndex(proof, max) return index, proof } @@ -33,7 +33,7 @@ func Evaluate(seed VerifiableSeed, prv *bls.PrivateKey, maxVal uint64) (uint64, // Verify checks if the provided proof, based on the seed and public key, is valid. // If the proof is valid, it calculates the random number that // can be generated based on the given proof. -func Verify(seed VerifiableSeed, pub *bls.PublicKey, proof Proof, maxVal uint64) (uint64, bool) { +func Verify(seed VerifiableSeed, pub *bls.PublicKey, proof Proof, max uint64) (uint64, bool) { proofSig, err := bls.SignatureFromBytes(proof[:]) if err != nil { return 0, false @@ -47,12 +47,12 @@ func Verify(seed VerifiableSeed, pub *bls.PublicKey, proof Proof, maxVal uint64) return 0, false } - index := GetIndex(proof, maxVal) + index := GetIndex(proof, max) return index, true } -func GetIndex(proof Proof, maxVal uint64) uint64 { +func GetIndex(proof Proof, max uint64) uint64 { h := hash.CalcHash(proof[:]) // construct the numerator and denominator for normalizing the proof uint @@ -61,7 +61,7 @@ func GetIndex(proof Proof, maxVal uint64) uint64 { numerator := &big.Int{} bigRnd.SetBytes(h.Bytes()) - bigMax.SetUint64(maxVal) + bigMax.SetUint64(max) numerator = numerator.Mul(bigRnd, bigMax) diff --git a/sortition/vrf_test.go b/sortition/vrf_test.go index eedd8448f..0967c0162 100644 --- a/sortition/vrf_test.go +++ b/sortition/vrf_test.go @@ -20,12 +20,12 @@ func TestVRF(t *testing.T) { seed := ts.RandSeed() t.Logf("seed is: %x \n", seed) - maxVal := uint64(1 * 1e6) - index, proof := sortition.Evaluate(seed, valKey.PrivateKey(), maxVal) + maxSize := uint64(1 * 1e6) + index, proof := sortition.Evaluate(seed, valKey.PrivateKey(), maxSize) - assert.LessOrEqual(t, index, maxVal) + assert.LessOrEqual(t, index, maxSize) - index2, result := sortition.Verify(seed, pk, proof, maxVal) + index2, result := sortition.Verify(seed, pk, proof, maxSize) assert.True(t, result) assert.Equal(t, index, index2) diff --git a/util/encoding/encoding_test.go b/util/encoding/encoding_test.go index 164edede3..a5b81e28a 100644 --- a/util/encoding/encoding_test.go +++ b/util/encoding/encoding_test.go @@ -92,7 +92,7 @@ func TestElementEncoding(t *testing.T) { func TestElementEncodingErrors(t *testing.T) { tests := []struct { in any // Value to encode - maxVal int // Max size of fixed buffer to induce errors + max int // Max size of fixed buffer to induce errors writeErr error // Expected write error readErr error // Expected read error }{ @@ -119,11 +119,11 @@ func TestElementEncodingErrors(t *testing.T) { t.Logf("Running %d tests", len(tests)) for i, test := range tests { - w := util.NewFixedWriter(test.maxVal) + w := util.NewFixedWriter(test.max) err := WriteElement(w, test.in) assert.ErrorIs(t, err, test.writeErr, "writeElement #%d", i) - r := util.NewFixedReader(test.maxVal, nil) + r := util.NewFixedReader(test.max, nil) val := test.in if reflect.ValueOf(test.in).Kind() != reflect.Ptr { val = reflect.New(reflect.TypeOf(test.in)).Interface() @@ -176,7 +176,7 @@ func TestVarStringEncodingErrors(t *testing.T) { tests := []struct { in string // Value to encode buf []byte // Encoding bytes - maxVal int // Max size of fixed buffer to induce errors + max int // Max size of fixed buffer to induce errors writeErr error // Expected write error readErr error // Expected read error }{ @@ -191,11 +191,11 @@ func TestVarStringEncodingErrors(t *testing.T) { t.Logf("Running %d tests", len(tests)) for i, test := range tests { - w := util.NewFixedWriter(test.maxVal) + w := util.NewFixedWriter(test.max) err := WriteVarString(w, test.in) assert.ErrorIs(t, err, test.writeErr, "WriteVarString #%d", i) - r := util.NewFixedReader(test.maxVal, test.buf) + r := util.NewFixedReader(test.max, test.buf) _, err = ReadVarString(r) assert.ErrorIs(t, err, test.readErr, "ReadVarString #%d wrong", i) } @@ -264,7 +264,7 @@ func TestVarBytesEncodingErrors(t *testing.T) { tests := []struct { in []byte // Byte Array to write buf []byte // Encoding bytes - maxVal int // Max size of fixed buffer to induce errors + max int // Max size of fixed buffer to induce errors writeErr error // Expected write error readErr error // Expected read error }{ @@ -279,11 +279,11 @@ func TestVarBytesEncodingErrors(t *testing.T) { t.Logf("Running %d tests", len(tests)) for i, test := range tests { - w := util.NewFixedWriter(test.maxVal) + w := util.NewFixedWriter(test.max) err := WriteVarBytes(w, test.in) assert.ErrorIs(t, err, test.writeErr, "WriteVarBytes #%d", i) - r := util.NewFixedReader(test.maxVal, test.buf) + r := util.NewFixedReader(test.max, test.buf) _, err = ReadVarBytes(r) assert.ErrorIs(t, err, test.readErr, "ReadVarBytes #%d", i) } diff --git a/util/io.go b/util/io.go index 78cc24093..52d931169 100644 --- a/util/io.go +++ b/util/io.go @@ -155,8 +155,8 @@ func (w *FixedWriter) Bytes() []byte { // NewFixedWriter returns a new io.Writer that will error once more bytes than // the specified max have been written. -func NewFixedWriter(maxVal int) *FixedWriter { - b := make([]byte, maxVal) +func NewFixedWriter(max int) *FixedWriter { + b := make([]byte, max) fw := FixedWriter{b, 0} return &fw @@ -188,8 +188,8 @@ func (fr *FixedReader) Read(p []byte) (int, error) { // NewFixedReader returns a new io.Reader that will error once more bytes than // the specified max have been read. -func NewFixedReader(maxVal int, buf []byte) *FixedReader { - b := make([]byte, maxVal) +func NewFixedReader(max int, buf []byte) *FixedReader { + b := make([]byte, max) if buf != nil { copy(b, buf) } diff --git a/util/testsuite/testsuite.go b/util/testsuite/testsuite.go index cbcbf6a40..7adf16dc9 100644 --- a/util/testsuite/testsuite.go +++ b/util/testsuite/testsuite.go @@ -68,83 +68,83 @@ func (ts *TestSuite) RandBool() bool { } // RandInt8 returns a random int8 between 0 and max: [0, max). -func (ts *TestSuite) RandInt8(maxVal int8) int8 { - return int8(ts.RandUint64(uint64(maxVal))) +func (ts *TestSuite) RandInt8(max int8) int8 { + return int8(ts.RandUint64(uint64(max))) } // RandUint8 returns a random uint8 between 0 and max: [0, max). -func (ts *TestSuite) RandUint8(maxVal uint8) uint8 { - return uint8(ts.RandUint64(uint64(maxVal))) +func (ts *TestSuite) RandUint8(max uint8) uint8 { + return uint8(ts.RandUint64(uint64(max))) } // RandInt16 returns a random int16 between 0 and max: [0, max). -func (ts *TestSuite) RandInt16(maxVal int16) int16 { - return int16(ts.RandUint64(uint64(maxVal))) +func (ts *TestSuite) RandInt16(max int16) int16 { + return int16(ts.RandUint64(uint64(max))) } // RandUint16 returns a random uint16 between 0 and max: [0, max). -func (ts *TestSuite) RandUint16(maxVal uint16) uint16 { - return uint16(ts.RandUint64(uint64(maxVal))) +func (ts *TestSuite) RandUint16(max uint16) uint16 { + return uint16(ts.RandUint64(uint64(max))) } // RandInt32 returns a random int32 between 0 and max: [0, max). -func (ts *TestSuite) RandInt32(maxVal int32) int32 { - return int32(ts.RandUint64(uint64(maxVal))) +func (ts *TestSuite) RandInt32(max int32) int32 { + return int32(ts.RandUint64(uint64(max))) } // RandUint32 returns a random uint32 between 0 and max: [0, max). -func (ts *TestSuite) RandUint32(maxVal uint32) uint32 { - return uint32(ts.RandUint64(uint64(maxVal))) +func (ts *TestSuite) RandUint32(max uint32) uint32 { + return uint32(ts.RandUint64(uint64(max))) } // RandInt64 returns a random int64 between 0 and max: [0, max). -func (ts *TestSuite) RandInt64(maxVal int64) int64 { - return ts.Rand.Int63n(maxVal) +func (ts *TestSuite) RandInt64(max int64) int64 { + return ts.Rand.Int63n(max) } // RandUint64 returns a random uint64 between 0 and max: [0, max). -func (ts *TestSuite) RandUint64(maxVal uint64) uint64 { - return uint64(ts.RandInt64(int64(maxVal))) +func (ts *TestSuite) RandUint64(max uint64) uint64 { + return uint64(ts.RandInt64(int64(max))) } // RandInt returns a random int between 0 and max: [0, max). -func (ts *TestSuite) RandInt(maxVal int) int { - return int(ts.RandInt64(int64(maxVal))) +func (ts *TestSuite) RandInt(max int) int { + return int(ts.RandInt64(int64(max))) } // RandInt16NonZero returns a random int16 between 1 and max+1: [1, max+1). -func (ts *TestSuite) RandInt16NonZero(maxVal int16) int16 { - return ts.RandInt16(maxVal) + 1 +func (ts *TestSuite) RandInt16NonZero(max int16) int16 { + return ts.RandInt16(max) + 1 } // RandUint16NonZero returns a random uint16 between 1 and max+1: [1, max+1). -func (ts *TestSuite) RandUint16NonZero(maxVal uint16) uint16 { - return ts.RandUint16(maxVal) + 1 +func (ts *TestSuite) RandUint16NonZero(max uint16) uint16 { + return ts.RandUint16(max) + 1 } // RandInt32NonZero returns a random int32 between 1 and max+1: [1, max+1). -func (ts *TestSuite) RandInt32NonZero(maxVal int32) int32 { - return ts.RandInt32(maxVal) + 1 +func (ts *TestSuite) RandInt32NonZero(max int32) int32 { + return ts.RandInt32(max) + 1 } // RandUint32NonZero returns a random uint32 between 1 and max+1: [1, max+1). -func (ts *TestSuite) RandUint32NonZero(maxVal uint32) uint32 { - return ts.RandUint32(maxVal) + 1 +func (ts *TestSuite) RandUint32NonZero(max uint32) uint32 { + return ts.RandUint32(max) + 1 } // RandInt64NonZero returns a random int64 between 1 and max+1: [1, max+1). -func (ts *TestSuite) RandInt64NonZero(maxVal int64) int64 { - return ts.RandInt64(maxVal) + 1 +func (ts *TestSuite) RandInt64NonZero(max int64) int64 { + return ts.RandInt64(max) + 1 } // RandUint64NonZero returns a random uint64 between 1 and max+1: [1, max+1). -func (ts *TestSuite) RandUint64NonZero(maxVal uint64) uint64 { - return ts.RandUint64(maxVal) + 1 +func (ts *TestSuite) RandUint64NonZero(max uint64) uint64 { + return ts.RandUint64(max) + 1 } // RandIntNonZero returns a random int between 1 and max+1: [1, max+1). -func (ts *TestSuite) RandIntNonZero(maxVal int) int { - return ts.RandInt(maxVal) + 1 +func (ts *TestSuite) RandIntNonZero(max int) int { + return ts.RandInt(max) + 1 } // RandHeight returns a random number between [1000, 1000000] for block height. @@ -163,10 +163,10 @@ func (ts *TestSuite) RandAmount() amount.Amount { } // RandAmountRange returns a random amount between [min, max). -func (ts *TestSuite) RandAmountRange(minVal, maxVal amount.Amount) amount.Amount { - amt := amount.Amount(ts.RandInt64NonZero(int64(maxVal - minVal))) +func (ts *TestSuite) RandAmountRange(min, max amount.Amount) amount.Amount { + amt := amount.Amount(ts.RandInt64NonZero(int64(max - min))) - return amt + minVal + return amt + min } // RandFee returns a random fee between [0, 1). diff --git a/util/utils.go b/util/utils.go index 427f3385d..94ec20bd1 100644 --- a/util/utils.go +++ b/util/utils.go @@ -50,43 +50,43 @@ func Min[T constraints.Integer](a, b T) T { // RandInt16 returns a random int16 in between 0 and max. // If max set to zero or negative, the max will set to MaxInt16. -func RandInt16(maxVal int16) int16 { - return int16(RandUint64(uint64(maxVal))) +func RandInt16(max int16) int16 { + return int16(RandUint64(uint64(max))) } // RandUint16 returns a random uint16 in between 0 and max. // If max set to zero or negative, the max will set to MaxUint16. -func RandUint16(maxVal uint32) uint16 { - return uint16(RandUint64(uint64(maxVal))) +func RandUint16(max uint32) uint16 { + return uint16(RandUint64(uint64(max))) } // RandInt32 returns a random int32 in between 0 and max. // If max set to zero or negative, the max will set to MaxInt32. -func RandInt32(maxVal int32) int32 { - return int32(RandUint64(uint64(maxVal))) +func RandInt32(max int32) int32 { + return int32(RandUint64(uint64(max))) } // RandUint32 returns a random uint32 in between 0 and max. // If max set to zero or negative, the max will set to MaxUint32. -func RandUint32(maxVal uint32) uint32 { - return uint32(RandUint64(uint64(maxVal))) +func RandUint32(max uint32) uint32 { + return uint32(RandUint64(uint64(max))) } // RandInt64 returns a random int64 in between 0 and max. // If max set to zero or negative, the max will set to MaxInt64. -func RandInt64(maxVal int64) int64 { - return int64(RandUint64(uint64(maxVal))) +func RandInt64(max int64) int64 { + return int64(RandUint64(uint64(max))) } // RandUint64 returns a random uint64 in between 0 and max. // If max set to zero or negative, the max will set to MaxUint64. -func RandUint64(maxVal uint64) uint64 { - if maxVal <= 0 { - maxVal = MaxUint64 +func RandUint64(max uint64) uint64 { + if max <= 0 { + max = MaxUint64 } bigMax := &big.Int{} - bigMax.SetUint64(maxVal) + bigMax.SetUint64(max) bigRnd, _ := crand.Int(crand.Reader, bigMax) return bigRnd.Uint64()