-
Notifications
You must be signed in to change notification settings - Fork 35.7k
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
Significantly Improve Wallet Load times #2733
Closed
Closed
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
|
||
#include "walletdb.h" | ||
#include "wallet.h" | ||
#include "hash.h" | ||
#include <boost/filesystem.hpp> | ||
|
||
using namespace std; | ||
|
@@ -191,6 +192,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, | |
// Taking advantage of the fact that pair serialization | ||
// is just the two items serialized one after the other | ||
ssKey >> strType; | ||
|
||
if (strType == "name") | ||
{ | ||
string strAddress; | ||
|
@@ -269,23 +271,41 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, | |
strErr = "Error reading wallet database: CPubKey corrupt"; | ||
return false; | ||
} | ||
|
||
CKey key; | ||
CPrivKey pkey; | ||
uint256 hash = 0; | ||
|
||
if (strType == "key") | ||
ssValue >> pkey; | ||
else { | ||
CWalletKey wkey; | ||
ssValue >> wkey; | ||
pkey = wkey.vchPrivKey; | ||
} | ||
if (!key.SetPrivKey(pkey, vchPubKey.IsCompressed())) | ||
|
||
try | ||
{ | ||
strErr = "Error reading wallet database: CPrivKey corrupt"; | ||
return false; | ||
ssValue >> hash; | ||
} | ||
catch(...){} | ||
|
||
bool fSkipCheck = false; | ||
|
||
if (hash != 0) | ||
{ | ||
if (Hash(pkey.begin(), pkey.end()) != hash) | ||
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. The hash should be calculated over both private and public key, as the reason for the test is guaranteeing that we have matching keypairs. |
||
{ | ||
strErr = "Error reading wallet database: CPrivKey corrupt"; | ||
return false; | ||
} | ||
|
||
fSkipCheck = true; | ||
} | ||
if (key.GetPubKey() != vchPubKey) | ||
|
||
if (!key.Load(pkey, vchPubKey, fSkipCheck)) | ||
{ | ||
strErr = "Error reading wallet database: CPrivKey pubkey inconsistency"; | ||
strErr = "Error reading wallet database: CPrivKey corrupt"; | ||
return false; | ||
} | ||
if (!pwallet->LoadKey(key, vchPubKey)) | ||
|
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.
Looks like ctx and point are memory leaked in the return true case...
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.
Do I need my glasses or is that a "goto" statement?
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.
"goto" use is just fine. It is efficient for both the compiler and human eyes.
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.
No, goto is not okay.
http://stackoverflow.com/questions/46586/goto-still-considered-harmful
It's true that there are exceedingly rare cases where a goto is useful but this is not one of them. Especially this case, error handling.
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.
This is code that I assume was copied from the OpenSSL sources, which uses such error handling extensively. Without RAII-like features (as it was copied from C), I also know of no less contrived way of dealing with this.
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.
Goto is fine in C code (in many cases the only sane way to do error
handling with proper cleanup), but in C++ we can indeed do better with RAII.
Using boost would be preferable to rolling our own RAII wrappers.
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'm not a c++ guy, but I believe there are RuntimeExceptions, right? And maybe they allows to pass description of error. It would be much better than to use goto.
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.
Rather than creating 90 Lines of RAII wrappers, You could probably just rewrite it like this:
Then just ammend:
Since I didn't test this there is probably a good reason why it wouldn't work, but you get the basic idea.
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 still prefer RAII. Make the compiler do the work of making it leak proof.
Safer and more convenient.
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.
Indeed making an EC_POINT class that implements EC_POINT_new in the constructor and EC_POINT_free in the destructor, as well as operator overloads for EC_POINT_mul and EC_POINT_cmp, is much more in the spirit of C++. I was really just addressing the comment:
Its not really an issue worth making a fuss about really. Perhaps I should have kept quiet.