-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(data-proxy): add fee update endBlock handling
Part-of: #316
- Loading branch information
1 parent
a6cb490
commit 011edd0
Showing
8 changed files
with
194 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.