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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
0a3eb0e
HashAlgorithmType class
dahlia Jun 18, 2021
0def61d
ByteUtil.TimingSafelyCompare() method
dahlia Jun 18, 2021
2b63bd5
Overload Hashcash.Answer(Stamp, HashAlgorithm, long, CancellationToken)
dahlia Jun 18, 2021
344e467
Remove unused internal methods
dahlia Jun 18, 2021
cd5f23a
Remove Hashcash.Hash() method
dahlia Jun 18, 2021
b82b744
Completely remove the existing HashCash.Answer() overload
dahlia Jun 18, 2021
86d14ab
Add hashAlgorithm param to Block<T>.MineBlock() method
dahlia Jun 21, 2021
1caa503
ByteUtil.Satisfies() method
dahlia Jun 22, 2021
796eae0
Remove BlockHash.Satisfies() method
dahlia Jun 22, 2021
06fead1
Change PreEvaluationHash's type to ImmutableArray<byte>
dahlia Jun 22, 2021
6b71a6c
Make BlockHash type fixed-size
dahlia Jun 22, 2021
58504bd
Add hashAlgorithm param to Validate() method
dahlia Jun 23, 2021
8f55806
Add hashAlgorithmGetter param to ActionEvaluator() ctor
dahlia Jun 23, 2021
8f809fd
HashAlgorithmGetter delegate
dahlia Jun 23, 2021
34b256c
Add hashAlgorithmGetter param to BlockSet() ctor
dahlia Jun 23, 2021
136785f
BlockHash.DeriveFrom() method
dahlia Jun 24, 2021
563143e
Add hashAlgorithm param to Block<T>() ctor
dahlia Jun 24, 2021
0a97720
Add hashAlgorithm param to BlockChain<T>.MakeGenesisBlock()
dahlia Jun 24, 2021
af12db6
Add IBlockPolicy<T>.GetHashAlgorithm() method
dahlia Jun 24, 2021
ff07eca
Add a compacter AttachStateRootHash<T>() overload
dahlia Jun 24, 2021
38b7971
Add hashAlgorithmGetter to BlockPolicy<T>() ctor
dahlia Jun 24, 2021
8dc9f89
HashAlgorithmTable.ToHashAlgorithmGetter() ext method
dahlia Jun 24, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 48 additions & 14 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,14 @@ To be released.
- Block hashes are now represented as `BlockHash`, which was introduced in
this release, which has been done as `HashDigest<SHA256>`.
[[#1192], [#1197]]
- Removed `HashDigest<T>.Satisfies()` method. This was replaced by
`BlockHash.Satisfies()` method instead.
- The type of `Block<T>.Hash` property became `BlockHash`
(was `HashDigest<SHA256>`).
- The type of `Block<T>.PreviousHash` property became `BlockHash?`
(was `HashDigest<SHA256>?`).
- The type of `Block<T>.PreEvaluationHash` property became `BlockHash?`
(was `HashDigest<SHA256>?`).
- The types of `Block<T>()` constructors' `hash` parameter became
`BlockHash` (were `HashDigest<SHA256>`).
- The types of `Block<T>()` constructors' `previousHash` parameter became
`BlockHash?` (were `HashDigest<SHA256>?`).
- The types of `Block<T>()` constructors' `preEvaluationHash` parameter
became `BlockHash?` (were `HashDigest<SHA256>?`).
- The type of `Block<T>.Mine()` method's `previousHash` parameter became
`BlockHash?` (was `HashDigest<SHA256>?`).
- The return type of `HashCash.Hash()` method became `BlockHash`
Expand Down Expand Up @@ -62,12 +56,6 @@ To be released.
`BlockHash` (was `HashDigest<SHA256>`).
- The type of `BlockVerificationState.VerifiedBlockHash` property became
`BlockHash` (was `HashDigest<SHA256>`).
- The type of `ActionEvaluation.EvaluateActionsGradually()` method's
`blockHash` parameter became `BlockHash` (was `HashDigest<SHA256>`).
- The type of `UnexpectedlyTerminatedActionException()` constructor's
`blockHash` parameter became `BlockHash?` (was `HashDigest<SHA256>?`).
- The type of `UnexpectedlyTerminatedActionException.BlockHash` property
became `BlockHash?` (was `HashDigest<SHA256>?`).
- The type of `IncompleteBlockStatesException()` constructor's
`blockHash` parameter became `BlockHash` (was `HashDigest<SHA256>`).
- The type of `IncompleteBlockStatesException.BlockHash` property
Expand Down Expand Up @@ -118,6 +106,46 @@ To be released.
- The type of `TrieStateStore.PruneStates()` method's `excludeBlockHashes`
parameter became `IImmutableSet<BlockHash>`
(was `ImmutableHashSet<HashDigest<SHA256>>`).
- Hash algorithm for <abbr title="proof-of-work">PoW</abbr> (Hashcash) became
configurable. [#1314], [#1352]
- Added `IBlockPolicy<T>.GetHashAlgorithm()` method.
- Added an optional `HashAlgorithmType? hashAlgorithm` parameter to
`Block<T>(long, long, BigInteger, Nonce, Address?, BlockHash?,
DateTimeOffset, IReadOnlyList<Transaction<T>>, ImmutableArray<byte>?,
HashDigest<SHA256>?, int protocolVersion)` constructor.
- Added `HashAlgorithmType hashAlgorithm` parameter to
`Block<T>.MineBlock()` method.
- The type of `Block<T>.PreEvaluationHash` property became
`ImmutableArray<byte>?` (was `HashDigest<SHA256>?`).
[[#1192], [#1197]]
- The types of `Block<T>()` constructors' `preEvaluationHash` parameter
became `ImmutableArray<byte>?` (were `HashDigest<SHA256>?`).
[[#1192], [#1197]]
- The type of
`InvalidBlockPreEvaluationHashException.ActualPreEvaluationHash` and
`ExpectedPreEvaluationHash` properties became `ImmutableArray<byte>`
(were `HashDigest<SHA256>`). [[#1192], [#1197]]
- The type of `InvalidBlockPreEvaluationHashException()` constructor's
`actualPreEvaluationHash` and and `expectedPreEvaluationHash` parameters
became `ImmutableArray<byte>` (were `HashDigest<SHA256>`).
[[#1192], [#1197]]
- Replaced `UnexpectedlyTerminatedActionException()` constructor's
`HashDigest<SHA256>? blockHash` parameter with
`ImmutableArray<byte>? preEvaluationHash`.
[[#1192], [#1197]]
- Replaced `UnexpectedlyTerminatedActionException.BlockHash` property with
`PreEvaluationHash.` [[#1192], [#1197]]
- Replaced `Hashcash.Answer(Stamp, long, CancellationToken)` method with
`Hashcash.Answer<T>(Stamp, HashAlgorithm, long, CancellationToken)`
method.
- Removed `Hashcash.Hash()` method.
- Removed `HashDigest<T>.Satisfies()` method. This was replaced by
`ByteUtil.Satisfies()` method instead. [[#1192], [#1197]]
- Added `hashAlgorithmGetter` parameter to `BlockSet<T>()` constructor.
- Added `hashAlgorithm` parameter to `BlockChain<T>.MakeGenesisBlock()`
method.
- Added an optional `hashAlgorithmGetter` parameter to `BlockPolicy<T>()`
constructor.
- Added `IActionContext.TxId` property. [[#1275]]
- Added `IStore.PutTxExecution(TxSuccess)` method. [[#1156], [#1289]]
- Added `IStore.PutTxExecution(TxFailure)` method. [[#1156], [#1289]]
Expand Down Expand Up @@ -159,8 +187,7 @@ To be released.
[[#1294], [#1328]]
- Added `IStore.DeleteTxIdBlockHashIndex(TxId, BlockHash)` method.
[[#1294], [#1328]]
- Added `IStore.IterateTxIdBlockHashIndex(TxId)` method.
[[#1294], [#1328]]
- Added `IStore.IterateTxIdBlockHashIndex(TxId)` method. [[#1294], [#1328]]
- `Swarm<T>.StartAsync()` method became to receive `broadcastBlockInterval`
(or `millisecondsBroadcastBlockInterval`) parameter. [[#1351]]

Expand All @@ -173,6 +200,9 @@ To be released.
- Added `ActionEvaluator` class. [[#1301], [#1305]]
- Added `BlockHash` struct. [[#1192], [#1197]]
- Added `HashDigest<T>.DeriveFrom()` method. [[#1197]]
- Added `HashAlgorithmType` class. [[#1314], [#1352]]
- Added `HashAlgorithmGetter` delegate. [[#1314], [#1352]]
- Added `HashAlgorithmTable` static class. [[#1314], [#1352]]
- Added `BlockChain<T>.GetTxExecution()` method. [[#1156], [#1289]]
- Added `StunMessage.ParseAsync(Stream, CancellationToken)` method.
[[#1228]]
Expand All @@ -193,6 +223,8 @@ To be released.
- Added `Address(Binary)` overloaded constructor. [[#1289]]
- Added `Currency(IValue)` overloaded constructor. [[#1289]]
- Added `Currency.Serialize()` method. [[#1289]]
- Added `ByteUtil.TimingSafelyCompare()` method. [[#1314], [#1352]]
- Added `ByteUtil.Satisfies()` method. [[#1314], [#1352]]

### Behavioral changes

Expand Down Expand Up @@ -313,6 +345,7 @@ To be released.
[#1298]: https://github.com/planetarium/libplanet/pull/1298
[#1301]: https://github.com/planetarium/libplanet/issues/1301
[#1305]: https://github.com/planetarium/libplanet/pull/1305
[#1314]: https://github.com/planetarium/libplanet/issues/1314
[#1315]: https://github.com/planetarium/libplanet/issues/1315
[#1316]: https://github.com/planetarium/libplanet/issues/1316
[#1320]: https://github.com/planetarium/libplanet/issues/1320
Expand All @@ -328,6 +361,7 @@ To be released.
[#1349]: https://github.com/planetarium/libplanet/issues/1349
[#1350]: https://github.com/planetarium/libplanet/pull/1350
[#1351]: https://github.com/planetarium/libplanet/pull/1351
[#1352]: https://github.com/planetarium/libplanet/pull/1352
[#1353]: https://github.com/planetarium/libplanet/pull/1353
[#1360]: https://github.com/planetarium/libplanet/pull/1360

Expand Down
6 changes: 4 additions & 2 deletions Libplanet.Benchmarks/Store.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Security.Cryptography;
using BenchmarkDotNet.Attributes;
using Libplanet.Blocks;
using Libplanet.Crypto;
Expand All @@ -13,6 +14,7 @@ namespace Libplanet.Benchmarks
{
public class Store
{
private readonly HashAlgorithmType HashAlgorithmType = HashAlgorithmType.Of<SHA256>();
private readonly ImmutableArray<Block<DumbAction>> Blocks = default;
private readonly int BlocksCount = default;
private readonly ImmutableArray<Transaction<DumbAction>> Txs = default;
Expand All @@ -24,7 +26,7 @@ public Store()
{
var blocks = new List<Block<DumbAction>>();
var txs = new List<Transaction<DumbAction>>();
Block<DumbAction> genesis = TestUtils.MineGenesis<DumbAction>();
Block<DumbAction> genesis = TestUtils.MineGenesis<DumbAction>(_ => HashAlgorithmType);
blocks.Add(genesis);
Block<DumbAction> block = genesis;
var key = new PrivateKey();
Expand All @@ -36,7 +38,7 @@ public Store()
{
blockTxs.Add(Transaction<DumbAction>.Create(nonce++, key, genesis.Hash, new DumbAction[0]));
}
block = TestUtils.MineNext(block, blockTxs);
block = TestUtils.MineNext(block, _ => HashAlgorithmType, blockTxs);
blocks.Add(block);
txs.AddRange(blockTxs);
}
Expand Down
15 changes: 9 additions & 6 deletions Libplanet.Benchmarks/SwarmBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Net;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
Expand Down Expand Up @@ -38,13 +39,13 @@ public SwarmBenchmark()
_stagePolicy = new VolatileStagePolicy<DumbAction>();
_blocks = new List<Block<DumbAction>>
{
TestUtils.MineGenesis<DumbAction>(),
TestUtils.MineGenesis<DumbAction>(_policy.GetHashAlgorithm),
};
_appProtocolVersion = AppProtocolVersion.Sign(new PrivateKey(), 1);
_blocks.Add(TestUtils.MineNext(_blocks[0]));
_blocks.Add(TestUtils.MineNext(_blocks[1]));
_blocks.Add(TestUtils.MineNext(_blocks[2]));
_blocks.Add(TestUtils.MineNext(_blocks[3]));
_blocks.Add(TestUtils.MineNext(_blocks[0], _policy.GetHashAlgorithm));
_blocks.Add(TestUtils.MineNext(_blocks[1], _policy.GetHashAlgorithm));
_blocks.Add(TestUtils.MineNext(_blocks[2], _policy.GetHashAlgorithm));
_blocks.Add(TestUtils.MineNext(_blocks[3], _policy.GetHashAlgorithm));
}

[IterationSetup(Targets = new[] {"BroadcastBlock", "BroadcastBlockWithoutFill"})]
Expand All @@ -55,7 +56,9 @@ public void InitializeSwarms()
_blockChains = new BlockChain<DumbAction>[SwarmNumber];
_swarms = new Swarm<DumbAction>[SwarmNumber];

var genesisBlock = BlockChain<DumbAction>.MakeGenesisBlock();
var genesisBlock = BlockChain<DumbAction>.MakeGenesisBlock(
_blockChains[SwarmNumber].Policy.GetHashAlgorithm(0)
);
var tasks = new List<Task>();
for (int i = 0; i < SwarmNumber; i++)
{
Expand Down
3 changes: 3 additions & 0 deletions Libplanet.Explorer.Executable/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,9 @@ BlockChain<AppAgnosticAction> blocks, Block<AppAgnosticAction> nextBlock
{
return _impl.ValidateNextBlock(blocks, nextBlock);
}

public HashAlgorithmType GetHashAlgorithm(long index) =>
_impl.GetHashAlgorithm(index);
}

internal class AppAgnosticAction : IAction
Expand Down
1 change: 1 addition & 0 deletions Libplanet.Explorer.UnitTests/GraphTypes/BlockTypeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public async void Query()
new BlockHash(TestUtils.GetRandomBytes(HashDigest<SHA256>.Size)),
DateTimeOffset.UtcNow,
ImmutableArray<Transaction<NoOpAction>>.Empty,
HashAlgorithmType.Of<SHA256>(),
stateRootHash: new HashDigest<SHA256>(
TestUtils.GetRandomBytes(HashDigest<SHA256>.Size)));
var query =
Expand Down
16 changes: 10 additions & 6 deletions Libplanet.Extensions.Cocona.Tests/Commands/StoreCommandTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Immutable;
using System.IO;
using System.Security.Cryptography;
using Cocona;
using Libplanet.Blocks;
using Libplanet.Crypto;
Expand Down Expand Up @@ -45,17 +46,17 @@ public StoreCommandTest()
throw new SkipException("RocksDB is not available.");
}

_genesisBlock = TestUtils.MineGenesis<Utils.DummyAction>();
_genesisBlock = TestUtils.MineGenesis<Utils.DummyAction>(GetHashAlgorithm);
_transaction1 = DummyTransaction();
_transaction2 = DummyTransaction();
_transaction3 = DummyTransaction();
_transaction4 = DummyTransaction();

_block1 = TestUtils.MineNext(_genesisBlock, new[] { _transaction1 });
_block2 = TestUtils.MineNext(_block1, new[] { _transaction2 });
_block3 = TestUtils.MineNext(_block2, new[] { _transaction3 });
_block4 = TestUtils.MineNext(_block3, new[] { _transaction3 });
_block5 = TestUtils.MineNext(_block4);
_block1 = TestUtils.MineNext(_genesisBlock, GetHashAlgorithm, new[] { _transaction1 });
_block2 = TestUtils.MineNext(_block1, GetHashAlgorithm, new[] { _transaction2 });
_block3 = TestUtils.MineNext(_block2, GetHashAlgorithm, new[] { _transaction3 });
_block4 = TestUtils.MineNext(_block3, GetHashAlgorithm, new[] { _transaction3 });
_block5 = TestUtils.MineNext(_block4, GetHashAlgorithm);

var guid = Guid.NewGuid();
foreach (var v in _storeFixtures)
Expand Down Expand Up @@ -310,6 +311,9 @@ public void Dispose()
Console.SetOut(_originalWriter);
}

private HashAlgorithmType GetHashAlgorithm(long blockIndex) =>
HashAlgorithmType.Of<SHA256>();

private Transaction<Utils.DummyAction> DummyTransaction()
{
return Transaction<Utils.DummyAction>.Create(
Expand Down
3 changes: 2 additions & 1 deletion Libplanet.Tests/Action/AccountStateDeltaImplTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ public override BlockChain<DumbAction> TransferAssetInBlock()
chain.Append(
TestUtils.MineNext(
chain.Tip,
chain.Policy.GetHashAlgorithm,
new[] { tx },
protocolVersion: ProtocolVersion
).AttachStateRootHash(chain.StateStore, chain.Policy.BlockAction)
).AttachStateRootHash(chain.StateStore, chain.Policy)
);
Assert.Equal(
DumbAction.DumbCurrency * 5,
Expand Down
3 changes: 2 additions & 1 deletion Libplanet.Tests/Action/AccountStateDeltaImplV0Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ public override BlockChain<DumbAction> TransferAssetInBlock()
chain.Append(
TestUtils.MineNext(
chain.Tip,
chain.Policy.GetHashAlgorithm,
new[] { tx },
protocolVersion: ProtocolVersion
).AttachStateRootHash(chain.StateStore, chain.Policy.BlockAction)
).AttachStateRootHash(chain.StateStore, chain.Policy)
);
Assert.Equal(
DumbAction.DumbCurrency * 6,
Expand Down
3 changes: 2 additions & 1 deletion Libplanet.Tests/Action/AccountStateDeltaTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,10 @@ public virtual BlockChain<DumbAction> TransferAssetInBlock()
chain.Append(
TestUtils.MineNext(
chain.Tip,
chain.Policy.GetHashAlgorithm,
new[] { tx },
protocolVersion: ProtocolVersion
).AttachStateRootHash(stateStore, chain.Policy.BlockAction)
).AttachStateRootHash(stateStore, chain.Policy)
);
Assert.Equal(
DumbAction.DumbCurrency * 5,
Expand Down
Loading