Skip to content

Commit

Permalink
[NethermindEth#545] fix linter error
Browse files Browse the repository at this point in the history
  • Loading branch information
nsiregar committed Mar 27, 2024
1 parent b837e65 commit 679ba17
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 35 deletions.
55 changes: 38 additions & 17 deletions curve/curve_test.go
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 @@ -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
9 changes: 7 additions & 2 deletions rpc/block_test.go
Expand Up @@ -2,6 +2,7 @@ package rpc

import (
"context"
"log"
"strings"
"testing"

Expand Down Expand Up @@ -591,7 +592,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 {
log.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 +664,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 {
log.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
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
8 changes: 6 additions & 2 deletions rpc/mock_test.go
Expand Up @@ -214,7 +214,9 @@ func mock_starknet_getTransactionByBlockIdAndIndex(result interface{}, method st
]
}`

json.Unmarshal([]byte(InvokeTxnV1example), r)
if err := json.Unmarshal([]byte(InvokeTxnV1example), r); err != nil {
return err
}
return nil
}

Expand All @@ -239,7 +241,9 @@ func mock_starknet_getBlockTransactionCount(result interface{}, method string, a
if err != nil {
return err
}
json.Unmarshal(outputContent, r)
if err := json.Unmarshal(outputContent, r); err != nil {
return err
}
return nil
}

Expand Down
29 changes: 20 additions & 9 deletions typed/typed_test.go
Expand Up @@ -2,6 +2,7 @@ package typed

import (
"fmt"
"log"
"math/big"
"testing"

Expand Down Expand Up @@ -44,7 +45,9 @@ func (mail Mail) FmtDefinitionEncoding(field string) (fmtEnc []*big.Int) {
// The function returns the generated TypedData object.
//
// Parameters:
// none
//
// none
//
// Returns:
// - ttd: the generated TypedData object
func MockTypedData() (ttd TypedData) {
Expand Down Expand Up @@ -114,7 +117,8 @@ func TestGeneral_GetMessageHash(t *testing.T) {
// Parameters:
// - b: a testing.B object that provides methods for benchmarking the function
// Returns:
// none
//
// none
func BenchmarkGetMessageHash(b *testing.B) {
ttd := MockTypedData()

Expand All @@ -131,7 +135,9 @@ func BenchmarkGetMessageHash(b *testing.B) {
}
addr := utils.HexToBN("0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826")
b.Run(fmt.Sprintf("input_size_%d", addr.BitLen()), func(b *testing.B) {
ttd.GetMessageHash(addr, mail, curve.Curve)
if _, err := ttd.GetMessageHash(addr, mail, curve.Curve); err != nil {
log.Fatal(err)
}
})
}

Expand All @@ -143,7 +149,8 @@ func BenchmarkGetMessageHash(b *testing.B) {
// Parameters:
// - t: a testing.T object that provides methods for testing functions
// Returns:
// none
//
// none
func TestGeneral_GetDomainHash(t *testing.T) {
ttd := MockTypedData()

Expand All @@ -168,7 +175,8 @@ func TestGeneral_GetDomainHash(t *testing.T) {
// Parameters:
// - t: a testing.T object that provides methods for testing functions
// Returns:
// none
//
// none
func TestGeneral_GetTypedMessageHash(t *testing.T) {
ttd := MockTypedData()

Expand Down Expand Up @@ -200,11 +208,12 @@ func TestGeneral_GetTypedMessageHash(t *testing.T) {
// It tests the GetTypeHash function by calling it with different input values
// and comparing the result with expected values. It also checks that the
// encoding of the types matches the expected values.
//
//
// Parameters:
// - t: The testing.T object used for reporting test failures and logging test output
// Returns:
// none
//
// none
func TestGeneral_GetTypeHash(t *testing.T) {
tdd := MockTypedData()

Expand Down Expand Up @@ -250,7 +259,8 @@ func TestGeneral_GetTypeHash(t *testing.T) {
// Parameters:
// - t: The testing.T object used for reporting test failures and logging test output
// Returns:
// none
//
// none
func TestGeneral_GetSelectorFromName(t *testing.T) {
sel1 := utils.BigToHex(utils.GetSelectorFromName("initialize"))
sel2 := utils.BigToHex(utils.GetSelectorFromName("mint"))
Expand All @@ -275,7 +285,8 @@ func TestGeneral_GetSelectorFromName(t *testing.T) {
// Parameters:
// - t: The testing.T object used for reporting test failures and logging test output
// Returns:
// none
//
// none
func TestGeneral_EncodeType(t *testing.T) {
tdd := MockTypedData()

Expand Down
12 changes: 9 additions & 3 deletions utils/keccak.go
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/hex"
"fmt"
"hash"
"log"
"math/big"
"strings"

Expand Down Expand Up @@ -73,7 +74,7 @@ func HexToBN(hexString string) *big.Int {
}

// HexToBytes converts a hexadecimal string to a byte slice.
// trim "0x" prefix(if exists)
// trim "0x" prefix(if exists)
//
// Parameters:
// - hexString: the hexadecimal string to be converted
Expand Down Expand Up @@ -151,15 +152,20 @@ func Keccak256(data ...[]byte) []byte {
for _, b := range data {
d.Write(b)
}
d.Read(b)

if _, err := d.Read(b); err != nil {
log.Fatal(err)
}
return b
}

// NewKeccakState returns a new instance of KeccakState.
// (ref: https://github.com/ethereum/go-ethereum/blob/master/crypto/crypto.go)
//
// Parameters:
// none
//
// none
//
// Returns:
// - KeccakState: a new instance of KeccakState
func NewKeccakState() KeccakState {
Expand Down

0 comments on commit 679ba17

Please sign in to comment.