Skip to content

Commit

Permalink
refactor: using consistent logic for getting pending tx count
Browse files Browse the repository at this point in the history
  • Loading branch information
praetoriansentry committed Oct 6, 2023
1 parent a7a8c47 commit dbd9598
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 60 deletions.
5 changes: 0 additions & 5 deletions cmd/loadtest/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@ type (
ParsedModes []loadTestMode
MultiMode bool
}

txpoolStatus struct {
Pending any `json:"pending"`
Queued any `json:"queued"`
}
)

var (
Expand Down
56 changes: 5 additions & 51 deletions cmd/loadtest/loadtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package loadtest

import (
"context"
_ "embed"
"encoding/hex"
"errors"
"fmt"
Expand All @@ -10,17 +11,15 @@ import (
"math/rand"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"time"

"github.com/maticnetwork/polygon-cli/contracts"
"github.com/maticnetwork/polygon-cli/contracts/tokens"
"github.com/maticnetwork/polygon-cli/rpctypes"

_ "embed"

"github.com/maticnetwork/polygon-cli/metrics"
"github.com/maticnetwork/polygon-cli/rpctypes"
"github.com/maticnetwork/polygon-cli/util"

ethereum "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
Expand All @@ -29,8 +28,6 @@ import (
ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
ethrpc "github.com/ethereum/go-ethereum/rpc"

"github.com/maticnetwork/polygon-cli/contracts"
"github.com/rs/zerolog/log"
"golang.org/x/time/rate"
)
Expand Down Expand Up @@ -340,56 +337,13 @@ func runLoadTest(ctx context.Context) error {
return nil
}

func convHexToUint64(hexString string) (uint64, error) {
hexString = strings.TrimPrefix(hexString, "0x")
if len(hexString)%2 != 0 {
hexString = "0" + hexString
}

result, err := strconv.ParseUint(hexString, 16, 64)
if err != nil {
return 0, err
}
return uint64(result), nil
}

func tryCastToUint64(val any) (uint64, error) {
switch t := val.(type) {
case float64:
return uint64(t), nil
case string:
return convHexToUint64(t)
default:
return 0, fmt.Errorf("the value %v couldn't be marshalled to uint64", t)

}
}

func getTxPoolSize(rpc *ethrpc.Client) (uint64, error) {
var status = new(txpoolStatus)
err := rpc.Call(status, "txpool_status")
if err != nil {
return 0, err
}
pendingCount, err := tryCastToUint64(status.Pending)
if err != nil {
return 0, err
}
queuedCount, err := tryCastToUint64(status.Queued)
if err != nil {
return 0, err
}

return pendingCount + queuedCount, nil
}

func updateRateLimit(ctx context.Context, rl *rate.Limiter, rpc *ethrpc.Client, steadyStateQueueSize uint64, rateLimitIncrement uint64, cycleDuration time.Duration, backoff float64) {
ticker := time.NewTicker(cycleDuration)
defer ticker.Stop()
for {
select {
case <-ticker.C:
txPoolSize, err := getTxPoolSize(rpc)
txPoolSize, err := util.GetTxPoolSize(rpc)
if err != nil {
log.Error().Err(err).Msg("Error getting txpool size")
return
Expand Down
10 changes: 6 additions & 4 deletions cmd/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package monitor
import (
"context"
"fmt"
"github.com/maticnetwork/polygon-cli/util"
"math/big"
"net/url"
"sort"
Expand Down Expand Up @@ -48,7 +49,7 @@ type (
HeadBlock *big.Int
PeerCount uint64
GasPrice *big.Int
PendingCount uint
PendingCount uint64

Blocks map[string]rpctypes.PolyBlock `json:"-"`
BlocksLock sync.RWMutex `json:"-"`
Expand All @@ -60,7 +61,7 @@ type (
ChainID *big.Int
PeerCount uint64
GasPrice *big.Int
PendingCount uint
PendingCount uint64
}
historicalDataPoint struct {
SampleTime time.Time
Expand Down Expand Up @@ -113,7 +114,8 @@ func getChainState(ctx context.Context, ec *ethclient.Client) (*chainState, erro
if err != nil {
return nil, fmt.Errorf("couldn't estimate gas: %s", err.Error())
}
cs.PendingCount, err = ec.PendingTransactionCount(ctx)

cs.PendingCount, err = util.GetTxPoolSize(ec.Client())
if err != nil {
log.Debug().Err(err).Msg("Unable to get pending transaction count")
cs.PendingCount = 0
Expand All @@ -129,7 +131,7 @@ func (h historicalRange) getValues(limit int) []float64 {
values[idx] = v.SampleValue
}
if limit < len(values) {
values = values[len(values) - limit:]
values = values[len(values)-limit:]
}
return values
}
Expand Down
48 changes: 48 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package util
import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"

ethrpc "github.com/ethereum/go-ethereum/rpc"
"github.com/rs/zerolog/log"
Expand All @@ -17,6 +19,10 @@ type (
Number string `json:"number"`
Transactions []simpleRPCTransaction `json:"transactions"`
}
txpoolStatus struct {
Pending any `json:"pending"`
Queued any `json:"queued"`
}
)

func GetBlockRange(ctx context.Context, from, to uint64, c *ethrpc.Client) ([]*json.RawMessage, error) {
Expand Down Expand Up @@ -155,3 +161,45 @@ func GetReceipts(ctx context.Context, rawBlocks []*json.RawMessage, c *ethrpc.Cl
log.Info().Int("hashes", len(txHashes)).Int("receipts", len(receipts)).Msg("Fetched tx receipts")
return receipts, nil
}

func GetTxPoolSize(rpc *ethrpc.Client) (uint64, error) {
var status = new(txpoolStatus)
err := rpc.Call(status, "txpool_status")
if err != nil {
return 0, err
}
pendingCount, err := tryCastToUint64(status.Pending)
if err != nil {
return 0, err
}
queuedCount, err := tryCastToUint64(status.Queued)
if err != nil {
return 0, err
}

return pendingCount + queuedCount, nil
}

func tryCastToUint64(val any) (uint64, error) {
switch t := val.(type) {
case float64:
return uint64(t), nil
case string:
return convHexToUint64(t)
default:
return 0, fmt.Errorf("the value %v couldn't be marshalled to uint64", t)

}
}
func convHexToUint64(hexString string) (uint64, error) {
hexString = strings.TrimPrefix(hexString, "0x")
if len(hexString)%2 != 0 {
hexString = "0" + hexString
}

result, err := strconv.ParseUint(hexString, 16, 64)
if err != nil {
return 0, err
}
return uint64(result), nil
}

0 comments on commit dbd9598

Please sign in to comment.