Skip to content

Commit

Permalink
chore(data-proxy): avoid accessing the stores directly from boundaries
Browse files Browse the repository at this point in the history
Closes: #316
  • Loading branch information
Thomasvdam committed Aug 22, 2024
1 parent 47f26fd commit d5666b4
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 105 deletions.
8 changes: 3 additions & 5 deletions x/data-proxy/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package keeper
import (
"encoding/hex"

"cosmossdk.io/collections"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/sedaprotocol/seda-chain/x/data-proxy/types"
Expand Down Expand Up @@ -36,19 +34,19 @@ func (k *Keeper) ProcessFeeUpdates(ctx sdk.Context) error {
}

for _, pubkey := range pubkeys {
proxyConfig, err := k.DataProxyConfigs.Get(ctx, pubkey)
proxyConfig, err := k.GetDataProxyConfig(ctx, pubkey)
if err != nil {
return err
}

proxyConfig.Fee = proxyConfig.FeeUpdate.NewFee
proxyConfig.FeeUpdate = nil

if err := k.DataProxyConfigs.Set(ctx, pubkey, proxyConfig); err != nil {
if err := k.SetDataProxyConfig(ctx, pubkey, proxyConfig); err != nil {
return err
}

if err := k.FeeUpdateQueue.Remove(ctx, collections.Join(blockHeight, pubkey)); err != nil {
if err := k.RemoveFeeUpdate(ctx, blockHeight, pubkey); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion x/data-proxy/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (s *KeeperTestSuite) SetupTest() {
types.RegisterQueryServer(queryHelper, querier)
s.queryClient = types.NewQueryClient(queryHelper)

err := s.keeper.Params.Set(s.ctx, types.DefaultParams())
err := s.keeper.SetParams(s.ctx, types.DefaultParams())
s.Require().NoError(err)
}

Expand Down
14 changes: 6 additions & 8 deletions x/data-proxy/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
package keeper

import (
"cosmossdk.io/collections"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/sedaprotocol/seda-chain/x/data-proxy/types"
)

// InitGenesis initializes the store based on the given genesis state.
func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) {
if err := k.Params.Set(ctx, data.Params); err != nil {
if err := k.SetParams(ctx, data.Params); err != nil {
panic(err)
}

for _, dataProxyConfig := range data.DataProxyConfigs {
if err := k.DataProxyConfigs.Set(ctx, dataProxyConfig.DataProxyPubkey, *dataProxyConfig.Config); err != nil {
if err := k.SetDataProxyConfig(ctx, dataProxyConfig.DataProxyPubkey, *dataProxyConfig.Config); err != nil {
panic(err)
}
}

for _, feeUpdate := range data.FeeUpdateQueue {
if err := k.FeeUpdateQueue.Set(ctx, collections.Join(feeUpdate.UpdateHeight, feeUpdate.DataProxyPubkey)); err != nil {
if err := k.SetFeeUpdate(ctx, feeUpdate.UpdateHeight, feeUpdate.DataProxyPubkey); err != nil {
panic(err)
}
}
Expand All @@ -31,7 +29,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) {
func (k Keeper) ExportGenesis(ctx sdk.Context) types.GenesisState {
var gs types.GenesisState

params, err := k.Params.Get(ctx)
params, err := k.params.Get(ctx)
if err != nil {
panic(err)
}
Expand All @@ -55,7 +53,7 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) types.GenesisState {
func (k Keeper) getAllDataProxyConfigs(ctx sdk.Context) ([]types.DataProxyConfig, error) {
configs := make([]types.DataProxyConfig, 0)

itr, err := k.DataProxyConfigs.Iterate(ctx, nil)
itr, err := k.dataProxyConfigs.Iterate(ctx, nil)
if err != nil {
return nil, err
}
Expand All @@ -79,7 +77,7 @@ func (k Keeper) getAllDataProxyConfigs(ctx sdk.Context) ([]types.DataProxyConfig
func (k Keeper) getAllFeeUpdateRecords(ctx sdk.Context) ([]types.FeeUpdateQueueRecord, error) {
feeUpdates := make([]types.FeeUpdateQueueRecord, 0)

itr, err := k.FeeUpdateQueue.Iterate(ctx, nil)
itr, err := k.feeUpdateQueue.Iterate(ctx, nil)
if err != nil {
return nil, err
}
Expand Down
9 changes: 8 additions & 1 deletion x/data-proxy/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package keeper

import (
"context"
"encoding/hex"
"errors"

"cosmossdk.io/collections"
errorsmod "cosmossdk.io/errors"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

Expand All @@ -18,7 +20,12 @@ type Querier struct {
}

func (q Querier) DataProxyConfig(ctx context.Context, req *types.QueryDataProxyConfigRequest) (*types.QueryDataProxyConfigResponse, error) {
result, err := q.GetDataProxyConfig(ctx, req.PubKey)
pubKeyBytes, err := hex.DecodeString(req.PubKey)
if err != nil {
return nil, errorsmod.Wrapf(err, "invalid hex in pubkey: %s", req.PubKey)
}

result, err := q.GetDataProxyConfig(ctx, pubKeyBytes)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return nil, sdkerrors.ErrNotFound.Wrapf("no data proxy registered for %s", req.PubKey)
Expand Down
2 changes: 1 addition & 1 deletion x/data-proxy/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (s *KeeperTestSuite) TestQuerier_ProxyConfig() {
pubkeyBytes, err := hex.DecodeString(tt.pubKeyHex)
s.Require().NoError(err)

err = s.keeper.DataProxyConfigs.Set(s.ctx, pubkeyBytes, *tt.config)
err = s.keeper.SetDataProxyConfig(s.ctx, pubkeyBytes, *tt.config)
s.Require().NoError(err)
}

Expand Down
84 changes: 49 additions & 35 deletions x/data-proxy/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper

import (
"context"
"encoding/hex"
"fmt"

"cosmossdk.io/collections"
Expand All @@ -22,18 +21,18 @@ type Keeper struct {
authority string

Schema collections.Schema
DataProxyConfigs collections.Map[[]byte, types.ProxyConfig]
FeeUpdateQueue collections.KeySet[collections.Pair[int64, []byte]]
Params collections.Item[types.Params]
dataProxyConfigs collections.Map[[]byte, types.ProxyConfig]
feeUpdateQueue collections.KeySet[collections.Pair[int64, []byte]]
params collections.Item[types.Params]
}

func NewKeeper(cdc codec.BinaryCodec, storeService storetypes.KVStoreService, authority string) *Keeper {
sb := collections.NewSchemaBuilder(storeService)
k := Keeper{
authority: authority,
DataProxyConfigs: collections.NewMap(sb, types.DataProxyConfigPrefix, "configs", collections.BytesKey, codec.CollValue[types.ProxyConfig](cdc)),
FeeUpdateQueue: collections.NewKeySet(sb, types.FeeUpdatesPrefix, "fee_updates", collections.PairKeyCodec(collections.Int64Key, collections.BytesKey)),
Params: collections.NewItem(sb, types.ParamsPrefix, "params", codec.CollValue[types.Params](cdc)),
dataProxyConfigs: collections.NewMap(sb, types.DataProxyConfigPrefix, "configs", collections.BytesKey, codec.CollValue[types.ProxyConfig](cdc)),
feeUpdateQueue: collections.NewKeySet(sb, types.FeeUpdatesPrefix, "fee_updates", collections.PairKeyCodec(collections.Int64Key, collections.BytesKey)),
params: collections.NewItem(sb, types.ParamsPrefix, "params", codec.CollValue[types.Params](cdc)),
}

schema, err := sb.Build()
Expand All @@ -49,42 +48,32 @@ func (k Keeper) GetAuthority() string {
return k.authority
}

func (k Keeper) GetDataProxyConfig(ctx context.Context, pubKey string) (result types.ProxyConfig, err error) {
pubKeyBytes, err := hex.DecodeString(pubKey)
if err != nil {
return types.ProxyConfig{}, err
}
func (k Keeper) HasDataProxy(ctx sdk.Context, pubKey []byte) (bool, error) {
return k.dataProxyConfigs.Has(ctx, pubKey)
}

func (k Keeper) SetDataProxyConfig(ctx context.Context, pubKey []byte, proxyConfig types.ProxyConfig) error {
return k.dataProxyConfigs.Set(ctx, pubKey, proxyConfig)
}

config, err := k.DataProxyConfigs.Get(ctx, pubKeyBytes)
func (k Keeper) GetDataProxyConfig(ctx context.Context, pubKey []byte) (result types.ProxyConfig, err error) {
config, err := k.dataProxyConfigs.Get(ctx, pubKey)
if err != nil {
return types.ProxyConfig{}, err
}

return config, nil
}

func (k Keeper) GetFeeUpdatePubKeys(ctx context.Context, activationHeight int64) ([][]byte, error) {
pubkeys := make([][]byte, 0)
rng := collections.NewPrefixedPairRange[int64, []byte](activationHeight)

itr, err := k.FeeUpdateQueue.Iterate(ctx, rng)
if err != nil {
return nil, err
}

keys, err := itr.Keys()
if err != nil {
return nil, err
}

for _, k := range keys {
pubkeys = append(pubkeys, k.K2())
}
func (k Keeper) SetFeeUpdate(ctx sdk.Context, height int64, pubKey []byte) error {
return k.feeUpdateQueue.Set(ctx, collections.Join(height, pubKey))
}

return pubkeys, nil
func (k Keeper) RemoveFeeUpdate(ctx sdk.Context, height int64, pubKey []byte) error {
return k.feeUpdateQueue.Remove(ctx, collections.Join(height, pubKey))
}

func (k Keeper) processProxyFeeUpdate(ctx sdk.Context, pubKeyBytes []byte, proxyConfig *types.ProxyConfig, newFee *sdk.Coin, updateDelay uint32) (int64, error) {
func (k Keeper) processProxyFeeUpdate(ctx sdk.Context, pubKeyBytes []byte, proxyConfig types.ProxyConfig, newFee *sdk.Coin, updateDelay uint32) (int64, error) {
// Determine update height
updateHeight := ctx.BlockHeight() + int64(updateDelay)
feeUpdate := &types.FeeUpdate{
Expand All @@ -94,27 +83,52 @@ func (k Keeper) processProxyFeeUpdate(ctx sdk.Context, pubKeyBytes []byte, proxy

// Delete previous pending update, if applicable
if proxyConfig.FeeUpdate != nil {
err := k.FeeUpdateQueue.Remove(ctx, collections.Join(proxyConfig.FeeUpdate.UpdateHeight, pubKeyBytes))
err := k.RemoveFeeUpdate(ctx, proxyConfig.FeeUpdate.UpdateHeight, pubKeyBytes)
if err != nil {
return 0, err
}
}

// Schedule new update
proxyConfig.FeeUpdate = feeUpdate
err := k.FeeUpdateQueue.Set(ctx, collections.Join(updateHeight, pubKeyBytes))
err := k.SetFeeUpdate(ctx, updateHeight, pubKeyBytes)
if err != nil {
return 0, err
}

err = k.DataProxyConfigs.Set(ctx, pubKeyBytes, *proxyConfig)
err = k.SetDataProxyConfig(ctx, pubKeyBytes, proxyConfig)
if err != nil {
return 0, err
}

return updateHeight, nil
}

func (k Keeper) GetFeeUpdatePubKeys(ctx sdk.Context, activationHeight int64) ([][]byte, error) {
pubkeys := make([][]byte, 0)
rng := collections.NewPrefixedPairRange[int64, []byte](activationHeight)

itr, err := k.feeUpdateQueue.Iterate(ctx, rng)
if err != nil {
return nil, err
}

keys, err := itr.Keys()
if err != nil {
return nil, err
}

for _, k := range keys {
pubkeys = append(pubkeys, k.K2())
}

return pubkeys, nil
}

func (k Keeper) HasFeeUpdate(ctx sdk.Context, height int64, pubKey []byte) (bool, error) {
return k.feeUpdateQueue.Has(ctx, collections.Join(height, pubKey))
}

func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
23 changes: 10 additions & 13 deletions x/data-proxy/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (

"github.com/cometbft/cometbft/crypto/secp256k1"

"cosmossdk.io/collections"

"github.com/sedaprotocol/seda-chain/x/data-proxy/types"
)

Expand All @@ -15,9 +13,8 @@ func (s *KeeperTestSuite) TestKeeper_EndBlockFeeUpdate() {
s.SetupTest()

pubKeyBytes := secp256k1.GenPrivKey().PubKey().Bytes()
pubKeyHex := hex.EncodeToString(pubKeyBytes)

err := s.keeper.DataProxyConfigs.Set(s.ctx, pubKeyBytes, types.ProxyConfig{
err := s.keeper.SetDataProxyConfig(s.ctx, pubKeyBytes, types.ProxyConfig{
PayoutAddress: "seda1uea9km4nup9q7qu96ak683kc67x9jf7ste45z5",
Fee: s.NewFeeFromString("10000"),
Memo: "test",
Expand All @@ -28,13 +25,13 @@ func (s *KeeperTestSuite) TestKeeper_EndBlockFeeUpdate() {
AdminAddress: "seda1uea9km4nup9q7qu96ak683kc67x9jf7ste45z5",
})
s.Require().NoError(err)
err = s.keeper.FeeUpdateQueue.Set(s.ctx, collections.Join(int64(0), pubKeyBytes))
err = s.keeper.SetFeeUpdate(s.ctx, int64(0), pubKeyBytes)
s.Require().NoError(err)

err = s.keeper.EndBlock(s.ctx)
s.Require().NoError(err)

proxyConfig, err := s.keeper.GetDataProxyConfig(s.ctx, pubKeyHex)
proxyConfig, err := s.keeper.GetDataProxyConfig(s.ctx, pubKeyBytes)
s.Require().NoError(err)

s.Require().Equal(types.ProxyConfig{
Expand All @@ -45,7 +42,7 @@ func (s *KeeperTestSuite) TestKeeper_EndBlockFeeUpdate() {
AdminAddress: "seda1uea9km4nup9q7qu96ak683kc67x9jf7ste45z5",
}, proxyConfig)

found, err := s.keeper.FeeUpdateQueue.Has(s.ctx, collections.Join(int64(0), pubKeyBytes))
found, err := s.keeper.HasFeeUpdate(s.ctx, int64(0), pubKeyBytes)
s.Require().NoError(err)
s.Require().False(found)
})
Expand All @@ -71,7 +68,7 @@ func (s *KeeperTestSuite) TestKeeper_EndBlockFeeUpdate() {
updateHeight = int64(1)
}

err := s.keeper.DataProxyConfigs.Set(s.ctx, pubKeyBytes, types.ProxyConfig{
err := s.keeper.SetDataProxyConfig(s.ctx, pubKeyBytes, types.ProxyConfig{
PayoutAddress: "seda1uea9km4nup9q7qu96ak683kc67x9jf7ste45z5",
Fee: s.NewFeeFromString("10"),
Memo: "test",
Expand All @@ -82,15 +79,15 @@ func (s *KeeperTestSuite) TestKeeper_EndBlockFeeUpdate() {
AdminAddress: "seda1uea9km4nup9q7qu96ak683kc67x9jf7ste45z5",
})
s.Require().NoError(err)
err = s.keeper.FeeUpdateQueue.Set(s.ctx, collections.Join(updateHeight, pubKeyBytes))
err = s.keeper.SetFeeUpdate(s.ctx, updateHeight, pubKeyBytes)
s.Require().NoError(err)
}

err := s.keeper.EndBlock(s.ctx)
s.Require().NoError(err)

for i, testInput := range pubKeys {
proxyConfig, err := s.keeper.GetDataProxyConfig(s.ctx, testInput.pubKeyHex)
proxyConfig, err := s.keeper.GetDataProxyConfig(s.ctx, testInput.pubKeyBytes)
s.Require().NoError(err)

expected := types.ProxyConfig{
Expand All @@ -114,7 +111,7 @@ func (s *KeeperTestSuite) TestKeeper_EndBlockFeeUpdate() {
if i >= 5 {
updateHeight = int64(1)
}
found, err := s.keeper.FeeUpdateQueue.Has(s.ctx, collections.Join(updateHeight, testInput.pubKeyBytes))
found, err := s.keeper.HasFeeUpdate(s.ctx, updateHeight, testInput.pubKeyBytes)
s.Require().NoError(err)
if i < 5 {
s.Require().False(found)
Expand All @@ -129,7 +126,7 @@ func (s *KeeperTestSuite) TestKeeper_EndBlockFeeUpdate() {
s.Require().NoError(err)

for i, testInput := range pubKeys {
proxyConfig, err := s.keeper.GetDataProxyConfig(s.ctx, testInput.pubKeyHex)
proxyConfig, err := s.keeper.GetDataProxyConfig(s.ctx, testInput.pubKeyBytes)
s.Require().NoError(err)

expected := types.ProxyConfig{
Expand All @@ -152,7 +149,7 @@ func (s *KeeperTestSuite) TestKeeper_EndBlockFeeUpdate() {
if i >= 5 {
updateHeight = int64(1)
}
found, err := s.keeper.FeeUpdateQueue.Has(s.ctx, collections.Join(updateHeight, testInput.pubKeyBytes))
found, err := s.keeper.HasFeeUpdate(s.ctx, updateHeight, testInput.pubKeyBytes)
s.Require().NoError(err)
s.Require().False(found)
}
Expand Down
Loading

0 comments on commit d5666b4

Please sign in to comment.