Skip to content

Commit 00709df

Browse files
Add simple status check for node to backend (#608)
* wip * wip
1 parent d33ed31 commit 00709df

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

routes/server.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,8 @@ const (
317317
RoutePathStateChecksum = "/api/v0/state-checksum"
318318

319319
// validators.go
320-
RoutePathValidators = "/api/v0/validators"
320+
RoutePathValidators = "/api/v0/validators"
321+
RoutePathCheckNodeStatus = "/api/v0/check-node-status"
321322

322323
// stake.go
323324
RoutePathStake = "/api/v0/stake"
@@ -1353,6 +1354,13 @@ func (fes *APIServer) NewRouter() *muxtrace.Router {
13531354
fes.GetValidatorByPublicKeyBase58Check,
13541355
PublicAccess,
13551356
},
1357+
{
1358+
"CheckNodeStatus",
1359+
[]string{"POST", "OPTIONS"},
1360+
RoutePathCheckNodeStatus,
1361+
fes.CheckNodeStatus,
1362+
PublicAccess,
1363+
},
13561364
{
13571365
"CreateStakeTxn",
13581366
[]string{"POST", "OPTIONS"},

routes/validators.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"github.com/gorilla/mux"
1010
"github.com/holiman/uint256"
1111
"io"
12+
"net"
1213
"net/http"
14+
"time"
1315
)
1416

1517
type RegisterAsValidatorRequest struct {
@@ -352,6 +354,44 @@ func (fes *APIServer) GetValidatorByPublicKeyBase58Check(ww http.ResponseWriter,
352354
}
353355
}
354356

357+
type CheckNodeStatusRequest struct {
358+
NodeHostPort string `safeForLogging:"true"`
359+
}
360+
361+
type CheckNodeStatusResponse struct {
362+
Success bool `safeForLogging:"true"`
363+
}
364+
365+
func (fes *APIServer) CheckNodeStatus(ww http.ResponseWriter, req *http.Request) {
366+
// Decode request body.
367+
decoder := json.NewDecoder(io.LimitReader(req.Body, MaxRequestBodySizeBytes))
368+
requestData := CheckNodeStatusRequest{}
369+
if err := decoder.Decode(&requestData); err != nil {
370+
_AddBadRequestError(ww, "UnjailValidator: problem parsing request body")
371+
return
372+
}
373+
374+
// We do an *extremely* simple check for now, which is that we just check to see if the node
375+
// is reachable at all.
376+
// TODO: We should beef this up to test an actual version handshake or something more robust.
377+
conn, err := net.DialTimeout("tcp", requestData.NodeHostPort, 5*time.Second)
378+
if err != nil {
379+
_AddBadRequestError(ww, fmt.Sprintf(
380+
"Problem connecting to %v: %v", requestData.NodeHostPort, err))
381+
return
382+
}
383+
// If we get here it means we succeeded. Close the connection to clean up.
384+
conn.Close()
385+
386+
res := CheckNodeStatusResponse{
387+
Success: true,
388+
}
389+
if err := json.NewEncoder(ww).Encode(res); err != nil {
390+
_AddInternalServerError(ww, "UnjailValidator: problem encoding response as JSON")
391+
return
392+
}
393+
}
394+
355395
func _convertValidatorEntryToResponse(
356396
utxoView *lib.UtxoView, validatorEntry *lib.ValidatorEntry, params *lib.DeSoParams,
357397
) *ValidatorResponse {

0 commit comments

Comments
 (0)