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

updated nodeapi #71

Merged
merged 2 commits into from
Nov 2, 2023
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
43 changes: 22 additions & 21 deletions nodeapi/nodeapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (
baseUrl = "/eigen"
// Spec version is the version of the avs node spec that this node is implementing
// see https://eigen.nethermind.io/docs/spec/api/#api-versioning
specVersion = "v0.0.1"
specSemVer = "v0.0.1"
)

type NodeHealth int
Expand All @@ -46,20 +46,22 @@ type nodeService struct {
}

type NodeApi struct {
health NodeHealth
nodeServices []nodeService
ipPortAddr string
logger logging.Logger
avsNameVersion string
avsNodeName string
avsNodeSemVer string
health NodeHealth
nodeServices []nodeService
ipPortAddr string
logger logging.Logger
}

func NewNodeApi(avsName, avsNodeSemVer, IpPortAddr string, logger logging.Logger) *NodeApi {
func NewNodeApi(avsNodeName, avsNodeSemVer, IpPortAddr string, logger logging.Logger) *NodeApi {
nodeApi := &NodeApi{
health: Healthy,
nodeServices: []nodeService{},
ipPortAddr: IpPortAddr,
logger: logger,
avsNameVersion: avsName + "/" + avsNodeSemVer,
avsNodeName: avsNodeName,
avsNodeSemVer: avsNodeSemVer,
health: Healthy,
nodeServices: []nodeService{},
ipPortAddr: IpPortAddr,
logger: logger,
}
return nodeApi
}
Expand Down Expand Up @@ -100,7 +102,7 @@ func (api *NodeApi) DeregisterService(serviceId string) error {
}

// Start starts the node api server in a goroutine
func (api *NodeApi) Start() {
func (api *NodeApi) Start() <-chan error {
api.logger.Infof("Starting node api server at address %v", api.ipPortAddr)

mux := http.NewServeMux()
Expand All @@ -109,24 +111,21 @@ func (api *NodeApi) Start() {
Handler: mux,
}

mux.HandleFunc(baseUrl+"/node/spec-version", api.specVersionHandler)
mux.HandleFunc(baseUrl+"/node/version", api.nodeVersionHandler)
mux.HandleFunc(baseUrl+"/node", api.nodeHandler)
mux.HandleFunc(baseUrl+"/node/health", api.healthHandler)
mux.HandleFunc(baseUrl+"/node/services", api.servicesHandler)
// Note: You'll need to extract the service_ID from the URL
// /node/services/{service_ID}/health
mux.HandleFunc(baseUrl+"/node/services/", api.serviceHealthHandler)

errChan := run(api.logger, &httpServer)
if err := <-errChan; err != nil {
api.logger.Fatal("error while running node_api server", "err", err)
}
return errChan
}

// https://eigen.nethermind.io/docs/metrics/metrics-api#get-eigennodespec-version
func (api *NodeApi) specVersionHandler(w http.ResponseWriter, r *http.Request) {
response := map[string]string{
"spec_version": specVersion,
"spec_version": specSemVer,
}
err := jsonResponse(w, response)
if err != nil {
Expand All @@ -135,9 +134,11 @@ func (api *NodeApi) specVersionHandler(w http.ResponseWriter, r *http.Request) {
}

// https://eigen.nethermind.io/docs/metrics/metrics-api#get-eigennodeversion
func (api *NodeApi) nodeVersionHandler(w http.ResponseWriter, r *http.Request) {
func (api *NodeApi) nodeHandler(w http.ResponseWriter, r *http.Request) {
response := map[string]string{
"version": api.avsNameVersion,
"node_name": api.avsNodeName,
"spec_version": specSemVer,
"node_version": api.avsNodeSemVer,
}
err := jsonResponse(w, response)
if err != nil {
Expand Down
20 changes: 16 additions & 4 deletions nodeapi/nodeapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/stretchr/testify/assert"

Expand All @@ -14,6 +15,17 @@ import (
var noopLogger = logging.NewNoopLogger()
var testNodeApi = NewNodeApi("testAvs", "v0.0.1", "localhost:8080", noopLogger)

// just making sure that the nodeapi starts without any errors
func TestStart(t *testing.T) {
errC := testNodeApi.Start()
select {
case <-time.After(3 * time.Second):
// consider it a pass if no errors received after 3 seconds
case err := <-errC:
assert.NoError(t, err)
}
}

func TestSpecVersionHandler(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/eigen/node/spec-version", nil)
w := httptest.NewRecorder()
Expand All @@ -30,11 +42,11 @@ func TestSpecVersionHandler(t *testing.T) {
assert.Equal(t, "{\"spec_version\":\"v0.0.1\"}\n", string(data))
}

func TestNodeVersionHandler(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/eigen/node/version", nil)
func TestNodeHandler(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/eigen/node", nil)
w := httptest.NewRecorder()

testNodeApi.nodeVersionHandler(w, req)
testNodeApi.nodeHandler(w, req)

res := w.Result()
defer res.Body.Close()
Expand All @@ -43,7 +55,7 @@ func TestNodeVersionHandler(t *testing.T) {
assert.NoError(t, err)

assert.Equal(t, http.StatusOK, res.StatusCode)
assert.Equal(t, "{\"version\":\"testAvs/v0.0.1\"}\n", string(data))
assert.Equal(t, "{\"node_name\":\"testAvs\",\"node_version\":\"v0.0.1\",\"spec_version\":\"v0.0.1\"}\n", string(data))
}

func TestHealthHandler(t *testing.T) {
Expand Down
Loading