split up index store

Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
Lorenz Herzberger 2023-06-15 17:34:03 +02:00
parent f5cdba9ed5
commit 0902ca5375
No known key found for this signature in database
GPG Key ID: FA5EE906EB55316A
5 changed files with 62 additions and 25 deletions

View File

@ -515,7 +515,9 @@ func New(
app.MachineKeeper = *machinemodulekeeper.NewKeeper( app.MachineKeeper = *machinemodulekeeper.NewKeeper(
appCodec, appCodec,
keys[machinemoduletypes.StoreKey], keys[machinemoduletypes.StoreKey],
keys[machinemoduletypes.IndexKey], keys[machinemoduletypes.TAIndexKey],
keys[machinemoduletypes.IssuerPlanetmintIndexKey],
keys[machinemoduletypes.IssuerLiquidIndexKey],
keys[machinemoduletypes.MemStoreKey], keys[machinemoduletypes.MemStoreKey],
app.GetSubspace(machinemoduletypes.ModuleName), app.GetSubspace(machinemoduletypes.ModuleName),
) )

View File

@ -20,13 +20,17 @@ import (
func MachineKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) { func MachineKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
storeKey := sdk.NewKVStoreKey(types.StoreKey) storeKey := sdk.NewKVStoreKey(types.StoreKey)
indexStoreKey := sdk.NewKVStoreKey(types.IndexKey) taIndexStoreKey := sdk.NewKVStoreKey(types.TAIndexKey)
issuerPlanetmintIndexStoreKey := sdk.NewKVStoreKey(types.IssuerPlanetmintIndexKey)
issuerLiquidIndexStoreKey := sdk.NewKVStoreKey(types.IssuerLiquidIndexKey)
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey) memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)
db := tmdb.NewMemDB() db := tmdb.NewMemDB()
stateStore := store.NewCommitMultiStore(db) stateStore := store.NewCommitMultiStore(db)
stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db) stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(indexStoreKey, storetypes.StoreTypeIAVL, db) stateStore.MountStoreWithDB(taIndexStoreKey, storetypes.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(issuerPlanetmintIndexStoreKey, storetypes.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(issuerLiquidIndexStoreKey, storetypes.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil) stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil)
require.NoError(t, stateStore.LoadLatestVersion()) require.NoError(t, stateStore.LoadLatestVersion())
@ -42,7 +46,9 @@ func MachineKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
k := keeper.NewKeeper( k := keeper.NewKeeper(
cdc, cdc,
storeKey, storeKey,
indexStoreKey, taIndexStoreKey,
issuerPlanetmintIndexStoreKey,
issuerLiquidIndexStoreKey,
memStoreKey, memStoreKey,
paramsSubspace, paramsSubspace,
) )

View File

@ -16,7 +16,9 @@ type (
Keeper struct { Keeper struct {
cdc codec.BinaryCodec cdc codec.BinaryCodec
storeKey storetypes.StoreKey storeKey storetypes.StoreKey
indexStoreKey storetypes.StoreKey taIndexStoreKey storetypes.StoreKey
issuerPlanetmintIndexStoreKey storetypes.StoreKey
issuerLiquidIndexStoreKey storetypes.StoreKey
memKey storetypes.StoreKey memKey storetypes.StoreKey
paramstore paramtypes.Subspace paramstore paramtypes.Subspace
} }
@ -26,6 +28,8 @@ func NewKeeper(
cdc codec.BinaryCodec, cdc codec.BinaryCodec,
storeKey, storeKey,
indexStoreKey, indexStoreKey,
issuerPlanetmintIndexStoreKey,
issuerLiquidIndexStoreKey,
memKey storetypes.StoreKey, memKey storetypes.StoreKey,
ps paramtypes.Subspace, ps paramtypes.Subspace,
@ -38,7 +42,9 @@ func NewKeeper(
return &Keeper{ return &Keeper{
cdc: cdc, cdc: cdc,
storeKey: storeKey, storeKey: storeKey,
indexStoreKey: indexStoreKey, taIndexStoreKey: indexStoreKey,
issuerPlanetmintIndexStoreKey: issuerPlanetmintIndexStoreKey,
issuerLiquidIndexStoreKey: issuerLiquidIndexStoreKey,
memKey: memKey, memKey: memKey,
paramstore: ps, paramstore: ps,
} }

View File

@ -31,7 +31,10 @@ func (k Keeper) GetMachine(ctx sdk.Context, pubKey string) (val types.Machine, f
} }
func (k Keeper) StoreMachineIndex(ctx sdk.Context, machine types.Machine) { func (k Keeper) StoreMachineIndex(ctx sdk.Context, machine types.Machine) {
indexStore := prefix.NewStore(ctx.KVStore(k.indexStoreKey), types.KeyPrefix(types.IndexKey)) taIndexStore := prefix.NewStore(ctx.KVStore(k.taIndexStoreKey), types.KeyPrefix(types.TAIndexKey))
issuerPlanetmintIndexStore := prefix.NewStore(ctx.KVStore(k.issuerPlanetmintIndexStoreKey), types.KeyPrefix(types.IssuerPlanetmintIndexKey))
issuerLiquidIndexStore := prefix.NewStore(ctx.KVStore(k.issuerLiquidIndexStoreKey), types.KeyPrefix(types.IssuerLiquidIndexKey))
index := types.MachineIndex{ index := types.MachineIndex{
MachineId: machine.MachineId, MachineId: machine.MachineId,
IssuerPlanetmint: machine.IssuerPlanetmint, IssuerPlanetmint: machine.IssuerPlanetmint,
@ -42,23 +45,39 @@ func (k Keeper) StoreMachineIndex(ctx sdk.Context, machine types.Machine) {
issuerPlanetmintIndexKey := GetMachineBytes(machine.IssuerPlanetmint) issuerPlanetmintIndexKey := GetMachineBytes(machine.IssuerPlanetmint)
issuerLiquidIndexKey := GetMachineBytes(machine.IssuerLiquid) issuerLiquidIndexKey := GetMachineBytes(machine.IssuerLiquid)
indexAppendValue := k.cdc.MustMarshal(&index) indexAppendValue := k.cdc.MustMarshal(&index)
indexStore.Set(machineIdIndexKey, indexAppendValue) taIndexStore.Set(machineIdIndexKey, indexAppendValue)
indexStore.Set(issuerPlanetmintIndexKey, indexAppendValue) issuerPlanetmintIndexStore.Set(issuerPlanetmintIndexKey, indexAppendValue)
indexStore.Set(issuerLiquidIndexKey, indexAppendValue) issuerLiquidIndexStore.Set(issuerLiquidIndexKey, indexAppendValue)
} }
func (k Keeper) GetMachineIndex(ctx sdk.Context, pubKey string) (val types.MachineIndex, found bool) { func (k Keeper) GetMachineIndex(ctx sdk.Context, pubKey string) (val types.MachineIndex, found bool) {
indexStore := prefix.NewStore(ctx.KVStore(k.indexStoreKey), types.KeyPrefix(types.IndexKey)) taIndexStore := prefix.NewStore(ctx.KVStore(k.taIndexStoreKey), types.KeyPrefix(types.TAIndexKey))
index := indexStore.Get(GetMachineBytes(pubKey)) issuerPlanetmintIndexStore := prefix.NewStore(ctx.KVStore(k.issuerPlanetmintIndexStoreKey), types.KeyPrefix(types.IssuerPlanetmintIndexKey))
issuerLiquidIndexStore := prefix.NewStore(ctx.KVStore(k.issuerLiquidIndexStoreKey), types.KeyPrefix(types.IssuerLiquidIndexKey))
if index == nil { keyBytes := GetMachineBytes(pubKey)
return val, false
taIndex := taIndexStore.Get(keyBytes)
if taIndex != nil {
k.cdc.Unmarshal(taIndex, &val)
return val, true
} }
k.cdc.Unmarshal(index, &val) ipIndex := issuerPlanetmintIndexStore.Get(keyBytes)
if ipIndex != nil {
k.cdc.Unmarshal(ipIndex, &val)
return val, true return val, true
} }
ilIndex := issuerLiquidIndexStore.Get(keyBytes)
if ilIndex != nil {
k.cdc.Unmarshal(ilIndex, &val)
return val, true
}
return val, false
}
func GetMachineBytes(pubKey string) []byte { func GetMachineBytes(pubKey string) []byte {
return []byte(pubKey) return []byte(pubKey)
} }

View File

@ -15,7 +15,11 @@ const (
MachineKey = "Machine/value/" MachineKey = "Machine/value/"
IndexKey = "Machine/index/" TAIndexKey = "Machine/TAIndex/"
IssuerPlanetmintIndexKey = "Machine/IssuerPlanetmintIndex/"
IssuerLiquidIndexKey = "Machine/IssuerLiquidIndex/"
) )
func KeyPrefix(p string) []byte { func KeyPrefix(p string) []byte {