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

Create PoC for metadata handling in ProtobufRpcClient #1378

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion packages/stargate/src/modules/authz/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ export function setupAuthzExtension(base: QueryClient): AuthzExtension {
return {
authz: {
grants: async (granter: string, grantee: string, msgTypeUrl: string, paginationKey?: Uint8Array) => {
return await queryService.Grants({
// TODO: remove this proof of concept
rpc.setMetadata(new Map([["x-cosmos-block-height", "123"]]));
const resp = await queryService.Grants({
granter: granter,
grantee: grantee,
msgTypeUrl: msgTypeUrl,
pagination: createPagination(paginationKey),
});
rpc.clearMetadata();
return resp;
},
granteeGrants: async (grantee: string, paginationKey?: Uint8Array) => {
return await queryService.GranteeGrants({
Expand Down
35 changes: 33 additions & 2 deletions packages/stargate/src/queryclient/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fromAscii, fromBech32 } from "@cosmjs/encoding";
import { Decimal, Uint64 } from "@cosmjs/math";
import { Decimal, Uint53, Uint64 } from "@cosmjs/math";
import { PageRequest } from "cosmjs-types/cosmos/base/query/v1beta1/pagination";
import Long from "long";

Expand All @@ -25,15 +25,46 @@ export function createPagination(paginationKey?: Uint8Array): PageRequest | unde
return paginationKey ? PageRequest.fromPartial({ key: paginationKey }) : undefined;
}

/**
* This interface is more or less a copy of
* https://github.com/confio/cosmjs-types/blob/v0.6.1/src/helpers.ts#L180-L182.
*
* It needs to be compatible to the `Rpc` client expected by the query services
* auto-generated in cosmjs-types.
*/
export interface ProtobufRpcClient {
// See https://github.com/grpc/grpc-go/blob/master/Documentation/grpc-metadata.md.
// To keep things simple, we only support one items per key here.
setMetadata(md: Map<string, string>): void;
// A version of setMetadata that clears all elements
clearMetadata(): void;
// Returns a copy of the current metadata
getMetadata(): Map<string, string>;
request(service: string, method: string, data: Uint8Array): Promise<Uint8Array>;
}

export function createProtobufRpcClient(base: QueryClient): ProtobufRpcClient {
const metadata = new Map<string, string>();
return {
setMetadata: (md: Map<string, string>) => {
// Override metadata without changing the reference
metadata.clear();
for (const [k, v] of md.entries()) {
metadata.set(k, v);
}
},
clearMetadata: () => {
metadata.clear();
},
getMetadata: (): Map<string, string> => {
const copies = Array.from(metadata.entries());
return new Map(copies);
},
request: async (service: string, method: string, data: Uint8Array): Promise<Uint8Array> => {
const cosmosBlockHeight = metadata.get("x-cosmos-block-height");
const queryHeight = cosmosBlockHeight ? Uint53.fromString(cosmosBlockHeight).toNumber() : undefined;
const path = `/${service}/${method}`;
const response = await base.queryAbci(path, data, undefined);
const response = await base.queryAbci(path, data, queryHeight);
return response.value;
},
};
Expand Down