cosmos-sdk/server/rosetta/lib/server/server.go
Marin Basic b565069bcf
build(deps): upgrade rosetta to 0.7.0 (#10432)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->

## Description

Closes: #10155

<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->

Rosetta v0.7.0 introduces a new parameter while initializing Asserter. 
This includes more validation by passing validation config path.
Since we are not using this feature, we are passing a empty string.

[Here is the rosetta release changelog](https://github.com/coinbase/rosetta-sdk-go/releases/tag/v0.7.0)

---

### 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/master/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/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] 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)
2021-10-26 13:14:13 +00:00

118 lines
2.8 KiB
Go

package server
import (
"fmt"
"net/http"
"time"
assert "github.com/coinbase/rosetta-sdk-go/asserter"
"github.com/coinbase/rosetta-sdk-go/server"
"github.com/coinbase/rosetta-sdk-go/types"
"github.com/cosmos/cosmos-sdk/server/rosetta/lib/internal/service"
crgtypes "github.com/cosmos/cosmos-sdk/server/rosetta/lib/types"
)
const DefaultRetries = 5
const 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
}
func (h Server) Start() error {
return http.ListenAndServe(h.addr, h.h)
}
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)
}
var (
adapter crgtypes.API
)
switch settings.Offline {
case true:
adapter, err = newOfflineAdapter(settings)
case false:
adapter, err = newOnlineAdapter(settings)
}
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,
}, 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) (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 {
time.Sleep(settings.RetryWait)
continue
}
return service.NewOnlineNetwork(settings.Network, settings.Client)
}
return nil, fmt.Errorf("maximum number of retries exceeded, last error: %w", err)
}