Skip to content

Commit

Permalink
Merge pull request #90 from bianjieai/yuandu/side-chain-v1.1.0
Browse files Browse the repository at this point in the history
feat: enhance query for side-chain
  • Loading branch information
Dreamer authored Jul 24, 2023
2 parents fad0052 + 760cbde commit c1adb03
Show file tree
Hide file tree
Showing 25 changed files with 773 additions and 177 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

## [v1.5.0] - 2023-07-20

### Improvements

* (iritamod/sidechain) [\#90](https://github.com/bianjieai/iritamod/pull/90) enhance side-chain module query
* (iritamod/perm) [\#90](https://github.com/bianjieai/iritamod/pull/90) correct name for side-chain user role

### Bug Fixes

* (iritamod/perm) [\#90](https://github.com/bianjieai/iritamod/pull/90) multiple transfer space msgs should fail

## [v1.4.0] - 2023-07-18

### Application
Expand Down
2 changes: 1 addition & 1 deletion modules/perm/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
RoleRelayerUser = types.RoleRelayerUser
RolePlatformUser = types.RolePlatformUser
RolePowerUserAdmin = types.RolePowerUserAdmin
RoleLayer2User = types.RoleLayer2User
RoleSideChainUser = types.RoleSideChainUser
)

var (
Expand Down
4 changes: 2 additions & 2 deletions modules/perm/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func NewAssignRolesCmd() *cobra.Command {
types.RoleRelayerUser,
types.RolePlatformUser,
types.RolePowerUserAdmin,
types.RoleLayer2User,
types.RoleSideChainUser,
)),
Args: cobra.MinimumNArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -105,7 +105,7 @@ func NewUnassignRolesCmd() *cobra.Command {
types.RoleRelayerUser,
types.RolePlatformUser,
types.RolePowerUserAdmin,
types.RoleLayer2User,
types.RoleSideChainUser,
)),
Args: cobra.MinimumNArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
2 changes: 1 addition & 1 deletion modules/perm/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (suite *KeeperTestSuite) TestPowerAdmin() {
types.RoleBaseM1Admin,
types.RolePlatformUser,
types.RolePowerUserAdmin,
types.RoleLayer2User,
types.RoleSideChainUser,
}
for _, role := range roles {
err = suite.keeper.Authorize(suite.ctx, accountPowerUser, accountPowerUserAdmin, role)
Expand Down
4 changes: 2 additions & 2 deletions modules/perm/types/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func (a Auth) Roles() (rs []Role) {
if a.Access(RolePowerUserAdmin.Auth()) {
rs = append(rs, RolePowerUserAdmin)
}
if a.Access(RoleLayer2User.Auth()) {
rs = append(rs, RoleLayer2User)
if a.Access(RoleSideChainUser.Auth()) {
rs = append(rs, RoleSideChainUser)
}

return rs
Expand Down
67 changes: 34 additions & 33 deletions modules/perm/types/perm.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion modules/perm/types/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func ValidRole(role Role) bool {
role == RoleRelayerUser ||
role == RolePlatformUser ||
role == RolePowerUserAdmin ||
role == RoleLayer2User {
role == RoleSideChainUser {
return true
}
return false
Expand Down
6 changes: 3 additions & 3 deletions modules/side-chain/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func GetCmdQuerySpaceInfo() *cobra.Command {
cmd := &cobra.Command{
Use: "space [space-id]",
Long: "query the space info of the given space-id",
Example: fmt.Sprintf("$ %s q layer2 space space [space-id]", version.AppName),
Example: fmt.Sprintf("$ %s q sidechain space space [space-id]", version.AppName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
Expand Down Expand Up @@ -84,7 +84,7 @@ func GetCmdQuerySpacesOfOwner() *cobra.Command {
cmd := &cobra.Command{
Use: "owner [owner]",
Long: "query the spaces of the given owner",
Example: fmt.Sprintf("$ %s q layer2 space owner [owner]", version.AppName),
Example: fmt.Sprintf("$ %s q sidechain space owner [owner]", version.AppName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
Expand Down Expand Up @@ -124,7 +124,7 @@ func GetCmdQueryBlockHeader() *cobra.Command {
cmd := &cobra.Command{
Use: "blockheader [space-id] [height]",
Long: "query the side chain block header",
Example: fmt.Sprintf("$ %s q layer2 blockheader [space-id] [height]", version.AppName),
Example: fmt.Sprintf("$ %s q sidechain blockheader [space-id] [height]", version.AppName),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
Expand Down
6 changes: 3 additions & 3 deletions modules/side-chain/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func GetCmdSpaceCreate() *cobra.Command {
Use: "create",
Long: "create a new side chain space",
Example: fmt.Sprintf(
"$ %s tx side-chain space create "+
"$ %s tx sidechain space create "+
"--name=<name> "+
"--uri=<uri>",
version.AppName),
Expand Down Expand Up @@ -96,7 +96,7 @@ func GetCmdSpaceTransfer() *cobra.Command {
Use: "transfer [space-id] [recipient]",
Long: "transfer ownership of space from sender to recipient",
Example: fmt.Sprintf(
"$ %s tx layer2 space transfer [space-id] [recipient]",
"$ %s tx sidechain space transfer [space-id] [recipient]",
version.AppName),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -130,7 +130,7 @@ func GetCmdCreateBlockHeader() *cobra.Command {
Use: "create-blockheader [space-id] [height] [header]",
Long: "create a side chain block header record",
Example: fmt.Sprintf(
"$ %s tx layer2 create-blockheader [space-id] [height] [header]",
"$ %s tx sidechain create-blockheader [space-id] [height] [header]",
version.AppName),
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
18 changes: 3 additions & 15 deletions modules/side-chain/keeper/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (dlt ValidateSideChainDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, sim
return ctx, err
}
case *types.MsgTransferSpace:
if err := dlt.validateSpaceOwnership(ctx, msg.Sender, msg.SpaceId); err != nil {
if err := dlt.validateOnlySpace(ctx, msg.SpaceId); err != nil {
return ctx, err
}
if err := dlt.validateSideChainUserRole(ctx, msg.Sender); err != nil {
Expand All @@ -37,7 +37,7 @@ func (dlt ValidateSideChainDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, sim
return ctx, err
}
case *types.MsgCreateBlockHeader:
if err := dlt.validateSpaceOwnership(ctx, msg.Sender, msg.SpaceId); err != nil {
if err := dlt.validateOnlySpace(ctx, msg.SpaceId); err != nil {
return ctx, err
}
if err := dlt.validateSideChainUserRole(ctx, msg.Sender); err != nil {
Expand All @@ -56,22 +56,10 @@ func (dlt ValidateSideChainDecorator) validateOnlySpace(ctx sdk.Context, spaceId
return nil
}

func (dlt ValidateSideChainDecorator) validateSpaceOwnership(ctx sdk.Context, addr string, spaceId uint64) error {
accAddr, _ := sdk.AccAddressFromBech32(addr)
if !dlt.keeper.HasSpace(ctx, spaceId) {
return sdkerrors.Wrapf(types.ErrInvalidSpaceId, "space (%d) does not exist", spaceId)
}

if !dlt.keeper.HasSpaceOfOwner(ctx, accAddr, spaceId) {
return sdkerrors.Wrapf(types.ErrInvalidSpaceOwner, "space (%d) is not owned by (%s)", spaceId, addr)
}
return nil
}

func (dlt ValidateSideChainDecorator) validateSideChainUserRole(ctx sdk.Context, addr string) error {
accAddr, _ := sdk.AccAddressFromBech32(addr)
if !dlt.permKeeper.HasSideChainUserRole(ctx, accAddr) {
return sdkerrors.Wrapf(types.ErrInvalidSideChainUser, "account (%s) does not have l2 user role", addr)
return sdkerrors.Wrapf(types.ErrInvalidSideChainUser, "account (%s) does not have side chain user role", addr)
}
return nil
}
12 changes: 9 additions & 3 deletions modules/side-chain/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,23 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) {
for _, blockHeader := range data.BlockHeaders {
k.setBlockHeader(ctx, blockHeader.SpaceId, blockHeader.Height, blockHeader.Header)
}

for _, spaceLatestHeight := range data.SpaceLatestHeights {
k.setSpaceLatestHeight(ctx, spaceLatestHeight.SpaceId, spaceLatestHeight.Height)
}
}

func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
data := types.GenesisState{
SpaceSequence: 0,
Spaces: make([]types.Space, 0),
BlockHeaders: make([]types.BlockHeader, 0),
SpaceSequence: 0,
Spaces: make([]types.Space, 0),
BlockHeaders: make([]types.BlockHeader, 0),
SpaceLatestHeights: make([]types.SpaceLatestHeight, 0),
}

data.SpaceSequence = k.GetSpaceSequence(ctx)
data.Spaces = k.GetSpaces(ctx)
data.BlockHeaders = k.GetBlockHeaders(ctx)
data.SpaceLatestHeights = k.GetSpaceLatestHeights(ctx)
return &data
}
26 changes: 24 additions & 2 deletions modules/side-chain/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,16 @@ func (k Keeper) Space(goCtx context.Context, request *types.QuerySpaceRequest) (
return nil, err
}

// NOTE: history data didn't record latest height, so return 0 if not exist.
latestHeight := uint64(0)
lh, exist := k.GetSpaceLatestHeight(ctx, request.SpaceId)
if exist {
latestHeight = lh
}

return &types.QuerySpaceResponse{
Space: &space,
Space: &space,
LatestHeight: latestHeight,
}, nil
}

Expand Down Expand Up @@ -62,5 +70,19 @@ func (k Keeper) BlockHeader(goCtx context.Context, request *types.QueryBlockHead
if err != nil {
return nil, err
}
return &types.QueryBlockHeaderResponse{Header: header}, nil

// NOTE: history data didn't record create block header tx hash, so return empty if not exist.
txHash := ""
if k.HasBlockHeaderTxHash(ctx, request.SpaceId, request.Height) {
th, err := k.GetBlockHeaderTxHash(ctx, request.SpaceId, request.Height)
if err != nil {
return nil, err
}
txHash = th
}

return &types.QueryBlockHeaderResponse{
TxHash: txHash,
Header: header,
}, nil
}
30 changes: 23 additions & 7 deletions modules/side-chain/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import (
"github.com/tendermint/tendermint/crypto/tmhash"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/bianjieai/iritamod/modules/perm"
permtypes "github.com/bianjieai/iritamod/modules/perm/types"
"github.com/bianjieai/iritamod/modules/side-chain/keeper"
"github.com/bianjieai/iritamod/simapp"
)

var (
rootAdmin = sdk.AccAddress(tmhash.SumTruncated([]byte("rootAdmin")))
accAvata = sdk.AccAddress(tmhash.SumTruncated([]byte("acc_avata"))) // l2 user: cosmos1j0898zyz64cyxy2s2km99t2c3s6tn5tzfppw9h
accXvata = sdk.AccAddress(tmhash.SumTruncated([]byte("acc_xvata"))) // l2 user
accAvata = sdk.AccAddress(tmhash.SumTruncated([]byte("acc_avata"))) // side chain user: cosmos1j0898zyz64cyxy2s2km99t2c3s6tn5tzfppw9h
accXvata = sdk.AccAddress(tmhash.SumTruncated([]byte("acc_xvata"))) // side chain user
accAlice = sdk.AccAddress(tmhash.SumTruncated([]byte("acc_alice"))) // cosmos16877jxzrdetmzsl3pntv4n402m8d0cpvwd74w7
accBob = sdk.AccAddress(tmhash.SumTruncated([]byte("acc_bob"))) // cosmos1a53v8ksyd6x47sju572t48s3ynmqyan0n2c6kx

Expand All @@ -28,10 +30,11 @@ var (
type TestSuite struct {
suite.Suite

ctx sdk.Context
cdc *codec.LegacyAmino
keeper keeper.Keeper
app *simapp.SimApp
ctx sdk.Context
cdc *codec.LegacyAmino
keeper keeper.Keeper
permKeeper perm.Keeper
app *simapp.SimApp
}

func TestTestSuite(t *testing.T) {
Expand All @@ -48,8 +51,10 @@ func (s *TestSuite) SetupTest() {
s.cdc = app.LegacyAmino()
s.ctx = app.BaseApp.NewContext(false, tmproto.Header{})
s.app = app
s.keeper = app.Layer2Keeper
s.keeper = app.SideChainKeeper
s.permKeeper = app.PermKeeper

s.prepareRoles()
s.prepareSideChain()
}

Expand All @@ -58,3 +63,14 @@ func (s *TestSuite) prepareSideChain() {
s.Require().NoError(err)
s.Require().Equal(avataSpaceId, id)
}

func (s *TestSuite) prepareRoles() {
err := s.permKeeper.Authorize(s.ctx, accAvata, rootAdmin, permtypes.RoleSideChainUser)
if err != nil {
panic("failed to authorize role")
}
err = s.permKeeper.Authorize(s.ctx, accXvata, rootAdmin, permtypes.RoleSideChainUser)
if err != nil {
panic("failed to authorize role")
}
}
Loading

0 comments on commit c1adb03

Please sign in to comment.