|
| 1 | +package interop |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "cosmossdk.io/math" |
| 7 | + ismtypes "github.com/bcp-innovations/hyperlane-cosmos/x/core/01_interchain_security/types" |
| 8 | + abci "github.com/cometbft/cometbft/abci/types" |
| 9 | + cosmostx "github.com/cosmos/cosmos-sdk/client/tx" |
| 10 | + "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" |
| 11 | + "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" |
| 12 | + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" |
| 13 | + multisigtypes "github.com/cosmos/cosmos-sdk/crypto/types/multisig" |
| 14 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 15 | + "github.com/cosmos/cosmos-sdk/types/tx/signing" |
| 16 | + authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" |
| 17 | + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" |
| 18 | +) |
| 19 | + |
| 20 | +// TestMultisigSetRoutingIsmDomain is a regression test for |
| 21 | +// https://github.com/celestiaorg/celestia-app/issues/6541. |
| 22 | +// MsgSetRoutingIsmDomain previously had a proto descriptor registration issue |
| 23 | +// that caused multisig transactions (which use SIGN_MODE_LEGACY_AMINO_JSON) |
| 24 | +// to fail with "unknown protobuf field" errors. |
| 25 | +func (s *HyperlaneTestSuite) TestMultisigSetRoutingIsmDomain() { |
| 26 | + chain := s.celestia |
| 27 | + |
| 28 | + // Generate two private keys and create a 2-of-2 multisig public key. |
| 29 | + priv1 := secp256k1.GenPrivKey() |
| 30 | + priv2 := secp256k1.GenPrivKey() |
| 31 | + pubKeys := []cryptotypes.PubKey{priv1.PubKey(), priv2.PubKey()} |
| 32 | + msigPubKey := multisig.NewLegacyAminoPubKey(2, pubKeys) |
| 33 | + msigAddr := sdk.AccAddress(msigPubKey.Address()) |
| 34 | + |
| 35 | + // Fund the multisig account. The test infra uses "stake" as the default |
| 36 | + // bond denom. |
| 37 | + fundMsg := &banktypes.MsgSend{ |
| 38 | + FromAddress: chain.SenderAccount.GetAddress().String(), |
| 39 | + ToAddress: msigAddr.String(), |
| 40 | + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(1_000_000))), |
| 41 | + } |
| 42 | + _, err := chain.SendMsgs(fundMsg) |
| 43 | + s.Require().NoError(err) |
| 44 | + |
| 45 | + // Create a noop ISM to use as the route target. |
| 46 | + noopIsmID := s.SetupNoopISM(chain) |
| 47 | + |
| 48 | + // Create a routing ISM (owned by the default sender). |
| 49 | + msgCreateRoutingIsm := &ismtypes.MsgCreateRoutingIsm{ |
| 50 | + Creator: chain.SenderAccount.GetAddress().String(), |
| 51 | + } |
| 52 | + res, err := chain.SendMsgs(msgCreateRoutingIsm) |
| 53 | + s.Require().NoError(err) |
| 54 | + |
| 55 | + var createResp ismtypes.MsgCreateRoutingIsmResponse |
| 56 | + err = unmarshalMsgResponses(chain.Codec, res.GetData(), &createResp) |
| 57 | + s.Require().NoError(err) |
| 58 | + routingIsmID := createResp.Id |
| 59 | + |
| 60 | + // Transfer routing ISM ownership to the multisig address. |
| 61 | + msgTransferOwnership := &ismtypes.MsgUpdateRoutingIsmOwner{ |
| 62 | + IsmId: routingIsmID, |
| 63 | + Owner: chain.SenderAccount.GetAddress().String(), |
| 64 | + NewOwner: msigAddr.String(), |
| 65 | + } |
| 66 | + _, err = chain.SendMsgs(msgTransferOwnership) |
| 67 | + s.Require().NoError(err) |
| 68 | + |
| 69 | + // Build the MsgSetRoutingIsmDomain from the multisig. |
| 70 | + msgSetDomain := &ismtypes.MsgSetRoutingIsmDomain{ |
| 71 | + IsmId: routingIsmID, |
| 72 | + Owner: msigAddr.String(), |
| 73 | + Route: ismtypes.Route{ |
| 74 | + Ism: noopIsmID, |
| 75 | + Domain: 1, |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + // Sign the transaction with the multisig using SIGN_MODE_LEGACY_AMINO_JSON |
| 80 | + // (this is what real multisig accounts use). |
| 81 | + celestiaApp := s.GetCelestiaApp(chain) |
| 82 | + txCfg := celestiaApp.GetTxConfig() |
| 83 | + txBuilder := txCfg.NewTxBuilder() |
| 84 | + |
| 85 | + err = txBuilder.SetMsgs(msgSetDomain) |
| 86 | + s.Require().NoError(err) |
| 87 | + txBuilder.SetGasLimit(400_000) |
| 88 | + txBuilder.SetFeeAmount(sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(0)))) |
| 89 | + |
| 90 | + // Look up the multisig account to get its account number and sequence. |
| 91 | + msigAcc := celestiaApp.AccountKeeper.GetAccount(chain.GetContext(), msigAddr) |
| 92 | + s.Require().NotNil(msigAcc, "multisig account should exist after funding") |
| 93 | + accNum := msigAcc.GetAccountNumber() |
| 94 | + accSeq := msigAcc.GetSequence() |
| 95 | + |
| 96 | + signMode := signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON |
| 97 | + |
| 98 | + // Round 1: Set an empty multisig signature to populate signer infos on the |
| 99 | + // tx builder, so that sign bytes are computed correctly. |
| 100 | + emptyMsigData := multisigtypes.NewMultisig(len(pubKeys)) |
| 101 | + emptySig := signing.SignatureV2{ |
| 102 | + PubKey: msigPubKey, |
| 103 | + Data: emptyMsigData, |
| 104 | + Sequence: accSeq, |
| 105 | + } |
| 106 | + err = txBuilder.SetSignatures(emptySig) |
| 107 | + s.Require().NoError(err) |
| 108 | + |
| 109 | + // Round 2: Each key signs the transaction. |
| 110 | + signerData := authsigning.SignerData{ |
| 111 | + Address: msigAddr.String(), |
| 112 | + ChainID: chain.ChainID, |
| 113 | + AccountNumber: accNum, |
| 114 | + Sequence: accSeq, |
| 115 | + PubKey: msigPubKey, |
| 116 | + } |
| 117 | + |
| 118 | + sig1, err := cosmostx.SignWithPrivKey( |
| 119 | + context.Background(), signMode, signerData, txBuilder, priv1, txCfg, accSeq, |
| 120 | + ) |
| 121 | + s.Require().NoError(err) |
| 122 | + |
| 123 | + sig2, err := cosmostx.SignWithPrivKey( |
| 124 | + context.Background(), signMode, signerData, txBuilder, priv2, txCfg, accSeq, |
| 125 | + ) |
| 126 | + s.Require().NoError(err) |
| 127 | + |
| 128 | + // Round 3: Aggregate individual signatures into the multisig. |
| 129 | + msigData := multisigtypes.NewMultisig(len(pubKeys)) |
| 130 | + err = multisigtypes.AddSignatureV2(msigData, sig1, pubKeys) |
| 131 | + s.Require().NoError(err) |
| 132 | + err = multisigtypes.AddSignatureV2(msigData, sig2, pubKeys) |
| 133 | + s.Require().NoError(err) |
| 134 | + |
| 135 | + finalSig := signing.SignatureV2{ |
| 136 | + PubKey: msigPubKey, |
| 137 | + Data: msigData, |
| 138 | + Sequence: accSeq, |
| 139 | + } |
| 140 | + err = txBuilder.SetSignatures(finalSig) |
| 141 | + s.Require().NoError(err) |
| 142 | + |
| 143 | + // Encode and submit the transaction via FinalizeBlock. |
| 144 | + txBytes, err := txCfg.TxEncoder()(txBuilder.GetTx()) |
| 145 | + s.Require().NoError(err) |
| 146 | + |
| 147 | + resp, err := chain.App.GetBaseApp().FinalizeBlock(&abci.RequestFinalizeBlock{ |
| 148 | + Height: chain.App.GetBaseApp().LastBlockHeight() + 1, |
| 149 | + Time: chain.CurrentHeader.GetTime(), |
| 150 | + NextValidatorsHash: chain.NextVals.Hash(), |
| 151 | + Txs: [][]byte{txBytes}, |
| 152 | + }) |
| 153 | + s.Require().NoError(err) |
| 154 | + s.Require().Len(resp.TxResults, 1) |
| 155 | + |
| 156 | + txResult := resp.TxResults[0] |
| 157 | + s.Require().Equal(uint32(0), txResult.Code, |
| 158 | + "MsgSetRoutingIsmDomain from multisig should succeed but got code %d: %s", |
| 159 | + txResult.Code, txResult.Log) |
| 160 | +} |
0 commit comments