Skip to content

Commit

Permalink
Quality Gate (#61)
Browse files Browse the repository at this point in the history
* add linters

* fix: lint fix

* fix: lint make file

* feat: sonar

* fix: remove unused files

* try to include coverage

* fix: change sonar cloud project key

* small fix

* fix: comments

---------

Co-authored-by: Rachit Sonthalia <rachitsonthalia02@gmail.com>
  • Loading branch information
goran-ethernal and rachit77 authored Sep 4, 2024
1 parent 6e08784 commit 0813aff
Show file tree
Hide file tree
Showing 23 changed files with 423 additions and 607 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/test-unit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
name: Test unit

on:
push:
branches:
- main
- 'release/**'
pull_request:

jobs:
test-unit:
strategy:
fail-fast: false
matrix:
go-version: [ 1.21.x ]
goarch: [ "amd64" ]
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Install Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
env:
GOARCH: ${{ matrix.goarch }}

- name: Test
run: make test-unit

- name: Archive code coverage results
uses: actions/upload-artifact@v3
with:
name: code-coverage-report
path: coverage.out

sonar-cloud:
needs: test-unit
name: SonarCloud
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
with:
submodules: recursive
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis

- name: Download code coverage results
uses: actions/download-artifact@v3
with:
name: code-coverage-report

- name: Analyze with SonarCloud
uses: sonarsource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
37 changes: 29 additions & 8 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,49 @@ run:
timeout: 5m
skip-dirs:
- test


linters:
enable:
- whitespace
- gosec
- whitespace # Tool for detection of leading and trailing whitespace
- gosec # Security problems
- gci
- misspell
- gomnd
- gofmt
- goimports
- misspell # Misspelled English words in comments
- mnd
- gofmt # Whether the code was gofmt-ed
- goimports # Unused imports
- revive
- unconvert
- wastedassign # Finds wasted assignment statements
- unconvert # Unnecessary type conversions
- prealloc # Finds slice declarations that could potentially be pre-allocated
- predeclared # Finds code that shadows one of Go's predeclared identifiers
- nolintlint # Ill-formed or insufficient nolint directives
- makezero # Finds slice declarations with non-zero initial length
- importas # Enforces consistent import aliases
- dogsled # Checks assignments with too many blank identifiers (e.g. x, , , _, := f())
- errname # Checks that sentinel errors are prefixed with the Err and error types are suffixed with the Error
- goconst # Repeated strings that could be replaced by a constant
- forcetypeassert # Finds forced type assertions
- tparallel # Detects inappropriate usage of t.Parallel() method in your Go test codes
- thelper # Detects golang test helpers without t.Helper() call and checks the consistency of test helpers
- errcheck # Errcheck is a go lint rule for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases
- lll # Long lines

linters-settings:
revive:
rules:
- name: exported
arguments:
- disableStutteringCheck
goconst:
min-len: 4
min-occurrences: 3

issues:
exclude-rules:
- path: _test\.go
linters:
- gosec
- lll
include:
- EXC0012 # EXC0012 revive: Annoying issue about not having a comment. The rare codebase has such comments
- EXC0014 # EXC0014 revive: Annoying issue about not having a comment. The rare codebase has such comments
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ install-linter: check-go check-curl

.PHONY: install-linter
install-linter: ## Installs the linter
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin v1.54.2
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin v1.59.1

.PHONY: lint
lint: ## Runs the linter
export "GOROOT=$$(go env GOROOT)" && $$(go env GOPATH)/bin/golangci-lint run

.PHONY: test-unit
test-unit:
trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -short -race -p 1 -covermode=atomic -coverprofile=coverage.out -coverpkg ./... -timeout 200s ./...

## Help display.
## Pulls comments from beside commands and prints a nicely formatted
## display with the commands and their usage information.
Expand Down
8 changes: 8 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package common

const (
// Base10 decimal base
Base10 = 10
// Gwei represents 1000000000 wei
Gwei = 1000000000
)
15 changes: 0 additions & 15 deletions config/types/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package types

import (
"time"

"github.com/invopop/jsonschema"
)

// Duration is a wrapper type that parses time duration from text.
Expand All @@ -25,16 +23,3 @@ func (d *Duration) UnmarshalText(data []byte) error {
func NewDuration(duration time.Duration) Duration {
return Duration{duration}
}

// JSONSchema returns a custom schema to be used for the JSON Schema generation of this type
func (Duration) JSONSchema() *jsonschema.Schema {
return &jsonschema.Schema{
Type: "string",
Title: "Duration",
Description: "Duration expressed in units: [ns, us, ms, s, m, h, d]",
Examples: []interface{}{
"1m",
"300ms",
},
}
}
110 changes: 0 additions & 110 deletions encoding/encoding.go

This file was deleted.

35 changes: 29 additions & 6 deletions etherman/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ func (etherMan *Client) GetLatestBlockNumber(ctx context.Context) (uint64, error
}

// WaitTxToBeMined waits for an L1 tx to be mined. It will return error if the tx is reverted or timeout is exceeded
func (etherMan *Client) WaitTxToBeMined(ctx context.Context, tx *types.Transaction, timeout time.Duration) (bool, error) {
func (etherMan *Client) WaitTxToBeMined(
ctx context.Context,
tx *types.Transaction,
timeout time.Duration,
) (bool, error) {
err := WaitTxToBeMined(ctx, etherMan.EthClient, tx, timeout)
if errors.Is(err, context.DeadlineExceeded) {
return false, nil
Expand Down Expand Up @@ -155,7 +159,13 @@ func (etherMan *Client) SuggestedGasPrice(ctx context.Context) (*big.Int, error)
}

// EstimateGas returns the estimated gas for the tx
func (etherMan *Client) EstimateGas(ctx context.Context, from common.Address, to *common.Address, value *big.Int, data []byte) (uint64, error) {
func (etherMan *Client) EstimateGas(
ctx context.Context,
from common.Address,
to *common.Address,
value *big.Int,
data []byte,
) (uint64, error) {
return etherMan.EthClient.EstimateGas(ctx, ethereum.CallMsg{
From: from,
To: to,
Expand All @@ -165,7 +175,15 @@ func (etherMan *Client) EstimateGas(ctx context.Context, from common.Address, to
}

// EstimateGasBlobTx returns the estimated gas for the blob tx
func (etherMan *Client) EstimateGasBlobTx(ctx context.Context, from common.Address, to *common.Address, gasFeeCap *big.Int, gasTipCap *big.Int, value *big.Int, data []byte) (uint64, error) {
func (etherMan *Client) EstimateGasBlobTx(
ctx context.Context,
from common.Address,
to *common.Address,
gasFeeCap *big.Int,
gasTipCap *big.Int,
value *big.Int,
data []byte,
) (uint64, error) {
return etherMan.EthClient.EstimateGas(ctx, ethereum.CallMsg{
From: from,
To: to,
Expand All @@ -189,7 +207,11 @@ func (etherMan *Client) CheckTxWasMined(ctx context.Context, txHash common.Hash)
}

// SignTx tries to sign a transaction accordingly to the provided sender
func (etherMan *Client) SignTx(ctx context.Context, sender common.Address, tx *types.Transaction) (*types.Transaction, error) {
func (etherMan *Client) SignTx(
ctx context.Context,
sender common.Address,
tx *types.Transaction,
) (*types.Transaction, error) {
auth, err := etherMan.getAuthByAddress(sender)
if err == ErrNotFound {
return nil, ErrPrivateKeyNotFound
Expand Down Expand Up @@ -293,13 +315,14 @@ func (etherMan *Client) getBlockNumber(ctx context.Context, blockNumber rpc.Bloc
return header.Number.Uint64(), nil
}

// GetHeaderByNumber returns a block header from the current canonical chain, if number is nil the latest header is returned
// GetHeaderByNumber returns a block header from the current canonical chain.
// If number is nil the latest header is returned
func (etherMan *Client) GetHeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
header, err := etherMan.EthClient.HeaderByNumber(ctx, number)
return header, err
}

// GetSuggestGasTipCap retrieves the currently suggested gas tip cap after 1559 to allow a timely execution of a transaction
// GetSuggestGasTipCap retrieves the currently suggested gas tip cap after EIP-1559 for timely transaction execution.
func (etherMan *Client) GetSuggestGasTipCap(ctx context.Context) (*big.Int, error) {
gasTipCap, err := etherMan.EthClient.SuggestGasTipCap(ctx)
return gasTipCap, err
Expand Down
9 changes: 5 additions & 4 deletions etherman/etherscan/etherscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"math/big"
"net/http"

"github.com/0xPolygonHermez/zkevm-ethtx-manager/encoding"
"github.com/0xPolygonHermez/zkevm-ethtx-manager/common"
)

type etherscanResponse struct {
Expand All @@ -29,7 +29,8 @@ type gasPriceEtherscan struct {
type Config struct {
// Need API key to use etherscan, if it's empty etherscan is not used
ApiKey string `mapstructure:"ApiKey"`
// URL of the etherscan API. Overwritten with a hardcoded URL: "https://api.etherscan.io/api?module=gastracker&action=gasoracle&apikey="
// URL of the etherscan API. Overwritten with a hardcoded URL:
// "https://api.etherscan.io/api?module=gastracker&action=gasoracle&apikey="
Url string
}

Expand Down Expand Up @@ -72,6 +73,6 @@ func (e *Client) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
if err != nil {
return big.NewInt(0), fmt.Errorf("Reading body failed: %w", err)
}
fgp, _ := big.NewInt(0).SetString(resBody.Result.FastGasPrice, encoding.Base10)
return new(big.Int).Mul(fgp, big.NewInt(encoding.Gwei)), nil
fgp, _ := big.NewInt(0).SetString(resBody.Result.FastGasPrice, common.Base10)
return new(big.Int).Mul(fgp, big.NewInt(common.Gwei)), nil
}
Loading

0 comments on commit 0813aff

Please sign in to comment.