Skip to content

Commit

Permalink
refactor: Remove the use of sdk LegacyMsg (#93)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
JulianToledano and lucaslopezf authored Mar 11, 2024
1 parent 5cfd4f4 commit f253ec2
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 52 deletions.
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`.

## [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
34 changes: 17 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, 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))
Expand Down Expand Up @@ -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,
}

Expand Down Expand Up @@ -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 {
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

0 comments on commit f253ec2

Please sign in to comment.