Skip to content

Commit

Permalink
prepare upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Oct 31, 2024
1 parent 0745e90 commit 5c5c194
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ARG TARGETARCH
ARG GOARCH

# See https://github.com/initia-labs/movevm/releases
ENV LIBMOVEVM_VERSION=v0.5.0
ENV LIBMOVEVM_VERSION=v0.6.0

# Install necessary packages
RUN set -eux; apk add --no-cache ca-certificates build-base git cmake
Expand Down
81 changes: 68 additions & 13 deletions app/upgrade.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,98 @@
package app

import (
"bytes"
"context"
"encoding/base64"
"errors"
"fmt"

"cosmossdk.io/errors"
"cosmossdk.io/collections"
sdkerrors "cosmossdk.io/errors"
upgradetypes "cosmossdk.io/x/upgrade/types"
"github.com/cosmos/cosmos-sdk/types/module"

movetypes "github.com/initia-labs/initia/x/move/types"
vmprecompile "github.com/initia-labs/movevm/precompile"
vmtypes "github.com/initia-labs/movevm/types"
)

const upgradeName = "0.5.4"
const upgradeName = "0.6.0"

// RegisterUpgradeHandlers returns upgrade handlers
func (app *MinitiaApp) RegisterUpgradeHandlers(cfg module.Configurator) {
app.UpgradeKeeper.SetUpgradeHandler(
upgradeName,
func(ctx context.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
// diff: https://github.com/initia-labs/movevm/compare/v0.4.12...v0.5.0
codes := []string{
// cosmos.move
"oRzrCwYAAAALAQAQAhAaAyqXAQTBAQIFwwGQBAfTBZ4FCPEKIAaRCxoQqwt0Cp8MGgy5DPUJAAAACAAPABEAEgAZACgAMAABAwAABQMAAQcHAAIOBwEAAQMQCwAGJwgAAA0AAQAEEwIDAAEUBAUAABUGAQAAFgcBAAAXCAEAAAIBCgAAGAwKAAUaDQ0AARsEDgAAHAEKAAAdDAoAAB4QAQAAHxEB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EQBMAAAAAAAAAAAwOBgAAAAAAAAAACgRBFAwGDAcKBwoGIwRDCgQKB0IUDQ4MCRECFAwLCwkLC0QTCwcGAQAAAAAAAAAWDAcFRgsEAQVHBS8LDgwPCAwQCwgLAQsNCw8LEBESAhQBBAAgRAsAEQEOARECFA4CDgMMCkATAAAAAAAAAAAMCwYAAAAAAAAAAAoKQRQMDAwNDA4MDwwQCg0KDCMEJgoKCg1CFA0LDBERAhQMEwsRCxNEEwsNBgEAAAAAAAAAFgwNBSkLCgEFKgUSCwsMFQ4EEQIUDgURAhQOCRECFAwWDBcMEwsQCw8LDgsVCxMLFwsGCwcLCAsWERUCFQACABYBBAAjFAsAEQEOARECFA4CEQIUDgMOBQ4HDAkMCgsECwoLBgsJCwgRFwIXAAIAGAEEACYICwARAREKDAILAQsCERkCGQACABoBBAAqDwcAERsLAQsCCwMLBBIBDAUOBTgADAYLAAsGERgCHQEAAAEGCwARAQsBCwIRGQIFAAIAAA==",

// 1. publish new code module first
codeModuleBz, err := vmprecompile.ReadStdlib("code.mv")
if err != nil {
return nil, err
}
err = app.MoveKeeper.SetModule(ctx, vmtypes.StdAddress, movetypes.MoveModuleNameCode, codeModuleBz[0])
if err != nil {
return nil, err
}

// 2. update vm data with new seperator and add checksums of each module

// Previous:
// ModuleSeparator = byte(0)
// ResourceSeparator = byte(1)
// TableEntrySeparator = byte(2)
// TableInfoSeparator = byte(3)

// Current:
// ModuleSeparator = byte(0)
// ChecksumSeparator = byte(1)
// ResourceSeparator = byte(2)
// TableEntrySeparator = byte(3)
// TableInfoSeparator = byte(4)

err = app.MoveKeeper.VMStore.Walk(ctx, new(collections.Range[[]byte]).Descending(), func(key, value []byte) (stop bool, err error) {
key = bytes.Clone(key)

cursor := movetypes.AddressBytesLength
if len(key) <= cursor {
return true, fmt.Errorf("invalid key length: %d", len(key))
}

separator := key[cursor]
if separator == movetypes.ModuleSeparator {
checksum := movetypes.ModuleBzToChecksum(value)
value = checksum[:]
} else if separator >= movetypes.TableInfoSeparator {
return true, errors.New("unknown prefix")
} else if err = app.MoveKeeper.VMStore.Remove(ctx, key); err != nil {
return true, err
}

modules := make([]vmtypes.Module, len(codes))
for i, code := range codes {
codeBz, err := base64.StdEncoding.DecodeString(code)
key[cursor] = key[cursor] + 1
err = app.MoveKeeper.VMStore.Set(ctx, key, value)
if err != nil {
return nil, errors.Wrap(err, "failed to decode module code")
return true, err
}

return false, nil
})
if err != nil {
return nil, err
}

// 3. update new modules

codesBz, err := vmprecompile.ReadMinlib("object_code_deployment.mv", "coin.mv", "cosmos.mv", "dex.mv", "json.mv", "bech32.mv", "hash.mv", "collection.mv")
if err != nil {
return nil, err
}
modules := make([]vmtypes.Module, len(codesBz))
for i, codeBz := range codesBz {
modules[i] = vmtypes.NewModule(codeBz)
}

err := app.MoveKeeper.PublishModuleBundle(ctx, vmtypes.StdAddress, vmtypes.NewModuleBundle(modules...), movetypes.UpgradePolicy_COMPATIBLE)
err = app.MoveKeeper.PublishModuleBundle(ctx, vmtypes.StdAddress, vmtypes.NewModuleBundle(modules...), movetypes.UpgradePolicy_COMPATIBLE)
if err != nil {
return nil, errors.Wrap(err, "failed to publish module bundle")
return nil, sdkerrors.Wrap(err, "failed to publish module bundle")
}

return vm, nil
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22.5
require (
cosmossdk.io/api v0.7.5
cosmossdk.io/client/v2 v2.0.0-beta.4
cosmossdk.io/collections v0.4.0
cosmossdk.io/core v0.11.1
cosmossdk.io/errors v1.0.1
cosmossdk.io/log v1.4.1
Expand All @@ -26,14 +27,14 @@ require (
github.com/golang/mock v1.6.0
github.com/gorilla/mux v1.8.1
github.com/initia-labs/OPinit v0.5.5
github.com/initia-labs/initia v0.5.7
github.com/initia-labs/initia v0.6.0
github.com/initia-labs/kvindexer v0.1.9
github.com/initia-labs/kvindexer/submodules/block v0.1.0
github.com/initia-labs/kvindexer/submodules/move-nft v0.1.5
github.com/initia-labs/kvindexer/submodules/pair v0.1.1
github.com/initia-labs/kvindexer/submodules/tx v0.1.0
// we also need to update `LIBMOVEVM_VERSION` of Dockerfile#5
github.com/initia-labs/movevm v0.5.0
github.com/initia-labs/movevm v0.6.0
github.com/noble-assets/forwarding/v2 v2.0.0-20240521090705-86712c4c9e43
github.com/pkg/errors v0.9.1
github.com/rakyll/statik v0.1.7
Expand All @@ -53,7 +54,6 @@ require (
cloud.google.com/go/compute/metadata v0.5.0 // indirect
cloud.google.com/go/iam v1.1.12 // indirect
cloud.google.com/go/storage v1.41.0 // indirect
cosmossdk.io/collections v0.4.0 // indirect
cosmossdk.io/depinject v1.0.0 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1426,8 +1426,8 @@ github.com/initia-labs/cometbft-client v0.0.0-20240924071428-ef115cefa07e h1:k+p
github.com/initia-labs/cometbft-client v0.0.0-20240924071428-ef115cefa07e/go.mod h1:aVposiPW9FOUeAeJ7JjJRdE3g+L6i8YDxFn6Cv6+Az4=
github.com/initia-labs/ibc-go/v8 v8.0.0-20240802003717-19c0b4ad450d h1:TLq8lB1PtQ0pjGf+bN8YgGVeLMuytZ26SBGMOs1seKY=
github.com/initia-labs/ibc-go/v8 v8.0.0-20240802003717-19c0b4ad450d/go.mod h1:zh6x1osR0hNvEcFrC/lhGD08sMfQmr9wHVvZ/mRWMCs=
github.com/initia-labs/initia v0.5.7 h1:N0QKlSgvlBtsWXesIwSj/VzfLxS//Q58/lMGIz45pg8=
github.com/initia-labs/initia v0.5.7/go.mod h1:BPtxDrbu3HHnlZ8EGb3AZ5PDR4d+UVR5KMeX0K+oGI8=
github.com/initia-labs/initia v0.6.0 h1:/39ZN26zeixxZZdcfY1sOitiBhfnG3lcbPtpFqd9z7A=
github.com/initia-labs/initia v0.6.0/go.mod h1:nTHzSyI6Ldv8uG98h/A6N+h/qcfrlbALB7UDabWqenY=
github.com/initia-labs/kvindexer v0.1.9 h1:rv/zD8VMU0+P7f+rTtYefIG93f3vRy55JqNK+fohQr8=
github.com/initia-labs/kvindexer v0.1.9/go.mod h1:OV85HaQ9KVrg+zGPUlxT9RF9nAaM3Yq4/3MoHqGqhWk=
github.com/initia-labs/kvindexer/submodules/block v0.1.0 h1:y+EXnksd/I2F96mzIoQA64nZUZON2P+99YrSzeLCLoY=
Expand All @@ -1438,8 +1438,8 @@ github.com/initia-labs/kvindexer/submodules/pair v0.1.1 h1:o151gA4jIbqEl+pWTOCiz
github.com/initia-labs/kvindexer/submodules/pair v0.1.1/go.mod h1:8X1GE1ZLkH7z8TKb5MUh7UClTkcqVFIwXIIRdsqeUZY=
github.com/initia-labs/kvindexer/submodules/tx v0.1.0 h1:6kbf6wmzXPN0XCQLasiFgq1AlZHkt5K3/ZG+IWw1nNs=
github.com/initia-labs/kvindexer/submodules/tx v0.1.0/go.mod h1:i0XeLbLa6xdgTR01WF8kaAO50vMmwxbeq0fKexwpFHU=
github.com/initia-labs/movevm v0.5.0 h1:dBSxoVyUumSE4x6/ZSOWtvbtZpw+V4W25/NH6qLU0uQ=
github.com/initia-labs/movevm v0.5.0/go.mod h1:aUWdvFZPdULjJ2McQTE+mLnfnG3CLAz0TWJRFzFFUwg=
github.com/initia-labs/movevm v0.6.0 h1:46JjDfOId6hfeoqYqKWTzPeb5ESQzzm8AhhGWYnFOmE=
github.com/initia-labs/movevm v0.6.0/go.mod h1:aUWdvFZPdULjJ2McQTE+mLnfnG3CLAz0TWJRFzFFUwg=
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jhump/protoreflect v1.15.3 h1:6SFRuqU45u9hIZPJAoZ8c28T3nK64BNdp9w6jFonzls=
Expand Down

0 comments on commit 5c5c194

Please sign in to comment.