Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions les/flowcontrol/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package flowcontrol

import (
"fmt"
"math"
"sync"
"time"

Expand Down Expand Up @@ -316,6 +317,9 @@ func (node *ServerNode) CanSend(maxCost uint64) (time.Duration, float64) {
node.lock.RLock()
defer node.lock.RUnlock()

if node.params.BufLimit == 0 {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it's the root cause for overflowed stuff right? The bufferlimit is 0 but we don't have any protection.

return time.Duration(math.MaxInt64), 0
}
now := node.clock.Now()
node.recalcBLE(now)
maxCost += uint64(safetyMargin) * node.params.MinRecharge / uint64(fcTimeConst)
Expand Down
11 changes: 11 additions & 0 deletions les/utils/weighted_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package utils

import (
"math"
"math/rand"

"github.com/ethereum/go-ethereum/log"
)

type (
Expand Down Expand Up @@ -54,6 +57,14 @@ func (w *WeightedRandomSelect) IsEmpty() bool {

// setWeight sets an item's weight to a specific value (removes it if zero)
func (w *WeightedRandomSelect) setWeight(item WrsItem, weight uint64) {
if weight > math.MaxInt64-w.root.sumWeight {
// old weight is still included in sumWeight, remove and check again
w.setWeight(item, 0)
if weight > math.MaxInt64-w.root.sumWeight {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we check the overflow twice?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. For removing the old weight.

log.Error("WeightedRandomSelect overflow", "sumWeight", w.root.sumWeight, "new weight", weight)
weight = math.MaxInt64 - w.root.sumWeight
}
}
idx, ok := w.idx[item]
if ok {
w.root.setWeight(idx, weight)
Expand Down