add: batch rpc calls

This commit is contained in:
Mohamed Sohail 2021-12-14 17:50:07 +03:00
parent 32ad2d84aa
commit 92797c4ea5
Signed by: kamikazechaser
GPG Key ID: 7DD45520C01CD85D
3 changed files with 73 additions and 34 deletions

View File

@ -1,52 +1,51 @@
package server
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"git.grassecon.org/grassrootseconomics/openethereum-node-status/pkg/rpc"
)
type (
R struct {
Result string `json:"result"`
}
P map[string]interface{}
"github.com/rs/zerolog/log"
"github.com/ybbus/jsonrpc/v2"
)
var (
RpcClient rpc.RpcClient
)
func metricsHandler(c *gin.Context) {
// pllholder rpc call
rpcResponse, err := RpcClient.EthRpcCall("eth_gasPrice")
type BatchResponse struct {
SyncComplete bool
ChainName string
}
func batchCall() (bool, BatchResponse) {
var batchResponse BatchResponse
rpcResponses, err := RpcClient.EthBatchRpcCall(jsonrpc.RPCRequests{
jsonrpc.NewRequest("eth_syncing"),
jsonrpc.NewRequest("parity_chain"),
})
if err != nil {
log.Error().
Err(err).
Str("module", "server").
Msg("rpc call failed")
Msg("error in batch responses")
return false, batchResponse
}
value, err := rpcResponse.GetString()
if syncStatus, ok := rpcResponses[0]; ok {
val, err := syncStatus.GetBool()
if err != nil {
log.Error().
Err(err).
Str("module", "server").
Msg("rpc response to string conversion failed")
return false, batchResponse
}
batchResponse.SyncComplete = !val
}
c.JSON(http.StatusOK, gin.H{
"ok": true,
"data": value,
})
if chainName, ok := rpcResponses[1]; ok {
val, err := chainName.GetString()
if err != nil {
return false, batchResponse
}
batchResponse.ChainName = val
}
func healthHandler(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"ok": true,
})
return true, batchResponse
}

View File

@ -1,6 +1,9 @@
package server
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
@ -15,3 +18,28 @@ func Start(port string, ginMode string) error {
return router.Run(port)
}
func healthHandler(c *gin.Context) {
allOk, data := batchCall()
fmt.Println(data.SyncComplete)
if !allOk {
c.JSON(http.StatusExpectationFailed, gin.H{
"ok": false,
"message": "could not get node health status",
})
return
}
c.JSON(http.StatusOK, gin.H{
"ok": true,
"syncComplete": data.SyncComplete,
"chainName": data.ChainName,
})
}
func metricsHandler(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"ok": true,
})
}

View File

@ -19,12 +19,24 @@ func NewRpcClient(endpoint string) RpcClient {
func (r *RpcClient) EthRpcCall(method string) (*jsonrpc.RPCResponse, error) {
response, err := r.ethClient.Call(method)
if err != nil {
return response, err
return nil, err
}
if response.Error != nil {
return response, fmt.Errorf("rpc client error: (code %d) %s", response.Error.Code, response.Error.Message)
return nil, fmt.Errorf("rpc client error: (code %d) %s", response.Error.Code, response.Error.Message)
}
return response, nil
}
func (r *RpcClient) EthBatchRpcCall(batchedMethods jsonrpc.RPCRequests) (map[int]*jsonrpc.RPCResponse, error) {
response, _ := r.ethClient.CallBatch(batchedMethods)
if response.HasError() {
return nil, fmt.Errorf("batch rpc request failed")
}
results := response.AsMap()
return results, nil
}