Skip to content

Commit

Permalink
NethermindEth#545 Add golangci-lint workflow (NethermindEth#546)
Browse files Browse the repository at this point in the history
[NethermindEth#545] Add golangci-lint and fix linter issues

Co-authored-by: Rian Hughes <[email protected]>
  • Loading branch information
nsiregar and rianhughes authored Apr 2, 2024
1 parent 02a8f45 commit 099cbdf
Show file tree
Hide file tree
Showing 17 changed files with 296 additions and 146 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: golangci-lint
on:
push:
tags:
- v*
branches:
- main
pull_request:
permissions:
contents: read
pull-requests: read
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/[email protected]
with:
go-version-file: go.mod
cache: false
- name: Install Jemalloc (Ubuntu or self-hosted)
run: sudo apt-get update -qq && sudo apt-get install -y libjemalloc-dev libjemalloc2 -y
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.56.2
6 changes: 2 additions & 4 deletions account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (account *Account) TransactionHashDeployAccount(tx rpc.DeployAccountType, c
return nil, ErrNotAllParametersSet
}
calldata := []*felt.Felt{txn.ClassHash, txn.ContractAddressSalt}
calldata = append(calldata, txn.ConstructorCalldata...)
calldata = append(calldata, txn.ConstructorCalldata...) //nolint:all

txnVersionFelt, err := new(felt.Felt).SetString(string(txn.Version))
if err != nil {
Expand Down Expand Up @@ -932,9 +932,7 @@ func FmtCallDataCairo0(callArray []rpc.FunctionCall) []*felt.Felt {
calldata = append(calldata, new(felt.Felt).SetUint64(callDataLen))
offset += callDataLen

for _, data := range call.Calldata {
calls = append(calls, data)
}
calls = append(calls, call.Calldata...)
}

calldata = append(calldata, new(felt.Felt).SetUint64(offset))
Expand Down
4 changes: 3 additions & 1 deletion account/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ var (
func TestMain(m *testing.M) {
flag.StringVar(&testEnv, "env", "mock", "set the test environment")
flag.Parse()
godotenv.Load(fmt.Sprintf(".env.%s", testEnv), ".env")
if err := godotenv.Load(fmt.Sprintf(".env.%s", testEnv), ".env"); err != nil {
panic(fmt.Sprint("Failed to load env for ", testEnv))
}
base = os.Getenv("INTEGRATION_BASE")
if base == "" && testEnv != "mock" {
panic(fmt.Sprint("Failed to set INTEGRATION_BASE for ", testEnv))
Expand Down
17 changes: 9 additions & 8 deletions curve/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,14 @@ type StarkCurvePayload struct {
//
// Note: Not all operations require a stark curve initialization including the provided constant points.
// This function can be used to initialize the curve without the constant points.
//
//
// Parameters:
// none
//
// none
//
// Returns:
// none
//
// none
func init() {
if err := json.Unmarshal(PedersenParamsRaw, &PedersenParams); err != nil {
log.Fatalf("unmarshalling pedersen params: %v", err)
Expand Down Expand Up @@ -200,7 +203,7 @@ func (sc StarkCurve) ScalarMult(x1, y1 *big.Int, k []byte) (x, y *big.Int) {
return x, y
}

// ScalarBaseMult returns the result of multiplying the base point of the StarkCurve
// ScalarBaseMult returns the result of multiplying the base point of the StarkCurve
// by the given scalar value.
//
// Parameters:
Expand Down Expand Up @@ -493,8 +496,6 @@ func (sc StarkCurve) Sign(msgHash, privKey *big.Int, seed ...*big.Int) (x, y *bi
s := sc.InvModCurveSize(w)
return r, s, nil
}

return x, y, nil
}

// SignFelt signs a message hash with a private key using the StarkCurve.
Expand Down Expand Up @@ -721,8 +722,8 @@ func (sc StarkCurve) GetRandomPrivateKey() (priv *big.Int, err error) {

// PrivateToPoint generates a point on the StarkCurve from a private key.
//
// It takes a private key as a parameter and returns the x and y coordinates of
// the generated point on the curve. If the private key is not within the range
// It takes a private key as a parameter and returns the x and y coordinates of
// the generated point on the curve. If the private key is not within the range
// of the curve, it returns an error.
//
// Parameters:
Expand Down
57 changes: 39 additions & 18 deletions curve/curve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package curve
import (
"crypto/elliptic"
"fmt"
"log"
"math/big"
"testing"

Expand All @@ -17,7 +18,8 @@ import (
// Parameters:
// - b: a *testing.B value representing the testing context
// Returns:
// none
//
// none
func BenchmarkPedersenHash(b *testing.B) {
suite := [][]*big.Int{
{utils.HexToBN("0x12773"), utils.HexToBN("0x872362")},
Expand All @@ -31,7 +33,9 @@ func BenchmarkPedersenHash(b *testing.B) {

for _, test := range suite {
b.Run(fmt.Sprintf("input_size_%d_%d", test[0].BitLen(), test[1].BitLen()), func(b *testing.B) {
Curve.PedersenHash(test)
if _, err := Curve.PedersenHash(test); err != nil {
log.Fatal(err)
}
})
}
}
Expand All @@ -41,7 +45,8 @@ func BenchmarkPedersenHash(b *testing.B) {
// Parameters:
// - b: a *testing.B value representing the testing context
// Returns:
// none
//
// none
func BenchmarkCurveSign(b *testing.B) {
type data struct {
MessageHash *big.Int
Expand All @@ -61,7 +66,9 @@ func BenchmarkCurveSign(b *testing.B) {
})

for _, test := range dataSet {
Curve.Sign(test.MessageHash, test.PrivateKey, test.Seed)
if _, _, err := Curve.Sign(test.MessageHash, test.PrivateKey, test.Seed); err != nil {
log.Fatal(err)
}
}
}
}
Expand All @@ -79,7 +86,8 @@ func BenchmarkCurveSign(b *testing.B) {
// Parameters:
// - b: a *testing.B value representing the testing context
// Returns:
// none
//
// none
func BenchmarkSignatureVerify(b *testing.B) {
private, _ := Curve.GetRandomPrivateKey()
x, y, _ := Curve.PrivateToPoint(private)
Expand All @@ -93,7 +101,9 @@ func BenchmarkSignatureVerify(b *testing.B) {
r, s, _ := Curve.Sign(hash, private)

b.Run(fmt.Sprintf("sign_input_size_%d", hash.BitLen()), func(b *testing.B) {
Curve.Sign(hash, private)
if _, _, err := Curve.Sign(hash, private); err != nil {
log.Fatal(err)
}
})
b.Run(fmt.Sprintf("verify_input_size_%d", hash.BitLen()), func(b *testing.B) {
Curve.Verify(hash, r, s, x, y)
Expand All @@ -105,7 +115,8 @@ func BenchmarkSignatureVerify(b *testing.B) {
// Parameters:
// - t: a *testing.T value representing the testing context
// Returns:
// none
//
// none
func TestGeneral_PrivateToPoint(t *testing.T) {
x, _, err := Curve.PrivateToPoint(big.NewInt(2))
if err != nil {
Expand All @@ -125,7 +136,8 @@ func TestGeneral_PrivateToPoint(t *testing.T) {
// Parameters:
// - t: a *testing.T value representing the testing context
// Returns:
// none
//
// none
func TestGeneral_PedersenHash(t *testing.T) {
testPedersen := []struct {
elements []*big.Int
Expand Down Expand Up @@ -167,7 +179,8 @@ func TestGeneral_PedersenHash(t *testing.T) {
// Parameters:
// - t: a *testing.T value representing the testing context
// Returns:
// none
//
// none
func TestGeneral_DivMod(t *testing.T) {
testDivmod := []struct {
x *big.Int
Expand Down Expand Up @@ -205,7 +218,8 @@ func TestGeneral_DivMod(t *testing.T) {
// Parameters:
// - t: a *testing.T value representing the testing context
// Returns:
// none
//
// none
func TestGeneral_Add(t *testing.T) {
testAdd := []struct {
x *big.Int
Expand Down Expand Up @@ -251,7 +265,8 @@ func TestGeneral_Add(t *testing.T) {
// Parameters:
// - t: a *testing.T value representing the testing context
// Returns:
// none
//
// none
func TestGeneral_MultAir(t *testing.T) {
testMult := []struct {
r *big.Int
Expand Down Expand Up @@ -294,7 +309,8 @@ func TestGeneral_MultAir(t *testing.T) {
// Parameters:
// - t: a *testing.T value representing the testing context
// Returns:
// none
//
// none
func TestGeneral_ComputeHashOnElements(t *testing.T) {
hashEmptyArray, err := Curve.ComputeHashOnElements([]*big.Int{})
expectedHashEmmptyArray := utils.HexToBN("0x49ee3eba8c1600700ee1b87eb599f16716b0b1022947733551fde4050ca6804")
Expand Down Expand Up @@ -325,7 +341,8 @@ func TestGeneral_ComputeHashOnElements(t *testing.T) {
// Parameters:
// - t: The testing.T object for running the test.
// Returns:
// none
//
// none
func TestGeneral_HashAndSign(t *testing.T) {
hashy, err := Curve.HashElements([]*big.Int{
big.NewInt(1953658213),
Expand Down Expand Up @@ -362,7 +379,8 @@ func TestGeneral_HashAndSign(t *testing.T) {
// Parameters:
// - t: The testing.T object for running the test
// Returns:
// none
//
// none
func TestGeneral_ComputeFact(t *testing.T) {
testFacts := []struct {
programHash *big.Int
Expand Down Expand Up @@ -394,7 +412,8 @@ func TestGeneral_ComputeFact(t *testing.T) {
// Parameters:
// - t: The testing.T object for running the test
// Returns:
// none
//
// none
func TestGeneral_BadSignature(t *testing.T) {
hash, err := Curve.PedersenHash([]*big.Int{utils.HexToBN("0x12773"), utils.HexToBN("0x872362")})
if err != nil {
Expand Down Expand Up @@ -439,7 +458,8 @@ func TestGeneral_BadSignature(t *testing.T) {
// Parameters:
// - t: The testing.T object for running the test
// Returns:
// none
//
// none
func TestGeneral_Signature(t *testing.T) {
testSignature := []struct {
private *big.Int
Expand Down Expand Up @@ -476,7 +496,7 @@ func TestGeneral_Signature(t *testing.T) {
for _, tt := range testSignature {
if tt.raw != "" {
h, _ := utils.HexToBytes(tt.raw)
tt.publicX, tt.publicY = elliptic.Unmarshal(Curve, h)
tt.publicX, tt.publicY = elliptic.Unmarshal(Curve, h) //nolint:all
} else if tt.private != nil {
tt.publicX, tt.publicY, err = Curve.PrivateToPoint(tt.private)
if err != nil {
Expand Down Expand Up @@ -507,7 +527,8 @@ func TestGeneral_Signature(t *testing.T) {
// Parameters:
// - t: The testing.T object for running the test
// Returns:
// none
//
// none
func TestGeneral_SplitFactStr(t *testing.T) {
data := []map[string]string{
{"input": "0x3", "h": "0x0", "l": "0x3"},
Expand Down
5 changes: 3 additions & 2 deletions devnet/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const (
)

// testConfiguration is a type that is used to configure tests
type testConfiguration struct {
type testConfiguration struct { //nolint:golint,unused
base string
}

Expand All @@ -27,7 +27,8 @@ var (
// Parameters:
// - m: is the testing.M parameter
// Returns:
// none
//
// none
func TestMain(m *testing.M) {
flag.StringVar(&testEnv, "env", "devnet", "set the test environment")
flag.Parse()
Expand Down
8 changes: 6 additions & 2 deletions rpc/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,9 @@ func TestBlockWithTxsAndDeployOrDeclare(t *testing.T) {
t.Fatal("expecting to match", err)
}
if diff != "FullMatch" {
spy.Compare(blockWithTxs, false)
if _, err := spy.Compare(blockWithTxs, false); err != nil {
t.Fatal(err)
}
}
if !strings.HasPrefix(blockWithTxs.BlockHash.String(), "0x") {
t.Fatal("Block Hash should start with \"0x\", instead", blockWithTxs.BlockHash)
Expand Down Expand Up @@ -661,7 +663,9 @@ func TestBlockTransactionCount(t *testing.T) {
t.Fatal("expecting to match", err)
}
if diff != "FullMatch" {
spy.Compare(count, true)
if _, err := spy.Compare(count, true); err != nil {
t.Fatal(err)
}
t.Fatal("structure expecting to be FullMatch, instead", diff)
}
if count != test.ExpectedCount {
Expand Down
8 changes: 6 additions & 2 deletions rpc/call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rpc

import (
"context"
"log"
"testing"

"github.com/NethermindEth/juno/core/felt"
Expand All @@ -22,7 +23,8 @@ import (
// Parameters:
// - t: the testing object for running the test cases
// Returns:
// none
//
// none
func TestCall(t *testing.T) {
testConfig := beforeEach(t)

Expand Down Expand Up @@ -87,7 +89,9 @@ func TestCall(t *testing.T) {
t.Fatal(err)
}
if diff, err := spy.Compare(output, false); err != nil || diff != "FullMatch" {
spy.Compare(output, true)
if _, err := spy.Compare(output, true); err != nil {
log.Fatal(err)
}
t.Fatal("expecting to match", err)
}
if len(output) == 0 {
Expand Down
Loading

0 comments on commit 099cbdf

Please sign in to comment.