2022-09-30 20:04:34 +00:00
|
|
|
package requests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
2023-05-31 18:47:32 +00:00
|
|
|
"github.com/valyala/fastjson"
|
2022-09-30 20:04:34 +00:00
|
|
|
)
|
|
|
|
|
2023-05-31 18:47:32 +00:00
|
|
|
type CallResult struct {
|
|
|
|
Target string
|
|
|
|
Took time.Duration
|
|
|
|
RequestID int
|
|
|
|
Method string
|
|
|
|
RequestBody string
|
|
|
|
Response []byte
|
|
|
|
Result *fastjson.Value
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
2023-06-04 19:53:05 +00:00
|
|
|
type CommonResponse struct {
|
|
|
|
Version string `json:"jsonrpc"`
|
|
|
|
RequestId int `json:"id"`
|
|
|
|
Error *EthError `json:"error"`
|
|
|
|
}
|
2022-09-30 20:04:34 +00:00
|
|
|
|
2023-06-04 19:53:05 +00:00
|
|
|
type EthError struct {
|
|
|
|
Code int `json:"code"`
|
|
|
|
Message string `json:"message"`
|
2022-09-30 20:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RequestGenerator struct {
|
|
|
|
reqID int
|
|
|
|
client *http.Client
|
2023-05-20 20:57:32 +00:00
|
|
|
logger log.Logger
|
2023-06-04 19:53:05 +00:00
|
|
|
target string
|
2022-09-30 20:04:34 +00:00
|
|
|
}
|
|
|
|
|
2023-06-04 19:53:05 +00:00
|
|
|
type (
|
|
|
|
// RPCMethod is the type for rpc methods used
|
|
|
|
RPCMethod string
|
|
|
|
// SubMethod is the type for sub methods used in subscriptions
|
|
|
|
SubMethod string
|
|
|
|
// BlockNumber represents the block number type
|
|
|
|
BlockNumber string
|
|
|
|
)
|
|
|
|
|
|
|
|
var BlockNumbers = struct {
|
|
|
|
// Latest is the parameter for the latest block
|
|
|
|
Latest BlockNumber
|
|
|
|
// Earliest is the parameter for the earliest block
|
|
|
|
Earliest BlockNumber
|
|
|
|
// Pending is the parameter for the pending block
|
|
|
|
Pending BlockNumber
|
|
|
|
}{
|
|
|
|
Latest: "latest",
|
|
|
|
Earliest: "earliest",
|
|
|
|
Pending: "pending",
|
|
|
|
}
|
|
|
|
|
|
|
|
var Methods = struct {
|
|
|
|
// ETHGetTransactionCount represents the eth_getTransactionCount method
|
|
|
|
ETHGetTransactionCount RPCMethod
|
|
|
|
// ETHGetBalance represents the eth_getBalance method
|
|
|
|
ETHGetBalance RPCMethod
|
|
|
|
// ETHSendRawTransaction represents the eth_sendRawTransaction method
|
|
|
|
ETHSendRawTransaction RPCMethod
|
|
|
|
// ETHGetBlockByNumber represents the eth_getBlockByNumber method
|
|
|
|
ETHGetBlockByNumber RPCMethod
|
|
|
|
// ETHGetBlock represents the eth_getBlock method
|
|
|
|
ETHGetBlock RPCMethod
|
|
|
|
// ETHGetLogs represents the eth_getLogs method
|
|
|
|
ETHGetLogs RPCMethod
|
|
|
|
// ETHBlockNumber represents the eth_blockNumber method
|
|
|
|
ETHBlockNumber RPCMethod
|
|
|
|
// AdminNodeInfo represents the admin_nodeInfo method
|
|
|
|
AdminNodeInfo RPCMethod
|
|
|
|
// TxpoolContent represents the txpool_content method
|
|
|
|
TxpoolContent RPCMethod
|
|
|
|
// OTSGetBlockDetails represents the ots_getBlockDetails method
|
|
|
|
OTSGetBlockDetails RPCMethod
|
|
|
|
// ETHNewHeads represents the eth_newHeads sub method
|
|
|
|
ETHNewHeads SubMethod
|
|
|
|
}{
|
|
|
|
ETHGetTransactionCount: "eth_getTransactionCount",
|
|
|
|
ETHGetBalance: "eth_getBalance",
|
|
|
|
ETHSendRawTransaction: "eth_sendRawTransaction",
|
|
|
|
ETHGetBlockByNumber: "eth_getBlockByNumber",
|
|
|
|
ETHGetBlock: "eth_getBlock",
|
|
|
|
ETHGetLogs: "eth_getLogs",
|
|
|
|
ETHBlockNumber: "eth_blockNumber",
|
|
|
|
AdminNodeInfo: "admin_nodeInfo",
|
|
|
|
TxpoolContent: "txpool_content",
|
|
|
|
OTSGetBlockDetails: "ots_getBlockDetails",
|
|
|
|
ETHNewHeads: "eth_newHeads",
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req *RequestGenerator) call(method RPCMethod, body string, response interface{}) CallResult {
|
2022-09-30 20:04:34 +00:00
|
|
|
start := time.Now()
|
2023-06-04 19:53:05 +00:00
|
|
|
err := post(req.client, req.target, string(method), body, response, req.logger)
|
|
|
|
req.reqID++
|
|
|
|
return CallResult{
|
2022-09-30 20:04:34 +00:00
|
|
|
RequestBody: body,
|
2023-06-04 19:53:05 +00:00
|
|
|
Target: req.target,
|
2022-09-30 20:04:34 +00:00
|
|
|
Took: time.Since(start),
|
|
|
|
RequestID: req.reqID,
|
2023-06-04 19:53:05 +00:00
|
|
|
Method: string(method),
|
2022-09-30 20:04:34 +00:00
|
|
|
Err: err,
|
|
|
|
}
|
2023-03-02 10:25:11 +00:00
|
|
|
}
|
|
|
|
|
2023-05-31 18:47:32 +00:00
|
|
|
func (req *RequestGenerator) PingErigonRpc() CallResult {
|
2022-10-11 12:34:32 +00:00
|
|
|
start := time.Now()
|
2023-05-31 18:47:32 +00:00
|
|
|
res := CallResult{
|
2022-10-11 12:34:32 +00:00
|
|
|
RequestID: req.reqID,
|
|
|
|
}
|
|
|
|
|
|
|
|
// return early if the http module has issue fetching the url
|
2023-06-04 19:53:05 +00:00
|
|
|
resp, err := http.Get(req.target) //nolint
|
2022-10-11 12:34:32 +00:00
|
|
|
if err != nil {
|
|
|
|
res.Took = time.Since(start)
|
|
|
|
res.Err = err
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// close the response body after reading its content at the end of the function
|
|
|
|
defer func(body io.ReadCloser) {
|
|
|
|
closeErr := body.Close()
|
|
|
|
if closeErr != nil {
|
2023-05-20 20:57:32 +00:00
|
|
|
req.logger.Warn("failed to close readCloser", "err", closeErr)
|
2022-10-11 12:34:32 +00:00
|
|
|
}
|
|
|
|
}(resp.Body)
|
|
|
|
|
|
|
|
// return a bad request if the status code is not 200
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
res.Took = time.Since(start)
|
2023-06-04 19:53:05 +00:00
|
|
|
res.Err = ErrBadRequest
|
2022-10-11 12:34:32 +00:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
res.Took = time.Since(start)
|
|
|
|
res.Err = err
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Response = body
|
|
|
|
res.Took = time.Since(start)
|
|
|
|
res.Err = err
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2023-06-04 19:53:05 +00:00
|
|
|
func NewRequestGenerator(target string, logger log.Logger) *RequestGenerator {
|
2022-09-30 20:04:34 +00:00
|
|
|
var client = &http.Client{
|
|
|
|
Timeout: time.Second * 600,
|
|
|
|
}
|
|
|
|
reqGen := RequestGenerator{
|
|
|
|
client: client,
|
2023-05-29 19:35:45 +00:00
|
|
|
reqID: 1,
|
2023-05-20 20:57:32 +00:00
|
|
|
logger: logger,
|
2023-06-04 19:53:05 +00:00
|
|
|
target: target,
|
2022-09-30 20:04:34 +00:00
|
|
|
}
|
|
|
|
return &reqGen
|
|
|
|
}
|
2023-06-04 19:53:05 +00:00
|
|
|
|
|
|
|
func post(client *http.Client, url, method, request string, response interface{}, logger log.Logger) error {
|
|
|
|
start := time.Now()
|
|
|
|
r, err := client.Post(url, "application/json", strings.NewReader(request)) // nolint:bodyclose
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("client failed to make post request: %w", err)
|
|
|
|
}
|
|
|
|
defer func(Body io.ReadCloser) {
|
|
|
|
closeErr := Body.Close()
|
|
|
|
if closeErr != nil {
|
|
|
|
logger.Warn("body close", "err", closeErr)
|
|
|
|
}
|
|
|
|
}(r.Body)
|
|
|
|
|
|
|
|
if r.StatusCode != 200 {
|
|
|
|
return fmt.Errorf("status %s", r.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to readAll from body: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(b, &response)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal response: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(method) > 0 {
|
|
|
|
method = "#" + method
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Info(fmt.Sprintf("%s%s", url, method), "time", time.Since(start).Seconds())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|