Skip to content
Permalink
Browse files

Fixed a bug that related branch point index.

  • Loading branch information...
longfin committed Jun 11, 2019
1 parent d5db09e commit a11dff727c1e0c9f6a4a6a672fcc198421bcc786
Showing with 55 additions and 6 deletions.
  1. +53 −4 Libplanet.Tests/Store/StoreTest.cs
  2. +2 −2 Libplanet/Store/LiteDBStore.cs
@@ -4,6 +4,7 @@
using System.Security.Cryptography;
using Libplanet.Action;
using Libplanet.Blocks;
using Libplanet.Crypto;
using Libplanet.Store;
using Libplanet.Tests.Common.Action;
using Libplanet.Tx;
@@ -217,8 +218,11 @@ public void LookupStateReference()
Assert.Null(Fx.Store.LookupStateReference(Fx.StoreNamespace, address, prevBlock));
}

[Fact]
public void ForkStateReferences()
[InlineData(0)]
[InlineData(1)]
[InlineData(2)]
[Theory]
public void ForkStateReferences(int branchPointIndex)
{
Address address = Fx.Address1;
Block<DumbAction> prevBlock = Fx.Block3;
@@ -243,7 +247,7 @@ public void ForkStateReferences()
Fx.StoreNamespace, updatedAddresses.ToImmutableHashSet(), block);
}

var branchPoint = blocks[0];
var branchPoint = blocks[branchPointIndex];
var addressesToStrip = new[] { address }.ToImmutableHashSet();

Fx.Store.ForkStateReferences(
@@ -256,7 +260,7 @@ public void ForkStateReferences()
blocks[2].Hash,
Fx.Store.LookupStateReference(Fx.StoreNamespace, address, blocks[2]));
Assert.Equal(
blocks[0].Hash,
blocks[branchPointIndex].Hash,
Fx.Store.LookupStateReference(targetNamespace, address, blocks[2]));
}

@@ -354,5 +358,50 @@ public void TxNonce()
Assert.Equal(1, Fx.Store.GetTxNonce(Fx.StoreNamespace, Fx.Transaction1.Signer));
Assert.Equal(1, Fx.Store.GetTxNonce(Fx.StoreNamespace, Fx.Transaction2.Signer));
}

[InlineData(0)]
[InlineData(1)]
[InlineData(2)]
[Theory]
public void ForkTxNonce(int branchPointIndex)
{
var privateKey = new PrivateKey();
Address address = privateKey.PublicKey.ToAddress();
Block<DumbAction> prevBlock = Fx.Block3;
const string targetNamespace = "dummy";

var blocks = new List<Block<DumbAction>>
{
TestUtils.MineNext(
prevBlock,
new[] { Fx.MakeTransaction(privateKey: privateKey) }),
};
blocks.Add(
TestUtils.MineNext(
blocks[0],
new[] { Fx.MakeTransaction(privateKey: privateKey) }));
blocks.Add(
TestUtils.MineNext(
blocks[1],
new[] { Fx.MakeTransaction(privateKey: privateKey) }));

foreach (Block<DumbAction> block in blocks)
{
Fx.Store.IncreaseTxNonce(Fx.StoreNamespace, block);
}

var branchPoint = blocks[branchPointIndex];
Fx.Store.ForkTxNonce(
Fx.StoreNamespace,
targetNamespace,
branchPoint,
new[] { address }.ToImmutableHashSet());
Assert.Equal(
3,
Fx.Store.GetTxNonce(Fx.StoreNamespace, address));
Assert.Equal(
branchPointIndex + 1,
Fx.Store.GetTxNonce(targetNamespace, address));
}
}
}
@@ -416,7 +416,7 @@ public AddressStateMap GetBlockStates(HashDigest<SHA256> blockHash)
using (LiteFileStream srcStream = srcFile.OpenRead())
using (LiteFileStream destStream = destFile.OpenWrite())
{
while (srcStream.CanRead)
while (srcStream.Position < srcStream.Length)
{
var hashBytes = new byte[HashDigest<SHA256>.Size];
var indexBytes = new byte[sizeof(long)];
@@ -553,7 +553,7 @@ public void IncreaseTxNonce<T>(string @namespace, Block<T> block)
using (LiteFileStream srcStream = srcFile.OpenRead())
using (LiteFileStream destStream = destFile.OpenWrite())
{
while (srcStream.CanRead)
while (srcStream.Position < srcStream.Length)
{
var hashBytes = new byte[HashDigest<SHA256>.Size];
var indexBytes = new byte[sizeof(long)];

0 comments on commit a11dff7

Please sign in to comment.
You can’t perform that action at this time.