Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial implementation #4

Merged
merged 1 commit into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.2.0

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
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
# Oasis Gateway for Rosetta

This repository implements the [Rosetta][1] server for the [Oasis][0] network.

To build the server:

make

To run tests:

make test

To clean-up:

make clean


`make test` will automatically download the [Oasis node][0] and [rosetta-cli][2],
set up a test Oasis network, make some sample transactions, then run the
gateway and validate it using `rosetta-cli`.

[0]: https://github.com/oasislabs/oasis-core
[1]: https://github.com/coinbase/rosetta-sdk-go
[2]: https://github.com/coinbase/rosetta-cli
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