-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Prioritize verification of higher fee TXs and do not starve RpcServer w/ Large mempool #356
Closed
jsolman
wants to merge
8
commits into
neo-project:master
from
aphtoken:PrioritizeVerificationOfHigherFeeTxs
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3a52d1c
Use R/W Locking for mempool, cache, and block headers for greater con…
0922f19
Prioritize verification of high fee txs and do not starve RpcServer w…
cb686df
Switch to more performant ReadWriteLockSlim.
44e4247
Fix bug when switching to ReadWriteLockSlim.
2c60c58
ReadWriterLockSlim does not default to supporting recursion, set it e…
f2e8383
Dispose RW locks when containing object is disposed.
fe38c1e
Merge branch 'master' into PrioritizeVerificationOfHigherFeeTxs
erikzhang 15a0e35
Fix sort order of transactions being verified.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,12 +143,56 @@ private void AddTransactionLoop() | |
{ | ||
new_tx_event.WaitOne(); | ||
|
||
bool shouldCoolDown = false; | ||
MemPoolReadWriteLock.EnterReadLock(); | ||
if (mem_pool.Count > 1000 && temp_pool.Count > 1000) shouldCoolDown = true; | ||
MemPoolReadWriteLock.ExitReadLock(); | ||
// Don't starve readers wanting to acquire the mem_pool lock | ||
if (shouldCoolDown) Thread.Sleep(1000); | ||
|
||
Transaction[] transactions; | ||
lock (temp_pool) | ||
{ | ||
if (temp_pool.Count == 0) continue; | ||
transactions = temp_pool.ToArray(); | ||
temp_pool.Clear(); | ||
if (temp_pool.Count < 1000) | ||
{ | ||
transactions = temp_pool.ToArray(); | ||
temp_pool.Clear(); | ||
} | ||
else | ||
{ | ||
// Attempt verifying large fee transactions first. | ||
transactions = temp_pool | ||
.OrderByDescending(p => | ||
{ | ||
try | ||
{ | ||
var fee = p.NetworkFee; | ||
return fee / p.Size; | ||
} | ||
catch (Exception) | ||
{ | ||
return Fixed8.Zero; | ||
} | ||
}) | ||
.ThenByDescending(p => | ||
{ | ||
try | ||
{ | ||
return p.NetworkFee; | ||
} | ||
catch (Exception) | ||
{ | ||
return Fixed8.Zero; | ||
} | ||
}) | ||
.ThenByDescending(p => new BigInteger(p.Hash.ToArray())) | ||
.Take(1000) | ||
.ToArray(); | ||
|
||
foreach (var tx in transactions) | ||
temp_pool.Remove(tx); | ||
} | ||
} | ||
ConcurrentBag<Transaction> verified = new ConcurrentBag<Transaction>(); | ||
lock (Blockchain.Default.PersistLock) | ||
|
@@ -230,8 +274,10 @@ private void Blockchain_PersistCompleted(object sender, Block block) | |
|
||
remain = mem_pool.Values.ToArray(); | ||
mem_pool.Clear(); | ||
|
||
if (millisSinceLastBlock > 10000) | ||
|
||
// Use normal AddTransactionLoop to verify if there is a large mem_pool to avoid | ||
// starvation of RpcSerer with large mem_pool. | ||
if (millisSinceLastBlock > 10000 && remain.Length < 1000) | ||
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. I should probably make these numbers constants also. |
||
{ | ||
ConcurrentBag<Transaction> verified = new ConcurrentBag<Transaction>(); | ||
// Reverify the remaining transactions in the mem_pool | ||
|
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 should make the 1000 here a constant to make it more easily configurable
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.
would be a bit more accurate actually if the condition were <= instead of <, but it doesn't make much of a difference just optimizes for the case that temp_pool.Count is exactly 1000.