Skip to content

Commit

Permalink
chore: linting with ci integration (#86)
Browse files Browse the repository at this point in the history
* lint error handling

* Add linter to ci

* Update Linter Settings

* Attest w/ Timeout

* Lint, format, log

* Add Req Timeout to Metrics

* Validate Attestation URL
  • Loading branch information
joelsmith-2019 authored Apr 24, 2024
1 parent 9f4cf29 commit 61ff2d1
Show file tree
Hide file tree
Showing 43 changed files with 460 additions and 275 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Lint
on:
push:
branches:
- main
paths:
- "**/*.go"
- "go.mod"
- "go.sum"
- "**/go.mod"
- "**/go.sum"
pull_request:
paths:
- "**/*.go"
- "go.mod"
- "go.sum"
- "**/go.mod"
- "**/go.sum"
merge_group:
permissions:
contents: read
jobs:
golangci:
name: golangci-lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: "1.21"
check-latest: true
- name: run linting
run: |
make lint
65 changes: 65 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
run:
timeout: 10m
tests: true

linters:
disable-all: true
enable:
- asciicheck
- bidichk
- bodyclose
- decorder
- dupl
- dupword
- errcheck
- errchkjson
- errname
- exhaustive
- exportloopref
- forbidigo
- gci
- goconst
- gocritic
- gofmt
- gosec
- gosimple
- gosmopolitan
- govet
- grouper
- ineffassign
- loggercheck
- misspell
- nilerr
- nilnil
- noctx
- stylecheck
- testifylint
- thelper
- tparallel
- typecheck
- unconvert
- unparam
- unused
- usestdlibvars
- wastedassign
- whitespace

linters-settings:
gci:
custom-order: true
sections:
- standard # Standard section: captures all standard packages.
- default # Default section: contains all imports that could not be matched to another section type.
- blank # blank imports
- dot # dot imports
- prefix(github.com/cometbft/cometbft)
- prefix(github.com/cosmos)
- prefix(github.com/cosmos/cosmos-sdk)
- prefix(cosmossdk.io)
- prefix(github.com/strangelove-ventures/noble-cctp-relayer)
gosec:
excludes:
- G404

issues:
max-issues-per-linter: 0
22 changes: 11 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ GOBIN := $(GOPATH)/bin
###############################################################################
### Formatting & Linting ###
###############################################################################
.PHONY: format lint
.PHONY: lint lint-fix

gofumpt_cmd=mvdan.cc/gofumpt
golangci_lint_cmd=github.com/golangci/golangci-lint/cmd/golangci-lint

format:
@echo "🤖 Running formatter..."
@go run $(gofumpt_cmd) -l -w .
@echo "✅ Completed formatting!"
golangci_lint_cmd=golangci-lint
golangci_version=v1.57.2

lint:
@echo "🤖 Running linter..."
@go run $(golangci_lint_cmd) run --timeout=10m
@echo "✅ Completed linting!"
@echo "--> Running linter"
@go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(golangci_version)
@$(golangci_lint_cmd) run ./... --timeout 15m

lint-fix:
@echo "--> Running linter and fixing issues"
@go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(golangci_version)
@$(golangci_lint_cmd) run ./... --fix --timeout 15m


###############################################################################
Expand Down
30 changes: 26 additions & 4 deletions circle/attestation.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,49 @@
package circle

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"

"cosmossdk.io/log"

"github.com/strangelove-ventures/noble-cctp-relayer/types"
)

// CheckAttestation checks the iris api for attestation status and returns true if attestation is complete
func CheckAttestation(attestationURL string, logger log.Logger, irisLookupId string, txHash string, sourceDomain, destDomain types.Domain) *types.AttestationResponse {
logger.Debug(fmt.Sprintf("Checking attestation for %s%s%s for source tx %s from %d to %d", attestationURL, "0x", irisLookupId, txHash, sourceDomain, destDomain))
func CheckAttestation(attestationURL string, logger log.Logger, irisLookupID string, txHash string, sourceDomain, destDomain types.Domain) *types.AttestationResponse {
logger.Debug(fmt.Sprintf("Checking attestation for %s%s%s for source tx %s from %d to %d", attestationURL, "0x", irisLookupID, txHash, sourceDomain, destDomain))

client := http.Client{Timeout: 2 * time.Second}

rawResponse, err := client.Get(attestationURL + "0x" + irisLookupId)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

// append ending / if not present
if attestationURL[len(attestationURL)-1:] != "/" {
attestationURL += "/"
}

// add 0x prefix if not present
if len(irisLookupID) > 2 && irisLookupID[:2] != "0x" {
irisLookupID = "0x" + irisLookupID
}

req, err := http.NewRequestWithContext(ctx, http.MethodGet, attestationURL+irisLookupID, nil)
if err != nil {
logger.Debug("error creating request: " + err.Error())
return nil
}

rawResponse, err := client.Do(req)
if err != nil {
logger.Debug("error during request: " + err.Error())
return nil
}
defer rawResponse.Body.Close()
if rawResponse.StatusCode != http.StatusOK {
logger.Debug("non 200 response received from Circles attestation API")
return nil
Expand All @@ -38,7 +60,7 @@ func CheckAttestation(attestationURL string, logger log.Logger, irisLookupId str
logger.Debug("unable to unmarshal response")
return nil
}
logger.Info(fmt.Sprintf("Attestation found for %s%s%s", attestationURL, "0x", irisLookupId))
logger.Info(fmt.Sprintf("Attestation found for %s%s%s", attestationURL, "0x", irisLookupID))

return &response
}
29 changes: 24 additions & 5 deletions circle/attestation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,47 @@ import (
"os"
"testing"

"cosmossdk.io/log"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"

"cosmossdk.io/log"

"github.com/strangelove-ventures/noble-cctp-relayer/circle"
"github.com/strangelove-ventures/noble-cctp-relayer/types"
"github.com/stretchr/testify/require"
)

var cfg types.Config
var logger log.Logger

func init() {
cfg.Circle.AttestationBaseUrl = "https://iris-api-sandbox.circle.com/attestations/"
cfg.Circle.AttestationBaseURL = "https://iris-api-sandbox.circle.com/attestations/"
logger = log.NewLogger(os.Stdout, log.LevelOption(zerolog.ErrorLevel))
}

func TestAttestationIsReady(t *testing.T) {
resp := circle.CheckAttestation(cfg.Circle.AttestationBaseUrl, logger, "85bbf7e65a5992e6317a61f005e06d9972a033d71b514be183b179e1b47723fe", "", 0, 4)
resp := circle.CheckAttestation(cfg.Circle.AttestationBaseURL, logger, "85bbf7e65a5992e6317a61f005e06d9972a033d71b514be183b179e1b47723fe", "", 0, 4)
require.NotNil(t, resp)
require.Equal(t, "complete", resp.Status)
}

func TestAttestationNotFound(t *testing.T) {
resp := circle.CheckAttestation(cfg.Circle.AttestationBaseUrl, logger, "not an attestation", "", 0, 4)
resp := circle.CheckAttestation(cfg.Circle.AttestationBaseURL, logger, "not an attestation", "", 0, 4)
require.Nil(t, resp)
}

func TestAttestationWithoutEndingSlash(t *testing.T) {
startURL := cfg.Circle.AttestationBaseURL
cfg.Circle.AttestationBaseURL = startURL[:len(startURL)-1]

resp := circle.CheckAttestation(cfg.Circle.AttestationBaseURL, logger, "85bbf7e65a5992e6317a61f005e06d9972a033d71b514be183b179e1b47723fe", "", 0, 4)
require.NotNil(t, resp)
require.Equal(t, "complete", resp.Status)

cfg.Circle.AttestationBaseURL = startURL
}

func TestAttestationWithLeading0x(t *testing.T) {
resp := circle.CheckAttestation(cfg.Circle.AttestationBaseURL, logger, "0x85bbf7e65a5992e6317a61f005e06d9972a033d71b514be183b179e1b47723fe", "", 0, 4)
require.NotNil(t, resp)
require.Equal(t, "complete", resp.Status)
}
6 changes: 4 additions & 2 deletions cmd/appstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package cmd
import (
"os"

"cosmossdk.io/log"
"github.com/rs/zerolog"

"cosmossdk.io/log"

"github.com/strangelove-ventures/noble-cctp-relayer/types"
)

Expand Down Expand Up @@ -73,7 +75,7 @@ func (a *AppState) loadConfigFile() {

// validateConfig checks the AppState Config for any invalid settings.
func (a *AppState) validateConfig() {
if a.Config.Circle.AttestationBaseUrl == "" {
if a.Config.Circle.AttestationBaseURL == "" {
a.Logger.Error("AttestationBaseUrl is required in the config")
os.Exit(1)
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"strings"

"github.com/spf13/cobra"
"gopkg.in/yaml.v2"

"github.com/strangelove-ventures/noble-cctp-relayer/ethereum"
"github.com/strangelove-ventures/noble-cctp-relayer/noble"
"github.com/strangelove-ventures/noble-cctp-relayer/types"
"gopkg.in/yaml.v2"
)

// Command for printing current configuration
Expand All @@ -26,7 +27,6 @@ func configShowCmd(a *AppState) *cobra.Command {
$ %s show-config --config %s
$ %s sc`, appName, defaultConfigPath, appName)),
RunE: func(cmd *cobra.Command, args []string) error {

jsn, err := cmd.Flags().GetBool(flagJSON)
if err != nil {
return err
Expand All @@ -50,8 +50,7 @@ $ %s sc`, appName, defaultConfigPath, appName)),
}
},
}
addJsonFlag(cmd)
return cmd
return addJSONFlag(cmd)
}

// ParseConfig parses the app config file
Expand All @@ -70,7 +69,7 @@ func ParseConfig(file string) (*types.Config, error) {
EnabledRoutes: cfg.EnabledRoutes,
Circle: cfg.Circle,
ProcessorWorkerCount: cfg.ProcessorWorkerCount,
Api: cfg.Api,
API: cfg.API,
Chains: make(map[string]types.ChainConfig),
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package cmd_test
import (
"testing"

"github.com/stretchr/testify/require"

"github.com/strangelove-ventures/noble-cctp-relayer/cmd"
"github.com/strangelove-ventures/noble-cctp-relayer/ethereum"
"github.com/strangelove-ventures/noble-cctp-relayer/noble"
"github.com/stretchr/testify/require"
)

func TestConfig(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ func addAppPersistantFlags(cmd *cobra.Command, a *AppState) *cobra.Command {
cmd.PersistentFlags().Int16P(flagMetricsPort, "p", 2112, "customize Prometheus metrics port")
cmd.PersistentFlags().DurationP(flagFlushInterval, "i", 0, "how frequently should a flush routine be run")
return cmd

}

func addJsonFlag(cmd *cobra.Command) *cobra.Command {
func addJSONFlag(cmd *cobra.Command) *cobra.Command {
cmd.Flags().Bool(flagJSON, false, "return in json format")
return cmd
}
Loading

0 comments on commit 61ff2d1

Please sign in to comment.