Skip to content

Commit

Permalink
chore(data-proxy): add edit message 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 e9b0f43 commit a35dc0d
Show file tree
Hide file tree
Showing 8 changed files with 580 additions and 65 deletions.
17 changes: 13 additions & 4 deletions proto/sedachain/data_proxy/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,23 @@ message MsgRegisterDataProxyResponse {}
// Allow updating memo and payout address instantly and/or scheduling a fee
// update.
message MsgEditDataProxy {
string new_payout_address = 1
option (cosmos.msg.v1.signer) = "sender";

string sender = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];

string new_payout_address = 2
[ (cosmos_proto.scalar) = "cosmos.AddressString" ];

string new_memo = 2 [ (gogoproto.nullable) = true ];
string new_memo = 3;

cosmos.base.v1beta1.Coin new_fee = 4 [ (gogoproto.nullable) = true ];

cosmos.base.v1beta1.Coin new_fee = 3 [ (gogoproto.nullable) = true ];
// 0 will default to the minimum delay configured in the params
uint32 fee_update_delay = 5;

uint32 fee_update_delay = 4 [ (gogoproto.nullable) = true ];
// hex encoded bytes as the expected flow is users sending updates from the
// browser
string pub_key = 6;
}

// Returns the height at which the fee update will go into effect.
Expand Down
3 changes: 3 additions & 0 deletions x/data-proxy/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,7 @@ func (s *KeeperTestSuite) SetupTest() {
querier := keeper.Querier{Keeper: *s.keeper}
types.RegisterQueryServer(queryHelper, querier)
s.queryClient = types.NewQueryClient(queryHelper)

err := s.keeper.Params.Set(s.ctx, types.DefaultParams())
s.Require().NoError(err)
}
108 changes: 103 additions & 5 deletions x/data-proxy/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package keeper
import (
"context"
"encoding/hex"
"errors"
"fmt"

"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/secp256k1"

"cosmossdk.io/collections"

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

"github.com/sedaprotocol/seda-chain/x/data-proxy/types"
Expand Down Expand Up @@ -67,23 +70,118 @@ func (m msgServer) RegisterDataProxy(goCtx context.Context, msg *types.MsgRegist
return nil, types.ErrInvalidSignature
}

err = m.DataProxyConfigs.Set(ctx, pubKeyBytes, types.ProxyConfig{
proxyConfig := types.ProxyConfig{
PayoutAddress: msg.PayoutAddress,
Fee: msg.Fee,
Memo: msg.Memo,
FeeUpdate: nil,
AdminAddress: msg.AdminAddress,
})
}

err = proxyConfig.Validate()
if err != nil {
return nil, err
}

err = m.DataProxyConfigs.Set(ctx, pubKeyBytes, proxyConfig)
if err != nil {
return nil, err
}

return &types.MsgRegisterDataProxyResponse{}, nil
}

func (m msgServer) EditDataProxy(_ context.Context, _ *types.MsgEditDataProxy) (*types.MsgEditDataProxyResponse, error) {
// TODO
return &types.MsgEditDataProxyResponse{}, nil
func (m msgServer) EditDataProxy(goCtx context.Context, msg *types.MsgEditDataProxy) (*types.MsgEditDataProxyResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

if err := msg.Validate(); err != nil {
return nil, err
}

pubKeyBytes, err := hex.DecodeString(msg.PubKey)
if err != nil {
return nil, types.ErrInvalidHex.Wrap(err.Error())
}

proxyConfig, err := m.DataProxyConfigs.Get(ctx, pubKeyBytes)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return nil, types.ErrUnknownDataProxy.Wrapf("no data proxy registered for %s", msg.PubKey)
}
return nil, err
}

if msg.Sender != proxyConfig.AdminAddress {
return nil, types.ErrUnauthorized
}

err = proxyConfig.UpdateBasic(msg.NewPayoutAddress, msg.NewMemo)
if err != nil {
return nil, err
}

// If there is no new fee we can terminate early
if msg.NewFee == nil {
err = m.DataProxyConfigs.Set(ctx, pubKeyBytes, proxyConfig)
if err != nil {
return nil, err
}

return &types.MsgEditDataProxyResponse{}, nil
}

return m.ProcessProxyFeeUpdate(ctx, pubKeyBytes, &proxyConfig, msg)
}

func (m msgServer) ProcessProxyFeeUpdate(ctx sdk.Context, pubKeyBytes []byte, proxyConfig *types.ProxyConfig, msg *types.MsgEditDataProxy) (*types.MsgEditDataProxyResponse, error) {
params, err := m.Keeper.Params.Get(ctx)
if err != nil {
return nil, err
}

updateDelay := params.MinFeeUpdateDelay
// Validate custom delay if passed
if msg.FeeUpdateDelay != types.UseMinimumDelay {
if msg.FeeUpdateDelay < params.MinFeeUpdateDelay {
return nil, types.ErrInvalidDelay.Wrapf("minimum delay %d, got %d", params.MinFeeUpdateDelay, msg.FeeUpdateDelay)
}

updateDelay = msg.FeeUpdateDelay
}

// Determine update height
updateHeight := ctx.BlockHeight() + int64(updateDelay)
feeUpdate := &types.FeeUpdate{
NewFee: *msg.NewFee,
UpdateHeight: updateHeight,
}

// Retain previous pending update so it can be removed
previousPendingUpdate := proxyConfig.FeeUpdate
proxyConfig.FeeUpdate = feeUpdate

// Schedule new update
err = m.Keeper.FeeUpdates.Set(ctx, collections.Join(updateHeight, pubKeyBytes))
if err != nil {
return nil, err
}

// Delete previous pending update, if applicable
if previousPendingUpdate != nil {
err = m.Keeper.FeeUpdates.Remove(ctx, collections.Join(previousPendingUpdate.UpdateHeight, pubKeyBytes))
if err != nil {
return nil, err
}
}

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

return &types.MsgEditDataProxyResponse{
UpdateHeight: updateHeight,
}, nil
}

func (m msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
Expand Down
Loading

0 comments on commit a35dc0d

Please sign in to comment.