From 41daa2a43edf2cca475b42256cfb15a8e71f3df0 Mon Sep 17 00:00:00 2001 From: Julian Toledano Date: Wed, 28 Feb 2024 19:48:48 +0100 Subject: [PATCH 1/7] test change --- converter.go | 35 ++++++++++++------------ converter_test.go | 70 ++++++++++++++++++++++++----------------------- 2 files changed, 54 insertions(+), 51 deletions(-) diff --git a/converter.go b/converter.go index dc2eae9..7b62aff 100644 --- a/converter.go +++ b/converter.go @@ -18,9 +18,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 +27,9 @@ 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" + protov2 "google.golang.org/protobuf/proto" ) // Converter is a utility that can be used to convert @@ -62,7 +62,7 @@ type ToRosettaConverter interface { // Amounts converts sdk.Coins to rosetta.Amounts Amounts(ownedCoins []sdk.Coin, availableCoins sdk.Coins) []*rosettatypes.Amount // Ops converts an sdk.Msg to rosetta operations - Ops(status string, msg sdk.Msg) ([]*rosettatypes.Operation, error) + Ops(status string, msg protov2.Message) ([]*rosettatypes.Operation, error) // OpsAndSigners takes raw transaction bytes and returns rosetta operations and the expected signers OpsAndSigners(txBytes []byte) (ops []*rosettatypes.Operation, signers []*rosettatypes.AccountIdentifier, err error) // Meta converts an sdk.Msg to rosetta metadata @@ -238,14 +238,13 @@ func (c converter) Meta(msg sdk.Msg) (meta map[string]interface{}, err error) { // Ops will create an operation for each msg signer // 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())) - } - +func (c converter) Ops(status string, msg protov2.Message) ([]*rosettatypes.Operation, error) { + //meta, err := c.Meta(msg) + //if err != nil { + // return nil, crgerrs.WrapError(crgerrs.ErrConverter, fmt.Sprintf("while getting meta from message %s", err.Error())) + //} + x := msg.ProtoReflect() + fmt.Println(x) legacyMsg, ok := msg.(sdk.LegacyMsg) if !ok { return nil, crgerrs.WrapError(crgerrs.ErrCodec, "Failed asserting LegacyMsg type") @@ -254,10 +253,10 @@ func (c converter) Ops(status string, msg sdk.Msg) ([]*rosettatypes.Operation, e ops := make([]*rosettatypes.Operation, len(legacyMsg.GetSigners())) for i, signer := range legacyMsg.GetSigners() { op := &rosettatypes.Operation{ - Type: opName, - Status: &status, - Account: &rosettatypes.AccountIdentifier{Address: signer.String()}, - Metadata: meta, + Type: "/" + string(msg.ProtoReflect().Descriptor().FullName()), + Status: &status, + Account: &rosettatypes.AccountIdentifier{Address: signer.String()}, + //Metadata: meta, } ops[i] = op @@ -290,7 +289,9 @@ func (c converter) Tx(rawTx cmttypes.Tx, txResult *abci.ExecTxResult) (*rosettat } } // get operations from msgs - msgs := tx.GetMsgs() + //msgs := tx.GetMsgs() + msgs, err := tx.GetMsgsV2() + // todo var rawTxOps []*rosettatypes.Operation for _, msg := range msgs { diff --git a/converter_test.go b/converter_test.go index 2274bd4..9e9613d 100644 --- a/converter_test.go +++ b/converter_test.go @@ -8,6 +8,8 @@ import ( "github.com/cosmos/rosetta" crgerrs "github.com/cosmos/rosetta/lib/errors" + bankv1 "cosmossdk.io/api/cosmos/bank/v1beta1" + v1beta1 "cosmossdk.io/api/cosmos/base/v1beta1" rosettatypes "github.com/coinbase/rosetta-sdk-go/types" abci "github.com/cometbft/cometbft/abci/types" "github.com/stretchr/testify/suite" @@ -56,40 +58,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 := &bankv1.MsgSend{ + FromAddress: addr1, + ToAddress: addr2, + Amount: []*v1beta1.Coin{{Amount: "100", Denom: "test"}}, + } + + msg2 := &bankv1.MsgSend{ + FromAddress: addr2, + ToAddress: addr1, + Amount: []*v1beta1.Coin{{Amount: "10", Denom: "utxo"}}, + } + + 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() { From e9d1211bd52e3505ad8e92d9541dd565bce55d2f Mon Sep 17 00:00:00 2001 From: Lucas Francisco Lopez Date: Thu, 29 Feb 2024 18:09:43 -0300 Subject: [PATCH 2/7] Add command to build plugin for debugging --- Makefile | 3 +++ plugins/cosmos-hub/makefile | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) 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/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 From 705f557a051422c1a3108b3b93e0cd6d987a5ec9 Mon Sep 17 00:00:00 2001 From: Julian Toledano Date: Mon, 4 Mar 2024 19:05:04 +0100 Subject: [PATCH 3/7] fix: msgV2Signers --- converter.go | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/converter.go b/converter.go index 7b62aff..0aeaddf 100644 --- a/converter.go +++ b/converter.go @@ -9,6 +9,7 @@ import ( "reflect" signingv1beta1 "cosmossdk.io/api/cosmos/tx/signing/v1beta1" + "google.golang.org/protobuf/encoding/protojson" rosettatypes "github.com/coinbase/rosetta-sdk-go/types" abci "github.com/cometbft/cometbft/abci/types" @@ -66,7 +67,7 @@ type ToRosettaConverter interface { // OpsAndSigners takes raw transaction bytes and returns rosetta operations and the expected signers OpsAndSigners(txBytes []byte) (ops []*rosettatypes.Operation, signers []*rosettatypes.AccountIdentifier, err error) // Meta converts an sdk.Msg to rosetta metadata - Meta(msg sdk.Msg) (meta map[string]interface{}, err error) + Meta(msg protov2.Message) (meta map[string]interface{}, err error) // SignerData returns account signing data from a queried any account SignerData(anyAccount *codectypes.Any) (*SignerData, error) // SigningComponents returns rosetta's components required to build a signable transaction @@ -221,8 +222,8 @@ func (c converter) Msg(meta map[string]interface{}, msg sdk.Msg) error { return c.cdc.UnmarshalJSON(metaBytes, msg) } -func (c converter) Meta(msg sdk.Msg) (meta map[string]interface{}, err error) { - b, err := c.cdc.MarshalJSON(msg) +func (c converter) Meta(msg protov2.Message) (meta map[string]interface{}, err error) { + b, err := protojson.Marshal(msg) if err != nil { return nil, crgerrs.WrapError(crgerrs.ErrCodec, err.Error()) } @@ -239,24 +240,24 @@ 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 protov2.Message) ([]*rosettatypes.Operation, error) { - //meta, err := c.Meta(msg) - //if err != nil { - // return nil, crgerrs.WrapError(crgerrs.ErrConverter, fmt.Sprintf("while getting meta from message %s", err.Error())) - //} - x := msg.ProtoReflect() - fmt.Println(x) - legacyMsg, ok := msg.(sdk.LegacyMsg) - if !ok { - return nil, crgerrs.WrapError(crgerrs.ErrCodec, "Failed asserting LegacyMsg type") - } - - ops := make([]*rosettatypes.Operation, len(legacyMsg.GetSigners())) - for i, signer := range legacyMsg.GetSigners() { + meta, err := c.Meta(msg) + if err != nil { + return nil, crgerrs.WrapError(crgerrs.ErrConverter, fmt.Sprintf("while getting meta from message %s", err.Error())) + } + + signers, err := c.cdc.InterfaceRegistry().SigningContext().GetSigners(msg) + if err != nil { + return nil, err + } + + ops := make([]*rosettatypes.Operation, len(signers)) + for i, signer := range signers { + addr, _ := c.cdc.InterfaceRegistry().SigningContext().AddressCodec().BytesToString(signer) op := &rosettatypes.Operation{ - Type: "/" + string(msg.ProtoReflect().Descriptor().FullName()), - Status: &status, - Account: &rosettatypes.AccountIdentifier{Address: signer.String()}, - //Metadata: meta, + Type: "/" + string(msg.ProtoReflect().Descriptor().FullName()), + Status: &status, + Account: &rosettatypes.AccountIdentifier{Address: addr}, + Metadata: meta, } ops[i] = op From 6d21265746ed1738543a228282df337e4ea5bc0b Mon Sep 17 00:00:00 2001 From: Julian Toledano Date: Mon, 4 Mar 2024 19:05:36 +0100 Subject: [PATCH 4/7] fix --- converter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/converter.go b/converter.go index 0aeaddf..f36bd5b 100644 --- a/converter.go +++ b/converter.go @@ -245,7 +245,7 @@ func (c converter) Ops(status string, msg protov2.Message) ([]*rosettatypes.Oper return nil, crgerrs.WrapError(crgerrs.ErrConverter, fmt.Sprintf("while getting meta from message %s", err.Error())) } - signers, err := c.cdc.InterfaceRegistry().SigningContext().GetSigners(msg) + signers, err := c.cdc.GetMsgV2Signers(msg) if err != nil { return nil, err } From 3a707008cf59f7291e26aebc9e723532b376b280 Mon Sep 17 00:00:00 2001 From: Julian Toledano Date: Tue, 5 Mar 2024 14:04:59 +0100 Subject: [PATCH 5/7] update: get signer with codec --- converter.go | 29 ++++++++++++----------------- converter_test.go | 10 ++++------ 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/converter.go b/converter.go index f36bd5b..abfc202 100644 --- a/converter.go +++ b/converter.go @@ -9,8 +9,6 @@ import ( "reflect" signingv1beta1 "cosmossdk.io/api/cosmos/tx/signing/v1beta1" - "google.golang.org/protobuf/encoding/protojson" - rosettatypes "github.com/coinbase/rosetta-sdk-go/types" abci "github.com/cometbft/cometbft/abci/types" "github.com/cometbft/cometbft/crypto" @@ -30,7 +28,6 @@ import ( banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" crgerrs "github.com/cosmos/rosetta/lib/errors" crgtypes "github.com/cosmos/rosetta/lib/types" - protov2 "google.golang.org/protobuf/proto" ) // Converter is a utility that can be used to convert @@ -63,11 +60,11 @@ type ToRosettaConverter interface { // Amounts converts sdk.Coins to rosetta.Amounts Amounts(ownedCoins []sdk.Coin, availableCoins sdk.Coins) []*rosettatypes.Amount // Ops converts an sdk.Msg to rosetta operations - Ops(status string, msg protov2.Message) ([]*rosettatypes.Operation, error) + Ops(status string, msg sdk.Msg) ([]*rosettatypes.Operation, error) // OpsAndSigners takes raw transaction bytes and returns rosetta operations and the expected signers OpsAndSigners(txBytes []byte) (ops []*rosettatypes.Operation, signers []*rosettatypes.AccountIdentifier, err error) // Meta converts an sdk.Msg to rosetta metadata - Meta(msg protov2.Message) (meta map[string]interface{}, err error) + Meta(msg sdk.Msg) (meta map[string]interface{}, err error) // SignerData returns account signing data from a queried any account SignerData(anyAccount *codectypes.Any) (*SignerData, error) // SigningComponents returns rosetta's components required to build a signable transaction @@ -164,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, err } - 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)) @@ -222,8 +218,8 @@ func (c converter) Msg(meta map[string]interface{}, msg sdk.Msg) error { return c.cdc.UnmarshalJSON(metaBytes, msg) } -func (c converter) Meta(msg protov2.Message) (meta map[string]interface{}, err error) { - b, err := protojson.Marshal(msg) +func (c converter) Meta(msg sdk.Msg) (meta map[string]interface{}, err error) { + b, err := c.cdc.MarshalJSON(msg) if err != nil { return nil, crgerrs.WrapError(crgerrs.ErrCodec, err.Error()) } @@ -239,13 +235,13 @@ func (c converter) Meta(msg protov2.Message) (meta map[string]interface{}, err e // Ops will create an operation for each msg signer // with the message proto name as type, and the raw fields // as metadata -func (c converter) Ops(status string, msg protov2.Message) ([]*rosettatypes.Operation, error) { +func (c converter) Ops(status string, msg sdk.Msg) ([]*rosettatypes.Operation, error) { meta, err := c.Meta(msg) if err != nil { return nil, crgerrs.WrapError(crgerrs.ErrConverter, fmt.Sprintf("while getting meta from message %s", err.Error())) } - signers, err := c.cdc.GetMsgV2Signers(msg) + signers, _, err := c.cdc.GetMsgV1Signers(msg) if err != nil { return nil, err } @@ -254,7 +250,7 @@ func (c converter) Ops(status string, msg protov2.Message) ([]*rosettatypes.Oper for i, signer := range signers { addr, _ := c.cdc.InterfaceRegistry().SigningContext().AddressCodec().BytesToString(signer) op := &rosettatypes.Operation{ - Type: "/" + string(msg.ProtoReflect().Descriptor().FullName()), + Type: sdk.MsgTypeURL(msg), Status: &status, Account: &rosettatypes.AccountIdentifier{Address: addr}, Metadata: meta, @@ -290,9 +286,8 @@ func (c converter) Tx(rawTx cmttypes.Tx, txResult *abci.ExecTxResult) (*rosettat } } // get operations from msgs - //msgs := tx.GetMsgs() - msgs, err := tx.GetMsgsV2() - // todo + msgs := tx.GetMsgs() + var rawTxOps []*rosettatypes.Operation for _, msg := range msgs { diff --git a/converter_test.go b/converter_test.go index 9e9613d..057192c 100644 --- a/converter_test.go +++ b/converter_test.go @@ -8,8 +8,6 @@ import ( "github.com/cosmos/rosetta" crgerrs "github.com/cosmos/rosetta/lib/errors" - bankv1 "cosmossdk.io/api/cosmos/bank/v1beta1" - v1beta1 "cosmossdk.io/api/cosmos/base/v1beta1" rosettatypes "github.com/coinbase/rosetta-sdk-go/types" abci "github.com/cometbft/cometbft/abci/types" "github.com/stretchr/testify/suite" @@ -62,16 +60,16 @@ func (s *ConverterTestSuite) TestFromRosettaOpsToTxSuccess() { addr1 := sdk.AccAddress("address1").String() addr2 := sdk.AccAddress("address2").String() - msg1 := &bankv1.MsgSend{ + msg1 := &bank.MsgSend{ FromAddress: addr1, ToAddress: addr2, - Amount: []*v1beta1.Coin{{Amount: "100", Denom: "test"}}, + Amount: sdk.NewCoins(sdk.NewInt64Coin("test", 10)), } - msg2 := &bankv1.MsgSend{ + msg2 := &bank.MsgSend{ FromAddress: addr2, ToAddress: addr1, - Amount: []*v1beta1.Coin{{Amount: "10", Denom: "utxo"}}, + Amount: sdk.NewCoins(sdk.NewInt64Coin("utxo", 10)), } ops, err := s.c.ToRosetta().Ops("", msg1) From 48381e4a28b7931e4b285f42d22108bdced9b9fe Mon Sep 17 00:00:00 2001 From: Julian Toledano Date: Tue, 5 Mar 2024 17:06:52 +0100 Subject: [PATCH 6/7] CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) 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 From abb8e490f0ec21803f75fbac410674c2a2268223 Mon Sep 17 00:00:00 2001 From: Julian Toledano Date: Thu, 7 Mar 2024 09:05:45 +0100 Subject: [PATCH 7/7] fix: error handling --- converter.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/converter.go b/converter.go index abfc202..b35dd67 100644 --- a/converter.go +++ b/converter.go @@ -163,7 +163,7 @@ func (c converter) UnsignedTx(ops []*rosettatypes.Operation) (tx authsigning.Tx, signers, _, err := c.cdc.GetMsgV1Signers(msg) if err != nil { - return nil, err + return nil, crgerrs.WrapError(crgerrs.ErrConverter, fmt.Sprintf("while getting msg signers %s", err.Error())) } // check if there are enough signers @@ -243,12 +243,15 @@ func (c converter) Ops(status string, msg sdk.Msg) ([]*rosettatypes.Operation, e signers, _, err := c.cdc.GetMsgV1Signers(msg) if err != nil { - return nil, err + return nil, crgerrs.WrapError(crgerrs.ErrConverter, fmt.Sprintf("while getting msg signers in Ops %s", err.Error())) } ops := make([]*rosettatypes.Operation, len(signers)) for i, signer := range signers { - addr, _ := c.cdc.InterfaceRegistry().SigningContext().AddressCodec().BytesToString(signer) + 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: sdk.MsgTypeURL(msg), Status: &status,