2021-11-03 11:18:49 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2021-12-22 19:28:58 +00:00
|
|
|
"fmt"
|
2021-11-03 11:18:49 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2021-11-13 09:23:43 +00:00
|
|
|
settings "github.com/bugout-dev/moonstream/nodes/server/configs"
|
2021-11-03 11:18:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func pingRoute(w http.ResponseWriter, req *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
response := PingResponse{Status: "ok"}
|
|
|
|
json.NewEncoder(w).Encode(response)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch latest block from Geth
|
2021-11-13 09:23:43 +00:00
|
|
|
func (es *extendedServer) pingGethRoute(w http.ResponseWriter, req *http.Request) {
|
2021-11-03 11:18:49 +00:00
|
|
|
postBody, err := json.Marshal(map[string]interface{}{
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"method": "eth_blockNumber",
|
|
|
|
"params": []string{},
|
|
|
|
"id": 1,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("An error occurred due marshal postBody, error: %s", err)
|
|
|
|
http.Error(w, http.StatusText(500), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2022-05-10 10:23:31 +00:00
|
|
|
if es.blockchain != "ethereum" && es.blockchain != "polygon" && es.blockchain != "xdai" {
|
2021-11-13 09:23:43 +00:00
|
|
|
log.Printf("Unaccepted blockchain type: %s", es.blockchain)
|
|
|
|
http.Error(w, http.StatusText(400), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2021-12-22 19:28:58 +00:00
|
|
|
MOONSTREAM_WEB3_PROVIDER_URI := fmt.Sprintf("http://%s:8545", settings.LOCAL_IPV4)
|
|
|
|
gethResponse, err := http.Post(MOONSTREAM_WEB3_PROVIDER_URI, "application/json",
|
2021-11-03 11:18:49 +00:00
|
|
|
bytes.NewBuffer(postBody))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Unable to request geth, error: %s", err)
|
|
|
|
http.Error(w, http.StatusText(500), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer gethResponse.Body.Close()
|
|
|
|
|
|
|
|
gethResponseBody, err := ioutil.ReadAll(gethResponse.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Unable to read geth response, error: %s", err)
|
|
|
|
http.Error(w, http.StatusText(500), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var obj GethResponse
|
|
|
|
_ = json.Unmarshal(gethResponseBody, &obj)
|
|
|
|
|
|
|
|
blockNumberHex := strings.Replace(obj.Result, "0x", "", -1)
|
|
|
|
blockNumberStr, err := strconv.ParseUint(blockNumberHex, 16, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Unable to parse block number from hex to string, error: %s", err)
|
|
|
|
http.Error(w, http.StatusText(500), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
response := PingGethResponse{CurrentBlock: blockNumberStr}
|
|
|
|
json.NewEncoder(w).Encode(response)
|
|
|
|
}
|