Skip to content

Commit

Permalink
Explicitly store canonical chain
Browse files Browse the repository at this point in the history
Co-authored-by: Hong Minhee <hong.minhee@gmail.com>
  • Loading branch information
limebell and dahlia committed Aug 12, 2019
1 parent 8ad11a9 commit 9e75d06
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 47 deletions.
47 changes: 19 additions & 28 deletions Libplanet.Tests/Blockchain/BlockChainTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,25 @@ public void CanFindBlockByIndex()
Assert.Equal(anotherBlock, _blockChain[1]);
}

[Fact]
public void CanonicalId()
{
var x = _blockChain;
x.MineBlock(_fx.Address1);
x.MineBlock(_fx.Address1);
Assert.Equal(x.Id.ToString(), _fx.Store.GetCanonicalNamespace());
var y = x.Fork(x.Tip.Hash);
y.MineBlock(_fx.Address1);
Assert.Equal(x.Id.ToString(), _fx.Store.GetCanonicalNamespace());

var z = new BlockChain<DumbAction>(
new BlockPolicy<DumbAction>(new MinerReward(1)),
_fx.Store
);

Assert.Equal(x.Id, z.Id);
}

[Fact]
public void Enumerate()
{
Expand Down Expand Up @@ -1212,34 +1231,6 @@ public void MakeTransaction()
Assert.Equal(actions, transaction.Actions);
}

[Fact]
public void GetCanonicalChain()
{
Assert.Null(BlockChain<DumbAction>.GetCanonicalChain(_fx.Store));

Block<DumbAction> block1 = TestUtils.MineGenesis<DumbAction>();
Block<DumbAction> block2 = TestUtils.MineNext(block1);
Block<DumbAction> block3 = TestUtils.MineNext(block2);
Guid id1 = Guid.NewGuid();
Guid id2 = Guid.NewGuid();

foreach (Block<DumbAction> b in new[] { block1 })
{
_fx.Store.AppendIndex(id1.ToString(), b.Hash);
_fx.Store.PutBlock(b);
}

Assert.Equal(id1, BlockChain<DumbAction>.GetCanonicalChain(_fx.Store));

foreach (Block<DumbAction> b in new[] { block2, block3 })
{
_fx.Store.AppendIndex(id2.ToString(), b.Hash);
_fx.Store.PutBlock(b);
}

Assert.Equal(id2, BlockChain<DumbAction>.GetCanonicalChain(_fx.Store));
}

[Fact]
public void MineBlockWithBlockAction()
{
Expand Down
30 changes: 11 additions & 19 deletions Libplanet/Blockchain/BlockChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ public class BlockChain<T> : IReadOnlyList<Block<T>>
private readonly object _txLock;

public BlockChain(IBlockPolicy<T> policy, IStore store)
: this(policy, store, GetCanonicalChain(store) ?? Guid.NewGuid())
: this(
policy,
store,
store.GetCanonicalNamespace() is string ns ? Guid.Parse(ns) : Guid.NewGuid()
)
{
}

Expand All @@ -43,6 +47,11 @@ internal BlockChain(IBlockPolicy<T> policy, IStore store, Guid id)

_rwlock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
_txLock = new object();

if (Store.GetCanonicalNamespace() is null)
{
Store.SetCanonicalNamespace(Id.ToString());
}
}

~BlockChain()
Expand Down Expand Up @@ -522,24 +531,6 @@ public Transaction<T> MakeTransaction(
}
}

// FIXME it should be separated into separate class (like IConsensus).
internal static Guid? GetCanonicalChain(IStore store)
{
string @namespace = store
.ListNamespaces()
.OrderByDescending(store.CountIndex)
.FirstOrDefault();

if (@namespace is null)
{
return null;
}
else
{
return new Guid(@namespace);
}
}

internal void Append(
Block<T> block,
DateTimeOffset currentTime,
Expand Down Expand Up @@ -1009,6 +1000,7 @@ t.PreviousHash is HashDigest<SHA256> tp &&

Guid obsoleteId = Id;
Id = other.Id;
Store.SetCanonicalNamespace(Id.ToString());
Blocks = new BlockSet<T>(Store);
Transactions = new TransactionSet<T>(Store);
Store.DeleteNamespace(obsoleteId.ToString());
Expand Down

0 comments on commit 9e75d06

Please sign in to comment.