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

fix: decoding validator update from slashing #1178

Merged
merged 4 commits into from
Jun 21, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ and this project adheres to

- @cosmjs/stargate: Fix valid values of `BondStatusString` for `validators`
query ([#1170]).
- @cosmjs/tendermint-rpc: Fix decoding validator updates due to slashing ([#1177]).

[#1170]: https://github.com/cosmos/cosmjs/issues/1170
[#1177]: https://github.com/cosmos/cosmjs/issues/1177

### Changed

Expand Down
21 changes: 21 additions & 0 deletions packages/tendermint-rpc/src/tendermint34/adaptor/responses.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,27 @@ describe("Adaptor Responses", () => {
votingPower: 11418237,
});
});

it("works for block results format without voting power", () => {
// from https://rpc.cosmos.network/block_results?height=10883046
const update = decodeValidatorUpdate({
pub_key: {
Sum: {
type: "tendermint.crypto.PublicKey_Ed25519",
value: {
ed25519: "HjSC7VkhKih6xMhudlqfaFE8ZZnP8RKJPv4iqR7RhcE=",
},
},
},
});
expect(update).toEqual({
pubkey: {
algorithm: "ed25519",
data: fromBase64("HjSC7VkhKih6xMhudlqfaFE8ZZnP8RKJPv4iqR7RhcE="),
},
votingPower: 0,
});
});
});

describe("decodeValidatorInfo", () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/tendermint-rpc/src/tendermint34/adaptor/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,14 @@ function decodeConsensusParams(data: RpcConsensusParams): responses.ConsensusPar
// for block results
interface RpcValidatorUpdate {
readonly pub_key: RpcPubkey;
readonly power: string;
// When omitted, this means zero (see https://github.com/cosmos/cosmjs/issues/1177#issuecomment-1160115080)
readonly power?: string;
blorgon1 marked this conversation as resolved.
Show resolved Hide resolved
}

export function decodeValidatorUpdate(data: RpcValidatorUpdate): responses.ValidatorUpdate {
return {
pubkey: decodePubkey(assertObject(data.pub_key)),
votingPower: Integer.parse(assertNotEmpty(data.power)),
votingPower: Integer.parse(data.power ?? 0),
};
}

Expand Down
21 changes: 21 additions & 0 deletions packages/tendermint-rpc/src/tendermint35/adaptor/responses.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,27 @@ describe("Adaptor Responses", () => {
votingPower: 11418237,
});
});

it("works for block results format without voting power", () => {
// from https://rpc.cosmos.network/block_results?height=10883046
const update = decodeValidatorUpdate({
pub_key: {
Sum: {
type: "tendermint.crypto.PublicKey_Ed25519",
value: {
ed25519: "HjSC7VkhKih6xMhudlqfaFE8ZZnP8RKJPv4iqR7RhcE=",
},
},
},
});
expect(update).toEqual({
pubkey: {
algorithm: "ed25519",
data: fromBase64("HjSC7VkhKih6xMhudlqfaFE8ZZnP8RKJPv4iqR7RhcE="),
},
votingPower: 0,
});
});
});

describe("decodeValidatorInfo", () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/tendermint-rpc/src/tendermint35/adaptor/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,14 @@ function decodeConsensusParams(data: RpcConsensusParams): responses.ConsensusPar
// for block results
interface RpcValidatorUpdate {
readonly pub_key: RpcPubkey;
readonly power: string;
// When omitted, this means zero (see https://github.com/cosmos/cosmjs/issues/1177#issuecomment-1160115080)
readonly power?: string;
blorgon1 marked this conversation as resolved.
Show resolved Hide resolved
}

export function decodeValidatorUpdate(data: RpcValidatorUpdate): responses.ValidatorUpdate {
return {
pubkey: decodePubkey(assertObject(data.pub_key)),
votingPower: Integer.parse(assertNotEmpty(data.power)),
votingPower: Integer.parse(data.power ?? 0),
};
}

Expand Down