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

optional strategy for maintaining app versions #1819

Closed
wants to merge 2 commits into from
Closed
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
22 changes: 11 additions & 11 deletions app/setup_handlers.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package app

import (
"fmt"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/zeta-chain/zetacore/common"
crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types"
emissionstypes "github.com/zeta-chain/zetacore/x/emissions/types"
fungibletypes "github.com/zeta-chain/zetacore/x/fungible/types"
observerTypes "github.com/zeta-chain/zetacore/x/observer/types"
)

const releaseVersion = "v14"
var releaseVersion = fmt.Sprintf("v%d", common.AppVersion)

func SetupHandlers(app *App) {
app.UpgradeKeeper.SetUpgradeHandler(releaseVersion, func(ctx sdk.Context, plan types.Plan, vm module.VersionMap) (module.VersionMap, error) {
Expand All @@ -17,7 +23,10 @@ func SetupHandlers(app *App) {
for m, mb := range app.mm.Modules {
vm[m] = mb.ConsensusVersion()
}
VersionMigrator{v: vm}.TriggerMigration(crosschaintypes.ModuleName)
zetaModules := []string{crosschaintypes.ModuleName, observerTypes.ModuleName, fungibletypes.ModuleName, emissionstypes.ModuleName}
for _, zetaModule := range zetaModules {
vm[zetaModule] = common.AppVersion - 1
}

return app.mm.RunMigrations(ctx, app.configurator, vm)
})
Expand All @@ -37,12 +46,3 @@ func SetupHandlers(app *App) {
app.SetStoreLoader(types.UpgradeStoreLoader(upgradeInfo.Height, &storeUpgrades))
}
}

type VersionMigrator struct {
v module.VersionMap
}

func (v VersionMigrator) TriggerMigration(moduleName string) module.VersionMap {
v.v[moduleName] = v.v[moduleName] - 1
return v.v
}
2 changes: 2 additions & 0 deletions common/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ var (
CommitHash = ""
BuildTime = ""
)

const AppVersion = 14
5 changes: 5 additions & 0 deletions x/crosschain/keeper/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
v14 "github.com/zeta-chain/zetacore/x/crosschain/migrations/v14"
v2 "github.com/zeta-chain/zetacore/x/crosschain/migrations/v2"
v3 "github.com/zeta-chain/zetacore/x/crosschain/migrations/v3"
v4 "github.com/zeta-chain/zetacore/x/crosschain/migrations/v4"
Expand Down Expand Up @@ -39,3 +40,7 @@ func (m Migrator) Migrate3to4(ctx sdk.Context) error {
func (m Migrator) Migrate4to5(ctx sdk.Context) error {
return v5.MigrateStore(ctx, m.crossChainKeeper, m.crossChainKeeper.zetaObserverKeeper)
}

func (m Migrator) Migrate13to14(ctx sdk.Context) error {
return v14.MigrateStore(ctx, m.crossChainKeeper, m.crossChainKeeper.zetaObserverKeeper)
}
137 changes: 137 additions & 0 deletions x/crosschain/migrations/v14/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package v14

import (
"fmt"

sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/zeta-chain/zetacore/common"
"github.com/zeta-chain/zetacore/x/crosschain/types"
)

// crosschainKeeper is an interface to prevent cyclic dependency
type crosschainKeeper interface {
GetStoreKey() storetypes.StoreKey
GetCodec() codec.Codec
GetAllCrossChainTx(ctx sdk.Context) []types.CrossChainTx

SetCrossChainTx(ctx sdk.Context, cctx types.CrossChainTx)
AddFinalizedInbound(ctx sdk.Context, inboundTxHash string, senderChainID int64, height uint64)

SetZetaAccounting(ctx sdk.Context, accounting types.ZetaAccounting)
}

// MigrateStore migrates the x/crosschain module state from the consensus version 4 to 5
// It resets the aborted zeta amount to use the inbound tx amount instead in situations where the outbound cctx is never created.
func MigrateStore(ctx sdk.Context, crosschainKeeper crosschainKeeper, observerKeeper types.ObserverKeeper) error {
err := SetZetaAccounting(ctx, crosschainKeeper, observerKeeper)
if err != nil {
return err
}
ResetTestnetNonce(ctx, observerKeeper)

return nil
}

func ResetTestnetNonce(
ctx sdk.Context,
observerKeeper types.ObserverKeeper,
) {
tss, found := observerKeeper.GetTSS(ctx)
if !found {
return
}
for chain, nonce := range CurrentTestnetChains() {
cn, found := observerKeeper.GetChainNonces(ctx, chain.ChainName.String())
if !found {
continue
}
cn.Nonce = nonce.nonceHigh
observerKeeper.SetChainNonces(ctx, cn)
pn, found := observerKeeper.GetPendingNonces(ctx, tss.TssPubkey, chain.ChainId)
if !found {
continue
}
pn.NonceLow = int64(nonce.nonceLow)
pn.NonceHigh = int64(nonce.nonceHigh)
observerKeeper.SetPendingNonces(ctx, pn)
}
}

type Nonce struct {
nonceHigh uint64
nonceLow uint64
}

func CurrentTestnetChains() map[common.Chain]Nonce {
return map[common.Chain]Nonce{
common.GoerliChain(): {nonceHigh: 226841, nonceLow: 226841},
common.MumbaiChain(): {nonceHigh: 200599, nonceLow: 200599},
common.BscTestnetChain(): {nonceHigh: 110454, nonceLow: 110454},
common.BtcTestNetChain(): {nonceHigh: 4881, nonceLow: 4881},
}
}

func SetZetaAccounting(
ctx sdk.Context,
crosschainKeeper crosschainKeeper,
observerKeeper types.ObserverKeeper,
) error {
ccctxList := crosschainKeeper.GetAllCrossChainTx(ctx)
abortedAmountZeta := sdkmath.ZeroUint()
for _, cctx := range ccctxList {
if cctx.CctxStatus.Status == types.CctxStatus_Aborted {

switch cctx.InboundTxParams.CoinType {
case common.CoinType_ERC20:
{
receiverChain := observerKeeper.GetSupportedChainFromChainID(ctx, cctx.GetCurrentOutTxParam().ReceiverChainId)
if receiverChain == nil {
ctx.Logger().Error(fmt.Sprintf("Error getting chain from chain id: %d , cctx index", cctx.GetCurrentOutTxParam().ReceiverChainId), cctx.Index)
continue
}
// There is a chance that this cctx has already been refunded, so we set the isRefunded flag to true.
// Even though, there is a slight possibility that the refund tx failed when doing an auto refund; there is no way for us to know. Which is why we can mark this type of cctx as non-refundable
// Auto refunds are done for ERC20 cctx's when the receiver chain is a zeta chain.
if receiverChain.IsZetaChain() {
cctx.CctxStatus.IsAbortRefunded = true
} else {
cctx.CctxStatus.IsAbortRefunded = false
}
}
case common.CoinType_Zeta:
{
// add the required amount into the zeta accounting.
// GetAbortedAmount replaces using Outbound Amount directly, to make sure we refund the amount deposited by the user if the outbound is never created and the cctx is aborted.
// For these cctx's we allow the refund to be processed later and the Aborted amount would be adjusted when the refund is processed.
abortedValue := GetAbortedAmount(cctx)
abortedAmountZeta = abortedAmountZeta.Add(abortedValue)
cctx.CctxStatus.IsAbortRefunded = false

}
case common.CoinType_Gas:
{
// CointType gas can be processed as normal and we can issue the refund using the admin refund tx .
cctx.CctxStatus.IsAbortRefunded = false
}
}
crosschainKeeper.SetCrossChainTx(ctx, cctx)
}

}
crosschainKeeper.SetZetaAccounting(ctx, types.ZetaAccounting{AbortedZetaAmount: abortedAmountZeta})

return nil
}
func GetAbortedAmount(cctx types.CrossChainTx) sdkmath.Uint {
if cctx.OutboundTxParams != nil && !cctx.GetCurrentOutTxParam().Amount.IsZero() {
return cctx.GetCurrentOutTxParam().Amount
}
if cctx.InboundTxParams != nil {
return cctx.InboundTxParams.Amount
}

return sdkmath.ZeroUint()
}
Loading
Loading