Skip to content

Commit caef151

Browse files
Increase gossip size on first push (#2787)
Signed-off-by: Stephen Buttolph <stephen@avalabs.org> Co-authored-by: Joshua Kim <20001595+joshua-kim@users.noreply.github.com>
1 parent d1312cb commit caef151

File tree

12 files changed

+314
-178
lines changed

12 files changed

+314
-178
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ go 1.21
1010
require (
1111
github.com/DataDog/zstd v1.5.2
1212
github.com/NYTimes/gziphandler v1.1.1
13-
github.com/ava-labs/coreth v0.13.1-rc.3
13+
github.com/ava-labs/coreth v0.13.1-rc.4
1414
github.com/ava-labs/ledger-avalanche/go v0.0.0-20231102202641-ae2ebdaeac34
1515
github.com/btcsuite/btcd/btcutil v1.1.3
1616
github.com/cockroachdb/pebble v0.0.0-20230209160836-829675f94811

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRF
6363
github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah4HI848JfFxHt+iPb26b4zyfspmqY0/8=
6464
github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM=
6565
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
66-
github.com/ava-labs/coreth v0.13.1-rc.3 h1:d32AzRI5HLwfFEevhR/RU4QPRjdl7LIMvPxTwlMlsmM=
67-
github.com/ava-labs/coreth v0.13.1-rc.3/go.mod h1:J1boUw9u7S3JrisnJ81PvrhhyZUBnS4WuxSeMtTuVU0=
66+
github.com/ava-labs/coreth v0.13.1-rc.4 h1:/3LsQi64oet6uCoUhEkgEXXcAAZFGPMUNJGdU03XH30=
67+
github.com/ava-labs/coreth v0.13.1-rc.4/go.mod h1:4y1igTe/sFOIrpAtXoY+AdmfftNHrmrhBBRVfGCAPcw=
6868
github.com/ava-labs/ledger-avalanche/go v0.0.0-20231102202641-ae2ebdaeac34 h1:mg9Uw6oZFJKytJxgxnl3uxZOs/SB8CVHg6Io4Tf99Zc=
6969
github.com/ava-labs/ledger-avalanche/go v0.0.0-20231102202641-ae2ebdaeac34/go.mod h1:pJxaT9bUgeRNVmNRgtCHb7sFDIRKy7CzTQVi8gGNT6g=
7070
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=

network/p2p/gossip/gossip.go

Lines changed: 74 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -263,22 +263,19 @@ func NewPushGossiper[T Gossipable](
263263
mempool Set[T],
264264
client *p2p.Client,
265265
metrics Metrics,
266-
numValidators int,
267-
numNonValidators int,
268-
numPeers int,
266+
gossipParams BranchingFactor,
267+
regossipParams BranchingFactor,
269268
discardedSize int,
270269
targetGossipSize int,
271270
maxRegossipFrequency time.Duration,
272271
) (*PushGossiper[T], error) {
272+
if err := gossipParams.Verify(); err != nil {
273+
return nil, fmt.Errorf("invalid gossip params: %w", err)
274+
}
275+
if err := regossipParams.Verify(); err != nil {
276+
return nil, fmt.Errorf("invalid regossip params: %w", err)
277+
}
273278
switch {
274-
case numValidators < 0:
275-
return nil, ErrInvalidNumValidators
276-
case numNonValidators < 0:
277-
return nil, ErrInvalidNumNonValidators
278-
case numPeers < 0:
279-
return nil, ErrInvalidNumPeers
280-
case max(numValidators, numNonValidators, numPeers) == 0:
281-
return nil, ErrInvalidNumToGossip
282279
case discardedSize < 0:
283280
return nil, ErrInvalidDiscardedSize
284281
case targetGossipSize < 0:
@@ -292,9 +289,8 @@ func NewPushGossiper[T Gossipable](
292289
set: mempool,
293290
client: client,
294291
metrics: metrics,
295-
numValidators: numValidators,
296-
numNonValidators: numNonValidators,
297-
numPeers: numPeers,
292+
gossipParams: gossipParams,
293+
regossipParams: regossipParams,
298294
targetGossipSize: targetGossipSize,
299295
maxRegossipFrequency: maxRegossipFrequency,
300296

@@ -312,9 +308,8 @@ type PushGossiper[T Gossipable] struct {
312308
client *p2p.Client
313309
metrics Metrics
314310

315-
numValidators int
316-
numNonValidators int
317-
numPeers int
311+
gossipParams BranchingFactor
312+
regossipParams BranchingFactor
318313
targetGossipSize int
319314
maxRegossipFrequency time.Duration
320315

@@ -326,6 +321,27 @@ type PushGossiper[T Gossipable] struct {
326321
discarded *cache.LRU[ids.ID, struct{}] // discarded attempts to avoid overgossiping transactions that are frequently dropped
327322
}
328323

324+
type BranchingFactor struct {
325+
Validators int
326+
NonValidators int
327+
Peers int
328+
}
329+
330+
func (b *BranchingFactor) Verify() error {
331+
switch {
332+
case b.Validators < 0:
333+
return ErrInvalidNumValidators
334+
case b.NonValidators < 0:
335+
return ErrInvalidNumNonValidators
336+
case b.Peers < 0:
337+
return ErrInvalidNumPeers
338+
case max(b.Validators, b.NonValidators, b.Peers) == 0:
339+
return ErrInvalidNumToGossip
340+
default:
341+
return nil
342+
}
343+
}
344+
329345
type tracking struct {
330346
addedTime float64 // unix nanoseconds
331347
lastGossiped time.Time
@@ -348,46 +364,46 @@ func (p *PushGossiper[T]) Gossip(ctx context.Context) error {
348364
return nil
349365
}
350366

351-
var (
352-
sentBytes = 0
353-
gossip = make([][]byte, 0, defaultGossipableCount)
354-
)
355-
356-
// Iterate over all unsent gossipables.
357-
for sentBytes < p.targetGossipSize {
358-
gossipable, ok := p.toGossip.PopLeft()
359-
if !ok {
360-
break
361-
}
362-
363-
// Ensure item is still in the set before we gossip.
364-
gossipID := gossipable.GossipID()
365-
tracking := p.tracking[gossipID]
366-
if !p.set.Has(gossipID) {
367-
delete(p.tracking, gossipID)
368-
p.addedTimeSum -= tracking.addedTime
369-
continue
370-
}
371-
372-
bytes, err := p.marshaller.MarshalGossip(gossipable)
373-
if err != nil {
374-
delete(p.tracking, gossipID)
375-
p.addedTimeSum -= tracking.addedTime
376-
return err
377-
}
367+
if err := p.gossip(
368+
ctx,
369+
now,
370+
p.gossipParams,
371+
p.toGossip,
372+
p.toRegossip,
373+
&cache.Empty[ids.ID, struct{}]{}, // Don't mark dropped unsent transactions as discarded
374+
); err != nil {
375+
return fmt.Errorf("unexpected error during gossip: %w", err)
376+
}
378377

379-
gossip = append(gossip, bytes)
380-
sentBytes += len(bytes)
381-
p.toRegossip.PushRight(gossipable)
382-
tracking.lastGossiped = now
378+
if err := p.gossip(
379+
ctx,
380+
now,
381+
p.regossipParams,
382+
p.toRegossip,
383+
p.toRegossip,
384+
p.discarded, // Mark dropped sent transactions as discarded
385+
); err != nil {
386+
return fmt.Errorf("unexpected error during regossip: %w", err)
383387
}
388+
return nil
389+
}
384390

385-
maxLastGossipTimeToRegossip := now.Add(-p.maxRegossipFrequency)
391+
func (p *PushGossiper[T]) gossip(
392+
ctx context.Context,
393+
now time.Time,
394+
gossipParams BranchingFactor,
395+
toGossip buffer.Deque[T],
396+
toRegossip buffer.Deque[T],
397+
discarded cache.Cacher[ids.ID, struct{}],
398+
) error {
399+
var (
400+
sentBytes = 0
401+
gossip = make([][]byte, 0, defaultGossipableCount)
402+
maxLastGossipTimeToRegossip = now.Add(-p.maxRegossipFrequency)
403+
)
386404

387-
// Iterate over all previously sent gossipables to fill any remaining space
388-
// in the gossip batch.
389405
for sentBytes < p.targetGossipSize {
390-
gossipable, ok := p.toRegossip.PopLeft()
406+
gossipable, ok := toGossip.PopLeft()
391407
if !ok {
392408
break
393409
}
@@ -398,29 +414,28 @@ func (p *PushGossiper[T]) Gossip(ctx context.Context) error {
398414
if !p.set.Has(gossipID) {
399415
delete(p.tracking, gossipID)
400416
p.addedTimeSum -= tracking.addedTime
401-
p.discarded.Put(gossipID, struct{}{}) // only add to discarded if previously sent
417+
discarded.Put(gossipID, struct{}{}) // Cache that the item was dropped
402418
continue
403419
}
404420

405421
// Ensure we don't attempt to send a gossipable too frequently.
406422
if maxLastGossipTimeToRegossip.Before(tracking.lastGossiped) {
407423
// Put the gossipable on the front of the queue to keep items sorted
408424
// by last issuance time.
409-
p.toRegossip.PushLeft(gossipable)
425+
toGossip.PushLeft(gossipable)
410426
break
411427
}
412428

413429
bytes, err := p.marshaller.MarshalGossip(gossipable)
414430
if err != nil {
415-
// Should never happen because we've already sent this once.
416431
delete(p.tracking, gossipID)
417432
p.addedTimeSum -= tracking.addedTime
418433
return err
419434
}
420435

421436
gossip = append(gossip, bytes)
422437
sentBytes += len(bytes)
423-
p.toRegossip.PushRight(gossipable)
438+
toRegossip.PushRight(gossipable)
424439
tracking.lastGossiped = now
425440
}
426441

@@ -448,9 +463,9 @@ func (p *PushGossiper[T]) Gossip(ctx context.Context) error {
448463
return p.client.AppGossip(
449464
ctx,
450465
msgBytes,
451-
p.numValidators,
452-
p.numNonValidators,
453-
p.numPeers,
466+
gossipParams.Validators,
467+
gossipParams.NonValidators,
468+
gossipParams.Peers,
454469
)
455470
}
456471

0 commit comments

Comments
 (0)