Skip to content

Commit

Permalink
rosetta: upgrade to newest version (#9314)
Browse files Browse the repository at this point in the history
* feat:  update rosetta sdk to v0.6.10 

embed from v1.0.0 release branch of the library: https://github.com/tendermint/cosmos-rosetta-gateway/tree/release/v1.0.0

closes:
#9300

Co-authored-by: Alessio Treglia <[email protected]>
  • Loading branch information
noandrea and Alessio Treglia authored May 14, 2021
1 parent 925e63c commit 25ecec6
Show file tree
Hide file tree
Showing 29 changed files with 1,036 additions and 57 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [\#9205](https://github.com/cosmos/cosmos-sdk/pull/9205) Improve readability in `abci` handleQueryP2P
* [\#9235](https://github.com/cosmos/cosmos-sdk/pull/9235) CreateMembershipProof/CreateNonMembershipProof now returns an error
if input key is empty, or input data contains empty key.
* [\#9314](https://github.com/cosmos/cosmos-sdk/pull/9314) Update Rosetta SDK to upstream's latest release.

### Features

Expand Down
2 changes: 1 addition & 1 deletion contrib/rosetta/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ services:
- 8080

test_rosetta:
image: tendermintdev/rosetta-cli:v0.6.6
image: tendermintdev/rosetta-cli:v0.6.7
volumes:
- ./configuration:/rosetta/config:z
command: ["./config/run_tests.sh"]
Expand Down
2 changes: 1 addition & 1 deletion contrib/rosetta/rosetta-cli/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM golang:1.15-alpine as build

RUN apk add git gcc libc-dev --no-cache

ARG ROSETTA_VERSION="v0.5.23"
ARG ROSETTA_VERSION="v0.6.7"

# build rosetta CLI
WORKDIR /rosetta
Expand Down
7 changes: 5 additions & 2 deletions docs/architecture/adr-035-rosetta-api-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
- Alessio Treglia (@alessio)
- Frojdy Dymylja (@fdymylja)


## Changelog

- 2021-05-12: the external library [cosmos-rosetta-gateway](https://github.com/tendermint/cosmos-rosetta-gateway) has been moved within the SDK.
## Context

[Rosetta API](https://www.rosetta-api.org/) is an open-source specification and set of tools developed by Coinbase to
Expand Down Expand Up @@ -34,7 +38,7 @@ The driving principles of the proposed design are:

We will achieve these delivering on these principles by the following:

1. There will be an external repo called [cosmos-rosetta-gateway](https://github.com/tendermint/cosmos-rosetta-gateway)
1. There will be a package `rosetta/lib`
for the implementation of the core Rosetta API features, particularly:
a. The types and interfaces (`Client`, `OfflineClient`...), this separates design from implementation detail.
b. The `Server` functionality as this is independent of the Cosmos SDK version.
Expand Down Expand Up @@ -207,4 +211,3 @@ Proposed
## References

- https://www.rosetta-api.org/
- https://github.com/tendermint/cosmos-rosetta-gateway
6 changes: 2 additions & 4 deletions docs/run-node/rosetta.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

Package rosetta implements the rosetta API for the current cosmos sdk release series.

The client satisfies [cosmos-rosetta-gateway](https://github.com/tendermint/cosmos-rosetta-gateway) `Client` interface implementation.

## Extension

There are two ways in which you can customize and extend the implementation with your custom settings.
Expand All @@ -24,7 +22,7 @@ import (

"context"
"github.com/coinbase/rosetta-sdk-go/types"
"github.com/cosmos/cosmos-sdk/server/rosetta"
"github.com/cosmos/cosmos-sdk/server/rosetta/lib"
)

// CustomClient embeds the standard cosmos client
Expand All @@ -48,7 +46,7 @@ Example:

```go
package custom_errors
import crgerrs "github.com/tendermint/cosmos-rosetta-gateway/errors"
import crgerrs "github.com/cosmos/cosmos-sdk/server/rosetta/lib/errors"

var customErrRetriable = true
var CustomError = crgerrs.RegisterError(100, "custom message", customErrRetriable, "description")
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/bgentry/speakeasy v0.1.0
github.com/btcsuite/btcd v0.21.0-beta
github.com/btcsuite/btcutil v1.0.2
github.com/coinbase/rosetta-sdk-go v0.5.9
github.com/coinbase/rosetta-sdk-go v0.6.10
github.com/confio/ics23/go v0.6.6
github.com/cosmos/go-bip39 v1.0.0
github.com/cosmos/iavl v0.16.0
Expand All @@ -19,7 +19,6 @@ require (
github.com/gogo/protobuf v1.3.3
github.com/golang/mock v1.4.4
github.com/golang/protobuf v1.5.2
github.com/golang/snappy v0.0.2 // indirect
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
Expand All @@ -46,12 +45,12 @@ require (
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.7.0
github.com/tendermint/btcd v0.1.1
github.com/tendermint/cosmos-rosetta-gateway v0.3.0-rc2.0.20210304154332-87d6ca4410df
github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15
github.com/tendermint/go-amino v0.16.0
github.com/tendermint/tendermint v0.34.10
github.com/tendermint/tm-db v0.6.4
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
google.golang.org/genproto v0.0.0-20210114201628-6edceaf6022f
google.golang.org/grpc v1.37.0
google.golang.org/protobuf v1.26.0
Expand Down
49 changes: 26 additions & 23 deletions go.sum

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions server/rosetta.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/server/rosetta"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/server/rosetta"
)

// RosettaCommand builds the rosetta root command given
Expand Down
3 changes: 2 additions & 1 deletion server/rosetta/client_offline.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"encoding/hex"

"github.com/coinbase/rosetta-sdk-go/types"
crgerrs "github.com/tendermint/cosmos-rosetta-gateway/errors"

crgerrs "github.com/cosmos/cosmos-sdk/server/rosetta/lib/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand Down
4 changes: 2 additions & 2 deletions server/rosetta/client_online.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"github.com/tendermint/tendermint/rpc/client/http"
"google.golang.org/grpc"

crgerrs "github.com/tendermint/cosmos-rosetta-gateway/errors"
crgtypes "github.com/tendermint/cosmos-rosetta-gateway/types"
crgerrs "github.com/cosmos/cosmos-sdk/server/rosetta/lib/errors"
crgtypes "github.com/cosmos/cosmos-sdk/server/rosetta/lib/types"

sdk "github.com/cosmos/cosmos-sdk/types"
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
Expand Down
3 changes: 2 additions & 1 deletion server/rosetta/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (

"github.com/coinbase/rosetta-sdk-go/types"
"github.com/spf13/pflag"
crg "github.com/tendermint/cosmos-rosetta-gateway/server"

crg "github.com/cosmos/cosmos-sdk/server/rosetta/lib/server"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down
12 changes: 7 additions & 5 deletions server/rosetta/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ import (
"github.com/tendermint/tendermint/crypto"

"github.com/btcsuite/btcd/btcec"
crgtypes "github.com/tendermint/cosmos-rosetta-gateway/types"
tmcoretypes "github.com/tendermint/tendermint/rpc/core/types"

crgtypes "github.com/cosmos/cosmos-sdk/server/rosetta/lib/types"

"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/types/tx/signing"

rosettatypes "github.com/coinbase/rosetta-sdk-go/types"
crgerrs "github.com/tendermint/cosmos-rosetta-gateway/errors"
abci "github.com/tendermint/tendermint/abci/types"
tmtypes "github.com/tendermint/tendermint/types"

crgerrs "github.com/cosmos/cosmos-sdk/server/rosetta/lib/errors"

sdkclient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -251,7 +253,7 @@ func (c converter) Ops(status string, msg sdk.Msg) ([]*rosettatypes.Operation, e
for i, signer := range msg.GetSigners() {
op := &rosettatypes.Operation{
Type: opName,
Status: status,
Status: &status,
Account: &rosettatypes.AccountIdentifier{Address: signer.String()},
Metadata: meta,
}
Expand Down Expand Up @@ -395,7 +397,7 @@ func sdkEventToBalanceOperations(status string, event abci.Event) (operations []

op := &rosettatypes.Operation{
Type: event.Type,
Status: status,
Status: &status,
Account: &rosettatypes.AccountIdentifier{Address: accountIdentifier},
Amount: &rosettatypes.Amount{
Value: value,
Expand Down Expand Up @@ -520,7 +522,7 @@ func (c converter) SyncStatus(status *tmcoretypes.ResultStatus) *rosettatypes.Sy
}

return &rosettatypes.SyncStatus{
CurrentIndex: status.SyncInfo.LatestBlockHeight,
CurrentIndex: &status.SyncInfo.LatestBlockHeight,
TargetIndex: nil, // sync info does not allow us to get target height
Stage: &stage,
}
Expand Down
6 changes: 3 additions & 3 deletions server/rosetta/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"encoding/json"
"testing"

"github.com/cosmos/cosmos-sdk/server/rosetta"

abci "github.com/tendermint/tendermint/abci/types"

authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
Expand All @@ -17,7 +15,9 @@ import (

rosettatypes "github.com/coinbase/rosetta-sdk-go/types"
"github.com/stretchr/testify/suite"
crgerrs "github.com/tendermint/cosmos-rosetta-gateway/errors"

"github.com/cosmos/cosmos-sdk/server/rosetta"
crgerrs "github.com/cosmos/cosmos-sdk/server/rosetta/lib/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
Expand Down
146 changes: 146 additions & 0 deletions server/rosetta/lib/errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package errors

// errors.go contains all the errors returned by the adapter implementation
// plus some extra utilities to parse those errors

import (
"fmt"

grpccodes "google.golang.org/grpc/codes"
grpcstatus "google.golang.org/grpc/status"

"github.com/coinbase/rosetta-sdk-go/types"
)

// ListErrors lists all the registered errors
func ListErrors() []*types.Error {
return registry.list()
}

// SealAndListErrors seals the registry and lists its errors
func SealAndListErrors() []*types.Error {
registry.seal()
return registry.list()
}

// Error defines an error that can be converted to a Rosetta API error.
type Error struct {
rosErr *types.Error
}

func (e *Error) Error() string {
if e.rosErr == nil {
return ErrUnknown.Error()
}
return fmt.Sprintf("rosetta: (%d) %s", e.rosErr.Code, e.rosErr.Message)
}

// Is implements errors.Is for *Error, two errors are considered equal
// if their error codes are identical
func (e *Error) Is(err error) bool {
// assert it can be casted
rosErr, ok := err.(*Error)
if rosErr == nil || !ok {
return false
}
// check that both *Error's are correctly initialized to avoid dereference panics
if rosErr.rosErr == nil || e.rosErr == nil {
return false
}
// messages are equal if their error codes match
return rosErr.rosErr.Code == e.rosErr.Code
}

// WrapError wraps the rosetta error with additional context
func WrapError(err *Error, msg string) *Error {
return &Error{rosErr: &types.Error{
Code: err.rosErr.Code,
Message: err.rosErr.Message,
Description: err.rosErr.Description,
Retriable: err.rosErr.Retriable,
Details: map[string]interface{}{
"info": msg,
},
}}
}

// ToRosetta attempts to converting an error into a rosetta
// error, if the error cannot be converted it will be parsed as unknown
func ToRosetta(err error) *types.Error {
// if it's null or not known
rosErr, ok := err.(*Error)
if rosErr == nil || !ok {
return ToRosetta(WrapError(ErrUnknown, ErrUnknown.Error()))
}
return rosErr.rosErr
}

// FromGRPCToRosettaError converts a gRPC error to rosetta error
func FromGRPCToRosettaError(err error) *Error {
status, ok := grpcstatus.FromError(err)
if !ok {
return WrapError(ErrUnknown, err.Error())
}
switch status.Code() {
case grpccodes.NotFound:
return WrapError(ErrNotFound, status.Message())
case grpccodes.FailedPrecondition:
return WrapError(ErrBadArgument, status.Message())
case grpccodes.InvalidArgument:
return WrapError(ErrBadArgument, status.Message())
case grpccodes.Internal:
return WrapError(ErrInternal, status.Message())
default:
return WrapError(ErrUnknown, status.Message())
}
}

func RegisterError(code int32, message string, retryable bool, description string) *Error {
e := &Error{rosErr: &types.Error{
Code: code,
Message: message,
Description: &description,
Retriable: retryable,
Details: nil,
}}
registry.add(e)
return e
}

// Default error list
var (
// ErrUnknown defines an unknown error, if this is returned it means
// the library is ignoring an error
ErrUnknown = RegisterError(0, "unknown", false, "unknown error")
// ErrOffline is returned when there is an attempt to query an endpoint in offline mode
ErrOffline = RegisterError(1, "cannot query endpoint in offline mode", false, "returned when querying an online endpoint in offline mode")
// ErrNetworkNotSupported is returned when there is an attempt to query a network which is not supported
ErrNetworkNotSupported = RegisterError(2, "network is not supported", false, "returned when querying a non supported network")
// ErrCodec is returned when there's an error while marshalling or unmarshalling data
ErrCodec = RegisterError(3, "encode/decode error", true, "returned when there are errors encoding or decoding information to and from the node")
// ErrInvalidOperation is returned when the operation supplied to rosetta is not a valid one
ErrInvalidOperation = RegisterError(4, "invalid operation", false, "returned when the operation is not valid")
// ErrInvalidTransaction is returned when the provided hex bytes of a TX are not valid
ErrInvalidTransaction = RegisterError(5, "invalid transaction", false, "returned when the transaction is invalid")
// ErrInvalidAddress is returned when the byte of the address are bad
ErrInvalidAddress = RegisterError(7, "invalid address", false, "returned when the address is malformed")
// ErrInvalidPubkey is returned when the public key is invalid
ErrInvalidPubkey = RegisterError(8, "invalid pubkey", false, "returned when the public key is invalid")
// ErrInterpreting is returned when there are errors interpreting the data from the node, most likely related to breaking changes, version incompatibilities
ErrInterpreting = RegisterError(9, "error interpreting data from node", false, "returned when there are issues interpreting requests or response from node")
ErrInvalidMemo = RegisterError(11, "invalid memo", false, "returned when the memo is invalid")
// ErrBadArgument is returned when the request is malformed
ErrBadArgument = RegisterError(400, "bad argument", false, "request is malformed")
// ErrNotFound is returned when the required object was not found
// retry is set to true because something that is not found now
// might be found later, example: a TX
ErrNotFound = RegisterError(404, "not found", true, "returned when the node does not find what the client is asking for")
// ErrInternal is returned when the node is experiencing internal errors
ErrInternal = RegisterError(500, "internal error", false, "returned when the node experiences internal errors")
// ErrBadGateway is returned when there are problems interacting with the nodes
ErrBadGateway = RegisterError(502, "bad gateway", true, "return when the node is unreachable")
// ErrNotImplemented is returned when a method is not implemented yet
ErrNotImplemented = RegisterError(14, "not implemented", false, "returned when querying an endpoint which is not implemented")
// ErrUnsupportedCurve is returned when the curve specified is not supported
ErrUnsupportedCurve = RegisterError(15, "unsupported curve, expected secp256k1", false, "returned when using an unsupported crypto curve")
)
Loading

0 comments on commit 25ecec6

Please sign in to comment.