Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixed crash in TimeTillUpgradeGenerator #60

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions pkg/fetchers/block_time_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"main/pkg/constants"
"main/pkg/query_info"

"cosmossdk.io/x/upgrade/types"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"

Expand Down Expand Up @@ -45,9 +47,18 @@ func (n *BlockTimeFetcher) Get(ctx context.Context, data ...interface{}) (interf
panic("data is empty")
}

// upgrade-info was not fetched
if data[0] == nil {
n.Logger.Trace().Msg("Upgrade plan is empty, not fetching upgrade info.")
n.Logger.Trace().Msg("Upgrade plan is empty, not fetching block time.")
return nil, []query_info.QueryInfo{}
}

// upgrade-info was not fetched
upgradePlan, ok := data[0].(*types.Plan)
if !ok {
panic("expected upgrade plan to be *types.Plan")
}
if upgradePlan == nil {
n.Logger.Trace().Msg("Upgrade plan is empty, not fetching block time.")
return nil, []query_info.QueryInfo{}
}

Expand Down
26 changes: 24 additions & 2 deletions pkg/fetchers/block_time_fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,31 @@ func TestBlockTimeFetcherDataNil(t *testing.T) {
tracer := tracing.InitNoopTracer()
client := tendermint.NewRPC(config, *logger, tracer)
fetcher := NewBlockTimeFetcher(*logger, client, tracer)
data, queryInfos := fetcher.Get(context.Background(), nil)

var upgradePlan *types.Plan

data, queryInfos := fetcher.Get(context.Background(), upgradePlan)
assert.Empty(t, queryInfos)
assert.Nil(t, data)
}

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

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

config := configPkg.TendermintConfig{Address: "https://example.com"}
logger := loggerPkg.GetNopLogger()
tracer := tracing.InitNoopTracer()
client := tendermint.NewRPC(config, *logger, tracer)
fetcher := NewBlockTimeFetcher(*logger, client, tracer)
fetcher.Get(context.Background(), 3)
}

//nolint:paralleltest // disabled due to httpmock usage
func TestBlockTimeFetcherTendermintFailBlock(t *testing.T) {
httpmock.Activate()
Expand Down Expand Up @@ -125,7 +145,9 @@ func TestBlockTimeFetcherNoUpgrade(t *testing.T) {
client := tendermint.NewRPC(config, *logger, tracer)
fetcher := NewBlockTimeFetcher(*logger, client, tracer)

data, queryInfos := fetcher.Get(context.Background(), nil)
var upgradePlan *types.Plan

data, queryInfos := fetcher.Get(context.Background(), upgradePlan)
assert.Empty(t, queryInfos)
assert.Nil(t, data)
}
Expand Down
Loading