cosmos-sdk/tools/rosetta/lib/server/server.go
sleepy ramen 39c5c079cd
fix: misc fixes for cosmos-rosetta (#13583)
### Description

Closes:
https://github.com/cosmos/cosmos-sdk/issues/13083
https://github.com/cosmos/cosmos-sdk/issues/11402
https://github.com/cosmos/cosmos-sdk/issues/10678
https://github.com/cosmos/cosmos-sdk/issues/12358
https://github.com/cosmos/cosmos-sdk/issues/10776
https://github.com/cosmos/cosmos-sdk/issues/12934

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules)
- [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [x] added a changelog entry to `CHANGELOG.md`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
2022-11-04 11:48:45 +00:00

126 lines
3.1 KiB
Go

package server
import (
"fmt"
"net/http"
"os"
"time"
assert "github.com/coinbase/rosetta-sdk-go/asserter"
"github.com/coinbase/rosetta-sdk-go/server"
"github.com/coinbase/rosetta-sdk-go/types"
"cosmossdk.io/tools/rosetta/lib/internal/service"
crgtypes "cosmossdk.io/tools/rosetta/lib/types"
"github.com/tendermint/tendermint/libs/log"
)
const (
DefaultRetries = 5
DefaultRetryWait = 5 * time.Second
)
// Settings define the rosetta server settings
type Settings struct {
// Network contains the information regarding the network
Network *types.NetworkIdentifier
// Client is the online API handler
Client crgtypes.Client
// Listen is the address the handler will listen at
Listen string
// Offline defines if the rosetta service should be exposed in offline mode
Offline bool
// Retries is the number of readiness checks that will be attempted when instantiating the handler
// valid only for online API
Retries int
// RetryWait is the time that will be waited between retries
RetryWait time.Duration
}
type Server struct {
h http.Handler
addr string
logger log.Logger
}
func (h Server) Start() error {
h.logger.Info(fmt.Sprintf("Rosetta server listening on add %s", h.addr))
return http.ListenAndServe(h.addr, h.h) //nolint:gosec
}
func NewServer(settings Settings) (Server, error) {
asserter, err := assert.NewServer(
settings.Client.SupportedOperations(),
true,
[]*types.NetworkIdentifier{settings.Network},
nil,
false,
"",
)
if err != nil {
return Server{}, fmt.Errorf("cannot build asserter: %w", err)
}
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
var adapter crgtypes.API
switch settings.Offline {
case true:
adapter, err = newOfflineAdapter(settings)
case false:
adapter, err = newOnlineAdapter(settings, logger)
}
if err != nil {
return Server{}, err
}
h := server.NewRouter(
server.NewAccountAPIController(adapter, asserter),
server.NewBlockAPIController(adapter, asserter),
server.NewNetworkAPIController(adapter, asserter),
server.NewMempoolAPIController(adapter, asserter),
server.NewConstructionAPIController(adapter, asserter),
)
return Server{
h: h,
addr: settings.Listen,
logger: logger,
}, nil
}
func newOfflineAdapter(settings Settings) (crgtypes.API, error) {
if settings.Client == nil {
return nil, fmt.Errorf("client is nil")
}
return service.NewOffline(settings.Network, settings.Client)
}
func newOnlineAdapter(settings Settings, logger log.Logger) (crgtypes.API, error) {
if settings.Client == nil {
return nil, fmt.Errorf("client is nil")
}
if settings.Retries <= 0 {
settings.Retries = DefaultRetries
}
if settings.RetryWait == 0 {
settings.RetryWait = DefaultRetryWait
}
var err error
err = settings.Client.Bootstrap()
if err != nil {
return nil, err
}
for i := 0; i < settings.Retries; i++ {
err = settings.Client.Ready()
if err != nil {
logger.Error(fmt.Sprintf("[Rosetta]- Client is not ready: %v. Retrying ...", err))
time.Sleep(settings.RetryWait)
continue
}
return service.NewOnlineNetwork(settings.Network, settings.Client, logger)
}
return nil, fmt.Errorf("maximum number of retries exceeded, last error: %w", err)
}