Skip to content

Commit

Permalink
Created new node HTTP API route: GET /node/version.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickeskov committed Jul 2, 2021
1 parent 32bc576 commit 31f6b7b
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 14 deletions.
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ build-retransmitter-windows:
release-retransmitter: ver build-retransmitter-linux build-retransmitter-darwin build-retransmitter-windows

build-node-linux:
@GOOS=linux GOARCH=amd64 go build -o build/bin/linux-amd64/node ./cmd/node
@GOOS=linux GOARCH=amd64 go build -ldflags "-X main.buildVersion=$(VERSION)" -o build/bin/linux-amd64/node ./cmd/node
build-node-darwin:
@GOOS=darwin GOARCH=amd64 go build -o build/bin/darwin-amd64/node ./cmd/node
@GOOS=darwin GOARCH=amd64 go build -ldflags "-X main.buildVersion=$(VERSION)" -o build/bin/darwin-amd64/node ./cmd/node
build-node-windows:
@GOOS=windows GOARCH=amd64 go build -o build/bin/windows-amd64/node.exe ./cmd/node
@GOOS=windows GOARCH=amd64 go build -ldflags "-X main.buildVersion=$(VERSION)" -o build/bin/windows-amd64/node.exe ./cmd/node

release-node: ver build-node-linux build-node-darwin build-node-windows

Expand All @@ -103,11 +103,11 @@ dist-node: release-node build-node-mainnet-deb-package build-node-testnet-deb-pa
@cd ./build/bin/darwin-amd64/; tar pzcvf ../../dist/node_$(VERSION)_macOS-64bit.tar.gz ./node*

build-custom-linux:
@CGO_ENABLE=0 GOOS=linux GOARCH=amd64 go build -o build/bin/linux-amd64/custom ./cmd/custom
@CGO_ENABLE=0 GOOS=linux GOARCH=amd64 go build -ldflags "-X main.buildVersion=$(VERSION)" -o build/bin/linux-amd64/custom ./cmd/custom
build-custom-darwin:
@CGO_ENABLE=0 GOOS=darwin GOARCH=amd64 go build -o build/bin/darwin-amd64/custom ./cmd/custom
@CGO_ENABLE=0 GOOS=darwin GOARCH=amd64 go build -ldflags "-X main.buildVersion=$(VERSION)" -o build/bin/darwin-amd64/custom ./cmd/custom
build-custom-windows:
@CGO_ENABLE=0 GOOS=windows GOARCH=amd64 go build -o build/bin/windows-amd64/custom.exe ./cmd/custom
@CGO_ENABLE=0 GOOS=windows GOARCH=amd64 go build -ldflags "-X main.buildVersion=$(VERSION)" -o build/bin/windows-amd64/custom.exe ./cmd/custom

build-custom: ver build-custom-linux build-custom-darwin build-custom-windows

Expand Down
7 changes: 6 additions & 1 deletion cmd/custom/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ var (
dropPeers = flag.Bool("drop-peers", false, "Drop peers storage before node start.")
)

// nickeskov: compile time constants with defaults
var (
buildVersion = "(not specified)"
)

func init() {
common.SetupLogger(*logLevel)
}
Expand Down Expand Up @@ -268,7 +273,7 @@ func main() {
}

// TODO hardcore
app, err := api.NewApp("integration-test-rest-api", scheduler, nodeServices)
app, err := api.NewApp("integration-test-rest-api", scheduler, nodeServices, buildVersion)
if err != nil {
zap.S().Error(err)
cancel()
Expand Down
7 changes: 6 additions & 1 deletion cmd/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ var defaultPeers = map[string]string{
"stagenet": "88.99.185.128:6868,49.12.15.166:6868,95.216.205.3:6868,88.198.179.16:6868",
}

// nickeskov: compile time constants with defaults
var (
buildVersion = "(not specified)"
)

type Scheduler interface {
Mine() chan scheduler.Emit
types.Scheduler
Expand Down Expand Up @@ -378,7 +383,7 @@ func main() {
}
}

app, err := api.NewApp(*apiKey, minerScheduler, svs)
app, err := api.NewApp(*apiKey, minerScheduler, svs, buildVersion)
if err != nil {
zap.S().Error(err)
cancel()
Expand Down
8 changes: 7 additions & 1 deletion pkg/api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ type App struct {
peers peer_manager.PeerManager
sync types.StateSync
services services.Services
buildVersion string
}

func NewApp(apiKey string, scheduler SchedulerEmits, services services.Services) (*App, error) {
func NewApp(apiKey string, scheduler SchedulerEmits, services services.Services, buildVersion string) (*App, error) {
digest, err := crypto.SecureHash([]byte(apiKey))
if err != nil {
return nil, err
Expand All @@ -51,9 +52,14 @@ func NewApp(apiKey string, scheduler SchedulerEmits, services services.Services)
utx: services.UtxPool,
peers: services.Peers,
services: services,
buildVersion: buildVersion,
}, nil
}

func (a *App) BuildVersion() string {
return a.buildVersion
}

func (a *App) TransactionsBroadcast(ctx context.Context, b []byte) error {
tt := proto.TransactionTypeVersion{}
err := json.Unmarshal(b, &tt)
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/app_blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestApp_BlocksFirst(t *testing.T) {
s := mock.NewMockState(ctrl)
s.EXPECT().BlockByHeight(proto.Height(1)).Return(g, nil)

app, err := NewApp("api-key", nil, services.Services{State: s})
app, err := NewApp("api-key", nil, services.Services{State: s}, "")
require.NoError(t, err)
first, err := app.BlocksFirst()
require.NoError(t, err)
Expand All @@ -40,7 +40,7 @@ func TestApp_BlocksLast(t *testing.T) {
s.EXPECT().Height().Return(proto.Height(1), nil)
s.EXPECT().BlockByHeight(proto.Height(1)).Return(g, nil)

app, err := NewApp("api-key", nil, services.Services{State: s})
app, err := NewApp("api-key", nil, services.Services{State: s}, "")
require.NoError(t, err)
first, err := app.BlocksLast()
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/app_peers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestApp_PeersKnown(t *testing.T) {
addr := proto.NewTCPAddr(net.ParseIP("127.0.0.1"), 6868).ToIpPort()
peerManager.EXPECT().KnownPeers().Return([]storage.KnownPeer{storage.KnownPeer(addr)})

app, err := NewApp("key", nil, services.Services{Peers: peerManager})
app, err := NewApp("key", nil, services.Services{Peers: peerManager}, "")
require.NoError(t, err)

rs2, err := app.PeersKnown()
Expand Down Expand Up @@ -56,7 +56,7 @@ func TestApp_PeersSuspended(t *testing.T) {

peerManager.EXPECT().Suspended().Return(testData)

app, err := NewApp("key", nil, services.Services{Peers: peerManager})
app, err := NewApp("key", nil, services.Services{Peers: peerManager}, "")
require.NoError(t, err)

suspended := app.PeersSuspended()
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestAppAuth(t *testing.T) {
app, _ := NewApp("apiKey", nil, services.Services{})
app, _ := NewApp("apiKey", nil, services.Services{}, "")
require.Error(t, app.checkAuth("bla"))
require.NoError(t, app.checkAuth("apiKey"))
}
14 changes: 14 additions & 0 deletions pkg/api/node_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,20 @@ func (a *NodeApi) NodeStatus(w http.ResponseWriter, r *http.Request) error {
return nil
}

func (a *NodeApi) BuildVersion(w http.ResponseWriter, _ *http.Request) error {
type ver struct {
Version string `json:"version"`
}

buildVersion := a.app.BuildVersion()

out := ver{Version: fmt.Sprintf("GoWaves %s", buildVersion)}
if err := trySendJson(w, out); err != nil {
return errors.Wrap(err, "BuildVersion")
}
return nil
}

func (a *NodeApi) nodeProcesses(w http.ResponseWriter, _ *http.Request) error {
rs := a.app.NodeProcesses()
if err := trySendJson(w, rs); err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func (a *NodeApi) routes(opts *RunOptions) (chi.Router, error) {

r.Route("/node", func(r chi.Router) {
r.Get("/status", wrapper(a.NodeStatus))
r.Get("/version", wrapper(a.BuildVersion))

rAuth := r.With(checkAuthMiddleware)

Expand Down

0 comments on commit 31f6b7b

Please sign in to comment.