Skip to content

Conversation

achow101
Copy link
Member

@achow101 achow101 commented Sep 10, 2020

There are existing SaltedOutPointHasher and SaltedTxidHasher classes used for std::unordered_map and std::unordered_set that could be useful in other places in the codebase. So we these to their own saltedhash.{cpp/h} file. An existing KeyIDHasher is moved there too. Additionally, ScriptIDHasher, SaltedPubkeyHasher, and SaltedScriptHasher are added so that they can be used in future work.

KeyIDHasher and ScriptIDHasher are not salted so that equality comparisons of maps and sets keyed by CKeyID and CScriptID will actually work.

Split from #19602 (and a few other PRs/branches I have).

src/Makefile.am Outdated
@@ -193,6 +193,7 @@ BITCOIN_CORE_H = \
rpc/request.h \
rpc/server.h \
rpc/util.h \
saltedhash.h \
Copy link
Member

@laanwj laanwj Sep 11, 2020

Choose a reason for hiding this comment

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

Maybe place these in util or crypto? I'd prefer not to add files to the source root anymore when possible.

Copy link
Member Author

Choose a reason for hiding this comment

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

Moved to util.

@laanwj
Copy link
Member

laanwj commented Sep 11, 2020

Concept ACK

@DrahtBot
Copy link
Contributor

DrahtBot commented Sep 19, 2020

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

Copy link
Contributor

@promag promag left a comment

Choose a reason for hiding this comment

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

Concept ACK.

Maybe name the file hashers.{h,cpp}? Would be nice to see SaltedScriptHasher and SaltedPubkeyHasher being used - didn't find them in #19602 either.

Verified moved code in ea4c980 and 8ab5135 with git show --color-moved=dimmed_zebra.

BTW, should include util/saltedhash.h explicitly - it's indirectly included in coins.h.

@practicalswift
Copy link
Contributor

Concept ACK

+1 on @promag's feedback regarding hashers.{cpp,h} and making use of all hashers.

@achow101
Copy link
Member Author

Maybe name the file hashers.{h,cpp}?

Done.

Would be nice to see SaltedScriptHasher and SaltedPubkeyHasher being used - didn't find them in #19602 either.

I'm using them in another branch that I haven't opened a PR for yet.

@sipa
Copy link
Member

sipa commented Sep 28, 2020

For the non-optimized ones, it may be better to have a single SaltedSipHasher (or UnsaltedSipHasher) that takes a Span<const unsigned char> as input. That would automatically work for uint256, CPubKey, CScript, CKeyId, and generic ones like std::vector<unsigned char>.

@achow101
Copy link
Member Author

For the non-optimized ones, it may be better to have a single SaltedSipHasher (or UnsaltedSipHasher) that takes a Span<const unsigned char> as input. That would automatically work for uint256, CPubKey, CScript, CKeyId, and generic ones like std::vector<unsigned char>.

Good idea. Done.

Apparently we have other Hashers too, so I've added those to util/hasher.

Move the hashers that we use for hash tables to a common place.

Moved hashers:
- SaltedTxidHasher
- SaltedOutpointHasher
- FilterHeaderHasher
- SignatureCacheHasher
- BlockHasher
SaltedSipHasher is a generic hasher that can be used with most things we
would hash in an unordered container.
@laanwj
Copy link
Member

laanwj commented Nov 23, 2020

Code review ACK 281fd1a

I must say that in general I think throwing all hashers of all kinds of different data structures into one grabbag header is not a great subdivision criterion. However in this case it's all consensus transaction/block related things so I think it's fine!

But I wouldn't want to say, add P2P, wallet or mempool specific data structure hashers to this file.

@achow101
Copy link
Member Author

I must say that in general I think throwing all hashers of all kinds of different data structures into one grabbag header is not a great subdivision criterion. However in this case it's all consensus transaction/block related things so I think it's fine!

Right. What I had really needed was TxidHasher and OutpointHasher and it didn't seem right to be including txmempool.h and sigcache.h in wallet code.

Comment on lines +36 to +39
* This *must* return size_t. With Boost 1.46 on 32-bit systems the
* unordered_map will behave unpredictably if the custom hasher returns a
* uint64_t, resulting in failures when syncing the chain (#4634).
*
Copy link
Contributor

Choose a reason for hiding this comment

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

Our minimum boost version is now 1.58, and has been higher than 1.46 since #8920. According to this comment: #4635 (comment), boost versions higher than 1.54 are fine.

I think we can just delete this part of the comment rather than retaining it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Will remove if I have to push again

Copy link
Member

@jonatack jonatack left a comment

Choose a reason for hiding this comment

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

ACK 281fd1a, code review, debug build and ran bitcoind after rebasing to master @ dff0f6f

src/util/hasher.h could use clang formatting if you retouch

@@ -303,7 +303,7 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv

/* the HD chain data model (external chain counters) */
CHDChain m_hd_chain;
std::unordered_map<CKeyID, CHDChain, KeyIDHasher> m_inactive_hd_chains;
std::unordered_map<CKeyID, CHDChain, SaltedSipHasher> m_inactive_hd_chains;
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like KeyIDHasher could be removed since it's no longer used after this this change.

Copy link
Member Author

Choose a reason for hiding this comment

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

Will remove if I have to push again.

Copy link
Contributor

@fjahr fjahr left a comment

Choose a reason for hiding this comment

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

utACK 281fd1a

I agree with @mzumsande that KeyIDHasher seems not longer used and could be removed unless I am missing something.

#include <crypto/siphash.h>
#include <primitives/transaction.h>
#include <uint256.h>

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be great if there was a small comment here explaining what belongs into this file and what doesn't to help future contributors so they don't have to go back in git history to understand it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Will add if I have to push again.

@@ -0,0 +1,87 @@
// Copyright (c) 2019 The Bitcoin Core developers
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: 2020

Copy link
Member Author

Choose a reason for hiding this comment

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

Will fix if I have to push again.

@maflcko
Copy link
Member

maflcko commented Jan 13, 2021

sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jan 13, 2021
… new ones

281fd1a Replace KeyIDHasher with SaltedSipHasher (Andrew Chow)
210b693 Add generic SaltedSipHasher (Andrew Chow)
95e61c1 Move Hashers to util/hasher.{cpp/h} (Andrew Chow)

Pull request description:

  There are existing `SaltedOutPointHasher` and `SaltedTxidHasher` classes used for `std::unordered_map` and `std::unordered_set` that could be useful in other places in the codebase. So we these to their own `saltedhash.{cpp/h}` file. An existing `KeyIDHasher` is moved there too. Additionally, `ScriptIDHasher`, `SaltedPubkeyHasher`, and `SaltedScriptHasher` are added so that they can be used in future work.

  `KeyIDHasher` and `ScriptIDHasher` are not salted so that equality comparisons of maps and sets keyed by `CKeyID` and `CScriptID` will actually work.

  Split from bitcoin#19602 (and a few other PRs/branches I have).

ACKs for top commit:
  laanwj:
    Code review ACK 281fd1a
  jonatack:
    ACK 281fd1a, code review, debug build and ran bitcoind after rebasing to master @ dff0f6f
  fjahr:
    utACK 281fd1a

Tree-SHA512: bb03b231ccf3c9ecefc997b8da9c3770af4819f9be5b0a72997a103864e84046a2ac39b8eadf0dc9247bdccd53f86f433642e3a098882e6748341a9e7736271b
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jan 20, 2021
… new ones

281fd1a Replace KeyIDHasher with SaltedSipHasher (Andrew Chow)
210b693 Add generic SaltedSipHasher (Andrew Chow)
95e61c1 Move Hashers to util/hasher.{cpp/h} (Andrew Chow)

Pull request description:

  There are existing `SaltedOutPointHasher` and `SaltedTxidHasher` classes used for `std::unordered_map` and `std::unordered_set` that could be useful in other places in the codebase. So we these to their own `saltedhash.{cpp/h}` file. An existing `KeyIDHasher` is moved there too. Additionally, `ScriptIDHasher`, `SaltedPubkeyHasher`, and `SaltedScriptHasher` are added so that they can be used in future work.

  `KeyIDHasher` and `ScriptIDHasher` are not salted so that equality comparisons of maps and sets keyed by `CKeyID` and `CScriptID` will actually work.

  Split from bitcoin#19602 (and a few other PRs/branches I have).

ACKs for top commit:
  laanwj:
    Code review ACK 281fd1a
  jonatack:
    ACK 281fd1a, code review, debug build and ran bitcoind after rebasing to master @ dff0f6f
  fjahr:
    utACK 281fd1a

Tree-SHA512: bb03b231ccf3c9ecefc997b8da9c3770af4819f9be5b0a72997a103864e84046a2ac39b8eadf0dc9247bdccd53f86f433642e3a098882e6748341a9e7736271b
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jan 20, 2021
fanquake added a commit to bitcoin-core/gui that referenced this pull request Aug 24, 2021
…9935)

4c69571 doc: remove outdated comment (Martin Zumsande)
16652a9 refactor: Remove unused KeyIDHasher (Martin Zumsande)

Pull request description:

  Small follow-ups to  #19935:

  - Removal of unused `KeyIDHasher`  class ([comment in 19935](bitcoin/bitcoin#19935 (comment)))
  - Removal of an outdated comment, which referred to an old problem with the no longer supported Boost 1.46 and `boost::unordered_map`, now replaced by `std::unordered_map`. ([comment in 19935](bitcoin/bitcoin#19935 (comment)))

ACKs for top commit:
  Saviour1001:
    Tested ACK <code>[4c69571](https://github.com/bitcoin/bitcoin/commit/4c69571e6eeae2c03d59045ea102baa5fd1c3816)</code>
  Zero-1729:
    ACK 4c69571
  theStack:
    ACK 4c69571 🆗

Tree-SHA512: 243fda2120bfac6c40a268ca2c0f34482ce27e71fbc50005c0d13c2ad5db9ee72a037f9937c37cc50ed0f9f6f11ee6afee4ac50e5031d6876ec942f41f38dadf
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Aug 24, 2021
4c69571 doc: remove outdated comment (Martin Zumsande)
16652a9 refactor: Remove unused KeyIDHasher (Martin Zumsande)

Pull request description:

  Small follow-ups to  bitcoin#19935:

  - Removal of unused `KeyIDHasher`  class ([comment in 19935](bitcoin#19935 (comment)))
  - Removal of an outdated comment, which referred to an old problem with the no longer supported Boost 1.46 and `boost::unordered_map`, now replaced by `std::unordered_map`. ([comment in 19935](bitcoin#19935 (comment)))

ACKs for top commit:
  Saviour1001:
    Tested ACK <code>[4c69571](https://github.com/bitcoin/bitcoin/commit/4c69571e6eeae2c03d59045ea102baa5fd1c3816)</code>
  Zero-1729:
    ACK 4c69571
  theStack:
    ACK 4c69571 🆗

Tree-SHA512: 243fda2120bfac6c40a268ca2c0f34482ce27e71fbc50005c0d13c2ad5db9ee72a037f9937c37cc50ed0f9f6f11ee6afee4ac50e5031d6876ec942f41f38dadf
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 16, 2022
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.