-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add upgrade plan fetcher and generator
- Loading branch information
1 parent
03072e0
commit 573cd72
Showing
10 changed files
with
279 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package fetchers | ||
|
||
import ( | ||
"context" | ||
"main/pkg/clients/tendermint" | ||
"main/pkg/constants" | ||
"main/pkg/query_info" | ||
|
||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/trace" | ||
|
||
"github.com/rs/zerolog" | ||
) | ||
|
||
type UpgradesFetcher struct { | ||
TendermintRPC *tendermint.RPC | ||
Logger zerolog.Logger | ||
Tracer trace.Tracer | ||
} | ||
|
||
func NewUpgradesFetcher(logger zerolog.Logger, tendermintRPC *tendermint.RPC, tracer trace.Tracer) *UpgradesFetcher { | ||
return &UpgradesFetcher{ | ||
Logger: logger.With().Str("component", "upgrades_fetcher").Logger(), | ||
TendermintRPC: tendermintRPC, | ||
Tracer: tracer, | ||
} | ||
} | ||
|
||
func (n *UpgradesFetcher) Enabled() bool { | ||
return n.TendermintRPC != nil | ||
} | ||
|
||
func (n *UpgradesFetcher) Name() constants.FetcherName { | ||
return constants.FetcherNameUpgrades | ||
} | ||
|
||
func (n *UpgradesFetcher) Dependencies() []constants.FetcherName { | ||
return []constants.FetcherName{} | ||
} | ||
|
||
func (n *UpgradesFetcher) Get(ctx context.Context) (interface{}, []query_info.QueryInfo) { | ||
childCtx, span := n.Tracer.Start( | ||
ctx, | ||
"Fetcher "+string(n.Name()), | ||
trace.WithAttributes(attribute.String("node", n.TendermintRPC.Address)), | ||
) | ||
defer span.End() | ||
|
||
upgradePlan, queryInfo, err := n.TendermintRPC.GetUpgradePlan(childCtx) | ||
if err != nil { | ||
n.Logger.Error().Err(err).Msg("Could not fetch upgrade plan") | ||
return nil, []query_info.QueryInfo{queryInfo} | ||
} | ||
|
||
return upgradePlan, []query_info.QueryInfo{queryInfo} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package fetchers | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"main/assets" | ||
"main/pkg/clients/tendermint" | ||
configPkg "main/pkg/config" | ||
"main/pkg/constants" | ||
loggerPkg "main/pkg/logger" | ||
"main/pkg/tracing" | ||
"testing" | ||
|
||
"github.com/jarcoal/httpmock" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUpgradesFetcherBase(t *testing.T) { | ||
t.Parallel() | ||
|
||
config := configPkg.TendermintConfig{ | ||
Address: "https://example.com", | ||
} | ||
logger := loggerPkg.GetNopLogger() | ||
tracer := tracing.InitNoopTracer() | ||
client := tendermint.NewRPC(config, *logger, tracer) | ||
fetcher := NewUpgradesFetcher(*logger, client, tracer) | ||
assert.True(t, fetcher.Enabled()) | ||
assert.Equal(t, constants.FetcherNameUpgrades, fetcher.Name()) | ||
assert.Empty(t, fetcher.Dependencies()) | ||
} | ||
|
||
//nolint:paralleltest // disabled due to httpmock usage | ||
func TestUpgradesFetcherFail(t *testing.T) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
httpmock.RegisterResponder( | ||
"GET", | ||
"https://example.com/abci_query?path=%22%2Fcosmos.upgrade.v1beta1.Query%2FCurrentPlan%22&data=0x", | ||
httpmock.NewErrorResponder(errors.New("custom error")), | ||
) | ||
|
||
config := configPkg.TendermintConfig{Address: "https://example.com"} | ||
logger := loggerPkg.GetNopLogger() | ||
tracer := tracing.InitNoopTracer() | ||
client := tendermint.NewRPC(config, *logger, tracer) | ||
fetcher := NewUpgradesFetcher(*logger, client, tracer) | ||
|
||
data, queryInfos := fetcher.Get(context.Background()) | ||
assert.Len(t, queryInfos, 1) | ||
assert.False(t, queryInfos[0].Success) | ||
assert.Nil(t, data) | ||
} | ||
|
||
//nolint:paralleltest // disabled due to httpmock usage | ||
func TestUpgradesFetcherOk(t *testing.T) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
httpmock.RegisterResponder( | ||
"GET", | ||
"https://example.com/abci_query?path=%22%2Fcosmos.upgrade.v1beta1.Query%2FCurrentPlan%22&data=0x", | ||
httpmock.NewBytesResponder(200, assets.GetBytesOrPanic("upgrade-plan.json")), | ||
) | ||
|
||
config := configPkg.TendermintConfig{Address: "https://example.com"} | ||
logger := loggerPkg.GetDefaultLogger() | ||
tracer := tracing.InitNoopTracer() | ||
client := tendermint.NewRPC(config, *logger, tracer) | ||
fetcher := NewUpgradesFetcher(*logger, client, tracer) | ||
|
||
data, queryInfos := fetcher.Get(context.Background()) | ||
assert.Len(t, queryInfos, 1) | ||
assert.True(t, queryInfos[0].Success) | ||
assert.NotNil(t, data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package generators | ||
|
||
import ( | ||
"main/pkg/constants" | ||
"main/pkg/fetchers" | ||
"main/pkg/metrics" | ||
|
||
upgradeTypes "cosmossdk.io/x/upgrade/types" | ||
) | ||
|
||
type UpgradesGenerator struct{} | ||
|
||
func NewUpgradesGenerator() *UpgradesGenerator { | ||
return &UpgradesGenerator{} | ||
} | ||
|
||
func (g *UpgradesGenerator) Get(state fetchers.State) []metrics.MetricInfo { | ||
upgradesRaw, ok := state[constants.FetcherNameUpgrades] | ||
if !ok || upgradesRaw == nil { | ||
return []metrics.MetricInfo{} | ||
} | ||
|
||
upgradeInfo, ok := upgradesRaw.(*upgradeTypes.Plan) | ||
if !ok { | ||
panic("expected the state entry to be string") | ||
} | ||
|
||
return []metrics.MetricInfo{ | ||
{ | ||
MetricName: metrics.MetricNameUpgradeInfo, | ||
Labels: map[string]string{"name": upgradeInfo.Name, "info": upgradeInfo.Info}, | ||
Value: 1, | ||
}, | ||
{ | ||
MetricName: metrics.MetricNameUpgradeHeight, | ||
Labels: map[string]string{"name": upgradeInfo.Name}, | ||
Value: float64(upgradeInfo.Height), | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package generators | ||
|
||
import ( | ||
"context" | ||
"main/assets" | ||
"main/pkg/clients/tendermint" | ||
configPkg "main/pkg/config" | ||
"main/pkg/constants" | ||
"main/pkg/fetchers" | ||
loggerPkg "main/pkg/logger" | ||
"main/pkg/tracing" | ||
"testing" | ||
|
||
"github.com/jarcoal/httpmock" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUpgradesGeneratorEmpty(t *testing.T) { | ||
t.Parallel() | ||
|
||
state := fetchers.State{} | ||
|
||
generator := NewUpgradesGenerator() | ||
|
||
metrics := generator.Get(state) | ||
assert.Empty(t, metrics) | ||
} | ||
|
||
func TestUpgradesGeneratorInvalid(t *testing.T) { | ||
t.Parallel() | ||
|
||
defer func() { | ||
if r := recover(); r == nil { | ||
require.Fail(t, "Expected to have a panic here!") | ||
} | ||
}() | ||
|
||
state := fetchers.State{ | ||
constants.FetcherNameUpgrades: 3, | ||
} | ||
|
||
generator := NewUpgradesGenerator() | ||
generator.Get(state) | ||
} | ||
|
||
//nolint:paralleltest // disabled due to httpmock usage | ||
func TestUpgradesGeneratorOk(t *testing.T) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
httpmock.RegisterResponder( | ||
"GET", | ||
"https://example.com/abci_query?path=%22%2Fcosmos.upgrade.v1beta1.Query%2FCurrentPlan%22&data=0x", | ||
httpmock.NewBytesResponder(200, assets.GetBytesOrPanic("upgrade-plan.json")), | ||
) | ||
|
||
config := configPkg.TendermintConfig{ | ||
Address: "https://example.com", | ||
} | ||
logger := loggerPkg.GetNopLogger() | ||
tracer := tracing.InitNoopTracer() | ||
client := tendermint.NewRPC(config, *logger, tracer) | ||
fetcher := fetchers.NewUpgradesFetcher(*logger, client, tracer) | ||
|
||
data, _ := fetcher.Get(context.Background()) | ||
assert.NotNil(t, data) | ||
|
||
state := fetchers.State{ | ||
constants.FetcherNameUpgrades: data, | ||
} | ||
|
||
generator := NewUpgradesGenerator() | ||
|
||
metrics := generator.Get(state) | ||
assert.Len(t, metrics, 2) | ||
|
||
upgradeInfo := metrics[0] | ||
assert.NotEmpty(t, upgradeInfo.Labels["info"]) | ||
assert.Equal(t, "v1.5.0", upgradeInfo.Labels["name"]) | ||
assert.InDelta(t, 1, upgradeInfo.Value, 0.01) | ||
|
||
upgradeHeight := metrics[1] | ||
assert.Equal(t, "v1.5.0", upgradeHeight.Labels["name"]) | ||
assert.InDelta(t, 8375044, upgradeHeight.Value, 0.01) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.