Skip to content

Commit b2363ff

Browse files
committed
add changes from jhernandezb:genesisexporter to v0.61.5
1 parent 406c79a commit b2363ff

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

x/wasm/keeper/genesis_exports.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package keeper
2+
3+
import (
4+
"context"
5+
6+
"github.com/CosmWasm/wasmd/x/wasm/types"
7+
sdk "github.com/cosmos/cosmos-sdk/types"
8+
)
9+
10+
// Genesis-specific exports for custom genesis tooling and external integrations.
11+
// These wrap private keeper methods to enable external packages to perform
12+
// genesis operations and state management.
13+
//
14+
// ImportCode wraps the private importCode method to allow external packages
15+
// to import compiled Wasm code during genesis or state migrations.
16+
func (k Keeper) ImportCode(ctx context.Context, codeID uint64, codeInfo types.CodeInfo, wasmCode []byte) error {
17+
return k.importCode(ctx, codeID, codeInfo, wasmCode)
18+
}
19+
20+
// ImportContract wraps the private importContract method to allow external packages
21+
// to import complete contract instances with their state and history during genesis.
22+
//
23+
// This uses the optimized appendToContractHistoryForGenesis internally for performance.
24+
func (k Keeper) ImportContract(ctx context.Context, contractAddr sdk.AccAddress, c *types.ContractInfo, state []types.Model, historyEntries []types.ContractCodeHistoryEntry) error {
25+
return k.importContract(ctx, contractAddr, c, state, historyEntries)
26+
}
27+
28+
// ImportAutoIncrementID wraps the private importAutoIncrementID method to allow
29+
// external packages to set sequence counters for ID generation during genesis import.
30+
func (k Keeper) ImportAutoIncrementID(ctx context.Context, sequenceKey []byte, val uint64) error {
31+
return k.importAutoIncrementID(ctx, sequenceKey, val)
32+
}

x/wasm/keeper/keeper.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,26 @@ func (k Keeper) appendToContractHistory(ctx context.Context, contractAddr sdk.Ac
823823
return nil
824824
}
825825

826+
// appendToContractHistoryForGenesis is optimized for genesis import where
827+
// contract history is known to be empty. It writes entries sequentially starting
828+
// from position 0 without checking for existing entries.
829+
//
830+
// WARNING: Only use during genesis import via importContract(). For runtime operations,
831+
// use appendToContractHistory() which safely handles existing history.
832+
//
833+
// Performance: Avoids O(N) iteration over existing entries when state is known to be empty.
834+
// This provides significant speedup during genesis import with many contracts.
835+
func (k Keeper) appendToContractHistoryForGenesis(ctx context.Context, contractAddr sdk.AccAddress, newEntries ...types.ContractCodeHistoryEntry) error {
836+
store := k.storeService.OpenKVStore(ctx)
837+
for pos, e := range newEntries {
838+
key := types.GetContractCodeHistoryElementKey(contractAddr, uint64(pos))
839+
if err := store.Set(key, k.cdc.MustMarshal(&e)); err != nil {
840+
return err
841+
}
842+
}
843+
return nil
844+
}
845+
826846
func (k Keeper) GetContractHistory(ctx context.Context, contractAddr sdk.AccAddress) []types.ContractCodeHistoryEntry {
827847
prefixStore := prefix.NewStore(runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)), types.GetContractCodeHistoryElementPrefix(contractAddr))
828848
r := make([]types.ContractCodeHistoryEntry, 0)
@@ -1413,6 +1433,8 @@ func (k Keeper) importAutoIncrementID(ctx context.Context, sequenceKey []byte, v
14131433
return store.Set(sequenceKey, bz)
14141434
}
14151435

1436+
// importContract imports a contract instance during genesis initialization.
1437+
// Uses the optimized appendToContractHistoryForGenesis since contract history is known to be empty.
14161438
func (k Keeper) importContract(ctx context.Context, contractAddr sdk.AccAddress, c *types.ContractInfo, state []types.Model, historyEntries []types.ContractCodeHistoryEntry) error {
14171439
if !k.containsCodeInfo(ctx, c.CodeID) {
14181440
return types.ErrNoSuchCodeFn(c.CodeID).Wrapf("code id %d", c.CodeID)
@@ -1429,7 +1451,7 @@ func (k Keeper) importContract(ctx context.Context, contractAddr sdk.AccAddress,
14291451
return err
14301452
}
14311453

1432-
err = k.appendToContractHistory(ctx, contractAddr, historyEntries...)
1454+
err = k.appendToContractHistoryForGenesis(ctx, contractAddr, historyEntries...)
14331455
if err != nil {
14341456
return err
14351457
}

0 commit comments

Comments
 (0)