Skip to content

Commit

Permalink
Add simple status check for node to backend (#608)
Browse files Browse the repository at this point in the history
* wip

* wip
  • Loading branch information
diamondhands0 authored Apr 26, 2024
1 parent d33ed31 commit 00709df
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
10 changes: 9 additions & 1 deletion routes/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ const (
RoutePathStateChecksum = "/api/v0/state-checksum"

// validators.go
RoutePathValidators = "/api/v0/validators"
RoutePathValidators = "/api/v0/validators"
RoutePathCheckNodeStatus = "/api/v0/check-node-status"

// stake.go
RoutePathStake = "/api/v0/stake"
Expand Down Expand Up @@ -1353,6 +1354,13 @@ func (fes *APIServer) NewRouter() *muxtrace.Router {
fes.GetValidatorByPublicKeyBase58Check,
PublicAccess,
},
{
"CheckNodeStatus",
[]string{"POST", "OPTIONS"},
RoutePathCheckNodeStatus,
fes.CheckNodeStatus,
PublicAccess,
},
{
"CreateStakeTxn",
[]string{"POST", "OPTIONS"},
Expand Down
40 changes: 40 additions & 0 deletions routes/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"github.com/gorilla/mux"
"github.com/holiman/uint256"
"io"
"net"
"net/http"
"time"
)

type RegisterAsValidatorRequest struct {
Expand Down Expand Up @@ -352,6 +354,44 @@ func (fes *APIServer) GetValidatorByPublicKeyBase58Check(ww http.ResponseWriter,
}
}

type CheckNodeStatusRequest struct {
NodeHostPort string `safeForLogging:"true"`
}

type CheckNodeStatusResponse struct {
Success bool `safeForLogging:"true"`
}

func (fes *APIServer) CheckNodeStatus(ww http.ResponseWriter, req *http.Request) {
// Decode request body.
decoder := json.NewDecoder(io.LimitReader(req.Body, MaxRequestBodySizeBytes))
requestData := CheckNodeStatusRequest{}
if err := decoder.Decode(&requestData); err != nil {
_AddBadRequestError(ww, "UnjailValidator: problem parsing request body")
return
}

// We do an *extremely* simple check for now, which is that we just check to see if the node
// is reachable at all.
// TODO: We should beef this up to test an actual version handshake or something more robust.
conn, err := net.DialTimeout("tcp", requestData.NodeHostPort, 5*time.Second)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf(
"Problem connecting to %v: %v", requestData.NodeHostPort, err))
return
}
// If we get here it means we succeeded. Close the connection to clean up.
conn.Close()

res := CheckNodeStatusResponse{
Success: true,
}
if err := json.NewEncoder(ww).Encode(res); err != nil {
_AddInternalServerError(ww, "UnjailValidator: problem encoding response as JSON")
return
}
}

func _convertValidatorEntryToResponse(
utxoView *lib.UtxoView, validatorEntry *lib.ValidatorEntry, params *lib.DeSoParams,
) *ValidatorResponse {
Expand Down

0 comments on commit 00709df

Please sign in to comment.