Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds flexibility for different duration limits #18

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 47 additions & 18 deletions quaistats/quaistats.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,24 @@ const (
// reportInterval is the time interval between two reports.
reportInterval = 15

c_alpha = 8
c_txBatchSize = 20
c_blocksPerMinute = 5
c_blocksPerHour = c_blocksPerMinute * 60
c_txLookupCacheLimit = c_blocksPerHour / c_txBatchSize
c_statsErrorValue = int64(-1)
c_queueBatchSize = 5
c_nodeStatsWarningThreshold = 50
c_alpha = 8
c_statsErrorValue = int64(-1)

// Max number of stats objects to send in one batch
c_queueBatchSize = 5
// Number of blocks to include in one batch of transactions
c_txBatchSize = 20
)

var (
c_blocksPerMinute int
c_blocksPerHour int
c_txLookupCacheLimit int // Number of tx batches to have in the cache
chainID9000 = big.NewInt(9000)
chainID12000 = big.NewInt(12000)
chainID15000 = big.NewInt(15000)
chainID17000 = big.NewInt(17000)
chainID1337 = big.NewInt(1337)
)

// backend encompasses the bare-minimum functionality needed for quaistats reporting
Expand Down Expand Up @@ -220,6 +230,30 @@ func New(node *node.Node, backend backend, engine consensus.Engine, url string,
return err
}

chainID := backend.ChainConfig().ChainID
var durationLimit *big.Int

switch {
case chainID.Cmp(chainID9000) == 0:
durationLimit = params.DurationLimit
case chainID.Cmp(chainID12000) == 0:
durationLimit = params.GardenDurationLimit
case chainID.Cmp(chainID15000) == 0:
durationLimit = params.OrchardDurationLimit
case chainID.Cmp(chainID17000) == 0:
durationLimit = params.LighthouseDurationLimit
case chainID.Cmp(chainID1337) == 0:
durationLimit = params.LocalDurationLimit
default:
durationLimit = params.DurationLimit
}

durationLimitInt := int(durationLimit.Int64())

c_blocksPerMinute = 60 / durationLimitInt
c_blocksPerHour = 60 * c_blocksPerMinute
c_txLookupCacheLimit = c_blocksPerHour / c_txBatchSize

txLookupCache, _ := lru.New(c_txLookupCacheLimit)

quaistats := &Service{
Expand Down Expand Up @@ -328,7 +362,6 @@ func (s *Service) loopSender(urlMap map[string]string) {
quitCh = make(chan struct{})
)

var nodeStatsWarningCounter int
nodeStatsMod := 0

errTimer := time.NewTimer(0)
Expand All @@ -341,9 +374,9 @@ func (s *Service) loopSender(urlMap map[string]string) {
return
case <-errTimer.C:
// If we don't have a JWT or it's expired, get a new one
log.Info("Trying to login to quaistats")
isJwtExpiredResult, jwtIsExpiredErr := s.isJwtExpired(authJwt)
if authJwt == "" || isJwtExpiredResult || jwtIsExpiredErr != nil {
log.Info("Trying to login to quaistats")
var err error
authJwt, err = s.login2(urlMap["login"])
if err != nil {
Expand Down Expand Up @@ -402,11 +435,7 @@ func (s *Service) loopSender(urlMap map[string]string) {
nodeStatsMod ^= 1
if err = s.reportNodeStats(urlMap["nodeStats"], nodeStatsMod, authJwt); err != nil {
noErrs = false
nodeStatsWarningCounter += 1
if nodeStatsWarningCounter == c_nodeStatsWarningThreshold {
log.Warn("nodeStats full stats report failed", "err", err)
nodeStatsWarningCounter = 0
}
log.Warn("nodeStats full stats report failed", "err", err)
}
case <-s.statsReadyCh:
if url, ok := urlMap["blockTransactionStats"]; ok {
Expand Down Expand Up @@ -963,7 +992,7 @@ type totalTransactions struct {
TotalNoTransactions1m uint64
}

func (s *Service) evictOutdatedEntries(currentMaxBlock uint64) {
func (s *Service) evictOutdatedEntries(currentMaxBlock int) {
minAcceptableBlock := currentMaxBlock - c_blocksPerHour
for key := minAcceptableBlock - 20; key >= minAcceptableBlock-c_blocksPerHour; key -= 20 {
// Check if the key exists before trying to delete
Expand Down Expand Up @@ -1051,7 +1080,7 @@ func (s *Service) calculateTotalNoTransactions(block *types.Block) *totalTransac
}

if s.txLookupCache.Len() > c_txLookupCacheLimit {
s.evictOutdatedEntries(block.NumberU64())
s.evictOutdatedEntries(int(block.NumberU64()))
}

// Now totalTransactions1h and totalTransactions1m have the transaction counts for the last c_blocksPerHour and c_txBatchSize blocks respectively
Expand Down Expand Up @@ -1164,7 +1193,7 @@ func getQuaiRAMUsage() (uint64, error) {
var totalRam uint64

// Debug: log number of processes
log.Info("Number of processes", "number", len(processes))
log.Trace("Number of processes", "number", len(processes))

for _, p := range processes {
cmdline, err := p.Cmdline()
Expand Down
Loading