Skip to content

Commit

Permalink
Shentu prefix revoke certificate (#754)
Browse files Browse the repository at this point in the history
* fix revoke-certificate, platform cmd

* fix revoke-certificate, platform cmd

* fix revoke-certificate, platform cmd
  • Loading branch information
kevin-yuhh committed Aug 11, 2023
1 parent d65c976 commit e180d1e
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 8 deletions.
2 changes: 1 addition & 1 deletion x/cert/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 10 additions & 2 deletions x/cert/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,15 @@ func GetCmdCertifyPlatform() *cobra.Command {
cmd := &cobra.Command{
Use: "certify-platform <validator pubkey> <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=<key_or_address>
`,
version.AppName,
),
),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientTxContext(cmd)
if err != nil {
Expand All @@ -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
}
Expand Down
17 changes: 13 additions & 4 deletions x/cert/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion x/cert/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
44 changes: 44 additions & 0 deletions x/cert/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions x/cert/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func Test_GetNewCertificateID(t *testing.T) {
c2, err := types.NewCertificate("compilation", "sourcodehash0", "compiler1",
"bytecodehash1", "", addrs[0])
require.NoError(t, err)

id2 := app.CertKeeper.GetNextCertificateID(ctx)
c2.CertificateId = id2
app.CertKeeper.SetNextCertificateID(ctx, id2+1)
Expand Down
9 changes: 9 additions & 0 deletions x/cert/types/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ 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() + ">"
}
return certStr + "<>"
}

// NewKVPair returns a new key-value pair.
func NewKVPair(key string, value string) KVPair {
return KVPair{Key: key, Value: value}
Expand Down

0 comments on commit e180d1e

Please sign in to comment.