Add linter checks to GHA & Fix code to pass lints

Add linter checks to GHA & Fix code to pass lints.
The linters added are:
- whitespace: Tool for detection of leading and trailing whitespace
- gosec: Inspects source code for security problems
- gci: Gci control golang package import order and make it always deterministic
- misspell: Finds commonly misspelled English words in comments
- gomnd: An analyzer to detect magic numbers

The file utils/utils.go is excluded from the checks of gomnd, as uses magic numbers through the code
This commit is contained in:
arnaucube
2020-08-27 18:10:05 +02:00
parent fe8431edfa
commit fd1e9c25ee
27 changed files with 95 additions and 75 deletions

View File

@@ -12,7 +12,13 @@ import (
cryptoUtils "github.com/iden3/go-iden3-crypto/utils"
)
const NLEAFELEMS = 4
const (
NLEAFELEMS = 4
// maxNonceValue is the maximum value that the Account.Nonce can have (40 bits: maxNonceValue=2**40-1)
maxNonceValue = 0xffffffffff
// maxBalanceBytes is the maximum bytes that can use the Account.Balance *big.Int
maxBalanceBytes = 24
)
// Account is a struct that gives information of the holdings of an address and a specific token. Is the data structure that generates the Value stored in the leaf of the MerkleTree
type Account struct {
@@ -37,10 +43,10 @@ func (a *Account) String() string {
func (a *Account) Bytes() ([32 * NLEAFELEMS]byte, error) {
var b [32 * NLEAFELEMS]byte
if a.Nonce > 0xffffffffff {
if a.Nonce > maxNonceValue {
return b, fmt.Errorf("%s Nonce", ErrNumOverflow)
}
if len(a.Balance.Bytes()) > 24 {
if len(a.Balance.Bytes()) > maxBalanceBytes {
return b, fmt.Errorf("%s Balance", ErrNumOverflow)
}

View File

@@ -8,6 +8,8 @@ import (
ethCommon "github.com/ethereum/go-ethereum/common"
)
const batchNumBytesLen = 4
// Batch is a struct that represents Hermez network batch
type Batch struct {
BatchNum BatchNum `meddler:"batch_num"`
@@ -33,7 +35,7 @@ func (bn BatchNum) Bytes() []byte {
// BatchNumFromBytes returns BatchNum from a []byte
func BatchNumFromBytes(b []byte) (BatchNum, error) {
if len(b) != 4 {
if len(b) != batchNumBytesLen {
return 0, fmt.Errorf("can not parse BatchNumFromBytes, bytes len %d, expected 4", len(b))
}
batchNum := binary.LittleEndian.Uint32(b[:4])

View File

@@ -1,6 +1,6 @@
package common
// Fee is a type that represents the percentage of tokens that will be payed in a transaction
// Fee is a type that represents the percentage of tokens that will be paid in a transaction
// to incentivaise the materialization of it
type Fee float64
@@ -15,5 +15,7 @@ type RecommendedFee struct {
// FeeSelector is used to select a percentage from the FeePlan.
type FeeSelector uint8
const MAXFEEPLAN = 256
// FeePlan represents the fee model, a position in the array indicates the percentage of tokens paid in concept of fee for a transaction
var FeePlan = [256]float64{}
var FeePlan = [MAXFEEPLAN]float64{}

View File

@@ -17,7 +17,7 @@ type Nonce uint64
// Bytes returns a byte array of length 5 representing the Nonce
func (n Nonce) Bytes() ([5]byte, error) {
if n >= 1099511627776 { // 2**40bits
if n > maxNonceValue {
return [5]byte{}, ErrNonceOverflow
}
var nonceBytes [8]byte

View File

@@ -68,7 +68,6 @@ func TestTxCompressedData(t *testing.T) {
// test vector value generated from javascript implementation
assert.Equal(t, "6571340879233176732837827812956721483162819083004853354503", txCompressedData.String())
assert.Equal(t, "10c000000000b0000000a0009000000000008000000000007", hex.EncodeToString(txCompressedData.Bytes())[1:])
}
func TestHashToSign(t *testing.T) {

View File

@@ -16,6 +16,6 @@ type SmartContractParameters struct {
ContractAddr ethCommon.Address // Ethereum address of the rollup smart contract
NLevels uint16 // Heigth of the SMT. This will determine the maximum number of accounts that can coexist in the Hermez network by 2^nLevels
MaxTxs uint16 // Max amount of txs that can be added in a batch, either L1 or L2
FeeL1Tx *big.Int // amount of eth (in wei) that has to be payed to do a L1 tx
FeeDeposit *big.Int // amount of eth (in wei) that has to be payed to do a deposit
FeeL1Tx *big.Int // amount of eth (in wei) that has to be paid to do a L1 tx
FeeDeposit *big.Int // amount of eth (in wei) that has to be paid to do a deposit
}

View File

@@ -4,7 +4,7 @@ import (
"time"
)
// SyncronizerState describes the syncronization progress of the smart contracts
// SyncronizerState describes the synchronization progress of the smart contracts
type SyncronizerState struct {
LastUpdate time.Time // last time this information was updated
CurrentBatchNum BatchNum // Last batch that was forged on the blockchain

View File

@@ -6,6 +6,12 @@ import (
"math/big"
)
const (
idxBytesLen = 4
// maxIdxValue is the maximum value that Idx can have (32 bits: maxIdxValue=2**32-1)
maxIdxValue = 0xffffffff
)
// Idx represents the account Index in the MerkleTree
type Idx uint32
@@ -23,7 +29,7 @@ func (idx Idx) BigInt() *big.Int {
// IdxFromBytes returns Idx from a byte array
func IdxFromBytes(b []byte) (Idx, error) {
if len(b) != 4 {
if len(b) != idxBytesLen {
return 0, fmt.Errorf("can not parse Idx, bytes len %d, expected 4", len(b))
}
idx := binary.LittleEndian.Uint32(b[:4])
@@ -32,7 +38,7 @@ func IdxFromBytes(b []byte) (Idx, error) {
// IdxFromBigInt converts a *big.Int to Idx type
func IdxFromBigInt(b *big.Int) (Idx, error) {
if b.Int64() > 0xffffffff { // 2**32-1
if b.Int64() > maxIdxValue {
return 0, ErrNumOverflow
}
return Idx(uint32(b.Int64())), nil

View File

@@ -19,5 +19,4 @@ func TestIdx(t *testing.T) {
assert.NotNil(t, err)
assert.Equal(t, ErrNumOverflow, err)
assert.Equal(t, Idx(0), i)
}