From 543355ab768d31f076de0b1dbaad06153df68de0 Mon Sep 17 00:00:00 2001 From: Alex Peters Date: Wed, 30 Aug 2023 15:24:01 +0200 Subject: [PATCH] Rename key store key attribute to be more concrete Please enter the commit message for your changes. Lines starting --- x/wasm/alias.go | 4 ++-- x/wasm/keeper/addresses.go | 2 +- x/wasm/keeper/genesis.go | 10 +++++----- x/wasm/keeper/genesis_test.go | 30 +++++++++++++++--------------- x/wasm/keeper/keeper.go | 20 ++++++++++---------- x/wasm/simulation/genesis.go | 2 +- x/wasm/types/keys.go | 4 ++-- 7 files changed, 36 insertions(+), 36 deletions(-) diff --git a/x/wasm/alias.go b/x/wasm/alias.go index c843c0e9a0..931cc837a6 100644 --- a/x/wasm/alias.go +++ b/x/wasm/alias.go @@ -133,9 +133,9 @@ var ( // Deprecated: Do not use. ErrInvalidMsg = types.ErrInvalidMsg // Deprecated: Do not use. - KeyLastCodeID = types.KeyLastCodeID + KeyLastCodeID = types.KeySequenceCodeID // Deprecated: Do not use. - KeyLastInstanceID = types.KeyLastInstanceID + KeyLastInstanceID = types.KeySequenceInstanceID // Deprecated: Do not use. CodeKeyPrefix = types.CodeKeyPrefix // Deprecated: Do not use. diff --git a/x/wasm/keeper/addresses.go b/x/wasm/keeper/addresses.go index 92d0f2e394..9df74f40da 100644 --- a/x/wasm/keeper/addresses.go +++ b/x/wasm/keeper/addresses.go @@ -16,7 +16,7 @@ type AddressGenerator func(ctx sdk.Context, codeID uint64, checksum []byte) sdk. // ClassicAddressGenerator generates a contract address using codeID and instanceID sequence func (k Keeper) ClassicAddressGenerator() AddressGenerator { return func(ctx sdk.Context, codeID uint64, _ []byte) sdk.AccAddress { - instanceID := k.autoIncrementID(ctx, types.KeyLastInstanceID) + instanceID := k.autoIncrementID(ctx, types.KeySequenceInstanceID) return BuildContractAddressClassic(codeID, instanceID) } } diff --git a/x/wasm/keeper/genesis.go b/x/wasm/keeper/genesis.go index 59ab3b4c92..03e54bb2d1 100644 --- a/x/wasm/keeper/genesis.go +++ b/x/wasm/keeper/genesis.go @@ -62,13 +62,13 @@ func InitGenesis(ctx sdk.Context, keeper *Keeper, data types.GenesisState) ([]ab } // sanity check seq values - seqVal := keeper.PeekAutoIncrementID(ctx, types.KeyLastCodeID) + seqVal := keeper.PeekAutoIncrementID(ctx, types.KeySequenceCodeID) if seqVal <= maxCodeID { - return nil, errorsmod.Wrapf(types.ErrInvalid, "seq %s with value: %d must be greater than: %d ", string(types.KeyLastCodeID), seqVal, maxCodeID) + return nil, errorsmod.Wrapf(types.ErrInvalid, "seq %s with value: %d must be greater than: %d ", string(types.KeySequenceCodeID), seqVal, maxCodeID) } - seqVal = keeper.PeekAutoIncrementID(ctx, types.KeyLastInstanceID) + seqVal = keeper.PeekAutoIncrementID(ctx, types.KeySequenceInstanceID) if seqVal <= uint64(maxContractID) { - return nil, errorsmod.Wrapf(types.ErrInvalid, "seq %s with value: %d must be greater than: %d ", string(types.KeyLastInstanceID), seqVal, maxContractID) + return nil, errorsmod.Wrapf(types.ErrInvalid, "seq %s with value: %d must be greater than: %d ", string(types.KeySequenceInstanceID), seqVal, maxContractID) } return nil, nil } @@ -111,7 +111,7 @@ func ExportGenesis(ctx sdk.Context, keeper *Keeper) *types.GenesisState { return false }) - for _, k := range [][]byte{types.KeyLastCodeID, types.KeyLastInstanceID} { + for _, k := range [][]byte{types.KeySequenceCodeID, types.KeySequenceInstanceID} { genState.Sequences = append(genState.Sequences, types.Sequence{ IDKey: k, Value: keeper.PeekAutoIncrementID(ctx, k), diff --git a/x/wasm/keeper/genesis_test.go b/x/wasm/keeper/genesis_test.go index 73561d1eb6..3b4e51cbad 100644 --- a/x/wasm/keeper/genesis_test.go +++ b/x/wasm/keeper/genesis_test.go @@ -171,8 +171,8 @@ func TestGenesisInit(t *testing.T) { CodeBytes: wasmCode, }}, Sequences: []types.Sequence{ - {IDKey: types.KeyLastCodeID, Value: 2}, - {IDKey: types.KeyLastInstanceID, Value: 1}, + {IDKey: types.KeySequenceCodeID, Value: 2}, + {IDKey: types.KeySequenceInstanceID, Value: 1}, }, Params: types.DefaultParams(), }, @@ -190,8 +190,8 @@ func TestGenesisInit(t *testing.T) { CodeBytes: wasmCode, }}, Sequences: []types.Sequence{ - {IDKey: types.KeyLastCodeID, Value: 10}, - {IDKey: types.KeyLastInstanceID, Value: 1}, + {IDKey: types.KeySequenceCodeID, Value: 10}, + {IDKey: types.KeySequenceInstanceID, Value: 1}, }, Params: types.DefaultParams(), }, @@ -210,8 +210,8 @@ func TestGenesisInit(t *testing.T) { }}, Contracts: nil, Sequences: []types.Sequence{ - {IDKey: types.KeyLastCodeID, Value: 3}, - {IDKey: types.KeyLastInstanceID, Value: 1}, + {IDKey: types.KeySequenceCodeID, Value: 3}, + {IDKey: types.KeySequenceInstanceID, Value: 1}, }, Params: types.DefaultParams(), }, @@ -281,8 +281,8 @@ func TestGenesisInit(t *testing.T) { }, }, Sequences: []types.Sequence{ - {IDKey: types.KeyLastCodeID, Value: 2}, - {IDKey: types.KeyLastInstanceID, Value: 2}, + {IDKey: types.KeySequenceCodeID, Value: 2}, + {IDKey: types.KeySequenceInstanceID, Value: 2}, }, Params: types.DefaultParams(), }, @@ -321,8 +321,8 @@ func TestGenesisInit(t *testing.T) { }, }, Sequences: []types.Sequence{ - {IDKey: types.KeyLastCodeID, Value: 2}, - {IDKey: types.KeyLastInstanceID, Value: 3}, + {IDKey: types.KeySequenceCodeID, Value: 2}, + {IDKey: types.KeySequenceInstanceID, Value: 3}, }, Params: types.DefaultParams(), }, @@ -433,7 +433,7 @@ func TestGenesisInit(t *testing.T) { CodeBytes: wasmCode, }}, Sequences: []types.Sequence{ - {IDKey: types.KeyLastCodeID, Value: 1}, + {IDKey: types.KeySequenceCodeID, Value: 1}, }, Params: types.DefaultParams(), }, @@ -460,8 +460,8 @@ func TestGenesisInit(t *testing.T) { }, }, Sequences: []types.Sequence{ - {IDKey: types.KeyLastCodeID, Value: 2}, - {IDKey: types.KeyLastInstanceID, Value: 1}, + {IDKey: types.KeySequenceCodeID, Value: 2}, + {IDKey: types.KeySequenceInstanceID, Value: 1}, }, Params: types.DefaultParams(), }, @@ -626,8 +626,8 @@ func TestImportContractWithCodeHistoryPreserved(t *testing.T) { }, } assert.Equal(t, expHistory, keeper.GetContractHistory(ctx, contractAddr)) - assert.Equal(t, uint64(2), keeper.PeekAutoIncrementID(ctx, types.KeyLastCodeID)) - assert.Equal(t, uint64(3), keeper.PeekAutoIncrementID(ctx, types.KeyLastInstanceID)) + assert.Equal(t, uint64(2), keeper.PeekAutoIncrementID(ctx, types.KeySequenceCodeID)) + assert.Equal(t, uint64(3), keeper.PeekAutoIncrementID(ctx, types.KeySequenceInstanceID)) } func setupKeeper(t *testing.T) (*Keeper, sdk.Context) { diff --git a/x/wasm/keeper/keeper.go b/x/wasm/keeper/keeper.go index 8b7eb0eff6..5c44f3e4ab 100644 --- a/x/wasm/keeper/keeper.go +++ b/x/wasm/keeper/keeper.go @@ -181,7 +181,7 @@ func (k Keeper) create(ctx sdk.Context, creator sdk.AccAddress, wasmCode []byte, if err != nil { return 0, checksum, errorsmod.Wrap(types.ErrCreateFailed, err.Error()) } - codeID = k.autoIncrementID(ctx, types.KeyLastCodeID) + codeID = k.autoIncrementID(ctx, types.KeySequenceCodeID) k.Logger(ctx).Debug("storing new contract", "capabilities", report.RequiredCapabilities, "code_id", codeID) codeInfo := types.NewCodeInfo(checksum, creator, *instantiateAccess) k.storeCodeInfo(ctx, codeID, codeInfo) @@ -1030,22 +1030,22 @@ func (k Keeper) consumeRuntimeGas(ctx sdk.Context, gas uint64) { } } -func (k Keeper) autoIncrementID(ctx sdk.Context, lastIDKey []byte) uint64 { +func (k Keeper) autoIncrementID(ctx sdk.Context, sequenceKey []byte) uint64 { store := ctx.KVStore(k.storeKey) - bz := store.Get(lastIDKey) + bz := store.Get(sequenceKey) id := uint64(1) if bz != nil { id = binary.BigEndian.Uint64(bz) } bz = sdk.Uint64ToBigEndian(id + 1) - store.Set(lastIDKey, bz) + store.Set(sequenceKey, bz) return id } // PeekAutoIncrementID reads the current value without incrementing it. -func (k Keeper) PeekAutoIncrementID(ctx sdk.Context, lastIDKey []byte) uint64 { +func (k Keeper) PeekAutoIncrementID(ctx sdk.Context, sequenceKey []byte) uint64 { store := ctx.KVStore(k.storeKey) - bz := store.Get(lastIDKey) + bz := store.Get(sequenceKey) id := uint64(1) if bz != nil { id = binary.BigEndian.Uint64(bz) @@ -1053,13 +1053,13 @@ func (k Keeper) PeekAutoIncrementID(ctx sdk.Context, lastIDKey []byte) uint64 { return id } -func (k Keeper) importAutoIncrementID(ctx sdk.Context, lastIDKey []byte, val uint64) error { +func (k Keeper) importAutoIncrementID(ctx sdk.Context, sequenceKey []byte, val uint64) error { store := ctx.KVStore(k.storeKey) - if store.Has(lastIDKey) { - return errorsmod.Wrapf(types.ErrDuplicate, "autoincrement id: %s", string(lastIDKey)) + if store.Has(sequenceKey) { + return errorsmod.Wrapf(types.ErrDuplicate, "autoincrement id: %s", string(sequenceKey)) } bz := sdk.Uint64ToBigEndian(val) - store.Set(lastIDKey, bz) + store.Set(sequenceKey, bz) return nil } diff --git a/x/wasm/simulation/genesis.go b/x/wasm/simulation/genesis.go index eff45898ef..3c687f07e2 100644 --- a/x/wasm/simulation/genesis.go +++ b/x/wasm/simulation/genesis.go @@ -14,7 +14,7 @@ func RandomizedGenState(simstate *module.SimulationState) { Codes: nil, Contracts: nil, Sequences: []types.Sequence{ - {IDKey: types.KeyLastCodeID, Value: simstate.Rand.Uint64()}, + {IDKey: types.KeySequenceCodeID, Value: simstate.Rand.Uint64()}, }, } diff --git a/x/wasm/types/keys.go b/x/wasm/types/keys.go index 6a2e8a9a51..468d9865ed 100644 --- a/x/wasm/types/keys.go +++ b/x/wasm/types/keys.go @@ -34,8 +34,8 @@ var ( ContractsByCreatorPrefix = []byte{0x09} ParamsKey = []byte{0x10} - KeyLastCodeID = append(SequenceKeyPrefix, []byte("lastCodeId")...) - KeyLastInstanceID = append(SequenceKeyPrefix, []byte("lastContractId")...) + KeySequenceCodeID = append(SequenceKeyPrefix, []byte("lastCodeId")...) + KeySequenceInstanceID = append(SequenceKeyPrefix, []byte("lastContractId")...) ) // GetCodeKey constructs the key for retreiving the ID for the WASM code