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

refactor(golang/cosmos): Simplify and DRY out isPrimaryUpgradeName #10091

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 5 additions & 2 deletions golang/cosmos/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ func NewAgoricApp(
app.SetBeginBlocker(app.BeginBlocker)
app.SetEndBlocker(app.EndBlocker)

for _, name := range upgradeNamesOfThisVersion {
for name := range upgradeNamesOfThisVersion {
app.UpgradeKeeper.SetUpgradeHandler(
name,
unreleasedUpgradeHandler(app, name),
Expand All @@ -889,7 +889,10 @@ func NewAgoricApp(
// Store migrations can only run once, so we use a notion of "primary upgrade
// name" to trigger them. Testnets may end up upgrading from one rc to
// another, which shouldn't re-run store upgrades.
if isPrimaryUpgradeName(upgradeInfo.Name) && !app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) {
if upgradeInfo.Name != "" &&
mhofman marked this conversation as resolved.
Show resolved Hide resolved
isPrimaryUpgradeName(upgradeInfo.Name) &&
!app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) {

storeUpgrades := storetypes.StoreUpgrades{
Added: []string{},
Deleted: []string{},
Expand Down
72 changes: 24 additions & 48 deletions golang/cosmos/app/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,61 +10,37 @@ import (
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
)

var upgradeNamesOfThisVersion = []string{
"UNRELEASED_BASIC", // no-frills
"UNRELEASED_A3P_INTEGRATION",
"UNRELEASED_main",
"UNRELEASED_devnet",
"UNRELEASED_REAPPLY",
// upgradeNamesOfThisVersion documents the current upgrade names and whether
// each is "primary" (used to trigger store migrations, which can only run
// once). An actual release should have exactly one primary upgrade name and any
// number of non-primary upgrade names (each of the latter being associated with
// a release candidate iteration after the first RC and used only for
// pre-production chains), but for testing purposes, the master branch has
// multiple primary upgrade names.
var upgradeNamesOfThisVersion = map[string]bool{
"UNRELEASED_BASIC": true,
"UNRELEASED_A3P_INTEGRATION": true,
"UNRELEASED_main": true,
"UNRELEASED_devnet": true,
"UNRELEASED_REAPPLY": false,
}

// isUpgradeNameOfThisVersion returns whether the provided plan name is a
// known upgrade name of this software version
func isUpgradeNameOfThisVersion(name string) bool {
for _, upgradeName := range upgradeNamesOfThisVersion {
if upgradeName == name {
return true
}
}
return false
}

// validUpgradeName is an identity function that asserts the provided name
// is an upgrade name of this software version. It can be used as a sort of
// dynamic enum check.
func validUpgradeName(name string) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While no longer in use right now, this helper was extremely useful when we had to generate different core proposal configs depending on the upgrade name (different price feed depending on the network). Let keep it around.

Maybe restore the check that name is a valid upgrade name in isPrimaryUpgradeName ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isPrimaryUpgradeName already includes that check... if the provided name is not a valid upgrade name, then isPrimary, found := upgradeNamesOfThisVersion[name] will set both isPrimary and found to false, the latter resulting in a panic two lines down. And any non-panicky validity check could be trivially performed against upgradeNamesOfThisVersion in exactly the same way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I'm asking here is to keep validUpgradeName around as it was useful for a switch case that we needed when conditionally generating core proposals. I figured calling it from isPrimaryUpgradeName was an easy way to keep it in use to satisfy golint checks.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you point to that switch case? I think I'd also like it better without validUpgradeName.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to do some git blaming to track the refactor that introduced it, but the concept was first introduced here:

if !upgradeNamesOfThisVersion[expectedUpgradeName] {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, for such a release I'd probably just generalize upgradeNamesOfThisVersion from map[string]bool to map[string][]string (with each value containing the correct oracle addresses). And if we want that kind of generalization to be obvious, I can update this PR from map[string]bool to map[string]*upgradeMetaData (where the basic upgradeMetaData is just struct{ isPrimary bool }).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the concern with keeping part of the customization related to upgrade-name imperative instead of through configuration? In JS a switch case is perfectly acceptable to do this. The problem is that in golang we don't have types to help us check a typo wasn't made.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It requires the upgrade names to be retyped and muddies their source of truth, creating another opportunity for error in release branch initialization (which in fact I did miss in #10088). And it's not actually imperative anyway, since that link demonstrates that there was in fact a static mapping from upgrade name to data that was serialized into a JSON template. Basically, it's unnecessary complexity.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that particular case there was only a config change indeed. I would like a flexible option that allows conditionally including some core proposals depending on the upgrade name. What do you suggest is the most robust approach?

Copy link
Member Author

@gibson042 gibson042 Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest generalizing upgradeNamesOfThisVersion:

// upgradeDataOfThisVersion documents the current upgrade names and whether
// each is "primary" (used to trigger store migrations, which can only run
// once), sometimes with other data as well (such as chain-specific core
// proposal names).
// An actual release should have exactly one primary upgrade name and any number
// of non-primary upgrade names (each of the latter being associated with a
// release candidate iteration after the first RC and used only for
// pre-production chains), but for testing purposes, the master branch has
// multiple primary upgrade names.
var upgradeDataOfThisVersion = map[string]*upgradeData{
	"UNRELEASED_BASIC":           &upgradeData{true, "foo"},
	"UNRELEASED_A3P_INTEGRATION": &upgradeData{true, "foo"},
	"UNRELEASED_main":            &upgradeData{true, "real"},
	"UNRELEASED_devnet":          &upgradeData{true, "phony"},
	"UNRELEASED_REAPPLY":         &upgradeData{false, ""},
}

type upgradeData struct {
	isPrimary bool
	// Extra fields for this particular version go here.
	coreProposalName string
}

if !isUpgradeNameOfThisVersion(name) {
panic(fmt.Errorf("invalid upgrade name: %s", name))
}
return name
}

// isPrimaryUpgradeName returns wether the provided plan name is considered a
// isPrimaryUpgradeName returns whether the provided plan name is considered a
// primary for the purpose of applying store migrations for the first upgrade
// of this version.
// It is expected that only primary plan names are used for non testing chains.
// It is expected that only primary plan names are used for production chains.
func isPrimaryUpgradeName(name string) bool {
if name == "" {
// An empty upgrade name can happen if there are no upgrade in progress
return false
}
switch name {
case validUpgradeName("UNRELEASED_BASIC"),
validUpgradeName("UNRELEASED_A3P_INTEGRATION"),
validUpgradeName("UNRELEASED_main"),
validUpgradeName("UNRELEASED_devnet"):
return true
case validUpgradeName("UNRELEASED_REAPPLY"):
return false
default:
panic(fmt.Errorf("unexpected upgrade name %s", validUpgradeName(name)))
isPrimary, found := upgradeNamesOfThisVersion[name]
if !found {
panic(fmt.Errorf("invalid upgrade name: %q", name))
}
return isPrimary
}

// isFirstTimeUpgradeOfThisVersion looks up in the upgrade store whether no
// upgrade plan name of this version have previously been applied.
func isFirstTimeUpgradeOfThisVersion(app *GaiaApp, ctx sdk.Context) bool {
for _, name := range upgradeNamesOfThisVersion {
for name := range upgradeNamesOfThisVersion {
if app.UpgradeKeeper.GetDoneHeight(ctx, name) != 0 {
return false
}
Expand All @@ -82,11 +58,11 @@ func unreleasedUpgradeHandler(app *GaiaApp, targetUpgrade string) func(sdk.Conte
// These CoreProposalSteps are not idempotent and should only be executed
// as part of the first upgrade using this handler on any given chain.
if isFirstTimeUpgradeOfThisVersion(app, ctx) {
// The storeUpgrades defined in app.go only execute for the primary upgrade name
// If we got here and this first upgrade of this version does not use the
// primary upgrade name, stores have not been initialized correctly.
// The storeUpgrades defined in app.go only execute for primary upgrade names.
// If this first upgrade is *not* primary, then stores have not been
// initialized correctly.
if !isPrimaryUpgradeName(plan.Name) {
return module.VersionMap{}, fmt.Errorf("cannot run %s as first upgrade", plan.Name)
return module.VersionMap{}, fmt.Errorf("cannot run %q as first upgrade", plan.Name)
}

// Each CoreProposalStep runs sequentially, and can be constructed from
Expand Down
Loading