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

Correctly handle multiple passed upgrade proposals acc to cosmos-sdk #24

Merged
merged 1 commit into from
Nov 29, 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
23 changes: 23 additions & 0 deletions internal/pkg/provider/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,29 @@ func (p *Provider) GetUpgrades(ctx context.Context) ([]*urproto.Upgrade, error)
}
}

// If multiple passed upgrade proposals are in the "passed" state,
// the cosmos upgrade handler only treats the one with the highest proposal ID
// as "passed" and all other passed proposals as "cancelled".
// This is not to be confused with the code above, which handles the
// case where multiple upgrade proposals exist for the same upgrade height
// https://github.com/cosmos/cosmos-sdk/blob/f007a4ea0711da2bac20afc6283885c1b2496ae5/x/upgrade/keeper/keeper.go#L189-L193
latestPassedProposal := uint64(0)
isAnyProposalPassed := false
for _, upgrade := range filtered {
if upgrade.Status == PASSED {
latestPassedProposal = max(latestPassedProposal, upgrade.ProposalID)
isAnyProposalPassed = true
}
}

if isAnyProposalPassed {
for i := range filtered {
if filtered[i].Status == PASSED && filtered[i].ProposalID < latestPassedProposal {
filtered[i].Status = CANCELLED
}
}
}

// sort upgrades in descending order by proposal id because iterating over map doesn't guarantee order
sort.Slice(filtered, func(i, j int) bool {
return filtered[i].ProposalID > filtered[j].ProposalID
Expand Down
25 changes: 23 additions & 2 deletions internal/pkg/provider/chain/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,39 @@ func TestGetUpgrades(t *testing.T) {
},
},
},
{
name: "MultiplePassed",
proposals: v1.Proposals{
newProposal(t, 1, 100, v1.StatusPassed),
newProposal(t, 2, 200, v1.StatusPassed),
},
expected: []*urproto.Upgrade{
{
Height: 200,
Type: urproto.UpgradeType_GOVERNANCE,
Status: urproto.UpgradeStatus_ACTIVE,
Source: urproto.ProviderType_CHAIN,
},
{
Height: 100,
Type: urproto.UpgradeType_GOVERNANCE,
Status: urproto.UpgradeStatus_CANCELLED,
Source: urproto.ProviderType_CHAIN,
},
},
},
{
name: "DuplicateProposalsWithPassedStatus",
proposals: v1.Proposals{
newProposal(t, 1, 100, v1.StatusPassed),
newProposal(t, 2, 100, v1.StatusPassed),
newProposal(t, 3, 200, v1.StatusPassed),
newProposal(t, 3, 200, v1.StatusDepositPeriod),
Comment on lines -69 to +90
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I needed to change the status here as the newly added logic would cancel the proposal at height 100 seeing a passed upgrade at 200, which is the correct behaviour.

},
expected: []*urproto.Upgrade{
{
Height: 200,
Type: urproto.UpgradeType_GOVERNANCE,
Status: urproto.UpgradeStatus_ACTIVE,
Status: urproto.UpgradeStatus_SCHEDULED,
Source: urproto.ProviderType_CHAIN,
},
{
Expand Down
5 changes: 5 additions & 0 deletions internal/pkg/provider/chain/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
PASSED ProposalStatus = 3
REJECTED ProposalStatus = 4
FAILED ProposalStatus = 5
CANCELLED ProposalStatus = 6
)

func (ps ProposalStatus) String() string {
Expand All @@ -39,6 +40,8 @@ func (ps ProposalStatus) String() string {
return "REJECTED"
case FAILED:
return "FAILED"
case CANCELLED:
return "CANCELLED"
default:
return fmt.Sprintf("%d", int(ps))
}
Expand Down Expand Up @@ -70,6 +73,8 @@ func (cu chainUpgrade) ToProto() urproto.Upgrade {
upgradeStatus = urproto.UpgradeStatus_CANCELLED
case FAILED:
upgradeStatus = urproto.UpgradeStatus_CANCELLED
case CANCELLED:
upgradeStatus = urproto.UpgradeStatus_CANCELLED
}

// #nosec G115
Expand Down