erigon-pulse/cmd/devnettest/requests/requests.go
leonardchinonso 3102a04d7f
Draft PR for the devnet automation (#4057)
* Draft PR for the devnet automation

* Committing to save for later edit

* Finished creating shells, to test

* Changes:
* Added a shell for picking eth commands
* Implemented erigon node running with the --http flag to save processes
* Shell commands for get-balance and send-tx implemented
TODO:
* Make UX more friendly by adding start, stop and exit commands
* Add progress bar to show wait in progress
* Add flag or input to enable mining option for erigon node
* Implemented stress tests for other eth methods

* Experimenting

* little clean up

* lint

* Transitioned to static runs and tests from shell

* Finished stress test methods

* Rendering fixes

* save

* Cleanup

* Fixed lint

* Still fixing lint

* Removed args append ineffect

* Removed println in genesis init.go

* Removed println in genesis init.go

Co-authored-by: Alexey Sharp <alexeysharp@Alexeys-iMac.local>
Co-authored-by: Enrique Avila <eavilaasapche@gmail.com>
2022-05-26 13:08:25 +01:00

177 lines
4.9 KiB
Go

package requests
import (
"bytes"
"encoding/json"
"fmt"
"github.com/ledgerwatch/erigon/cmd/devnettest/utils"
"net/http"
"strconv"
"strings"
"time"
"github.com/ledgerwatch/erigon/cmd/rpctest/rpctest"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/log/v3"
)
func post(client *http.Client, url, request string, response interface{}) error {
start := time.Now()
r, err := client.Post(url, "application/json", strings.NewReader(request))
if err != nil {
return fmt.Errorf("client failed to make post request: %v", err)
}
defer r.Body.Close()
if r.StatusCode != 200 {
return fmt.Errorf("status %s", r.Status)
}
decoder := json.NewDecoder(r.Body)
err = decoder.Decode(response)
if err != nil {
return fmt.Errorf("failed to decode response: %v", err)
}
log.Info("Got in", "time", time.Since(start).Seconds())
return nil
}
func GetBalance(reqId int, address common.Address, blockNum string) (uint64, error) {
reqGen := initialiseRequestGenerator(reqId)
var b rpctest.EthBalance
if res := reqGen.Erigon("eth_getBalance", reqGen.getBalance(address, blockNum), &b); res.Err != nil {
return 0, fmt.Errorf("failed to get balance: %v", res.Err)
}
bal, err := json.Marshal(b.Balance)
if err != nil {
fmt.Println(err)
}
balStr := string(bal)[3 : len(bal)-1]
balance, err := strconv.ParseInt(balStr, 16, 64)
if err != nil {
return 0, fmt.Errorf("cannot convert balance to decimal: %v", err)
}
return uint64(balance), nil
}
func SendTx(reqId int, signedTx *types.Transaction) (*common.Hash, error) {
reqGen := initialiseRequestGenerator(reqId)
var b rpctest.EthSendRawTransaction
var buf bytes.Buffer
if err := (*signedTx).MarshalBinary(&buf); err != nil {
return nil, fmt.Errorf("failed to marshal binary: %v", err)
}
if res := reqGen.Erigon("eth_sendRawTransaction", reqGen.sendRawTransaction(buf.Bytes()), &b); res.Err != nil {
return nil, fmt.Errorf("could not make request to eth_sendRawTransaction: %v", res.Err)
}
return &b.TxnHash, nil
}
func TxpoolContent(reqId int) error {
reqGen := initialiseRequestGenerator(reqId)
var b rpctest.EthTxPool
if res := reqGen.Erigon("txpool_content", reqGen.txpoolContent(), &b); res.Err != nil {
return fmt.Errorf("failed to fetch txpool content: %v", res.Err)
}
//fmt.Printf("Pending: %+v\n", b.Result.(map[string]interface{})["pending"].(map[string]interface{})["hash"])
//fmt.Printf("Type: %T\n", b.Result.(map[string]interface{})["pending"])
s, err := utils.ParseResponse(b)
if err != nil {
return fmt.Errorf("error parsing resonse: %v", err)
}
fmt.Printf("Txpool content: %v\n", s)
return nil
}
func ParityList(reqId int, account common.Address, quantity int, offset []byte, blockNum string) error {
reqGen := initialiseRequestGenerator(reqId)
var b rpctest.ParityListStorageKeysResult
if res := reqGen.Erigon("parity_listStorageKeys", reqGen.parityStorageKeyListContent(account, quantity, offset, blockNum), &b); res.Err != nil {
return fmt.Errorf("failed to fetch storage keys: %v", res.Err)
}
s, err := utils.ParseResponse(b)
if err != nil {
return fmt.Errorf("error parsing resonse: %v", err)
}
fmt.Printf("Storage keys: %v\n", s)
return nil
}
func GetLogs(reqId int, fromBlock, toBlock uint64, address common.Address, show bool) error {
reqGen := initialiseRequestGenerator(reqId)
var b rpctest.EthGetLogs
if res := reqGen.Erigon("eth_getLogs", reqGen.getLogs(fromBlock, toBlock, address), &b); res.Err != nil {
return fmt.Errorf("error fetching logs: %v\n", res.Err)
}
s, err := utils.ParseResponse(b)
if err != nil {
return fmt.Errorf("error parsing resonse: %v", err)
}
if show {
fmt.Printf("Logs: %v\n", s)
}
return nil
}
func GetTransactionCountCmd(reqId int, address common.Address, blockNum string) (uint64, error) {
reqGen := initialiseRequestGenerator(reqId)
var b rpctest.EthGetTransactionCount
if res := reqGen.Erigon("eth_getTransactionCount", reqGen.getTransactionCount(address, blockNum), &b); res.Err != nil {
return 0, fmt.Errorf("error getting transaction count: %v\n", res.Err)
}
if b.Error != nil {
return 0, fmt.Errorf("error populating response object: %v", b.Error)
}
n, err := json.Marshal(b.Result)
if err != nil {
fmt.Println(err)
}
nonceStr := string(n)[3 : len(n)-1]
nonce, err := strconv.ParseInt(nonceStr, 16, 64)
if err != nil {
return 0, fmt.Errorf("cannot convert nonce to decimal: %v", err)
}
return uint64(nonce), nil
}
func GetTransactionCount(reqId int, address common.Address, blockNum string) (rpctest.EthGetTransactionCount, error) {
reqGen := initialiseRequestGenerator(reqId)
var b rpctest.EthGetTransactionCount
if res := reqGen.Erigon("eth_getTransactionCount", reqGen.getTransactionCount(address, blockNum), &b); res.Err != nil {
return b, fmt.Errorf("error getting transaction count: %v\n", res.Err)
}
if b.Error != nil {
return b, fmt.Errorf("error populating response object: %v", b.Error)
}
return b, nil
}