Skip to content

Commit

Permalink
add gRPC for RBAC
Browse files Browse the repository at this point in the history
  • Loading branch information
tkxkd0159 committed May 2, 2024
1 parent 1693f60 commit b3d0092
Show file tree
Hide file tree
Showing 8 changed files with 1,464 additions and 197 deletions.
74 changes: 71 additions & 3 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,10 @@
- [QueryProposalsResponse](#lbm.fbridge.v1.QueryProposalsResponse)
- [QuerySubmittedProvisionRequest](#lbm.fbridge.v1.QuerySubmittedProvisionRequest)
- [QuerySubmittedProvisionResponse](#lbm.fbridge.v1.QuerySubmittedProvisionResponse)
- [QueryVoteRequest](#lbm.fbridge.v1.QueryVoteRequest)
- [QueryVoteResponse](#lbm.fbridge.v1.QueryVoteResponse)
- [QueryVotesRequest](#lbm.fbridge.v1.QueryVotesRequest)
- [QueryVotesResponse](#lbm.fbridge.v1.QueryVotesResponse)

- [Query](#lbm.fbridge.v1.Query)

Expand Down Expand Up @@ -12133,7 +12137,7 @@ GenesisState defines the fbridge module's genesis state.

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `proposal_id` | [string](#string) | | the proposal id |
| `proposal_id` | [uint64](#uint64) | | the proposal id |



Expand Down Expand Up @@ -12179,6 +12183,7 @@ GenesisState defines the fbridge module's genesis state.
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `proposals` | [RoleProposal](#lbm.fbridge.v1.RoleProposal) | repeated | |
| `pagination` | [cosmos.base.query.v1beta1.PageResponse](#cosmos.base.query.v1beta1.PageResponse) | | pagination defines an pagination for the response. |



Expand Down Expand Up @@ -12216,6 +12221,67 @@ GenesisState defines the fbridge module's genesis state.




<a name="lbm.fbridge.v1.QueryVoteRequest"></a>

### QueryVoteRequest



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `proposal_id` | [uint64](#uint64) | | proposal_id defines the unique id of the proposal. |
| `voter` | [string](#string) | | voter defines the oter address for the proposals. |






<a name="lbm.fbridge.v1.QueryVoteResponse"></a>

### QueryVoteResponse



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `vote` | [Vote](#lbm.fbridge.v1.Vote) | | vote defined the queried vote. |






<a name="lbm.fbridge.v1.QueryVotesRequest"></a>

### QueryVotesRequest



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `proposal_id` | [uint64](#uint64) | | proposal_id defines the unique id of the proposal. |






<a name="lbm.fbridge.v1.QueryVotesResponse"></a>

### QueryVotesResponse



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `votes` | [Vote](#lbm.fbridge.v1.Vote) | repeated | votes defined the queried votes. |





<!-- end messages -->

<!-- end enums -->
Expand All @@ -12241,8 +12307,10 @@ GenesisState defines the fbridge module's genesis state.
| `Guardians` | [QueryGuardiansRequest](#lbm.fbridge.v1.QueryGuardiansRequest) | [QueryGuardiansResponse](#lbm.fbridge.v1.QueryGuardiansResponse) | Guardians queries a list of Guardians registered on the bridge | GET|/lbm/fbridge/v1/guardians|
| `Operators` | [QueryOperatorsRequest](#lbm.fbridge.v1.QueryOperatorsRequest) | [QueryOperatorsResponse](#lbm.fbridge.v1.QueryOperatorsResponse) | Operators queries a list of Operators registered on the bridge | GET|/lbm/fbridge/v1/operators|
| `Judges` | [QueryJudgesRequest](#lbm.fbridge.v1.QueryJudgesRequest) | [QueryJudgesResponse](#lbm.fbridge.v1.QueryJudgesResponse) | Judges queries a list of Judges registered on the bridge | GET|/lbm/fbridge/v1/judges|
| `Proposals` | [QueryProposalsRequest](#lbm.fbridge.v1.QueryProposalsRequest) | [QueryProposalsResponse](#lbm.fbridge.v1.QueryProposalsResponse) | Proposals queries a list of SuggestRole Proposals | GET|/lbm/fbridge/v1/role/proposals|
| `Proposal` | [QueryProposalRequest](#lbm.fbridge.v1.QueryProposalRequest) | [QueryProposalResponse](#lbm.fbridge.v1.QueryProposalResponse) | Proposal queries a SuggestRole Proposal | GET|/lbm/fbridge/v1/role/proposals/{proposal_id}|
| `Proposals` | [QueryProposalsRequest](#lbm.fbridge.v1.QueryProposalsRequest) | [QueryProposalsResponse](#lbm.fbridge.v1.QueryProposalsResponse) | Proposals queries a list of SuggestRole Proposals | GET|/lbm/fbridge/v1/proposals|
| `Proposal` | [QueryProposalRequest](#lbm.fbridge.v1.QueryProposalRequest) | [QueryProposalResponse](#lbm.fbridge.v1.QueryProposalResponse) | Proposal queries a SuggestRole Proposal | GET|/lbm/fbridge/v1/proposals/{proposal_id}|
| `Vote` | [QueryVoteRequest](#lbm.fbridge.v1.QueryVoteRequest) | [QueryVoteResponse](#lbm.fbridge.v1.QueryVoteResponse) | Vote queries voted information based on proposalID, voterAddr. | GET|/lbm/fbridge/v1/proposals/{proposal_id}/votes/{voter}|
| `Votes` | [QueryVotesRequest](#lbm.fbridge.v1.QueryVotesRequest) | [QueryVotesResponse](#lbm.fbridge.v1.QueryVotesResponse) | Votes queries votes of a given proposal. | GET|/lbm/fbridge/v1/proposals/{proposal_id}/votes|

<!-- end services -->

Expand Down
48 changes: 42 additions & 6 deletions proto/lbm/fbridge/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,22 @@ service Query {

// Proposals queries a list of SuggestRole Proposals
rpc Proposals(QueryProposalsRequest) returns (QueryProposalsResponse) {
option (google.api.http).get = "/lbm/fbridge/v1/role/proposals";
option (google.api.http).get = "/lbm/fbridge/v1/proposals";
}

// Proposal queries a SuggestRole Proposal
rpc Proposal(QueryProposalRequest) returns (QueryProposalResponse) {
option (google.api.http).get = "/lbm/fbridge/v1/role/proposals/{proposal_id}";
option (google.api.http).get = "/lbm/fbridge/v1/proposals/{proposal_id}";
}

// Vote queries voted information based on proposalID, voterAddr.
rpc Vote(QueryVoteRequest) returns (QueryVoteResponse) {
option (google.api.http).get = "/lbm/fbridge/v1/proposals/{proposal_id}/votes/{voter}";
}

// Votes queries votes of a given proposal.
rpc Votes(QueryVotesRequest) returns (QueryVotesResponse) {
option (google.api.http).get = "/lbm/fbridge/v1/proposals/{proposal_id}/votes";
}
}

Expand Down Expand Up @@ -172,14 +182,40 @@ message QueryProposalsRequest {
}

message QueryProposalsResponse {
repeated RoleProposal proposals = 1;
repeated RoleProposal proposals = 1 [(gogoproto.nullable) = false];

// pagination defines an pagination for the response.
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message QueryProposalRequest {
// the proposal id
string proposal_id = 1;
uint64 proposal_id = 1;
}

message QueryProposalResponse {
RoleProposal proposal = 1;
}
RoleProposal proposal = 1 [(gogoproto.nullable) = false];
}

message QueryVoteRequest {
// proposal_id defines the unique id of the proposal.
uint64 proposal_id = 1;

// voter defines the oter address for the proposals.
string voter = 2;
}

message QueryVoteResponse {
// vote defined the queried vote.
Vote vote = 1 [(gogoproto.nullable) = false];
}

message QueryVotesRequest {
// proposal_id defines the unique id of the proposal.
uint64 proposal_id = 1;
}

message QueryVotesResponse {
// votes defined the queried votes.
repeated Vote votes = 1 [(gogoproto.nullable) = false];
}
15 changes: 10 additions & 5 deletions x/fbridge/keeper/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ func (k Keeper) IterateProposals(ctx sdk.Context, cb func(proposal types.RolePro
}
}

// GetProposals returns all the role proposals from store
func (k Keeper) GetProposals(ctx sdk.Context) (proposals []types.RoleProposal) {
// GetRoleProposals returns all the role proposals from store
func (k Keeper) GetRoleProposals(ctx sdk.Context) (proposals []types.RoleProposal) {
k.IterateProposals(ctx, func(proposal types.RoleProposal) bool {
proposals = append(proposals, proposal)
return false
Expand All @@ -157,14 +157,19 @@ func (k Keeper) GetVote(ctx sdk.Context, proposalID uint64, voter sdk.AccAddress
return types.VoteOption(binary.BigEndian.Uint32(bz)), nil

Check warning on line 157 in x/fbridge/keeper/auth.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/auth.go#L157

Added line #L157 was not covered by tests
}

func (k Keeper) GetVotes(ctx sdk.Context, proposalID uint64) []types.VoteOption {
func (k Keeper) GetProposalVotes(ctx sdk.Context, proposalID uint64) []types.Vote {
store := ctx.KVStore(k.storeKey)

Check warning on line 161 in x/fbridge/keeper/auth.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/auth.go#L160-L161

Added lines #L160 - L161 were not covered by tests

votes := make([]types.VoteOption, 0)
votes := make([]types.Vote, 0)
iterator := sdk.KVStorePrefixIterator(store, types.VotesKey(proposalID))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
v := types.VoteOption(binary.BigEndian.Uint32(iterator.Value()))
_, voter := types.SplitVoterVoteKey(iterator.Key())
v := types.Vote{
ProposalId: proposalID,
Voter: voter.String(),
Option: types.VoteOption(binary.BigEndian.Uint32(iterator.Value())),

Check warning on line 171 in x/fbridge/keeper/auth.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/auth.go#L163-L171

Added lines #L163 - L171 were not covered by tests
}
votes = append(votes, v)

Check warning on line 173 in x/fbridge/keeper/auth.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/auth.go#L173

Added line #L173 was not covered by tests
}

Expand Down
4 changes: 2 additions & 2 deletions x/fbridge/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
NextSeq: k.GetNextSequence(ctx),
},
NextRoleProposalId: k.GetNextProposalID(ctx),
RoleProposals: k.GetProposals(ctx),
RoleProposals: k.GetRoleProposals(ctx),
Votes: k.GetAllVotes(ctx),
RoleMetadata: k.GetRoleMetadata(ctx),
Roles: k.GetRolePairs(ctx),

Check warning on line 42 in x/fbridge/keeper/genesis.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/genesis.go#L38-L42

Added lines #L38 - L42 were not covered by tests
Expand All @@ -47,7 +47,7 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
func (k Keeper) IterateVotes(ctx sdk.Context, cb func(proposal types.Vote) (stop bool)) {
store := ctx.KVStore(k.storeKey)

Check warning on line 48 in x/fbridge/keeper/genesis.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/genesis.go#L47-L48

Added lines #L47 - L48 were not covered by tests

iterator := sdk.KVStorePrefixIterator(store, types.KeyProposalPrefix)
iterator := sdk.KVStorePrefixIterator(store, types.KeyProposalVotePrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
id, voter := types.SplitVoterVoteKey(iterator.Key())
Expand Down
124 changes: 112 additions & 12 deletions x/fbridge/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@ package keeper

import (
"context"
"fmt"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/Finschia/finschia-sdk/store/prefix"
sdk "github.com/Finschia/finschia-sdk/types"
"github.com/Finschia/finschia-sdk/types/query"
"github.com/Finschia/finschia-sdk/x/fbridge/types"
)

var _ types.QueryServer = Keeper{}

func (k Keeper) Params(ctx context.Context, request *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
panic("implement me")
func (k Keeper) Params(goCtx context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")

Check warning on line 20 in x/fbridge/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/grpc_query.go#L18-L20

Added lines #L18 - L20 were not covered by tests
}

ctx := sdk.UnwrapSDKContext(goCtx)
params := k.GetParams(ctx)

Check warning on line 24 in x/fbridge/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/grpc_query.go#L23-L24

Added lines #L23 - L24 were not covered by tests

return &types.QueryParamsResponse{Params: params}, nil

Check warning on line 26 in x/fbridge/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/grpc_query.go#L26

Added line #L26 was not covered by tests
}

func (k Keeper) NextSeqSend(goCtx context.Context, req *types.QueryNextSeqSendRequest) (*types.QueryNextSeqSendResponse, error) {
Expand Down Expand Up @@ -51,22 +61,112 @@ func (k Keeper) Commitments(ctx context.Context, request *types.QueryCommitments
panic("implement me")
}

func (k Keeper) Guardians(ctx context.Context, request *types.QueryGuardiansRequest) (*types.QueryGuardiansResponse, error) {
panic("implement me")
func (k Keeper) Guardians(goCtx context.Context, req *types.QueryGuardiansRequest) (*types.QueryGuardiansResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")

Check warning on line 66 in x/fbridge/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/grpc_query.go#L64-L66

Added lines #L64 - L66 were not covered by tests
}

ctx := sdk.UnwrapSDKContext(goCtx)
guardians := make([]string, 0)
roles := k.GetRolePairs(ctx)
for _, pair := range roles {
if pair.Role == types.RoleGuardian {
guardians = append(guardians, pair.Address)

Check warning on line 74 in x/fbridge/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/grpc_query.go#L69-L74

Added lines #L69 - L74 were not covered by tests
}
}

return &types.QueryGuardiansResponse{Guardians: guardians}, nil

Check warning on line 78 in x/fbridge/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/grpc_query.go#L78

Added line #L78 was not covered by tests
}

func (k Keeper) Operators(ctx context.Context, request *types.QueryOperatorsRequest) (*types.QueryOperatorsResponse, error) {
panic("implement me")
func (k Keeper) Operators(goCtx context.Context, req *types.QueryOperatorsRequest) (*types.QueryOperatorsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")

Check warning on line 83 in x/fbridge/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/grpc_query.go#L81-L83

Added lines #L81 - L83 were not covered by tests
}

ctx := sdk.UnwrapSDKContext(goCtx)
operators := make([]string, 0)
roles := k.GetRolePairs(ctx)
for _, pair := range roles {
if pair.Role == types.RoleOperator {
operators = append(operators, pair.Address)

Check warning on line 91 in x/fbridge/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/grpc_query.go#L86-L91

Added lines #L86 - L91 were not covered by tests
}
}

return &types.QueryOperatorsResponse{Operators: operators}, nil

Check warning on line 95 in x/fbridge/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/grpc_query.go#L95

Added line #L95 was not covered by tests
}

func (k Keeper) Judges(ctx context.Context, request *types.QueryJudgesRequest) (*types.QueryJudgesResponse, error) {
panic("implement me")
func (k Keeper) Judges(goCtx context.Context, req *types.QueryJudgesRequest) (*types.QueryJudgesResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")

Check warning on line 100 in x/fbridge/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/grpc_query.go#L98-L100

Added lines #L98 - L100 were not covered by tests
}

ctx := sdk.UnwrapSDKContext(goCtx)
judges := make([]string, 0)
roles := k.GetRolePairs(ctx)
for _, pair := range roles {
if pair.Role == types.RoleJudge {
judges = append(judges, pair.Address)

Check warning on line 108 in x/fbridge/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/grpc_query.go#L103-L108

Added lines #L103 - L108 were not covered by tests
}
}

return &types.QueryJudgesResponse{Judges: judges}, nil

Check warning on line 112 in x/fbridge/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/grpc_query.go#L112

Added line #L112 was not covered by tests
}

func (k Keeper) Proposals(ctx context.Context, request *types.QueryProposalsRequest) (*types.QueryProposalsResponse, error) {
panic("implement me")
func (k Keeper) Proposals(goCtx context.Context, req *types.QueryProposalsRequest) (*types.QueryProposalsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")

Check warning on line 117 in x/fbridge/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/grpc_query.go#L115-L117

Added lines #L115 - L117 were not covered by tests
}

ctx := sdk.UnwrapSDKContext(goCtx)
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyProposalPrefix)
proposals := make([]types.RoleProposal, 0)
pageRes, err := query.Paginate(store, req.Pagination, func(_, value []byte) error {
var proposal types.RoleProposal
k.cdc.MustUnmarshal(value, &proposal)
proposals = append(proposals, proposal)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())

Check warning on line 130 in x/fbridge/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/grpc_query.go#L120-L130

Added lines #L120 - L130 were not covered by tests
}

return &types.QueryProposalsResponse{Proposals: proposals, Pagination: pageRes}, nil

Check warning on line 133 in x/fbridge/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/grpc_query.go#L133

Added line #L133 was not covered by tests
}

func (k Keeper) Proposal(ctx context.Context, request *types.QueryProposalRequest) (*types.QueryProposalResponse, error) {
panic("implement me")
func (k Keeper) Proposal(goCtx context.Context, req *types.QueryProposalRequest) (*types.QueryProposalResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")

Check warning on line 138 in x/fbridge/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/grpc_query.go#L136-L138

Added lines #L136 - L138 were not covered by tests
}

ctx := sdk.UnwrapSDKContext(goCtx)
proposal, found := k.GetRoleProposal(ctx, req.ProposalId)
if !found {
return nil, status.Error(codes.NotFound, fmt.Sprintf("role proposal %d", req.ProposalId))

Check warning on line 144 in x/fbridge/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/grpc_query.go#L141-L144

Added lines #L141 - L144 were not covered by tests
}

return &types.QueryProposalResponse{Proposal: proposal}, nil

Check warning on line 147 in x/fbridge/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/grpc_query.go#L147

Added line #L147 was not covered by tests
}

func (k Keeper) Votes(goCtx context.Context, req *types.QueryVotesRequest) (*types.QueryVotesResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")

Check warning on line 152 in x/fbridge/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/grpc_query.go#L150-L152

Added lines #L150 - L152 were not covered by tests
}

ctx := sdk.UnwrapSDKContext(goCtx)
votes := k.GetProposalVotes(ctx, req.ProposalId)
return &types.QueryVotesResponse{Votes: votes}, nil

Check warning on line 157 in x/fbridge/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/grpc_query.go#L155-L157

Added lines #L155 - L157 were not covered by tests
}

func (k Keeper) Vote(goCtx context.Context, req *types.QueryVoteRequest) (*types.QueryVoteResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")

Check warning on line 162 in x/fbridge/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/grpc_query.go#L160-L162

Added lines #L160 - L162 were not covered by tests
}

ctx := sdk.UnwrapSDKContext(goCtx)
opt, err := k.GetVote(ctx, req.ProposalId, sdk.MustAccAddressFromBech32(req.Voter))
if err != nil {
return nil, status.Error(codes.NotFound, err.Error())

Check warning on line 168 in x/fbridge/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/grpc_query.go#L165-L168

Added lines #L165 - L168 were not covered by tests
}

return &types.QueryVoteResponse{Vote: types.Vote{ProposalId: req.ProposalId, Voter: req.Voter, Option: opt}}, nil

Check warning on line 171 in x/fbridge/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/grpc_query.go#L171

Added line #L171 was not covered by tests
}
Loading

0 comments on commit b3d0092

Please sign in to comment.