Skip to content

Commit

Permalink
feat: Update Registered Data Validator (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
inchori committed Apr 6, 2022
1 parent 3f37140 commit d5af49b
Show file tree
Hide file tree
Showing 12 changed files with 602 additions and 68 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ require (

replace google.golang.org/grpc => google.golang.org/grpc v1.33.2

replace github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
replace github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
14 changes: 14 additions & 0 deletions proto/panacea/datapool/v2/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ service Msg {
// RegisterDataValidator defines a method for registration of data validator.
rpc RegisterDataValidator(MsgRegisterDataValidator) returns (MsgRegisterDataValidatorResponse);

// UpdateDataValidator defines a method for updating of data validator.
rpc UpdateDataValidator(MsgUpdateDataValidator) returns (MsgUpdateDataValidatorResponse);

// CreatePool defines a method for creating data pool.
rpc CreatePool(MsgCreatePool) returns (MsgCreatePoolResponse);

Expand Down Expand Up @@ -43,6 +46,17 @@ message MsgRegisterDataValidator {
message MsgRegisterDataValidatorResponse {
}

// MsgUpdateDataValidator defines the Msg/UpdateDataValidator request type.
message MsgUpdateDataValidator {
string data_validator = 1;
string endpoint = 2;
}

// MsgUpdateResponse defines the Msg/UpdateDataValidator response type.
message MsgUpdateDataValidatorResponse {

}

// MsgCreatePool defines the Msg/CreatePool request type.
message MsgCreatePool {
string curator = 1; // 'panacea1' address
Expand Down
3 changes: 2 additions & 1 deletion x/datapool/client/cli/queryPool.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package cli

import (
"strconv"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/medibloc/panacea-core/v2/x/datapool/types"
"github.com/spf13/cobra"
"strconv"
)

func CmdGetDataValidator() *cobra.Command {
Expand Down
1 change: 1 addition & 0 deletions x/datapool/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func GetTxCmd() *cobra.Command {
}

cmd.AddCommand(CmdRegisterDataValidator())
cmd.AddCommand(CmdUpdateDataValidator())
cmd.AddCommand(CmdCreatePool())
cmd.AddCommand(CmdRegisterNFTContract())
cmd.AddCommand(CmdUpgradeNFTContract())
Expand Down
25 changes: 25 additions & 0 deletions x/datapool/client/cli/txPool.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,31 @@ func CmdRegisterDataValidator() *cobra.Command {
return cmd
}

func CmdUpdateDataValidator() *cobra.Command {
cmd := &cobra.Command{
Use: "update-data-validator [endpoint URL]",
Short: "update data validator endpoint",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return nil
}

fromAddress := clientCtx.GetFromAddress()

msg := types.NewMsgUpdateDataValidator(fromAddress.String(), args[0])
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)
return cmd
}

func CmdCreatePool() *cobra.Command {
cmd := &cobra.Command{
Use: "create-pool [pool params file]",
Expand Down
3 changes: 3 additions & 0 deletions x/datapool/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
case *types.MsgRegisterDataValidator:
res, err := msgServer.RegisterDataValidator(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgUpdateDataValidator:
res, err := msgServer.UpdateDataValidator(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgCreatePool:
res, err := msgServer.CreatePool(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
Expand Down
15 changes: 15 additions & 0 deletions x/datapool/keeper/msg_server_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ func (m msgServer) RegisterDataValidator(goCtx context.Context, msg *types.MsgRe
return &types.MsgRegisterDataValidatorResponse{}, nil
}

func (m msgServer) UpdateDataValidator(goCtx context.Context, msg *types.MsgUpdateDataValidator) (*types.MsgUpdateDataValidatorResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

address, err := sdk.AccAddressFromBech32(msg.DataValidator)
if err != nil {
return nil, err
}

err = m.Keeper.UpdateDataValidator(ctx, address, msg.Endpoint)
if err != nil {
return nil, err
}
return &types.MsgUpdateDataValidatorResponse{}, nil
}

func (m msgServer) CreatePool(goCtx context.Context, msg *types.MsgCreatePool) (*types.MsgCreatePoolResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

Expand Down
18 changes: 17 additions & 1 deletion x/datapool/keeper/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ func (k Keeper) IsRegisteredDataValidator(ctx sdk.Context, dataValidatorAddress
return store.Has(dataValidatorKey)
}

func (k Keeper) UpdateDataValidator(ctx sdk.Context, address sdk.AccAddress, endpoint string) error {
validator, err := k.GetDataValidator(ctx, address)
if err != nil {
return err
}

validator.Endpoint = endpoint

err = k.SetDataValidator(ctx, validator)
if err != nil {
return err
}

return nil
}

func (k Keeper) CreatePool(ctx sdk.Context, curator sdk.AccAddress, poolParams types.PoolParams) (uint64, error) {
// Get the next pool id
poolID := k.GetNextPoolNumberAndIncrement(ctx)
Expand Down Expand Up @@ -187,7 +203,7 @@ func (k Keeper) GetPool(ctx sdk.Context, poolID uint64) (*types.Pool, error) {
poolKey := types.GetKeyPrefixPools(poolID)
bz := store.Get(poolKey)
if bz == nil {
return nil, types.ErrPoolNotFound
return nil, types.ErrPoolNotFound
}
pool := &types.Pool{}
k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, pool)
Expand Down
34 changes: 33 additions & 1 deletion x/datapool/keeper/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (suite *poolTestSuite) TestIsDataValidatorDuplicate() {
suite.Require().Error(err, types.ErrDataValidatorAlreadyExist)
}

func (suite *poolTestSuite) TestNotGetPubkey() {
func (suite *poolTestSuite) TestNotGetPubKey() {
err := suite.BankKeeper.AddCoins(suite.Ctx, dataVal1, fundForDataVal)
suite.Require().NoError(err)

Expand All @@ -108,6 +108,38 @@ func (suite *poolTestSuite) TestNotGetPubkey() {
suite.Require().Error(err, sdkerrors.ErrKeyNotFound)
}

func (suite *poolTestSuite) TestUpdateDataValidator() {
err := suite.BankKeeper.AddCoins(suite.Ctx, dataVal1, fundForDataVal)
suite.Require().NoError(err)

validatorAccount := suite.AccountKeeper.NewAccountWithAddress(suite.Ctx, dataVal1)
err = validatorAccount.SetPubKey(pubKey)
suite.Require().NoError(err)
suite.AccountKeeper.SetAccount(suite.Ctx, validatorAccount)

tempDataValidator := types.DataValidator{
Address: dataVal1.String(),
Endpoint: "https://my-validator.org",
}

err = suite.DataPoolKeeper.RegisterDataValidator(suite.Ctx, tempDataValidator)
suite.Require().NoError(err)

updateTempDataValidator := types.DataValidator{
Address: dataVal1.String(),
Endpoint: "https://update-my-validator.org",
}

err = suite.DataPoolKeeper.UpdateDataValidator(suite.Ctx, dataVal1, updateTempDataValidator.Endpoint)
suite.Require().NoError(err)

getDataValidator, err := suite.DataPoolKeeper.GetDataValidator(suite.Ctx, dataVal1)
suite.Require().NoError(err)

suite.Require().Equal(getDataValidator.GetAddress(), updateTempDataValidator.GetAddress())
suite.Require().Equal(getDataValidator.GetEndpoint(), updateTempDataValidator.GetEndpoint())
}

func (suite *poolTestSuite) TestGetPool() {
poolID := uint64(1)
nftPrice := sdk.NewCoin(assets.MicroMedDenom, sdk.NewInt(1000000))
Expand Down
2 changes: 2 additions & 0 deletions x/datapool/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

func RegisterCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgRegisterDataValidator{}, "datapool/RegisterDataValidator", nil)
cdc.RegisterConcrete(&MsgUpdateDataValidator{}, "datapool/UpdateDataValidator", nil)
cdc.RegisterConcrete(&MsgCreatePool{}, "datapool/CreatePool", nil)
cdc.RegisterConcrete(&MsgSellData{}, "datapool/SellData", nil)
cdc.RegisterConcrete(&MsgBuyDataAccessNFT{}, "datapool/BuyDataAccessNFT", nil)
Expand All @@ -22,6 +23,7 @@ func RegisterCodec(cdc *codec.LegacyAmino) {
func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgRegisterDataValidator{},
&MsgUpdateDataValidator{},
&MsgCreatePool{},
&MsgSellData{},
&MsgBuyDataAccessNFT{},
Expand Down
42 changes: 42 additions & 0 deletions x/datapool/types/message_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,48 @@ func (msg *MsgRegisterDataValidator) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{dataValidator}
}

var _ sdk.Msg = &MsgUpdateDataValidator{}

func NewMsgUpdateDataValidator(address, endpoint string) *MsgUpdateDataValidator {
return &MsgUpdateDataValidator{
DataValidator: address,
Endpoint: endpoint,
}
}

func (msg *MsgUpdateDataValidator) Route() string {
return RouterKey
}

func (msg *MsgUpdateDataValidator) Type() string {
return "UpdateDataValidator"
}

func (msg *MsgUpdateDataValidator) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.DataValidator)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid data validator address (%s)", err)
}

if msg.Endpoint == "" {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "empty data validator endpoint URL")
}
return nil
}

func (msg *MsgUpdateDataValidator) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}

func (msg *MsgUpdateDataValidator) GetSigners() []sdk.AccAddress {
dataValidator, err := sdk.AccAddressFromBech32(msg.DataValidator)
if err != nil {
panic(err)
}
return []sdk.AccAddress{dataValidator}
}

var _ sdk.Msg = &MsgCreatePool{}

func NewMsgCreatePool(poolParams *PoolParams, curator string) *MsgCreatePool {
Expand Down
Loading

0 comments on commit d5af49b

Please sign in to comment.