Skip to content

Commit

Permalink
chore: add rpc-url flag to dumpblocks
Browse files Browse the repository at this point in the history
  • Loading branch information
leovct committed Jan 18, 2024
1 parent a940a8f commit af7e2b5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
38 changes: 24 additions & 14 deletions cmd/dumpblocks/dumpblocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/binary"
"encoding/json"
"fmt"
"net/url"
"os"
"strconv"
"strings"
Expand All @@ -26,7 +25,7 @@ import (

type (
dumpblocksParams struct {
URL string
RpcUrl string
Start uint64
End uint64
BatchSize uint64
Expand All @@ -52,12 +51,15 @@ var (

// dumpblocksCmd represents the dumpblocks command
var DumpblocksCmd = &cobra.Command{
Use: "dumpblocks url start end",
Use: "dumpblocks start end",
Short: "Export a range of blocks from a JSON-RPC endpoint.",
Long: usage,
PreRunE: func(cmd *cobra.Command, args []string) error {
return checkFlags()
},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
ec, err := ethrpc.DialContext(ctx, args[0])
ec, err := ethrpc.DialContext(ctx, inputDumpblocks.RpcUrl)
if err != nil {
return err
}
Expand Down Expand Up @@ -136,19 +138,15 @@ var DumpblocksCmd = &cobra.Command{
return nil
},
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 3 {
return fmt.Errorf("command needs at least three arguments. A URL a start block and an end block")
if len(args) < 2 {
return fmt.Errorf("command needs at least two arguments. A start block and an end block")
}

_, err := url.Parse(args[0])
if err != nil {
return err
}
start, err := strconv.ParseInt(args[1], 10, 64)
start, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
return err
}
end, err := strconv.ParseInt(args[2], 10, 64)
end, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
return err
}
Expand All @@ -159,7 +157,6 @@ var DumpblocksCmd = &cobra.Command{
start, end = end, start
}

inputDumpblocks.URL = args[0]
inputDumpblocks.Start = uint64(start)
inputDumpblocks.End = uint64(end)

Expand Down Expand Up @@ -187,15 +184,28 @@ var DumpblocksCmd = &cobra.Command{
}

func init() {
DumpblocksCmd.PersistentFlags().StringVarP(&inputDumpblocks.RpcUrl, "rpc-url", "r", "http://localhost:8545", "The RPC endpoint url")
DumpblocksCmd.PersistentFlags().UintVarP(&inputDumpblocks.Threads, "concurrency", "c", 1, "how many go routines to leverage")
DumpblocksCmd.PersistentFlags().BoolVarP(&inputDumpblocks.ShouldDumpBlocks, "dump-blocks", "B", true, "if the blocks will be dumped")
DumpblocksCmd.PersistentFlags().BoolVarP(&inputDumpblocks.ShouldDumpReceipts, "dump-receipts", "r", true, "if the receipts will be dumped")
DumpblocksCmd.PersistentFlags().BoolVar(&inputDumpblocks.ShouldDumpReceipts, "dump-receipts", true, "if the receipts will be dumped")
DumpblocksCmd.PersistentFlags().StringVarP(&inputDumpblocks.Filename, "filename", "f", "", "where to write the output to (default stdout)")
DumpblocksCmd.PersistentFlags().StringVarP(&inputDumpblocks.Mode, "mode", "m", "json", "the output format [json, proto]")
DumpblocksCmd.PersistentFlags().Uint64VarP(&inputDumpblocks.BatchSize, "batch-size", "b", 150, "the batch size. Realistically, this probably shouldn't be bigger than 999. Most providers seem to cap at 1000.")
DumpblocksCmd.PersistentFlags().StringVarP(&inputDumpblocks.FilterStr, "filter", "F", "{}", "filter output based on tx to and from, not setting a filter means all are allowed")
}

func checkFlags() error {
// Check rpc url flag.
if inputDumpblocks.RpcUrl == "" {
panic("RPC URL is empty")
}
if err := util.ValidateUrl(inputDumpblocks.RpcUrl); err != nil {
return err
}

return nil
}

// writeResponses writes the data to either stdout or a file if one is provided.
// The message type can be either "block" or "transaction". The format of the
// output is either "json" or "proto" depending on the mode.
Expand Down
2 changes: 1 addition & 1 deletion cmd/dumpblocks/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ For various reasons, we might want to dump a large range of blocks for analytics
The following command would download the first 500K blocks and zip them and then look for blocks with transactions that create an account.

```bash
$ polycli dumpblocks http://172.26.26.12:8545/ 0 500000 | gzip > foo.gz
$ polycli dumpblocks 0 500000 --rpc-url http://172.26.26.12:8545/ | gzip > foo.gz
$ zcat < foo.gz | jq '. | select(.transactions | length > 0) | select(.transactions[].to == null)'
```

Expand Down
7 changes: 4 additions & 3 deletions doc/polycli_dumpblocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
Export a range of blocks from a JSON-RPC endpoint.

```bash
polycli dumpblocks url start end [flags]
polycli dumpblocks start end [flags]
```

## Usage
Expand All @@ -24,7 +24,7 @@ For various reasons, we might want to dump a large range of blocks for analytics
The following command would download the first 500K blocks and zip them and then look for blocks with transactions that create an account.

```bash
$ polycli dumpblocks http://172.26.26.12:8545/ 0 500000 | gzip > foo.gz
$ polycli dumpblocks 0 500000 --rpc-url http://172.26.26.12:8545/ | gzip > foo.gz
$ zcat < foo.gz | jq '. | select(.transactions | length > 0) | select(.transactions[].to == null)'
```

Expand Down Expand Up @@ -77,11 +77,12 @@ To solve this, add the unknown fields to the `.proto` files and recompile them (
-b, --batch-size uint the batch size. Realistically, this probably shouldn't be bigger than 999. Most providers seem to cap at 1000. (default 150)
-c, --concurrency uint how many go routines to leverage (default 1)
-B, --dump-blocks if the blocks will be dumped (default true)
-r, --dump-receipts if the receipts will be dumped (default true)
--dump-receipts if the receipts will be dumped (default true)
-f, --filename string where to write the output to (default stdout)
-F, --filter string filter output based on tx to and from, not setting a filter means all are allowed (default "{}")
-h, --help help for dumpblocks
-m, --mode string the output format [json, proto] (default "json")
-r, --rpc-url string The RPC endpoint url (default "http://localhost:8545")
```
The command also inherits flags from parent commands.
Expand Down

0 comments on commit af7e2b5

Please sign in to comment.