From cb2abf0592f188f65652cc7d296afd98726d2f07 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 13 Oct 2022 16:30:26 +0200 Subject: [PATCH] Test MsgEditValidator --- .../src/modules/staking/messages.spec.ts | 165 +++++++++++++++++- 1 file changed, 159 insertions(+), 6 deletions(-) diff --git a/packages/stargate/src/modules/staking/messages.spec.ts b/packages/stargate/src/modules/staking/messages.spec.ts index d49c421b60..a8694fcfad 100644 --- a/packages/stargate/src/modules/staking/messages.spec.ts +++ b/packages/stargate/src/modules/staking/messages.spec.ts @@ -3,10 +3,17 @@ import { Random } from "@cosmjs/crypto"; import { fromBech32, toBase64, toBech32 } from "@cosmjs/encoding"; import { DirectSecp256k1HdWallet, encodePubkey } from "@cosmjs/proto-signing"; +import { calculateFee } from "../../fee"; import { SigningStargateClient } from "../../signingstargateclient"; -import { assertIsDeliverTxSuccess } from "../../stargateclient"; -import { defaultSigningClientOptions, faucet, pendingWithoutSimapp, simapp } from "../../testutils.spec"; -import { MsgCreateValidatorEncodeObject } from "./messages"; +import { assertIsDeliverTxFailure, assertIsDeliverTxSuccess } from "../../stargateclient"; +import { + defaultGasPrice, + defaultSigningClientOptions, + faucet, + pendingWithoutSimapp, + simapp, +} from "../../testutils.spec"; +import { MsgCreateValidatorEncodeObject, MsgEditValidatorEncodeObject } from "./messages"; function changePrefix(address: string, newPrefix: string): string { return toBech32(newPrefix, fromBech32(address).data); @@ -24,7 +31,7 @@ async function sendFeeAndStakingTokens(address: string): Promise { const res = await client.sendTokens( firstAccount.address, address, - [coin(5000, simapp.denomFee), coin(28, simapp.denomStaking)], + [coin(11000, simapp.denomFee), coin(28, simapp.denomStaking)], "auto", ); assertIsDeliverTxSuccess(res); @@ -32,6 +39,9 @@ async function sendFeeAndStakingTokens(address: string): Promise { } describe("staking messages", () => { + const createFee = calculateFee(200_000, defaultGasPrice); + const editFee = calculateFee(200_000, defaultGasPrice); + describe("MsgCreateValidator", () => { it("works", async () => { pendingWithoutSimapp(); @@ -76,7 +86,7 @@ describe("staking messages", () => { }, }, }; - const result = await client.signAndBroadcast(valAccount.address, [createMsg], "auto"); + const result = await client.signAndBroadcast(valAccount.address, [createMsg], createFee); assertIsDeliverTxSuccess(result); @@ -126,10 +136,153 @@ describe("staking messages", () => { }, }, }; - const result = await client.signAndBroadcast(valAccount.address, [createMsg], "auto"); + const result = await client.signAndBroadcast(valAccount.address, [createMsg], createFee); + + assertIsDeliverTxSuccess(result); + + client.disconnect(); + }); + }); + + describe("MsgEditValidator", () => { + it("works", async () => { + pendingWithoutSimapp(); + + const valWallet = await DirectSecp256k1HdWallet.generate(); + const [valAccount] = await valWallet.getAccounts(); + + await sendFeeAndStakingTokens(valAccount.address); + + const client = await SigningStargateClient.connectWithSigner( + simapp.tendermintUrl, + valWallet, + defaultSigningClientOptions, + ); + + const createMsg: MsgCreateValidatorEncodeObject = { + typeUrl: "/cosmos.staking.v1beta1.MsgCreateValidator", + value: { + description: { + moniker: "That's me", + identity: "AABB1234", + website: "http://example.com/me", + details: "What should I write?", + securityContact: "DM on Twitter", + }, + commission: { + maxChangeRate: "10000000000000000", // 0.01 + maxRate: "200000000000000000", // 0.2 + rate: "100000000000000000", // 0.1 + }, + minSelfDelegation: "1", + // Those two addresses need to be the same with different prefix 🤷‍♂️ + delegatorAddress: valAccount.address, + validatorAddress: changePrefix(valAccount.address, "cosmosvaloper"), + pubkey: encodePubkey({ + type: "tendermint/PubKeyEd25519", + value: toBase64(Random.getBytes(32)), + }), + value: { + amount: "1", + denom: simapp.denomStaking, + }, + }, + }; + const result = await client.signAndBroadcast(valAccount.address, [createMsg], createFee); + assertIsDeliverTxSuccess(result); + + const editMsg: MsgEditValidatorEncodeObject = { + typeUrl: "/cosmos.staking.v1beta1.MsgEditValidator", + value: { + commissionRate: "100000000000000000", // we cannot change until 24h have passed + description: { + moniker: "new name", + identity: "ZZZZ", + website: "http://example.com/new-site", + details: "Still no idea", + securityContact: "DM on Discord", + }, + minSelfDelegation: "1", + validatorAddress: changePrefix(valAccount.address, "cosmosvaloper"), // unchanged + }, + }; + const editResult = await client.signAndBroadcast(valAccount.address, [editMsg], editFee); + + // Currently we have no way to unset commissionRate, so the DeliverTx is expected to fail + // with "commission cannot be changed more than once in 24h" :( + assertIsDeliverTxFailure(editResult); + + client.disconnect(); + }); + it("works with Amino JSON sign mode", async () => { + pendingWithoutSimapp(); + + const valWallet = await Secp256k1HdWallet.generate(); + const [valAccount] = await valWallet.getAccounts(); + + await sendFeeAndStakingTokens(valAccount.address); + + const client = await SigningStargateClient.connectWithSigner( + simapp.tendermintUrl, + valWallet, + defaultSigningClientOptions, + ); + + const createMsg: MsgCreateValidatorEncodeObject = { + typeUrl: "/cosmos.staking.v1beta1.MsgCreateValidator", + value: { + description: { + moniker: "That's me", + identity: "AABB1234", + website: "http://example.com/me", + details: "What should I write?", + securityContact: "DM on Twitter", + }, + commission: { + maxChangeRate: "10000000000000000", // 0.01 + maxRate: "200000000000000000", // 0.2 + rate: "100000000000000000", // 0.1 + }, + minSelfDelegation: "1", + // Those two addresses need to be the same with different prefix 🤷‍♂️ + delegatorAddress: valAccount.address, + validatorAddress: changePrefix(valAccount.address, "cosmosvaloper"), + pubkey: encodePubkey({ + type: "tendermint/PubKeyEd25519", + value: toBase64(Random.getBytes(32)), + }), + value: { + amount: "1", + denom: simapp.denomStaking, + }, + }, + }; + const result = await client.signAndBroadcast(valAccount.address, [createMsg], createFee); assertIsDeliverTxSuccess(result); + const editMsg: MsgEditValidatorEncodeObject = { + typeUrl: "/cosmos.staking.v1beta1.MsgEditValidator", + value: { + commissionRate: "100000000000000000", // we cannot change until 24h have passed + description: { + moniker: "new name", + identity: "ZZZZ", + website: "http://example.com/new-site", + details: "Still no idea", + securityContact: "DM on Discord", + }, + minSelfDelegation: "1", + validatorAddress: changePrefix(valAccount.address, "cosmosvaloper"), // unchanged + }, + }; + const editResult = await client.signAndBroadcast(valAccount.address, [editMsg], editFee); + + // Currently we have no way to unset commissionRate, so the DeliverTx is expected to fail + // with "commission cannot be changed more than once in 24h" :( + // assertIsDeliverTxSuccess(editResult); + assertIsDeliverTxFailure(editResult); + client.disconnect(); }); });