Skip to content

Commit f4f74b4

Browse files
authored
refactor: cosmos-sdk v0.50 upgrade and multi zone enhancements (#27)
* feat: multi zone - plugin & reflection (#5) * added plugins feature and reflection features * fixed minor sdkTx variables issue * Roll back some changes on config.go * fixed makefile build command * improvements over lint * Added nolint ant changed variable name * fix .gitworkflow build * fix .gitworkflow build * Added makefile build plugin steps * added nolint on staticcheck on load.go * added nolint on staticcheck on load.go * added nolint on staticcheck on load.go * added nolint on staticcheck on load.go * gofumpt on project * skiped a test due to reliance on an experimental feature of cosmos v0.46.0-beta * build plugin before test * build plugin before test * fixed failing test * fix make test * added docs * small refactor on variable names * Zondax/feature/multi zone fix * added plugins feature and reflection features * fixed minor sdkTx variables issue * Roll back some changes on config.go * fixed makefile build command * improvements over lint * Added nolint ant changed variable name * fix .gitworkflow build * fix .gitworkflow build * Added makefile build plugin steps * added nolint on staticcheck on load.go * added nolint on staticcheck on load.go * added nolint on staticcheck on load.go * added nolint on staticcheck on load.go * gofumpt on project * skiped a test due to reliance on an experimental feature of cosmos v0.46.0-beta * build plugin before test * build plugin before test * fixed failing test * fix make test * added docs * small refactor on variable names * Updated documentation and added --plugin flag * updated install step on README * lint fix * updated docs * updated go.sum (#8) * refactor: Upgrade rosetta to cosmos-sdk v0.50.0 (#7) * changed go.mod name, renamed dependencies on rosetta, adapted methods and uses of cosmos-sdk, upgraded dependencies * fixed all code errors * improvements over parsing * fees and gas parsing * remmoved validation on message due to deprecation (it should be validated on the server side) * solved issue on missing tx tipper * added codec types * fixed interface registre addr issue * improvements * fixed converter tests * commented plugin_test.go due to some modules not being available at v0.50 yet * improved code, moved parsing functions to utils.go * solved comments, fixed lint and added utils.go for parsing * fixed comment on cast variables * lint comments * Commented ibc-go dependdency until upgrade * added type assertion error handlinhg * updated go.sum * rename module for vanity url * update go.mod and go.sum
1 parent 1d6db57 commit f4f74b4

25 files changed

+2693
-266
lines changed

Diff for: README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Rosetta can be executed as a standalone service, it connects to the node endpoin
1515
Install Rosetta standalone server with the following command:
1616

1717
```bash
18-
go install cosmossdk.io/tools/rosetta
18+
go install github.com/cosmos/rosetta
1919
```
2020

2121
Alternatively, for building from source, simply run `make rosetta`. The binary will be located in the root folder.
@@ -27,7 +27,7 @@ To enable Native Rosetta API support, it's required to add the `RosettaCommand`
2727
Import the `rosettaCmd` package:
2828

2929
```go
30-
import "cosmossdk.io/tools/rosetta/cmd"
30+
import "github.com/cosmos/rosetta/cmd"
3131
```
3232

3333
Find the following line:
@@ -44,7 +44,7 @@ rootCmd.AddCommand(
4444
)
4545
```
4646

47-
The `RosettaCommand` function builds the `rosetta` root command and is defined in the `rosettaCmd` package (`cosmossdk.io/tools/rosetta/cmd`).
47+
The `RosettaCommand` function builds the `rosetta` root command and is defined in the `rosettaCmd` package (`github.com/cosmos/rosetta/cmd`).
4848

4949
Since we’ve updated the Cosmos SDK to work with the Rosetta API, updating the application's root command file is all you need to do.
5050

@@ -109,7 +109,7 @@ import (
109109

110110
"context"
111111
"github.com/coinbase/rosetta-sdk-go/types"
112-
"cosmossdk.io/tools/rosetta/lib"
112+
"github.com/cosmos/rosetta/lib"
113113
)
114114

115115
// CustomClient embeds the standard cosmos client
@@ -135,7 +135,7 @@ Example:
135135

136136
```go
137137
package custom_errors
138-
import crgerrs "cosmossdk.io/tools/rosetta/lib/errors"
138+
import crgerrs "github.com/cosmos/rosetta/lib/errors"
139139

140140
var customErrRetriable = true
141141
var CustomError = crgerrs.RegisterError(100, "custom message", customErrRetriable, "description")

Diff for: client_offline.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
"github.com/coinbase/rosetta-sdk-go/types"
88

9-
crgerrs "cosmossdk.io/tools/rosetta/lib/errors"
9+
crgerrs "github.com/cosmos/rosetta/lib/errors"
1010

1111
sdk "github.com/cosmos/cosmos-sdk/types"
1212
)
@@ -79,15 +79,19 @@ func (c *Client) PreprocessOperationsToOptions(_ context.Context, req *types.Con
7979
}
8080

8181
// get the signers
82-
signers := tx.GetSigners()
82+
signers, err := tx.GetSigners()
83+
if err != nil {
84+
return nil, err
85+
}
86+
8387
signersStr := make([]string, len(signers))
8488
accountIdentifiers := make([]*types.AccountIdentifier, len(signers))
8589

8690
for i, sig := range signers {
87-
addr := sig.String()
88-
signersStr[i] = addr
91+
addr := sdk.AccAddress(sig)
92+
signersStr[i] = addr.String()
8993
accountIdentifiers[i] = &types.AccountIdentifier{
90-
Address: addr,
94+
Address: addr.String(),
9195
}
9296
}
9397
// get the metadata request information

Diff for: client_online.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222
"github.com/cometbft/cometbft/rpc/client/http"
2323
"google.golang.org/grpc"
2424

25-
crgerrs "cosmossdk.io/tools/rosetta/lib/errors"
26-
crgtypes "cosmossdk.io/tools/rosetta/lib/types"
25+
crgerrs "github.com/cosmos/rosetta/lib/errors"
26+
crgtypes "github.com/cosmos/rosetta/lib/types"
2727

2828
sdk "github.com/cosmos/cosmos-sdk/types"
2929
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
@@ -71,14 +71,12 @@ func NewClient(cfg *Config) (*Client, error) {
7171

7272
var supportedOperations []string
7373
for _, ii := range cfg.InterfaceRegistry.ListImplementations(sdk.MsgInterfaceProtoName) {
74-
resolvedMsg, err := cfg.InterfaceRegistry.Resolve(ii)
74+
_, err := cfg.InterfaceRegistry.Resolve(ii)
7575
if err != nil {
7676
continue
7777
}
7878

79-
if _, ok := resolvedMsg.(sdk.Msg); ok {
80-
supportedOperations = append(supportedOperations, ii)
81-
}
79+
supportedOperations = append(supportedOperations, ii)
8280
}
8381

8482
supportedOperations = append(
@@ -518,15 +516,15 @@ func (c *Client) blockTxs(ctx context.Context, height *int64) (crgtypes.BlockTra
518516
TransactionIdentifier: &rosettatypes.TransactionIdentifier{Hash: c.converter.ToRosetta().BeginBlockTxHash(blockInfo.BlockID.Hash)},
519517
Operations: AddOperationIndexes(
520518
nil,
521-
c.converter.ToRosetta().BalanceOps(StatusTxSuccess, blockResults.BeginBlockEvents),
519+
c.converter.ToRosetta().BalanceOps(StatusTxSuccess, blockResults.FinalizeBlockEvents),
522520
),
523521
}
524522

525523
endBlockTx := &rosettatypes.Transaction{
526524
TransactionIdentifier: &rosettatypes.TransactionIdentifier{Hash: c.converter.ToRosetta().EndBlockTxHash(blockInfo.BlockID.Hash)},
527525
Operations: AddOperationIndexes(
528526
nil,
529-
c.converter.ToRosetta().BalanceOps(StatusTxSuccess, blockResults.EndBlockEvents),
527+
c.converter.ToRosetta().BalanceOps(StatusTxSuccess, blockResults.FinalizeBlockEvents),
530528
),
531529
}
532530

Diff for: cmd/rosetta.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55

66
"github.com/spf13/cobra"
77

8-
"cosmossdk.io/tools/rosetta"
98
"github.com/cosmos/cosmos-sdk/codec"
109
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
10+
"github.com/cosmos/rosetta"
1111
)
1212

1313
// RosettaCommand builds the rosetta root command given

Diff for: cmd/rosetta/main.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ package main
33
import (
44
"os"
55

6+
"github.com/cosmos/rosetta"
7+
68
"cosmossdk.io/log"
7-
rosettaCmd "cosmossdk.io/tools/rosetta/cmd"
8-
"github.com/cosmos/cosmos-sdk/codec"
9-
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
9+
rosettaCmd "github.com/cosmos/rosetta/cmd"
1010
)
1111

1212
func main() {
1313
var (
14-
logger = log.NewLogger(os.Stdout).With(log.ModuleKey, "rosetta")
15-
interfaceRegistry = codectypes.NewInterfaceRegistry()
16-
cdc = codec.NewProtoCodec(interfaceRegistry)
14+
cdc, interfaceRegistry = rosetta.MakeCodec()
15+
logger = log.NewLogger(os.Stdout).With(log.ModuleKey, "rosetta")
1716
)
1817

1918
if err := rosettaCmd.RosettaCommand(interfaceRegistry, cdc).Execute(); err != nil {

Diff for: codec.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
11
package rosetta
22

33
import (
4+
"cosmossdk.io/x/tx/signing"
45
"github.com/cosmos/cosmos-sdk/codec"
6+
"github.com/cosmos/cosmos-sdk/codec/address"
57
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
68
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
9+
sdk "github.com/cosmos/cosmos-sdk/types"
10+
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
711
authcodec "github.com/cosmos/cosmos-sdk/x/auth/types"
812
bankcodec "github.com/cosmos/cosmos-sdk/x/bank/types"
13+
"github.com/cosmos/gogoproto/proto"
914
)
1015

1116
// MakeCodec generates the codec required to interact
1217
// with the cosmos APIs used by the rosetta gateway
1318
func MakeCodec() (*codec.ProtoCodec, codectypes.InterfaceRegistry) {
14-
ir := codectypes.NewInterfaceRegistry()
19+
ir, _ := codectypes.NewInterfaceRegistryWithOptions(codectypes.InterfaceRegistryOptions{
20+
ProtoFiles: proto.HybridResolver,
21+
SigningOptions: signing.Options{
22+
AddressCodec: address.Bech32Codec{
23+
Bech32Prefix: sdk.GetConfig().GetBech32AccountAddrPrefix(),
24+
},
25+
ValidatorAddressCodec: address.Bech32Codec{
26+
Bech32Prefix: sdk.GetConfig().GetBech32ValidatorAddrPrefix(),
27+
},
28+
},
29+
})
1530
cdc := codec.NewProtoCodec(ir)
1631

1732
authcodec.RegisterInterfaces(ir)
1833
bankcodec.RegisterInterfaces(ir)
1934
cryptocodec.RegisterInterfaces(ir)
35+
txtypes.RegisterInterfaces(ir)
2036

2137
return cdc, ir
2238
}

Diff for: config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/coinbase/rosetta-sdk-go/types"
99
"github.com/spf13/pflag"
1010

11-
crg "cosmossdk.io/tools/rosetta/lib/server"
11+
crg "github.com/cosmos/rosetta/lib/server"
1212

1313
clientflags "github.com/cosmos/cosmos-sdk/client/flags"
1414
"github.com/cosmos/cosmos-sdk/codec"

Diff for: converter.go

+45-30
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package rosetta
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/base64"
67
"encoding/json"
78
"fmt"
89
"reflect"
910

11+
signingv1beta1 "cosmossdk.io/api/cosmos/tx/signing/v1beta1"
12+
1013
rosettatypes "github.com/coinbase/rosetta-sdk-go/types"
1114
abci "github.com/cometbft/cometbft/abci/types"
1215
"github.com/cometbft/cometbft/crypto"
@@ -15,8 +18,8 @@ import (
1518
secp "github.com/decred/dcrd/dcrec/secp256k1/v4"
1619

1720
sdkmath "cosmossdk.io/math"
18-
crgerrs "cosmossdk.io/tools/rosetta/lib/errors"
19-
crgtypes "cosmossdk.io/tools/rosetta/lib/types"
21+
crgerrs "github.com/cosmos/rosetta/lib/errors"
22+
crgtypes "github.com/cosmos/rosetta/lib/types"
2023

2124
sdkclient "github.com/cosmos/cosmos-sdk/client"
2225
"github.com/cosmos/cosmos-sdk/codec"
@@ -27,8 +30,6 @@ import (
2730
"github.com/cosmos/cosmos-sdk/types/tx/signing"
2831
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
2932
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
30-
31-
auth "github.com/cosmos/cosmos-sdk/x/auth/types"
3233
)
3334

3435
// Converter is a utility that can be used to convert
@@ -71,7 +72,7 @@ type ToRosettaConverter interface {
7172
// SigningComponents returns rosetta's components required to build a signable transaction
7273
SigningComponents(tx authsigning.Tx, metadata *ConstructionMetadata, rosPubKeys []*rosettatypes.PublicKey) (txBytes []byte, payloadsToSign []*rosettatypes.SigningPayload, err error)
7374
// Tx converts a CometBFT transaction and tx result if provided to a rosetta tx
74-
Tx(rawTx cmttypes.Tx, txResult *abci.ResponseDeliverTx) (*rosettatypes.Transaction, error)
75+
Tx(rawTx cmttypes.Tx, txResult *abci.ExecTxResult) (*rosettatypes.Transaction, error)
7576
// TxIdentifiers converts a CometBFT tx to transaction identifiers
7677
TxIdentifiers(txs []cmttypes.Tx) []*rosettatypes.TransactionIdentifier
7778
// BalanceOps converts events to balance operations
@@ -117,7 +118,13 @@ func NewConverter(cdc *codec.ProtoCodec, ir codectypes.InterfaceRegistry, cfg sd
117118
txDecode: cfg.TxDecoder(),
118119
txEncode: cfg.TxEncoder(),
119120
bytesToSign: func(tx authsigning.Tx, signerData authsigning.SignerData) (b []byte, err error) {
120-
bytesToSign, err := cfg.SignModeHandler().GetSignBytes(signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signerData, tx)
121+
parsedSignerData := parseSignerData(signerData)
122+
txData, err := parseTxData(tx, parsedSignerData)
123+
if err != nil {
124+
return nil, err
125+
}
126+
127+
bytesToSign, err := cfg.SignModeHandler().GetSignBytes(context.TODO(), signingv1beta1.SignMode(signing.SignMode_SIGN_MODE_DIRECT), parsedSignerData, *txData)
121128
if err != nil {
122129
return nil, err
123130
}
@@ -146,30 +153,22 @@ func (c converter) UnsignedTx(ops []*rosettatypes.Operation) (tx authsigning.Tx,
146153
for i := 0; i < len(ops); i++ {
147154
op := ops[i]
148155

149-
protoMessage, err := c.ir.Resolve(op.Type)
156+
msg, err := c.ir.Resolve(op.Type)
150157
if err != nil {
151158
return nil, crgerrs.WrapError(crgerrs.ErrBadArgument, "operation not found: "+op.Type)
152159
}
153160

154-
msg, ok := protoMessage.(sdk.Msg)
155-
if !ok {
156-
return nil, crgerrs.WrapError(crgerrs.ErrBadArgument, "operation is not a valid supported sdk.Msg: "+op.Type)
157-
}
158-
159161
err = c.Msg(op.Metadata, msg)
160162
if err != nil {
161163
return nil, crgerrs.WrapError(crgerrs.ErrCodec, err.Error())
162164
}
163165

164-
// verify message correctness
165-
if err = msg.ValidateBasic(); err != nil {
166-
return nil, crgerrs.WrapError(
167-
crgerrs.ErrBadArgument,
168-
fmt.Sprintf("validation of operation at index %d failed: %s", op.OperationIdentifier.Index, err),
169-
)
166+
legacyMsg, ok := msg.(sdk.LegacyMsg)
167+
if !ok {
168+
return nil, crgerrs.WrapError(crgerrs.ErrCodec, "Failed asserting LegacyMsg type")
170169
}
171170

172-
signers := msg.GetSigners()
171+
signers := legacyMsg.GetSigners()
173172
// check if there are enough signers
174173
if len(signers) == 0 {
175174
return nil, crgerrs.WrapError(crgerrs.ErrBadArgument, fmt.Sprintf("operation at index %d got no signers", op.OperationIdentifier.Index))
@@ -247,8 +246,13 @@ func (c converter) Ops(status string, msg sdk.Msg) ([]*rosettatypes.Operation, e
247246
return nil, err
248247
}
249248

250-
ops := make([]*rosettatypes.Operation, len(msg.GetSigners()))
251-
for i, signer := range msg.GetSigners() {
249+
legacyMsg, ok := msg.(sdk.LegacyMsg)
250+
if !ok {
251+
return nil, crgerrs.WrapError(crgerrs.ErrCodec, "Failed asserting LegacyMsg type")
252+
}
253+
254+
ops := make([]*rosettatypes.Operation, len(legacyMsg.GetSigners()))
255+
for i, signer := range legacyMsg.GetSigners() {
252256
op := &rosettatypes.Operation{
253257
Type: opName,
254258
Status: &status,
@@ -263,7 +267,7 @@ func (c converter) Ops(status string, msg sdk.Msg) ([]*rosettatypes.Operation, e
263267
}
264268

265269
// Tx converts a CometBFT raw transaction and its result (if provided) to a rosetta transaction
266-
func (c converter) Tx(rawTx cmttypes.Tx, txResult *abci.ResponseDeliverTx) (*rosettatypes.Transaction, error) {
270+
func (c converter) Tx(rawTx cmttypes.Tx, txResult *abci.ExecTxResult) (*rosettatypes.Transaction, error) {
267271
// decode tx
268272
tx, err := c.txDecode(rawTx)
269273
if err != nil {
@@ -596,9 +600,14 @@ func (c converter) OpsAndSigners(txBytes []byte) (ops []*rosettatypes.Operation,
596600
return nil, nil, crgerrs.WrapError(crgerrs.ErrCodec, err.Error())
597601
}
598602

599-
for _, signer := range txBuilder.GetTx().GetSigners() {
603+
txSigners, err := txBuilder.GetTx().GetSigners()
604+
if err != nil {
605+
return nil, nil, crgerrs.WrapError(crgerrs.ErrCodec, err.Error())
606+
}
607+
608+
for _, signer := range txSigners {
600609
signers = append(signers, &rosettatypes.AccountIdentifier{
601-
Address: signer.String(),
610+
Address: string(signer),
602611
})
603612
}
604613

@@ -678,7 +687,11 @@ func (c converter) SigningComponents(tx authsigning.Tx, metadata *ConstructionMe
678687
return nil, nil, crgerrs.WrapError(crgerrs.ErrBadArgument, err.Error())
679688
}
680689

681-
signers := tx.GetSigners()
690+
signers, err := tx.GetSignaturesV2()
691+
if err != nil {
692+
return nil, nil, crgerrs.WrapError(crgerrs.ErrBadArgument, err.Error())
693+
}
694+
682695
// assert the signers data provided in options are the same as the expected signing accounts
683696
// and that the number of rosetta provided public keys equals the one of the signers
684697
if len(metadata.SignersData) != len(signers) || len(signers) != len(rosPubKeys) {
@@ -706,16 +719,16 @@ func (c converter) SigningComponents(tx authsigning.Tx, metadata *ConstructionMe
706719
if err != nil {
707720
return nil, nil, err
708721
}
709-
if !bytes.Equal(pubKey.Address().Bytes(), signer.Bytes()) {
722+
if !bytes.Equal(pubKey.Address().Bytes(), signer.PubKey.Address()) {
710723
return nil, nil, crgerrs.WrapError(
711724
crgerrs.ErrBadArgument,
712-
fmt.Sprintf("public key at index %d does not match the expected transaction signer: %X <-> %X", i, rosPubKeys[i].Bytes, signer.Bytes()),
725+
fmt.Sprintf("public key at index %d does not match the expected transaction signer: %X <-> %X", i, rosPubKeys[i].Bytes, signer),
713726
)
714727
}
715728

716729
// set the signer data
717730
signerData := authsigning.SignerData{
718-
Address: signer.String(),
731+
Address: string(signer.PubKey.Address()),
719732
ChainID: metadata.ChainID,
720733
AccountNumber: metadata.SignersData[i].AccountNumber,
721734
Sequence: metadata.SignersData[i].Sequence,
@@ -729,8 +742,10 @@ func (c converter) SigningComponents(tx authsigning.Tx, metadata *ConstructionMe
729742
}
730743

731744
// set payload
745+
signerAddress := sdk.AccAddress(signer.PubKey.Address()).String()
746+
732747
payloadsToSign[i] = &rosettatypes.SigningPayload{
733-
AccountIdentifier: &rosettatypes.AccountIdentifier{Address: signer.String()},
748+
AccountIdentifier: &rosettatypes.AccountIdentifier{Address: signerAddress},
734749
Bytes: signBytes,
735750
SignatureType: rosettatypes.Ecdsa,
736751
}
@@ -763,7 +778,7 @@ func (c converter) SigningComponents(tx authsigning.Tx, metadata *ConstructionMe
763778

764779
// SignerData converts the given any account to signer data
765780
func (c converter) SignerData(anyAccount *codectypes.Any) (*SignerData, error) {
766-
var acc auth.AccountI
781+
var acc sdkclient.Account
767782
err := c.ir.UnpackAny(anyAccount, &acc)
768783
if err != nil {
769784
return nil, crgerrs.WrapError(crgerrs.ErrCodec, err.Error())

0 commit comments

Comments
 (0)