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

Add version info to common package and -version flag to CLI #128

Merged
merged 4 commits into from
Dec 4, 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
4 changes: 4 additions & 0 deletions .changelog/128.feature.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
common: Add package implementing common things for Oasis Core Rosetta Gateway

Initially, it stores the versions of the Rosetta API, Go toolchain and the
Oasis Core Rosetta Gateway itself.
1 change: 1 addition & 0 deletions .changelog/128.feature.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cli: Add `-version` flag to `oasis-core-rosetta-gateway` binary
1 change: 1 addition & 0 deletions .changelog/128.internal.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cli: Extract port setting steps to `getPortOrExit()` function
1 change: 1 addition & 0 deletions .changelog/128.internal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make: Add reproducibility and version info flags to Go builds
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,23 @@ $(error Please install wget or curl)
endif
endif

# Check if Go's linkers flags are set in common.mk and add them as extra flags.
ifneq ($(GOLDFLAGS),)
GO_EXTRA_FLAGS += -ldflags $(GOLDFLAGS)
endif

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

all: build
@$(ECHO) "$(CYAN)*** Everything built successfully!$(OFF)"

build:
@$(ECHO) "$(CYAN)*** Building...$(OFF)"
@$(GO) build
@$(GO) build $(GOFLAGS) $(GO_EXTRA_FLAGS)

build-tests:
@$(ECHO) "$(CYAN)*** Building tests...$(OFF)"
@$(GO) build ./tests/...
@$(GO) build $(GOFLAGS) $(GO_EXTRA_FLAGS) ./tests/...

tests/oasis_core_release.tar.gz:
@$(ECHO) "$(MAGENTA)*** Downloading oasis-core release $(OASIS_RELEASE)...$(OFF)"
Expand Down
10 changes: 10 additions & 0 deletions common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ export OASIS_GO ?= go
# Go command prefix to use in all Go commands.
GO := env -u GOPATH $(OASIS_GO)

# NOTE: The -trimpath flag strips all host dependent filesystem paths from
# binaries which is required for deterministic builds.
GOFLAGS ?= -trimpath -v

# Project's version as the linker's string value definition.
export GOLDFLAGS_VERSION := -X github.com/oasisprotocol/oasis-core-rosetta-gateway/common.SoftwareVersion=$(VERSION)

# Go's linker flags.
export GOLDFLAGS ?= "$(GOLDFLAGS_VERSION)"

# Helper that ensures the git workspace is clean.
define ENSURE_GIT_CLEAN =
if [[ ! -z `git status --porcelain` ]]; then \
Expand Down
20 changes: 20 additions & 0 deletions common/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Package common implements common things for Oasis Core Rosetta Gateway.
package common

import (
"runtime"
"strings"
)

var (
// SoftwareVersion represents the Oasis Core Rosetta Gateway's version and
// should be set by the linker.
SoftwareVersion = "0.0.0-unset"

// RosettaAPIVersion represents the Rosetta API version with which the
// Oasis Core Rosetta Gateway is guaranteed to be compatible with.
RosettaAPIVersion = "1.4.1"
Copy link
Member Author

@tjanez tjanez Dec 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be hard-coded in the future, see #129.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also make https://github.com/oasisprotocol/oasis-core-rosetta-gateway/blob/master/services/network.go#L135 use the RosettaAPIVersion specified here :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also make https://github.com/oasisprotocol/oasis-core-rosetta-gateway/blob/master/services/network.go#L135 use the RosettaAPIVersion specified here :)

Good catch, fixed.


// ToolchainVersion is the version of the Go compiler/standard library.
ToolchainVersion = strings.TrimPrefix(runtime.Version(), "go")
)
57 changes: 42 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"flag"
"fmt"
"net/http"
"os"
Expand All @@ -15,6 +16,7 @@ import (
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
"github.com/oasisprotocol/oasis-core/go/common/logging"

"github.com/oasisprotocol/oasis-core-rosetta-gateway/common"
"github.com/oasisprotocol/oasis-core-rosetta-gateway/oasis"
"github.com/oasisprotocol/oasis-core-rosetta-gateway/services"
)
Expand All @@ -30,7 +32,11 @@ const GatewayPortEnvVar = "OASIS_ROSETTA_GATEWAY_PORT"
// Don't forget to set services.OfflineModeChainIDEnvVar as well.
const OfflineModeEnvVar = "OASIS_ROSETTA_GATEWAY_OFFLINE_MODE"

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

versionFlag = flag.Bool("version", false, "Print version and exit")
)

// NewBlockchainRouter returns a Mux http.Handler from a collection of
// Rosetta service controllers.
Expand Down Expand Up @@ -113,29 +119,50 @@ func getEnvVarOrExit(name string) string {
return value
}

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

// Get server port from environment variable or use the default.
port := os.Getenv(GatewayPortEnvVar)
if port == "" {
port = "8080"
// Return the server port that should be used or exit if it is malformed.
func getPortOrExit() int {
portStr := os.Getenv(GatewayPortEnvVar)
if portStr == "" {
portStr = "8080"
}
nPort, err := strconv.Atoi(port)
port, err := strconv.Atoi(portStr)
if err != nil {
logger.Error("malformed environment variable",
"err", err,
"name", GatewayPortEnvVar,
)
os.Exit(1)
}
return port
}

// Print version information.
func printVersionInfo() {
fmt.Printf("Software version: %s\n", common.SoftwareVersion)
fmt.Printf("Rosetta API version: %s\n", common.RosettaAPIVersion)
fmt.Printf("Go toolchain version: %s\n", common.ToolchainVersion)
kostko marked this conversation as resolved.
Show resolved Hide resolved
}

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

// Print version info if -version flag is passed.
flag.Parse()
if *versionFlag {
printVersionInfo()
return
}

// Get server port.
port := getPortOrExit()

var chainID string
var oasisClient oasis.Client
var err error

// Check if we should run in offline mode.
offlineMode := os.Getenv(OfflineModeEnvVar) != ""
Expand Down Expand Up @@ -197,8 +224,8 @@ func main() {
}

// Start the server.
logger.Info("Oasis Rosetta Gateway listening", "port", nPort)
err = http.ListenAndServe(fmt.Sprintf(":%d", nPort), router)
logger.Info("Oasis Rosetta Gateway listening", "port", port)
err = http.ListenAndServe(fmt.Sprintf(":%d", port), router)
if err != nil {
logger.Error("Oasis Rosetta Gateway server exited",
"err", err,
Expand Down
3 changes: 2 additions & 1 deletion services/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/oasisprotocol/oasis-core/go/common/logging"

"github.com/oasisprotocol/oasis-core-rosetta-gateway/common"
"github.com/oasisprotocol/oasis-core-rosetta-gateway/oasis"
)

Expand Down Expand Up @@ -132,7 +133,7 @@ func (s *networkAPIService) NetworkOptions(

return &types.NetworkOptionsResponse{
Version: &types.Version{
RosettaVersion: "1.4.0",
RosettaVersion: common.RosettaAPIVersion,
NodeVersion: status.SoftwareVersion,
},
Allow: &types.Allow{
Expand Down