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

prepare initiation-2 upgrade #292

Merged
merged 17 commits into from
Oct 31, 2024
89 changes: 74 additions & 15 deletions app/upgrade.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,104 @@
package app

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

"cosmossdk.io/errors"
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.6"
const upgradeName = "0.6.0"

// RegisterUpgradeHandlers returns upgrade handlers
func (app *InitiaApp) 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
"oRzrCwYAAAALAQAQAhAaAyqXAQTBAQIFwwGQBAfTBZ4FCPEKIAaRCxoQqwt0Cp8MGgy5DPUJAAAACAAPABEAEgAZACgAMAABAwAABQMAAQcHAAIOBwEAAQMQCwAGJwgAAA0AAQAEEwIDAAEUBAUAABUGAQAAFgcBAAAXCAEAAAIBCgAAGAwKAAUaDQ0AARsEDgAAHAEKAAAdDAoAAB4QAQAAHxEBAAAgEgEAACEVAQAAIhcBAAAjGQEAACQaAQAAJRwBAAAmHgEAACkfAQAAKiEBAAArIgEAACwkAQAALSUBAAAuJwEAAS8TFAAHMSkTAQIAMisBABwoBAYMCAILAwEIBAMAAQYMAQUBBggCAQYKAgQFCgIGCwMBCAQDCgYMCAILAwEIBAMIAggCAwMDCAIKBQoCBgsDAQgE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YFAAAAAAAAAAwEDAUMBgsFCwQLBhErDAcOBxEsES0MCAUrBwALABEFDAgLCDgCAi4BAAEEJxQLABAEFAwCDgI4BgwDCgMpBAQLBQ0GAgAAAAAAAAAnCwMrBBAFCwEIES8CMAEAACsKCwALAQsCCwMLBAsFCwYRMQECMQEAADcyDgMRIxQMBwsACwcRMgwIDggMCQoJCwELAgsDCwQLBQsGETMKCRE0CgkRNQoJETYKCRE3DAoOCgwAEgQMDgsACw4tBAoJETgSAjgMCgk4DQwPCg8SBQoPEgALDxIDCwkROwI8AQAABAMLACkEAj0BAAAEBQsACwERBSkEAj4BAAA7Ng4AOAYMAQoAEQcLAAcAOA4MAwwECwMEFgoEDAUHAhFADAYLBQsGIQwDBRgJDAMLAwQeBwMRQAwEBR4HAAoEEQUKASEEJgsEAgcBEUAMBw4BOA8MCA4IEUINBwwJDAQLCQsEEUMLBwJEAQABBCcUCwAQBBQMAg4COAYMAwoDKQQECwUNBgIAAAAAAAAAJwsDKwQQBQsBCREvAgAABAEFAAQAAwAEAgBWAFcA",

// 1. publish new code module first
codeModuleBz, err := vmprecompile.ReadStdlib("code.move")
if err != nil {
return nil, err
}
err = app.MoveKeeper.SetModule(ctx, vmtypes.StdAddress, movetypes.MoveModuleNameCode, codeModuleBz[0])
if err != nil {
return nil, err
}
beer-1 marked this conversation as resolved.
Show resolved Hide resolved

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

type KV struct {
key []byte
value []byte
}
kvs := make([]KV, 0)

// 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, nil, func(key, value []byte) (stop bool, err error) {
cursor := movetypes.AddressBytesLength
separator := key[cursor]

sh-cha marked this conversation as resolved.
Show resolved Hide resolved
if separator == movetypes.ModuleSeparator {
checksum := movetypes.ModuleBzToChecksum(value)
value = checksum[:]
} else if separator >= movetypes.TableInfoSeparator {
return true, errors.New("unknown prefix")
}
sh-cha marked this conversation as resolved.
Show resolved Hide resolved

err = app.MoveKeeper.VMStore.Remove(ctx, key)
beer-1 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return true, err
}
key[cursor] = key[cursor] + 1
sh-cha marked this conversation as resolved.
Show resolved Hide resolved
kvs = append(kvs, KV{
key: bytes.Clone(key),
value: bytes.Clone(value),
})
return false, nil
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add bounds check before key modification.

The key modification at line 92 could cause a panic if cursor is out of bounds.

Add a safety check:

+				if len(key) <= cursor {
+					return true, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "invalid key length for separator increment")
+				}
				key[cursor] = key[cursor] + 1
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
key[cursor] = key[cursor] + 1
kvs = append(kvs, KV{
key: bytes.Clone(key),
value: bytes.Clone(value),
})
return false, nil
if len(key) <= cursor {
return true, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "invalid key length for separator increment")
}
key[cursor] = key[cursor] + 1
kvs = append(kvs, KV{
key: bytes.Clone(key),
value: bytes.Clone(value),
})
return false, nil

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

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

// 3. update new modules
codesBz, err := vmprecompile.ReadStdlib("object_code_deployment.move", "coin.move", "dex.move", "json.move", "bech32.move", "hash.move")
sh-cha marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
modules := make([]vmtypes.Module, len(codesBz))
for i, codeBz := range codesBz {
modules[i] = vmtypes.NewModule(codeBz)
beer-1 marked this conversation as resolved.
Show resolved Hide resolved
}

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
29 changes: 1 addition & 28 deletions x/move/keeper/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/cosmos/gogoproto/proto"
"github.com/initia-labs/initia/x/move/ante"
"github.com/initia-labs/initia/x/move/types"
vmapi "github.com/initia-labs/movevm/api"
vmtypes "github.com/initia-labs/movevm/types"
)

Expand All @@ -33,21 +32,6 @@ func isSimulation(
return sdkCtx.ExecMode() == sdk.ExecModeSimulate
}

// extract module address and module name from the compiled module bytes
func (k Keeper) extractModuleIdentifier(moduleBundle vmtypes.ModuleBundle) ([]string, error) {
modules := make([]string, len(moduleBundle.Codes))
for i, moduleBz := range moduleBundle.Codes {
moduleAddr, moduleName, err := vmapi.ReadModuleInfo(moduleBz.Code)
if err != nil {
return nil, err
}

modules[i] = vmtypes.NewModuleId(moduleAddr, moduleName).String()
}

return modules, nil
}

////////////////////////////////////////
// Publish Functions

Expand All @@ -57,16 +41,6 @@ func (k Keeper) PublishModuleBundle(
moduleBundle vmtypes.ModuleBundle,
upgradePolicy types.UpgradePolicy,
) error {
moduleIds, err := k.extractModuleIdentifier(moduleBundle)
if err != nil {
return err
}

moduleIdBz, err := json.Marshal(&moduleIds)
if err != nil {
return err
}

moduleCodes := make([]string, len(moduleBundle.Codes))
for i, moduleCode := range moduleBundle.Codes {
// bytes -> hex string
Expand All @@ -88,11 +62,10 @@ func (k Keeper) PublishModuleBundle(
sender,
vmtypes.StdAddress,
types.MoveModuleNameCode,
types.FunctionNameCodePublish,
types.FunctionNameCodePublishV2,
[]vmtypes.TypeTag{},
[]string{
// use unsafe method for fast conversion
unsafe.String(unsafe.SliceData(moduleIdBz), len(moduleIdBz)),
unsafe.String(unsafe.SliceData(moduleCodesBz), len(moduleCodesBz)),
unsafe.String(unsafe.SliceData(upgradePolicyBz), len(upgradePolicyBz)),
},
Expand Down
16 changes: 16 additions & 0 deletions x/move/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,22 @@ func (k Keeper) GetExecutionCounter(
return counter, nil
}

// SetChecksum store checksum bytes
// This function should be used only when Migration
func (k Keeper) SetChecksum(
ctx context.Context,
addr vmtypes.AccountAddress,
moduleName string,
checksum []byte,
) error {
if checksumKey, err := types.GetChecksumKey(addr, moduleName); err != nil {
return err
} else if err := k.VMStore.Set(ctx, checksumKey, checksum); err != nil {
return err
}
return nil
}

// SetModule store Module bytes
// This function should be used only when InitGenesis
func (k Keeper) SetModule(
Expand Down
9 changes: 9 additions & 0 deletions x/move/types/checksum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package types

import (
"crypto/sha256"
)

func ModuleBzToChecksum(moduleBz []byte) [32]byte {
return sha256.Sum256(moduleBz)
}
1 change: 1 addition & 0 deletions x/move/types/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const (

// function names for code
FunctionNameCodePublish = "publish"
FunctionNameCodePublishV2 = "publish_v2"
FunctionNameCodeSetAllowedPublishers = "set_allowed_publishers"

// function names for vesting
Expand Down
23 changes: 20 additions & 3 deletions x/move/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ var (
ParamsKey = []byte{0x31} // prefix for parameters for module x/move

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

// GetModulePrefix returns the prefix key of an account module store
Expand All @@ -86,6 +87,22 @@ func GetModuleKey(addr vmtypes.AccountAddress, moduleName string) ([]byte, error
return append(append(addr.Bytes(), ModuleSeparator), bz...), nil
}

// GetModulePrefix returns the prefix key of an account module store
func GetChecksumPrefix(addr vmtypes.AccountAddress) []byte {
return append(addr.Bytes(), ChecksumSeparator)
}

// GetModuleKey returns the key of the published move module
func GetChecksumKey(addr vmtypes.AccountAddress, moduleName string) ([]byte, error) {
identifier := vmtypes.Identifier(moduleName)
bz, err := identifier.BcsSerialize()
if err != nil {
return nil, err
}

return append(append(addr.Bytes(), ChecksumSeparator), bz...), nil
}

// GetResourcePrefix returns the prefix key of an account resource store
func GetResourcePrefix(addr vmtypes.AccountAddress) []byte {
return append(addr.Bytes(), ResourceSeparator)
Expand Down
Loading