diff --git a/x/randomness/genesis.go b/x/randomness/genesis.go index be340c73..4bdecab5 100644 --- a/x/randomness/genesis.go +++ b/x/randomness/genesis.go @@ -9,7 +9,10 @@ import ( // InitGenesis puts data from genesis state into store. func InitGenesis(ctx sdk.Context, k keeper.Keeper, data types.GenesisState) { - k.SetSeed(ctx, data.Seed) + err := k.SetSeed(ctx, data.Seed) + if err != nil { + panic(err) + } } // ExportGenesis extracts data from store to genesis state. diff --git a/x/randomness/keeper/keeper.go b/x/randomness/keeper/keeper.go index f52826ba..c4270b02 100644 --- a/x/randomness/keeper/keeper.go +++ b/x/randomness/keeper/keeper.go @@ -16,11 +16,13 @@ import ( "github.com/sedaprotocol/seda-chain/x/randomness/types" ) -var SeedPrefix = collections.NewPrefix(0) -var ValidatorVRFPrefix = collections.NewPrefix(1) +var ( + SeedPrefix = collections.NewPrefix(0) + ValidatorVRFPrefix = collections.NewPrefix(1) +) -// GetValidatorVRFKeyFull gets the key for the validator VRF object. -func GetValidatorVRFKeyFull(consensusAddr sdk.ConsAddress) []byte { +// GetValidatorVRFKeyPrefixFull gets the key for the validator VRF object. +func GetValidatorVRFKeyPrefixFull(consensusAddr sdk.ConsAddress) []byte { return append(ValidatorVRFPrefix, address.MustLengthPrefix(consensusAddr)...) } @@ -61,7 +63,7 @@ func (k Keeper) GetValidatorVRFPubKey(ctx sdk.Context, consensusAddr string) (cr if err != nil { return nil, err } - validatorVRFKeyPrefixFull := GetValidatorVRFKeyFull(addr) + validatorVRFKeyPrefixFull := GetValidatorVRFKeyPrefixFull(addr) vrfPubKey, err := k.ValidatorVRFPubKeys.Get(ctx, string(validatorVRFKeyPrefixFull)) if err != nil { return nil, err @@ -75,7 +77,7 @@ func (k Keeper) SetValidatorVRFPubKey(goCtx context.Context, consensusAddr strin if err != nil { return err } - validatorVRFKeyPrefixFull := GetValidatorVRFKeyFull(addr) + validatorVRFKeyPrefixFull := GetValidatorVRFKeyPrefixFull(addr) return k.ValidatorVRFPubKeys.Set(goCtx, string(validatorVRFKeyPrefixFull), vrfPubKey) } diff --git a/x/randomness/keeper/msg_server.go b/x/randomness/keeper/msg_server.go index feb79510..5001ad92 100644 --- a/x/randomness/keeper/msg_server.go +++ b/x/randomness/keeper/msg_server.go @@ -22,6 +22,12 @@ var _ types.MsgServer = msgServer{} func (k msgServer) NewSeed(goCtx context.Context, msg *types.MsgNewSeed) (*types.MsgNewSeedResponse, error) { sdkCtx := sdk.UnwrapSDKContext(goCtx) - k.Keeper.SetSeed(sdkCtx, msg.Beta) + err := k.Keeper.SetSeed(sdkCtx, msg.Beta) + if err != nil { + return nil, err + } + + // TO-DO event? + return &types.MsgNewSeedResponse{}, nil } diff --git a/x/wasm-storage/genesis.go b/x/wasm-storage/genesis.go index f832e106..dcd9c24d 100644 --- a/x/wasm-storage/genesis.go +++ b/x/wasm-storage/genesis.go @@ -13,11 +13,17 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, data types.GenesisState) { wasm := data.Wasms[i] if wasm.WasmType == types.WasmTypeDataRequest || wasm.WasmType == types.WasmTypeTally { - k.SetDataRequestWasm(ctx, &wasm) + err := k.SetDataRequestWasm(ctx, &wasm) + if err != nil { + panic(err) + } } if wasm.WasmType == types.WasmTypeDataRequestExecutor || wasm.WasmType == types.WasmTypeRelayer { - k.SetOverlayWasm(ctx, &wasm) + err := k.SetOverlayWasm(ctx, &wasm) + if err != nil { + panic(err) + } } } if data.ProxyContractRegistry != "" { @@ -25,13 +31,19 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, data types.GenesisState) { if err != nil { panic(err) } - k.SetProxyContractRegistry(ctx, proxyAddr) + err = k.SetProxyContractRegistry(ctx, proxyAddr) + if err != nil { + panic(err) + } } } // ExportGenesis extracts all data from store to genesis state. func ExportGenesis(ctx sdk.Context, k keeper.Keeper) types.GenesisState { wasms := k.GetAllWasms(ctx) - proxy := k.GetProxyContractRegistry(ctx) + proxy, err := k.GetProxyContractRegistry(ctx) + if err != nil { + return types.NewGenesisState(wasms, "") + } return types.NewGenesisState(wasms, proxy.String()) } diff --git a/x/wasm-storage/keeper/common_test.go b/x/wasm-storage/keeper/common_test.go index 3c43bd95..32c15184 100644 --- a/x/wasm-storage/keeper/common_test.go +++ b/x/wasm-storage/keeper/common_test.go @@ -9,6 +9,7 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/runtime" "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" @@ -63,7 +64,7 @@ func setupKeeper(t *testing.T, authority string) (*keeper.Keeper, moduletestutil encCfg := moduletestutil.MakeTestEncodingConfig(wasmstorage.AppModuleBasic{}) wasmstoragetypes.RegisterInterfaces(encCfg.InterfaceRegistry) - wasmStorageKeeper := keeper.NewKeeper(encCfg.Codec, key, authority, nil) + wasmStorageKeeper := keeper.NewKeeper(encCfg.Codec, runtime.NewKVStoreService(key), authority, nil) return wasmStorageKeeper, encCfg, ctx } diff --git a/x/wasm-storage/keeper/keeper.go b/x/wasm-storage/keeper/keeper.go index 3c58df3d..1c39657a 100644 --- a/x/wasm-storage/keeper/keeper.go +++ b/x/wasm-storage/keeper/keeper.go @@ -3,9 +3,11 @@ package keeper import ( "encoding/hex" "fmt" + "time" + "cosmossdk.io/collections" + storetypes "cosmossdk.io/core/store" "cosmossdk.io/log" - storetypes "cosmossdk.io/store/types" wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types" "github.com/cosmos/cosmos-sdk/codec" @@ -13,19 +15,60 @@ import ( "github.com/sedaprotocol/seda-chain/x/wasm-storage/types" ) +var ( + // DataRequestPrefix defines prefix to store Data Request Wasm binaries. + DataRequestPrefix = collections.NewPrefix(0) + + // OverlayPrefix defines prefix to store Overlay Wasm binaries. + OverlayPrefix = collections.NewPrefix(1) + + // DataRequestQueuePrefix defines prefix to store the queue that contains + // the hashes of Data Request Wasm binaries. + DataRequestQueuePrefix = collections.NewPrefix(2) + + // ProxyContractRegistryPrefix defines prefix to store address of + // Proxy Contract. + ProxyContractRegistryPrefix = collections.NewPrefix(3) + + ParamsPrefix = collections.NewPrefix(4) +) + +func GetDataRequestWasmKeyPrefixFull(hash []byte) []byte { + return append(DataRequestPrefix, hash...) +} + +func GetOverlayWasmKeyPrefixFull(hash []byte) []byte { + return append(OverlayPrefix, hash...) +} + +// GetDataRequestTimeKeyPrefixFull gets the key for an item in Data Request Queue. This key +// is the timestamp of when the Data Request Wasm was stored. +func GetDataRequestTimeKeyPrefixFull(timestamp time.Time) []byte { + bz := sdk.FormatTimeBytes(timestamp) + return append(DataRequestQueuePrefix, bz...) +} + type Keeper struct { - cdc codec.BinaryCodec - storeKey storetypes.StoreKey authority string wasmKeeper wasmtypes.ContractOpsKeeper + + // state management + Schema collections.Schema + DataRequestWasm collections.Map[[]byte, types.Wasm] + OverlayWasm collections.Map[[]byte, types.Wasm] + ProxyContractRegistry collections.Item[[]byte] + Params collections.Item[types.Params] } -func NewKeeper(cdc codec.BinaryCodec, storeKey storetypes.StoreKey, authority string, wk wasmtypes.ContractOpsKeeper) *Keeper { +func NewKeeper(cdc codec.BinaryCodec, storeService storetypes.KVStoreService, authority string, wk wasmtypes.ContractOpsKeeper) *Keeper { + sb := collections.NewSchemaBuilder(storeService) + return &Keeper{ - cdc: cdc, - storeKey: storeKey, - authority: authority, - wasmKeeper: wk, + authority: authority, + wasmKeeper: wk, + DataRequestWasm: collections.NewMap(sb, DataRequestPrefix, "data-request-wasm", collections.BytesKey, codec.CollValue[types.Wasm](cdc)), + OverlayWasm: collections.NewMap(sb, OverlayPrefix, "overlay-wasm", collections.BytesKey, codec.CollValue[types.Wasm](cdc)), + Params: collections.NewItem(sb, ParamsPrefix, "params", codec.CollValue[types.Params](cdc)), } } @@ -35,139 +78,164 @@ func (k Keeper) GetAuthority() string { } // SetDataRequestWasm stores Data Request Wasm using its hash as the key. -func (k Keeper) SetDataRequestWasm(ctx sdk.Context, wasm *types.Wasm) { - store := ctx.KVStore(k.storeKey) - bz := k.cdc.MustMarshal(wasm) - store.Set(types.GetDataRequestWasmKey(wasm.Hash), bz) +func (k Keeper) SetDataRequestWasm(ctx sdk.Context, wasm *types.Wasm) error { + dataRequestWasmKeyPrefixFull := GetDataRequestWasmKeyPrefixFull(wasm.Hash) + + return k.DataRequestWasm.Set(ctx, dataRequestWasmKeyPrefixFull, *wasm) } // GetDataRequestWasm returns Data Request Wasm given its key. -func (k Keeper) GetDataRequestWasm(ctx sdk.Context, hash []byte) *types.Wasm { - var wasm types.Wasm - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.GetDataRequestWasmKey(hash)) - k.cdc.MustUnmarshal(bz, &wasm) - return &wasm +func (k Keeper) GetDataRequestWasm(ctx sdk.Context, hash []byte) (*types.Wasm, error) { + dataRequestWasmKeyPrefixFull := GetDataRequestWasmKeyPrefixFull(hash) + wasm, err := k.DataRequestWasm.Get(ctx, dataRequestWasmKeyPrefixFull) + if err != nil { + return nil, err + } + + return &wasm, nil } // HasDataRequestWasm checks if a given Data Request Wasm exists. -func (k Keeper) HasDataRequestWasm(ctx sdk.Context, wasm *types.Wasm) bool { - store := ctx.KVStore(k.storeKey) - return store.Has(types.GetDataRequestWasmKey(wasm.Hash)) +func (k Keeper) HasDataRequestWasm(ctx sdk.Context, wasm *types.Wasm) (bool, error) { + dataRequestWasmKeyPrefixFull := GetDataRequestWasmKeyPrefixFull(wasm.Hash) + + return k.DataRequestWasm.Has(ctx, dataRequestWasmKeyPrefixFull) } // SetOverlayWasm stores Overlay Wasm using its hash as the key. -func (k Keeper) SetOverlayWasm(ctx sdk.Context, wasm *types.Wasm) { - store := ctx.KVStore(k.storeKey) - bz := k.cdc.MustMarshal(wasm) - store.Set(types.GetOverlayWasmKey(wasm.Hash), bz) +func (k Keeper) SetOverlayWasm(ctx sdk.Context, wasm *types.Wasm) error { + overlayWasmKeyPrefixFull := GetOverlayWasmKeyPrefixFull(wasm.Hash) + return k.OverlayWasm.Set(ctx, overlayWasmKeyPrefixFull, *wasm) } // GetOverlayWasm returns Overlay Wasm given its key. -func (k Keeper) GetOverlayWasm(ctx sdk.Context, hash []byte) *types.Wasm { - var wasm types.Wasm - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.GetOverlayWasmKey(hash)) - k.cdc.MustUnmarshal(bz, &wasm) - return &wasm +func (k Keeper) GetOverlayWasm(ctx sdk.Context, hash []byte) (*types.Wasm, error) { + overlayWasmKeyPrefixFull := GetOverlayWasmKeyPrefixFull(hash) + wasm, err := k.OverlayWasm.Get(ctx, overlayWasmKeyPrefixFull) + if err != nil { + return nil, err + } + + return &wasm, nil } // HasOverlayWasm checks if a given Overlay Wasm exists. -func (k Keeper) HasOverlayWasm(ctx sdk.Context, wasm *types.Wasm) bool { - store := ctx.KVStore(k.storeKey) - return store.Has(types.GetOverlayWasmKey(wasm.Hash)) +func (k Keeper) HasOverlayWasm(ctx sdk.Context, wasm *types.Wasm) (bool, error) { + overlayWasmKeyPrefixFull := GetOverlayWasmKeyPrefixFull(wasm.Hash) + + return k.OverlayWasm.Has(ctx, overlayWasmKeyPrefixFull) } // IterateAllDataRequestWasms iterates over the all the stored Data Request // Wasms and performs a given callback function. -func (k Keeper) IterateAllDataRequestWasms(ctx sdk.Context, callback func(wasm types.Wasm) (stop bool)) { - store := ctx.KVStore(k.storeKey) - iterator := storetypes.KVStorePrefixIterator(store, types.KeyPrefixDataRequest) +func (k Keeper) IterateAllDataRequestWasms(ctx sdk.Context, callback func(wasm types.Wasm) (stop bool)) error { + iter, err := k.DataRequestWasm.Iterate(ctx, nil) + if err != nil { + return err + } + defer iter.Close() - defer iterator.Close() - for ; iterator.Valid(); iterator.Next() { - var wasm types.Wasm - k.cdc.MustUnmarshal(iterator.Value(), &wasm) + for ; iter.Valid(); iter.Next() { + kv, err := iter.KeyValue() + if err != nil { + return err + } - if callback(wasm) { + if callback(kv.Value) { break } } + return nil } // IterateAllOverlayWasms iterates over the all the stored Overlay Wasms // and performs a given callback function. -func (k Keeper) IterateAllOverlayWasms(ctx sdk.Context, callback func(wasm types.Wasm) (stop bool)) { - store := ctx.KVStore(k.storeKey) - iterator := storetypes.KVStorePrefixIterator(store, types.KeyPrefixOverlay) +func (k Keeper) IterateAllOverlayWasms(ctx sdk.Context, callback func(wasm types.Wasm) (stop bool)) error { + iter, err := k.OverlayWasm.Iterate(ctx, nil) + if err != nil { + return err + } + defer iter.Close() - defer iterator.Close() - for ; iterator.Valid(); iterator.Next() { - var wasm types.Wasm - k.cdc.MustUnmarshal(iterator.Value(), &wasm) + for ; iter.Valid(); iter.Next() { + kv, err := iter.KeyValue() + if err != nil { + return err + } - if callback(wasm) { + if callback(kv.Value) { break } } + return nil } // ListDataRequestWasms returns hashes and types of all Data Request Wasms // in the store. func (k Keeper) ListDataRequestWasms(ctx sdk.Context) []string { var hashTypePairs []string - k.IterateAllDataRequestWasms(ctx, func(w types.Wasm) bool { + err := k.IterateAllDataRequestWasms(ctx, func(w types.Wasm) bool { hashTypePairs = append(hashTypePairs, hex.EncodeToString(w.Hash)+","+w.WasmType.String()) return false }) + if err != nil { + return nil + } return hashTypePairs } // ListOverlayWasms returns hashes and types of all Overlay Wasms in the store. func (k Keeper) ListOverlayWasms(ctx sdk.Context) []string { var hashTypePairs []string - k.IterateAllOverlayWasms(ctx, func(w types.Wasm) bool { + err := k.IterateAllOverlayWasms(ctx, func(w types.Wasm) bool { hashTypePairs = append(hashTypePairs, hex.EncodeToString(w.Hash)+","+w.WasmType.String()) return false }) + if err != nil { + return nil + } return hashTypePairs } func (k Keeper) GetAllWasms(ctx sdk.Context) []types.Wasm { var wasms []types.Wasm - k.IterateAllDataRequestWasms(ctx, func(wasm types.Wasm) bool { + err := k.IterateAllDataRequestWasms(ctx, func(wasm types.Wasm) bool { wasms = append(wasms, wasm) return false }) - k.IterateAllOverlayWasms(ctx, func(wasm types.Wasm) bool { + if err != nil { + return nil + } + err = k.IterateAllOverlayWasms(ctx, func(wasm types.Wasm) bool { wasms = append(wasms, wasm) return false }) + if err != nil { + return nil + } return wasms } +// SetProxyContractRegistry stores Proxy Contract address. +func (k Keeper) SetProxyContractRegistry(ctx sdk.Context, address sdk.AccAddress) error { + return k.ProxyContractRegistry.Set(ctx, address) +} + +// GetProxyContractRegistry returns Proxy Contract address. +func (k Keeper) GetProxyContractRegistry(ctx sdk.Context) (sdk.AccAddress, error) { + return k.ProxyContractRegistry.Get(ctx) +} + func (k Keeper) Logger(ctx sdk.Context) log.Logger { return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName)) } // GetParams returns all the parameters for the module. -func (k Keeper) GetParams(ctx sdk.Context) types.Params { - store := ctx.KVStore(k.storeKey) - bz := store.Get([]byte(types.KeyParams)) - - var params types.Params - k.cdc.MustUnmarshal(bz, ¶ms) - return params +func (k Keeper) GetParams(ctx sdk.Context) (types.Params, error) { + return k.Params.Get(ctx) } // SetParams sets the parameters in the store. func (k Keeper) SetParams(ctx sdk.Context, params types.Params) error { - store := ctx.KVStore(k.storeKey) - bz, err := k.cdc.Marshal(¶ms) - if err != nil { - return err - } - store.Set([]byte(types.KeyParams), bz) - - return nil + return k.Params.Set(ctx, params) } diff --git a/x/wasm-storage/keeper/keeper_test.go b/x/wasm-storage/keeper/keeper_test.go index f23dd908..f41808a6 100644 --- a/x/wasm-storage/keeper/keeper_test.go +++ b/x/wasm-storage/keeper/keeper_test.go @@ -33,7 +33,7 @@ func (s *KeeperTestSuite) TestGetDataRequestWasm() { WasmType: wasmstoragetypes.WasmTypeDataRequest, } s.wasmStorageKeeper.SetDataRequestWasm(s.ctx, mockWasm) - value := s.wasmStorageKeeper.GetDataRequestWasm(s.ctx, mockWasm.Hash) + value, _ := s.wasmStorageKeeper.GetDataRequestWasm(s.ctx, mockWasm.Hash) s.Assert().NotNil(value) s.Assert().Equal(*mockWasm, *value) } @@ -45,10 +45,10 @@ func (s *KeeperTestSuite) TestHasDataRequestWasm() { Bytecode: mockedByteArray, WasmType: wasmstoragetypes.WasmTypeDataRequest, } - has := s.wasmStorageKeeper.HasDataRequestWasm(s.ctx, mockWasm) + has, _ := s.wasmStorageKeeper.HasDataRequestWasm(s.ctx, mockWasm) s.Assert().False(has) s.wasmStorageKeeper.SetDataRequestWasm(s.ctx, mockWasm) - has = s.wasmStorageKeeper.HasDataRequestWasm(s.ctx, mockWasm) + has, _ = s.wasmStorageKeeper.HasDataRequestWasm(s.ctx, mockWasm) s.Assert().True(has) } @@ -70,7 +70,7 @@ func (s *KeeperTestSuite) TestGetOverlayWasm() { WasmType: wasmstoragetypes.WasmTypeRelayer, } s.wasmStorageKeeper.SetOverlayWasm(s.ctx, mockWasm) - value := s.wasmStorageKeeper.GetOverlayWasm(s.ctx, mockWasm.Hash) + value, _ := s.wasmStorageKeeper.GetOverlayWasm(s.ctx, mockWasm.Hash) s.Assert().NotNil(value) s.Assert().Equal(*mockWasm, *value) } @@ -82,10 +82,10 @@ func (s *KeeperTestSuite) TestHasOverlayWasm() { Bytecode: mockedByteArray, WasmType: wasmstoragetypes.WasmTypeRelayer, } - has := s.wasmStorageKeeper.HasOverlayWasm(s.ctx, mockWasm) + has, _ := s.wasmStorageKeeper.HasOverlayWasm(s.ctx, mockWasm) s.Assert().False(has) s.wasmStorageKeeper.SetOverlayWasm(s.ctx, mockWasm) - has = s.wasmStorageKeeper.HasOverlayWasm(s.ctx, mockWasm) + has, _ = s.wasmStorageKeeper.HasOverlayWasm(s.ctx, mockWasm) s.Assert().True(has) } diff --git a/x/wasm-storage/keeper/msg_server.go b/x/wasm-storage/keeper/msg_server.go index 7e7b814d..44dc96aa 100644 --- a/x/wasm-storage/keeper/msg_server.go +++ b/x/wasm-storage/keeper/msg_server.go @@ -64,10 +64,14 @@ func (m msgServer) StoreDataRequestWasm(goCtx context.Context, msg *types.MsgSto return nil, err } wasm := types.NewWasm(unzipped, msg.WasmType, ctx.BlockTime()) - if m.Keeper.HasDataRequestWasm(ctx, wasm) { + exists, _ := m.Keeper.HasDataRequestWasm(ctx, wasm) + if exists { return nil, fmt.Errorf("data Request Wasm with given hash already exists") } - m.Keeper.SetDataRequestWasm(ctx, wasm) + err = m.Keeper.SetDataRequestWasm(ctx, wasm) + if err != nil { + return nil, err + } hashString := hex.EncodeToString(wasm.Hash) @@ -104,10 +108,14 @@ func (m msgServer) StoreOverlayWasm(goCtx context.Context, msg *types.MsgStoreOv return nil, err } wasm := types.NewWasm(unzipped, msg.WasmType, ctx.BlockTime()) - if m.Keeper.HasOverlayWasm(ctx, wasm) { + exists, _ := m.Keeper.HasOverlayWasm(ctx, wasm) + if exists { return nil, fmt.Errorf("overlay Wasm with given hash already exists") } - m.Keeper.SetOverlayWasm(ctx, wasm) + err = m.Keeper.SetOverlayWasm(ctx, wasm) + if err != nil { + return nil, err + } hashString := hex.EncodeToString(wasm.Hash) err = ctx.EventManager().EmitTypedEvent( @@ -153,7 +161,10 @@ func (m msgServer) InstantiateAndRegisterProxyContract(goCtx context.Context, ms } // update Proxy Contract registry - m.SetProxyContractRegistry(ctx, contractAddr) + err = m.SetProxyContractRegistry(ctx, contractAddr) + if err != nil { + return nil, err + } return &types.MsgInstantiateAndRegisterProxyContractResponse{ ContractAddress: contractAddr.String(), diff --git a/x/wasm-storage/keeper/msg_server_test.go b/x/wasm-storage/keeper/msg_server_test.go index 0ccf8407..42d19448 100644 --- a/x/wasm-storage/keeper/msg_server_test.go +++ b/x/wasm-storage/keeper/msg_server_test.go @@ -313,7 +313,7 @@ func (s *KeeperTestSuite) TestUpdateParams() { s.Require().NoError(err) // Check that the Params were correctly set - params := s.wasmStorageKeeper.GetParams(s.ctx) + params, _ := s.wasmStorageKeeper.GetParams(s.ctx) s.Require().Equal(tc.input.Params, params) } }) diff --git a/x/wasm-storage/keeper/proxy_contract_registry.go b/x/wasm-storage/keeper/proxy_contract_registry.go deleted file mode 100644 index e4b24a43..00000000 --- a/x/wasm-storage/keeper/proxy_contract_registry.go +++ /dev/null @@ -1,20 +0,0 @@ -package keeper - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - - "github.com/sedaprotocol/seda-chain/x/wasm-storage/types" -) - -// SetProxyContractRegistry stores Proxy Contract address. -func (k Keeper) SetProxyContractRegistry(ctx sdk.Context, address sdk.AccAddress) { - store := ctx.KVStore(k.storeKey) - store.Set(types.KeyPrefixProxyContractRegistry, address.Bytes()) -} - -// GetProxyContractRegistry returns Proxy Contract address. -func (k Keeper) GetProxyContractRegistry(ctx sdk.Context) sdk.AccAddress { - store := ctx.KVStore(k.storeKey) - bz := store.Get(types.KeyPrefixProxyContractRegistry) - return sdk.AccAddress(bz) -} diff --git a/x/wasm-storage/keeper/querier.go b/x/wasm-storage/keeper/querier.go index 6e5492ce..934730ac 100644 --- a/x/wasm-storage/keeper/querier.go +++ b/x/wasm-storage/keeper/querier.go @@ -27,8 +27,13 @@ func (q Querier) DataRequestWasm(c context.Context, req *types.QueryDataRequestW if err != nil { return nil, err } + wasm, err := q.GetDataRequestWasm(ctx, hash) + if err != nil { + return nil, err + } + return &types.QueryDataRequestWasmResponse{ - Wasm: q.GetDataRequestWasm(ctx, hash), + Wasm: wasm, }, nil } @@ -45,8 +50,12 @@ func (q Querier) OverlayWasm(c context.Context, req *types.QueryOverlayWasmReque if err != nil { return nil, err } + wasm, err := q.GetOverlayWasm(ctx, hash) + if err != nil { + return nil, err + } return &types.QueryOverlayWasmResponse{ - Wasm: q.GetOverlayWasm(ctx, hash), + Wasm: wasm, }, nil } @@ -59,7 +68,11 @@ func (q Querier) OverlayWasms(c context.Context, _ *types.QueryOverlayWasmsReque func (q Querier) ProxyContractRegistry(c context.Context, _ *types.QueryProxyContractRegistryRequest) (*types.QueryProxyContractRegistryResponse, error) { ctx := sdk.UnwrapSDKContext(c) + proxyAddress, err := q.GetProxyContractRegistry(ctx) + if err != nil { + return nil, err + } return &types.QueryProxyContractRegistryResponse{ - Address: q.GetProxyContractRegistry(ctx).String(), + Address: proxyAddress.String(), }, nil } diff --git a/x/wasm-storage/types/keys.go b/x/wasm-storage/types/keys.go index 727a7828..75e4c3b1 100644 --- a/x/wasm-storage/types/keys.go +++ b/x/wasm-storage/types/keys.go @@ -1,11 +1,5 @@ package types -import ( - "time" - - sdk "github.com/cosmos/cosmos-sdk/types" -) - const ( // ModuleName defines the module name ModuleName = "wasm-storage" @@ -19,34 +13,3 @@ const ( // KeyParams is the store key for the parameters. KeyParams = "params" ) - -var ( - // KeyPrefixDataRequest defines prefix to store Data Request Wasm binaries. - KeyPrefixDataRequest = []byte{0x00} - - // KeyPrefixOverlay defines prefix to store Overlay Wasm binaries. - KeyPrefixOverlay = []byte{0x01} - - // KeyPrefixDataRequestQueue defines prefix to store the queue that contains - // the hashes of Data Request Wasm binaries. - KeyPrefixDataRequestQueue = []byte{0x02} - - // KeyPrefixProxyContractRegistry defines prefix to store address of - // Proxy Contract. - KeyPrefixProxyContractRegistry = []byte{0x03} -) - -func GetDataRequestWasmKey(hash []byte) []byte { - return append(KeyPrefixDataRequest, hash...) -} - -func GetOverlayWasmKey(hash []byte) []byte { - return append(KeyPrefixOverlay, hash...) -} - -// GetDataRequestTimeKey gets the key for an item in Data Request Queue. This key -// is the timestamp of when the Data Request Wasm was stored. -func GetDataRequestTimeKey(timestamp time.Time) []byte { - bz := sdk.FormatTimeBytes(timestamp) - return append(KeyPrefixDataRequestQueue, bz...) -}