Skip to content

Commit

Permalink
Merge pull request #1585 from CosmWasm/rename_key
Browse files Browse the repository at this point in the history
Rename key store key attribute to be more concrete
  • Loading branch information
alpe authored Aug 31, 2023
2 parents 78b5af2 + 0d3bfce commit 1f49d75
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 36 deletions.
4 changes: 2 additions & 2 deletions x/wasm/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/keeper/addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
10 changes: 5 additions & 5 deletions x/wasm/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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),
Expand Down
30 changes: 15 additions & 15 deletions x/wasm/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
Expand All @@ -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(),
},
Expand All @@ -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(),
},
Expand Down Expand Up @@ -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(),
},
Expand Down Expand Up @@ -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(),
},
Expand Down Expand Up @@ -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(),
},
Expand All @@ -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(),
},
Expand Down Expand Up @@ -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) {
Expand Down
20 changes: 10 additions & 10 deletions x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -1030,36 +1030,36 @@ 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)
}
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
}

Expand Down
2 changes: 1 addition & 1 deletion x/wasm/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()},
},
}

Expand Down
4 changes: 2 additions & 2 deletions x/wasm/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1f49d75

Please sign in to comment.