Skip to content

Commit

Permalink
feat: group member expiration
Browse files Browse the repository at this point in the history
  • Loading branch information
j75689 committed Aug 4, 2023
1 parent 5312550 commit 7adba6d
Show file tree
Hide file tree
Showing 26 changed files with 3,059 additions and 585 deletions.
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.BEP1001,
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.BEP1001,
func() error {
app.Logger().Info("processing upgrade initializer", "name", upgradetypes.BEP1001)
// enable the expiration of the group member from cross-chain operation
app.Logger().Info("register UpdateGroupMemberV2SynPackageType")
storagetypes.RegisterUpdateGroupMemberV2SynPackageType()
return nil
},
)
}
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
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.4
go.uber.org/mock v0.2.0
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1
google.golang.org/grpc v1.56.1
google.golang.org/protobuf v1.30.0
Expand Down Expand Up @@ -176,7 +177,8 @@ 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
// TODO: bump to the official version after the release
github.com/cosmos/cosmos-sdk => github.com/j75689/greenfield-cosmos-sdk v0.0.0-20230803221121-ae1fe2c384e5 //github.com/bnb-chain/greenfield-cosmos-sdk v0.2.3-alpha.5
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
)
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,6 @@ 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/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 Expand Up @@ -704,6 +702,8 @@ github.com/ipfs/go-log/v2 v2.1.1/go.mod h1:2v2nsGfZsvvAJz13SyFzf9ObaqwHiHxsPLEHn
github.com/ipfs/go-log/v2 v2.1.3/go.mod h1:/8d0SH3Su5Ooc31QlL1WysJhvyOTDCjcCZ9Axpmri6g=
github.com/ipfs/go-log/v2 v2.3.0/go.mod h1:QqGoj30OTpnKaG/LKTGTxoP2mmQtjVMEnK72gynbe/g=
github.com/ipfs/go-log/v2 v2.4.0/go.mod h1:nPZnh7Cj7lwS3LpRU5Mwr2ol1c2gXIEXuF6aywqrtmo=
github.com/j75689/greenfield-cosmos-sdk v0.0.0-20230803221121-ae1fe2c384e5 h1:NV6AqwL4apq4SFX71P+6naQuLszE2Ysp/BF3q27EkGM=
github.com/j75689/greenfield-cosmos-sdk v0.0.0-20230803221121-ae1fe2c384e5/go.mod h1:hpvg93+VGXHAcv/pVVdp24Ik/9miw4uRh8+tD0DDYas=
github.com/jackpal/go-nat-pmp v1.0.2-0.20160603034137-1fa385a6f458/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc=
github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc=
github.com/jbenet/go-cienv v0.1.0/go.mod h1:TqNnHUmJgXau0nCzC7kXWeotg3J9W34CUv5Djy1+FlA=
Expand Down Expand Up @@ -1454,6 +1454,8 @@ go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/automaxprocs v1.3.0/go.mod h1:9CWT6lKIep8U41DDaPiH6eFscnTyjfTANNQNx6LrIcA=
go.uber.org/goleak v1.0.0/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
go.uber.org/mock v0.2.0 h1:TaP3xedm7JaAgScZO7tlvlKrqT0p7I6OsdGB5YNSMDU=
go.uber.org/mock v0.2.0/go.mod h1:J0y0rp9L3xiff1+ZBfKxlC1fz2+aO16tw0tsDOixfuM=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
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"];
// 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
67 changes: 65 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())
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())
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 @@ -108,6 +149,28 @@ func (k Keeper) GetGroupMemberByID(ctx sdk.Context, groupMemberID math.Uint) (*t
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
}

func (k Keeper) updatePolicy(ctx sdk.Context, policy *types.Policy, newPolicy *types.Policy) *types.Policy {
store := ctx.KVStore(k.storeKey)
policy.Statements = newPolicy.Statements
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

0 comments on commit 7adba6d

Please sign in to comment.