Skip to content

Commit

Permalink
Devnet build to test eth_getBalance, eth_sendRawTransaction and txpoo…
Browse files Browse the repository at this point in the history
…l_content (erigontech#3041)

* completed and cleaned up dev chain build

* Last minute syntax changes
  • Loading branch information
leonardchinonso authored Nov 26, 2021
1 parent 6910e18 commit d02903b
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 39 deletions.
16 changes: 2 additions & 14 deletions cmd/devnettest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"flag"
"fmt"
"github.com/ledgerwatch/erigon/cmd/devnettest/services"
)

Expand All @@ -26,18 +25,7 @@ func main() {
flag.BoolVar(&clearDev, "clear-dev", false, "Boolean Flag to determine if service should clear /dev after this call")
flag.Parse()

//fmt.Printf("to: %v\n", to)
//fmt.Printf("value: %v\n", value)
//fmt.Printf("blockNum: %v\n", blockNum)
//fmt.Printf("getBalance: %v\n", getBalance)
//fmt.Printf("sendTx: %v\n", sendTx)
//fmt.Printf("txPoolContent: %v\n", txPoolContent)
//fmt.Printf("clearDev: %v\n", clearDev)
services.ValidateInputs(getBalance, sendTx, txPoolContent, blockNum, value, to)

services.ValidateInputs(&getBalance, &sendTx, &txPoolContent, &blockNum, &value, &to)

services.ParseRequests(&getBalance, &sendTx, &txPoolContent, &clearDev, &blockNum, &value, &to)

fmt.Print("\n")
fmt.Print("Finished processing\n")
services.ParseRequests(getBalance, sendTx, txPoolContent, clearDev, blockNum, value, to)
}
10 changes: 10 additions & 0 deletions cmd/devnettest/requests/request_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,13 @@ func (req *RequestGenerator) getBalance(address common.Address, blockNum string)
const template = `{"jsonrpc":"2.0","method":"eth_getBalance","params":["0x%x","%v"],"id":%d}`
return fmt.Sprintf(template, address, blockNum, req.reqID)
}

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)
}
50 changes: 42 additions & 8 deletions cmd/devnettest/requests/requests.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package requests

import (
"crypto/ecdsa"
"bytes"
"encoding/json"
"fmt"
"github.com/ledgerwatch/erigon/cmd/rpctest/rpctest"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/core/types"
)

func parseResponse(resp interface{}) string {
result, err := json.Marshal(resp)
if err != nil {
panic(err)
}

return string(result)
}

func GetBalance(address common.Address, blockNum string) {
reqGen := initialiseRequestGenerator()
var b rpctest.EthBalance
Expand All @@ -17,15 +28,38 @@ func GetBalance(address common.Address, blockNum string) {
return
}

fmt.Printf("Balance is: %v\n", b.Balance.ToInt())
fmt.Printf("Balance retrieved: %v\n", parseResponse(b))
}

func SendTx(from *ecdsa.PrivateKey, to common.Address, value uint64) {
fmt.Printf("from is: %v", from)
fmt.Printf("to is: %v", to)
fmt.Printf("value is: %v", value)
func SendTx(signedTx *types.Transaction) {
reqGen := initialiseRequestGenerator()
var b rpctest.EthSendRawTransaction

var buf bytes.Buffer
err := (*signedTx).MarshalBinary(&buf)
if err != nil {
fmt.Printf("Error trying to marshal binary: %v\n", err)
return
}

res := reqGen.Erigon("eth_sendRawTransaction", reqGen.sendRawTransaction(buf.Bytes()), &b)
if res.Err != nil {
fmt.Printf("Error sending transaction: %v\n", res.Err)
return
}

fmt.Printf("Submitted transaction successfully: %v\n", parseResponse(b))
}

func TxpoolContent() string {
return ""
func TxpoolContent() {
reqGen := initialiseRequestGenerator()
var b rpctest.EthTxPool

res := reqGen.Erigon("txpool_content", reqGen.txpoolContent(), &b)
if res.Err != nil {
fmt.Printf("Error fetching txpool: %v\n", res.Err)
return
}

fmt.Printf("Txpool content: %v\n", parseResponse(b))
}
45 changes: 28 additions & 17 deletions cmd/devnettest/services/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@ package services

import (
"fmt"
"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon/cmd/devnettest/requests"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/crypto"
"github.com/ledgerwatch/erigon/params"
)

var devnetSignPrivateKey, _ = crypto.HexToECDSA("26e86e45f6fc45ec6e2ecd128cec80fa1d1505e5507dcd2ae58c3130a7a97b48")

func ValidateInputs(getBalance *bool, sendTx *bool, txpoolContent *bool, blockNum *string, value *uint64, to *string) {
if !(*getBalance) && !(*sendTx) && !(*txpoolContent) {
func ValidateInputs(getBalance bool, sendTx bool, txpoolContent bool, blockNum string, value uint64, to string) {
if !(getBalance) && !(sendTx) && !(txpoolContent) {
panic("At least one function flag (get-balance, send-tx, txpool-content) should be true")
}

seen := false
for _, val := range []bool{*getBalance, *sendTx, *txpoolContent} {
for _, val := range []bool{getBalance, sendTx, txpoolContent} {
if val {
if seen {
panic("Only function flag (get-balance, send-tx, txpool-content) can be true at a time")
Expand All @@ -24,45 +27,53 @@ func ValidateInputs(getBalance *bool, sendTx *bool, txpoolContent *bool, blockNu
}
}

if *value <= 0 {
if value <= 0 {
panic("Value must be greater than zero")
}

if *getBalance {
if *to == "" {
if getBalance {
if to == "" {
panic("Cannot check balance of empty address")
}
if *blockNum != "pending" && *blockNum != "latest" && *blockNum != "earliest" {
if blockNum != "pending" && blockNum != "latest" && blockNum != "earliest" {
panic("Block number must be 'pending', 'latest' or 'earliest'")
}
}

if *sendTx && *to == "" {
if sendTx && to == "" {
panic("Cannot send to empty address")
}

}

func ParseRequests(getBalance *bool, sendTx *bool, txpoolContent *bool, clearDev *bool, blockNum *string, value *uint64, to *string) {
if *getBalance {
toAddress := common.HexToAddress(*to)
requests.GetBalance(toAddress, *blockNum)
func ParseRequests(getBalance bool, sendTx bool, txpoolContent bool, clearDev bool, blockNum string, value uint64, to string) {
if getBalance {
toAddress := common.HexToAddress(to)
requests.GetBalance(toAddress, blockNum)
}

if *sendTx {
toAddress := common.HexToAddress(*to)
requests.SendTx(devnetSignPrivateKey, toAddress, *value)
if sendTx {
toAddress := common.HexToAddress(to)
signer := types.LatestSigner(params.AllCliqueProtocolChanges)
signedTx, _ := types.SignTx(types.NewTransaction(0, toAddress, uint256.NewInt(value),
params.TxGas, uint256.NewInt(50000), nil), *signer, devnetSignPrivateKey)
requests.SendTx(&signedTx)
}

if *txpoolContent {
if txpoolContent {
requests.TxpoolContent()
}

if *clearDev {
if clearDev {
clearDevDB()
}
}

func clearDevDB() {
fmt.Printf("Clearing ~/dev\n")
//
//_, err := exec.Command("rm", "-rf", "~/dev", "~/dev2").Output()
//if err != nil {
// fmt.Println(err)
//}
}
10 changes: 10 additions & 0 deletions cmd/rpctest/rpctest/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ type EthTransaction struct {
Value hexutil.Big `json:"value"`
}

type EthSendRawTransaction struct {
CommonResponse
TxnHash common.Hash `json:"result"`
}

type EthTxPool struct {
CommonResponse
Result interface{} `json:"result"`
}

type EthBlockByNumberResult struct {
Difficulty hexutil.Big `json:"difficulty"`
Miner common.Address `json:"miner"`
Expand Down

0 comments on commit d02903b

Please sign in to comment.