|
9 | 9 | "github.com/gorilla/mux" |
10 | 10 | "github.com/holiman/uint256" |
11 | 11 | "io" |
| 12 | + "net" |
12 | 13 | "net/http" |
| 14 | + "time" |
13 | 15 | ) |
14 | 16 |
|
15 | 17 | type RegisterAsValidatorRequest struct { |
@@ -352,6 +354,44 @@ func (fes *APIServer) GetValidatorByPublicKeyBase58Check(ww http.ResponseWriter, |
352 | 354 | } |
353 | 355 | } |
354 | 356 |
|
| 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 | + |
355 | 395 | func _convertValidatorEntryToResponse( |
356 | 396 | utxoView *lib.UtxoView, validatorEntry *lib.ValidatorEntry, params *lib.DeSoParams, |
357 | 397 | ) *ValidatorResponse { |
|
0 commit comments