The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/rosetta/rosetta.go

88 lines
2.3 KiB

Node API Refactor - pt3 (Stage 3.1 of Node API Overhaul) (#3297) * [rosetta] Add server with example block & network services * Update go.mod for rosetta SDK Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [internal/configs] Add RosettaServer to node config Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [cmd] Add rosetta port flag Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [node] Add rosetta server start & rename api.go Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Change server start to use nodeconfig of rosetta Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [cmd] Cleanup debugging prints Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Fix stdout print & document placeholders Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Fix lint & make StartSevers more consistent Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [cmd] Disable rosetta by default Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [test] Make explorer deploy rosetta server Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Use direct http server for start * Make go.mod changes minimal Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Fix fmt Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Fix fmt of go.mod Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Use port 9700 instead of 10000 Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [cmd] Bump config version Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [cmd] Add v1.0.0 config backwards compatibility test * Included update message if old config is loaded Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [test] Do not broadcast invalid tx on localnet Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [cmd] Correct for invalid port when loading old config Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [cmd] Make rosetta variable names consistent Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu>
4 years ago
package rosetta
import (
"fmt"
"net"
"net/http"
"time"
"github.com/coinbase/rosetta-sdk-go/asserter"
"github.com/coinbase/rosetta-sdk-go/server"
"github.com/coinbase/rosetta-sdk-go/types"
"github.com/harmony-one/harmony/hmy"
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
"github.com/harmony-one/harmony/internal/utils"
common "github.com/harmony-one/harmony/rosetta/common"
"github.com/harmony-one/harmony/rosetta/services"
)
// StartServers starts the rosetta http server
func StartServers(hmy *hmy.Harmony, config nodeconfig.RosettaServerConfig) error {
if !config.HTTPEnabled {
utils.Logger().Info().Msg("Rosetta http server disabled...")
return nil
}
network := common.GetNetwork(hmy.ShardID)
serverAsserter, err := asserter.NewServer(
common.TransactionTypes,
nodeconfig.GetDefaultConfig().Role() == nodeconfig.ExplorerNode,
[]*types.NetworkIdentifier{network},
)
if err != nil {
return err
}
router := server.CorsMiddleware(loggerMiddleware(getRouter(network, serverAsserter, hmy)))
utils.Logger().Info().
Int("port", config.HTTPPort).
Str("ip", config.HTTPIp).
Msg("Starting Rosetta server")
endpoint := fmt.Sprintf("%s:%d", config.HTTPIp, config.HTTPPort)
var (
listener net.Listener
)
if listener, err = net.Listen("tcp", endpoint); err != nil {
return err
}
go newHTTPServer(router).Serve(listener)
return nil
}
func newHTTPServer(handler http.Handler) *http.Server {
return &http.Server{
Handler: handler,
ReadTimeout: common.ReadTimeout,
WriteTimeout: common.WriteTimeout,
IdleTimeout: common.IdleTimeout,
}
}
func getRouter(
network *types.NetworkIdentifier,
asserter *asserter.Asserter,
hmy *hmy.Harmony,
) http.Handler {
return server.NewRouter(
server.NewNetworkAPIController(services.NewNetworkAPIService(network, hmy), asserter),
server.NewBlockAPIController(services.NewBlockAPIService(network, hmy), asserter),
)
}
func loggerMiddleware(router http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
router.ServeHTTP(w, r)
msg := fmt.Sprintf(
"Rosetta: %s %s %s",
r.Method,
r.RequestURI,
time.Since(start),
)
utils.Logger().Info().Msg(msg)
// Print to stdout for quick check of rosetta activity
fmt.Printf("%s %s\n", time.Now().Format("2006-01-02 15:04:05"), msg)
})
}