-
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
Improve Large MemoryPool Performance - Sort + intelligent TX reverification #500
Conversation
…inteligent tx reverification.
This will be surely a great change, Jeff. |
Maybe we should create a background thread to specifically validate transactions instead of porting the policy plugin. |
I thought about having another thread for verifying transactions, but it may require some locking and add some additional complexity. I’ll think about it some more while I do some testing on this. This didn’t really port the whole policy plugin, but it does need access to how many transactions can be verified per block and how many of those are low priority. It still can end up with more transactions verified than the policy accepts by the time the consensus happens though, so the policy is still necessary with this as the changes currently stand. |
Whether I add an additional thread for verifying transactions or not, I do think it make sense to verify more transactions after Options:
Option 1 is probably the easiest to implement and I may go that route for this PR. There aren’t that many other messages the Blockchain actor is processing while waiting for a new block and transaction verification is taking place, so there doesn’t seem like there would be a significant benefit for another thread for the verification purpose, but let me think about it some more. |
Option 1 is fine, we can send an |
Send |
Thanks for the quick addition of the Idle message. I’ll update it to use the Idle message when it is working hours for me again. |
I updated call-outs in my PR overview to account for my latest changes that re-verify unverified transactions if necessary when the |
@shargon thanks for that optimization |
Better |
What about reordering transactions each time? Our memory pool can only contain up to 50,000 transactions, and reordering takes only 44 milliseconds. void TestSort()
{
Random rand = new Random();
byte[] buffer = new byte[sizeof(long)];
long[] values = new long[50000];
for (int i = 0; i < values.Length; i++)
{
rand.NextBytes(buffer);
values[i] = BitConverter.ToInt64(buffer, 0);
}
Stopwatch stopwatch = Stopwatch.StartNew();
values = values.OrderBy(p => p).ToArray();
stopwatch.Stop();
Console.WriteLine($"{stopwatch.ElapsedMilliseconds} ms"); // 44 ms
} |
Sorting on persist may be faster; I'll look tomorrow. |
Now testing filling the pool with high priority transactions. I have to reduce from allowing 500 per block to be able to test well in my PrivateNet or it verifies them as fast as I send them, and have trouble filling it up. It's because I'm sending by scripting a full node though; if I was using Rpc to send it would send them much faster. I might switch to sending that way later. |
I set MaxTransactionsPerBlock to 25 and low priority to 5 to make testing faster. Some interesting logs from testing as we hit max tx count for the pool filling with high priority transactions, the high priority TX start pushing out low priority from the pool (as expected):
Reduced TX per block again again, continuing to push out low priority transactions
Nearly all pushed out:
All out:
Summary of what was verified:
|
Final version is ready. |
Great job and report, my friend. Congratulations. |
Thanks Jeff, this is great work! It will provide a lot of improved stability. |
* Handles escape characters in JSON * Pass ApplicationExecution to IPersistencePlugin (neo-project#531) * Update dependencies: (neo-project#532) - Akka 1.3.11 - Microsoft.AspNetCore.ResponseCompression 2.2.0 - Microsoft.AspNetCore.Server.Kestrel 2.2.0 - Microsoft.AspNetCore.Server.Kestrel.Https 2.2.0 - Microsoft.AspNetCore.WebSockets 2.2.0 - Microsoft.EntityFrameworkCore.Sqlite 2.2.0 - Microsoft.Extensions.Configuration.Json 2.2.0 * change version to v2.9.4 * Updating Unknown to Policy Fail (neo-project#533) * Fix a dead lock in `WalletIndexer` * Downgrade Sqlite to 2.1.4 (neo-project#535) * RPC call gettransactionheight (neo-project#541) * getrawtransactionheight Nowadays two calls are need to get a transaction height, `getrawtransaction` with `verbose` and then use the `blockhash`. Other option is to use `confirmations`, but it can be misleading. * Minnor fix * Shargon's tip * modified * Allow to use the wallet inside a RPC plugin (neo-project#536) * Improve Large MemoryPool Performance - Sort + intelligent TX reverification (neo-project#500) Improve Large MemoryPool Performance - Sort + intelligent TX reverification (neo-project#500) * Keep both verified and unverified (previously verified) transactions in sorted trees so ejecting transactions above the pool size is a low latency operation. * Re-verify unverified transactions when Blockchain actor is idle. * Don't re-verify transactions needlessly when not at the tip of the chain. * Support passing a flag to `getrawmempool` to retrieve both verified and unverified TX hashes. * Support MaxTransactionsPerBlock and MaxFreeTransactionsPerBlock from Policy plugins. * Rebroadcast re-verified transactions if it has been a while since last broadcast (high priority transactions are rebroadcast more frequently than low priority transactions. * Policy filter GetRelayResult message (neo-project#543) * Policy filter GetRelayResult message * adding fixed numbering for return codes * Removed enum fixed values * Add some initial MemoryPool unit tests. Fix bug when Persisting the GenesisBlock (neo-project#549) * More MemoryPool Unit Tests. Improve Re-broadcast back-off to an increasing linear formula. (neo-project#554) * Ensuring Object Reference check of SortedSets for speed-up (neo-project#557) * Minor comments update on Mempool class (neo-project#556) * Update MemoryPool Unit Test to add random fees to Mock Transactions (neo-project#558) * Add Unit Test for MemoryPool sort order. Fixed sort order to return descending. (neo-project#559) * Add unit test to verify memory pool sort order and reverification order. Fixed sort order bug. * VerifyCanTransactionFitInPool works as intended. Also inadvertantly verified GetLowestFeeTransaction() works. * Benchmark structure for UInt classes (neo-project#553) * basic benchmark structure for UInt classes * commented code2 from lights for now * updated tests. all seem correct now * Switch to using a benchmark method taking a method delegate to benchmark. * Make pass. * 1 million iterations. * Switch to ulong for the 4th option, and it is still the same speed. * fix test data for 50% equal data * make test pass * neo.UnitTests/UT_UIntBenchmarks.cs * neo.UnitTests/UT_UIntBenchmarks.cs * Base 20 - UInt160 tests * neo.UnitTests/UT_UIntBenchmarks.cs * inlined 160 * complete tests with UInt256 and UInt160 * neo.UnitTests/UT_UIntBenchmarks.cs * Lights division calculation * Treat lower hashes as higher priority. Fix MemoryPool UT for Hash order. (neo-project#563) * Treat lower hashes as higher priority. * Fix MemoryPool UT for Hash order. * Renaming Trasanction in PoolItem for clarity. * Make PoolItem independent and add PoolItem tests (neo-project#562) * make poolitem independent * Merging * Multiply by -1 * Fix other * Fix Tx * Removing -1 extra multiplication * Fix * make PoolItem internal and added test class * Update PoolItem.cs * added comments for PoolItem variables * getting time from TimeProvider to allow testing * basic test * reset time provider * Add Hash comparison * Adding time provider again and equals * Fix arithmetic * Comment on PoolItem * Update PoolItem.cs * protecting tests against TimeProvider changes on fails * reusing setup part * fixed serialization properties * Improve generation of creating mock DateTime values. Implement hash comparison tests. * Adjust comment. * Treat Claim transactions as the highest low priority transactions. (neo-project#565) * Allow persistence plugins to commit as a final step. (neo-project#568) * Allow persistence plugins to commit as a final step. * Plugins commit before core commits, once all plugins have handled initial work OnPersist. * Allow PersistencePlugin to determine whether to crash if commit fails. * Add ShouldThrowExceptionFromCommit method to IPersistencePlugin. * Throw all commit exceptions that should be thrown in an AggregateException. * Add a Plugin type for observing transactions added or removed from the MemoryPool. (neo-project#580) * Correctly handle conversions between JSON objects (neo-project#586) * Fix neo-project/neo-node#297 (neo-project#587) * Replace new JArray with .ToArray (AccountState) (neo-project#581) * Ensure `LocalNode` to be stoped before shutting down the `NeoSystem`
* Handles escape characters in JSON * Pass ApplicationExecution to IPersistencePlugin (neo-project#531) * Update dependencies: (neo-project#532) - Akka 1.3.11 - Microsoft.AspNetCore.ResponseCompression 2.2.0 - Microsoft.AspNetCore.Server.Kestrel 2.2.0 - Microsoft.AspNetCore.Server.Kestrel.Https 2.2.0 - Microsoft.AspNetCore.WebSockets 2.2.0 - Microsoft.EntityFrameworkCore.Sqlite 2.2.0 - Microsoft.Extensions.Configuration.Json 2.2.0 * change version to v2.9.4 * Updating Unknown to Policy Fail (neo-project#533) * Fix a dead lock in `WalletIndexer` * Downgrade Sqlite to 2.1.4 (neo-project#535) * RPC call gettransactionheight (neo-project#541) * getrawtransactionheight Nowadays two calls are need to get a transaction height, `getrawtransaction` with `verbose` and then use the `blockhash`. Other option is to use `confirmations`, but it can be misleading. * Minnor fix * Shargon's tip * modified * Allow to use the wallet inside a RPC plugin (neo-project#536) * Improve Large MemoryPool Performance - Sort + intelligent TX reverification (neo-project#500) Improve Large MemoryPool Performance - Sort + intelligent TX reverification (neo-project#500) * Keep both verified and unverified (previously verified) transactions in sorted trees so ejecting transactions above the pool size is a low latency operation. * Re-verify unverified transactions when Blockchain actor is idle. * Don't re-verify transactions needlessly when not at the tip of the chain. * Support passing a flag to `getrawmempool` to retrieve both verified and unverified TX hashes. * Support MaxTransactionsPerBlock and MaxFreeTransactionsPerBlock from Policy plugins. * Rebroadcast re-verified transactions if it has been a while since last broadcast (high priority transactions are rebroadcast more frequently than low priority transactions. * Policy filter GetRelayResult message (neo-project#543) * Policy filter GetRelayResult message * adding fixed numbering for return codes * Removed enum fixed values * Add some initial MemoryPool unit tests. Fix bug when Persisting the GenesisBlock (neo-project#549) * More MemoryPool Unit Tests. Improve Re-broadcast back-off to an increasing linear formula. (neo-project#554) * Ensuring Object Reference check of SortedSets for speed-up (neo-project#557) * Minor comments update on Mempool class (neo-project#556) * Update MemoryPool Unit Test to add random fees to Mock Transactions (neo-project#558) * Add Unit Test for MemoryPool sort order. Fixed sort order to return descending. (neo-project#559) * Add unit test to verify memory pool sort order and reverification order. Fixed sort order bug. * VerifyCanTransactionFitInPool works as intended. Also inadvertantly verified GetLowestFeeTransaction() works. * Benchmark structure for UInt classes (neo-project#553) * basic benchmark structure for UInt classes * commented code2 from lights for now * updated tests. all seem correct now * Switch to using a benchmark method taking a method delegate to benchmark. * Make pass. * 1 million iterations. * Switch to ulong for the 4th option, and it is still the same speed. * fix test data for 50% equal data * make test pass * neo.UnitTests/UT_UIntBenchmarks.cs * neo.UnitTests/UT_UIntBenchmarks.cs * Base 20 - UInt160 tests * neo.UnitTests/UT_UIntBenchmarks.cs * inlined 160 * complete tests with UInt256 and UInt160 * neo.UnitTests/UT_UIntBenchmarks.cs * Lights division calculation * Treat lower hashes as higher priority. Fix MemoryPool UT for Hash order. (neo-project#563) * Treat lower hashes as higher priority. * Fix MemoryPool UT for Hash order. * Renaming Trasanction in PoolItem for clarity. * Make PoolItem independent and add PoolItem tests (neo-project#562) * make poolitem independent * Merging * Multiply by -1 * Fix other * Fix Tx * Removing -1 extra multiplication * Fix * make PoolItem internal and added test class * Update PoolItem.cs * added comments for PoolItem variables * getting time from TimeProvider to allow testing * basic test * reset time provider * Add Hash comparison * Adding time provider again and equals * Fix arithmetic * Comment on PoolItem * Update PoolItem.cs * protecting tests against TimeProvider changes on fails * reusing setup part * fixed serialization properties * Improve generation of creating mock DateTime values. Implement hash comparison tests. * Adjust comment. * Treat Claim transactions as the highest low priority transactions. (neo-project#565) * Allow persistence plugins to commit as a final step. (neo-project#568) * Allow persistence plugins to commit as a final step. * Plugins commit before core commits, once all plugins have handled initial work OnPersist. * Allow PersistencePlugin to determine whether to crash if commit fails. * Add ShouldThrowExceptionFromCommit method to IPersistencePlugin. * Throw all commit exceptions that should be thrown in an AggregateException. * Add a Plugin type for observing transactions added or removed from the MemoryPool. (neo-project#580) * Correctly handle conversions between JSON objects (neo-project#586) * Fix neo-project/neo-node#297 (neo-project#587) * Replace new JArray with .ToArray (AccountState) (neo-project#581) * Ensure `LocalNode` to be stoped before shutting down the `NeoSystem`
…cation (neo-project#500) Improve Large MemoryPool Performance - Sort + intelligent TX reverification (neo-project#500) * Keep both verified and unverified (previously verified) transactions in sorted trees so ejecting transactions above the pool size is a low latency operation. * Re-verify unverified transactions when Blockchain actor is idle. * Don't re-verify transactions needlessly when not at the tip of the chain. * Support passing a flag to `getrawmempool` to retrieve both verified and unverified TX hashes. * Support MaxTransactionsPerBlock and MaxFreeTransactionsPerBlock from Policy plugins. * Rebroadcast re-verified transactions if it has been a while since last broadcast (high priority transactions are rebroadcast more frequently than low priority transactions.
Update: Fairly extensive testing has been performed on this PR and it is now considered stable and ready for merging.
Background
When a large amount of transactions were broadcast to the chain, the overhead in the current design would cause performance issues. Also, new transactions with high fees, could be delayed. In the existing code base, after each block, transactions were removed and re-sent to the
Blockchain
mailbox in the order by highest fee. This could cause mailbox transaction messages to pile up. Also, since theBlockchain
mailbox treated these transaction messages all at the same priority level, so new transactions added with a high fee would still need to wait for those ahead of it in the mailbox to be re-verified before getting to them.Root Cause
A very large MemoryPool would lead to way too many transactions potentially needing to be re-verified each block. A lot of the re-verification is wasted effort also since the consensus will only take a subset (500) of such transactions per block. The ideal scenario would only be to reverify those that can make it into the next block.
Summary of Change
This change only re-verifies as many transactions as necessary to start off with a pool containing as many transactions as can be accepted in a block. To facilitate this, it is best to keep the transactions sorted so it can easily iterate the top transactions to be re-verified after persisting a block. Since it keeps transactions now sorted based on their priority by fee per byte, then fee, then TX hash, it can easily verify the transactions with minimal required effort. It also only attempts to re-verify transactions if it is at the header height. Additionally, it limits the time it will spend verifying transactions to add them back to the next block.
The mempool
Count
now includes all transactions that had been verified at some point that haven't become known to be invalid. TheVerifiedCount
andUnverifiedCount
are also now exposed.Closes #478
Closes #427
Closes #353
Closes #358
Closes #329
Closes #443
Closes #539
Closes #408 - May be debatable whether this should close this without a third category (aside from high and low priority) allowing a few completely free transactions to be persisted each block; but since we should not really promote hash war, I think it closes it.
Closes #428