2021-12-14 12:24:18 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2021-12-14 16:52:59 +01:00
|
|
|
"git.grassecon.net/grassrootseconomics/openethereum-node-status/pkg/rpc"
|
|
|
|
"git.grassecon.net/grassrootseconomics/openethereum-node-status/pkg/util"
|
2021-12-14 15:50:07 +01:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/ybbus/jsonrpc/v2"
|
2021-12-14 12:24:18 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
RpcClient rpc.RpcClient
|
|
|
|
)
|
|
|
|
|
2021-12-14 16:29:30 +01:00
|
|
|
type (
|
|
|
|
BatchHealthResponse struct {
|
|
|
|
SyncComplete bool
|
|
|
|
ChainName string
|
|
|
|
}
|
|
|
|
|
|
|
|
BatchMetricsResponse struct {
|
2021-12-15 10:57:47 +01:00
|
|
|
GasPrice uint64
|
|
|
|
BlockNumber uint64
|
|
|
|
PeerCount uint64
|
|
|
|
PendingTx []pendingTx
|
|
|
|
PendingTxCount int
|
|
|
|
Enode string
|
|
|
|
}
|
|
|
|
|
|
|
|
pendingTx struct {
|
|
|
|
Hash string `json:"hash"`
|
|
|
|
From string `json:"from"`
|
|
|
|
To string `json:"to"`
|
2021-12-14 16:29:30 +01:00
|
|
|
}
|
|
|
|
)
|
2021-12-14 15:50:07 +01:00
|
|
|
|
2021-12-14 16:29:30 +01:00
|
|
|
func batchHealthCall() (bool, BatchHealthResponse) {
|
|
|
|
var batchResponse BatchHealthResponse
|
2021-12-14 15:50:07 +01:00
|
|
|
|
|
|
|
rpcResponses, err := RpcClient.EthBatchRpcCall(jsonrpc.RPCRequests{
|
2021-12-14 15:55:50 +01:00
|
|
|
// The rpc call id here is guranteed to be in order of appearance starting from index 0
|
2021-12-14 15:50:07 +01:00
|
|
|
jsonrpc.NewRequest("eth_syncing"),
|
|
|
|
jsonrpc.NewRequest("parity_chain"),
|
|
|
|
})
|
|
|
|
|
2021-12-14 12:24:18 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Err(err).
|
|
|
|
Str("module", "server").
|
2021-12-14 15:50:07 +01:00
|
|
|
Msg("error in batch responses")
|
|
|
|
return false, batchResponse
|
2021-12-14 12:24:18 +01:00
|
|
|
}
|
|
|
|
|
2021-12-14 15:55:50 +01:00
|
|
|
// Order and format isn't guranteed with batch calls therefore we individually process/transform each result in the map
|
2021-12-14 15:50:07 +01:00
|
|
|
if syncStatus, ok := rpcResponses[0]; ok {
|
|
|
|
val, err := syncStatus.GetBool()
|
|
|
|
if err != nil {
|
|
|
|
return false, batchResponse
|
|
|
|
}
|
2021-12-14 15:55:50 +01:00
|
|
|
// If sync is complete the rpc result is false, however this call dones't gurantee all blocks have been imported
|
2021-12-14 15:50:07 +01:00
|
|
|
batchResponse.SyncComplete = !val
|
2021-12-14 12:24:18 +01:00
|
|
|
}
|
|
|
|
|
2021-12-14 15:50:07 +01:00
|
|
|
if chainName, ok := rpcResponses[1]; ok {
|
|
|
|
val, err := chainName.GetString()
|
|
|
|
if err != nil {
|
|
|
|
return false, batchResponse
|
|
|
|
}
|
|
|
|
batchResponse.ChainName = val
|
|
|
|
}
|
2021-12-14 12:24:18 +01:00
|
|
|
|
2021-12-14 15:50:07 +01:00
|
|
|
return true, batchResponse
|
2021-12-14 12:24:18 +01:00
|
|
|
}
|
2021-12-14 16:29:30 +01:00
|
|
|
|
|
|
|
func batchMetricsCall() (bool, BatchMetricsResponse) {
|
|
|
|
var batchResponse BatchMetricsResponse
|
|
|
|
|
|
|
|
rpcResponses, err := RpcClient.EthBatchRpcCall(jsonrpc.RPCRequests{
|
|
|
|
// The rpc call id here is guranteed to be in order of appearance starting from index 0
|
|
|
|
jsonrpc.NewRequest("eth_gasPrice"),
|
|
|
|
jsonrpc.NewRequest("eth_blockNumber"),
|
2021-12-15 10:57:47 +01:00
|
|
|
jsonrpc.NewRequest("net_peerCount"),
|
|
|
|
jsonrpc.NewRequest("parity_pendingTransactions"),
|
|
|
|
jsonrpc.NewRequest("parity_enode"),
|
2021-12-14 16:29:30 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Err(err).
|
|
|
|
Str("module", "server").
|
|
|
|
Msg("error in batch responses")
|
|
|
|
return false, batchResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
// Order and format isn't guranteed with batch calls therefore we individually process/transform each result in the map
|
|
|
|
if gasPrice, ok := rpcResponses[0]; ok {
|
|
|
|
// TODO: decode hex
|
|
|
|
val, err := gasPrice.GetString()
|
|
|
|
if err != nil {
|
|
|
|
return false, batchResponse
|
|
|
|
}
|
|
|
|
// If sync is complete the rpc result is false, however this call dones't gurantee all blocks have been imported
|
2021-12-14 16:41:24 +01:00
|
|
|
batchResponse.GasPrice = util.Hex2Int(val)
|
2021-12-14 16:29:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if blockNumber, ok := rpcResponses[1]; ok {
|
|
|
|
val, err := blockNumber.GetString()
|
|
|
|
if err != nil {
|
|
|
|
return false, batchResponse
|
|
|
|
}
|
2021-12-14 16:41:24 +01:00
|
|
|
batchResponse.BlockNumber = util.Hex2Int(val)
|
2021-12-14 16:29:30 +01:00
|
|
|
}
|
|
|
|
|
2021-12-15 10:57:47 +01:00
|
|
|
if peerCount, ok := rpcResponses[2]; ok {
|
|
|
|
val, err := peerCount.GetString()
|
|
|
|
if err != nil {
|
|
|
|
return false, batchResponse
|
|
|
|
}
|
|
|
|
batchResponse.PeerCount = util.Hex2Int(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pendingTransactionsArray, ok := rpcResponses[3]; ok {
|
|
|
|
var pendingTransactions = []pendingTx{}
|
|
|
|
err := pendingTransactionsArray.GetObject(&pendingTransactions)
|
|
|
|
if err != nil {
|
|
|
|
return false, batchResponse
|
|
|
|
}
|
|
|
|
batchResponse.PendingTxCount = len(pendingTransactions)
|
|
|
|
batchResponse.PendingTx = pendingTransactions
|
|
|
|
}
|
|
|
|
|
|
|
|
if enode, ok := rpcResponses[4]; ok {
|
|
|
|
val, err := enode.GetString()
|
|
|
|
if err != nil {
|
|
|
|
return false, batchResponse
|
|
|
|
}
|
|
|
|
batchResponse.Enode = val
|
|
|
|
}
|
|
|
|
|
2021-12-14 16:29:30 +01:00
|
|
|
return true, batchResponse
|
|
|
|
}
|