Skip to content

Commit

Permalink
feat: add batch-size flag to polycli monitor command (#21)
Browse files Browse the repository at this point in the history
* feat: add batch-size flag to polycli monitor command

* refactor: use uint64 instead of int

* fix: miscalculation of the number of blocks
  • Loading branch information
leovct authored Dec 8, 2022
1 parent 87160b1 commit e74346a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ point.
If you're using the terminal UI and you'd like to be able to select text
for copying, you might need to use a modifier key.

```sh
polycli monitor --help

polycli monitor https://polygon-rpc.com
```

# Nodekey

The `nodekey` command is still in progress, but the idea is to have a
Expand Down
24 changes: 22 additions & 2 deletions cmd/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (
"github.com/spf13/cobra"
)

var inputBatchSize *uint64

type (
monitorStatus struct {
ChainID *big.Int
Expand All @@ -60,6 +62,8 @@ const (
monitorModeHelp monitorMode = iota
monitorModeExplorer monitorMode = iota
monitorModeBlock monitorMode = iota

defaultBatchSize uint64 = 25
)

func getChainState(ctx context.Context, ec *ethclient.Client) (*chainState, error) {
Expand Down Expand Up @@ -133,9 +137,15 @@ var monitorCmd = &cobra.Command{

from := big.NewInt(0)

// if the max block is 0, meaning we haven't fetched any blocks, we're going to start with head - 25
batchSize := defaultBatchSize
if *inputBatchSize > 0 {
batchSize = *inputBatchSize
}

// if the max block is 0, meaning we haven't fetched any blocks, we're going to start with head - batchSize
if ms.MaxBlockRetrieved.Cmp(from) == 0 {
from.Sub(ms.HeadBlock, big.NewInt(25))
headBlockMinusBatchSize := new(big.Int).SetUint64(batchSize - 1)
from.Sub(ms.HeadBlock, headBlockMinusBatchSize)
} else {
from = ms.MaxBlockRetrieved
}
Expand Down Expand Up @@ -166,11 +176,19 @@ var monitorCmd = &cobra.Command{
if len(args) != 1 {
return fmt.Errorf("expected exactly one argument")
}

// validate url argument
_, err := url.Parse(args[0])
if err != nil {
log.Error().Err(err).Msg("Unable to parse url input error")
return err
}

// validate batch-size flag
if *inputBatchSize == 0 {
return fmt.Errorf("batch-size can't be equal to zero")
}

return nil
},
}
Expand Down Expand Up @@ -212,6 +230,8 @@ func (ms *monitorStatus) getBlockRange(ctx context.Context, from, to *big.Int, c

func init() {
rootCmd.AddCommand(monitorCmd)

inputBatchSize = monitorCmd.PersistentFlags().Uint64P("batch-size", "b", 25, "Number of requests per batch")
}

func renderMonitorUI(ms *monitorStatus) error {
Expand Down

0 comments on commit e74346a

Please sign in to comment.