From 839e993e024ebb3231d576921dc27463e51e529a Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Sat, 2 Mar 2024 16:25:16 +0530 Subject: [PATCH] Add tests for registry module gRPC requests --- .../data/examples/example1.yml | 0 .../data/examples/general_record_example.yml | 0 .../examples/service_provider_example.yml | 0 .../examples/website_registration_example.yml | 0 tests/e2e/auction/grpc.go | 24 +- tests/e2e/bond/grpc.go | 10 +- tests/e2e/registry/grpc.go | 324 ++++++++++++++++++ tests/e2e/registry/suite.go | 102 ++++++ tests/e2e/registry/tx.go | 70 ++++ .../registry/keeper/query_server_test.go | 14 +- x/registry/client/cli/query.go | 58 ++++ x/registry/client/cli/tx.go | 63 ++++ x/registry/msgs.go | 35 ++ 13 files changed, 676 insertions(+), 24 deletions(-) rename tests/{integration => }/data/examples/example1.yml (100%) rename tests/{integration => }/data/examples/general_record_example.yml (100%) rename tests/{integration => }/data/examples/service_provider_example.yml (100%) rename tests/{integration => }/data/examples/website_registration_example.yml (100%) create mode 100644 tests/e2e/registry/tx.go create mode 100644 x/registry/client/cli/query.go diff --git a/tests/integration/data/examples/example1.yml b/tests/data/examples/example1.yml similarity index 100% rename from tests/integration/data/examples/example1.yml rename to tests/data/examples/example1.yml diff --git a/tests/integration/data/examples/general_record_example.yml b/tests/data/examples/general_record_example.yml similarity index 100% rename from tests/integration/data/examples/general_record_example.yml rename to tests/data/examples/general_record_example.yml diff --git a/tests/integration/data/examples/service_provider_example.yml b/tests/data/examples/service_provider_example.yml similarity index 100% rename from tests/integration/data/examples/service_provider_example.yml rename to tests/data/examples/service_provider_example.yml diff --git a/tests/integration/data/examples/website_registration_example.yml b/tests/data/examples/website_registration_example.yml similarity index 100% rename from tests/integration/data/examples/website_registration_example.yml rename to tests/data/examples/website_registration_example.yml diff --git a/tests/e2e/auction/grpc.go b/tests/e2e/auction/grpc.go index ad3cabc2..c285fd47 100644 --- a/tests/e2e/auction/grpc.go +++ b/tests/e2e/auction/grpc.go @@ -9,7 +9,7 @@ import ( ) const ( - randomAuctionID = "randomAuctionID" + randomAuctionId = "randomAuctionId" randomBidderAddress = "randomBidderAddress" randomOwnerAddress = "randomOwnerAddress" ) @@ -44,7 +44,7 @@ func (ets *E2ETestSuite) TestGetAllAuctionsGrpc() { }{ { "invalid request to get all auctions", - reqURL + randomAuctionID, + reqURL + randomAuctionId, "", true, }, @@ -85,7 +85,7 @@ func (ets *E2ETestSuite) TestGetAuctionGrpc() { }{ { "invalid request to get an auction", - reqURL + randomAuctionID, + reqURL + randomAuctionId, "", true, func() string { return "" }, @@ -100,8 +100,8 @@ func (ets *E2ETestSuite) TestGetAuctionGrpc() { } for _, tc := range testCases { ets.Run(tc.msg, func() { - auctionID := tc.preRun() - resp, err := testutil.GetRequest(tc.url + auctionID) + auctionId := tc.preRun() + resp, err := testutil.GetRequest(tc.url + auctionId) if tc.isErrorExpected { sr.Contains(string(resp), tc.errorMsg) } else { @@ -109,7 +109,7 @@ func (ets *E2ETestSuite) TestGetAuctionGrpc() { var auction auctiontypes.QueryAuctionResponse err = val.ClientCtx.Codec.UnmarshalJSON(resp, &auction) sr.NoError(err) - sr.Equal(auctionID, auction.Auction.Id) + sr.Equal(auctionId, auction.Auction.Id) } }) } @@ -144,8 +144,8 @@ func (ets *E2ETestSuite) TestGetBidsGrpc() { for _, tc := range testCases { ets.Run(tc.msg, func() { - auctionID := tc.preRun() - tc.url += auctionID + auctionId := tc.preRun() + tc.url += auctionId resp, err := testutil.GetRequest(tc.url) if tc.isErrorExpected { sr.Contains(string(resp), tc.errorMsg) @@ -154,7 +154,7 @@ func (ets *E2ETestSuite) TestGetBidsGrpc() { var bids auctiontypes.QueryBidsResponse err = val.ClientCtx.Codec.UnmarshalJSON(resp, &bids) sr.NoError(err) - sr.Equal(auctionID, bids.Bids[0].AuctionId) + sr.Equal(auctionId, bids.Bids[0].AuctionId) } }) } @@ -176,7 +176,7 @@ func (ets *E2ETestSuite) TestGetBidGrpc() { reqURL, "", true, - func() string { return randomAuctionID }, + func() string { return randomAuctionId }, }, { "valid request to get bid", @@ -188,8 +188,8 @@ func (ets *E2ETestSuite) TestGetBidGrpc() { } for _, tc := range testCases { ets.Run(tc.msg, func() { - auctionID := tc.preRun() - tc.url += auctionID + "/" + bidderAddress + auctionId := tc.preRun() + tc.url += auctionId + "/" + bidderAddress resp, err := testutil.GetRequest(tc.url) if tc.isErrorExpected { diff --git a/tests/e2e/bond/grpc.go b/tests/e2e/bond/grpc.go index 48bec960..b326a51d 100644 --- a/tests/e2e/bond/grpc.go +++ b/tests/e2e/bond/grpc.go @@ -113,7 +113,7 @@ func (ets *E2ETestSuite) TestGRPCGetBondsByOwner() { } } -func (ets *E2ETestSuite) TestGRPCGetBondByID() { +func (ets *E2ETestSuite) TestGRPCGetBondById() { val := ets.network.Validators[0] sr := ets.Require() reqURL := val.APIAddress + "/cerc/bond/v1/bonds/%s" @@ -139,10 +139,10 @@ func (ets *E2ETestSuite) TestGRPCGetBondByID() { } for _, tc := range testCases { ets.Run(tc.name, func() { - var bondID string + var bondId string if !tc.expErr { - bondID = tc.preRun() - tc.url = fmt.Sprintf(reqURL, bondID) + bondId = tc.preRun() + tc.url = fmt.Sprintf(reqURL, bondId) } resp, err := testutil.GetRequest(tc.url) @@ -156,7 +156,7 @@ func (ets *E2ETestSuite) TestGRPCGetBondByID() { } else { sr.NoError(err) sr.NotZero(bonds.GetBond().GetId()) - sr.Equal(bonds.GetBond().GetId(), bondID) + sr.Equal(bonds.GetBond().GetId(), bondId) } }) } diff --git a/tests/e2e/registry/grpc.go b/tests/e2e/registry/grpc.go index c8f05f29..f050b05a 100644 --- a/tests/e2e/registry/grpc.go +++ b/tests/e2e/registry/grpc.go @@ -1,11 +1,15 @@ package registry import ( + "encoding/json" "fmt" + "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/testutil" + clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" registrytypes "git.vdb.to/cerc-io/laconic2d/x/registry" + "git.vdb.to/cerc-io/laconic2d/x/registry/client/cli" ) const badPath = "/asdasd" @@ -103,3 +107,323 @@ func (ets *E2ETestSuite) TestGRPCQueryWhoIs() { }) } } + +func (ets *E2ETestSuite) TestGRPCQueryLookup() { + val := ets.network.Validators[0] + sr := ets.Require() + reqURL := val.APIAddress + "/cerc/registry/v1/lookup" + authorityName := "QueryLookUp" + + testCases := []struct { + name string + url string + expectErr bool + errorMsg string + preRun func(authorityName string) + }{ + { + "invalid url", + reqURL + badPath, + true, + "", + func(authorityName string) { + }, + }, + { + "valid request", + fmt.Sprintf(reqURL+"?lrn=lrn://%s/", authorityName), + false, + "", + func(authorityName string) { + // create name record + ets.createNameRecord(authorityName) + }, + }, + } + + for _, tc := range testCases { + ets.Run(tc.name, func() { + tc.preRun(authorityName) + resp, err := testutil.GetRequest(tc.url) + ets.NoError(err) + if tc.expectErr { + sr.Contains(string(resp), tc.errorMsg) + } else { + var response registrytypes.QueryLookupLrnResponse + err := val.ClientCtx.Codec.UnmarshalJSON(resp, &response) + sr.NoError(err) + sr.NotZero(len(response.Name.Latest.Id)) + } + }) + } +} + +func (ets *E2ETestSuite) TestGRPCQueryListRecords() { + val := ets.network.Validators[0] + sr := ets.Require() + reqUrl := val.APIAddress + "/cerc/registry/v1/records" + + testCases := []struct { + name string + url string + expectErr bool + errorMsg string + preRun func(bondId string) + }{ + { + "invalid url", + reqUrl + badPath, + true, + "", + func(bondId string) { + }, + }, + { + "valid request", + reqUrl, + false, + "", + func(bondId string) { ets.createRecord(bondId) }, + }, + } + + for _, tc := range testCases { + ets.Run(tc.name, func() { + tc.preRun(ets.bondId) + resp, err := testutil.GetRequest(tc.url) + ets.NoError(err) + require := ets.Require() + if tc.expectErr { + require.Contains(string(resp), tc.errorMsg) + } else { + var response registrytypes.QueryRecordsResponse + err := val.ClientCtx.Codec.UnmarshalJSON(resp, &response) + sr.NoError(err) + sr.NotZero(len(response.GetRecords())) + sr.Equal(ets.bondId, response.GetRecords()[0].GetBondId()) + } + }) + } +} + +func (ets *E2ETestSuite) TestGRPCQueryGetRecordById() { + val := ets.network.Validators[0] + sr := ets.Require() + reqURL := val.APIAddress + "/cerc/registry/v1/records/%s" + + testCases := []struct { + name string + url string + expectErr bool + errorMsg string + preRun func(bondId string) string + }{ + { + "invalid url", + reqURL + badPath, + true, + "", + func(bondId string) string { + return "" + }, + }, + { + "valid request", + reqURL, + false, + "", + func(bondId string) string { + // creating the record + ets.createRecord(bondId) + + // list the records + clientCtx := val.ClientCtx + cmd := cli.GetCmdList() + args := []string{ + fmt.Sprintf("--%s=json", flags.FlagOutput), + } + out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args) + sr.NoError(err) + var records []registrytypes.ReadableRecord + err = json.Unmarshal(out.Bytes(), &records) + sr.NoError(err) + return records[0].Id + }, + }, + } + + for _, tc := range testCases { + ets.Run(tc.name, func() { + recordId := tc.preRun(ets.bondId) + tc.url = fmt.Sprintf(reqURL, recordId) + + resp, err := testutil.GetRequest(tc.url) + ets.NoError(err) + require := ets.Require() + if tc.expectErr { + require.Contains(string(resp), tc.errorMsg) + } else { + var response registrytypes.QueryRecordByIdResponse + err := val.ClientCtx.Codec.UnmarshalJSON(resp, &response) + sr.NoError(err) + record := response.GetRecord() + sr.NotZero(len(record.GetId())) + sr.Equal(record.GetId(), recordId) + } + }) + } +} + +func (ets *E2ETestSuite) TestGRPCQueryGetRecordByBondId() { + val := ets.network.Validators[0] + sr := ets.Require() + reqURL := val.APIAddress + "/cerc/registry/v1/records-by-bond-id/%s" + + testCases := []struct { + name string + url string + expectErr bool + errorMsg string + preRun func(bondId string) + }{ + { + "invalid url", + reqURL + badPath, + true, + "", + func(bondId string) { + }, + }, + { + "valid request", + reqURL, + false, + "", + func(bondId string) { + // creating the record + ets.createRecord(bondId) + }, + }, + } + + for _, tc := range testCases { + ets.Run(tc.name, func() { + tc.preRun(ets.bondId) + tc.url = fmt.Sprintf(reqURL, ets.bondId) + + resp, err := testutil.GetRequest(tc.url) + ets.NoError(err) + require := ets.Require() + if tc.expectErr { + require.Contains(string(resp), tc.errorMsg) + } else { + var response registrytypes.QueryRecordsByBondIdResponse + err := val.ClientCtx.Codec.UnmarshalJSON(resp, &response) + sr.NoError(err) + records := response.GetRecords() + sr.NotZero(len(records)) + sr.Equal(records[0].GetBondId(), ets.bondId) + } + }) + } +} + +func (ets *E2ETestSuite) TestGRPCQueryGetRegistryModuleBalance() { + val := ets.network.Validators[0] + sr := ets.Require() + reqURL := val.APIAddress + "/cerc/registry/v1/balance" + + testCases := []struct { + name string + url string + expectErr bool + errorMsg string + preRun func(bondId string) + }{ + { + "invalid url", + reqURL + badPath, + true, + "", + func(bondId string) { + }, + }, + { + "Success", + reqURL, + false, + "", + func(bondId string) { + // creating the record + ets.createRecord(bondId) + }, + }, + } + + for _, tc := range testCases { + ets.Run(tc.name, func() { + tc.preRun(ets.bondId) + resp, err := testutil.GetRequest(tc.url) + ets.NoError(err) + require := ets.Require() + if tc.expectErr { + require.Contains(string(resp), tc.errorMsg) + } else { + var response registrytypes.QueryGetRegistryModuleBalanceResponse + err := val.ClientCtx.Codec.UnmarshalJSON(resp, &response) + sr.NoError(err) + sr.NotZero(len(response.GetBalances())) + } + }) + } +} + +func (ets *E2ETestSuite) TestGRPCQueryNamesList() { + val := ets.network.Validators[0] + sr := ets.Require() + reqURL := val.APIAddress + "/cerc/registry/v1/names" + + testCases := []struct { + name string + url string + expectErr bool + errorMsg string + preRun func(authorityName string) + }{ + { + "invalid url", + reqURL + badPath, + true, + "", + func(authorityName string) { + }, + }, + { + "valid request", + reqURL, + false, + "", + func(authorityName string) { + // create name record + ets.createNameRecord(authorityName) + }, + }, + } + + for _, tc := range testCases { + ets.Run(tc.name, func() { + tc.preRun("ListNameRecords") + resp, err := testutil.GetRequest(tc.url) + ets.NoError(err) + require := ets.Require() + if tc.expectErr { + require.Contains(string(resp), tc.errorMsg) + } else { + var response registrytypes.QueryNameRecordsResponse + err := val.ClientCtx.Codec.UnmarshalJSON(resp, &response) + sr.NoError(err) + sr.NotZero(len(response.GetNames())) + } + }) + } +} diff --git a/tests/e2e/registry/suite.go b/tests/e2e/registry/suite.go index a15652ab..058a5ff0 100644 --- a/tests/e2e/registry/suite.go +++ b/tests/e2e/registry/suite.go @@ -2,6 +2,7 @@ package registry import ( "fmt" + "path/filepath" "time" "cosmossdk.io/math" @@ -167,6 +168,107 @@ func (ets *E2ETestSuite) reserveName(authorityName string) { sr.NoError(err) } +func (ets *E2ETestSuite) createNameRecord(authorityName string) { + val := ets.network.Validators[0] + sr := ets.Require() + + // reserving the name + clientCtx := val.ClientCtx + cmd := cli.GetCmdReserveName() + args := []string{ + authorityName, + fmt.Sprintf("--owner=%s", ets.accountAddress), + fmt.Sprintf("--%s=%s", flags.FlagFrom, ets.accountName), + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=json", flags.FlagOutput), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), + fmt.Sprintf("--%s=%s", flags.FlagFees, fmt.Sprintf("3%s", ets.cfg.BondDenom)), + } + out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args) + sr.NoError(err) + var d sdk.TxResponse + err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &d) + sr.NoError(err) + sr.Zero(d.Code) + + err = ets.network.WaitForNextBlock() + sr.NoError(err) + + // Get the bond-id + bondId := ets.bondId + + // adding bond-id to name authority + args = []string{ + authorityName, bondId, + fmt.Sprintf("--%s=%s", flags.FlagFrom, ets.accountName), + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=json", flags.FlagOutput), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), + fmt.Sprintf("--%s=%s", flags.FlagFees, fmt.Sprintf("3%s", ets.cfg.BondDenom)), + } + cmd = cli.GetCmdSetAuthorityBond() + + out, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, args) + sr.NoError(err) + err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &d) + sr.NoError(err) + sr.Zero(d.Code) + + err = ets.network.WaitForNextBlock() + sr.NoError(err) + + args = []string{ + fmt.Sprintf("lrn://%s/", authorityName), + "test_hello_cid", + fmt.Sprintf("--%s=%s", flags.FlagFrom, ets.accountName), + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=json", flags.FlagOutput), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), + fmt.Sprintf("--%s=%s", flags.FlagFees, fmt.Sprintf("3%s", ets.cfg.BondDenom)), + } + + cmd = cli.GetCmdSetName() + + out, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, args) + sr.NoError(err) + err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &d) + sr.NoError(err) + sr.Zero(d.Code) + + err = ets.network.WaitForNextBlock() + sr.NoError(err) +} + +func (ets *E2ETestSuite) createRecord(bondId string) { + val := ets.network.Validators[0] + sr := ets.Require() + + payloadPath := "../../data/examples/service_provider_example.yml" + payloadFilePath, err := filepath.Abs(payloadPath) + sr.NoError(err) + + args := []string{ + fmt.Sprintf("--%s=%s", flags.FlagFrom, ets.accountName), + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=json", flags.FlagOutput), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), + fmt.Sprintf("--%s=%s", flags.FlagFees, fmt.Sprintf("3%s", ets.cfg.BondDenom)), + } + args = append([]string{payloadFilePath, bondId}, args...) + clientCtx := val.ClientCtx + cmd := cli.GetCmdSetRecord() + + out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args) + sr.NoError(err) + var d sdk.TxResponse + err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &d) + sr.NoError(err) + sr.Zero(d.Code, d.RawLog) + + err = ets.network.WaitForNextBlock() + sr.NoError(err) +} + func (ets *E2ETestSuite) updateParams(params *registrytypes.Params) { params.RecordRent = sdk.NewCoin(ets.cfg.BondDenom, math.NewInt(1000)) params.RecordRentDuration = 10 * time.Second diff --git a/tests/e2e/registry/tx.go b/tests/e2e/registry/tx.go new file mode 100644 index 00000000..1267728a --- /dev/null +++ b/tests/e2e/registry/tx.go @@ -0,0 +1,70 @@ +package registry + +import ( + "fmt" + "path/filepath" + + "github.com/cosmos/cosmos-sdk/client/flags" + clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" + sdk "github.com/cosmos/cosmos-sdk/types" + + "git.vdb.to/cerc-io/laconic2d/x/registry/client/cli" +) + +func (ets *E2ETestSuite) TestGetCmdSetRecord() { + val := ets.network.Validators[0] + sr := ets.Require() + + bondId := ets.bondId + payloadPath := "../../data/examples/service_provider_example.yml" + payloadFilePath, err := filepath.Abs(payloadPath) + sr.NoError(err) + + testCases := []struct { + name string + args []string + err bool + }{ + { + "invalid request without bond id/without payload", + []string{ + fmt.Sprintf("--%s=%s", flags.FlagFrom, ets.accountName), + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=json", flags.FlagOutput), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), + fmt.Sprintf("--%s=%s", flags.FlagFees, fmt.Sprintf("3%s", ets.cfg.BondDenom)), + }, + true, + }, + { + "success", + []string{ + payloadFilePath, bondId, + fmt.Sprintf("--%s=%s", flags.FlagFrom, ets.accountName), + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=json", flags.FlagOutput), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), + fmt.Sprintf("--%s=%s", flags.FlagFees, fmt.Sprintf("3%s", ets.cfg.BondDenom)), + }, + false, + }, + } + + for _, tc := range testCases { + ets.Run(fmt.Sprintf("Case %s", tc.name), func() { + clientCtx := val.ClientCtx + cmd := cli.GetCmdSetRecord() + + out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) + if tc.err { + sr.Error(err) + } else { + sr.NoError(err) + var d sdk.TxResponse + err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &d) + sr.NoError(err) + sr.Zero(d.Code) + } + }) + } +} diff --git a/tests/integration/registry/keeper/query_server_test.go b/tests/integration/registry/keeper/query_server_test.go index 0c527dd8..ee4294e3 100644 --- a/tests/integration/registry/keeper/query_server_test.go +++ b/tests/integration/registry/keeper/query_server_test.go @@ -37,9 +37,9 @@ func (kts *KeeperTestSuite) TestGrpcGetRecordLists() { var recordId string examples := []string{ - "../../data/examples/service_provider_example.yml", - "../../data/examples/website_registration_example.yml", - "../../data/examples/general_record_example.yml", + "../../../data/examples/service_provider_example.yml", + "../../../data/examples/website_registration_example.yml", + "../../../data/examples/general_record_example.yml", } testCases := []struct { msg string @@ -268,7 +268,7 @@ func (kts *KeeperTestSuite) TestGrpcGetRecordLists() { } // Get the records by record id - testCasesByBondID := []struct { + testCasesByBondId := []struct { msg string req *types.QueryRecordsByBondIdRequest createRecord bool @@ -292,7 +292,7 @@ func (kts *KeeperTestSuite) TestGrpcGetRecordLists() { 1, }, } - for _, test := range testCasesByBondID { + for _, test := range testCasesByBondId { kts.Run(fmt.Sprintf("Case %s ", test.msg), func() { resp, err := queryClient.GetRecordsByBondId(context.Background(), test.req) @@ -314,8 +314,8 @@ func (kts *KeeperTestSuite) TestGrpcQueryRegistryModuleBalance() { queryClient, ctx := kts.queryClient, kts.SdkCtx sr := kts.Require() examples := []string{ - "../../data/examples/service_provider_example.yml", - "../../data/examples/website_registration_example.yml", + "../../../data/examples/service_provider_example.yml", + "../../../data/examples/website_registration_example.yml", } testCases := []struct { msg string diff --git a/x/registry/client/cli/query.go b/x/registry/client/cli/query.go new file mode 100644 index 00000000..c13564a2 --- /dev/null +++ b/x/registry/client/cli/query.go @@ -0,0 +1,58 @@ +package cli + +import ( + "encoding/json" + "fmt" + "strings" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/version" + "github.com/spf13/cobra" + + registrytypes "git.vdb.to/cerc-io/laconic2d/x/registry" +) + +// GetCmdList queries all records. +func GetCmdList() *cobra.Command { + cmd := &cobra.Command{ + Use: "list", + Short: "List records.", + Long: strings.TrimSpace( + fmt.Sprintf(`Get the records. +Example: +$ %s query %s list +`, + version.AppName, registrytypes.ModuleName, + ), + ), + Args: cobra.ExactArgs(0), + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx, err := client.GetClientQueryContext(cmd) + if err != nil { + return err + } + + queryClient := registrytypes.NewQueryClient(clientCtx) + res, err := queryClient.Records(cmd.Context(), ®istrytypes.QueryRecordsRequest{}) + if err != nil { + return err + } + + recordsList := res.GetRecords() + records := make([]registrytypes.ReadableRecord, len(recordsList)) + for i, record := range res.GetRecords() { + records[i] = record.ToReadableRecord() + } + bytesResult, err := json.Marshal(records) + if err != nil { + return err + } + return clientCtx.PrintBytes(bytesResult) + }, + } + + flags.AddQueryFlagsToCmd(cmd) + + return cmd +} diff --git a/x/registry/client/cli/tx.go b/x/registry/client/cli/tx.go index 366e9ad2..2c645d74 100644 --- a/x/registry/client/cli/tx.go +++ b/x/registry/client/cli/tx.go @@ -129,3 +129,66 @@ $ %s tx %s reserve-name [name] --owner [ownerAddress] return cmd } + +// GetCmdSetAuthorityBond is the CLI command for associating a bond with an authority. +func GetCmdSetAuthorityBond() *cobra.Command { + cmd := &cobra.Command{ + Use: "authority-bond [name] [bond-id]", + Short: "Associate authority with bond.", + Long: strings.TrimSpace( + fmt.Sprintf(`Reserver name with owner address . +Example: +$ %s tx %s authority-bond [name] [bond-id] +`, + version.AppName, registrytypes.ModuleName, + ), + ), + Args: cobra.ExactArgs(2), + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx, err := client.GetClientTxContext(cmd) + if err != nil { + return err + } + msg := registrytypes.NewMsgSetAuthorityBond(args[0], args[1], clientCtx.GetFromAddress()) + return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg) + }, + } + + flags.AddTxFlagsToCmd(cmd) + + return cmd +} + +// GetCmdSetName is the CLI command for mapping a name to a CID. +func GetCmdSetName() *cobra.Command { + cmd := &cobra.Command{ + Use: "set-name [crn] [cid]", + Short: "Set CRN to CID mapping.", + Long: strings.TrimSpace( + fmt.Sprintf(`Set name with crn and cid. +Example: +$ %s tx %s set-name [crn] [cid] +`, + version.AppName, registrytypes.ModuleName, + ), + ), + Args: cobra.ExactArgs(2), + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx, err := client.GetClientTxContext(cmd) + if err != nil { + return err + } + + msg := registrytypes.NewMsgSetName(args[0], args[1], clientCtx.GetFromAddress()) + err = msg.ValidateBasic() + if err != nil { + return err + } + return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) + }, + } + + flags.AddTxFlagsToCmd(cmd) + + return cmd +} diff --git a/x/registry/msgs.go b/x/registry/msgs.go index 882078fb..93f65743 100644 --- a/x/registry/msgs.go +++ b/x/registry/msgs.go @@ -55,3 +55,38 @@ func (msg MsgReserveAuthority) ValidateBasic() error { return nil } + +// NewMsgSetAuthorityBond is the constructor function for MsgSetAuthorityBond. +func NewMsgSetAuthorityBond(name string, bondID string, signer sdk.AccAddress) MsgSetAuthorityBond { + return MsgSetAuthorityBond{ + Name: name, + Signer: signer.String(), + BondId: bondID, + } +} + +// NewMsgSetName is the constructor function for MsgSetName. +func NewMsgSetName(lrn string, cid string, signer sdk.AccAddress) *MsgSetName { + return &MsgSetName{ + Lrn: lrn, + Cid: cid, + Signer: signer.String(), + } +} + +// ValidateBasic Implements Msg. +func (msg MsgSetName) ValidateBasic() error { + if msg.Lrn == "" { + return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "LRN is required.") + } + + if msg.Cid == "" { + return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "CID is required.") + } + + if len(msg.Signer) == 0 { + return errorsmod.Wrap(sdkerrors.ErrInvalidAddress, "invalid signer") + } + + return nil +}