From f253ec28138421771e8f8993d664844188098b08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Toledano?= Date: Mon, 11 Mar 2024 17:45:05 +0100 Subject: [PATCH] refactor: Remove the use of sdk LegacyMsg (#93) * test change * Add command to build plugin for debugging * fix: msgV2Signers * fix * update: get signer with codec * CHANGELOG * fix: error handling --------- Co-authored-by: Lucas Francisco Lopez --- CHANGELOG.md | 4 +++ Makefile | 3 ++ converter.go | 34 +++++++++---------- converter_test.go | 68 ++++++++++++++++++------------------- plugins/cosmos-hub/makefile | 5 ++- 5 files changed, 62 insertions(+), 52 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25da0e9..f14e690 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,10 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## [Unreleased] +### Improvements + +* [93](https://github.com/cosmos/rosetta/pull/93) Removes the use of `LegacyMsg.GetSigners()` in favor of `codec.GetMsgV1Signers`. + ## [v0.50.4](https://github.com/cosmos/rosetta/releases/tag/v0.50.4) 2024-02-26 ### Improvements diff --git a/Makefile b/Makefile index 7789f61..60cb3b6 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,9 @@ build: plugin: cd plugins/cosmos-hub && make plugin +plugin-debug: + cd plugins/cosmos-hub && make plugin-debug + docker: docker build . --tag rosetta diff --git a/converter.go b/converter.go index dc2eae9..b35dd67 100644 --- a/converter.go +++ b/converter.go @@ -9,7 +9,6 @@ import ( "reflect" signingv1beta1 "cosmossdk.io/api/cosmos/tx/signing/v1beta1" - rosettatypes "github.com/coinbase/rosetta-sdk-go/types" abci "github.com/cometbft/cometbft/abci/types" "github.com/cometbft/cometbft/crypto" @@ -18,9 +17,6 @@ import ( secp "github.com/decred/dcrd/dcrec/secp256k1/v4" sdkmath "cosmossdk.io/math" - crgerrs "github.com/cosmos/rosetta/lib/errors" - crgtypes "github.com/cosmos/rosetta/lib/types" - sdkclient "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" @@ -30,6 +26,8 @@ import ( "github.com/cosmos/cosmos-sdk/types/tx/signing" authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + crgerrs "github.com/cosmos/rosetta/lib/errors" + crgtypes "github.com/cosmos/rosetta/lib/types" ) // Converter is a utility that can be used to convert @@ -163,12 +161,11 @@ func (c converter) UnsignedTx(ops []*rosettatypes.Operation) (tx authsigning.Tx, return nil, crgerrs.WrapError(crgerrs.ErrCodec, err.Error()) } - legacyMsg, ok := msg.(sdk.LegacyMsg) - if !ok { - return nil, crgerrs.WrapError(crgerrs.ErrCodec, "Failed asserting LegacyMsg type") + signers, _, err := c.cdc.GetMsgV1Signers(msg) + if err != nil { + return nil, crgerrs.WrapError(crgerrs.ErrConverter, fmt.Sprintf("while getting msg signers %s", err.Error())) } - signers := legacyMsg.GetSigners() // check if there are enough signers if len(signers) == 0 { return nil, crgerrs.WrapError(crgerrs.ErrBadArgument, fmt.Sprintf("operation at index %d got no signers", op.OperationIdentifier.Index)) @@ -239,24 +236,26 @@ func (c converter) Meta(msg sdk.Msg) (meta map[string]interface{}, err error) { // with the message proto name as type, and the raw fields // as metadata func (c converter) Ops(status string, msg sdk.Msg) ([]*rosettatypes.Operation, error) { - opName := sdk.MsgTypeURL(msg) - meta, err := c.Meta(msg) if err != nil { return nil, crgerrs.WrapError(crgerrs.ErrConverter, fmt.Sprintf("while getting meta from message %s", err.Error())) } - legacyMsg, ok := msg.(sdk.LegacyMsg) - if !ok { - return nil, crgerrs.WrapError(crgerrs.ErrCodec, "Failed asserting LegacyMsg type") + signers, _, err := c.cdc.GetMsgV1Signers(msg) + if err != nil { + return nil, crgerrs.WrapError(crgerrs.ErrConverter, fmt.Sprintf("while getting msg signers in Ops %s", err.Error())) } - ops := make([]*rosettatypes.Operation, len(legacyMsg.GetSigners())) - for i, signer := range legacyMsg.GetSigners() { + ops := make([]*rosettatypes.Operation, len(signers)) + for i, signer := range signers { + addr, err := c.ir.SigningContext().AddressCodec().BytesToString(signer) + if err != nil { + return nil, crgerrs.WrapError(crgerrs.ErrConverter, fmt.Sprintf("while getting address from signer %s", err.Error())) + } op := &rosettatypes.Operation{ - Type: opName, + Type: sdk.MsgTypeURL(msg), Status: &status, - Account: &rosettatypes.AccountIdentifier{Address: signer.String()}, + Account: &rosettatypes.AccountIdentifier{Address: addr}, Metadata: meta, } @@ -291,6 +290,7 @@ func (c converter) Tx(rawTx cmttypes.Tx, txResult *abci.ExecTxResult) (*rosettat } // get operations from msgs msgs := tx.GetMsgs() + var rawTxOps []*rosettatypes.Operation for _, msg := range msgs { diff --git a/converter_test.go b/converter_test.go index 2274bd4..057192c 100644 --- a/converter_test.go +++ b/converter_test.go @@ -56,40 +56,40 @@ func (s *ConverterTestSuite) SetupTest() { s.unsignedTx = builder.GetTx() } -// func (s *ConverterTestSuite) TestFromRosettaOpsToTxSuccess() { -// addr1 := sdk.AccAddress("address1").String() -// addr2 := sdk.AccAddress("address2").String() -// -// msg1 := &bank.MsgSend{ -// FromAddress: addr1, -// ToAddress: addr2, -// Amount: sdk.NewCoins(sdk.NewInt64Coin("test", 10)), -// } -// -// msg2 := &bank.MsgSend{ -// FromAddress: addr2, -// ToAddress: addr1, -// Amount: sdk.NewCoins(sdk.NewInt64Coin("utxo", 10)), -// } -// -// ops, err := s.c.ToRosetta().Ops("", msg1) -// s.Require().NoError(err) -// -// ops2, err := s.c.ToRosetta().Ops("", msg2) -// s.Require().NoError(err) -// -// ops = append(ops, ops2...) -// -// tx, err := s.c.ToSDK().UnsignedTx(ops) -// s.Require().NoError(err) -// -// getMsgs := tx.GetMsgs() -// -// s.Require().Equal(2, len(getMsgs)) -// -// s.Require().Equal(getMsgs[0], msg1) -// s.Require().Equal(getMsgs[1], msg2) -//} +func (s *ConverterTestSuite) TestFromRosettaOpsToTxSuccess() { + addr1 := sdk.AccAddress("address1").String() + addr2 := sdk.AccAddress("address2").String() + + msg1 := &bank.MsgSend{ + FromAddress: addr1, + ToAddress: addr2, + Amount: sdk.NewCoins(sdk.NewInt64Coin("test", 10)), + } + + msg2 := &bank.MsgSend{ + FromAddress: addr2, + ToAddress: addr1, + Amount: sdk.NewCoins(sdk.NewInt64Coin("utxo", 10)), + } + + ops, err := s.c.ToRosetta().Ops("", msg1) + s.Require().NoError(err) + + ops2, err := s.c.ToRosetta().Ops("", msg2) + s.Require().NoError(err) + + ops = append(ops, ops2...) + + tx, err := s.c.ToSDK().UnsignedTx(ops) + s.Require().NoError(err) + + getMsgs := tx.GetMsgs() + + s.Require().Equal(2, len(getMsgs)) + + s.Require().Equal(getMsgs[0], msg1) + s.Require().Equal(getMsgs[1], msg2) +} func (s *ConverterTestSuite) TestFromRosettaOpsToTxErrors() { s.Run("unrecognized op", func() { diff --git a/plugins/cosmos-hub/makefile b/plugins/cosmos-hub/makefile index 61ee7f6..547db66 100644 --- a/plugins/cosmos-hub/makefile +++ b/plugins/cosmos-hub/makefile @@ -1,4 +1,7 @@ #!/usr/bin/make -f plugin: - go build -buildmode=plugin -o main.so main.go \ No newline at end of file + go build -buildmode=plugin -o main.so main.go + +plugin-debug: + go build -buildmode=plugin -gcflags="all=-N -l" -o main.so main.go \ No newline at end of file