mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-10-14 00:59:21 +00:00
added machine keeper
Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
parent
64d7e6ced0
commit
ae324eb591
@ -113,6 +113,7 @@ import (
|
|||||||
machinemodule "planetmint-go/x/machine"
|
machinemodule "planetmint-go/x/machine"
|
||||||
machinemodulekeeper "planetmint-go/x/machine/keeper"
|
machinemodulekeeper "planetmint-go/x/machine/keeper"
|
||||||
machinemoduletypes "planetmint-go/x/machine/types"
|
machinemoduletypes "planetmint-go/x/machine/types"
|
||||||
|
|
||||||
// this line is used by starport scaffolding # stargate/app/moduleImport
|
// this line is used by starport scaffolding # stargate/app/moduleImport
|
||||||
|
|
||||||
appparams "planetmint-go/app/params"
|
appparams "planetmint-go/app/params"
|
||||||
@ -296,7 +297,7 @@ func New(
|
|||||||
govtypes.StoreKey, paramstypes.StoreKey, ibcexported.StoreKey, upgradetypes.StoreKey,
|
govtypes.StoreKey, paramstypes.StoreKey, ibcexported.StoreKey, upgradetypes.StoreKey,
|
||||||
feegrant.StoreKey, evidencetypes.StoreKey, ibctransfertypes.StoreKey, icahosttypes.StoreKey,
|
feegrant.StoreKey, evidencetypes.StoreKey, ibctransfertypes.StoreKey, icahosttypes.StoreKey,
|
||||||
capabilitytypes.StoreKey, group.StoreKey, icacontrollertypes.StoreKey, consensusparamtypes.StoreKey,
|
capabilitytypes.StoreKey, group.StoreKey, icacontrollertypes.StoreKey, consensusparamtypes.StoreKey,
|
||||||
machinemoduletypes.StoreKey,
|
machinemoduletypes.StoreKey, machinemoduletypes.TAIndexKey, machinemoduletypes.IssuerPlanetmintIndexKey, machinemoduletypes.IssuerLiquidIndexKey,
|
||||||
// this line is used by starport scaffolding # stargate/app/storeKey
|
// this line is used by starport scaffolding # stargate/app/storeKey
|
||||||
)
|
)
|
||||||
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
|
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
|
||||||
@ -522,6 +523,9 @@ func New(
|
|||||||
app.MachineKeeper = *machinemodulekeeper.NewKeeper(
|
app.MachineKeeper = *machinemodulekeeper.NewKeeper(
|
||||||
appCodec,
|
appCodec,
|
||||||
keys[machinemoduletypes.StoreKey],
|
keys[machinemoduletypes.StoreKey],
|
||||||
|
keys[machinemoduletypes.TAIndexKey],
|
||||||
|
keys[machinemoduletypes.IssuerPlanetmintIndexKey],
|
||||||
|
keys[machinemoduletypes.IssuerLiquidIndexKey],
|
||||||
keys[machinemoduletypes.MemStoreKey],
|
keys[machinemoduletypes.MemStoreKey],
|
||||||
app.GetSubspace(machinemoduletypes.ModuleName),
|
app.GetSubspace(machinemoduletypes.ModuleName),
|
||||||
)
|
)
|
||||||
|
@ -3,6 +3,9 @@ package keeper
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"planetmint-go/x/machine/keeper"
|
||||||
|
"planetmint-go/x/machine/types"
|
||||||
|
|
||||||
tmdb "github.com/cometbft/cometbft-db"
|
tmdb "github.com/cometbft/cometbft-db"
|
||||||
"github.com/cometbft/cometbft/libs/log"
|
"github.com/cometbft/cometbft/libs/log"
|
||||||
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
|
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
|
||||||
@ -13,17 +16,21 @@ import (
|
|||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
typesparams "github.com/cosmos/cosmos-sdk/x/params/types"
|
typesparams "github.com/cosmos/cosmos-sdk/x/params/types"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"planetmint-go/x/machine/keeper"
|
|
||||||
"planetmint-go/x/machine/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
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)
|
||||||
|
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(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())
|
||||||
|
|
||||||
@ -39,6 +46,9 @@ func MachineKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
|
|||||||
k := keeper.NewKeeper(
|
k := keeper.NewKeeper(
|
||||||
cdc,
|
cdc,
|
||||||
storeKey,
|
storeKey,
|
||||||
|
taIndexStoreKey,
|
||||||
|
issuerPlanetmintIndexStoreKey,
|
||||||
|
issuerLiquidIndexStoreKey,
|
||||||
memStoreKey,
|
memStoreKey,
|
||||||
paramsSubspace,
|
paramsSubspace,
|
||||||
)
|
)
|
||||||
|
@ -14,16 +14,22 @@ import (
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
Keeper struct {
|
Keeper struct {
|
||||||
cdc codec.BinaryCodec
|
cdc codec.BinaryCodec
|
||||||
storeKey storetypes.StoreKey
|
storeKey storetypes.StoreKey
|
||||||
memKey storetypes.StoreKey
|
taIndexStoreKey storetypes.StoreKey
|
||||||
paramstore paramtypes.Subspace
|
issuerPlanetmintIndexStoreKey storetypes.StoreKey
|
||||||
|
issuerLiquidIndexStoreKey storetypes.StoreKey
|
||||||
|
memKey storetypes.StoreKey
|
||||||
|
paramstore paramtypes.Subspace
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewKeeper(
|
func NewKeeper(
|
||||||
cdc codec.BinaryCodec,
|
cdc codec.BinaryCodec,
|
||||||
storeKey,
|
storeKey,
|
||||||
|
indexStoreKey,
|
||||||
|
issuerPlanetmintIndexStoreKey,
|
||||||
|
issuerLiquidIndexStoreKey,
|
||||||
memKey storetypes.StoreKey,
|
memKey storetypes.StoreKey,
|
||||||
ps paramtypes.Subspace,
|
ps paramtypes.Subspace,
|
||||||
|
|
||||||
@ -34,10 +40,13 @@ func NewKeeper(
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &Keeper{
|
return &Keeper{
|
||||||
cdc: cdc,
|
cdc: cdc,
|
||||||
storeKey: storeKey,
|
storeKey: storeKey,
|
||||||
memKey: memKey,
|
taIndexStoreKey: indexStoreKey,
|
||||||
paramstore: ps,
|
issuerPlanetmintIndexStoreKey: issuerPlanetmintIndexStoreKey,
|
||||||
|
issuerLiquidIndexStoreKey: issuerLiquidIndexStoreKey,
|
||||||
|
memKey: memKey,
|
||||||
|
paramstore: ps,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
77
x/machine/keeper/machine.go
Normal file
77
x/machine/keeper/machine.go
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
package keeper
|
||||||
|
|
||||||
|
import (
|
||||||
|
"planetmint-go/x/machine/types"
|
||||||
|
|
||||||
|
"github.com/cosmos/cosmos-sdk/store/prefix"
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (k Keeper) StoreMachine(ctx sdk.Context, machine types.Machine) {
|
||||||
|
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.MachineKey))
|
||||||
|
appendValue := k.cdc.MustMarshal(&machine)
|
||||||
|
store.Set(GetMachineBytes(machine.IssuerPlanetmint), appendValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Keeper) GetMachine(ctx sdk.Context, index types.MachineIndex) (val types.Machine, found bool) {
|
||||||
|
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.MachineKey))
|
||||||
|
machine := store.Get(GetMachineBytes(index.IssuerPlanetmint))
|
||||||
|
|
||||||
|
if machine == nil {
|
||||||
|
return val, false
|
||||||
|
}
|
||||||
|
k.cdc.Unmarshal(machine, &val)
|
||||||
|
return val, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Keeper) StoreMachineIndex(ctx sdk.Context, machine types.Machine) {
|
||||||
|
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{
|
||||||
|
MachineId: machine.MachineId,
|
||||||
|
IssuerPlanetmint: machine.IssuerPlanetmint,
|
||||||
|
IssuerLiquid: machine.IssuerLiquid,
|
||||||
|
}
|
||||||
|
|
||||||
|
machineIdIndexKey := GetMachineBytes(machine.MachineId)
|
||||||
|
issuerPlanetmintIndexKey := GetMachineBytes(machine.IssuerPlanetmint)
|
||||||
|
issuerLiquidIndexKey := GetMachineBytes(machine.IssuerLiquid)
|
||||||
|
indexAppendValue := k.cdc.MustMarshal(&index)
|
||||||
|
taIndexStore.Set(machineIdIndexKey, indexAppendValue)
|
||||||
|
issuerPlanetmintIndexStore.Set(issuerPlanetmintIndexKey, indexAppendValue)
|
||||||
|
issuerLiquidIndexStore.Set(issuerLiquidIndexKey, indexAppendValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Keeper) GetMachineIndex(ctx sdk.Context, pubKey string) (val types.MachineIndex, found bool) {
|
||||||
|
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))
|
||||||
|
|
||||||
|
keyBytes := GetMachineBytes(pubKey)
|
||||||
|
|
||||||
|
taIndex := taIndexStore.Get(keyBytes)
|
||||||
|
if taIndex != nil {
|
||||||
|
k.cdc.Unmarshal(taIndex, &val)
|
||||||
|
return val, true
|
||||||
|
}
|
||||||
|
|
||||||
|
ipIndex := issuerPlanetmintIndexStore.Get(keyBytes)
|
||||||
|
if ipIndex != nil {
|
||||||
|
k.cdc.Unmarshal(ipIndex, &val)
|
||||||
|
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 {
|
||||||
|
return []byte(pubKey)
|
||||||
|
}
|
55
x/machine/keeper/machine_test.go
Normal file
55
x/machine/keeper/machine_test.go
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
package keeper_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
keepertest "planetmint-go/testutil/keeper"
|
||||||
|
"planetmint-go/x/machine/keeper"
|
||||||
|
"planetmint-go/x/machine/types"
|
||||||
|
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func createNMachine(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.Machine {
|
||||||
|
items := make([]types.Machine, n)
|
||||||
|
for i := range items {
|
||||||
|
items[i].MachineId = fmt.Sprintf("machineId%v", i)
|
||||||
|
items[i].IssuerPlanetmint = fmt.Sprintf("issuerPlanetmint%v", i)
|
||||||
|
items[i].IssuerLiquid = fmt.Sprintf("issuerLiquid%v", i)
|
||||||
|
keeper.StoreMachine(ctx, items[i])
|
||||||
|
keeper.StoreMachineIndex(ctx, items[i])
|
||||||
|
}
|
||||||
|
return items
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetMachine(t *testing.T) {
|
||||||
|
keeper, ctx := keepertest.MachineKeeper(t)
|
||||||
|
items := createNMachine(keeper, ctx, 10)
|
||||||
|
for _, item := range items {
|
||||||
|
index := types.MachineIndex{
|
||||||
|
MachineId: item.MachineId,
|
||||||
|
IssuerPlanetmint: item.IssuerPlanetmint,
|
||||||
|
IssuerLiquid: item.IssuerLiquid,
|
||||||
|
}
|
||||||
|
machineById, found := keeper.GetMachine(ctx, index)
|
||||||
|
assert.True(t, found)
|
||||||
|
assert.Equal(t, item, machineById)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetMachineIndex(t *testing.T) {
|
||||||
|
keeper, ctx := keepertest.MachineKeeper(t)
|
||||||
|
items := createNMachine(keeper, ctx, 10)
|
||||||
|
for _, item := range items {
|
||||||
|
expectedIndex := types.MachineIndex{
|
||||||
|
MachineId: item.MachineId,
|
||||||
|
IssuerPlanetmint: item.IssuerPlanetmint,
|
||||||
|
IssuerLiquid: item.IssuerLiquid,
|
||||||
|
}
|
||||||
|
index, found := keeper.GetMachineIndex(ctx, item.MachineId)
|
||||||
|
assert.True(t, found)
|
||||||
|
assert.Equal(t, expectedIndex, index)
|
||||||
|
}
|
||||||
|
}
|
@ -12,6 +12,14 @@ const (
|
|||||||
|
|
||||||
// MemStoreKey defines the in-memory store key
|
// MemStoreKey defines the in-memory store key
|
||||||
MemStoreKey = "mem_machine"
|
MemStoreKey = "mem_machine"
|
||||||
|
|
||||||
|
MachineKey = "Machine/value/"
|
||||||
|
|
||||||
|
TAIndexKey = "Machine/TAIndex/"
|
||||||
|
|
||||||
|
IssuerPlanetmintIndexKey = "Machine/IssuerPlanetmintIndex/"
|
||||||
|
|
||||||
|
IssuerLiquidIndexKey = "Machine/IssuerLiquidIndex/"
|
||||||
)
|
)
|
||||||
|
|
||||||
func KeyPrefix(p string) []byte {
|
func KeyPrefix(p string) []byte {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user