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

Upgrade cosmossdk from v0.47 to latest #23118

Open
dkatzan opened this issue Dec 29, 2024 · 0 comments
Open

Upgrade cosmossdk from v0.47 to latest #23118

dkatzan opened this issue Dec 29, 2024 · 0 comments
Labels
needs-triage Issue that needs to be triaged

Comments

@dkatzan
Copy link

dkatzan commented Dec 29, 2024

Hi,
we are trying to upgrade our cosmos SDK usage from v0.47.14 to latest version, and are encountering some difficulties with serializing transactions

specifically, we are trying to adapt to the breaking change of getting a transaction bytes to sign on, i.e replace this:

	bytesToSign, err := txConfig.SignModeHandler().GetSignBytes(
		signMode,
		signerData,
		txBuilder.GetTx(),
	)

with what seems to be an in place replacement:

		bytesToSign, err = xauthsigning.GetSignBytesAdapter(
			context.Background(),
			txConfig.SignModeHandler(),
			signMode,
			signerData,
			txBuilder.GetTx(),
		)

but it fails in some cases

specifically, this is failing for me when trying to serialize generic messages in amino format, which their types are not known in advance and were not registered in codec.
This is what we previously did to implement such functionality

This is a minimal reproducible example which work on v0.47.14

package signable

import (
	"context"
	"encoding/hex"
	"testing"

	"github.com/cosmos/cosmos-sdk/codec"
	"github.com/cosmos/cosmos-sdk/codec/types"
	codectypes "github.com/cosmos/cosmos-sdk/codec/types"
	"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
	"github.com/cosmos/cosmos-sdk/std"
	sdk "github.com/cosmos/cosmos-sdk/types"
	"github.com/cosmos/cosmos-sdk/types/tx/signing"
	xauthsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
	authTx "github.com/cosmos/cosmos-sdk/x/auth/tx"
	"github.com/stretchr/testify/require"
)

type AnyMessage struct {
	codectypes.Any
	MessageName string
}

func (m *AnyMessage) GetSigners() []sdk.AccAddress {
	return []sdk.AccAddress{}
}

func (m *AnyMessage) ValidateBasic() error { return nil }

func (m *AnyMessage) XXX_MessageName() string { return m.MessageName }

// Note: The function `XXX_Marshal` of `Any` adds tag and length which we don't need, so we override it.
func (m *AnyMessage) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
	b = b[:cap(b)]
	copy(b, m.Value)
	return b[:len(m.Value)], nil
}

func (m *AnyMessage) Size() int {
	if m == nil {
		return 0
	}
	typeUrlTagAndLength := 2
	n := typeUrlTagAndLength + len(m.TypeUrl) + len(m.Value)
	return n
}

func (m *AnyMessage) GetSignBytes() []byte {
	return m.Value
}
func (m *AnyMessage) Route() string {
	return m.MessageName
}

func (m *AnyMessage) Type() string {
	return m.MessageName
}

func TestGetBytes(t *testing.T) {
	interfaceRegistry := types.NewInterfaceRegistry()
	codec := codec.NewProtoCodec(interfaceRegistry)
	std.RegisterInterfaces(interfaceRegistry)

	txConfig := authTx.NewTxConfig(codec, authTx.DefaultSignModes)
	txBuilder := txConfig.NewTxBuilder()

	jsonAminoMessage := "{\"type\":\"cosmos-sdk/MsgSend\",\"value\":{\"amount\":[{\"amount\":\"10000\",\"denom\":\"uatom\"}],\"from_address\":\"cosmos183epjqv3r4eyxptr6p5jduan2488hzl5v8j6y8\",\"to_address\":\"cosmos1ay9j5h6gxgp5qa0paaccecagg6pwswsca6z9km\"}}"
	sortedJson, err := sdk.SortJSON([]byte(jsonAminoMessage))
	require.NoError(t, err)
	anyMessage := &AnyMessage{
		MessageName: "cosmos-sdk/MsgSend",
		Any:         codectypes.Any{Value: sortedJson},
	}

	publicKeyString := "03b73a8ed5a58bb70205e2706b3e292a1bab8a3c9e5f4bab7e53fc18587b01dbae"
	signMode := signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON
	key, err := hex.DecodeString(publicKeyString)
	require.NoError(t, err)
	pubkey := &secp256k1.PubKey{Key: key}

	signature := signing.SignatureV2{
		PubKey: pubkey,
		Data: &signing.SingleSignatureData{
			SignMode:  signMode,
			Signature: nil,
		},
		Sequence: 1,
	}

	txBuilder.SetMemo("")
	txBuilder.SetFeeAmount([]sdk.Coin{})
	txBuilder.SetGasLimit(0)
	txBuilder.SetTimeoutHeight(0)
	txBuilder.SetSignatures(signature)
	txBuilder.SetMsgs(anyMessage)

	signerData := xauthsigning.SignerData{
		ChainID:       "cosmoshub-4",
		Address:       "cosmos1p4x4jp9524vgtkmkes25ug96t3t0k3cajmqxnt",
		AccountNumber: 1,
		Sequence:      1,
	}
	 _, err = txConfig.SignModeHandler().GetSignBytes(
	 	signMode,
	 	signerData,
	 	txBuilder.GetTx(),
	 )
	require.NoError(t, err)
}

upgrading to latest version, and trying to replace

	 _, err = txConfig.SignModeHandler().GetSignBytes(
	 	signMode,
	 	signerData,
	 	txBuilder.GetTx(),
	 )

with

	_, err = xauthsigning.GetSignBytesAdapter(
		context.Background(),
		txConfig.SignModeHandler(),
		signMode,
		signerData,
		txBuilder.GetTx(),
	)

will fail

this is probably due to the not going through this code path any more

msgsBytes = append(msgsBytes, json.RawMessage(legacyMsg.GetSignBytes()))

and the new code path for serializing is assuming the messages types are all known and registered before hand

any help\pointers for maintaining our current functionality will be greatly appreciated
thx

@github-actions github-actions bot added the needs-triage Issue that needs to be triaged label Dec 29, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Cosmos-SDK Dec 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-triage Issue that needs to be triaged
Projects
Status: 📋 Backlog
Development

No branches or pull requests

1 participant