Skip to content

Commit

Permalink
chore(data-proxy): add fee update endBlock handling
Browse files Browse the repository at this point in the history
Part-of: #316
  • Loading branch information
Thomasvdam committed Aug 17, 2024
1 parent a6cb490 commit 011edd0
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 7 deletions.
2 changes: 1 addition & 1 deletion proto/sedachain/data_proxy/v1/data_proxy.proto
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ message FeeUpdate {
// new_fee defines the new fee for the data proxy.
cosmos.base.v1beta1.Coin new_fee = 1 [ (gogoproto.nullable) = false ];

// update_height defines the height at which the new fee comes into effect.
// update_height defines the height after which the new fee comes into effect.
int64 update_height = 2;
}
2 changes: 1 addition & 1 deletion proto/sedachain/data_proxy/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ message MsgEditDataProxy {
string pub_key = 6;
}

// Returns the height at which the fee update will go into effect.
// Returns the height after which the fee update will go into effect.
message MsgEditDataProxyResponse { int64 update_height = 1; }

// The request message for the UpdateParams method.
Expand Down
54 changes: 54 additions & 0 deletions x/data-proxy/keeper/abci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package keeper

import (
"cosmossdk.io/collections"

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

func (k *Keeper) EndBlock(ctx sdk.Context) (err error) {
defer func() {
if r := recover(); r != nil {
k.Logger(ctx).Error("recovered from panic in data-proxy EndBlock", "err", r)
}
if err != nil {
k.Logger(ctx).Error("error in data-proxy EndBlock", "err", err)
}
err = nil
}()

err = k.ProcessFeeUpdates(ctx)
if err != nil {
return
}
return
}

func (k *Keeper) ProcessFeeUpdates(ctx sdk.Context) error {
blockHeight := ctx.BlockHeight()
pubkeys, err := k.GetFeeUpdates(ctx, blockHeight)
if err != nil {
return err
}

for _, pubkey := range pubkeys {
proxyConfig, err := k.DataProxyConfigs.Get(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 {
return err
}

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

// TODO emit events
}
return nil
}
7 changes: 7 additions & 0 deletions x/data-proxy/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package keeper
import (
"context"
"encoding/hex"
"fmt"

"cosmossdk.io/collections"
storetypes "cosmossdk.io/core/store"
"cosmossdk.io/log"

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

"github.com/sedaprotocol/seda-chain/x/data-proxy/types"
)
Expand Down Expand Up @@ -80,3 +83,7 @@ func (k Keeper) GetFeeUpdates(ctx context.Context, activationHeight int64) ([][]

return pubkeys, nil
}

func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
126 changes: 126 additions & 0 deletions x/data-proxy/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package keeper_test

import (
"encoding/hex"

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

"cosmossdk.io/collections"

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

func (s *KeeperTestSuite) TestKeeper_EndBlockFeeUpdate() {
s.Run("Apply single pending fee update", func() {
s.SetupTest()

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

err := s.keeper.DataProxyConfigs.Set(s.ctx, pubKeyBytes, types.ProxyConfig{
PayoutAddress: "seda1uea9km4nup9q7qu96ak683kc67x9jf7ste45z5",
Fee: s.NewFeeFromString("10000"),
Memo: "test",
FeeUpdate: &types.FeeUpdate{
UpdateHeight: 0,
NewFee: *s.NewFeeFromString("987654321"),
},
AdminAddress: "seda1uea9km4nup9q7qu96ak683kc67x9jf7ste45z5",
})
s.Require().NoError(err)
err = s.keeper.FeeUpdates.Set(s.ctx, collections.Join(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)
s.Require().NoError(err)

s.Require().Equal(types.ProxyConfig{
PayoutAddress: "seda1uea9km4nup9q7qu96ak683kc67x9jf7ste45z5",
Fee: s.NewFeeFromString("987654321"),
Memo: "test",
FeeUpdate: nil,
AdminAddress: "seda1uea9km4nup9q7qu96ak683kc67x9jf7ste45z5",
}, proxyConfig)

found, err := s.keeper.FeeUpdates.Has(s.ctx, collections.Join(int64(0), pubKeyBytes))
s.Require().NoError(err)
s.Require().False(found)
})

s.Run("Apply multiple pending fee updates", func() {
s.SetupTest()

pubKeys := []struct {
pubKeyBytes []byte
pubKeyHex string
}{}
for i := 0; i < 10; i++ {
pubKeyBytes := secp256k1.GenPrivKey().PubKey().Bytes()
pubKeyHex := hex.EncodeToString(pubKeyBytes)

pubKeys = append(pubKeys, struct {
pubKeyBytes []byte
pubKeyHex string
}{pubKeyBytes: pubKeyBytes, pubKeyHex: pubKeyHex})

updateHeight := int64(0)
if i >= 5 {
updateHeight = int64(1)
}

err := s.keeper.DataProxyConfigs.Set(s.ctx, pubKeyBytes, types.ProxyConfig{
PayoutAddress: "seda1uea9km4nup9q7qu96ak683kc67x9jf7ste45z5",
Fee: s.NewFeeFromString("10"),
Memo: "test",
FeeUpdate: &types.FeeUpdate{
UpdateHeight: updateHeight,
NewFee: *s.NewFeeFromString("30"),
},
AdminAddress: "seda1uea9km4nup9q7qu96ak683kc67x9jf7ste45z5",
})
s.Require().NoError(err)
err = s.keeper.FeeUpdates.Set(s.ctx, collections.Join(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)
s.Require().NoError(err)

expected := types.ProxyConfig{
PayoutAddress: "seda1uea9km4nup9q7qu96ak683kc67x9jf7ste45z5",
Fee: s.NewFeeFromString("10"),
Memo: "test",
FeeUpdate: &types.FeeUpdate{
UpdateHeight: 1,
NewFee: *s.NewFeeFromString("30"),
},
AdminAddress: "seda1uea9km4nup9q7qu96ak683kc67x9jf7ste45z5",
}
if i < 5 {
expected.Fee = &expected.FeeUpdate.NewFee
expected.FeeUpdate = nil
}

s.Require().Equal(expected, proxyConfig)

updateHeight := int64(0)
if i >= 5 {
updateHeight = int64(1)
}
found, err := s.keeper.FeeUpdates.Has(s.ctx, collections.Join(updateHeight, testInput.pubKeyBytes))
s.Require().NoError(err)
if i < 5 {
s.Require().False(found)
} else {
s.Require().True(found)
}
}
})
}
6 changes: 3 additions & 3 deletions x/data-proxy/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (am AppModule) BeginBlock(_ context.Context) error {
return nil
}

// EndBlock returns the end block logic for the wasm-storage module.
func (am AppModule) EndBlock(_ context.Context) error {
return nil
// EndBlock contains the logic that is automatically triggered at the end of each block
func (am AppModule) EndBlock(ctx context.Context) error {
return am.keeper.EndBlock(sdk.UnwrapSDKContext(ctx))
}
2 changes: 1 addition & 1 deletion x/data-proxy/types/data_proxy.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion x/data-proxy/types/tx.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 011edd0

Please sign in to comment.