Skip to content
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

Rewrite addrdb with less duplication using CHashVerifier #10248

Merged
merged 1 commit into from Jun 22, 2017

Conversation

sipa
Copy link
Member

@sipa sipa commented Apr 21, 2017

This rewrites addrdb.cpp to use CHashVerifier (from #10195), and combines the code for addrdb and bandb to the extent possible.

Copy link
Contributor

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK d76b7ac079e140948d589f5c53a4af15ddcad6f5

@@ -1455,19 +1455,18 @@ bool UndoReadFromDisk(CBlockUndo& blockundo, const CDiskBlockPos& pos, const uin

// Read block
uint256 hashChecksum;
CHashVerifier<CAutoFile> verifier(&filein); // We need a CHashVerifier as reserializing may lose data
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this comment is wrong if this is pulled out of #10195.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed; fixed.

Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK 0e8d567209ba198f64c7657acb0e05018bf0c5be

src/hash.h Outdated
char data[1024];
while (nSize > 0) {
size_t now = std::min<size_t>(nSize, 1024);
source->read(data, now);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In commit "Introduce CHashVerifier to hash read data"

Could replace these two lines with just read(data, now);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}
catch (const std::exception& e) {
// de-serialization has failed, ensure addrman is left in a clean state
bool ret = DeserializeDB(ssPeers, addr, false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In commit "Deduplicate addrdb.cpp and use CHashWriter/Verifier"

Not your change, but it isn't clear to me why this why this function is a part of the CAddrDB class and is non-static. It doesn't seem to access any state or be directly related to the class.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made it static.

return error("%s: Failed to open file %s", __func__, pathAddr.string());

// use file size to size memory buffer
uint64_t fileSize = fs::file_size(pathAddr);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In commit "Deduplicate addrdb.cpp and use CHashWriter/Verifier"

It seems this logic to read the file into a memory buffer before deserializing it has gone away. Any idea why it was written that way in the first place?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it's to protect against the case where the data is unusually expensive to deserialize. I'm not particularly worried about (non-adverserial) corruption that causes expensive deserialization without causing an error somewhere along the way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC the point was to be able to check the checksum before starting any deserialization work

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. I don't think that's particularly interesting, but if that remains a requirement, I'll drop this PR.

src/hash.h Outdated
CHashVerifier(Source* source_) : CHashWriter(source_->GetType(), source_->GetVersion()), source(source_) {}

void read(char* pch, size_t nSize)
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please check for nSize == 0 to avoid problems like #10250

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that the invoked read and write method already need to deal with nSize==0 anyway?

src/hash.h Outdated
Source* source;

public:
CHashVerifier(Source* source_) : CHashWriter(source_->GetType(), source_->GetVersion()), source(source_) {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pass a reference instead, so that it can't be null?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like using references for objects that persist. This risks calling CHashVerifier(function_that_returns_a_Source()).

@theuni
Copy link
Member

theuni commented May 8, 2017

utACK, other than the nits

@sipa
Copy link
Member Author

sipa commented May 17, 2017

I think I've addressed or responded to all comments.

@sipa
Copy link
Member Author

sipa commented Jun 1, 2017

The first commits of this PR are merged as part of #10195. I'm going to change the title to just reflect the effect of the last remaining commit.

@sipa sipa changed the title Introduce CHashVerifier to hash while deserializing Rewrite addrdb with less duplication using CHashVerifier Jun 1, 2017
Copy link
Contributor

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to leave comment for future PR.

utACK cf68a48

pathBanlist = GetDataDir() / "banlist.dat";
// Write and commit header, data
try {
CHashWriter hasher(SER_DISK, CLIENT_VERSION);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems it may be nicer to add a more analygous hasher to CHashVerifyer here - why not hash while writing the data instead of serializing twice?

@laanwj
Copy link
Member

laanwj commented Jun 22, 2017

utACK cf68a48

@laanwj laanwj merged commit cf68a48 into bitcoin:master Jun 22, 2017
laanwj added a commit that referenced this pull request Jun 22, 2017
cf68a48 Deduplicate addrdb.cpp and use CHashWriter/Verifier (Pieter Wuille)

Tree-SHA512: 0301332e797f64da3a1588c9ebaf533af58da41e38f8a64206bff20102c5e82c2a7c630ca3150cf451b2ccf4acb3dd45e44259b6ba15e92786e9e9a2b225bd2f
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 6, 2019
…Verifier

cf68a48 Deduplicate addrdb.cpp and use CHashWriter/Verifier (Pieter Wuille)

Tree-SHA512: 0301332e797f64da3a1588c9ebaf533af58da41e38f8a64206bff20102c5e82c2a7c630ca3150cf451b2ccf4acb3dd45e44259b6ba15e92786e9e9a2b225bd2f
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 8, 2019
…Verifier

cf68a48 Deduplicate addrdb.cpp and use CHashWriter/Verifier (Pieter Wuille)

Tree-SHA512: 0301332e797f64da3a1588c9ebaf533af58da41e38f8a64206bff20102c5e82c2a7c630ca3150cf451b2ccf4acb3dd45e44259b6ba15e92786e9e9a2b225bd2f
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 9, 2019
…Verifier

cf68a48 Deduplicate addrdb.cpp and use CHashWriter/Verifier (Pieter Wuille)

Tree-SHA512: 0301332e797f64da3a1588c9ebaf533af58da41e38f8a64206bff20102c5e82c2a7c630ca3150cf451b2ccf4acb3dd45e44259b6ba15e92786e9e9a2b225bd2f
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 11, 2019
…Verifier

cf68a48 Deduplicate addrdb.cpp and use CHashWriter/Verifier (Pieter Wuille)

Tree-SHA512: 0301332e797f64da3a1588c9ebaf533af58da41e38f8a64206bff20102c5e82c2a7c630ca3150cf451b2ccf4acb3dd45e44259b6ba15e92786e9e9a2b225bd2f
barrystyle pushed a commit to PACGlobalOfficial/PAC that referenced this pull request Jan 22, 2020
…Verifier

cf68a48 Deduplicate addrdb.cpp and use CHashWriter/Verifier (Pieter Wuille)

Tree-SHA512: 0301332e797f64da3a1588c9ebaf533af58da41e38f8a64206bff20102c5e82c2a7c630ca3150cf451b2ccf4acb3dd45e44259b6ba15e92786e9e9a2b225bd2f
random-zebra added a commit to PIVX-Project/PIVX that referenced this pull request Apr 20, 2021
…+ Add file syncing logging and error handling

d593e7e Migrate remaining FLATDATA serialization, we are natively supporting it now. (furszy)
051970d addrdb: Remove temporary files created in SerializeFileDB. Fixes non-determinism in unit tests. (practicalswift)
4be426f Add logging and error handling for file syncing (Wladimir J. van der Laan)
8662fb3 Remove unused double_safe_addition & double_safe_multiplication functions. (furszy)
a7c3885 Add native support for serializing char arrays without FLATDATA (Pieter Wuille)
459ecb9 Deduplicate addrdb.cpp and use CHashWriter/Verifier (Pieter Wuille)
a5d2f8a Fix copypasted comment. (Pavel Janík)

Pull request description:

  Digging down the peers and ban databases commits path in upstream found some good stuff, back ported the following PRs:

  * bitcoin#9216.
  * bitcoin#10248.
  * bitcoin#12740.
  * bitcoin#13039.
  * bitcoin#16212.

ACKs for top commit:
  random-zebra:
    ACK d593e7e
  Fuzzbawls:
    ACK d593e7e

Tree-SHA512: 8d567c68a2c36f43c749d5faa4b7f8a299dbfdda133495df434ac642c1a2ac96dc2259123ad85decc9039c62c46602665f6be4aadf6d5bf729344c51692ec60e
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants