From c29a414718cb8df41a61a906d2e6ea42376fab29 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 12 Mar 2026 15:36:50 +0000 Subject: [PATCH 1/8] Initial plan From 17ef318c42e4c4721a340e5de2ab8bb4d595a8d7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 12 Mar 2026 15:50:38 +0000 Subject: [PATCH 2/8] cmd/pushtx: add CLI tool for submitting raw signed transactions Co-authored-by: drQedwards <213266729+drQedwards@users.noreply.github.com> --- .gitignore | 1 + Makefile | 6 ++ cmd/pushtx/main.go | 177 ++++++++++++++++++++++++++++++ cmd/pushtx/main_test.go | 233 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 417 insertions(+) create mode 100644 cmd/pushtx/main.go create mode 100644 cmd/pushtx/main_test.go diff --git a/.gitignore b/.gitignore index 293359a669..9602bff893 100644 --- a/.gitignore +++ b/.gitignore @@ -57,3 +57,4 @@ cmd/geth/geth cmd/rlpdump/rlpdump cmd/workload/workload cmd/keeper/keeper +cmd/pushtx/pushtx diff --git a/Makefile b/Makefile index f3d7f48f2f..b7cb3bc38b 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,12 @@ geth: @echo "Done building." @echo "Run \"$(GOBIN)/geth\" to launch geth." +#? pushtx: Build pushtx. +pushtx: + $(GORUN) build/ci.go install ./cmd/pushtx + @echo "Done building." + @echo "Run \"$(GOBIN)/pushtx\" to launch pushtx." + #? evm: Build evm. evm: $(GORUN) build/ci.go install ./cmd/evm diff --git a/cmd/pushtx/main.go b/cmd/pushtx/main.go new file mode 100644 index 0000000000..674176a996 --- /dev/null +++ b/cmd/pushtx/main.go @@ -0,0 +1,177 @@ +// Copyright 2025 The go-ethereum Authors +// This file is part of go-ethereum. +// +// go-ethereum is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// go-ethereum is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with go-ethereum. If not, see . + +// pushtx submits a raw signed transaction to an Ethereum JSON-RPC endpoint. +package main + +import ( + "context" + "encoding/hex" + "fmt" + "io" + "math/big" + "os" + "strings" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/rpc" +) + +const defaultRPCURL = "http://127.0.0.1:8545" + +func main() { + if err := run(os.Args[1:], os.Stdin); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} + +func run(args []string, stdin io.Reader) error { + var ( + rpcURL string + txHex string + ) + // Parse flags manually so the tool stays minimal. + for i := 0; i < len(args); i++ { + switch { + case args[i] == "--rpc" || args[i] == "-rpc": + i++ + if i >= len(args) { + return fmt.Errorf("missing value for %s", args[i-1]) + } + rpcURL = args[i] + case strings.HasPrefix(args[i], "--rpc="): + rpcURL = strings.TrimPrefix(args[i], "--rpc=") + case strings.HasPrefix(args[i], "-rpc="): + rpcURL = strings.TrimPrefix(args[i], "-rpc=") + case args[i] == "-h" || args[i] == "--help": + printUsage() + return nil + case strings.HasPrefix(args[i], "-"): + return fmt.Errorf("unknown flag: %s", args[i]) + default: + if txHex != "" { + return fmt.Errorf("unexpected argument: %s", args[i]) + } + txHex = args[i] + } + } + + if rpcURL == "" { + rpcURL = defaultRPCURL + } + + // Read transaction hex from stdin when no positional argument is given. + if txHex == "" { + data, err := io.ReadAll(stdin) + if err != nil { + return fmt.Errorf("reading stdin: %w", err) + } + txHex = strings.TrimSpace(string(data)) + } + if txHex == "" { + return fmt.Errorf("no transaction data provided\nUsage: pushtx [--rpc URL] ") + } + + rawTx, err := hex.DecodeString(strings.TrimPrefix(txHex, "0x")) + if err != nil { + return fmt.Errorf("invalid hex data: %w", err) + } + + // Decode the transaction so we can display a summary. + var tx types.Transaction + if err := tx.UnmarshalBinary(rawTx); err != nil { + return fmt.Errorf("decoding transaction: %w", err) + } + printTxSummary(&tx) + + // Send to the RPC endpoint. + hash, err := sendRawTransaction(rpcURL, rawTx) + if err != nil { + return fmt.Errorf("sending transaction: %w", err) + } + fmt.Println("Transaction submitted successfully") + fmt.Println("Hash:", hash.Hex()) + return nil +} + +// sendRawTransaction dials the given RPC endpoint and calls +// eth_sendRawTransaction with the provided raw bytes. +func sendRawTransaction(rpcURL string, rawTx []byte) (common.Hash, error) { + client, err := rpc.Dial(rpcURL) + if err != nil { + return common.Hash{}, fmt.Errorf("connecting to %s: %w", rpcURL, err) + } + defer client.Close() + + var hash common.Hash + err = client.CallContext(context.Background(), &hash, "eth_sendRawTransaction", hexutil.Encode(rawTx)) + if err != nil { + return common.Hash{}, err + } + return hash, nil +} + +// printTxSummary displays the decoded transaction details to stdout. +func printTxSummary(tx *types.Transaction) { + signer := types.LatestSignerForChainID(tx.ChainId()) + from, err := types.Sender(signer, tx) + if err != nil { + from = common.Address{} + } + + fmt.Println("Transaction details:") + fmt.Println(" Type: ", tx.Type()) + fmt.Println(" From: ", from.Hex()) + if tx.To() != nil { + fmt.Println(" To: ", tx.To().Hex()) + } else { + fmt.Println(" To: (contract creation)") + } + fmt.Println(" Nonce: ", tx.Nonce()) + fmt.Println(" Value: ", formatWei(tx.Value())) + fmt.Println(" Gas limit:", tx.Gas()) + fmt.Println(" Chain ID: ", tx.ChainId()) +} + +// formatWei converts a wei amount to a human-readable string showing +// both the wei value and the ETH equivalent. +func formatWei(wei *big.Int) string { + if wei == nil || wei.Sign() == 0 { + return "0 wei (0 ETH)" + } + ether := new(big.Float).Quo(new(big.Float).SetInt(wei), new(big.Float).SetFloat64(1e18)) + return fmt.Sprintf("%s wei (%s ETH)", wei.String(), ether.Text('f', 18)) +} + +func printUsage() { + fmt.Fprintf(os.Stderr, `Usage: pushtx [--rpc URL] + +Submit a raw signed Ethereum transaction to a JSON-RPC endpoint. + +The transaction data can be provided as a positional argument or via stdin. + +Options: + --rpc URL JSON-RPC endpoint (default: %s) + -h, --help Show this help message + +Examples: + pushtx --rpc http://localhost:8545 0xf86c... + echo 0xf86c... | pushtx --rpc http://localhost:8545 +`, defaultRPCURL) +} diff --git a/cmd/pushtx/main_test.go b/cmd/pushtx/main_test.go new file mode 100644 index 0000000000..101c3e8a22 --- /dev/null +++ b/cmd/pushtx/main_test.go @@ -0,0 +1,233 @@ +// Copyright 2025 The go-ethereum Authors +// This file is part of go-ethereum. +// +// go-ethereum is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// go-ethereum is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with go-ethereum. If not, see . + +package main + +import ( + "crypto/ecdsa" + "encoding/json" + "math/big" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" +) + +// signedTestTx returns a signed legacy transaction and its hex encoding. +func signedTestTx(t *testing.T) (*types.Transaction, string) { + t.Helper() + + key, err := crypto.GenerateKey() + if err != nil { + t.Fatal(err) + } + return signedTestTxWithKey(t, key) +} + +func signedTestTxWithKey(t *testing.T, key *ecdsa.PrivateKey) (*types.Transaction, string) { + t.Helper() + + tx := types.NewTx(&types.LegacyTx{ + Nonce: 6, + GasPrice: big.NewInt(0), + Gas: 21055, + To: addrPtr(common.HexToAddress("0x78b5290269740033b05bd8d71c97331295eb5918")), + Value: new(big.Int).Mul(big.NewInt(10), big.NewInt(1e18)), // 10 ETH + }) + signer := types.NewEIP155Signer(big.NewInt(1)) + signed, err := types.SignTx(tx, signer, key) + if err != nil { + t.Fatal(err) + } + data, err := signed.MarshalBinary() + if err != nil { + t.Fatal(err) + } + return signed, hexutil.Encode(data) +} + +func addrPtr(a common.Address) *common.Address { return &a } + +// fakeRPC starts an HTTP server that responds to eth_sendRawTransaction. +func fakeRPC(t *testing.T, wantErr bool) *httptest.Server { + t.Helper() + + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var req struct { + Method string `json:"method"` + Params []json.RawMessage `json:"params"` + ID json.RawMessage `json:"id"` + } + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + if req.Method != "eth_sendRawTransaction" { + json.NewEncoder(w).Encode(map[string]interface{}{ + "jsonrpc": "2.0", + "id": req.ID, + "error": map[string]interface{}{"code": -32601, "message": "method not found"}, + }) + return + } + if wantErr { + json.NewEncoder(w).Encode(map[string]interface{}{ + "jsonrpc": "2.0", + "id": req.ID, + "error": map[string]interface{}{"code": -32000, "message": "already known"}, + }) + return + } + // Decode the raw tx to return its hash as the result. + var hexData string + json.Unmarshal(req.Params[0], &hexData) + rawBytes, _ := hexutil.Decode(hexData) + var tx types.Transaction + tx.UnmarshalBinary(rawBytes) + json.NewEncoder(w).Encode(map[string]interface{}{ + "jsonrpc": "2.0", + "id": req.ID, + "result": tx.Hash().Hex(), + }) + })) +} + +func TestRunSuccess(t *testing.T) { + srv := fakeRPC(t, false) + defer srv.Close() + + _, txHex := signedTestTx(t) + err := run([]string{"--rpc", srv.URL, txHex}, strings.NewReader("")) + if err != nil { + t.Fatal("unexpected error:", err) + } +} + +func TestRunFromStdin(t *testing.T) { + srv := fakeRPC(t, false) + defer srv.Close() + + _, txHex := signedTestTx(t) + err := run([]string{"--rpc", srv.URL}, strings.NewReader(txHex)) + if err != nil { + t.Fatal("unexpected error:", err) + } +} + +func TestRunRPCError(t *testing.T) { + srv := fakeRPC(t, true) + defer srv.Close() + + _, txHex := signedTestTx(t) + err := run([]string{"--rpc", srv.URL, txHex}, strings.NewReader("")) + if err == nil { + t.Fatal("expected error, got nil") + } + if !strings.Contains(err.Error(), "already known") { + t.Fatalf("unexpected error message: %v", err) + } +} + +func TestRunNoInput(t *testing.T) { + err := run(nil, strings.NewReader("")) + if err == nil { + t.Fatal("expected error, got nil") + } + if !strings.Contains(err.Error(), "no transaction data") { + t.Fatalf("unexpected error: %v", err) + } +} + +func TestRunBadHex(t *testing.T) { + err := run([]string{"not-hex-data"}, strings.NewReader("")) + if err == nil { + t.Fatal("expected error, got nil") + } + if !strings.Contains(err.Error(), "invalid hex") { + t.Fatalf("unexpected error: %v", err) + } +} + +func TestRunBadTx(t *testing.T) { + err := run([]string{"0xdeadbeef"}, strings.NewReader("")) + if err == nil { + t.Fatal("expected error, got nil") + } + if !strings.Contains(err.Error(), "decoding transaction") { + t.Fatalf("unexpected error: %v", err) + } +} + +func TestRunHelp(t *testing.T) { + err := run([]string{"--help"}, strings.NewReader("")) + if err != nil { + t.Fatal("unexpected error:", err) + } +} + +func TestRunUnknownFlag(t *testing.T) { + err := run([]string{"--unknown"}, strings.NewReader("")) + if err == nil { + t.Fatal("expected error, got nil") + } + if !strings.Contains(err.Error(), "unknown flag") { + t.Fatalf("unexpected error: %v", err) + } +} + +func TestRunExtraArgs(t *testing.T) { + err := run([]string{"0xaa", "0xbb"}, strings.NewReader("")) + if err == nil { + t.Fatal("expected error, got nil") + } + if !strings.Contains(err.Error(), "unexpected argument") { + t.Fatalf("unexpected error: %v", err) + } +} + +func TestFormatWei(t *testing.T) { + tests := []struct { + wei *big.Int + want string + }{ + {nil, "0 wei (0 ETH)"}, + {big.NewInt(0), "0 wei (0 ETH)"}, + {big.NewInt(1e18), "1000000000000000000 wei (1.000000000000000000 ETH)"}, + {new(big.Int).Mul(big.NewInt(10), big.NewInt(1e18)), "10000000000000000000 wei (10.000000000000000000 ETH)"}, + } + for _, tt := range tests { + got := formatWei(tt.wei) + if got != tt.want { + t.Errorf("formatWei(%v) = %q, want %q", tt.wei, got, tt.want) + } + } +} + +func TestRunEqualsSyntax(t *testing.T) { + srv := fakeRPC(t, false) + defer srv.Close() + + _, txHex := signedTestTx(t) + err := run([]string{"--rpc=" + srv.URL, txHex}, strings.NewReader("")) + if err != nil { + t.Fatal("unexpected error:", err) + } +} From b91a437b09cff717d2cc36bbf385c412057d41cd Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 12 Mar 2026 15:58:03 +0000 Subject: [PATCH 3/8] cmd/pushtx: address code review feedback Co-authored-by: drQedwards <213266729+drQedwards@users.noreply.github.com> --- cmd/pushtx/main.go | 2 +- cmd/pushtx/main_test.go | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/cmd/pushtx/main.go b/cmd/pushtx/main.go index 674176a996..eb936c7850 100644 --- a/cmd/pushtx/main.go +++ b/cmd/pushtx/main.go @@ -85,7 +85,7 @@ func run(args []string, stdin io.Reader) error { txHex = strings.TrimSpace(string(data)) } if txHex == "" { - return fmt.Errorf("no transaction data provided\nUsage: pushtx [--rpc URL] ") + return fmt.Errorf("no transaction data provided (see --help for usage)") } rawTx, err := hex.DecodeString(strings.TrimPrefix(txHex, "0x")) diff --git a/cmd/pushtx/main_test.go b/cmd/pushtx/main_test.go index 101c3e8a22..972edf100b 100644 --- a/cmd/pushtx/main_test.go +++ b/cmd/pushtx/main_test.go @@ -98,10 +98,20 @@ func fakeRPC(t *testing.T, wantErr bool) *httptest.Server { } // Decode the raw tx to return its hash as the result. var hexData string - json.Unmarshal(req.Params[0], &hexData) - rawBytes, _ := hexutil.Decode(hexData) + if err := json.Unmarshal(req.Params[0], &hexData); err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + rawBytes, err := hexutil.Decode(hexData) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } var tx types.Transaction - tx.UnmarshalBinary(rawBytes) + if err := tx.UnmarshalBinary(rawBytes); err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } json.NewEncoder(w).Encode(map[string]interface{}{ "jsonrpc": "2.0", "id": req.ID, From 9ed063f56e6f5e97bd8ad11d95f2ad9ecf91906e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 12 Mar 2026 16:10:29 +0000 Subject: [PATCH 4/8] cmd/pushtx, .github/workflows: add build workflow, show raw hex in output Co-authored-by: drQedwards <213266729+drQedwards@users.noreply.github.com> --- .github/workflows/build.yml | 37 +++++++++++++++++++++++++++++++ cmd/pushtx/main.go | 4 ++++ cmd/pushtx/main_test.go | 43 +++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 .github/workflows/build.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000000..1203d58f0e --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,37 @@ +name: Build + +on: + push: + branches: + - master + - copilot/** + pull_request: + branches: + - master + workflow_dispatch: + +jobs: + build: + name: Build All + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + submodules: false + + - uses: actions/cache@v4 + with: + path: build/cache + key: ${{ runner.os }}-build-tools-cache-${{ hashFiles('build/checksums.txt') }} + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: '1.24' + cache: false + + - name: Build all commands + run: make all + + - name: Run pushtx tests + run: go test ./cmd/pushtx/ -v diff --git a/cmd/pushtx/main.go b/cmd/pushtx/main.go index eb936c7850..fe4540f05b 100644 --- a/cmd/pushtx/main.go +++ b/cmd/pushtx/main.go @@ -107,6 +107,10 @@ func run(args []string, stdin io.Reader) error { } fmt.Println("Transaction submitted successfully") fmt.Println("Hash:", hash.Hex()) + + // Print the raw hex transaction as the last output for easy + // copy-paste into block explorers like etherscan.io/pushTx. + fmt.Println("Raw tx: 0x" + hex.EncodeToString(rawTx)) return nil } diff --git a/cmd/pushtx/main_test.go b/cmd/pushtx/main_test.go index 972edf100b..d815ab3b73 100644 --- a/cmd/pushtx/main_test.go +++ b/cmd/pushtx/main_test.go @@ -19,9 +19,11 @@ package main import ( "crypto/ecdsa" "encoding/json" + "io" "math/big" "net/http" "net/http/httptest" + "os" "strings" "testing" @@ -241,3 +243,44 @@ func TestRunEqualsSyntax(t *testing.T) { t.Fatal("unexpected error:", err) } } + +func TestRunOutputEndsWithRawHex(t *testing.T) { + srv := fakeRPC(t, false) + defer srv.Close() + + _, txHex := signedTestTx(t) + + // Capture stdout to verify "Raw tx:" appears in output. + oldStdout := os.Stdout + r, w, err := os.Pipe() + if err != nil { + t.Fatal(err) + } + os.Stdout = w + + runErr := run([]string{"--rpc", srv.URL, txHex}, strings.NewReader("")) + + w.Close() + os.Stdout = oldStdout + + if runErr != nil { + t.Fatal("unexpected error:", runErr) + } + + out, err := io.ReadAll(r) + if err != nil { + t.Fatal(err) + } + lines := strings.Split(strings.TrimSpace(string(out)), "\n") + lastLine := lines[len(lines)-1] + + // The last line must be the raw hex transaction. + if !strings.HasPrefix(lastLine, "Raw tx: 0x") { + t.Fatalf("last output line = %q, want prefix \"Raw tx: 0x\"", lastLine) + } + // Verify the hex payload round-trips back to the input. + rawHex := strings.TrimPrefix(lastLine, "Raw tx: ") + if rawHex != txHex { + t.Fatalf("raw hex mismatch:\n got %s\n want %s", rawHex, txHex) + } +} From 53ff8183080fd87d7dcb6f3119d7e747f3af1bb7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 12 Mar 2026 16:12:40 +0000 Subject: [PATCH 5/8] plan: fix gas price and show gas price in summary Co-authored-by: drQedwards <213266729+drQedwards@users.noreply.github.com> --- cmd/pushtx/main.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmd/pushtx/main.go b/cmd/pushtx/main.go index fe4540f05b..87fce39a57 100644 --- a/cmd/pushtx/main.go +++ b/cmd/pushtx/main.go @@ -92,6 +92,8 @@ func run(args []string, stdin io.Reader) error { if err != nil { return fmt.Errorf("invalid hex data: %w", err) } + // Normalize to 0x-prefixed form for consistent output. + txHex = "0x" + hex.EncodeToString(rawTx) // Decode the transaction so we can display a summary. var tx types.Transaction From cc2957508f41ec6ba3061b086b4928dd4ae80b05 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 12 Mar 2026 16:20:59 +0000 Subject: [PATCH 6/8] cmd/pushtx: fix gas price 0 rejection, show gas price in summary, print raw hex on error Co-authored-by: drQedwards <213266729+drQedwards@users.noreply.github.com> --- cmd/pushtx/main.go | 15 ++++++++++++- cmd/pushtx/main_test.go | 49 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/cmd/pushtx/main.go b/cmd/pushtx/main.go index 87fce39a57..e4a8ff226a 100644 --- a/cmd/pushtx/main.go +++ b/cmd/pushtx/main.go @@ -105,6 +105,9 @@ func run(args []string, stdin io.Reader) error { // Send to the RPC endpoint. hash, err := sendRawTransaction(rpcURL, rawTx) if err != nil { + // Still print the raw hex so the user can submit it elsewhere + // (e.g. etherscan.io/pushTx). + fmt.Println("Raw tx:", txHex) return fmt.Errorf("sending transaction: %w", err) } fmt.Println("Transaction submitted successfully") @@ -112,7 +115,7 @@ func run(args []string, stdin io.Reader) error { // Print the raw hex transaction as the last output for easy // copy-paste into block explorers like etherscan.io/pushTx. - fmt.Println("Raw tx: 0x" + hex.EncodeToString(rawTx)) + fmt.Println("Raw tx:", txHex) return nil } @@ -152,6 +155,7 @@ func printTxSummary(tx *types.Transaction) { fmt.Println(" Nonce: ", tx.Nonce()) fmt.Println(" Value: ", formatWei(tx.Value())) fmt.Println(" Gas limit:", tx.Gas()) + fmt.Println(" Gas price:", formatGwei(tx.GasPrice())) fmt.Println(" Chain ID: ", tx.ChainId()) } @@ -165,6 +169,15 @@ func formatWei(wei *big.Int) string { return fmt.Sprintf("%s wei (%s ETH)", wei.String(), ether.Text('f', 18)) } +// formatGwei converts a wei gas price to a human-readable string in Gwei. +func formatGwei(wei *big.Int) string { + if wei == nil || wei.Sign() == 0 { + return "0 wei (0 Gwei)" + } + gwei := new(big.Float).Quo(new(big.Float).SetInt(wei), new(big.Float).SetFloat64(1e9)) + return fmt.Sprintf("%s wei (%s Gwei)", wei.String(), gwei.Text('f', 9)) +} + func printUsage() { fmt.Fprintf(os.Stderr, `Usage: pushtx [--rpc URL] diff --git a/cmd/pushtx/main_test.go b/cmd/pushtx/main_test.go index d815ab3b73..0459602928 100644 --- a/cmd/pushtx/main_test.go +++ b/cmd/pushtx/main_test.go @@ -49,7 +49,7 @@ func signedTestTxWithKey(t *testing.T, key *ecdsa.PrivateKey) (*types.Transactio tx := types.NewTx(&types.LegacyTx{ Nonce: 6, - GasPrice: big.NewInt(0), + GasPrice: big.NewInt(1_000_000_000), // 1 Gwei – real networks reject gas price 0 Gas: 21055, To: addrPtr(common.HexToAddress("0x78b5290269740033b05bd8d71c97331295eb5918")), Value: new(big.Int).Mul(big.NewInt(10), big.NewInt(1e18)), // 10 ETH @@ -149,12 +149,33 @@ func TestRunRPCError(t *testing.T) { defer srv.Close() _, txHex := signedTestTx(t) - err := run([]string{"--rpc", srv.URL, txHex}, strings.NewReader("")) - if err == nil { + + // Capture stdout – raw hex should still be printed on RPC failure. + oldStdout := os.Stdout + r, w, err := os.Pipe() + if err != nil { + t.Fatal(err) + } + os.Stdout = w + + runErr := run([]string{"--rpc", srv.URL, txHex}, strings.NewReader("")) + + w.Close() + os.Stdout = oldStdout + + if runErr == nil { t.Fatal("expected error, got nil") } - if !strings.Contains(err.Error(), "already known") { - t.Fatalf("unexpected error message: %v", err) + if !strings.Contains(runErr.Error(), "already known") { + t.Fatalf("unexpected error message: %v", runErr) + } + + out, err := io.ReadAll(r) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(string(out), "Raw tx: 0x") { + t.Fatal("expected raw hex in output even on RPC error") } } @@ -233,6 +254,24 @@ func TestFormatWei(t *testing.T) { } } +func TestFormatGwei(t *testing.T) { + tests := []struct { + wei *big.Int + want string + }{ + {nil, "0 wei (0 Gwei)"}, + {big.NewInt(0), "0 wei (0 Gwei)"}, + {big.NewInt(1_000_000_000), "1000000000 wei (1.000000000 Gwei)"}, + {big.NewInt(20_000_000_000), "20000000000 wei (20.000000000 Gwei)"}, + } + for _, tt := range tests { + got := formatGwei(tt.wei) + if got != tt.want { + t.Errorf("formatGwei(%v) = %q, want %q", tt.wei, got, tt.want) + } + } +} + func TestRunEqualsSyntax(t *testing.T) { srv := fakeRPC(t, false) defer srv.Close() From 84f37d3915cfd254eb27bd25feff535e53a2fc1e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 12 Mar 2026 16:24:50 +0000 Subject: [PATCH 7/8] cmd/pushtx: add tx cost display to catch insufficient funds before broadcast Co-authored-by: drQedwards <213266729+drQedwards@users.noreply.github.com> --- cmd/pushtx/main.go | 8 ++++++++ cmd/pushtx/main_test.go | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/cmd/pushtx/main.go b/cmd/pushtx/main.go index e4a8ff226a..bc0953c615 100644 --- a/cmd/pushtx/main.go +++ b/cmd/pushtx/main.go @@ -156,9 +156,17 @@ func printTxSummary(tx *types.Transaction) { fmt.Println(" Value: ", formatWei(tx.Value())) fmt.Println(" Gas limit:", tx.Gas()) fmt.Println(" Gas price:", formatGwei(tx.GasPrice())) + fmt.Println(" Tx cost: ", formatWei(txCost(tx))) fmt.Println(" Chain ID: ", tx.ChainId()) } +// txCost returns value + gas * gasPrice, i.e. the total ETH the sender +// must hold for the transaction to be accepted by the network. +func txCost(tx *types.Transaction) *big.Int { + gasCost := new(big.Int).Mul(new(big.Int).SetUint64(tx.Gas()), tx.GasPrice()) + return new(big.Int).Add(tx.Value(), gasCost) +} + // formatWei converts a wei amount to a human-readable string showing // both the wei value and the ETH equivalent. func formatWei(wei *big.Int) string { diff --git a/cmd/pushtx/main_test.go b/cmd/pushtx/main_test.go index 0459602928..f5b6c0eb78 100644 --- a/cmd/pushtx/main_test.go +++ b/cmd/pushtx/main_test.go @@ -272,6 +272,20 @@ func TestFormatGwei(t *testing.T) { } } +func TestTxCost(t *testing.T) { + tx := types.NewTx(&types.LegacyTx{ + GasPrice: big.NewInt(1_000_000_000), // 1 Gwei + Gas: 21055, + Value: new(big.Int).Mul(big.NewInt(10), big.NewInt(1e18)), // 10 ETH + }) + got := txCost(tx) + // Expected: 10 ETH + 21055 * 1 Gwei = 10000000000000000000 + 21055000000000 = 10000021055000000000 + want, _ := new(big.Int).SetString("10000021055000000000", 10) + if got.Cmp(want) != 0 { + t.Errorf("txCost = %s, want %s", got, want) + } +} + func TestRunEqualsSyntax(t *testing.T) { srv := fakeRPC(t, false) defer srv.Close() From 468f45d5aad3cda68615155113fc2feab2a080b9 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 12 Mar 2026 16:39:55 +0000 Subject: [PATCH 8/8] cmd/pushtx: use nonce 6, deposit to 0x78b529, fix workflow permissions Co-authored-by: drQedwards <213266729+drQedwards@users.noreply.github.com> --- .github/workflows/build.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1203d58f0e..37a41593cc 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -10,6 +10,9 @@ on: - master workflow_dispatch: +permissions: + contents: read + jobs: build: name: Build All