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

Make Hashcash algorithm configurable #1352

Merged
merged 22 commits into from
Jul 6, 2021

Conversation

dahlia
Copy link
Contributor

@dahlia dahlia commented Jun 24, 2021

This patch enables blockchain to consist of blocks with heterogeneous hash algorithms. Few points:

  • IBlockPolicy<T>.GetHashAlgorithm(long blockIndex) determines a hash algorithm for a block at blockIndex.
    • Blocks are not aware of which algorithm is used for hashing themselves.
  • BlockPolicy<T>() constructor takes an optional hashAlgorithmGetter which configures the above method. If omitted SHA-256 is used for every block in the chain.
    • HashAlgorithmTable.ToHashAlgoirthmGetter() extension method can be useful to describe a simple rule to determine hash algorithms by block indices. E.g., new Dictionary<long, HashAlgorithmType> { [0] = HashAlgorithmType.Of<SHA256>(), [1000] = HashAlgorithmType.Of<SHA512>() }.ToHashAlgorithmGetter() is equivalent to (idx) => idx >= 1000 ? HashAlgorithmType.Of<SHA512>() : HashAlgorithmType.Of<SHA256>().
  • HashAlgorithmType is a type for comparing hash algorithms.
    • It can be instantiated using Of<T>() method, like HashAlgorithmType.Of<SHA256>().
    • It implements equality methods and operators. E.g., HashAlgorithmType.Of<SHA256>() != HashAlgorithmType.Of<SHA1>(), HashAlgorithmType.Of<SHA256>().Equals(HashAlgorithmType.Of<SHA256>()). Also can be elements of ISet<T>/IImmutableSet<T> or keys of IDictionary<TKey, TValue>/IImmutableDictionary<TKey, TValue>.
  • As proof-of-work does not directly determine Block<T>.Hash, but Block<T>.PreEvaluationHash, BlockHash became fixed-size (32 bytes).
  • As various hash algorithms can be used for proof-of-work, Block<T>.PreEvaluationHash became represented as variable-length ImmutableArray<byte> (instead of BlockHash, which was misleading).

@dahlia dahlia self-assigned this Jun 24, 2021
@dahlia dahlia changed the title WIP: Make Hashcash algorithm configurable Make Hashcash algorithm configurable Jun 24, 2021
@codecov
Copy link

codecov bot commented Jun 24, 2021

Codecov Report

Merging #1352 (a504f62) into main (0033ed7) will decrease coverage by 8.83%.
The diff coverage is 49.55%.

❗ Current head a504f62 differs from pull request most recent head 8dc9f89. Consider uploading reports for the commit 8dc9f89 to get more accurate results

@@            Coverage Diff             @@
##             main    #1352      +/-   ##
==========================================
- Coverage   86.03%   77.19%   -8.84%     
==========================================
  Files         361      256     -105     
  Lines       32457    17257   -15200     
==========================================
- Hits        27925    13322   -14603     
- Misses       2747     3371     +624     
+ Partials     1785      564    -1221     
Impacted Files Coverage Δ
...bplanet.Explorer/Controllers/ExplorerController.cs 0.00% <0.00%> (ø)
...rer/Controllers/GenericControllerNameConvention.cs 0.00% <0.00%> (ø)
Libplanet.Explorer/Controllers/GraphQLBody.cs 0.00% <0.00%> (ø)
Libplanet.Explorer/ExplorerStartup.cs 0.00% <0.00%> (ø)
Libplanet.Explorer/GraphTypes/NodeStateType.cs 0.00% <0.00%> (ø)
...ibplanet.Explorer/Interfaces/IBlockChainContext.cs 0.00% <0.00%> (ø)
Libplanet.Explorer/Queries/BlockQuery.cs 0.00% <0.00%> (ø)
Libplanet.Explorer/Queries/ExplorerQuery.cs 0.00% <0.00%> (ø)
Libplanet.Explorer/Queries/TransactionQuery.cs 0.00% <0.00%> (ø)
Libplanet.Explorer/Store/AddressRefDoc.cs 0.00% <0.00%> (ø)
... and 268 more

@dahlia dahlia marked this pull request as ready for review June 24, 2021 07:51
@longfin longfin added the consensus Related to blockchain consensus label Jun 24, 2021
@@ -211,10 +214,13 @@ public void DetectInvalidProtocolVersion()
new Transaction<DumbAction>[] { },
protocolVersion: -1
);
Assert.Throws<InvalidBlockProtocolVersionException>(() => block.Validate(now));
Assert.Throws<InvalidBlockProtocolVersionException>(
() => block.Validate(_fx.GetHashAlgorithm(_fx.Next.Index), now)
Copy link
Member

Choose a reason for hiding this comment

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

We may need ValidationContext to consolidate the hash algorithm and DateTime... 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As HashAlgorithmGetter is usually from IBlockPolicy.GetHashAlgorithm, even if we make a new data type its construction would be unnatural IMHO.

Libplanet.Tests/Net/SwarmTest.Broadcast.cs Outdated Show resolved Hide resolved
longfin
longfin previously approved these changes Jun 28, 2021
longfin
longfin previously approved these changes Jun 30, 2021
limebell
limebell previously approved these changes Jul 1, 2021
@dahlia dahlia dismissed stale reviews from limebell and longfin via cc8faa8 July 1, 2021 08:22
@dahlia dahlia requested a review from longfin July 1, 2021 08:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
consensus Related to blockchain consensus
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hashcash's hash algorithm needs to be configurable
4 participants