Skip to content

Commit

Permalink
fix: fixed crash in TimeTillUpgradeGenerator (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno authored Sep 13, 2024
1 parent bbad095 commit 6d9bed8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
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

0 comments on commit 6d9bed8

Please sign in to comment.