From 7d552c16829fda1fde04aa8ceddb25c13291647b Mon Sep 17 00:00:00 2001 From: kevin-yuhh Date: Thu, 10 Aug 2023 14:36:20 +0800 Subject: [PATCH 1/3] fix revoke-certificate, platform cmd --- x/cert/client/cli/query.go | 2 +- x/cert/client/cli/tx.go | 12 ++++++++++-- x/cert/keeper/keeper_test.go | 17 +++++++++++++---- x/cert/keeper/msg_server.go | 2 +- x/cert/types/certificate.go | 10 ++++++++++ 5 files changed, 35 insertions(+), 8 deletions(-) diff --git a/x/cert/client/cli/query.go b/x/cert/client/cli/query.go index 3a3fb2f19..1c9c33c91 100644 --- a/x/cert/client/cli/query.go +++ b/x/cert/client/cli/query.go @@ -181,7 +181,7 @@ func GetCmdPlatform() *cobra.Command { queryClient := types.NewQueryClient(cliCtx) var pk cryptotypes.PubKey - err = cliCtx.Codec.UnmarshalJSON([]byte(args[0]), pk) + err = cliCtx.Codec.UnmarshalInterfaceJSON([]byte(args[0]), &pk) if err != nil { return err } diff --git a/x/cert/client/cli/tx.go b/x/cert/client/cli/tx.go index 4243be77b..dccceb074 100644 --- a/x/cert/client/cli/tx.go +++ b/x/cert/client/cli/tx.go @@ -107,7 +107,15 @@ func GetCmdCertifyPlatform() *cobra.Command { cmd := &cobra.Command{ Use: "certify-platform ", Short: "Certify a validator's host platform", - Args: cobra.ExactArgs(2), + Long: strings.TrimSpace( + fmt.Sprintf(`Certify a validator's host platform +Example: +$ %s tx cert certify-platform '{\"@type\":\"/cosmos.crypto.secp256k1.PubKey\",\"key\":\"A/N4uA+0c7xg1nOI8eGSB5tYIiQXbLfAZif0/MnwdeJP\"}' test --from= +`, + version.AppName, + ), + ), + Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { cliCtx, err := client.GetClientTxContext(cmd) if err != nil { @@ -121,7 +129,7 @@ func GetCmdCertifyPlatform() *cobra.Command { } var validator cryptotypes.PubKey - err = cliCtx.Codec.UnmarshalJSON([]byte(args[0]), validator) + err = cliCtx.Codec.UnmarshalInterfaceJSON([]byte(args[0]), &validator) if err != nil { return err } diff --git a/x/cert/keeper/keeper_test.go b/x/cert/keeper/keeper_test.go index b2669f286..20e259ccb 100644 --- a/x/cert/keeper/keeper_test.go +++ b/x/cert/keeper/keeper_test.go @@ -185,10 +185,10 @@ func (suite *KeeperTestSuite) TestCertificate_Delete() { args{ cert: []cert{ { - certTypeStr: "compilation", - contStr: "sourcodehash0", - compiler: "compiler1", - bytecodeHash: "bytecodehash1", + certTypeStr: "identity", + contStr: "shentu13kq6ju9ftdas6lngqc9ehl8nfh89jp5k997563", + compiler: "", + bytecodeHash: "", description: "", certifier: suite.address[0], delete: true, @@ -212,6 +212,15 @@ func (suite *KeeperTestSuite) TestCertificate_Delete() { certifier: suite.address[0], delete: true, }, + { + certTypeStr: "compilation", + contStr: "sourcodehash0", + compiler: "compiler1", + bytecodeHash: "bytecodehash1", + description: "", + certifier: suite.address[0], + delete: true, + }, { certTypeStr: "compilation", contStr: "sourcodehash0", diff --git a/x/cert/keeper/msg_server.go b/x/cert/keeper/msg_server.go index 265513104..eb94706ee 100644 --- a/x/cert/keeper/msg_server.go +++ b/x/cert/keeper/msg_server.go @@ -75,7 +75,7 @@ func (k msgServer) RevokeCertificate(goCtx context.Context, msg *types.MsgRevoke revokeEvent := sdk.NewEvent( types.EventTypeRevokeCertificate, sdk.NewAttribute("revoker", msg.Revoker), - sdk.NewAttribute("revoked_certificate", certificate.String()), + sdk.NewAttribute("revoked_certificate", certificate.ToString()), sdk.NewAttribute("revoke_description", msg.Description), ) ctx.EventManager().EmitEvent(revokeEvent) diff --git a/x/cert/types/certificate.go b/x/cert/types/certificate.go index 3e70cef34..f093f3470 100644 --- a/x/cert/types/certificate.go +++ b/x/cert/types/certificate.go @@ -150,6 +150,16 @@ func (c Certificate) GetCertifier() sdk.AccAddress { return certifierAddr } +func (c Certificate) ToString() string { + certStr := fmt.Sprintf("certificate_id:%d content:%s description:%s certifier:%s compilation_content:", + c.CertificateId, c.GetContentString(), c.Description, c.Certifier) + if c.CompilationContent != nil { + return certStr + "<" + c.CompilationContent.String() + ">" + } else { + return certStr + "<>" + } +} + // NewKVPair returns a new key-value pair. func NewKVPair(key string, value string) KVPair { return KVPair{Key: key, Value: value} From c40282c22a917045287af2ba91fd0498e3d61cf7 Mon Sep 17 00:00:00 2001 From: kevin-yuhh Date: Thu, 10 Aug 2023 16:30:18 +0800 Subject: [PATCH 2/3] fix revoke-certificate, platform cmd --- x/cert/keeper_test.go | 2 ++ x/cert/types/certificate.go | 3 +-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/x/cert/keeper_test.go b/x/cert/keeper_test.go index b057ee97b..30092d1f3 100644 --- a/x/cert/keeper_test.go +++ b/x/cert/keeper_test.go @@ -45,6 +45,8 @@ func Test_GetNewCertificateID(t *testing.T) { c2, err := types.NewCertificate("compilation", "sourcodehash0", "compiler1", "bytecodehash1", "", addrs[0]) require.NoError(t, err) + require.NotEqual(t, c2.ToString(), "") + id2 := app.CertKeeper.GetNextCertificateID(ctx) c2.CertificateId = id2 app.CertKeeper.SetNextCertificateID(ctx, id2+1) diff --git a/x/cert/types/certificate.go b/x/cert/types/certificate.go index f093f3470..d09634ae6 100644 --- a/x/cert/types/certificate.go +++ b/x/cert/types/certificate.go @@ -155,9 +155,8 @@ func (c Certificate) ToString() string { c.CertificateId, c.GetContentString(), c.Description, c.Certifier) if c.CompilationContent != nil { return certStr + "<" + c.CompilationContent.String() + ">" - } else { - return certStr + "<>" } + return certStr + "<>" } // NewKVPair returns a new key-value pair. From 573dddcd8355d77dbeff0153be10e412a5f9d52a Mon Sep 17 00:00:00 2001 From: kevin-yuhh Date: Thu, 10 Aug 2023 19:31:17 +0800 Subject: [PATCH 3/3] fix revoke-certificate, platform cmd --- x/cert/keeper/msg_server_test.go | 44 ++++++++++++++++++++++++++++++++ x/cert/keeper_test.go | 1 - 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 x/cert/keeper/msg_server_test.go diff --git a/x/cert/keeper/msg_server_test.go b/x/cert/keeper/msg_server_test.go new file mode 100644 index 000000000..fd4ffeeae --- /dev/null +++ b/x/cert/keeper/msg_server_test.go @@ -0,0 +1,44 @@ +package keeper_test + +import ( + "github.com/stretchr/testify/require" + "testing" + "time" + + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + + sdk "github.com/cosmos/cosmos-sdk/types" + + shentuapp "github.com/shentufoundation/shentu/v2/app" + "github.com/shentufoundation/shentu/v2/x/cert/keeper" + "github.com/shentufoundation/shentu/v2/x/cert/types" +) + +func TestMsgServer_Certificate(t *testing.T) { + ctx, certKeeper, msgServer, addrs := DoInit(t) + + content := types.AssembleContent("identity", addrs[1].String()) + _, err := msgServer.IssueCertificate(sdk.WrapSDKContext(ctx), types.NewMsgIssueCertificate(content, "", "", "", addrs[0])) + require.NoError(t, err) + + certificateID := uint64(1) + _, err = msgServer.RevokeCertificate(sdk.WrapSDKContext(ctx), types.NewMsgRevokeCertificate(addrs[0], certificateID, "")) + require.NoError(t, err) + + _, err = certKeeper.GetCertificateByID(ctx, certificateID) + require.Error(t, err) +} + +func DoInit(t *testing.T) (sdk.Context, keeper.Keeper, types.MsgServer, []sdk.AccAddress) { + app := shentuapp.Setup(false) + ctx := app.BaseApp.NewContext(false, tmproto.Header{Time: time.Now().UTC()}) + addrs := shentuapp.AddTestAddrs(app, ctx, 5, sdk.NewInt(80000*1e6)) + ok := app.CertKeeper + msgServer := keeper.NewMsgServerImpl(ok) + ctx = ctx.WithValue("msgServer", msgServer).WithValue("t", t).WithValue("ok", ok) + + ok.SetCertifier(ctx, types.Certifier{ + Address: addrs[0].String(), + }) + return ctx, ok, msgServer, addrs +} diff --git a/x/cert/keeper_test.go b/x/cert/keeper_test.go index 30092d1f3..4b755cc95 100644 --- a/x/cert/keeper_test.go +++ b/x/cert/keeper_test.go @@ -45,7 +45,6 @@ func Test_GetNewCertificateID(t *testing.T) { c2, err := types.NewCertificate("compilation", "sourcodehash0", "compiler1", "bytecodehash1", "", addrs[0]) require.NoError(t, err) - require.NotEqual(t, c2.ToString(), "") id2 := app.CertKeeper.GetNextCertificateID(ctx) c2.CertificateId = id2