diff --git a/pkg/rpc/clients_cosmos.go b/pkg/rpc/clients_cosmos.go index c6d485dfcb..59be82bfb5 100644 --- a/pkg/rpc/clients_cosmos.go +++ b/pkg/rpc/clients_cosmos.go @@ -11,21 +11,16 @@ import ( "github.com/zeta-chain/node/cmd/zetacored/config" ) -// GetUpgradePlan returns the current upgrade plan. -// Returns empty plan if no upgrade is planned -func (c *Clients) GetUpgradePlan(ctx context.Context) (upgradetypes.Plan, error) { +// GetUpgradePlan returns the current upgrade plan or nil if there is no plan. +func (c *Clients) GetUpgradePlan(ctx context.Context) (*upgradetypes.Plan, error) { in := &upgradetypes.QueryCurrentPlanRequest{} resp, err := c.Upgrade.CurrentPlan(ctx, in) - switch { - case err != nil: - return upgradetypes.Plan{}, errors.Wrap(err, "failed to get current upgrade plan") - case resp.Plan == nil: - // no upgrade planned - return upgradetypes.Plan{}, nil - default: - return *resp.Plan, nil + if err != nil { + return nil, errors.Wrap(err, "failed to get current upgrade plan") } + + return resp.Plan, nil } // GetZetaTokenSupplyOnNode returns the zeta token supply on the node diff --git a/zetaclient/chains/interfaces/interfaces.go b/zetaclient/chains/interfaces/interfaces.go index cd36f938e7..4ed7c0797e 100644 --- a/zetaclient/chains/interfaces/interfaces.go +++ b/zetaclient/chains/interfaces/interfaces.go @@ -135,7 +135,7 @@ type ZetacoreClient interface { GetZetaHotKeyBalance(ctx context.Context) (sdkmath.Int, error) GetInboundTrackersForChain(ctx context.Context, chainID int64) ([]crosschaintypes.InboundTracker, error) - GetUpgradePlan(ctx context.Context) (upgradetypes.Plan, error) + GetUpgradePlan(ctx context.Context) (*upgradetypes.Plan, error) PostOutboundTracker(ctx context.Context, chainID int64, nonce uint64, txHash string) (string, error) } diff --git a/zetaclient/orchestrator/contextupdater.go b/zetaclient/orchestrator/contextupdater.go index ae601ff112..acef329ba5 100644 --- a/zetaclient/orchestrator/contextupdater.go +++ b/zetaclient/orchestrator/contextupdater.go @@ -15,7 +15,7 @@ import ( type Zetacore interface { GetBlockHeight(ctx context.Context) (int64, error) - GetUpgradePlan(ctx context.Context) (upgradetypes.Plan, error) + GetUpgradePlan(ctx context.Context) (*upgradetypes.Plan, error) GetSupportedChains(ctx context.Context) ([]chains.Chain, error) GetAdditionalChains(ctx context.Context) ([]chains.Chain, error) GetCrosschainFlags(ctx context.Context) (observertypes.CrosschainFlags, error) @@ -138,22 +138,23 @@ func UpdateAppContext(ctx context.Context, app *zctx.AppContext, zc Zetacore, lo // returns an error if an upgrade is required func checkForZetacoreUpgrade(ctx context.Context, zetaHeight int64, zc Zetacore) error { plan, err := zc.GetUpgradePlan(ctx) - if err != nil { + switch { + case err != nil: return errors.Wrap(err, "unable to get upgrade plan") - } - - // no upgrade planned - if plan.Height == 0 { + case plan == nil: + // no upgrade planned return nil } - // We can return an error in couple of blocks ahead. - // It's okay because ticker might have a long interval + upgradeHeight := plan.Height + + // We can return an error in a few blocks ahead. + // It's okay because the ticker might have a long interval. const upgradeRange = 2 // Note that after plan.Height's block `x/upgrade` module deletes the plan - if (plan.Height - zetaHeight) <= upgradeRange { - return errors.Wrapf(ErrUpgradeRequired, "current height: %d, upgrade height: %d", zetaHeight, plan.Height) + if (upgradeHeight - zetaHeight) <= upgradeRange { + return errors.Wrapf(ErrUpgradeRequired, "current height: %d, upgrade height: %d", zetaHeight, upgradeHeight) } return nil diff --git a/zetaclient/orchestrator/contextupdater_test.go b/zetaclient/orchestrator/contextupdater_test.go index faed8941a2..cc28d5ad9e 100644 --- a/zetaclient/orchestrator/contextupdater_test.go +++ b/zetaclient/orchestrator/contextupdater_test.go @@ -39,7 +39,7 @@ func Test_UpdateAppContext(t *testing.T) { } zetacore.On("GetBlockHeight", mock.Anything).Return(int64(123), nil) - zetacore.On("GetUpgradePlan", mock.Anything).Return(upgradetypes.Plan{}, nil) + zetacore.On("GetUpgradePlan", mock.Anything).Return(nil, nil) zetacore.On("GetSupportedChains", mock.Anything).Return(newChains, nil) zetacore.On("GetAdditionalChains", mock.Anything).Return(nil, nil) zetacore.On("GetChainParams", mock.Anything).Return(newParams, nil) @@ -68,7 +68,7 @@ func Test_UpdateAppContext(t *testing.T) { ) zetacore.On("GetBlockHeight", mock.Anything).Return(int64(123), nil) - zetacore.On("GetUpgradePlan", mock.Anything).Return(upgradetypes.Plan{ + zetacore.On("GetUpgradePlan", mock.Anything).Return(&upgradetypes.Plan{ Name: "hello", Height: 124, }, nil) diff --git a/zetaclient/testutils/mocks/zetacore_client.go b/zetaclient/testutils/mocks/zetacore_client.go index 609925ecbe..6bbaee022c 100644 --- a/zetaclient/testutils/mocks/zetacore_client.go +++ b/zetaclient/testutils/mocks/zetacore_client.go @@ -585,22 +585,24 @@ func (_m *ZetacoreClient) GetTSSHistory(ctx context.Context) ([]observertypes.TS } // GetUpgradePlan provides a mock function with given fields: ctx -func (_m *ZetacoreClient) GetUpgradePlan(ctx context.Context) (upgradetypes.Plan, error) { +func (_m *ZetacoreClient) GetUpgradePlan(ctx context.Context) (*upgradetypes.Plan, error) { ret := _m.Called(ctx) if len(ret) == 0 { panic("no return value specified for GetUpgradePlan") } - var r0 upgradetypes.Plan + var r0 *upgradetypes.Plan var r1 error - if rf, ok := ret.Get(0).(func(context.Context) (upgradetypes.Plan, error)); ok { + if rf, ok := ret.Get(0).(func(context.Context) (*upgradetypes.Plan, error)); ok { return rf(ctx) } - if rf, ok := ret.Get(0).(func(context.Context) upgradetypes.Plan); ok { + if rf, ok := ret.Get(0).(func(context.Context) *upgradetypes.Plan); ok { r0 = rf(ctx) } else { - r0 = ret.Get(0).(upgradetypes.Plan) + if ret.Get(0) != nil { + r0 = ret.Get(0).(*upgradetypes.Plan) + } } if rf, ok := ret.Get(1).(func(context.Context) error); ok {