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

Avoid parsing evidence from Tendermint #1012

Merged
merged 1 commit into from
Jan 26, 2022
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ and this project adheres to

- @cosmjs/stargate: The error messages for missing types in `AminoTypes` now
contain the type that was searched for ([#990]).
- @cosmjs/tendermint-rpc: Change the `Evidence` type to `any` and avoid decoding
it. The structure we had before was outdated and trying to decode it led to
exceptions at runtime when a block with actual values was encountered.
([#980])

[#990]: https://github.com/cosmos/cosmjs/pull/990
[#980]: https://github.com/cosmos/cosmjs/issues/980

## [0.27.0] - 2022-01-10

Expand Down
30 changes: 8 additions & 22 deletions packages/tendermint-rpc/src/tendermint34/adaptor/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -730,27 +730,9 @@ function decodeValidators(data: RpcValidatorsResponse): responses.ValidatorsResp
};
}

interface RpcEvidence {
readonly type: string;
readonly validator: RpcValidatorUpdate;
readonly height: string;
readonly time: string;
readonly totalVotingPower: string;
}

function decodeEvidence(data: RpcEvidence): responses.Evidence {
return {
type: assertNotEmpty(data.type),
height: Integer.parse(assertNotEmpty(data.height)),
time: Integer.parse(assertNotEmpty(data.time)),
totalVotingPower: Integer.parse(assertNotEmpty(data.totalVotingPower)),
validator: decodeValidatorUpdate(data.validator),
};
}

function decodeEvidences(ev: readonly RpcEvidence[]): readonly responses.Evidence[] {
return assertArray(ev).map(decodeEvidence);
}
// We lost track on how the evidence structure actually looks like.
// This is any now and passed to the caller untouched.
type RpcEvidence = any;

interface RpcBlock {
readonly header: RpcHeader;
Expand All @@ -759,6 +741,8 @@ interface RpcBlock {
/** Raw tx bytes, base64 encoded */
readonly txs?: readonly string[];
};
// It's currently unclear why the deep nesting is requied.
// See https://github.com/tendermint/tendermint/issues/7697.
readonly evidence?: {
readonly evidence?: readonly RpcEvidence[];
};
Expand All @@ -771,7 +755,9 @@ function decodeBlock(data: RpcBlock): responses.Block {
// { height: '0', round: 0, block_id: { hash: '', parts: [Object] }, signatures: [] }
lastCommit: data.last_commit.block_id.hash ? decodeCommit(assertObject(data.last_commit)) : null,
txs: data.data.txs ? assertArray(data.data.txs).map(fromBase64) : [],
evidence: data.evidence && may(decodeEvidences, data.evidence.evidence),
// Lift up .evidence.evidence to just .evidence
// See https://github.com/tendermint/tendermint/issues/7697
evidence: data.evidence?.evidence,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine and just pass it unvalidated (as long as it json parsed, it is fine)

};
}

Expand Down
15 changes: 8 additions & 7 deletions packages/tendermint-rpc/src/tendermint34/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,17 @@ export interface Block {
*/
readonly lastCommit: Commit | null;
readonly txs: readonly Uint8Array[];
// This field becomes non-optional in 0.28 (https://github.com/cosmos/cosmjs/issues/1011)
readonly evidence?: readonly Evidence[];
}

export interface Evidence {
readonly type: string;
readonly validator: Validator;
readonly height: number;
readonly time: number;
readonly totalVotingPower: number;
}
/**
* We lost track on how the evidence structure actually looks like.
* This is any now and passed to the caller untouched.
*
* See also https://github.com/cosmos/cosmjs/issues/980.
*/
export type Evidence = any;

export interface Commit {
readonly blockId: BlockId;
Expand Down