Skip to content

Commit

Permalink
feat!: introduce MsgTryUpgrade (#2880)
Browse files Browse the repository at this point in the history
Addresses: #2877

Introduces a crank message for tallying all the signal messages
  • Loading branch information
cmwaters authored Dec 1, 2023
1 parent c119b8f commit 95bda40
Show file tree
Hide file tree
Showing 7 changed files with 492 additions and 40 deletions.
12 changes: 12 additions & 0 deletions proto/celestia/upgrade/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ service Msg {
rpc SignalVersion(MsgSignalVersion) returns (MsgSignalVersionResponse) {
option (google.api.http).post = "/upgrade/v1/signal";
}

// TryUpgrade tallies all the votes and if a quorum is reached, it will
// trigger an upgrade for the following height
rpc TryUpgrade(MsgTryUpgrade) returns (MsgTryUpgradeResponse) {
option (google.api.http).post = "/upgrade/v1/upgrade";
}
}

// MsgSignalVersion signals for an upgrade
Expand All @@ -22,3 +28,9 @@ message MsgSignalVersion {
// MsgSignalVersionResponse describes the response returned after the submission
// of a SignalVersion
message MsgSignalVersionResponse {}

// MsgTryUpgrade tries to upgrade the chain
message MsgTryUpgrade { string signer = 1; }

// MsgTryUpgradeResponse describes the response returned after the submission
message MsgTryUpgradeResponse {}
27 changes: 15 additions & 12 deletions x/upgrade/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

// Keeper implements the MsgServer and QueryServer interfaces
var (
_ types.MsgServer = Keeper{}
_ types.MsgServer = &Keeper{}
_ types.QueryServer = Keeper{}

defaultSignalTheshold = Fraction{Numerator: 5, Denominator: 6}
Expand Down Expand Up @@ -93,6 +93,20 @@ func (k Keeper) SignalVersion(ctx context.Context, req *types.MsgSignalVersion)
return &types.MsgSignalVersionResponse{}, nil
}

// TryUpgrade is a method required by the MsgServer interface
// It tallies the voting power that has voted on each version.
// If one version has quorum, it is set as the quorum version
// which the application can use as signal to upgrade to that version.
func (k *Keeper) TryUpgrade(ctx context.Context, _ *types.MsgTryUpgrade) (*types.MsgTryUpgradeResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
threshold := k.GetVotingPowerThreshold(sdkCtx)
hasQuorum, version := k.TallyVotingPower(sdkCtx, threshold.Int64())
if hasQuorum {
k.quorumVersion = version
}
return &types.MsgTryUpgradeResponse{}, nil
}

// VersionTally is a method required by the QueryServer interface
func (k Keeper) VersionTally(ctx context.Context, req *types.QueryVersionTallyRequest) (*types.QueryVersionTallyResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
Expand Down Expand Up @@ -130,17 +144,6 @@ func (k Keeper) DeleteValidatorVersion(ctx sdk.Context, valAddress sdk.ValAddres
store.Delete(valAddress)
}

// EndBlock is called at the end of every block. It tallies the voting power that has
// voted on each version. If one version has quorum, it is set as the quorum version
// which the application can use as signal to upgrade to that version.
func (k *Keeper) EndBlock(ctx sdk.Context) {
threshold := k.GetVotingPowerThreshold(ctx)
hasQuorum, version := k.TallyVotingPower(ctx, threshold.Int64())
if hasQuorum {
k.quorumVersion = version
}
}

// TallyVotingPower tallies the voting power for each version and returns true if
// any version has reached the quorum in voting power
func (k Keeper) TallyVotingPower(ctx sdk.Context, threshold int64) (bool, uint64) {
Expand Down
7 changes: 1 addition & 6 deletions x/upgrade/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (am AppModule) LegacyQuerierHandler(_ *codec.LegacyAmino) sdk.Querier {

// RegisterServices registers module services.
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), am.keeper)
types.RegisterMsgServer(cfg.MsgServer(), &am.keeper)
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
}

Expand All @@ -122,8 +122,3 @@ func (am AppModule) ExportGenesis(_ sdk.Context, cdc codec.JSONCodec) json.RawMe

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return consensusVersion }

// EndBlock is used by the Endblocker
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) {
am.keeper.EndBlock(ctx)
}
6 changes: 4 additions & 2 deletions x/upgrade/tally_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ func TestTallyingLogic(t *testing.T) {
require.EqualValues(t, 100, res.ThresholdPower)
require.EqualValues(t, 120, res.TotalVotingPower)

upgradeKeeper.EndBlock(ctx)
_, err = upgradeKeeper.TryUpgrade(goCtx, &types.MsgTryUpgrade{})
require.NoError(t, err)
shouldUpgrade, version := upgradeKeeper.ShouldUpgrade()
require.False(t, shouldUpgrade)
require.Equal(t, uint64(0), version)
Expand All @@ -108,7 +109,8 @@ func TestTallyingLogic(t *testing.T) {
})
require.NoError(t, err)

upgradeKeeper.EndBlock(ctx)
_, err = upgradeKeeper.TryUpgrade(goCtx, &types.MsgTryUpgrade{})
require.NoError(t, err)
shouldUpgrade, version = upgradeKeeper.ShouldUpgrade()
require.True(t, shouldUpgrade)
require.Equal(t, uint64(2), version)
Expand Down
24 changes: 23 additions & 1 deletion x/upgrade/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ const (
QuerierRoute = ModuleName
)

var _ sdk.Msg = &MsgSignalVersion{}
var (
_ sdk.Msg = &MsgSignalVersion{}
_ sdk.Msg = &MsgTryUpgrade{}
)

func NewMsgSignalVersion(valAddress sdk.ValAddress, version uint64) *MsgSignalVersion {
return &MsgSignalVersion{
Expand All @@ -36,3 +39,22 @@ func (msg *MsgSignalVersion) ValidateBasic() error {
_, err := sdk.ValAddressFromBech32(msg.ValidatorAddress)
return err
}

func NewMsgTryUpgrade(signer sdk.AccAddress) *MsgTryUpgrade {
return &MsgTryUpgrade{
Signer: signer.String(),
}
}

func (msg *MsgTryUpgrade) GetSigners() []sdk.AccAddress {
valAddr, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{sdk.AccAddress(valAddr)}
}

func (msg *MsgTryUpgrade) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Signer)
return err
}
Loading

0 comments on commit 95bda40

Please sign in to comment.