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

feat: group member expiration #374

Merged
merged 22 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ format:
tools:
go install github.com/cosmos/gogoproto/protoc-gen-gocosmos
go install github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
go install github.com/golang/mock/mockgen

proto-gen:
cd proto && buf generate && cp -r github.com/bnb-chain/greenfield/x/* ../x && cp -r github.com/bnb-chain/greenfield/types/* ../types && rm -rf github.com && go mod tidy
Expand Down
33 changes: 30 additions & 3 deletions app/upgrade.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package app

import (
storagetypes "github.com/bnb-chain/greenfield/x/storage/types"
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
gashubtypes "github.com/cosmos/cosmos-sdk/x/gashub/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
)

func (app *App) RegisterUpgradeHandlers(chainID string, serverCfg *serverconfig.Config) error {
Expand All @@ -13,13 +18,12 @@ func (app *App) RegisterUpgradeHandlers(chainID string, serverCfg *serverconfig.

// Register the upgrade handlers here
// app.registerPublicDelegationUpgradeHandler()
// app.register...()
// ...
app.registerBEP1001UpgradeHandler()

return nil
}

// registerPublicDelegationUpgradeHandler registers the upgrade handlers for the public delegation upgrade.
// it will be enabled at the future version.
// func (app *App) registerPublicDelegationUpgradeHandler() {
// // Register the upgrade handler
// app.UpgradeKeeper.SetUpgradeHandler(upgradetypes.EnablePublicDelegationUpgrade,
Expand All @@ -36,3 +40,26 @@ func (app *App) RegisterUpgradeHandlers(chainID string, serverCfg *serverconfig.
// },
// )
// }

// registerBEP1001UpgradeHandler registers the upgrade handlers for BEP1001.
func (app *App) registerBEP1001UpgradeHandler() {
// Register the upgrade handler
app.UpgradeKeeper.SetUpgradeHandler(upgradetypes.Nagqu,
func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
app.Logger().Info("processing upgrade handler", "name", plan.Name, "info", plan.Info)
app.Logger().Info("register /greenfield.storage.MsgRenewGroupMember gas params", "name", plan.Name, "info", plan.Info)
app.GashubKeeper.SetMsgGasParams(ctx, *gashubtypes.NewMsgGasParamsWithFixedGas("/greenfield.storage.MsgRenewGroupMember", 1.2e3))
return fromVM, nil
})

// Register the upgrade initializer
app.UpgradeKeeper.SetUpgradeInitializer(upgradetypes.Nagqu,
func() error {
app.Logger().Info("processing upgrade initializer", "name", upgradetypes.Nagqu)
// enable the expiration of the group member from cross-chain operation
app.Logger().Info("register UpdateGroupMemberV2SynPackageType")
storagetypes.RegisterUpdateGroupMemberV2SynPackageType()
return nil
},
)
}
16 changes: 16 additions & 0 deletions deployment/localup/localup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,18 @@ function generate_sp_genesis {
${bin} collect-spgentxs --gentx-dir ${workspace}/.local/validator0/config/gensptx --home ${workspace}/.local/validator0
}

function export_validator {
size=$1

for ((i = 0; i < ${size}; i++)); do
bls_priv_key=("$(echo "y" | ${bin} keys export validator_bls${i} --unarmored-hex --unsafe --keyring-backend test --home ${workspace}/.local/validator${i})")
relayer_key=("$(echo "y" | ${bin} keys export relayer${i} --unarmored-hex --unsafe --keyring-backend test --home ${workspace}/.local/relayer${i})")

echo "validator_bls${i} bls_priv_key: ${bls_priv_key}"
echo "relayer${i} relayer_key: ${relayer_key}"
done
}

function export_sps {
size=$1
sp_size=1
Expand Down Expand Up @@ -312,6 +324,10 @@ generate)
export_sps)
export_sps $SIZE $SP_SIZE
;;

export_validator)
export_validator $SIZE
;;
start)
echo "===== start ===="
start $SIZE
Expand Down
9 changes: 9 additions & 0 deletions deployment/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ bash ./deployment/localup/localup.sh stop

3. Send Tx
```bash
VALIDATOR=$(./build/bin/gnfd keys show validator0 -a --home ./deployment/localup/.local/validator0 --keyring-backend test)
GROUP_NAME="admin2"

./build/bin/gnfd tx storage create-group $GROUP_NAME --members "0xfABDd8b607201667fE54054CB4AD9068Afa2993e,0xC6D661d5Ee633eA3DfD4D86f368cdd839D682D18,0xba8dde27ddc0f39041a1d9a4efd24b5b5d81fe17" --from validator0 --home ./deployment/localup/.local/validator0 --keyring-backend test --node http://localhost:26750 -b sync

./build/bin/gnfd q storage head-group-member $VALIDATOR $GROUP_NAME 0xfABDd8b607201667fE54054CB4AD9068Afa2993e --node http://localhost:26750

./build/bin/gnfd tx storage renew-group-member $GROUP_NAME 0xfABDd8b607201667fE54054CB4AD9068Afa2993e 1691118864 --from validator0 --home ./deployment/localup/.local/validator0 --keyring-backend test --node http://localhost:26750 -b sync

./build/bin/gnfd tx bank send validator0 0x32Ff14Fa1547314b95991976DB432F9Aa648A423 500000000000000000000BNB --home ./deployment/localup/.local/validator0 --keyring-backend test --node http://localhost:26750 -b sync
```

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ replace (
github.com/cometbft/cometbft => github.com/bnb-chain/greenfield-cometbft v0.0.2-alpha.2
github.com/cometbft/cometbft-db => github.com/bnb-chain/greenfield-cometbft-db v0.8.1-alpha.1
github.com/confio/ics23/go => github.com/cosmos/cosmos-sdk/ics23/go v0.8.0
github.com/cosmos/cosmos-sdk => github.com/bnb-chain/greenfield-cosmos-sdk v0.2.3-alpha.3.0.20230803020148-9216a6aea390
github.com/cosmos/cosmos-sdk => github.com/bnb-chain/greenfield-cosmos-sdk v0.2.3-alpha.3.0.20230804065518-1a504101bd5e
github.com/cosmos/iavl => github.com/bnb-chain/greenfield-iavl v0.20.1-alpha.1
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ github.com/bnb-chain/greenfield-cometbft v0.0.2-alpha.2 h1:ys9kmgtRx04wcCextE6Cr
github.com/bnb-chain/greenfield-cometbft v0.0.2-alpha.2/go.mod h1:EBmwmUdaNbGPyGjf1cMuoN3pAeM2tQu7Lfg95813EAw=
github.com/bnb-chain/greenfield-cometbft-db v0.8.1-alpha.1 h1:XcWulGacHVRiSCx90Q8Y//ajOrLNBQWR/KDB89dy3cU=
github.com/bnb-chain/greenfield-cometbft-db v0.8.1-alpha.1/go.mod h1:ey1CiK4bYo1RBNJLRiVbYr5CMdSxci9S/AZRINLtppI=
github.com/bnb-chain/greenfield-cosmos-sdk v0.2.3-alpha.3.0.20230803020148-9216a6aea390 h1:tuXCEm4WHJ/7mhYM9Nqq5z+Qu96xsSZ6pqw0LE0+Fqo=
github.com/bnb-chain/greenfield-cosmos-sdk v0.2.3-alpha.3.0.20230803020148-9216a6aea390/go.mod h1:hpvg93+VGXHAcv/pVVdp24Ik/9miw4uRh8+tD0DDYas=
github.com/bnb-chain/greenfield-cosmos-sdk v0.2.3-alpha.3.0.20230804065518-1a504101bd5e h1:4epfOfdg9rYRiaOmMMji4Os09e67+0Yx6zS1WSLqJGM=
github.com/bnb-chain/greenfield-cosmos-sdk v0.2.3-alpha.3.0.20230804065518-1a504101bd5e/go.mod h1:hpvg93+VGXHAcv/pVVdp24Ik/9miw4uRh8+tD0DDYas=
github.com/bnb-chain/greenfield-cosmos-sdk/api v0.0.0-20230425074444-eb5869b05fe9 h1:6fLpmmI0EZvDTfPvI0zy5dBaaTUboHnEkoC5/p/w8TQ=
github.com/bnb-chain/greenfield-cosmos-sdk/api v0.0.0-20230425074444-eb5869b05fe9/go.mod h1:rbc4o84RSEvhf09o2+4Qiazsv0snRJLiEZdk17HeIDw=
github.com/bnb-chain/greenfield-cosmos-sdk/math v0.0.0-20230425074444-eb5869b05fe9 h1:1ZdK+iR1Up02bOa2YTZCml7PBpP//kcdamOcK6aWO/s=
Expand Down
8 changes: 8 additions & 0 deletions proto/greenfield/permission/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,11 @@ message GroupMember {
// member is the account address of the member
string member = 3 [(cosmos_proto.scalar) = "cosmos.AddressString"];
}

message GroupMemberExtra {
// expiration_time defines the expiration time of the group member
google.protobuf.Timestamp expiration_time = 1 [
(gogoproto.stdtime) = true,
(gogoproto.nullable) = false
];
}
34 changes: 34 additions & 0 deletions proto/greenfield/storage/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package greenfield.storage;

import "cosmos_proto/cosmos.proto";
import "gogoproto/gogo.proto";
import "google/protobuf/timestamp.proto";
import "greenfield/storage/common.proto";
import "greenfield/storage/types.proto";

Expand Down Expand Up @@ -340,6 +341,39 @@ message EventUpdateGroupMember {
repeated string members_to_delete = 6 [(cosmos_proto.scalar) = "cosmos.AddressString"];
}

message EventRenewGroupMember {
// operator define the account address of operator who update the group member
string operator = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// owner define the account address of group owner
string owner = 2 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// group_name define the name of the group
string group_name = 3;
// id define an u256 id for group
string group_id = 4 [
(cosmos_proto.scalar) = "cosmos.Uint",
(gogoproto.customtype) = "Uint",
(gogoproto.nullable) = false
];
// source_type define the source of the group. CrossChain or Greenfield origin
SourceType source_type = 5;
// members define the all the address of the members.
repeated string members = 6 [(cosmos_proto.scalar) = "cosmos.AddressString"];
unclezoro marked this conversation as resolved.
Show resolved Hide resolved
// extra defines extra info for the group
string extra = 7;
// members_detail defines the all the members detail of the group.
repeated EventGroupMemberDetail members_detail = 8;
}

message EventGroupMemberDetail {
// member defines the account address of the group member
string member = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// expiration_time defines the expiration time of the group member
google.protobuf.Timestamp expiration_time = 2 [
(gogoproto.stdtime) = true,
(gogoproto.nullable) = false
];
}

// EventUpdateGroupExtra is emitted on MsgUpdateGroupExtra
message EventUpdateGroupExtra {
// operator define the account address of operator who update the group member
Expand Down
1 change: 1 addition & 0 deletions proto/greenfield/storage/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ message QueryHeadGroupMemberRequest {

message QueryHeadGroupMemberResponse {
permission.GroupMember group_member = 1;
permission.GroupMemberExtra group_member_extra = 2;
}

message QueryPolicyForGroupRequest {
Expand Down
29 changes: 29 additions & 0 deletions proto/greenfield/storage/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ service Msg {
rpc UpdateGroupExtra(MsgUpdateGroupExtra) returns (MsgUpdateGroupExtraResponse);
rpc LeaveGroup(MsgLeaveGroup) returns (MsgLeaveGroupResponse);
rpc MirrorGroup(MsgMirrorGroup) returns (MsgMirrorGroupResponse);
rpc RenewGroupMember(MsgRenewGroupMember) returns (MsgRenewGroupMemberResponse);

// basic operation of policy
rpc PutPolicy(MsgPutPolicy) returns (MsgPutPolicyResponse);
Expand Down Expand Up @@ -322,6 +323,34 @@ message MsgUpdateGroupMember {

message MsgUpdateGroupMemberResponse {}

message MsgRenewGroupMember {
option (cosmos.msg.v1.signer) = "operator";

// operator defines the account address of the operator who has the UpdateGroupMember permission of the group.
string operator = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];

// group_owner defines the account address of the group owner
string group_owner = 2 [(cosmos_proto.scalar) = "cosmos.AddressString"];

// group_name defines the name of the group which to be updated
string group_name = 3;

// members defines a list of members which will be renew to the group
repeated MsgGroupMember members = 4;
}

message MsgRenewGroupMemberResponse {}

message MsgGroupMember {
// member defines the account address of the group member
string member = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// expiration_time defines the expiration time of the group member
google.protobuf.Timestamp expiration_time = 2 [
(gogoproto.stdtime) = true,
(gogoproto.nullable) = false
];
}

message MsgUpdateGroupExtra {
option (cosmos.msg.v1.signer) = "operator";

Expand Down
76 changes: 74 additions & 2 deletions x/permission/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"fmt"
"time"

"cosmossdk.io/math"
"github.com/cometbft/cometbft/libs/log"
Expand Down Expand Up @@ -58,11 +59,11 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}

func (k Keeper) AddGroupMember(ctx sdk.Context, groupID math.Uint, member sdk.AccAddress) error {
func (k Keeper) AddGroupMember(ctx sdk.Context, groupID math.Uint, member sdk.AccAddress) (math.Uint, error) {
store := ctx.KVStore(k.storeKey)
memberKey := types.GetGroupMemberKey(groupID, member)
if store.Has(memberKey) {
return storagetypes.ErrGroupMemberAlreadyExists
return math.ZeroUint(), storagetypes.ErrGroupMemberAlreadyExists
}
groupMember := types.GroupMember{
GroupId: groupID,
Expand All @@ -71,9 +72,37 @@ func (k Keeper) AddGroupMember(ctx sdk.Context, groupID math.Uint, member sdk.Ac
id := k.groupMemberSeq.NextVal(store)
store.Set(memberKey, id.Bytes())
store.Set(types.GetGroupMemberByIDKey(id), k.cdc.MustMarshal(&groupMember))
return id, nil
}

func (k Keeper) AddGroupMemberWithExpiration(ctx sdk.Context, groupID math.Uint, member sdk.AccAddress, expiration time.Time) error {
id, err := k.AddGroupMember(ctx, groupID, member)
if err != nil {
return err
}

store := ctx.KVStore(k.storeKey)
// We can simply override the whole value here, because the expiration time is the only field in the value.
// If there are more fields in the future, we should use a more sophisticated way to update the value.
memberExtra := types.GroupMemberExtra{
ExpirationTime: expiration,
}
store.Set(types.GetGroupMemberExtraKey(groupID, member), id.Bytes())
unclezoro marked this conversation as resolved.
Show resolved Hide resolved
store.Set(types.GetGroupMemberExtraByIDKey(id), k.cdc.MustMarshal(&memberExtra))
return nil
}

func (k Keeper) UpdateGroupMemberExpiration(ctx sdk.Context, groupID math.Uint, member sdk.AccAddress, memberID math.Uint, expiration time.Time) {
store := ctx.KVStore(k.storeKey)
// We can simply override the whole value here, because the expiration time is the only field in the value.
// If there are more fields in the future, we should use a more sophisticated way to update the value.
memberExtra := types.GroupMemberExtra{
ExpirationTime: expiration,
}
store.Set(types.GetGroupMemberExtraKey(groupID, member), memberID.Bytes())
unclezoro marked this conversation as resolved.
Show resolved Hide resolved
store.Set(types.GetGroupMemberExtraByIDKey(memberID), k.cdc.MustMarshal(&memberExtra))
}

func (k Keeper) RemoveGroupMember(ctx sdk.Context, groupID math.Uint, member sdk.AccAddress) error {
store := ctx.KVStore(k.storeKey)
memberKey := types.GetGroupMemberKey(groupID, member)
Expand All @@ -86,6 +115,18 @@ func (k Keeper) RemoveGroupMember(ctx sdk.Context, groupID math.Uint, member sdk
return nil
}

func (k Keeper) RemoveGroupMemberExtra(ctx sdk.Context, groupID math.Uint, member sdk.AccAddress) error {
store := ctx.KVStore(k.storeKey)
memberKey := types.GetGroupMemberExtraKey(groupID, member)
bz := store.Get(memberKey)
if bz == nil {
return storagetypes.ErrNoSuchGroupMember
}
store.Delete(memberKey)
store.Delete(types.GetGroupMemberExtraByIDKey(k.groupMemberSeq.DecodeSequence(bz)))
return nil
}

func (k Keeper) GetGroupMember(ctx sdk.Context, groupID math.Uint, member sdk.AccAddress) (*types.GroupMember, bool) {
store := ctx.KVStore(k.storeKey)
memberKey := types.GetGroupMemberKey(groupID, member)
Expand All @@ -105,6 +146,29 @@ func (k Keeper) GetGroupMemberByID(ctx sdk.Context, groupMemberID math.Uint) (*t
}
var groupMember types.GroupMember
k.cdc.MustUnmarshal(bz, &groupMember)
groupMember.Id = groupMemberID
return &groupMember, true
}

func (k Keeper) GetGroupMemberExtra(ctx sdk.Context, groupID math.Uint, member sdk.AccAddress) (*types.GroupMemberExtra, bool) {
store := ctx.KVStore(k.storeKey)
memberKey := types.GetGroupMemberExtraKey(groupID, member)
bz := store.Get(memberKey)
if bz == nil {
return nil, false
}

return k.GetGroupMemberExtraByID(ctx, k.groupMemberSeq.DecodeSequence(bz))
}

func (k Keeper) GetGroupMemberExtraByID(ctx sdk.Context, groupMemberID math.Uint) (*types.GroupMemberExtra, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetGroupMemberExtraByIDKey(groupMemberID))
if bz == nil {
return nil, false
}
var groupMember types.GroupMemberExtra
k.cdc.MustUnmarshal(bz, &groupMember)
return &groupMember, true
}

Expand Down Expand Up @@ -291,6 +355,14 @@ func (k Keeper) VerifyPolicy(ctx sdk.Context, resourceID math.Uint, resourceType
return types.EFFECT_DENY
}
}

// post check if the operator has not been expired
unclezoro marked this conversation as resolved.
Show resolved Hide resolved
groupMemberExtra, memberExtraFound := k.GetGroupMemberExtra(ctx, item.GroupId, operator)
if allowed && memberExtraFound {
if groupMemberExtra.ExpirationTime.Before(ctx.BlockTime()) {
return types.EFFECT_DENY
}
}
}
}
if allowed {
Expand Down
18 changes: 16 additions & 2 deletions x/permission/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ var (
ObjectPolicyForAccountPrefix = []byte{0x12}
GroupPolicyForAccountPrefix = []byte{0x13}
GroupMemberPrefix = []byte{0x14}
GroupMemberExtraPrefix = []byte{0x15}

BucketPolicyForGroupPrefix = []byte{0x21}
ObjectPolicyForGroupPrefix = []byte{0x22}

PolicyByIDPrefix = []byte{0x31}
GroupMemberByIDPrefix = []byte{0x32}
PolicyByIDPrefix = []byte{0x31}
GroupMemberByIDPrefix = []byte{0x32}
GroupMemberExtraByIDPrefix = []byte{0x33}

PolicySequencePrefix = []byte{0x41}
GroupMemberSequencePrefix = []byte{0x42}
Expand Down Expand Up @@ -92,3 +94,15 @@ func GetGroupMemberKey(groupID math.Uint, member sdk.AccAddress) []byte {
func GetGroupMemberByIDKey(memberID math.Uint) []byte {
return append(GroupMemberByIDPrefix, memberID.Bytes()...)
}

func GroupMembersExtraPrefix(groupID math.Uint) []byte {
return append(GroupMemberExtraPrefix, groupID.Bytes()...)
}

func GetGroupMemberExtraKey(groupID math.Uint, member sdk.AccAddress) []byte {
return append(GroupMemberExtraPrefix, append(groupID.Bytes(), member.Bytes()...)...)
}

func GetGroupMemberExtraByIDKey(memberID math.Uint) []byte {
return append(GroupMemberExtraByIDPrefix, memberID.Bytes()...)
}
Loading