Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shentu prefix revoke certificate #754

Merged
merged 3 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a format error in the previous pb string?

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
Loading