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

feat: v14 migration #1817

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion Dockerfile-versioned-source
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ WORKDIR /go/delivery/zeta-node
RUN mkdir -p $GOPATH/bin/old
RUN mkdir -p $GOPATH/bin/new

ENV NEW_VERSION=v13
ENV NEW_VERSION=v14

# Build new release from the current source
COPY go.mod /go/delivery/zeta-node/
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ stateful-upgrade:

stateful-upgrade-source:
@echo "--> Starting stateful smoketest"
$(DOCKER) build --build-arg old_version=v12.2.1 -t zetanode -f ./Dockerfile-versioned-source .
$(DOCKER) build --build-arg old_version=v13.0.0 -t zetanode -f ./Dockerfile-versioned-source .
$(DOCKER) build -t orchestrator -f contrib/localnet/orchestrator/Dockerfile-upgrade.fastbuild .
cd contrib/localnet/ && $(DOCKER) compose -f docker-compose-stateful.yml up -d

Expand Down
2 changes: 1 addition & 1 deletion app/setup_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types"
)

const releaseVersion = "v13"
const releaseVersion = "v14"

func SetupHandlers(app *App) {
app.UpgradeKeeper.SetUpgradeHandler(releaseVersion, func(ctx sdk.Context, plan types.Plan, vm module.VersionMap) (module.VersionMap, error) {
Expand Down
53 changes: 50 additions & 3 deletions x/crosschain/migrations/v5/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,60 @@ type crosschainKeeper interface {

// 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(
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 {
Expand Down Expand Up @@ -77,7 +125,6 @@ func MigrateStore(

return nil
}

func GetAbortedAmount(cctx types.CrossChainTx) sdkmath.Uint {
if cctx.OutboundTxParams != nil && !cctx.GetCurrentOutTxParam().Amount.IsZero() {
return cctx.GetCurrentOutTxParam().Amount
Expand Down
111 changes: 111 additions & 0 deletions x/crosschain/migrations/v5/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (
"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/common"
keepertest "github.com/zeta-chain/zetacore/testutil/keeper"
"github.com/zeta-chain/zetacore/testutil/sample"
crosschainkeeper "github.com/zeta-chain/zetacore/x/crosschain/keeper"
v5 "github.com/zeta-chain/zetacore/x/crosschain/migrations/v5"
crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
)

func TestMigrateStore(t *testing.T) {
Expand Down Expand Up @@ -61,6 +63,115 @@ func TestMigrateStore(t *testing.T) {

}

func TestResetTestnetNonce(t *testing.T) {
t.Run("reset only testnet nonce without changing mainnet chains", func(t *testing.T) {
k, ctx, _, zk := keepertest.CrosschainKeeper(t)
testnetChains := []common.Chain{common.GoerliChain(), common.MumbaiChain(), common.BscTestnetChain(), common.BtcTestNetChain()}
mainnetChains := []common.Chain{common.EthChain(), common.BscMainnetChain(), common.BtcMainnetChain()}
nonceLow := int64(1)
nonceHigh := int64(10)
tss := sample.Tss()
zk.ObserverKeeper.SetTSS(ctx, tss)
for _, chain := range mainnetChains {
zk.ObserverKeeper.SetChainNonces(ctx, observertypes.ChainNonces{
Index: chain.ChainName.String(),
ChainId: chain.ChainId,
Nonce: uint64(nonceHigh),
})
zk.ObserverKeeper.SetPendingNonces(ctx, observertypes.PendingNonces{
Tss: tss.TssPubkey,
ChainId: chain.ChainId,
NonceLow: nonceLow,
NonceHigh: nonceHigh,
})
}
for _, chain := range testnetChains {
zk.ObserverKeeper.SetPendingNonces(ctx, observertypes.PendingNonces{
Tss: tss.TssPubkey,
ChainId: chain.ChainId,
NonceLow: nonceLow,
NonceHigh: nonceHigh,
})
zk.ObserverKeeper.SetChainNonces(ctx, observertypes.ChainNonces{
Index: chain.ChainName.String(),
ChainId: chain.ChainId,
Nonce: uint64(nonceHigh),
})
}
err := v5.MigrateStore(ctx, k, zk.ObserverKeeper)
require.NoError(t, err)
assertValues := map[common.Chain]int64{
common.GoerliChain(): 226841,
common.MumbaiChain(): 200599,
common.BscTestnetChain(): 110454,
common.BtcTestNetChain(): 4881,
}

for _, chain := range testnetChains {
pn, found := zk.ObserverKeeper.GetPendingNonces(ctx, tss.TssPubkey, chain.ChainId)
require.True(t, found)
require.Equal(t, assertValues[chain], pn.NonceHigh)
require.Equal(t, assertValues[chain], pn.NonceLow)
cn, found := zk.ObserverKeeper.GetChainNonces(ctx, chain.ChainName.String())
require.True(t, found)
require.Equal(t, uint64(assertValues[chain]), cn.Nonce)
}
for _, chain := range mainnetChains {
pn, found := zk.ObserverKeeper.GetPendingNonces(ctx, tss.TssPubkey, chain.ChainId)
require.True(t, found)
require.Equal(t, nonceHigh, pn.NonceHigh)
require.Equal(t, nonceLow, pn.NonceLow)
cn, found := zk.ObserverKeeper.GetChainNonces(ctx, chain.ChainName.String())
require.True(t, found)
require.Equal(t, uint64(nonceHigh), cn.Nonce)
}
})

t.Run("reset nonce even if some chain values are missing", func(t *testing.T) {
k, ctx, _, zk := keepertest.CrosschainKeeper(t)
testnetChains := []common.Chain{common.GoerliChain()}
nonceLow := int64(1)
nonceHigh := int64(10)
tss := sample.Tss()
zk.ObserverKeeper.SetTSS(ctx, tss)
for _, chain := range testnetChains {
zk.ObserverKeeper.SetPendingNonces(ctx, observertypes.PendingNonces{
Tss: tss.TssPubkey,
ChainId: chain.ChainId,
NonceLow: nonceLow,
NonceHigh: nonceHigh,
})
zk.ObserverKeeper.SetChainNonces(ctx, observertypes.ChainNonces{
Index: chain.ChainName.String(),
ChainId: chain.ChainId,
Nonce: uint64(nonceHigh),
})
}
err := v5.MigrateStore(ctx, k, zk.ObserverKeeper)
require.NoError(t, err)
assertValuesSet := map[common.Chain]int64{
common.GoerliChain(): 226841,
}
assertValuesNotSet := []common.Chain{common.MumbaiChain(), common.BscTestnetChain(), common.BtcTestNetChain()}

for _, chain := range testnetChains {
pn, found := zk.ObserverKeeper.GetPendingNonces(ctx, tss.TssPubkey, chain.ChainId)
require.True(t, found)
require.Equal(t, assertValuesSet[chain], pn.NonceHigh)
require.Equal(t, assertValuesSet[chain], pn.NonceLow)
cn, found := zk.ObserverKeeper.GetChainNonces(ctx, chain.ChainName.String())
require.True(t, found)
require.Equal(t, uint64(assertValuesSet[chain]), cn.Nonce)
}
for _, chain := range assertValuesNotSet {
_, found := zk.ObserverKeeper.GetPendingNonces(ctx, tss.TssPubkey, chain.ChainId)
require.False(t, found)
_, found = zk.ObserverKeeper.GetChainNonces(ctx, chain.ChainName.String())
require.False(t, found)
}
})
}

func CrossChainTxList(count int) []crosschaintypes.CrossChainTx {
cctxList := make([]crosschaintypes.CrossChainTx, count+100)
i := 0
Expand Down
2 changes: 1 addition & 1 deletion x/crosschain/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

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

// BeginBlock executes all ABCI BeginBlock logic respective to the crosschain module.
func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
Expand Down
Loading