mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-10 21:49:15 +00:00
Merge pull request #428 from CosmWasm/tags-to-events
Tendermint RPC: Migrate tags to events
This commit is contained in:
commit
7dae3205b6
@ -54,6 +54,11 @@
|
||||
is always set.
|
||||
- @cosmjs/tendermint-rpc: Change type of `GenesisResponse.appState` to
|
||||
`Record<string, unknown> | undefined`.
|
||||
- @cosmjs/tendermint-rpc: Remove obsolete `TxData.tags` and make `TxData.events`
|
||||
non-optional. Rename `Tag` to `Attribute`.
|
||||
- @cosmjs/tendermint-rpc: Remove obsolete `BlockResultsResponse.beginBlock` and
|
||||
`.beginBlock`. The new `.beginBlockEvents` and `.endBlockEvents` now parse the
|
||||
events correctly.
|
||||
- @cosmjs/utils: Add `assertDefined`.
|
||||
- @cosmjs/faucet: Rename binary from `cosmwasm-faucet` to `cosmos-faucet`.
|
||||
|
||||
|
@ -119,7 +119,6 @@ function defaultTestSuite(rpcFactory: () => RpcClient, adaptor: Adaptor): void {
|
||||
const client = new Client(rpcFactory(), adaptor);
|
||||
|
||||
expect(await client.block()).toBeTruthy();
|
||||
expect(await client.blockResults(3)).toBeTruthy();
|
||||
expect(await client.commit(4)).toBeTruthy();
|
||||
expect(await client.genesis()).toBeTruthy();
|
||||
expect(await client.health()).toBeNull();
|
||||
@ -145,6 +144,22 @@ function defaultTestSuite(rpcFactory: () => RpcClient, adaptor: Adaptor): void {
|
||||
});
|
||||
});
|
||||
|
||||
describe("blockResults", () => {
|
||||
it("works", async () => {
|
||||
pendingWithoutTendermint();
|
||||
const client = new Client(rpcFactory(), adaptor);
|
||||
|
||||
const height = 3;
|
||||
const results = await client.blockResults(height);
|
||||
expect(results.height).toEqual(height);
|
||||
expect(results.results).toEqual([]);
|
||||
expect(results.beginBlockEvents).toEqual([]);
|
||||
expect(results.endBlockEvents).toEqual([]);
|
||||
|
||||
client.disconnect();
|
||||
});
|
||||
});
|
||||
|
||||
describe("blockchain", () => {
|
||||
it("returns latest in descending order by default", async () => {
|
||||
pendingWithoutTendermint();
|
||||
@ -485,6 +500,7 @@ function websocketTestSuite(rpcFactory: () => RpcClient, adaptor: Adaptor, appCr
|
||||
expect(event.height).toBeGreaterThan(0);
|
||||
expect(event.index).toEqual(0);
|
||||
expect(event.result).toBeTruthy();
|
||||
expect(event.result.events.length).toBeGreaterThanOrEqual(1);
|
||||
|
||||
events.push(event);
|
||||
|
||||
@ -507,12 +523,7 @@ function websocketTestSuite(rpcFactory: () => RpcClient, adaptor: Adaptor, appCr
|
||||
expect(events.length).toEqual(2);
|
||||
// Meta
|
||||
expect(events[1].height).toEqual(events[0].height + 1);
|
||||
if (events[1].result.tags && events[0].result.tags) {
|
||||
expect(events[1].result.tags).not.toEqual(events[0].result.tags);
|
||||
}
|
||||
if (events[1].result.events && events[0].result.events) {
|
||||
expect(events[1].result.events).not.toEqual(events[0].result.events);
|
||||
}
|
||||
expect(events[1].result.events).not.toEqual(events[0].result.events);
|
||||
// Content
|
||||
expect(events[0].tx).toEqual(transactionData1);
|
||||
expect(events[1].tx).toEqual(transactionData2);
|
||||
@ -536,6 +547,7 @@ function websocketTestSuite(rpcFactory: () => RpcClient, adaptor: Adaptor, appCr
|
||||
expect(event.height).toBeGreaterThan(0);
|
||||
expect(event.index).toEqual(0);
|
||||
expect(event.result).toBeTruthy();
|
||||
expect(event.result.events.length).toBeGreaterThanOrEqual(1);
|
||||
events.push(event);
|
||||
|
||||
if (events.length === 2) {
|
||||
@ -554,12 +566,7 @@ function websocketTestSuite(rpcFactory: () => RpcClient, adaptor: Adaptor, appCr
|
||||
expect(events.length).toEqual(2);
|
||||
// Meta
|
||||
expect(events[1].height).toEqual(events[0].height + 1);
|
||||
if (events[1].result.tags && events[0].result.tags) {
|
||||
expect(events[1].result.tags).not.toEqual(events[0].result.tags);
|
||||
}
|
||||
if (events[1].result.events && events[0].result.events) {
|
||||
expect(events[1].result.events).not.toEqual(events[0].result.events);
|
||||
}
|
||||
expect(events[1].result.events).not.toEqual(events[0].result.events);
|
||||
// Content
|
||||
expect(events[0].tx).toEqual(transactionData1);
|
||||
expect(events[1].tx).toEqual(transactionData2);
|
||||
|
@ -55,8 +55,8 @@ export interface BlockResultsResponse {
|
||||
readonly results: readonly TxData[];
|
||||
readonly validatorUpdates: readonly Validator[];
|
||||
readonly consensusUpdates?: ConsensusParams;
|
||||
readonly beginBlock?: readonly Tag[];
|
||||
readonly endBlock?: readonly Tag[];
|
||||
readonly beginBlockEvents: readonly Event[];
|
||||
readonly endBlockEvents: readonly Event[];
|
||||
}
|
||||
|
||||
export interface BlockchainResponse {
|
||||
@ -163,22 +163,22 @@ export const getBlockEventHeight = (event: NewBlockEvent): number => event.heade
|
||||
|
||||
// Helper items used above
|
||||
|
||||
export interface Tag {
|
||||
/** An event attribute */
|
||||
export interface Attribute {
|
||||
readonly key: Uint8Array;
|
||||
readonly value: Uint8Array;
|
||||
}
|
||||
|
||||
export interface Event {
|
||||
readonly type: string;
|
||||
readonly attributes: readonly Tag[];
|
||||
readonly attributes: readonly Attribute[];
|
||||
}
|
||||
|
||||
export interface TxData {
|
||||
readonly code: number;
|
||||
readonly log?: string;
|
||||
readonly data?: Uint8Array;
|
||||
readonly tags?: readonly Tag[];
|
||||
readonly events?: readonly Event[];
|
||||
readonly events: readonly Event[];
|
||||
// readonly fees?: any;
|
||||
}
|
||||
|
||||
|
@ -90,31 +90,31 @@ function decodeAbciQuery(data: RpcAbciQueryResponse): responses.AbciQueryRespons
|
||||
};
|
||||
}
|
||||
|
||||
interface RpcTag {
|
||||
interface RpcAttribute {
|
||||
readonly key: Base64String;
|
||||
readonly value: Base64String;
|
||||
}
|
||||
|
||||
function decodeTag(tag: RpcTag): responses.Tag {
|
||||
function decodeAttribute(attribute: RpcAttribute): responses.Attribute {
|
||||
return {
|
||||
key: Base64.decode(assertNotEmpty(tag.key)),
|
||||
value: Base64.decode(assertNotEmpty(tag.value)),
|
||||
key: Base64.decode(assertNotEmpty(attribute.key)),
|
||||
value: Base64.decode(assertNotEmpty(attribute.value)),
|
||||
};
|
||||
}
|
||||
|
||||
function decodeTags(tags: readonly RpcTag[]): readonly responses.Tag[] {
|
||||
return assertArray(tags).map(decodeTag);
|
||||
function decodeAttributes(attributes: readonly RpcAttribute[]): responses.Attribute[] {
|
||||
return assertArray(attributes).map(decodeAttribute);
|
||||
}
|
||||
|
||||
interface RpcEvent {
|
||||
readonly type: string;
|
||||
readonly attributes: readonly RpcTag[];
|
||||
readonly attributes: readonly RpcAttribute[];
|
||||
}
|
||||
|
||||
function decodeEvent(event: RpcEvent): responses.Event {
|
||||
return {
|
||||
type: event.type,
|
||||
attributes: decodeTags(event.attributes),
|
||||
attributes: decodeAttributes(event.attributes),
|
||||
};
|
||||
}
|
||||
|
||||
@ -126,7 +126,7 @@ interface RpcTxData {
|
||||
readonly code?: number;
|
||||
readonly log?: string;
|
||||
readonly data?: Base64String;
|
||||
readonly events?: readonly RpcEvent[];
|
||||
readonly events: readonly RpcEvent[];
|
||||
}
|
||||
|
||||
function decodeTxData(data: RpcTxData): responses.TxData {
|
||||
@ -134,7 +134,7 @@ function decodeTxData(data: RpcTxData): responses.TxData {
|
||||
data: may(Base64.decode, data.data),
|
||||
log: data.log,
|
||||
code: Integer.parse(assertNumber(optional<number>(data.code, 0))),
|
||||
events: may(decodeEvents, data.events),
|
||||
events: decodeEvents(data.events),
|
||||
};
|
||||
}
|
||||
|
||||
@ -234,22 +234,20 @@ function decodeConsensusParams(data: RpcConsensusParams): responses.ConsensusPar
|
||||
interface RpcBlockResultsResponse {
|
||||
readonly height: IntegerString;
|
||||
readonly txs_results: readonly RpcTxData[] | null;
|
||||
readonly begin_block_events: null;
|
||||
readonly end_block_events: null;
|
||||
readonly validator_updates: null;
|
||||
readonly consensus_param_updates: null;
|
||||
readonly begin_block_events: readonly RpcEvent[] | null;
|
||||
readonly end_block_events: readonly RpcEvent[] | null;
|
||||
readonly validator_updates: readonly RpcValidatorUpdate[] | null;
|
||||
readonly consensus_param_updates: RpcConsensusParams | null;
|
||||
}
|
||||
|
||||
function decodeBlockResults(data: RpcBlockResultsResponse): responses.BlockResultsResponse {
|
||||
const results = optional(data.txs_results, [] as readonly RpcTxData[]);
|
||||
const validatorUpdates = optional(data.validator_updates, [] as readonly RpcValidatorUpdate[]);
|
||||
return {
|
||||
height: Integer.parse(assertNotEmpty(data.height)),
|
||||
results: results.map(decodeTxData),
|
||||
validatorUpdates: validatorUpdates.map(decodeValidatorUpdate),
|
||||
results: (data.txs_results || []).map(decodeTxData),
|
||||
validatorUpdates: (data.validator_updates || []).map(decodeValidatorUpdate),
|
||||
consensusUpdates: may(decodeConsensusParams, data.consensus_param_updates),
|
||||
beginBlock: may(decodeTags, data.begin_block_events),
|
||||
endBlock: may(decodeTags, data.end_block_events),
|
||||
beginBlockEvents: decodeEvents(data.begin_block_events || []),
|
||||
endBlockEvents: decodeEvents(data.end_block_events || []),
|
||||
};
|
||||
}
|
||||
|
||||
|
12
packages/tendermint-rpc/types/responses.d.ts
vendored
12
packages/tendermint-rpc/types/responses.d.ts
vendored
@ -47,8 +47,8 @@ export interface BlockResultsResponse {
|
||||
readonly results: readonly TxData[];
|
||||
readonly validatorUpdates: readonly Validator[];
|
||||
readonly consensusUpdates?: ConsensusParams;
|
||||
readonly beginBlock?: readonly Tag[];
|
||||
readonly endBlock?: readonly Tag[];
|
||||
readonly beginBlockEvents: readonly Event[];
|
||||
readonly endBlockEvents: readonly Event[];
|
||||
}
|
||||
export interface BlockchainResponse {
|
||||
readonly lastHeight: number;
|
||||
@ -126,20 +126,20 @@ export interface TxEvent {
|
||||
export declare const getTxEventHeight: (event: TxEvent) => number;
|
||||
export declare const getHeaderEventHeight: (event: NewBlockHeaderEvent) => number;
|
||||
export declare const getBlockEventHeight: (event: NewBlockEvent) => number;
|
||||
export interface Tag {
|
||||
/** An event attribute */
|
||||
export interface Attribute {
|
||||
readonly key: Uint8Array;
|
||||
readonly value: Uint8Array;
|
||||
}
|
||||
export interface Event {
|
||||
readonly type: string;
|
||||
readonly attributes: readonly Tag[];
|
||||
readonly attributes: readonly Attribute[];
|
||||
}
|
||||
export interface TxData {
|
||||
readonly code: number;
|
||||
readonly log?: string;
|
||||
readonly data?: Uint8Array;
|
||||
readonly tags?: readonly Tag[];
|
||||
readonly events?: readonly Event[];
|
||||
readonly events: readonly Event[];
|
||||
}
|
||||
export interface TxProof {
|
||||
readonly data: Uint8Array;
|
||||
|
Loading…
x
Reference in New Issue
Block a user