diff --git a/cmd/devnettest/main.go b/cmd/devnettest/main.go index 6cad3a97b535039b7753bd6e3daa37a5b0442122..73ff98d65cfd0adb2506c3423b22f7c237f55ee3 100644 --- a/cmd/devnettest/main.go +++ b/cmd/devnettest/main.go @@ -2,7 +2,6 @@ package main import ( "flag" - "fmt" "github.com/ledgerwatch/erigon/cmd/devnettest/services" ) @@ -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) } diff --git a/cmd/devnettest/requests/request_generator.go b/cmd/devnettest/requests/request_generator.go index c31fcc551397ce4da7e54ca0b3c36f93dc36342b..09c8190e07d6cc49c67a12e48737c3a2f398284d 100644 --- a/cmd/devnettest/requests/request_generator.go +++ b/cmd/devnettest/requests/request_generator.go @@ -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) +} diff --git a/cmd/devnettest/requests/requests.go b/cmd/devnettest/requests/requests.go index 59d2ab8b8e0c2859742d427add984b46e7b55e04..9874419b8835ab95dedf40abd4c23aaeb416d1ee 100644 --- a/cmd/devnettest/requests/requests.go +++ b/cmd/devnettest/requests/requests.go @@ -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 @@ -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)) } diff --git a/cmd/devnettest/services/utility.go b/cmd/devnettest/services/utility.go index 0e0a821756c92d515185298fb2cb1dbf91ef5180..bce6bc38ae8faf4434c57c55d8f9ecc220afd6c5 100644 --- a/cmd/devnettest/services/utility.go +++ b/cmd/devnettest/services/utility.go @@ -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") @@ -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) + //} } diff --git a/cmd/rpctest/rpctest/type.go b/cmd/rpctest/rpctest/type.go index 06f9740643fe300c3149ae06b2f7a4fc04b185ae..df13476d713adb4f2757b1bfeef082ac6bf994ac 100644 --- a/cmd/rpctest/rpctest/type.go +++ b/cmd/rpctest/rpctest/type.go @@ -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"`