Skip to content

Commit

Permalink
Remove HashAlgorithm from UntypedBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
greymistcube committed Aug 12, 2022
1 parent aee5f14 commit 974d667
Show file tree
Hide file tree
Showing 9 changed files with 6 additions and 38 deletions.
1 change: 0 additions & 1 deletion Libplanet.Benchmarks/Store.cs
Expand Up @@ -14,7 +14,6 @@ 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 Down
18 changes: 0 additions & 18 deletions Libplanet.Extensions.Cocona/Utils.cs
Expand Up @@ -144,7 +144,6 @@ public static IStore LoadStoreFromUri(string uriString)
{
new ByteArrayStringJsonConverter(),
new DateTimeOffsetJsonConverter(),
new HashAlgorithmTypeConverter(),
},
}
);
Expand Down Expand Up @@ -279,22 +278,5 @@ private class DateTimeOffsetJsonConverter : JsonConverter<DateTimeOffset>
));
}
}

private class HashAlgorithmTypeConverter : JsonConverter<HashAlgorithmType>
{
// FIXME: Placeholder implementation.
public override HashAlgorithmType Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options)
=> HashAlgorithmType.Of<SHA256>();

// FIXME: Placeholder implementation.
public override void Write(
Utf8JsonWriter writer,
HashAlgorithmType value,
JsonSerializerOptions options)
=> writer.WriteStringValue(nameof(SHA256));
}
}
}
2 changes: 1 addition & 1 deletion Libplanet.Net/Messages/BlockHeaderMsg.cs
Expand Up @@ -36,7 +36,7 @@ public BlockHeaderMsg(byte[][] dataFrames)
Codec.Encode(HeaderDictionary),
};

public BlockHeader GetHeader(HashAlgorithmGetter hashAlgorithmGetter)
public BlockHeader GetHeader()
{
return BlockMarshaler.UnmarshalBlockHeader(HeaderDictionary);
}
Expand Down
2 changes: 0 additions & 2 deletions Libplanet.Net/Swarm.BlockSync.cs
Expand Up @@ -437,8 +437,6 @@ in completedBlocks.WithCancellation(cancellationToken))
block.Index,
block.Hash
);
HashAlgorithmType hashAlgorithm =
workspace.Policy.GetHashAlgorithm(block.Index);
block.ValidateTimestamp();
workspace.Store.PutBlock(block);
if (tempTip is null ||
Expand Down
2 changes: 1 addition & 1 deletion Libplanet.Net/Swarm.MessageHandlers.cs
Expand Up @@ -137,7 +137,7 @@ private void ProcessBlockHeader(BlockHeaderMsg message)
BlockHeader header;
try
{
header = message.GetHeader(BlockChain.Policy.GetHashAlgorithm);
header = message.GetHeader();
}
catch (InvalidBlockException ibe)
{
Expand Down
3 changes: 1 addition & 2 deletions Libplanet.Node.Tests/UntypedBlockTest.cs
@@ -1,7 +1,6 @@
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Security.Cryptography;
using Bencodex;
using Libplanet.Action;
using Libplanet.Blocks;
Expand Down Expand Up @@ -83,7 +82,7 @@ public UntypedBlockTest()
public void Deserialize()
{
Bencodex.Types.Dictionary dict = _block.MarshalBlock();
var untyped = new UntypedBlock(_ => _sha256, dict);
var untyped = new UntypedBlock(dict);
Assert.Equal(_block.ProtocolVersion, untyped.ProtocolVersion);
Assert.Equal(_block.Index, untyped.Index);
Assert.Equal(_block.Timestamp, untyped.Timestamp);
Expand Down
13 changes: 3 additions & 10 deletions Libplanet.Node/UntypedBlock.cs
Expand Up @@ -6,7 +6,6 @@
using System.Security.Cryptography;
using Bencodex;
using Bencodex.Types;
using Libplanet.Blockchain.Policies;
using Libplanet.Blocks;
using Libplanet.Crypto;
using Libplanet.Tx;
Expand Down Expand Up @@ -64,17 +63,11 @@ public sealed class UntypedBlock : IBlockHeader
/// Decodes a Bencodex <paramref name="dictionary"/> into an <see cref="UntypedBlock"/>
/// instance.
/// </summary>
/// <param name="hashAlgorithmGetter">A function to determine the hash algorithm used
/// for the block to decode. See also <see cref="IBlockPolicy{T}.GetHashAlgorithm(long)"/>
/// method.</param>
/// <param name="dictionary">A Bencodex dictionary made using <see cref="ToBencodex()"/>
/// method or <see cref="BlockMarshaler.MarshalBlock{T}(Block{T})"/> method.</param>
/// <seealso cref="ToBencodex()"/>
/// <seealso cref="BlockMarshaler.MarshalBlock{T}(Block{T})"/>
public UntypedBlock(
HashAlgorithmGetter hashAlgorithmGetter,
Bencodex.Types.Dictionary dictionary
)
public UntypedBlock(Bencodex.Types.Dictionary dictionary)
: this(
BlockMarshaler.UnmarshalBlockHeader(
dictionary.GetValue<Dictionary>(BlockMarshaler.HeaderKey)),
Expand Down Expand Up @@ -138,10 +131,10 @@ Bencodex.Types.Dictionary dictionary
/// <returns>A Bencodex dictionary which encodes this block. This is equivalent to
/// <see cref="BlockMarshaler.MarshalBlock{T}(Block{T})"/> method's return value.
/// This can be decoded back to <see cref="UntypedBlock"/> using
/// <see cref="UntypedBlock(HashAlgorithmGetter, Dictionary)"/> constructor or
/// <see cref="UntypedBlock(Dictionary)"/> constructor or
/// <see cref="BlockMarshaler.UnmarshalBlock{T}"/>
/// method.</returns>
/// <seealso cref="UntypedBlock(HashAlgorithmGetter, Dictionary)"/>
/// <seealso cref="UntypedBlock(Dictionary)"/>
/// <seealso cref="BlockMarshaler.UnmarshalBlock{T}"/>
public Bencodex.Types.Dictionary ToBencodex()
{
Expand Down
1 change: 0 additions & 1 deletion Libplanet.Tests/Blocks/BlockMetadataTest.cs
@@ -1,6 +1,5 @@
using System;
using System.Collections.Immutable;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using Bencodex;
Expand Down
2 changes: 0 additions & 2 deletions Libplanet/Hashcash.cs
Expand Up @@ -14,8 +14,6 @@ namespace Libplanet
/// </summary>
public static class Hashcash
{
private static HashAlgorithmType _algo = HashAlgorithmType.Of<SHA256>();

/// <summary>
/// A delegate to determine a consistent <see cref="byte"/>s
/// representation derived from a given <paramref name="nonce"/>.
Expand Down

0 comments on commit 974d667

Please sign in to comment.