Skip to content

Commit

Permalink
Correctly handle multiple passed upgrade proposals acc to cosmos-sdk
Browse files Browse the repository at this point in the history
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".
https://github.com/cosmos/cosmos-sdk/blob/f007a4ea0711da2bac20afc6283885c1b2496ae5/x/upgrade/keeper/keeper.go#L189-L193
  • Loading branch information
sin3point14 committed Nov 29, 2024
1 parent 3f815dc commit a2c9d9b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
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),
},
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

0 comments on commit a2c9d9b

Please sign in to comment.