From 8c4b4c5b4d4c315d254ef847cfdb8077b17200d7 Mon Sep 17 00:00:00 2001 From: zemyblue Date: Fri, 22 Sep 2023 14:10:04 +0900 Subject: [PATCH 1/6] chore: add more unittest MsgGrantPermission of x/token --- x/token/keeper/keeper_test.go | 9 +++- x/token/keeper/msg_server_test.go | 84 ++++++++++++++++++++++++++----- 2 files changed, 80 insertions(+), 13 deletions(-) diff --git a/x/token/keeper/keeper_test.go b/x/token/keeper/keeper_test.go index 1cd589fe88..3277239410 100644 --- a/x/token/keeper/keeper_test.go +++ b/x/token/keeper/keeper_test.go @@ -31,7 +31,8 @@ type KeeperTestSuite struct { customer sdk.AccAddress stranger sdk.AccAddress - contractID string + contractID string + unmintableContractId string balance sdk.Int } @@ -122,6 +123,12 @@ func (s *KeeperTestSuite) SetupTest() { notTokenContractID := app.ClassKeeper.NewID(s.ctx) err = keeper.ValidateLegacyContract(s.keeper, s.ctx, notTokenContractID) s.Require().ErrorIs(err, token.ErrTokenNotExist) + + s.unmintableContractId = s.keeper.Issue(s.ctx, token.Contract{ + Name: "Unmintable", + Symbol: "UMT", + Mintable: false, + }, s.vendor, s.vendor, s.balance) } func TestKeeperTestSuite(t *testing.T) { diff --git a/x/token/keeper/msg_server_test.go b/x/token/keeper/msg_server_test.go index 01c967dc0c..7397d8868f 100644 --- a/x/token/keeper/msg_server_test.go +++ b/x/token/keeper/msg_server_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + "fmt" abci "github.com/tendermint/tendermint/abci/types" sdk "github.com/Finschia/finschia-sdk/types" @@ -273,27 +274,75 @@ func (s *KeeperTestSuite) TestMsgGrantPermission() { err error events sdk.Events }{ - "valid request": { - contractID: s.contractID, + "contract not found": { // tc1 + contractID: "fee1dead", granter: s.vendor, grantee: s.operator, permission: token.LegacyPermissionModify.String(), - events: sdk.Events{sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8{0x63, 0x6f, 0x6e, 0x74, 0x72, 0x61, 0x63, 0x74, 0x5f, 0x69, 0x64}, Value: []uint8{0x22, 0x39, 0x62, 0x65, 0x31, 0x37, 0x31, 0x36, 0x35, 0x22}, Index: false}, {Key: []uint8{0x67, 0x72, 0x61, 0x6e, 0x74, 0x65, 0x65}, Value: []uint8{0x22, 0x6c, 0x69, 0x6e, 0x6b, 0x31, 0x76, 0x39, 0x6a, 0x78, 0x67, 0x75, 0x6e, 0x39, 0x77, 0x64, 0x65, 0x6e, 0x7a, 0x77, 0x30, 0x38, 0x70, 0x36, 0x74, 0x22}, Index: false}, {Key: []uint8{0x67, 0x72, 0x61, 0x6e, 0x74, 0x65, 0x72}, Value: []uint8{0x22, 0x6c, 0x69, 0x6e, 0x6b, 0x31, 0x76, 0x39, 0x6a, 0x78, 0x67, 0x75, 0x6e, 0x39, 0x77, 0x64, 0x65, 0x6e, 0x71, 0x61, 0x32, 0x78, 0x7a, 0x66, 0x78, 0x22}, Index: false}, {Key: []uint8{0x70, 0x65, 0x72, 0x6d, 0x69, 0x73, 0x73, 0x69, 0x6f, 0x6e}, Value: []uint8{0x22, 0x50, 0x45, 0x52, 0x4d, 0x49, 0x53, 0x53, 0x49, 0x4f, 0x4e, 0x5f, 0x4d, 0x4f, 0x44, 0x49, 0x46, 0x59, 0x22}, Index: false}}}}, + err: class.ErrContractNotExist, }, - "contract not found": { - contractID: "fee1dead", + "contract has no permission - MINT": { // tc5 + contractID: s.unmintableContractId, granter: s.vendor, grantee: s.operator, - permission: token.LegacyPermissionModify.String(), - err: class.ErrContractNotExist, + permission: token.LegacyPermissionMint.String(), + err: token.ErrTokenNoPermission, }, - "granter has no permission": { + "contract has no permission - BURN": { // tc6 + contractID: s.unmintableContractId, + granter: s.vendor, + grantee: s.operator, + permission: token.LegacyPermissionBurn.String(), + err: token.ErrTokenNoPermission, + }, + "granter has no permission - MINT": { // tc9 contractID: s.contractID, granter: s.customer, - grantee: s.operator, + grantee: s.stranger, + permission: token.LegacyPermissionMint.String(), + err: token.ErrTokenNoPermission, + }, + "granter has no permission - BURN": { // tc10 + contractID: s.contractID, + granter: s.customer, + grantee: s.stranger, + permission: token.LegacyPermissionBurn.String(), + err: token.ErrTokenNoPermission, + }, + "granter has no permission - MODIFY": { // tc11 + contractID: s.contractID, + granter: s.customer, + grantee: s.stranger, permission: token.LegacyPermissionModify.String(), err: token.ErrTokenNoPermission, }, + "valid request - MINT": { // tc12 + contractID: s.contractID, + granter: s.vendor, + grantee: s.operator, + permission: token.LegacyPermissionMint.String(), + events: sdk.Events{ + sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", s.operator.String())), Index: false}, {Key: []uint8("granter"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor.String())), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MINT\""), Index: false}}}, + }, + }, + "valid request - BURN": { // tc13 + contractID: s.contractID, + granter: s.vendor, + grantee: s.operator, + permission: token.LegacyPermissionBurn.String(), + events: sdk.Events{ + sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", s.operator.String())), Index: false}, {Key: []uint8("granter"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor.String())), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_BURN\""), Index: false}}}, + }, + }, + "valid request - MODIFY": { // tc14 + contractID: s.contractID, + granter: s.vendor, + grantee: s.operator, + permission: token.LegacyPermissionModify.String(), + events: sdk.Events{ + sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", s.operator.String())), Index: false}, {Key: []uint8("granter"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor.String())), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MODIFY\""), Index: false}}}, + }, + }, } for name, tc := range testCases { @@ -313,10 +362,21 @@ func (s *KeeperTestSuite) TestMsgGrantPermission() { } s.Require().NotNil(res) + s.Require().Equal(tc.events, ctx.EventManager().Events()) - if s.deterministic { - s.Require().Equal(tc.events, ctx.EventManager().Events()) - } + // check to grant permission + per, err := s.queryServer.GranteeGrants(sdk.WrapSDKContext(ctx), &token.QueryGranteeGrantsRequest{ + ContractId: tc.contractID, + Grantee: tc.grantee.String(), + Pagination: nil, + }) + s.Require().NoError(err) + s.Require().NotNil(per) + expectPermission := token.Grant{ + Grantee: tc.grantee.String(), + Permission: token.Permission(token.LegacyPermissionFromString(tc.permission)), + } + s.Require().Contains(per.Grants, expectPermission) }) } } From 945945dbc0196061008c164f0c16894e38d826aa Mon Sep 17 00:00:00 2001 From: zemyblue Date: Fri, 22 Sep 2023 14:32:45 +0900 Subject: [PATCH 2/6] chore: remove comment for develop --- x/token/keeper/msg_server_test.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/x/token/keeper/msg_server_test.go b/x/token/keeper/msg_server_test.go index 7397d8868f..e9ea5ce4da 100644 --- a/x/token/keeper/msg_server_test.go +++ b/x/token/keeper/msg_server_test.go @@ -274,49 +274,49 @@ func (s *KeeperTestSuite) TestMsgGrantPermission() { err error events sdk.Events }{ - "contract not found": { // tc1 + "contract not found": { contractID: "fee1dead", granter: s.vendor, grantee: s.operator, permission: token.LegacyPermissionModify.String(), err: class.ErrContractNotExist, }, - "contract has no permission - MINT": { // tc5 + "contract has no permission - MINT": { contractID: s.unmintableContractId, granter: s.vendor, grantee: s.operator, permission: token.LegacyPermissionMint.String(), err: token.ErrTokenNoPermission, }, - "contract has no permission - BURN": { // tc6 + "contract has no permission - BURN": { contractID: s.unmintableContractId, granter: s.vendor, grantee: s.operator, permission: token.LegacyPermissionBurn.String(), err: token.ErrTokenNoPermission, }, - "granter has no permission - MINT": { // tc9 + "granter has no permission - MINT": { contractID: s.contractID, granter: s.customer, grantee: s.stranger, permission: token.LegacyPermissionMint.String(), err: token.ErrTokenNoPermission, }, - "granter has no permission - BURN": { // tc10 + "granter has no permission - BURN": { contractID: s.contractID, granter: s.customer, grantee: s.stranger, permission: token.LegacyPermissionBurn.String(), err: token.ErrTokenNoPermission, }, - "granter has no permission - MODIFY": { // tc11 + "granter has no permission - MODIFY": { contractID: s.contractID, granter: s.customer, grantee: s.stranger, permission: token.LegacyPermissionModify.String(), err: token.ErrTokenNoPermission, }, - "valid request - MINT": { // tc12 + "valid request - MINT": { contractID: s.contractID, granter: s.vendor, grantee: s.operator, @@ -325,7 +325,7 @@ func (s *KeeperTestSuite) TestMsgGrantPermission() { sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", s.operator.String())), Index: false}, {Key: []uint8("granter"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor.String())), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MINT\""), Index: false}}}, }, }, - "valid request - BURN": { // tc13 + "valid request - BURN": { contractID: s.contractID, granter: s.vendor, grantee: s.operator, @@ -334,7 +334,7 @@ func (s *KeeperTestSuite) TestMsgGrantPermission() { sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", s.operator.String())), Index: false}, {Key: []uint8("granter"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor.String())), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_BURN\""), Index: false}}}, }, }, - "valid request - MODIFY": { // tc14 + "valid request - MODIFY": { contractID: s.contractID, granter: s.vendor, grantee: s.operator, From 45da11d9212dc46bc40e61b7aadb2bd16344d8ae Mon Sep 17 00:00:00 2001 From: zemyblue Date: Fri, 22 Sep 2023 14:33:05 +0900 Subject: [PATCH 3/6] chore: add more unnittest for MsgRevokePermission of x/token --- x/token/keeper/msg_server_test.go | 67 ++++++++++++++++++++++++++----- 1 file changed, 57 insertions(+), 10 deletions(-) diff --git a/x/token/keeper/msg_server_test.go b/x/token/keeper/msg_server_test.go index e9ea5ce4da..fa59f9e2d4 100644 --- a/x/token/keeper/msg_server_test.go +++ b/x/token/keeper/msg_server_test.go @@ -389,24 +389,60 @@ func (s *KeeperTestSuite) TestMsgRevokePermission() { err error events sdk.Events }{ - "valid request": { - contractID: s.contractID, - from: s.operator, - permission: token.LegacyPermissionMint.String(), - events: sdk.Events{sdk.Event{Type: "lbm.token.v1.EventRenounced", Attributes: []abci.EventAttribute{{Key: []uint8{0x63, 0x6f, 0x6e, 0x74, 0x72, 0x61, 0x63, 0x74, 0x5f, 0x69, 0x64}, Value: []uint8{0x22, 0x39, 0x62, 0x65, 0x31, 0x37, 0x31, 0x36, 0x35, 0x22}, Index: false}, {Key: []uint8{0x67, 0x72, 0x61, 0x6e, 0x74, 0x65, 0x65}, Value: []uint8{0x22, 0x6c, 0x69, 0x6e, 0x6b, 0x31, 0x76, 0x39, 0x6a, 0x78, 0x67, 0x75, 0x6e, 0x39, 0x77, 0x64, 0x65, 0x6e, 0x7a, 0x77, 0x30, 0x38, 0x70, 0x36, 0x74, 0x22}, Index: false}, {Key: []uint8{0x70, 0x65, 0x72, 0x6d, 0x69, 0x73, 0x73, 0x69, 0x6f, 0x6e}, Value: []uint8{0x22, 0x50, 0x45, 0x52, 0x4d, 0x49, 0x53, 0x53, 0x49, 0x4f, 0x4e, 0x5f, 0x4d, 0x49, 0x4e, 0x54, 0x22}, Index: false}}}}, - }, "contract not found": { contractID: "fee1dead", from: s.operator, permission: token.LegacyPermissionMint.String(), err: class.ErrContractNotExist, }, - "not granted yet": { - contractID: s.contractID, + "contract has no permission - MINT": { + contractID: s.unmintableContractId, + from: s.operator, + permission: token.LegacyPermissionMint.String(), + err: token.ErrTokenNoPermission, + }, + "contract has no permission - BURN": { + contractID: s.unmintableContractId, from: s.operator, + permission: token.LegacyPermissionBurn.String(), + err: token.ErrTokenNoPermission, + }, + "grantee has no permission - MINT": { + contractID: s.contractID, + from: s.customer, + permission: token.LegacyPermissionMint.String(), + err: token.ErrTokenNoPermission, + }, + "grantee has no permission - BURN": { + contractID: s.contractID, + from: s.customer, + permission: token.LegacyPermissionBurn.String(), + err: token.ErrTokenNoPermission, + }, + "grantee has no permission - MODIFY": { + contractID: s.contractID, + from: s.customer, permission: token.LegacyPermissionModify.String(), err: token.ErrTokenNoPermission, }, + "valid request - revoke MINT": { + contractID: s.contractID, + from: s.operator, + permission: token.LegacyPermissionMint.String(), + events: sdk.Events{sdk.Event{Type: "lbm.token.v1.EventRenounced", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", s.operator)), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MINT\""), Index: false}}}}, + }, + "valid request - revoke BURN": { + contractID: s.contractID, + from: s.operator, + permission: token.LegacyPermissionBurn.String(), + events: sdk.Events{sdk.Event{Type: "lbm.token.v1.EventRenounced", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", s.operator)), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_BURN\""), Index: false}}}}, + }, + "valid request - revoke MODIFY": { + contractID: s.contractID, + from: s.vendor, + permission: token.LegacyPermissionModify.String(), + events: sdk.Events{sdk.Event{Type: "lbm.token.v1.EventRenounced", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor)), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MODIFY\""), Index: false}}}}, + }, } for name, tc := range testCases { @@ -425,10 +461,21 @@ func (s *KeeperTestSuite) TestMsgRevokePermission() { } s.Require().NotNil(res) + s.Require().Equal(tc.events, ctx.EventManager().Events()) - if s.deterministic { - s.Require().Equal(tc.events, ctx.EventManager().Events()) + // check to remove permission + per, err := s.queryServer.GranteeGrants(sdk.WrapSDKContext(ctx), &token.QueryGranteeGrantsRequest{ + ContractId: tc.contractID, + Grantee: tc.from.String(), + Pagination: nil, + }) + s.Require().NoError(err) + s.Require().NotNil(per) + expectPermission := token.Grant{ + Grantee: tc.from.String(), + Permission: token.Permission(token.LegacyPermissionFromString(tc.permission)), } + s.Require().NotContains(per.Grants, expectPermission) }) } } From 6965e08ad58db82249093efbd0124a2fe7702ae9 Mon Sep 17 00:00:00 2001 From: zemyblue Date: Fri, 22 Sep 2023 19:37:01 +0900 Subject: [PATCH 4/6] fix: unittest errors --- x/token/keeper/msg_server_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/x/token/keeper/msg_server_test.go b/x/token/keeper/msg_server_test.go index 2b6f010802..38e0c93a7e 100644 --- a/x/token/keeper/msg_server_test.go +++ b/x/token/keeper/msg_server_test.go @@ -241,20 +241,20 @@ func (s *KeeperTestSuite) TestMsgIssue() { mintable: true, amount: sdk.NewInt(10), events: sdk.Events{ - sdk.Event{Type: "lbm.token.v1.EventIssued", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"fee15a74\""), Index: false}, {Key: []uint8("creator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, {Key: []uint8("decimals"), Value: []uint8("0"), Index: false}, {Key: []uint8("meta"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("mintable"), Value: []uint8("true"), Index: false}, {Key: []uint8("name"), Value: []uint8("\"test\""), Index: false}, {Key: []uint8("symbol"), Value: []uint8("\"TT\""), Index: false}, {Key: []uint8("uri"), Value: []uint8("\"\""), Index: false}}}, - sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"fee15a74\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}, {Key: []uint8("granter"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MODIFY\""), Index: false}}}, - sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"fee15a74\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}, {Key: []uint8("granter"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MINT\""), Index: false}}}, - sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"fee15a74\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}, {Key: []uint8("granter"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_BURN\""), Index: false}}}, - sdk.Event{Type: "lbm.token.v1.EventMinted", Attributes: []abci.EventAttribute{{Key: []uint8("amount"), Value: []uint8("\"10\""), Index: false}, {Key: []uint8("contract_id"), Value: []uint8("\"fee15a74\""), Index: false}, {Key: []uint8("operator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, {Key: []uint8("to"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}}}, + sdk.Event{Type: "lbm.token.v1.EventIssued", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false}, {Key: []uint8("creator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, {Key: []uint8("decimals"), Value: []uint8("0"), Index: false}, {Key: []uint8("meta"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("mintable"), Value: []uint8("true"), Index: false}, {Key: []uint8("name"), Value: []uint8("\"test\""), Index: false}, {Key: []uint8("symbol"), Value: []uint8("\"TT\""), Index: false}, {Key: []uint8("uri"), Value: []uint8("\"\""), Index: false}}}, + sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}, {Key: []uint8("granter"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MODIFY\""), Index: false}}}, + sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}, {Key: []uint8("granter"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MINT\""), Index: false}}}, + sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}, {Key: []uint8("granter"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_BURN\""), Index: false}}}, + sdk.Event{Type: "lbm.token.v1.EventMinted", Attributes: []abci.EventAttribute{{Key: []uint8("amount"), Value: []uint8("\"10\""), Index: false}, {Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false}, {Key: []uint8("operator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, {Key: []uint8("to"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}}}, }, }, "mintable false": { mintable: false, amount: sdk.NewInt(10), events: sdk.Events{ - sdk.Event{Type: "lbm.token.v1.EventIssued", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"fee15a74\""), Index: false}, {Key: []uint8("creator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, {Key: []uint8("decimals"), Value: []uint8("0"), Index: false}, {Key: []uint8("meta"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("mintable"), Value: []uint8("false"), Index: false}, {Key: []uint8("name"), Value: []uint8("\"test\""), Index: false}, {Key: []uint8("symbol"), Value: []uint8("\"TT\""), Index: false}, {Key: []uint8("uri"), Value: []uint8("\"\""), Index: false}}}, - sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"fee15a74\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, {Key: []uint8("granter"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MODIFY\""), Index: false}}}, - sdk.Event{Type: "lbm.token.v1.EventMinted", Attributes: []abci.EventAttribute{{Key: []uint8("amount"), Value: []uint8("\"10\""), Index: false}, {Key: []uint8("contract_id"), Value: []uint8("\"fee15a74\""), Index: false}, {Key: []uint8("operator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, {Key: []uint8("to"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}}}, + sdk.Event{Type: "lbm.token.v1.EventIssued", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false}, {Key: []uint8("creator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, {Key: []uint8("decimals"), Value: []uint8("0"), Index: false}, {Key: []uint8("meta"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("mintable"), Value: []uint8("false"), Index: false}, {Key: []uint8("name"), Value: []uint8("\"test\""), Index: false}, {Key: []uint8("symbol"), Value: []uint8("\"TT\""), Index: false}, {Key: []uint8("uri"), Value: []uint8("\"\""), Index: false}}}, + sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, {Key: []uint8("granter"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MODIFY\""), Index: false}}}, + sdk.Event{Type: "lbm.token.v1.EventMinted", Attributes: []abci.EventAttribute{{Key: []uint8("amount"), Value: []uint8("\"10\""), Index: false}, {Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false}, {Key: []uint8("operator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, {Key: []uint8("to"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}}}, }, }, } From e712da3ffe2d538434b063654dca6832439ad091 Mon Sep 17 00:00:00 2001 From: zemyblue Date: Fri, 22 Sep 2023 20:20:23 +0900 Subject: [PATCH 5/6] chore: update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 787d2bd0eb..b176895439 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (ostracon) [\#1099](https://github.com/Finschia/finschia-sdk/pull/1099) Remove libsodium vrf library. * (refactor) [\#1114](https://github.com/Finschia/finschia-sdk/pull/1114) Check statistics and balance on x/collection mint and burn operations * (x/token) [\#1128](https://github.com/Finschia/finschia-sdk/pull/1128) add more unittest for MsgIssue of x/token +* (x/token) [\#1129](https://github.com/Finschia/finschia-sdk/pull/1129) add more unittest for `MsgGrantPermission` and `MsgRevokePermission` of x/token ### Bug Fixes * (ledger) [\#1040](https://github.com/Finschia/finschia-sdk/pull/1040) Fix a bug(unable to connect nano S plus ledger on ubuntu) From 53970c9ebf69738c70d8970889565d49aaa35eca Mon Sep 17 00:00:00 2001 From: zemyblue Date: Mon, 25 Sep 2023 14:28:50 +0900 Subject: [PATCH 6/6] chore: reformat the expected events --- x/token/keeper/msg_server_test.go | 148 +++++++++++++++++++++++++++--- 1 file changed, 134 insertions(+), 14 deletions(-) diff --git a/x/token/keeper/msg_server_test.go b/x/token/keeper/msg_server_test.go index 38e0c93a7e..c446822674 100644 --- a/x/token/keeper/msg_server_test.go +++ b/x/token/keeper/msg_server_test.go @@ -241,20 +241,92 @@ func (s *KeeperTestSuite) TestMsgIssue() { mintable: true, amount: sdk.NewInt(10), events: sdk.Events{ - sdk.Event{Type: "lbm.token.v1.EventIssued", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false}, {Key: []uint8("creator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, {Key: []uint8("decimals"), Value: []uint8("0"), Index: false}, {Key: []uint8("meta"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("mintable"), Value: []uint8("true"), Index: false}, {Key: []uint8("name"), Value: []uint8("\"test\""), Index: false}, {Key: []uint8("symbol"), Value: []uint8("\"TT\""), Index: false}, {Key: []uint8("uri"), Value: []uint8("\"\""), Index: false}}}, - sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}, {Key: []uint8("granter"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MODIFY\""), Index: false}}}, - sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}, {Key: []uint8("granter"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MINT\""), Index: false}}}, - sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}, {Key: []uint8("granter"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_BURN\""), Index: false}}}, - sdk.Event{Type: "lbm.token.v1.EventMinted", Attributes: []abci.EventAttribute{{Key: []uint8("amount"), Value: []uint8("\"10\""), Index: false}, {Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false}, {Key: []uint8("operator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, {Key: []uint8("to"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}}}, + sdk.Event{ + Type: "lbm.token.v1.EventIssued", + Attributes: []abci.EventAttribute{ + {Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false}, + {Key: []uint8("creator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, + {Key: []uint8("decimals"), Value: []uint8("0"), Index: false}, + {Key: []uint8("meta"), Value: []uint8("\"\""), Index: false}, + {Key: []uint8("mintable"), Value: []uint8("true"), Index: false}, + {Key: []uint8("name"), Value: []uint8("\"test\""), Index: false}, + {Key: []uint8("symbol"), Value: []uint8("\"TT\""), Index: false}, + {Key: []uint8("uri"), Value: []uint8("\"\""), Index: false}, + }, + }, + sdk.Event{ + Type: "lbm.token.v1.EventGranted", + Attributes: []abci.EventAttribute{ + {Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false}, + {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}, + {Key: []uint8("granter"), Value: []uint8("\"\""), Index: false}, + {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MODIFY\""), Index: false}, + }, + }, + sdk.Event{ + Type: "lbm.token.v1.EventGranted", + Attributes: []abci.EventAttribute{ + {Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false}, + {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}, + {Key: []uint8("granter"), Value: []uint8("\"\""), Index: false}, + {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MINT\""), Index: false}, + }, + }, + sdk.Event{ + Type: "lbm.token.v1.EventGranted", + Attributes: []abci.EventAttribute{ + {Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false}, + {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}, + {Key: []uint8("granter"), Value: []uint8("\"\""), Index: false}, + {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_BURN\""), Index: false}, + }, + }, + sdk.Event{ + Type: "lbm.token.v1.EventMinted", + Attributes: []abci.EventAttribute{ + {Key: []uint8("amount"), Value: []uint8("\"10\""), Index: false}, + {Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false}, + {Key: []uint8("operator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, + {Key: []uint8("to"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}, + }, + }, }, }, "mintable false": { mintable: false, amount: sdk.NewInt(10), events: sdk.Events{ - sdk.Event{Type: "lbm.token.v1.EventIssued", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false}, {Key: []uint8("creator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, {Key: []uint8("decimals"), Value: []uint8("0"), Index: false}, {Key: []uint8("meta"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("mintable"), Value: []uint8("false"), Index: false}, {Key: []uint8("name"), Value: []uint8("\"test\""), Index: false}, {Key: []uint8("symbol"), Value: []uint8("\"TT\""), Index: false}, {Key: []uint8("uri"), Value: []uint8("\"\""), Index: false}}}, - sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, {Key: []uint8("granter"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MODIFY\""), Index: false}}}, - sdk.Event{Type: "lbm.token.v1.EventMinted", Attributes: []abci.EventAttribute{{Key: []uint8("amount"), Value: []uint8("\"10\""), Index: false}, {Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false}, {Key: []uint8("operator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, {Key: []uint8("to"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}}}, + sdk.Event{ + Type: "lbm.token.v1.EventIssued", + Attributes: []abci.EventAttribute{ + {Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false}, + {Key: []uint8("creator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, + {Key: []uint8("decimals"), Value: []uint8("0"), Index: false}, + {Key: []uint8("meta"), Value: []uint8("\"\""), Index: false}, + {Key: []uint8("mintable"), Value: []uint8("false"), Index: false}, + {Key: []uint8("name"), Value: []uint8("\"test\""), Index: false}, + {Key: []uint8("symbol"), Value: []uint8("\"TT\""), Index: false}, + {Key: []uint8("uri"), Value: []uint8("\"\""), Index: false}, + }, + }, + sdk.Event{ + Type: "lbm.token.v1.EventGranted", + Attributes: []abci.EventAttribute{ + {Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false}, + {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, + {Key: []uint8("granter"), Value: []uint8("\"\""), Index: false}, + {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MODIFY\""), Index: false}, + }, + }, + sdk.Event{ + Type: "lbm.token.v1.EventMinted", + Attributes: []abci.EventAttribute{ + {Key: []uint8("amount"), Value: []uint8("\"10\""), Index: false}, + {Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false}, + {Key: []uint8("operator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, + {Key: []uint8("to"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}, + }, + }, }, }, } @@ -384,7 +456,15 @@ func (s *KeeperTestSuite) TestMsgGrantPermission() { grantee: s.operator, permission: token.LegacyPermissionMint.String(), events: sdk.Events{ - sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", s.operator.String())), Index: false}, {Key: []uint8("granter"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor.String())), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MINT\""), Index: false}}}, + sdk.Event{ + Type: "lbm.token.v1.EventGranted", + Attributes: []abci.EventAttribute{ + {Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, + {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", s.operator.String())), Index: false}, + {Key: []uint8("granter"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor.String())), Index: false}, + {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MINT\""), Index: false}, + }, + }, }, }, "valid request - BURN": { @@ -393,7 +473,15 @@ func (s *KeeperTestSuite) TestMsgGrantPermission() { grantee: s.operator, permission: token.LegacyPermissionBurn.String(), events: sdk.Events{ - sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", s.operator.String())), Index: false}, {Key: []uint8("granter"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor.String())), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_BURN\""), Index: false}}}, + sdk.Event{ + Type: "lbm.token.v1.EventGranted", + Attributes: []abci.EventAttribute{ + {Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, + {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", s.operator.String())), Index: false}, + {Key: []uint8("granter"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor.String())), Index: false}, + {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_BURN\""), Index: false}, + }, + }, }, }, "valid request - MODIFY": { @@ -402,7 +490,15 @@ func (s *KeeperTestSuite) TestMsgGrantPermission() { grantee: s.operator, permission: token.LegacyPermissionModify.String(), events: sdk.Events{ - sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", s.operator.String())), Index: false}, {Key: []uint8("granter"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor.String())), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MODIFY\""), Index: false}}}, + sdk.Event{ + Type: "lbm.token.v1.EventGranted", + Attributes: []abci.EventAttribute{ + {Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, + {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", s.operator.String())), Index: false}, + {Key: []uint8("granter"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor.String())), Index: false}, + {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MODIFY\""), Index: false}, + }, + }, }, }, } @@ -491,19 +587,43 @@ func (s *KeeperTestSuite) TestMsgRevokePermission() { contractID: s.contractID, from: s.operator, permission: token.LegacyPermissionMint.String(), - events: sdk.Events{sdk.Event{Type: "lbm.token.v1.EventRenounced", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", s.operator)), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MINT\""), Index: false}}}}, + events: sdk.Events{ + sdk.Event{ + Type: "lbm.token.v1.EventRenounced", + Attributes: []abci.EventAttribute{ + {Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, + {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", s.operator)), Index: false}, + {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MINT\""), Index: false}, + }, + }}, }, "valid request - revoke BURN": { contractID: s.contractID, from: s.operator, permission: token.LegacyPermissionBurn.String(), - events: sdk.Events{sdk.Event{Type: "lbm.token.v1.EventRenounced", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", s.operator)), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_BURN\""), Index: false}}}}, + events: sdk.Events{ + sdk.Event{ + Type: "lbm.token.v1.EventRenounced", + Attributes: []abci.EventAttribute{ + {Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, + {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", s.operator)), Index: false}, + {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_BURN\""), Index: false}, + }, + }}, }, "valid request - revoke MODIFY": { contractID: s.contractID, from: s.vendor, permission: token.LegacyPermissionModify.String(), - events: sdk.Events{sdk.Event{Type: "lbm.token.v1.EventRenounced", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor)), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MODIFY\""), Index: false}}}}, + events: sdk.Events{ + sdk.Event{ + Type: "lbm.token.v1.EventRenounced", + Attributes: []abci.EventAttribute{ + {Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false}, + {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor)), Index: false}, + {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MODIFY\""), Index: false}, + }, + }}, }, }