Skip to content

Commit

Permalink
prepare upgrade initiation-2
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-cha committed Oct 29, 2024
1 parent c8a6288 commit 5fc0930
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 36 deletions.
120 changes: 115 additions & 5 deletions app/upgrade.go

Large diffs are not rendered by default.

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

0 comments on commit 5fc0930

Please sign in to comment.