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

refactor: Remove the use of sdk LegacyMsg #93

Merged
merged 7 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
JulianToledano marked this conversation as resolved.
Show resolved Hide resolved

## [v0.50.4](https://github.com/cosmos/rosetta/releases/tag/v0.50.4) 2024-02-26

### Improvements
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 14 additions & 17 deletions converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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, err
JulianToledano marked this conversation as resolved.
Show resolved Hide resolved
}

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))
Expand Down Expand Up @@ -239,24 +236,23 @@ 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, err
JulianToledano marked this conversation as resolved.
Show resolved Hide resolved
}

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, _ := c.cdc.InterfaceRegistry().SigningContext().AddressCodec().BytesToString(signer)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a risk of encountering a nil pointer dereference at any point in this sequence?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should not, the codec already uses that internally, although we could make a check when initialising rosetta to make sure SigningContext is correctly set.

op := &rosettatypes.Operation{
Type: opName,
Type: sdk.MsgTypeURL(msg),
Status: &status,
Account: &rosettatypes.AccountIdentifier{Address: signer.String()},
Account: &rosettatypes.AccountIdentifier{Address: addr},
Metadata: meta,
}

Expand Down Expand Up @@ -291,6 +287,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 {
Expand Down
68 changes: 34 additions & 34 deletions converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
5 changes: 4 additions & 1 deletion plugins/cosmos-hub/makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/usr/bin/make -f

plugin:
go build -buildmode=plugin -o main.so main.go
go build -buildmode=plugin -o main.so main.go

plugin-debug:
go build -buildmode=plugin -gcflags="all=-N -l" -o main.so main.go
Loading