Skip to content

Commit

Permalink
chore(data-proxy): add TX to transfer admin address
Browse files Browse the repository at this point in the history
Part-of: #316
  • Loading branch information
Thomasvdam committed Aug 22, 2024
1 parent 6a73fa4 commit 67d2663
Show file tree
Hide file tree
Showing 6 changed files with 608 additions and 46 deletions.
20 changes: 20 additions & 0 deletions proto/sedachain/data_proxy/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ service Msg {
// Edits an existing data proxy.
rpc EditDataProxy(MsgEditDataProxy) returns (MsgEditDataProxyResponse);

// Transfers the admin address of a data proxy
rpc TransferAdmin(MsgTransferAdmin) returns (MsgTransferAdminResponse);

// Used to update the modules parameters through governance.
rpc UpdateParams(MsgUpdateParams) returns (MsgUpdateParamsResponse);
}
Expand Down Expand Up @@ -73,6 +76,23 @@ message MsgEditDataProxy {
string pub_key = 6;
}

// Allow transferring the admin role to a different address.
message MsgTransferAdmin {
option (cosmos.msg.v1.signer) = "sender";

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

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

// hex encoded bytes as the expected flow is users sending updates from the
// browser
string pub_key = 3;
}

// No response required.
message MsgTransferAdminResponse {}

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

Expand Down
27 changes: 27 additions & 0 deletions x/data-proxy/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func GetTxCmd() *cobra.Command {
cmd.AddCommand(
RegisterDataProxy(),
EditDataProxy(),
TransferAdmin(),
)
return cmd
}
Expand Down Expand Up @@ -119,3 +120,29 @@ func EditDataProxy() *cobra.Command {

return cmd
}

func TransferAdmin() *cobra.Command {
cmd := &cobra.Command{
Use: "transfer-admin [public_key_hex] [new_admin_address] --from [admin_address]",
Short: "Transfer the admin rights of the data proxy to a different address",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

msg := &types.MsgTransferAdmin{
Sender: clientCtx.GetFromAddress().String(),
PubKey: args[0],
NewAdminAddress: args[1],
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}
34 changes: 34 additions & 0 deletions x/data-proxy/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,40 @@ func (m msgServer) EditDataProxy(goCtx context.Context, msg *types.MsgEditDataPr
}, nil
}

func (m msgServer) TransferAdmin(goCtx context.Context, msg *types.MsgTransferAdmin) (*types.MsgTransferAdminResponse, 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
}

proxyConfig.AdminAddress = msg.NewAdminAddress

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

return &types.MsgTransferAdminResponse{}, nil
}

func (m msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

Expand Down
29 changes: 29 additions & 0 deletions x/data-proxy/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,33 @@ func (s *KeeperTestSuite) TestMsgServer_EditDataProxy() {
s.Require().NoError(err)
s.Require().False(firstUpdateNoLongerScheduled)
})

s.Run("Transferring admin address should allow the new address to submit changes", func() {
s.SetupTest()

err = s.keeper.DataProxyConfigs.Set(s.ctx, pubKeyBytes, initialProxyConfig)
s.Require().NoError(err)

editMsg := &types.MsgEditDataProxy{
Sender: "seda1wyzxdtpl0c99c92n397r3drlhj09qfjvf6teyh",
NewPayoutAddress: "seda1wyzxdtpl0c99c92n397r3drlhj09qfjvf6teyh",
NewMemo: types.DoNotModifyField,
PubKey: "02100efce2a783cc7a3fbf9c5d15d4cc6e263337651312f21a35d30c16cb38f4c3",
}
failedEdit, err := s.msgSrvr.EditDataProxy(s.ctx, editMsg)
s.Require().ErrorIs(err, types.ErrUnauthorized)
s.Require().Nil(failedEdit)

transferRes, err := s.msgSrvr.TransferAdmin(s.ctx, &types.MsgTransferAdmin{
Sender: "seda1uea9km4nup9q7qu96ak683kc67x9jf7ste45z5",
NewAdminAddress: "seda1wyzxdtpl0c99c92n397r3drlhj09qfjvf6teyh",
PubKey: "02100efce2a783cc7a3fbf9c5d15d4cc6e263337651312f21a35d30c16cb38f4c3",
})
s.Require().NoError(err)
s.Require().NotNil(transferRes)

successfulEdit, err := s.msgSrvr.EditDataProxy(s.ctx, editMsg)
s.Require().NoError(err)
s.Require().NotNil(successfulEdit)
})
}
15 changes: 15 additions & 0 deletions x/data-proxy/types/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ func (m *MsgEditDataProxy) Validate() error {
return nil
}

func (m *MsgTransferAdmin) Validate() error {
if m.Sender == "" {
return ErrEmptyValue.Wrap("empty sender")
}

if m.NewAdminAddress == "" {
return ErrEmptyValue.Wrap("empty admin address")
}
if _, err := sdk.AccAddressFromBech32(m.NewAdminAddress); err != nil {
return ErrInvalidAddress.Wrap(err.Error())
}

return nil
}

func (m *MsgUpdateParams) Validate() error {
// TODO
return nil
Expand Down
Loading

0 comments on commit 67d2663

Please sign in to comment.