From 679ba1723ee88faaa306abe1ddafd3b3dbe3a0aa Mon Sep 17 00:00:00 2001 From: Ngalim Siregar Date: Thu, 28 Mar 2024 00:38:34 +0700 Subject: [PATCH] [#545] fix linter error --- curve/curve_test.go | 55 +++++++++++++++++++++++++++++++-------------- rpc/block_test.go | 9 ++++++-- rpc/call_test.go | 8 +++++-- rpc/mock_test.go | 8 +++++-- typed/typed_test.go | 29 ++++++++++++++++-------- utils/keccak.go | 12 +++++++--- 6 files changed, 86 insertions(+), 35 deletions(-) diff --git a/curve/curve_test.go b/curve/curve_test.go index 6964915d..a697545b 100644 --- a/curve/curve_test.go +++ b/curve/curve_test.go @@ -3,6 +3,7 @@ package curve import ( "crypto/elliptic" "fmt" + "log" "math/big" "testing" @@ -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")}, @@ -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) + } }) } } @@ -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 @@ -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) + } } } } @@ -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) @@ -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) @@ -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 { @@ -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 @@ -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 @@ -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 @@ -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 @@ -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") @@ -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), @@ -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 @@ -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 { @@ -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 @@ -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"}, diff --git a/rpc/block_test.go b/rpc/block_test.go index 6a5c45eb..e6aa4300 100644 --- a/rpc/block_test.go +++ b/rpc/block_test.go @@ -2,6 +2,7 @@ package rpc import ( "context" + "log" "strings" "testing" @@ -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) @@ -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 { diff --git a/rpc/call_test.go b/rpc/call_test.go index 9f56a727..a5fd7c62 100644 --- a/rpc/call_test.go +++ b/rpc/call_test.go @@ -2,6 +2,7 @@ package rpc import ( "context" + "log" "testing" "github.com/NethermindEth/juno/core/felt" @@ -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) @@ -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 { diff --git a/rpc/mock_test.go b/rpc/mock_test.go index 359d2ab8..126236a9 100644 --- a/rpc/mock_test.go +++ b/rpc/mock_test.go @@ -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 } @@ -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 } diff --git a/typed/typed_test.go b/typed/typed_test.go index 472972fd..19b6b1e1 100644 --- a/typed/typed_test.go +++ b/typed/typed_test.go @@ -2,6 +2,7 @@ package typed import ( "fmt" + "log" "math/big" "testing" @@ -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) { @@ -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() @@ -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) + } }) } @@ -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() @@ -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() @@ -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() @@ -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")) @@ -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() diff --git a/utils/keccak.go b/utils/keccak.go index 36f49c53..171487e8 100644 --- a/utils/keccak.go +++ b/utils/keccak.go @@ -5,6 +5,7 @@ import ( "encoding/hex" "fmt" "hash" + "log" "math/big" "strings" @@ -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 @@ -151,7 +152,10 @@ 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 } @@ -159,7 +163,9 @@ func Keccak256(data ...[]byte) []byte { // (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 {