2021-11-23 16:44:46 +00:00
|
|
|
package requests
|
|
|
|
|
|
|
|
import (
|
2022-02-12 11:40:19 +00:00
|
|
|
"errors"
|
2021-11-23 16:44:46 +00:00
|
|
|
"fmt"
|
2022-04-23 14:43:00 +00:00
|
|
|
"io"
|
2021-11-23 16:44:46 +00:00
|
|
|
"net/http"
|
|
|
|
"time"
|
2022-01-14 13:08:41 +00:00
|
|
|
|
|
|
|
"github.com/ledgerwatch/erigon/cmd/rpctest/rpctest"
|
|
|
|
"github.com/ledgerwatch/erigon/common"
|
2022-08-04 13:23:00 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
2021-11-23 16:44:46 +00:00
|
|
|
)
|
|
|
|
|
2022-08-04 13:23:00 +00:00
|
|
|
const erigonUrl string = "http://localhost:8545"
|
2021-11-23 16:44:46 +00:00
|
|
|
|
|
|
|
type RequestGenerator struct {
|
|
|
|
reqID int
|
|
|
|
client *http.Client
|
|
|
|
}
|
|
|
|
|
2022-01-14 13:08:41 +00:00
|
|
|
func initialiseRequestGenerator(reqId int) *RequestGenerator {
|
2021-11-23 16:44:46 +00:00
|
|
|
var client = &http.Client{
|
|
|
|
Timeout: time.Second * 600,
|
|
|
|
}
|
|
|
|
|
|
|
|
reqGen := RequestGenerator{
|
|
|
|
client: client,
|
2022-01-14 13:08:41 +00:00
|
|
|
reqID: reqId,
|
|
|
|
}
|
|
|
|
if reqGen.reqID == 0 {
|
|
|
|
reqGen.reqID++
|
2021-11-23 16:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &reqGen
|
|
|
|
}
|
|
|
|
|
2022-02-12 11:40:19 +00:00
|
|
|
func (req *RequestGenerator) Get() rpctest.CallResult {
|
|
|
|
start := time.Now()
|
|
|
|
res := rpctest.CallResult{
|
|
|
|
RequestID: req.reqID,
|
|
|
|
}
|
|
|
|
|
2022-08-04 13:59:40 +00:00
|
|
|
resp, err := http.Get(erigonUrl) //nolint
|
2022-02-12 11:40:19 +00:00
|
|
|
if err != nil {
|
|
|
|
res.Took = time.Since(start)
|
|
|
|
res.Err = err
|
|
|
|
return res
|
|
|
|
}
|
2022-08-04 13:23:00 +00:00
|
|
|
defer func(Body io.ReadCloser) {
|
|
|
|
closeErr := Body.Close()
|
|
|
|
if closeErr != nil {
|
2022-08-04 13:59:40 +00:00
|
|
|
log.Warn("Failed to close readCloser", "err", closeErr)
|
2022-08-04 13:23:00 +00:00
|
|
|
}
|
|
|
|
}(resp.Body)
|
2022-02-12 11:40:19 +00:00
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
res.Took = time.Since(start)
|
|
|
|
res.Err = errors.New("bad request")
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-04-23 14:43:00 +00:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2022-02-12 11:40:19 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-11-23 16:44:46 +00:00
|
|
|
func (req *RequestGenerator) Erigon(method, body string, response interface{}) rpctest.CallResult {
|
|
|
|
return req.call(erigonUrl, method, body, response)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req *RequestGenerator) call(target string, method, body string, response interface{}) rpctest.CallResult {
|
|
|
|
start := time.Now()
|
|
|
|
err := post(req.client, erigonUrl, body, response)
|
|
|
|
return rpctest.CallResult{
|
|
|
|
RequestBody: body,
|
|
|
|
Target: target,
|
|
|
|
Took: time.Since(start),
|
|
|
|
RequestID: req.reqID,
|
|
|
|
Method: method,
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req *RequestGenerator) getBalance(address common.Address, blockNum string) string {
|
|
|
|
const template = `{"jsonrpc":"2.0","method":"eth_getBalance","params":["0x%x","%v"],"id":%d}`
|
|
|
|
return fmt.Sprintf(template, address, blockNum, req.reqID)
|
|
|
|
}
|
2021-11-26 16:05:16 +00:00
|
|
|
|
|
|
|
func (req *RequestGenerator) sendRawTransaction(signedTx []byte) string {
|
|
|
|
const template = `{"jsonrpc":"2.0","method":"eth_sendRawTransaction","params":["0x%x"],"id":%d}`
|
|
|
|
return fmt.Sprintf(template, signedTx, req.reqID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req *RequestGenerator) txpoolContent() string {
|
|
|
|
const template = `{"jsonrpc":"2.0","method":"txpool_content","params":[],"id":%d}`
|
|
|
|
return fmt.Sprintf(template, req.reqID)
|
|
|
|
}
|
2022-01-14 13:08:41 +00:00
|
|
|
|
2022-02-08 13:02:18 +00:00
|
|
|
func (req *RequestGenerator) parityStorageKeyListContent(address common.Address, quantity int, offset []byte, blockNum string) string {
|
|
|
|
const template = `{"jsonrpc":"2.0","method":"parity_listStorageKeys","params":["0x%x", %d, %v, "%s"],"id":%d}`
|
2022-01-14 13:08:41 +00:00
|
|
|
var offsetString string
|
|
|
|
if len(offset) != 0 {
|
|
|
|
offsetString = fmt.Sprintf(`"0x%x"`, offset)
|
|
|
|
} else {
|
|
|
|
offsetString = "null"
|
|
|
|
}
|
2022-02-08 13:02:18 +00:00
|
|
|
|
|
|
|
return fmt.Sprintf(template, address, quantity, offsetString, blockNum, req.reqID)
|
2022-01-14 13:08:41 +00:00
|
|
|
}
|
2022-03-14 13:59:51 +00:00
|
|
|
|
|
|
|
func (req *RequestGenerator) getLogs(fromBlock, toBlock uint64, address common.Address) string {
|
|
|
|
const template = `{"jsonrpc":"2.0","method":"eth_getLogs","params":[{"fromBlock": "0x%x", "toBlock": "0x%x", "address": "0x%x"}],"id":%d}`
|
|
|
|
return fmt.Sprintf(template, fromBlock, toBlock, address, req.reqID)
|
|
|
|
}
|
2022-03-18 11:57:23 +00:00
|
|
|
|
|
|
|
func (req *RequestGenerator) getTransactionCount(address common.Address, blockNum string) string {
|
|
|
|
const template = `{"jsonrpc":"2.0","method":"eth_getTransactionCount","params":["0x%x","%v"],"id":%d}`
|
|
|
|
return fmt.Sprintf(template, address, blockNum, req.reqID)
|
|
|
|
}
|