-
Notifications
You must be signed in to change notification settings - Fork 671
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
network/p2p/gossip: refactor Set.Add to accept multiple elements #2986
Closed
Closed
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8ff1cc7
network/p2p/gossip: refactor Set.Add to accept multiple elements
lebdron fd2ee6b
Fix platformvm gossip mempool lock Unlock call in Add
lebdron 63cead0
Merge branch 'master' into feat/add-many-set
lebdron 24c913b
Fix gossip tests
lebdron d27149c
Merge remote-tracking branch 'upstream/master' into feat/add-many-set
lebdron 17923bf
set version to fix build
lebdron f34be33
Merge branch 'master' into feat/add-many-set
lebdron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,49 +90,54 @@ type gossipMempool struct { | |
bloom *gossip.BloomFilter | ||
} | ||
|
||
func (g *gossipMempool) Add(tx *txs.Tx) error { | ||
txID := tx.ID() | ||
if _, ok := g.Mempool.Get(txID); ok { | ||
return fmt.Errorf("tx %s dropped: %w", txID, mempool.ErrDuplicateTx) | ||
func (g *gossipMempool) Add(txns ...*txs.Tx) []error { | ||
errs := make([]error, len(txns)) | ||
for i, tx := range txns { | ||
txID := tx.ID() | ||
if _, ok := g.Mempool.Get(txID); ok { | ||
errs[i] = fmt.Errorf("tx %s dropped: %w", txID, mempool.ErrDuplicateTx) | ||
continue | ||
} | ||
|
||
if errs[i] = g.Mempool.GetDropReason(txID); errs[i] != nil { | ||
// If the tx is being dropped - just ignore it | ||
// | ||
// TODO: Should we allow re-verification of the transaction even if it | ||
// failed previously? | ||
continue | ||
} | ||
|
||
if errs[i] = g.txVerifier.VerifyTx(tx); errs[i] != nil { | ||
g.Mempool.MarkDropped(txID, errs[i]) | ||
continue | ||
} | ||
|
||
if errs[i] = g.Mempool.Add(tx); errs[i] != nil { | ||
g.Mempool.MarkDropped(txID, errs[i]) | ||
continue | ||
} | ||
|
||
g.lock.Lock() | ||
defer g.lock.Unlock() | ||
|
||
g.bloom.Add(tx) | ||
var reset bool | ||
reset, errs[i] = gossip.ResetBloomFilterIfNeeded(g.bloom, g.Mempool.Len()*bloomChurnMultiplier) | ||
if errs[i] != nil { | ||
continue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should add tests for multiple txs - I think this can continue to the next iteration with the lock held. Would it make sense to keep the single tx addition as a method so that we can keep using |
||
} | ||
|
||
if reset { | ||
g.log.Debug("resetting bloom filter") | ||
g.Mempool.Iterate(func(tx *txs.Tx) bool { | ||
g.bloom.Add(tx) | ||
return true | ||
}) | ||
} | ||
} | ||
|
||
if reason := g.Mempool.GetDropReason(txID); reason != nil { | ||
// If the tx is being dropped - just ignore it | ||
// | ||
// TODO: Should we allow re-verification of the transaction even if it | ||
// failed previously? | ||
return reason | ||
} | ||
|
||
if err := g.txVerifier.VerifyTx(tx); err != nil { | ||
g.Mempool.MarkDropped(txID, err) | ||
return err | ||
} | ||
|
||
if err := g.Mempool.Add(tx); err != nil { | ||
g.Mempool.MarkDropped(txID, err) | ||
return err | ||
} | ||
|
||
g.lock.Lock() | ||
defer g.lock.Unlock() | ||
|
||
g.bloom.Add(tx) | ||
reset, err := gossip.ResetBloomFilterIfNeeded(g.bloom, g.Mempool.Len()*bloomChurnMultiplier) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if reset { | ||
g.log.Debug("resetting bloom filter") | ||
g.Mempool.Iterate(func(tx *txs.Tx) bool { | ||
g.bloom.Add(tx) | ||
return true | ||
}) | ||
} | ||
|
||
g.Mempool.RequestBuildBlock(false) | ||
return nil | ||
|
||
return errs | ||
} | ||
|
||
func (g *gossipMempool) Has(txID ids.ID) bool { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is going to work as expected if multiple transactions are provided. The
defer
will be executed on function return - not when the loop iterates.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops, overlooked that, thanks! Regarding X/P-Chain mempools, I'd rather put all the checks and tx verification inside the mempool not to acquire RLock for every lookup. Plus,
GetDropReason
acquires RW lock on mutex inside LRU cache, which would also be possible to rearrange if we do the verification inside Add. It's possible to pass txVerifier as a dependency to mempool I think.