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

View File

@ -1,6 +1,9 @@
package server package server
import ( import (
"fmt"
"net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -15,3 +18,28 @@ func Start(port string, ginMode string) error {
return router.Run(port) 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) { func (r *RpcClient) EthRpcCall(method string) (*jsonrpc.RPCResponse, error) {
response, err := r.ethClient.Call(method) response, err := r.ethClient.Call(method)
if err != nil { if err != nil {
return response, err return nil, err
} }
if response.Error != nil { 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 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
}