Skip to content

Commit

Permalink
fix: linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Ja7ad committed Sep 23, 2024
1 parent c0def7e commit 6fc8dcb
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 87 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ linters:
- errname
- errorlint
- exhaustive
- exportloopref
- copyloopvar
- forbidigo
- gci
- ginkgolinter
Expand Down
8 changes: 4 additions & 4 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func PromptConfirm(label string) bool {
}
result, err := prompt.Run()
if err != nil {
if !errors.Is(promptui.ErrAbort, err) {
if !errors.Is(err, promptui.ErrAbort) {
PrintErrorMsgf("prompt error: %v", err)
} else {
PrintWarnMsgf("Aborted.")
Expand Down Expand Up @@ -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, min, max int) int {
func PromptInputWithRange(label string, def, minVal, maxVal int) int {
prompt := promptui.Prompt{
Label: label,
Default: fmt.Sprintf("%v", def),
Expand All @@ -161,8 +161,8 @@ func PromptInputWithRange(label string, def, min, max int) int {
if err != nil {
return err
}
if num < min || num > max {
return fmt.Errorf("enter a number between %v and %v", min, max)
if num < minVal || num > maxVal {
return fmt.Errorf("enter a number between %v and %v", minVal, maxVal)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion crypto/hash/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"

"golang.org/x/crypto/blake2b"
"golang.org/x/crypto/ripemd160" //nolint:staticcheck // use to hash the public key to get the address
"golang.org/x/crypto/ripemd160" //nolint:gosec,staticcheck // use to hash the public key to get the address
)

const HashSize = 32
Expand Down
12 changes: 6 additions & 6 deletions sortition/vrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ 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, max uint64) (uint64, Proof) {
func Evaluate(seed VerifiableSeed, prv *bls.PrivateKey, maxVal uint64) (uint64, Proof) {
signData := make([]byte, 0, bls.SignatureSize+bls.PublicKeySize)
signData = append(signData, seed[:]...)
signData = append(signData, prv.PublicKey().Bytes()...)

sig := prv.Sign(signData)

proof, _ := ProofFromBytes(sig.Bytes())
index := GetIndex(proof, max)
index := GetIndex(proof, maxVal)

return index, proof
}

// 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, max uint64) (uint64, bool) {
func Verify(seed VerifiableSeed, pub *bls.PublicKey, proof Proof, maxVal uint64) (uint64, bool) {
proofSig, err := bls.SignatureFromBytes(proof[:])
if err != nil {
return 0, false
Expand All @@ -47,12 +47,12 @@ func Verify(seed VerifiableSeed, pub *bls.PublicKey, proof Proof, max uint64) (u
return 0, false
}

index := GetIndex(proof, max)
index := GetIndex(proof, maxVal)

return index, true
}

func GetIndex(proof Proof, max uint64) uint64 {
func GetIndex(proof Proof, maxVal uint64) uint64 {
h := hash.CalcHash(proof[:])

// construct the numerator and denominator for normalizing the proof uint
Expand All @@ -61,7 +61,7 @@ func GetIndex(proof Proof, max uint64) uint64 {
numerator := &big.Int{}

bigRnd.SetBytes(h.Bytes())
bigMax.SetUint64(max)
bigMax.SetUint64(maxVal)

numerator = numerator.Mul(bigRnd, bigMax)

Expand Down
8 changes: 4 additions & 4 deletions sortition/vrf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ func TestVRF(t *testing.T) {
seed := ts.RandSeed()
t.Logf("seed is: %x \n", seed)

max := uint64(1 * 1e6)
index, proof := sortition.Evaluate(seed, valKey.PrivateKey(), max)
maxVal := uint64(1 * 1e6)
index, proof := sortition.Evaluate(seed, valKey.PrivateKey(), maxVal)

assert.LessOrEqual(t, index, max)
assert.LessOrEqual(t, index, maxVal)

index2, result := sortition.Verify(seed, pk, proof, max)
index2, result := sortition.Verify(seed, pk, proof, maxVal)

assert.True(t, result)
assert.Equal(t, index, index2)
Expand Down
4 changes: 2 additions & 2 deletions sync/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (b *Bundle) Decode(r io.Reader) (int, error) {
err := d.Decode(&bdl)
bytesRead := d.NumBytesRead()
if err != nil {
return bytesRead, errors.Errorf(errors.ErrInvalidMessage, err.Error())
return bytesRead, errors.Errorf(errors.ErrInvalidMessage, "%s", err.Error())
}

data := bdl.MessageData
Expand All @@ -96,7 +96,7 @@ func (b *Bundle) Decode(r io.Reader) (int, error) {
if util.IsFlagSet(bdl.Flags, BundleFlagCompressed) {
c, err := util.DecompressBuffer(bdl.MessageData)
if err != nil {
return bytesRead, errors.Errorf(errors.ErrInvalidMessage, err.Error())
return bytesRead, errors.Errorf(errors.ErrInvalidMessage, "%s", err.Error())
}
data = c
}
Expand Down
4 changes: 2 additions & 2 deletions sync/firewall/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (f *Firewall) decodeBundle(r io.Reader, pid peer.ID) (*bundle.Bundle, error
bdl := new(bundle.Bundle)
bytesRead, err := bdl.Decode(r)
if err != nil {
return nil, errors.Errorf(errors.ErrInvalidMessage, err.Error())
return nil, errors.Errorf(errors.ErrInvalidMessage, "%s", err.Error())
}
f.peerSet.IncreaseReceivedBytesCounter(pid, bdl.Message.Type(), int64(bytesRead))

Expand All @@ -161,7 +161,7 @@ func (f *Firewall) decodeBundle(r io.Reader, pid peer.ID) (*bundle.Bundle, error

func (f *Firewall) checkBundle(bdl *bundle.Bundle) error {
if err := bdl.BasicCheck(); err != nil {
return errors.Errorf(errors.ErrInvalidMessage, err.Error())
return errors.Errorf(errors.ErrInvalidMessage, "%s", err.Error())
}

switch f.state.Genesis().ChainType() {
Expand Down
18 changes: 9 additions & 9 deletions util/encoding/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestElementEncoding(t *testing.T) {
func TestElementEncodingErrors(t *testing.T) {
tests := []struct {
in any // Value to encode
max int // Max size of fixed buffer to induce errors
maxVal int // Max size of fixed buffer to induce errors
writeErr error // Expected write error
readErr error // Expected read error
}{
Expand All @@ -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.max)
w := util.NewFixedWriter(test.maxVal)
err := WriteElement(w, test.in)
assert.ErrorIs(t, err, test.writeErr, "writeElement #%d", i)

r := util.NewFixedReader(test.max, nil)
r := util.NewFixedReader(test.maxVal, nil)
val := test.in
if reflect.ValueOf(test.in).Kind() != reflect.Ptr {
val = reflect.New(reflect.TypeOf(test.in)).Interface()
Expand Down Expand Up @@ -176,7 +176,7 @@ func TestVarStringEncodingErrors(t *testing.T) {
tests := []struct {
in string // Value to encode
buf []byte // Encoding bytes
max int // Max size of fixed buffer to induce errors
maxVal int // Max size of fixed buffer to induce errors
writeErr error // Expected write error
readErr error // Expected read error
}{
Expand All @@ -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.max)
w := util.NewFixedWriter(test.maxVal)
err := WriteVarString(w, test.in)
assert.ErrorIs(t, err, test.writeErr, "WriteVarString #%d", i)

r := util.NewFixedReader(test.max, test.buf)
r := util.NewFixedReader(test.maxVal, test.buf)
_, err = ReadVarString(r)
assert.ErrorIs(t, err, test.readErr, "ReadVarString #%d wrong", i)
}
Expand Down Expand Up @@ -264,7 +264,7 @@ func TestVarBytesEncodingErrors(t *testing.T) {
tests := []struct {
in []byte // Byte Array to write
buf []byte // Encoding bytes
max int // Max size of fixed buffer to induce errors
maxVal int // Max size of fixed buffer to induce errors
writeErr error // Expected write error
readErr error // Expected read error
}{
Expand All @@ -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.max)
w := util.NewFixedWriter(test.maxVal)
err := WriteVarBytes(w, test.in)
assert.ErrorIs(t, err, test.writeErr, "WriteVarBytes #%d", i)

r := util.NewFixedReader(test.max, test.buf)
r := util.NewFixedReader(test.maxVal, test.buf)
_, err = ReadVarBytes(r)
assert.ErrorIs(t, err, test.readErr, "ReadVarBytes #%d", i)
}
Expand Down
4 changes: 2 additions & 2 deletions util/errors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func TestMessages(t *testing.T) {

func TestErrorCode(t *testing.T) {
err1 := Error(ErrInvalidAmount)
err2 := Errorf(ErrInvalidTx, err1.Error())
err3 := Errorf(ErrInvalidBlock, err1.Error())
err2 := Errorf(ErrInvalidTx, "%s", err1.Error())
err3 := Errorf(ErrInvalidBlock, "%s", err1.Error())

assert.Equal(t, ErrInvalidTx, Code(err2))
assert.Equal(t, ErrInvalidBlock, Code(err3))
Expand Down
8 changes: 4 additions & 4 deletions util/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(max int) *FixedWriter {
b := make([]byte, max)
func NewFixedWriter(maxVal int) *FixedWriter {
b := make([]byte, maxVal)
fw := FixedWriter{b, 0}

return &fw
Expand Down Expand Up @@ -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(max int, buf []byte) *FixedReader {
b := make([]byte, max)
func NewFixedReader(maxVal int, buf []byte) *FixedReader {
b := make([]byte, maxVal)
if buf != nil {
copy(b, buf)
}
Expand Down
70 changes: 35 additions & 35 deletions util/testsuite/testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,83 +68,83 @@ func (ts *TestSuite) RandBool() bool {
}

// RandInt8 returns a random int8 between 0 and max: [0, max).
func (ts *TestSuite) RandInt8(max int8) int8 {
return int8(ts.RandUint64(uint64(max)))
func (ts *TestSuite) RandInt8(maxVal int8) int8 {
return int8(ts.RandUint64(uint64(maxVal)))
}

// RandUint8 returns a random uint8 between 0 and max: [0, max).
func (ts *TestSuite) RandUint8(max uint8) uint8 {
return uint8(ts.RandUint64(uint64(max)))
func (ts *TestSuite) RandUint8(maxVal uint8) uint8 {
return uint8(ts.RandUint64(uint64(maxVal)))
}

// RandInt16 returns a random int16 between 0 and max: [0, max).
func (ts *TestSuite) RandInt16(max int16) int16 {
return int16(ts.RandUint64(uint64(max)))
func (ts *TestSuite) RandInt16(maxVal int16) int16 {
return int16(ts.RandUint64(uint64(maxVal)))
}

// RandUint16 returns a random uint16 between 0 and max: [0, max).
func (ts *TestSuite) RandUint16(max uint16) uint16 {
return uint16(ts.RandUint64(uint64(max)))
func (ts *TestSuite) RandUint16(maxVal uint16) uint16 {
return uint16(ts.RandUint64(uint64(maxVal)))
}

// RandInt32 returns a random int32 between 0 and max: [0, max).
func (ts *TestSuite) RandInt32(max int32) int32 {
return int32(ts.RandUint64(uint64(max)))
func (ts *TestSuite) RandInt32(maxVal int32) int32 {
return int32(ts.RandUint64(uint64(maxVal)))
}

// RandUint32 returns a random uint32 between 0 and max: [0, max).
func (ts *TestSuite) RandUint32(max uint32) uint32 {
return uint32(ts.RandUint64(uint64(max)))
func (ts *TestSuite) RandUint32(maxVal uint32) uint32 {
return uint32(ts.RandUint64(uint64(maxVal)))
}

// RandInt64 returns a random int64 between 0 and max: [0, max).
func (ts *TestSuite) RandInt64(max int64) int64 {
return ts.Rand.Int63n(max)
func (ts *TestSuite) RandInt64(maxVal int64) int64 {
return ts.Rand.Int63n(maxVal)
}

// RandUint64 returns a random uint64 between 0 and max: [0, max).
func (ts *TestSuite) RandUint64(max uint64) uint64 {
return uint64(ts.RandInt64(int64(max)))
func (ts *TestSuite) RandUint64(maxVal uint64) uint64 {
return uint64(ts.RandInt64(int64(maxVal)))
}

// RandInt returns a random int between 0 and max: [0, max).
func (ts *TestSuite) RandInt(max int) int {
return int(ts.RandInt64(int64(max)))
func (ts *TestSuite) RandInt(maxVal int) int {
return int(ts.RandInt64(int64(maxVal)))
}

// RandInt16NonZero returns a random int16 between 1 and max+1: [1, max+1).
func (ts *TestSuite) RandInt16NonZero(max int16) int16 {
return ts.RandInt16(max) + 1
func (ts *TestSuite) RandInt16NonZero(maxVal int16) int16 {
return ts.RandInt16(maxVal) + 1
}

// RandUint16NonZero returns a random uint16 between 1 and max+1: [1, max+1).
func (ts *TestSuite) RandUint16NonZero(max uint16) uint16 {
return ts.RandUint16(max) + 1
func (ts *TestSuite) RandUint16NonZero(maxVal uint16) uint16 {
return ts.RandUint16(maxVal) + 1
}

// RandInt32NonZero returns a random int32 between 1 and max+1: [1, max+1).
func (ts *TestSuite) RandInt32NonZero(max int32) int32 {
return ts.RandInt32(max) + 1
func (ts *TestSuite) RandInt32NonZero(maxVal int32) int32 {
return ts.RandInt32(maxVal) + 1
}

// RandUint32NonZero returns a random uint32 between 1 and max+1: [1, max+1).
func (ts *TestSuite) RandUint32NonZero(max uint32) uint32 {
return ts.RandUint32(max) + 1
func (ts *TestSuite) RandUint32NonZero(maxVal uint32) uint32 {
return ts.RandUint32(maxVal) + 1
}

// RandInt64NonZero returns a random int64 between 1 and max+1: [1, max+1).
func (ts *TestSuite) RandInt64NonZero(max int64) int64 {
return ts.RandInt64(max) + 1
func (ts *TestSuite) RandInt64NonZero(maxVal int64) int64 {
return ts.RandInt64(maxVal) + 1
}

// RandUint64NonZero returns a random uint64 between 1 and max+1: [1, max+1).
func (ts *TestSuite) RandUint64NonZero(max uint64) uint64 {
return ts.RandUint64(max) + 1
func (ts *TestSuite) RandUint64NonZero(maxVal uint64) uint64 {
return ts.RandUint64(maxVal) + 1
}

// RandIntNonZero returns a random int between 1 and max+1: [1, max+1).
func (ts *TestSuite) RandIntNonZero(max int) int {
return ts.RandInt(max) + 1
func (ts *TestSuite) RandIntNonZero(maxVal int) int {
return ts.RandInt(maxVal) + 1
}

// RandHeight returns a random number between [1000, 1000000] for block height.
Expand All @@ -163,10 +163,10 @@ func (ts *TestSuite) RandAmount() amount.Amount {
}

// RandAmountRange returns a random amount between [min, max).
func (ts *TestSuite) RandAmountRange(min, max amount.Amount) amount.Amount {
amt := amount.Amount(ts.RandInt64NonZero(int64(max - min)))
func (ts *TestSuite) RandAmountRange(minVal, maxVal amount.Amount) amount.Amount {
amt := amount.Amount(ts.RandInt64NonZero(int64(maxVal - minVal)))

return amt + min
return amt + minVal
}

// RandFee returns a random fee between [0, 1).
Expand Down
Loading

0 comments on commit 6fc8dcb

Please sign in to comment.