Skip to content

Commit

Permalink
chore: add neutron fetcher test (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno authored Jul 3, 2024
1 parent ac3cc75 commit 5866285
Show file tree
Hide file tree
Showing 18 changed files with 432 additions and 96 deletions.
1 change: 1 addition & 0 deletions assets/neutron-params.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"data":{"threshold":{"threshold_quorum":{"threshold":{"percent":"0.5"},"quorum":{"percent":"0.1"}}},"max_voting_period":{"time":1209600},"min_voting_period":null,"allow_revoting":true,"dao":"neutron1suhgf5svhu4usrurvxzlgn54ksxmn8gljarjtxqnapv8kjnp4nrstdxvff","close_proposal_on_execution_failure":true}}
1 change: 1 addition & 0 deletions assets/neutron-proposals.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions assets/neutron-vote-not-found.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"data":{"vote":null}}
1 change: 1 addition & 0 deletions assets/neutron-vote-ok.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"data":{"vote":{"voter":"neutron103l025fw2x7d8k8px7d07vu667x0h5p5gke4yv","vote":"yes","power":"3069"}}}
1 change: 1 addition & 0 deletions assets/neutron-vote-unknown.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"data":{"vote":{"voter":"neutron103l025fw2x7d8k8px7d07vu667x0h5p5gke4yv","vote":"unknown","power":"3069"}}}
4 changes: 2 additions & 2 deletions pkg/fetchers/cosmos/tally_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestTallyTallyError(t *testing.T) {
LCDEndpoints: []string{"https://example.com"},
ProposalsType: "v1",
}
logger := loggerPkg.GetDefaultLogger()
logger := loggerPkg.GetNopLogger()
tracer := tracing.InitNoopTracer()

fetcher := NewRPC(config, logger, tracer)
Expand Down Expand Up @@ -113,7 +113,7 @@ func TestTallyAllOk(t *testing.T) {
LCDEndpoints: []string{"https://example.com"},
ProposalsType: "v1",
}
logger := loggerPkg.GetDefaultLogger()
logger := loggerPkg.GetNopLogger()
tracer := tracing.InitNoopTracer()

fetcher := NewRPC(config, logger, tracer)
Expand Down
25 changes: 25 additions & 0 deletions pkg/fetchers/fetcher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package fetchers

import (
"main/pkg/fetchers/cosmos"
"main/pkg/fetchers/neutron"
loggerPkg "main/pkg/logger"
"main/pkg/tracing"
"main/pkg/types"
"testing"

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

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

logger := loggerPkg.GetNopLogger()
tracer := tracing.InitNoopTracer()

neutronFetcher := GetFetcher(&types.Chain{Type: "neutron"}, logger, tracer)
assert.IsType(t, &neutron.Fetcher{}, neutronFetcher)

cosmosFetcher := GetFetcher(&types.Chain{}, logger, tracer)
assert.IsType(t, &cosmos.RPC{}, cosmosFetcher)
}
9 changes: 1 addition & 8 deletions pkg/fetchers/neutron/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package neutron
import (
"context"
"encoding/base64"
"errors"
"fmt"
"main/pkg/http"
"main/pkg/types"
Expand Down Expand Up @@ -59,12 +58,6 @@ func (fetcher *Fetcher) GetSmartContractState(
}
}

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

height, _ := utils.GetBlockHeightFromHeader(header)
return height, nil
}
7 changes: 1 addition & 6 deletions pkg/fetchers/neutron/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,5 @@ func (fetcher *Fetcher) GetChainParams(ctx context.Context) (*types.ChainWithVot
return nil, []error{err}
}

paramsParsed, errs := params.ToParams(fetcher.ChainConfig)
if len(errs) > 0 {
return nil, errs
}

return paramsParsed, nil
return params.ToParams(fetcher.ChainConfig), nil
}
70 changes: 70 additions & 0 deletions pkg/fetchers/neutron/params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package neutron

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

"github.com/stretchr/testify/require"

"github.com/jarcoal/httpmock"
)

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

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

fetcher := NewFetcher(config, logger, tracer)

httpmock.RegisterResponder(
"GET",
"https://example.com/cosmwasm/wasm/v1/contract/neutron1436kxs0w2es6xlqpp9rd35e3d0cjnw4sv8j3a7483sgks29jqwgshlt6zh/smart/eyJjb25maWciOnt9fQ==",
httpmock.NewErrorResponder(errors.New("custom error")),
)

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

require.Len(t, errs, 1)
require.ErrorContains(t, errs[0], "custom error")
require.Nil(t, params)
}

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

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

fetcher := NewFetcher(config, logger, tracer)

httpmock.RegisterResponder(
"GET",
"https://example.com/cosmwasm/wasm/v1/contract/neutron1436kxs0w2es6xlqpp9rd35e3d0cjnw4sv8j3a7483sgks29jqwgshlt6zh/smart/eyJjb25maWciOnt9fQ==",
httpmock.NewBytesResponder(200, assets.GetBytesOrPanic("neutron-params.json")),
)

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

require.Empty(t, err)
require.NotNil(t, params)
}
10 changes: 1 addition & 9 deletions pkg/fetchers/neutron/proposals.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,5 @@ func (fetcher *Fetcher) GetAllProposals(
return nil, height, err
}

proposalsParsed, parseErr := proposals.ToProposals()
if parseErr != nil {
return nil, height, &types.QueryError{
QueryError: err,
NodeErrors: nil,
}
}

return proposalsParsed, height, nil
return proposals.ToProposals(), height, nil
}
75 changes: 75 additions & 0 deletions pkg/fetchers/neutron/proposals_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package neutron

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

"github.com/stretchr/testify/require"

"github.com/jarcoal/httpmock"
)

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

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

fetcher := NewFetcher(config, logger, tracer)

httpmock.RegisterResponder(
"GET",
"https://example.com/cosmwasm/wasm/v1/contract/neutron1436kxs0w2es6xlqpp9rd35e3d0cjnw4sv8j3a7483sgks29jqwgshlt6zh/smart/eyJyZXZlcnNlX3Byb3Bvc2FscyI6IHsibGltaXQiOiAxMDAwfX0=",
httpmock.NewErrorResponder(errors.New("custom error")),
)

proposals, height, err := fetcher.GetAllProposals(0, context.Background())

require.Error(t, err)
require.Len(t, err.NodeErrors, 1)

firstError := err.NodeErrors[0].Error
require.ErrorContains(t, &firstError, "custom error")
require.Zero(t, height)
require.Empty(t, proposals)
}

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

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

fetcher := NewFetcher(config, logger, tracer)

httpmock.RegisterResponder(
"GET",
"https://example.com/cosmwasm/wasm/v1/contract/neutron1436kxs0w2es6xlqpp9rd35e3d0cjnw4sv8j3a7483sgks29jqwgshlt6zh/smart/eyJyZXZlcnNlX3Byb3Bvc2FscyI6IHsibGltaXQiOiAxMDAwfX0=",
httpmock.NewBytesResponder(200, assets.GetBytesOrPanic("neutron-proposals.json")),
)

proposals, height, err := fetcher.GetAllProposals(0, context.Background())

require.Empty(t, err)
require.Zero(t, height)
require.NotEmpty(t, proposals)
}
23 changes: 6 additions & 17 deletions pkg/fetchers/neutron/responses/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package responses

import (
"main/pkg/types"
"strconv"
"time"
)

Expand All @@ -11,10 +10,10 @@ type ParamsResponse struct {
Threshold struct {
ThresholdQuorum struct {
Threshold struct {
Percent string `json:"percent"`
Percent float64 `json:"percent,string"`
} `json:"threshold"`
Quorum struct {
Percent string `json:"percent"`
Percent float64 `json:"percent,string"`
} `json:"quorum"`
} `json:"threshold_quorum"`
} `json:"threshold"`
Expand All @@ -25,27 +24,17 @@ type ParamsResponse struct {
} `json:"data"`
}

func (params ParamsResponse) ToParams(chain *types.Chain) (*types.ChainWithVotingParams, []error) {
thresholdPercent, err := strconv.ParseFloat(params.Data.Threshold.ThresholdQuorum.Threshold.Percent, 64)
if err != nil {
return nil, []error{err}
}

quorumPercent, err := strconv.ParseFloat(params.Data.Threshold.ThresholdQuorum.Quorum.Percent, 64)
if err != nil {
return nil, []error{err}
}

func (params ParamsResponse) ToParams(chain *types.Chain) *types.ChainWithVotingParams {
return &types.ChainWithVotingParams{
Chain: chain,
Params: []types.ChainParam{
types.PercentParam{Description: "Threshold percent", Value: thresholdPercent},
types.PercentParam{Description: "Quorum percent", Value: quorumPercent},
types.PercentParam{Description: "Threshold percent", Value: params.Data.Threshold.ThresholdQuorum.Threshold.Percent},
types.PercentParam{Description: "Quorum percent", Value: params.Data.Threshold.ThresholdQuorum.Quorum.Percent},
types.DurationParam{
Description: "Max voting period",
Value: time.Duration(params.Data.MaxVotingPeriod.Time * 1e9),
},
types.BoolParam{Description: "Allow revoting", Value: params.Data.AllowRevoting},
},
}, []error{}
}
}
Loading

0 comments on commit 5866285

Please sign in to comment.