From 405d514e5eb851c9cbcacfb4c8ab0469a0c5c56c Mon Sep 17 00:00:00 2001 From: Hong Minhee Date: Wed, 28 Aug 2019 21:09:53 +0900 Subject: [PATCH] Fix https://github.com/planetarium/libplanet/issues/465 --- CHANGES.md | 9 +++++++++ Libplanet.Tests/Store/StoreTest.cs | 32 ++++++++++++++++++++++++++++++ Libplanet/Store/LiteDBStore.cs | 7 ++++--- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index d5c0fb9865e..3a27636ff4a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,6 +8,15 @@ To be released. ### Bug fixes + - Fixed a bug that `Swarm.PreloadAsync()` method had thrown `LiteException` + (or other exception depending on `IStore`), which indicates a state + reference is duplicate, where `trustedStateValidators` is present and + a miner tries to download precalculated states from a trusted peer. + [[#465], [#474]] + +[#465]: https://github.com/planetarium/libplanet/issues/465 +[#474]: https://github.com/planetarium/libplanet/pull/474 + Version 0.5.1 ------------- diff --git a/Libplanet.Tests/Store/StoreTest.cs b/Libplanet.Tests/Store/StoreTest.cs index fb4c8f95f84..39c7545342c 100644 --- a/Libplanet.Tests/Store/StoreTest.cs +++ b/Libplanet.Tests/Store/StoreTest.cs @@ -360,6 +360,38 @@ public void IterateStateReferences() ); } + [Fact] + public void StoreStateReferenceAllowsDuplication() + { + Address address3 = new PrivateKey().PublicKey.ToAddress(); + Fx.Store.StoreStateReference( + Fx.StoreNamespace, + new[] { Fx.Address1, Fx.Address2 }.ToImmutableHashSet(), + Fx.Block1 + ); + Fx.Store.StoreStateReference( + Fx.StoreNamespace, + new[] { Fx.Address2, address3 }.ToImmutableHashSet(), + Fx.Block1 + ); + var expectedStateRefs = new[] + { + new Tuple, long>(Fx.Block1.Hash, Fx.Block1.Index), + }; + Assert.Equal( + expectedStateRefs, + Fx.Store.IterateStateReferences(Fx.StoreNamespace, Fx.Address1) + ); + Assert.Equal( + expectedStateRefs, + Fx.Store.IterateStateReferences(Fx.StoreNamespace, Fx.Address2) + ); + Assert.Equal( + expectedStateRefs, + Fx.Store.IterateStateReferences(Fx.StoreNamespace, address3) + ); + } + [InlineData(0)] [InlineData(1)] [InlineData(2)] diff --git a/Libplanet/Store/LiteDBStore.cs b/Libplanet/Store/LiteDBStore.cs index 92e4d54d4d6..2ad6a75593c 100644 --- a/Libplanet/Store/LiteDBStore.cs +++ b/Libplanet/Store/LiteDBStore.cs @@ -439,14 +439,15 @@ public override void StoreStateReference( { string collId = StateRefId(@namespace); LiteCollection coll = _db.GetCollection(collId); - coll.InsertBulk( - addresses.Select(addr => new StateRefDoc + IEnumerable stateRefDocs = addresses + .Select(addr => new StateRefDoc { Address = addr, BlockIndex = block.Index, BlockHash = block.Hash, }) - ); + .Where(doc => !coll.Exists(d => d.Id == doc.Id)); + coll.InsertBulk(stateRefDocs); coll.EnsureIndex("AddressString"); coll.EnsureIndex("BlockIndex"); }