Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #798 from dahlia/block-completion
Restore BlockCompletion<T> (#707) and more
- Loading branch information
Showing
23 changed files
with
2,034 additions
and
387 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Security.Cryptography; | ||
using Libplanet.Blockchain; | ||
using NetMQ; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Libplanet.Tests.Blockchain | ||
{ | ||
public class BlockLocatorTest | ||
{ | ||
private readonly ITestOutputHelper _output; | ||
|
||
public BlockLocatorTest(ITestOutputHelper output) | ||
{ | ||
_output = output; | ||
} | ||
|
||
[Fact] | ||
public void Constructor() | ||
{ | ||
// Generate fixture block hashes looks like 0x000...1, 0x000...2, 0x000...3, and so on, | ||
// for the sake of easier debugging. | ||
ImmutableArray<HashDigest<SHA256>> blocks = Enumerable.Range(0, 0x10).Select(i => | ||
{ | ||
byte[] bytes = NetworkOrderBitsConverter.GetBytes(i); | ||
var buffer = new byte[HashDigest<SHA256>.Size]; | ||
bytes.CopyTo(buffer, buffer.Length - bytes.Length); | ||
return new HashDigest<SHA256>(buffer); | ||
}).ToImmutableArray(); | ||
|
||
var locator = new BlockLocator( | ||
indexBlockHash: idx => blocks[(int)(idx < 0 ? blocks.Length + idx : idx)], | ||
indexByBlockHash: hash => blocks.IndexOf(hash), | ||
sampleAfter: 5 | ||
); | ||
|
||
foreach (HashDigest<SHA256> hash in locator) | ||
{ | ||
_output.WriteLine(hash.ToString()); | ||
} | ||
|
||
Assert.Equal( | ||
new[] | ||
{ | ||
blocks[0xf], | ||
blocks[0xe], | ||
blocks[0xd], | ||
blocks[0xc], | ||
blocks[0xb], | ||
blocks[0xa], | ||
blocks[0x8], | ||
blocks[0x4], | ||
blocks[0x0], | ||
}, | ||
locator | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using Libplanet.Action; | ||
using Libplanet.Blockchain; | ||
using Serilog; | ||
using Serilog.Events; | ||
|
||
namespace Libplanet.Tests | ||
{ | ||
public static class LoggerExtensions | ||
{ | ||
public static void CompareBothChains<T>( | ||
this ILogger logger, | ||
LogEventLevel logLevel, | ||
string labelA, | ||
BlockChain<T> chainA, | ||
string labelB, | ||
BlockChain<T> chainB | ||
) | ||
where T : IAction, new() | ||
{ | ||
if (chainA is null) | ||
{ | ||
throw new ArgumentNullException(nameof(chainA)); | ||
} | ||
else if (chainB is null) | ||
{ | ||
throw new ArgumentNullException(nameof(chainB)); | ||
} | ||
|
||
if (!logger.IsEnabled(logLevel)) | ||
{ | ||
return; | ||
} | ||
|
||
void Print(string i, string x, string y) | ||
{ | ||
char bar = x.Equals(y) ? '|' : ':'; | ||
logger.Write(logLevel, $"{bar} {i,3} {bar} {x,-64} {bar} {y,-64} {bar}"); | ||
} | ||
|
||
long aTipIdx = chainA.Tip.Index; | ||
long bTipIdx = chainB.Tip.Index; | ||
Print("Idx", $"{labelA} (tip: {aTipIdx})", $"{labelB} (tip: {bTipIdx})"); | ||
long tipIdx = Math.Max(aTipIdx, bTipIdx); | ||
long idx = 0; | ||
while (idx <= tipIdx) | ||
{ | ||
Print( | ||
$"#{idx}", | ||
aTipIdx >= idx ? chainA[idx].ToString() : string.Empty, | ||
bTipIdx >= idx ? chainB[idx].ToString() : string.Empty | ||
); | ||
idx++; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.