Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed a bug that LiteDBStore.ForkStateReferences<T>() had thrown
ChainIdNotFoundException where a sourceChainId exists but
it has no state references.
  • Loading branch information
dahlia committed Sep 19, 2019
1 parent 2e02a7a commit 49b2af3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
6 changes: 4 additions & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ To be released.
HashDigest<SHA256>, long)` method so that it takes hash and index of
a block instead of an entire block. [[#420]]
- Added `IStore.ForkBlockIndexes()` method. [[#420]]
- Removed `addressesToStrip` parameter from `IStore.ForkStateReferences()`
method. [[#454], [#467]]
- Removed `addressesToStrip` parameter from `IStore.ForkStateReferences<T>()`
method. [[#454], [#467], [#509], [#522]]
- Removed the concept of "staged transactions that should not be broadcasted,"
because its primary usage had been to make a transaction of a reward action
for a candidate for block miner, and the case became achieved through
Expand Down Expand Up @@ -126,10 +126,12 @@ To be released.
[#498]: https://github.com/planetarium/libplanet/pull/498
[#496]: https://github.com/planetarium/libplanet/pull/496
[#508]: https://github.com/planetarium/libplanet/pull/508
[#509]: https://github.com/planetarium/libplanet/issues/509
[#511]: https://github.com/planetarium/libplanet/pull/511
[#512]: https://github.com/planetarium/libplanet/pull/512
[#519]: https://github.com/planetarium/libplanet/pull/519
[#520]: https://github.com/planetarium/libplanet/pull/520
[#522]: https://github.com/planetarium/libplanet/pull/522
[Kademlia]: https://en.wikipedia.org/wiki/Kademlia
[Guid]: https://docs.microsoft.com/ko-kr/dotnet/api/system.guid?view=netframework-4.8
[RFC 4122]: https://tools.ietf.org/html/rfc4122
Expand Down
1 change: 1 addition & 0 deletions Libplanet.Tests/Store/StoreFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public StoreFixture()
0x9c, 0xee,
});

// FIXME: Following names are misleading, because we index the genesis block 0, not 1.
Block1 = TestUtils.MineGenesis<DumbAction>();
Block2 = TestUtils.MineNext(Block1);
Block3 = TestUtils.MineNext(Block2);
Expand Down
9 changes: 9 additions & 0 deletions Libplanet.Tests/Store/StoreTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
using System.Security.Cryptography;
using System.Threading.Tasks;
using Libplanet.Action;
using Libplanet.Blockchain;
using Libplanet.Blocks;
using Libplanet.Crypto;
using Libplanet.Store;
using Libplanet.Tests.Blockchain;
using Libplanet.Tests.Common.Action;
using Libplanet.Tx;
using Xunit;
Expand Down Expand Up @@ -496,6 +498,13 @@ public void ForkStateReferencesChainIdNotFound()
Assert.Throws<ChainIdNotFoundException>(() =>
Fx.Store.ForkStateReferences(Fx.StoreChainId, targetChainId, Fx.Block1)
);

var chain = new BlockChain<DumbAction>(new NullPolicy<DumbAction>(), Fx.Store);
chain.Append(Fx.Block1);

// Even if state references in a chain are empty it should not throw
// ChainIdNotFoundException unless the chain in itself does not exist.
Fx.Store.ForkStateReferences(chain.Id, targetChainId, Fx.Block1);
}

[Fact]
Expand Down
16 changes: 8 additions & 8 deletions Libplanet/Store/LiteDBStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,20 +464,20 @@ public override void ForkStateReferences<T>(
Guid destinationChainId,
Block<T> branchPoint)
{
string srcCollId = StateRefId(sourceChainId);
string dstCollId = StateRefId(destinationChainId);
LiteCollection<StateRefDoc> srcColl = _db.GetCollection<StateRefDoc>(srcCollId),
dstColl = _db.GetCollection<StateRefDoc>(dstCollId);

dstColl.InsertBulk(srcColl.Find(Query.LTE("BlockIndex", branchPoint.Index)));

if (dstColl.Count() < 1)
if (CountIndex(sourceChainId) < 1)
{
throw new ChainIdNotFoundException(
sourceChainId,
"The source chain to be forked does not exist."
);
}

string srcCollId = StateRefId(sourceChainId);
string dstCollId = StateRefId(destinationChainId);
LiteCollection<StateRefDoc> srcColl = _db.GetCollection<StateRefDoc>(srcCollId),
dstColl = _db.GetCollection<StateRefDoc>(dstCollId);

dstColl.InsertBulk(srcColl.Find(Query.LTE("BlockIndex", branchPoint.Index)));
}

/// <inheritdoc/>
Expand Down

0 comments on commit 49b2af3

Please sign in to comment.