Skip to content

Commit

Permalink
chore: add cosmos fetcher test
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Jul 3, 2024
1 parent 46b8a33 commit 562a086
Show file tree
Hide file tree
Showing 28 changed files with 1,215 additions and 156 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ jobs:
with:
go-version: '1.21.3'
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v6
with:
version: latest
args: release --rm-dist
args: release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
8 changes: 1 addition & 7 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@ linters:
enable-all: true
disable:
- funlen
- scopelint
- interfacer
- exhaustivestruct
- maligned
- golint
- nlreturn
- wrapcheck
- gomnd
- cyclop
- goerr113
- err113
- exhaustruct
- wsl
- lll
Expand All @@ -41,7 +36,6 @@ linters:
- gci
- musttag
- depguard
- ifshort
- ireturn
- godox
- perfsprint
Expand Down
3 changes: 0 additions & 3 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
before:
hooks:
- go mod tidy
builds:
- main: ./cmd/cosmos-proposals-checker.go
env:
Expand Down
9 changes: 9 additions & 0 deletions assets/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,12 @@ import (

//go:embed *
var EmbedFS embed.FS

func GetBytesOrPanic(path string) []byte {
bytes, err := EmbedFS.ReadFile(path)
if err != nil {
panic(err)
}

return bytes
}
28 changes: 28 additions & 0 deletions assets/fs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package assets

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/stretchr/testify/require"
)

func TestGetPanicOrFailPanic(t *testing.T) {
t.Parallel()

defer func() {
if r := recover(); r == nil {
require.Fail(t, "Expected to have a panic here!")
}
}()

GetBytesOrPanic("not-existing")
}

func TestGetPanicOrFailOk(t *testing.T) {
t.Parallel()

bytes := GetBytesOrPanic("valid-config.toml")
assert.NotNil(t, bytes)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/bwmarrin/discordgo v0.28.1
github.com/creasty/defaults v1.7.0
github.com/guregu/null/v5 v5.0.0
github.com/jarcoal/httpmock v1.3.1
github.com/robfig/cron/v3 v3.0.1
github.com/rs/zerolog v1.26.1
github.com/spf13/cobra v1.4.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ github.com/guregu/null/v5 v5.0.0 h1:PRxjqyOekS11W+w/7Vfz6jgJE/BCwELWtgvOJzddimw=
github.com/guregu/null/v5 v5.0.0/go.mod h1:SjupzNy+sCPtwQTKWhUCqjhVCO69hpsl2QsZrWHjlwU=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww=
github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand All @@ -48,6 +50,8 @@ github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/maxatome/go-testdeep v1.12.0 h1:Ql7Go8Tg0C1D/uMMX59LAoYK7LffeJQ6X2T04nTH68g=
github.com/maxatome/go-testdeep v1.12.0/go.mod h1:lPZc/HAcJMP92l7yI6TRz1aZN5URwUBUAfUNvrclaNM=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
6 changes: 1 addition & 5 deletions pkg/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ func NewApp(configPath string, filesystem fs.FS, version string) *App {
logger.GetDefaultLogger().Info().Msg("Provided config is valid.")
}

tracer, err := tracing.InitTracer(config.TracingConfig, version)
if err != nil {
logger.GetDefaultLogger().Fatal().Err(err).Msg("Error setting up tracing")
}

tracer := tracing.InitTracer(config.TracingConfig, version)
log := logger.GetLogger(config.LogConfig)

stateManager := state.NewStateManager(config.StatePath, filesystem, log)
Expand Down
18 changes: 10 additions & 8 deletions pkg/fetchers/cosmos/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ import (
const PaginationLimit = 1000

type RPC struct {
ChainConfig *types.Chain
ProposalsType string
Client *http.Client
Logger zerolog.Logger
ChainConfig *types.Chain
ProposalsType string
Client *http.Client
Logger zerolog.Logger
PaginationLimit int
}

func NewRPC(chainConfig *types.Chain, logger *zerolog.Logger, tracer trace.Tracer) *RPC {
return &RPC{
ChainConfig: chainConfig,
ProposalsType: chainConfig.ProposalsType,
Logger: logger.With().Str("component", "rpc").Logger(),
Client: http.NewClient(chainConfig.Name, chainConfig.LCDEndpoints, logger, tracer),
ChainConfig: chainConfig,
ProposalsType: chainConfig.ProposalsType,
Logger: logger.With().Str("component", "rpc").Logger(),
Client: http.NewClient(chainConfig.Name, chainConfig.LCDEndpoints, logger, tracer),
PaginationLimit: PaginationLimit,
}
}

Expand Down
101 changes: 101 additions & 0 deletions pkg/fetchers/cosmos/params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package cosmos

import (
"context"
"errors"
"main/assets"
loggerPkg "main/pkg/logger"
"main/pkg/tracing"
"main/pkg/types"
"testing"

"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)

//nolint:paralleltest // disabled due to httpmock usage
func TestParamsFailAll(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

config := &types.Chain{
Name: "chain",
LCDEndpoints: []string{"https://example.com"},
}
logger := loggerPkg.GetNopLogger()
tracer := tracing.InitNoopTracer()

fetcher := NewRPC(config, logger, tracer)

httpmock.RegisterResponder(
"GET",
"https://example.com/cosmos/gov/v1beta1/params/deposit",
httpmock.NewErrorResponder(errors.New("custom error")),
)

httpmock.RegisterResponder(
"GET",
"https://example.com/cosmos/gov/v1beta1/params/voting",
httpmock.NewErrorResponder(errors.New("custom error")),
)

httpmock.RegisterResponder(
"GET",
"https://example.com/cosmos/gov/v1beta1/params/tallying",
httpmock.NewErrorResponder(errors.New("custom error")),
)

params, errs := fetcher.GetChainParams(
context.Background(),
)

assert.Len(t, errs, 3)
assert.Nil(t, params)
}

//nolint:paralleltest // disabled due to httpmock usage
func TestParamsFailOk(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

config := &types.Chain{
Name: "chain",
LCDEndpoints: []string{"https://example.com"},
}
logger := loggerPkg.GetNopLogger()
tracer := tracing.InitNoopTracer()

fetcher := NewRPC(config, logger, tracer)

httpmock.RegisterResponder(
"GET",
"https://example.com/cosmos/gov/v1beta1/params/deposit",
httpmock.NewBytesResponder(200, assets.GetBytesOrPanic("params_deposit.json")),
)

httpmock.RegisterResponder(
"GET",
"https://example.com/cosmos/gov/v1beta1/params/voting",
httpmock.NewBytesResponder(200, assets.GetBytesOrPanic("params_voting.json")),
)

httpmock.RegisterResponder(
"GET",
"https://example.com/cosmos/gov/v1beta1/params/tallying",
httpmock.NewBytesResponder(200, assets.GetBytesOrPanic("params_tallying.json")),
)

params, errs := fetcher.GetChainParams(
context.Background(),
)

assert.Empty(t, errs)
assert.NotNil(t, params)
assert.Len(t, params.Params, 6)
assert.Equal(t, "14 days", params.Params[0].Serialize())
assert.Equal(t, "14 days", params.Params[1].Serialize())
assert.Equal(t, "250000000 uatom", params.Params[2].Serialize())
assert.Equal(t, "40.00%", params.Params[3].Serialize())
assert.Equal(t, "50.00%", params.Params[4].Serialize())
assert.Equal(t, "33.40%", params.Params[5].Serialize())
}
17 changes: 5 additions & 12 deletions pkg/fetchers/cosmos/proposals_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ func (rpc *RPC) GetAllV1Proposals(

for {
url := fmt.Sprintf(
// 2 is for PROPOSAL_STATUS_VOTING_PERIOD
"/cosmos/gov/v1/proposals?pagination.limit=%d&pagination.offset=%d",
PaginationLimit,
"/cosmos/gov/v1/proposals?pagination.limit=%d&pagination.offset=%d&pagination.count_total=1",
rpc.PaginationLimit,
offset,
)

Expand All @@ -40,13 +39,7 @@ func (rpc *RPC) GetAllV1Proposals(
}
}

height, err := utils.GetBlockHeightFromHeader(header)
if err != nil {
return nil, 0, &types.QueryError{
QueryError: errors.New("got error when parsing proposal height"),
}
}

height, _ := utils.GetBlockHeightFromHeader(header)
lastHeight = height

if batchProposals.Message != "" {
Expand All @@ -59,11 +52,11 @@ func (rpc *RPC) GetAllV1Proposals(
return p.ToProposal()
})
proposals = append(proposals, parsedProposals...)
if len(batchProposals.Proposals) < PaginationLimit {
if len(batchProposals.Proposals) < rpc.PaginationLimit {
break
}

offset += PaginationLimit
offset += rpc.PaginationLimit
}

return proposals, lastHeight, nil
Expand Down
Loading

0 comments on commit 562a086

Please sign in to comment.