Skip to content

Commit

Permalink
add get&set checksums, genesis import&export module checksums
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-cha committed Oct 30, 2024
1 parent f31a628 commit 0d8f917
Show file tree
Hide file tree
Showing 12 changed files with 3,851 additions and 378 deletions.
197 changes: 131 additions & 66 deletions api/initia/intertx/v1/tx.pulsar.go

Large diffs are not rendered by default.

359 changes: 256 additions & 103 deletions api/initia/move/v1/genesis.pulsar.go

Large diffs are not rendered by default.

805 changes: 712 additions & 93 deletions api/initia/move/v1/types.pulsar.go

Large diffs are not rendered by default.

2,044 changes: 2,044 additions & 0 deletions api/initia/tx/v1/query.pulsar.go

Large diffs are not rendered by default.

150 changes: 150 additions & 0 deletions api/initia/tx/v1/query_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions proto/initia/move/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,27 @@ message GenesisState {
(gogoproto.nullable) = false,
(amino.dont_omitempty) = true
];
repeated Resource resources = 5 [
repeated Checksum checksums = 5 [
(gogoproto.jsontag) = "checksums,omitempty",
(gogoproto.nullable) = false,
(amino.dont_omitempty) = true
];
repeated Resource resources = 6 [
(gogoproto.jsontag) = "resources,omitempty",
(gogoproto.nullable) = false,
(amino.dont_omitempty) = true
];
repeated TableInfo table_infos = 6 [
repeated TableInfo table_infos = 7 [
(gogoproto.jsontag) = "table_infos,omitempty",
(gogoproto.nullable) = false,
(amino.dont_omitempty) = true
];
repeated TableEntry table_entries = 7 [
repeated TableEntry table_entries = 8 [
(gogoproto.jsontag) = "table_entries,omitempty",
(gogoproto.nullable) = false,
(amino.dont_omitempty) = true
];
repeated DexPair dex_pairs = 8 [
repeated DexPair dex_pairs = 9 [
(gogoproto.jsontag) = "dex_pairs,omitempty",
(gogoproto.nullable) = false,
(amino.dont_omitempty) = true
Expand Down
9 changes: 7 additions & 2 deletions proto/initia/move/v1/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ message RawParams {
}

// Module is data for the uploaded contract move code
// ex) 0000000000000000000000000000000000000001/0/BasicCoin
message Module {
string address = 1;
string module_name = 2;
Expand All @@ -73,8 +72,14 @@ message Module {
UpgradePolicy upgrade_policy = 5;
}

// Checksum is checksum of the uploaded contract move code
message Checksum {
string address = 1;
string module_name = 2;
bytes checksum = 3;
}

// Resource is data for the stored move resource
// ex) 0000000000000000000000000000000000000002/2/0x1::BasicCoin::Coin<0x1::BasicCoin::Initia>
message Resource {
string address = 1;
string struct_tag = 2;
Expand Down
18 changes: 18 additions & 0 deletions x/move/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ func (k Keeper) InitGenesis(ctx context.Context, moduleNames []string, genState
}
}

for _, checksum := range genState.GetChecksums() {
addr, err := types.AccAddressFromString(k.ac, checksum.Address)
if err != nil {
return err
}

if err := k.SetChecksum(ctx, addr, checksum.ModuleName, checksum.Checksum); err != nil {
return err
}
}

for _, resource := range genState.GetResources() {
addr, err := types.AccAddressFromString(k.ac, resource.Address)
if err != nil {
Expand Down Expand Up @@ -140,11 +151,13 @@ func (k Keeper) ExportGenesis(ctx context.Context) *types.GenesisState {
}

var modules []types.Module
var checksums []types.Checksum
var resources []types.Resource
var tableEntries []types.TableEntry
var tableInfos []types.TableInfo
err = k.IterateVMStore(ctx, func(
module *types.Module,
checksum *types.Checksum,
resource *types.Resource,
tableInfo *types.TableInfo,
tableEntry *types.TableEntry,
Expand All @@ -153,6 +166,10 @@ func (k Keeper) ExportGenesis(ctx context.Context) *types.GenesisState {
modules = append(modules, *module)
}

if checksum != nil {
checksums = append(checksums, *checksum)
}

if resource != nil {
resources = append(resources, *resource)
}
Expand Down Expand Up @@ -182,6 +199,7 @@ func (k Keeper) ExportGenesis(ctx context.Context) *types.GenesisState {
}

genState.Modules = modules
genState.Checksums = checksums
genState.Resources = resources
genState.TableInfos = tableInfos
genState.TableEntries = tableEntries
Expand Down
43 changes: 39 additions & 4 deletions x/move/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,29 @@ func (k Keeper) SetChecksum(
return nil
}

// GetChecksum return checksum of module of the given account address and name
func (k Keeper) GetChecksum(
ctx context.Context,
addr vmtypes.AccountAddress,
moduleName string,
) (types.Checksum, error) {
bz, err := types.GetChecksumKey(addr, moduleName)
if err != nil {
return types.Checksum{}, err
}

checksumBytes, err := k.VMStore.Get(ctx, bz)
if err != nil {
return types.Checksum{}, err
}

return types.Checksum{
Address: addr.String(),
ModuleName: moduleName,
Checksum: checksumBytes,
}, nil
}

// SetModule store Module bytes
// This function should be used only when InitGenesis
func (k Keeper) SetModule(
Expand Down Expand Up @@ -526,7 +549,7 @@ func (k Keeper) SetTableEntry(
}

// IterateVMStore iterate VMStore store for genesis export
func (k Keeper) IterateVMStore(ctx context.Context, cb func(*types.Module, *types.Resource, *types.TableInfo, *types.TableEntry)) error {
func (k Keeper) IterateVMStore(ctx context.Context, cb func(*types.Module, *types.Checksum, *types.Resource, *types.TableInfo, *types.TableEntry)) error {
err := k.VMStore.Walk(ctx, nil, func(key, value []byte) (stop bool, err error) {
cursor := types.AddressBytesLength
addrBytes := key[:cursor]
Expand Down Expand Up @@ -555,6 +578,18 @@ func (k Keeper) IterateVMStore(ctx context.Context, cb func(*types.Module, *type
ModuleName: string(moduleName),
RawBytes: value,
UpgradePolicy: policy,
}, nil, nil, nil, nil)
} else if separator == types.ChecksumSeparator {
// Checksum
moduleName, err := vmtypes.BcsDeserializeIdentifier(key[cursor:])
if err != nil {
return true, err
}

cb(nil, &types.Checksum{
Address: vmAddr.String(),
ModuleName: string(moduleName),
Checksum: value,
}, nil, nil, nil)
} else if separator == types.ResourceSeparator {
// Resource
Expand All @@ -568,7 +603,7 @@ func (k Keeper) IterateVMStore(ctx context.Context, cb func(*types.Module, *type
return true, err
}

cb(nil, &types.Resource{
cb(nil, nil, &types.Resource{
Address: vmAddr.String(),
StructTag: structTagStr,
RawBytes: value,
Expand All @@ -590,14 +625,14 @@ func (k Keeper) IterateVMStore(ctx context.Context, cb func(*types.Module, *type
return true, err
}

cb(nil, nil, &types.TableInfo{
cb(nil, nil, nil, &types.TableInfo{
Address: vmAddr.String(),
KeyType: keyType,
ValueType: valueType,
}, nil)
} else if separator == types.TableEntrySeparator {
// Table Entry
cb(nil, nil, nil, &types.TableEntry{
cb(nil, nil, nil, nil, &types.TableEntry{
Address: vmAddr.String(),
KeyBytes: key[cursor:],
ValueBytes: value,
Expand Down
Loading

0 comments on commit 0d8f917

Please sign in to comment.