Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
abukosek committed May 11, 2020
1 parent f4c881f commit 697444a
Show file tree
Hide file tree
Showing 14 changed files with 1,516 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@
*.o
*.so
oasis-core-rosetta-gateway
tests/oasis-net-runner
tests/oasis-node
tests/oasis_core_release.tar.gz
tests/oasis-core
tests/rosetta-cli*
tests/validator-data
50 changes: 47 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/usr/bin/env gmake

OASIS_RELEASE := 20.6
ROSETTA_CLI_RELEASE := 0.1.5

OASIS_GO ?= go
GO := env -u GOPATH $(OASIS_GO)
GOLINT := env -u GOPATH golangci-lint
Expand All @@ -25,6 +28,21 @@ OFF = ""
ECHO = echo
endif

# Check which tool to use for downloading.
HAVE_WGET := $(shell which wget > /dev/null && echo 1)
ifdef HAVE_WGET
DOWNLOAD := wget --quiet --show-progress --progress=bar:force:noscroll -O
else
HAVE_CURL := $(shell which curl > /dev/null && echo 1)
ifdef HAVE_CURL
DOWNLOAD := curl --progress-bar --location -o
else
$(error Please install wget or curl)
endif
endif

ROOT := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))

.PHONY: all build clean fmt lint nuke test

all: build
Expand All @@ -34,9 +52,31 @@ build:
@$(ECHO) "$(CYAN)*** Building...$(OFF)"
@$(GO) build

test:
tests/oasis_core_release.tar.gz:
@$(ECHO) "$(MAGENTA)*** Downloading oasis-core release $(OASIS_RELEASE)...$(OFF)"
@$(DOWNLOAD) $@ https://github.com/oasislabs/oasis-core/releases/download/v$(OASIS_RELEASE)/oasis_core_$(OASIS_RELEASE)_linux_amd64.tar.gz

tests/oasis-net-runner: tests/oasis_core_release.tar.gz
@$(ECHO) "$(MAGENTA)*** Unpacking oasis-net-runner...$(OFF)"
@tar -xf $< -C tests oasis-net-runner

tests/oasis-node: tests/oasis_core_release.tar.gz
@$(ECHO) "$(MAGENTA)*** Unpacking oasis-node...$(OFF)"
@tar -xf $< -C tests oasis-node

tests/rosetta-cli.tar.gz:
@$(ECHO) "$(MAGENTA)*** Downloading rosetta-cli release $(ROSETTA_CLI_RELEASE)...$(OFF)"
@$(DOWNLOAD) $@ https://github.com/coinbase/rosetta-cli/archive/v$(ROSETTA_CLI_RELEASE).tar.gz

tests/rosetta-cli: tests/rosetta-cli.tar.gz
@$(ECHO) "$(MAGENTA)*** Building rosetta-cli...$(OFF)"
@tar -xf $< -C tests
@cd tests/rosetta-cli-$(ROSETTA_CLI_RELEASE) && go build
@cp tests/rosetta-cli-$(ROSETTA_CLI_RELEASE)/rosetta-cli tests/.

test: build tests/oasis-net-runner tests/oasis-node tests/rosetta-cli
@$(ECHO) "$(CYAN)*** Running tests...$(OFF)"
@$(GO) test --timeout 2m -race -v ./...
@$(ROOT)/tests/test.sh

fmt:
@$(ECHO) "$(CYAN)*** Formatting code...$(OFF)"
Expand All @@ -49,7 +89,11 @@ lint:
clean:
@$(ECHO) "$(CYAN)*** Cleaning up...$(OFF)"
@$(GO) clean
@-rm -f tests/oasis_core_release.tar.gz tests/oasis-net-runner tests/oasis-node
@-rm -rf tests/oasis-core
@-rm -f tests/rosetta-cli.tar.gz tests/rosetta-cli
@-rm -rf tests/rosetta-cli-$(ROSETTA_CLI_RELEASE) tests/validator-data

nuke:
nuke: clean
@$(ECHO) "$(CYAN)*** Cleaning up really well...$(OFF)"
@$(GO) clean -cache -testcache -modcache
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module github.com/oasislabs/oasis-core-rosetta-gateway

go 1.14

replace github.com/tendermint/tendermint => github.com/oasislabs/tendermint v0.33.4-oasis1

require (
github.com/coinbase/rosetta-sdk-go v0.1.5
github.com/oasislabs/oasis-core/go v0.0.0-20200507164617-35b7f62efec5
google.golang.org/grpc v1.29.1
)
74 changes: 72 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,77 @@
package main

import "fmt"
import (
"context"
"fmt"
"net/http"
"os"
"strconv"

"github.com/coinbase/rosetta-sdk-go/server"
"github.com/oasislabs/oasis-core/go/common/logging"

"github.com/oasislabs/oasis-core-rosetta-gateway/oasis-client"
"github.com/oasislabs/oasis-core-rosetta-gateway/services"
)

// GatewayPortEnvVar is the name of the environment variable that specifies
// which port the Oasis Rosetta gateway should run on.
const GatewayPortEnvVar = "OASIS_ROSETTA_GATEWAY_PORT"

var logger = logging.GetLogger("oasis-rosetta-gateway")

// NewBlockchainRouter returns a Mux http.Handler from a collection of
// Rosetta service controllers.
func NewBlockchainRouter(oasisClient oasis_client.OasisClient) http.Handler {
networkAPIController := server.NewNetworkAPIController(services.NewNetworkAPIService(oasisClient))
accountAPIController := server.NewAccountAPIController(services.NewAccountAPIService(oasisClient))
blockAPIController := server.NewBlockAPIController(services.NewBlockAPIService(oasisClient))
constructionAPIController := server.NewConstructionAPIController(services.NewConstructionAPIService(oasisClient))

return server.NewRouter(networkAPIController, accountAPIController, blockAPIController, constructionAPIController)
}

func main() {
fmt.Println("Hello, world.")
// Get server port from environment variable or use the default.
port := os.Getenv(GatewayPortEnvVar)
if port == "" {
port = "8080"
}
nPort, err := strconv.Atoi(port)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Malformed %s environment variable: %v\n", GatewayPortEnvVar, err)
os.Exit(1)
}

// Prepare a new Oasis gRPC client.
oasisClient, err := oasis_client.New()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Failed to prepare Oasis gRPC client: %v\n", err)
os.Exit(1)
}

// Make a test request using the client to see if the node works.
cid, err := oasisClient.GetChainID(context.Background())
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Node connectivity error: %v\n", err)
os.Exit(1)
}

// Initialize logging.
err = logging.Initialize(os.Stdout, logging.FmtLogfmt, logging.LevelDebug, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Unable to initialize logging: %v\n", err)
os.Exit(1)
}

logger.Info("Connected to Oasis node", "chain_context", cid)

// Start the server.
router := NewBlockchainRouter(oasisClient)
logger.Info("Oasis Rosetta Gateway listening", "port", nPort)
err = http.ListenAndServe(fmt.Sprintf(":%d", nPort), router)
if err != nil {
fmt.Fprintf(os.Stderr, "Oasis Rosetta Gateway server exited with error: %v\n", err)
os.Exit(1)
}
}
Loading

0 comments on commit 697444a

Please sign in to comment.