Skip to content

Commit

Permalink
Merge pull request #16 from nodeset-org/dev
Browse files Browse the repository at this point in the history
Migrate API to Network Socket
  • Loading branch information
jclapis authored May 13, 2024
2 parents e96abb3 + 580ea0a commit fb99144
Show file tree
Hide file tree
Showing 17 changed files with 152 additions and 94 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on:
tags:
- v*
branches:
- master
- main
pull_request:
permissions:
Expand All @@ -13,8 +12,8 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.21.8
go-version: 1.21.10
- run: cd ${GITHUB_WORKSPACE} && go build .
2 changes: 1 addition & 1 deletion .github/workflows/commits.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Block Fixup Commit Merge
uses: 13rac1/[email protected]
12 changes: 7 additions & 5 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@ on:
tags:
- v*
branches:
- master
- main
pull_request:
permissions:
# Required: allow read access to the content for analysis.
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
# pull-requests: read
# Optional: allow write access to checks to allow the action to annotate code in the PR.
checks: write
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v4
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.21.8
- uses: actions/checkout@v3
go-version: 1.21.10
- name: golangci-lint
uses: golangci/golangci-lint-action@v4
uses: golangci/golangci-lint-action@v6
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: latest
Expand Down
7 changes: 3 additions & 4 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on:
tags:
- v*
branches:
- master
- main
pull_request:
permissions:
Expand All @@ -13,8 +12,8 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.21.8
go-version: 1.21.10
- run: go test ./...
8 changes: 5 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@ package swclient

import (
"log/slog"
"net/http/httptrace"
"net/url"

"github.com/rocket-pool/node-manager-core/api/client"
)

// Binder for the Hyperdrive daemon API server
type ApiClient struct {
context *client.RequesterContext
context client.IRequesterContext
Nodeset *NodesetRequester
Validator *ValidatorRequester
Wallet *WalletRequester
Status *StatusRequester
}

// Creates a new API client instance
func NewApiClient(baseRoute string, socketPath string, logger *slog.Logger) *ApiClient {
context := client.NewRequesterContext(baseRoute, socketPath, logger)
func NewApiClient(apiUrl *url.URL, logger *slog.Logger, tracer *httptrace.ClientTrace) *ApiClient {
context := client.NewNetworkRequesterContext(apiUrl, logger, tracer)

client := &ApiClient{
context: context,
Expand Down
6 changes: 3 additions & 3 deletions client/nodeset.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
)

type NodesetRequester struct {
context *client.RequesterContext
context client.IRequesterContext
}

func NewNodesetRequester(context *client.RequesterContext) *NodesetRequester {
func NewNodesetRequester(context client.IRequesterContext) *NodesetRequester {
return &NodesetRequester{
context: context,
}
Expand All @@ -25,7 +25,7 @@ func (r *NodesetRequester) GetName() string {
func (r *NodesetRequester) GetRoute() string {
return "nodeset"
}
func (r *NodesetRequester) GetContext() *client.RequesterContext {
func (r *NodesetRequester) GetContext() client.IRequesterContext {
return r.context
}

Expand Down
6 changes: 3 additions & 3 deletions client/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
)

type StatusRequester struct {
context *client.RequesterContext
context client.IRequesterContext
}

func NewStatusRequester(context *client.RequesterContext) *StatusRequester {
func NewStatusRequester(context client.IRequesterContext) *StatusRequester {
return &StatusRequester{
context: context,
}
Expand All @@ -24,7 +24,7 @@ func (r *StatusRequester) GetRoute() string {
return "status"
}

func (r *StatusRequester) GetContext() *client.RequesterContext {
func (r *StatusRequester) GetContext() client.IRequesterContext {
return r.context
}

Expand Down
6 changes: 3 additions & 3 deletions client/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
)

type ValidatorRequester struct {
context *client.RequesterContext
context client.IRequesterContext
}

func NewValidatorRequester(context *client.RequesterContext) *ValidatorRequester {
func NewValidatorRequester(context client.IRequesterContext) *ValidatorRequester {
return &ValidatorRequester{
context: context,
}
Expand All @@ -25,7 +25,7 @@ func (r *ValidatorRequester) GetName() string {
func (r *ValidatorRequester) GetRoute() string {
return "validator"
}
func (r *ValidatorRequester) GetContext() *client.RequesterContext {
func (r *ValidatorRequester) GetContext() client.IRequesterContext {
return r.context
}

Expand Down
6 changes: 3 additions & 3 deletions client/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
)

type WalletRequester struct {
context *client.RequesterContext
context client.IRequesterContext
}

func NewWalletRequester(context *client.RequesterContext) *WalletRequester {
func NewWalletRequester(context client.IRequesterContext) *WalletRequester {
return &WalletRequester{
context: context,
}
Expand All @@ -24,7 +24,7 @@ func (r *WalletRequester) GetName() string {
func (r *WalletRequester) GetRoute() string {
return "wallet"
}
func (r *WalletRequester) GetContext() *client.RequesterContext {
func (r *WalletRequester) GetContext() client.IRequesterContext {
return r.context
}

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ require (
github.com/goccy/go-json v0.10.2
github.com/google/uuid v1.6.0
github.com/gorilla/mux v1.8.1
github.com/nodeset-org/hyperdrive-daemon v0.4.2-dev.0.20240501154944-195be498d365
github.com/nodeset-org/hyperdrive-daemon v0.4.2-dev.0.20240509213433-213850c1cd29
github.com/rocket-pool/batch-query v1.0.0
github.com/rocket-pool/node-manager-core v0.3.0
github.com/rocket-pool/node-manager-core v0.3.1-0.20240507185958-ac96f93a88ec
github.com/urfave/cli/v2 v2.27.1
github.com/wealdtech/go-eth2-types/v2 v2.8.2
github.com/wealdtech/go-eth2-wallet-encryptor-keystorev4 v1.4.1
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A=
github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
github.com/nodeset-org/hyperdrive-daemon v0.4.2-dev.0.20240501154944-195be498d365 h1:RSunSkBjs5ILC+7syrK8bVIagrZEAvYGyyq0Ohcoa68=
github.com/nodeset-org/hyperdrive-daemon v0.4.2-dev.0.20240501154944-195be498d365/go.mod h1:65/oNCQjYcUeSr+hfb7EC1gGLNkaTJlCx6EEJhXh1N0=
github.com/nodeset-org/hyperdrive-daemon v0.4.2-dev.0.20240509213433-213850c1cd29 h1:HbR4esVHeb5Y0EMUFYei/0h7yIADHpwCPs/kfS9F/YU=
github.com/nodeset-org/hyperdrive-daemon v0.4.2-dev.0.20240509213433-213850c1cd29/go.mod h1:zXxPJ7tzwMJYORRhWJbVsQFOWqWoDGg7rsr4IYUJVBA=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
Expand Down Expand Up @@ -283,8 +283,8 @@ github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rocket-pool/batch-query v1.0.0 h1:5HejmT1n1fIdLIqUhTNwbkG2PGOPl3IVjCpFQcQZ4I4=
github.com/rocket-pool/batch-query v1.0.0/go.mod h1:d1CmxShzk0fioJ4yX0eFGhz2an1odnW/LZ2cp3eDGIQ=
github.com/rocket-pool/node-manager-core v0.3.0 h1:eXju+2URsEw6qaw92J3+JhqbtkHNrhE0GWNiEz7tKXo=
github.com/rocket-pool/node-manager-core v0.3.0/go.mod h1:f/w3jnzi3ipXet7acZ0pQhGbolU/g3IDVrqXcNWuHw4=
github.com/rocket-pool/node-manager-core v0.3.1-0.20240507185958-ac96f93a88ec h1:0mXsnjqHxJt1KnUTUFvKu2RynotExI1Sx+flSRt5QhI=
github.com/rocket-pool/node-manager-core v0.3.1-0.20240507185958-ac96f93a88ec/go.mod h1:f/w3jnzi3ipXet7acZ0pQhGbolU/g3IDVrqXcNWuHw4=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
Expand Down
68 changes: 42 additions & 26 deletions server/server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package server

import (
"path/filepath"
"fmt"
"sync"

swcommon "github.com/nodeset-org/hyperdrive-stakewise/common"
swnodeset "github.com/nodeset-org/hyperdrive-stakewise/server/nodeset"
Expand All @@ -12,39 +13,54 @@ import (
"github.com/rocket-pool/node-manager-core/api/server"
)

const (
CliOrigin string = "cli"
WebOrigin string = "net"
)
// ServerManager manages the API server run by the daemon
type ServerManager struct {
// The server for clients to interact with
apiServer *server.NetworkSocketApiServer
}

// Creates a new server manager
func NewServerManager(sp *swcommon.StakewiseServiceProvider, ip string, port uint16, stopWg *sync.WaitGroup) (*ServerManager, error) {
// Start the API server
apiServer, err := createServer(sp, ip, port)
if err != nil {
return nil, fmt.Errorf("error creating API server: %w", err)
}
err = apiServer.Start(stopWg)
if err != nil {
return nil, fmt.Errorf("error starting API server: %w", err)
}
fmt.Printf("API server started on %s:%d\n", ip, port)

type StakewiseServer struct {
*server.ApiServer
socketPath string
// Create the manager
mgr := &ServerManager{
apiServer: apiServer,
}
return mgr, nil
}

func NewStakewiseServer(origin string, sp *swcommon.StakewiseServiceProvider) (*StakewiseServer, error) {
// Stops and shuts down the servers
func (m *ServerManager) Stop() {
err := m.apiServer.Stop()
if err != nil {
fmt.Printf("WARNING: API server didn't shutdown cleanly: %s\n", err.Error())
}
}

// Creates a new Hyperdrive API server
func createServer(sp *swcommon.StakewiseServiceProvider, ip string, port uint16) (*server.NetworkSocketApiServer, error) {
apiLogger := sp.GetApiLogger()
subLogger := apiLogger.CreateSubLogger(origin)
ctx := subLogger.CreateContextWithLogger(sp.GetBaseContext())
ctx := apiLogger.CreateContextWithLogger(sp.GetBaseContext())

socketPath := filepath.Join(sp.GetUserDir(), swconfig.CliSocketFilename)
handlers := []server.IHandler{
swnodeset.NewNodesetHandler(subLogger, ctx, sp),
swvalidator.NewValidatorHandler(subLogger, ctx, sp),
swwallet.NewWalletHandler(subLogger, ctx, sp),
swstatus.NewStatusHandler(subLogger, ctx, sp),
swnodeset.NewNodesetHandler(apiLogger, ctx, sp),
swvalidator.NewValidatorHandler(apiLogger, ctx, sp),
swwallet.NewWalletHandler(apiLogger, ctx, sp),
swstatus.NewStatusHandler(apiLogger, ctx, sp),
}
server, err := server.NewApiServer(subLogger.Logger, socketPath, handlers, swconfig.DaemonBaseRoute, swconfig.ApiVersion)
server, err := server.NewNetworkSocketApiServer(apiLogger.Logger, ip, port, handlers, swconfig.DaemonBaseRoute, swconfig.ApiVersion)
if err != nil {
return nil, err
}

return &StakewiseServer{
ApiServer: server,
socketPath: socketPath,
}, nil
}

func (s *StakewiseServer) GetSocketPath() string {
return s.socketPath
return server, nil
}
2 changes: 2 additions & 0 deletions shared/config/ids/stakewise.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package ids
const (
// Param IDs
StakewiseEnableID string = "enable"
ApiPortID string = "apiPort"
DaemonContainerTagID string = "daemonContainerTag"
OperatorContainerTagID string = "operatorContainerTag"
AdditionalOpFlagsID string = "additionalOpFlags"
VerifyDepositRootsID string = "verifyDepositRoots"
Expand Down
3 changes: 1 addition & 2 deletions shared/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ const (
DaemonBaseRoute string = ModuleName
ApiVersion string = "1"
ApiClientRoute string = DaemonBaseRoute + "/api/v" + ApiVersion
CliSocketFilename string = ModuleName + "-cli.sock"
NetSocketFilename string = ModuleName + "-net.sock"
WalletFilename string = "wallet.json"
PasswordFilename string = "password.txt"
KeystorePasswordFile string = "secret.txt"
DepositDataFile string = "deposit-data.json"
DefaultApiPort uint16 = 8180

// Logging
ClientLogName string = "hd.log"
Expand Down
Loading

0 comments on commit fb99144

Please sign in to comment.