Skip to content

Commit

Permalink
Merge pull request #1290 from cosmos/test-MsgCreateValidator
Browse files Browse the repository at this point in the history
Test and fix MsgCreateValidator and MsgEditValidator
  • Loading branch information
webmaster128 committed Oct 13, 2022
2 parents 27c1e90 + 00ee040 commit bb5ef03
Show file tree
Hide file tree
Showing 7 changed files with 401 additions and 55 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Expand Up @@ -14,6 +14,8 @@ and this project adheres to
`anyToSinglePubkey`. Export `anyToSinglePubkey`.
- @cosmjs/utils: Add `isDefined` which checks for `undefined` in a
TypeScript-friendly way.
- @cosmjs/stargate: Add missing `{is,}MsgBeginRedelegateEncodeObject`,
`{is,MsgCreateValidatorEncodeObject}` and `{is,MsgEditValidatorEncodeObject}`.

### Fixed

Expand All @@ -22,9 +24,14 @@ and this project adheres to
`CosmWasmClient.queryContractSmart`, `SigningCosmWasmClient.instantiate`,
`SigningCosmWasmClient.migrate`, `SigningCosmWasmClient.execute`). This
reverts the type change done in CosmJS 0.23.0. ([#1281], [#1284])
- @cosmjs/cosmwasm-stargate: `AminoMsgCreateValidator` and
`createStakingAminoConverters` were fixed after testing both
`MsgCreateValidator` and `MsgEditValidator` in sign mode direct and Amino JSON
([#1290]).

[#1281]: https://github.com/cosmos/cosmjs/pull/1281
[#1284]: https://github.com/cosmos/cosmjs/pull/1284
[#1290]: https://github.com/cosmos/cosmjs/pull/1290

## [0.29.1] - 2022-10-09

Expand Down
6 changes: 6 additions & 0 deletions packages/stargate/src/index.ts
Expand Up @@ -61,8 +61,11 @@ export {
isAminoMsgVoteWeighted,
isAminoMsgWithdrawDelegatorReward,
isAminoMsgWithdrawValidatorCommission,
isMsgBeginRedelegateEncodeObject,
isMsgCreateValidatorEncodeObject,
isMsgDelegateEncodeObject,
isMsgDepositEncodeObject,
isMsgEditValidatorEncodeObject,
isMsgSendEncodeObject,
isMsgSubmitProposalEncodeObject,
isMsgTransferEncodeObject,
Expand All @@ -72,8 +75,11 @@ export {
isMsgWithdrawDelegatorRewardEncodeObject,
MintExtension,
MintParams,
MsgBeginRedelegateEncodeObject,
MsgCreateValidatorEncodeObject,
MsgDelegateEncodeObject,
MsgDepositEncodeObject,
MsgEditValidatorEncodeObject,
MsgSendEncodeObject,
MsgSubmitProposalEncodeObject,
MsgTransferEncodeObject,
Expand Down
6 changes: 6 additions & 0 deletions packages/stargate/src/modules/index.ts
Expand Up @@ -84,9 +84,15 @@ export {
isAminoMsgUndelegate,
} from "./staking/aminomessages";
export {
isMsgBeginRedelegateEncodeObject,
isMsgCreateValidatorEncodeObject,
isMsgDelegateEncodeObject,
isMsgEditValidatorEncodeObject,
isMsgUndelegateEncodeObject,
MsgBeginRedelegateEncodeObject,
MsgCreateValidatorEncodeObject,
MsgDelegateEncodeObject,
MsgEditValidatorEncodeObject,
MsgUndelegateEncodeObject,
stakingTypes,
} from "./staking/messages";
Expand Down
66 changes: 39 additions & 27 deletions packages/stargate/src/modules/staking/aminomessages.spec.ts
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { encodeBech32Pubkey } from "@cosmjs/amino";
import { fromBase64 } from "@cosmjs/encoding";
import { coin } from "@cosmjs/proto-signing";
import { PubKey as CosmosCryptoSecp256k1Pubkey } from "cosmjs-types/cosmos/crypto/secp256k1/keys";
import {
MsgBeginRedelegate,
MsgCreateValidator,
Expand Down Expand Up @@ -56,16 +56,22 @@ describe("AminoTypes", () => {
details: "...",
},
commission: {
rate: "0.2",
maxRate: "0.3",
maxChangeRate: "0.1",
rate: "200000000000000000", // 0.2
maxRate: "300000000000000000", // 0.3
maxChangeRate: "100000000000000000", // 0.1
},
minSelfDelegation: "123",
delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5",
pubkey: {
typeUrl: "/cosmos.crypto.secp256k1.PubKey",
value: fromBase64("A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ"),
value: Uint8Array.from(
CosmosCryptoSecp256k1Pubkey.encode(
CosmosCryptoSecp256k1Pubkey.fromPartial({
key: fromBase64("A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ"),
}),
).finish(),
),
},
value: coin(1234, "ucosm"),
};
Expand All @@ -85,17 +91,17 @@ describe("AminoTypes", () => {
details: "...",
},
commission: {
rate: "0.2",
max_rate: "0.3",
max_change_rate: "0.1",
rate: "0.200000000000000000",
max_rate: "0.300000000000000000",
max_change_rate: "0.100000000000000000",
},
min_self_delegation: "123",
delegator_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5",
pubkey: encodeBech32Pubkey(
{ type: "tendermint/PubKeySecp256k1", value: "A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ" },
"cosmos",
),
pubkey: {
type: "tendermint/PubKeySecp256k1",
value: "A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ",
},
value: coin(1234, "ucosm"),
},
};
Expand Down Expand Up @@ -133,7 +139,7 @@ describe("AminoTypes", () => {
securityContact: "Hamburglar",
details: "...",
},
commissionRate: "0.2",
commissionRate: "21000000000000000", // 0.021
minSelfDelegation: "123",
validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5",
};
Expand All @@ -152,7 +158,7 @@ describe("AminoTypes", () => {
security_contact: "Hamburglar",
details: "...",
},
commission_rate: "0.2",
commission_rate: "0.021000000000000000",
min_self_delegation: "123",
validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5",
},
Expand Down Expand Up @@ -219,17 +225,17 @@ describe("AminoTypes", () => {
details: "...",
},
commission: {
rate: "0.2",
max_rate: "0.3",
max_change_rate: "0.1",
rate: "0.200000000000000000",
max_rate: "0.300000000000000000",
max_change_rate: "0.100000000000000000",
},
min_self_delegation: "123",
delegator_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5",
pubkey: encodeBech32Pubkey(
{ type: "tendermint/PubKeySecp256k1", value: "A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ" },
"cosmos",
),
pubkey: {
type: "tendermint/PubKeySecp256k1",
value: "A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ",
},
value: coin(1234, "ucosm"),
},
};
Expand All @@ -243,16 +249,22 @@ describe("AminoTypes", () => {
details: "...",
},
commission: {
rate: "0.2",
maxRate: "0.3",
maxChangeRate: "0.1",
rate: "200000000000000000", // 0.2
maxRate: "300000000000000000", // 0.3
maxChangeRate: "100000000000000000", // 0.1
},
minSelfDelegation: "123",
delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5",
pubkey: {
typeUrl: "/cosmos.crypto.secp256k1.PubKey",
value: fromBase64("A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ"),
value: Uint8Array.from(
CosmosCryptoSecp256k1Pubkey.encode(
CosmosCryptoSecp256k1Pubkey.fromPartial({
key: fromBase64("A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ"),
}),
).finish(),
),
},
value: coin(1234, "ucosm"),
};
Expand Down Expand Up @@ -294,7 +306,7 @@ describe("AminoTypes", () => {
security_contact: "Hamburglar",
details: "...",
},
commission_rate: "0.2",
commission_rate: "0.050000000000000000", // 0.05
min_self_delegation: "123",
validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5",
},
Expand All @@ -308,7 +320,7 @@ describe("AminoTypes", () => {
securityContact: "Hamburglar",
details: "...",
},
commissionRate: "0.2",
commissionRate: "50000000000000000", // 0.05
minSelfDelegation: "123",
validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5",
};
Expand Down
55 changes: 27 additions & 28 deletions packages/stargate/src/modules/staking/aminomessages.ts
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { AminoMsg, Coin, decodeBech32Pubkey, encodeBech32Pubkey } from "@cosmjs/amino";
import { fromBase64, toBase64 } from "@cosmjs/encoding";
import { AminoMsg, Coin, Pubkey } from "@cosmjs/amino";
import { Decimal } from "@cosmjs/math";
import { anyToSinglePubkey, encodePubkey } from "@cosmjs/proto-signing";
import { assertDefinedAndNotNull } from "@cosmjs/utils";
import {
MsgBeginRedelegate,
Expand Down Expand Up @@ -28,6 +29,17 @@ interface Description {
readonly details: string;
}

function protoDecimalToJson(decimal: string): string {
const parsed = Decimal.fromAtomics(decimal, 18);
const [whole, fractional] = parsed.toString().split(".");
return `${whole}.${fractional.padEnd(18, "0")}`;
}

function jsonDecimalToProto(decimal: string): string {
const parsed = Decimal.fromUserInput(decimal, 18);
return parsed.atomics;
}

/** Creates a new validator. */
export interface AminoMsgCreateValidator extends AminoMsg {
readonly type: "cosmos-sdk/MsgCreateValidator";
Expand All @@ -39,8 +51,8 @@ export interface AminoMsgCreateValidator extends AminoMsg {
readonly delegator_address: string;
/** Bech32 encoded validator address */
readonly validator_address: string;
/** Bech32 encoded public key */
readonly pubkey: string;
/** Public key */
readonly pubkey: Pubkey;
readonly value: Coin;
};
}
Expand Down Expand Up @@ -120,7 +132,7 @@ export function isAminoMsgUndelegate(msg: AminoMsg): msg is AminoMsgUndelegate {
}

export function createStakingAminoConverters(
prefix: string,
_prefix: string,
): Record<string, AminoConverter | "not_supported_by_chain"> {
return {
"/cosmos.staking.v1beta1.MsgBeginRedelegate": {
Expand Down Expand Up @@ -175,20 +187,14 @@ export function createStakingAminoConverters(
details: description.details,
},
commission: {
rate: commission.rate,
max_rate: commission.maxRate,
max_change_rate: commission.maxChangeRate,
rate: protoDecimalToJson(commission.rate),
max_rate: protoDecimalToJson(commission.maxRate),
max_change_rate: protoDecimalToJson(commission.maxChangeRate),
},
min_self_delegation: minSelfDelegation,
delegator_address: delegatorAddress,
validator_address: validatorAddress,
pubkey: encodeBech32Pubkey(
{
type: "tendermint/PubKeySecp256k1",
value: toBase64(pubkey.value),
},
prefix,
),
pubkey: anyToSinglePubkey(pubkey),
value: value,
};
},
Expand All @@ -201,10 +207,6 @@ export function createStakingAminoConverters(
pubkey,
value,
}: AminoMsgCreateValidator["value"]): MsgCreateValidator => {
const decodedPubkey = decodeBech32Pubkey(pubkey);
if (decodedPubkey.type !== "tendermint/PubKeySecp256k1") {
throw new Error("Only Secp256k1 public keys are supported");
}
return {
description: {
moniker: description.moniker,
Expand All @@ -214,17 +216,14 @@ export function createStakingAminoConverters(
details: description.details,
},
commission: {
rate: commission.rate,
maxRate: commission.max_rate,
maxChangeRate: commission.max_change_rate,
rate: jsonDecimalToProto(commission.rate),
maxRate: jsonDecimalToProto(commission.max_rate),
maxChangeRate: jsonDecimalToProto(commission.max_change_rate),
},
minSelfDelegation: min_self_delegation,
delegatorAddress: delegator_address,
validatorAddress: validator_address,
pubkey: {
typeUrl: "/cosmos.crypto.secp256k1.PubKey",
value: fromBase64(decodedPubkey.value),
},
pubkey: encodePubkey(pubkey),
value: value,
};
},
Expand Down Expand Up @@ -266,7 +265,7 @@ export function createStakingAminoConverters(
security_contact: description.securityContact,
details: description.details,
},
commission_rate: commissionRate,
commission_rate: protoDecimalToJson(commissionRate),
min_self_delegation: minSelfDelegation,
validator_address: validatorAddress,
};
Expand All @@ -284,7 +283,7 @@ export function createStakingAminoConverters(
securityContact: description.security_contact,
details: description.details,
},
commissionRate: commission_rate,
commissionRate: jsonDecimalToProto(commission_rate),
minSelfDelegation: min_self_delegation,
validatorAddress: validator_address,
}),
Expand Down

0 comments on commit bb5ef03

Please sign in to comment.