Skip to content

Commit

Permalink
Merge pull request #3757 from greymistcube/port/4.3.1-to-4.4
Browse files Browse the repository at this point in the history
🔀 Port 4.3.1 to 4.4
  • Loading branch information
greymistcube authored Apr 18, 2024
2 parents da8689a + fada7fc commit 90e7057
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 33 deletions.
16 changes: 16 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ Version 4.4.1

To be released.

- Ported changes from [Libplanet 4.3.1] release. [[#3757]]

[#3757]: https://github.com/planetarium/libplanet/pull/3757
[Libplanet 4.3.1]: https://www.nuget.org/packages/Libplanet/4.3.1


Version 4.4.0
-------------
Expand Down Expand Up @@ -52,6 +57,17 @@ Released on April 17, 2024.
[#3746]: https://github.com/planetarium/libplanet/pull/3746


Version 4.3.1
-------------

Released on April 18, 2024.

- Downgraded *LiteDB* from [5.0.15][LiteDB 5.0.15] to
[4.1.4][LiteDB 4.1.4]. [[#3753]]

[#3753]: https://github.com/planetarium/libplanet/pull/3753


Version 4.3.0
-------------

Expand Down
1 change: 1 addition & 0 deletions Libplanet.Explorer.Executable/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ private static IStore LoadStore(Options options)
case "default":
return new DefaultStore(
options.StorePath,
flush: false,
readOnly: readOnlyMode);
default:
// FIXME: give available store type as argument hint without code duplication.
Expand Down
90 changes: 58 additions & 32 deletions Libplanet.Store/DefaultStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Serilog;
using Zio;
using Zio.FileSystems;
using FileMode = LiteDB.FileMode;

namespace Libplanet.Store
{
Expand All @@ -36,19 +37,36 @@ namespace Libplanet.Store
/// <para>The following query string parameters are supported:</para>
/// <list type="table">
/// <item>
/// <term><c>journal</c></term>
/// <description><see langword="true"/> (default) or <see langword="false"/>. Corresponds to
/// <see cref="DefaultStore(string, bool, int, int, int, bool, bool)"/>'s <c>journal</c>
/// parameter.</description>
/// </item>
/// <item>
/// <term><c>index-cache</c></term>
/// <description>Corresponds to <see cref="DefaultStore(string,bool,int,int,int,bool,bool)"/>'s
/// <c>indexCacheSize</c> parameter. 50000 by default.</description>
/// </item>
/// <item>
/// <term><c>block-cache</c></term>
/// <description>Corresponds to <see cref="DefaultStore(string, int, int, bool)"/>'s
/// <description>Corresponds to <see cref="DefaultStore(string,bool,int,int,int,bool,bool)"/>'s
/// <c>blockCacheSize</c> parameter. 512 by default.</description>
/// </item>
/// <item>
/// <term><c>tx-cache</c></term>
/// <description>Corresponds to <see cref="DefaultStore(string, int, int, bool)"/>'s
/// <description>Corresponds to <see cref="DefaultStore(string,bool,int,int,int,bool,bool)"/>'s
/// <c>txCacheSize</c> parameter. 1024 by default.</description>
/// </item>
/// <item>
/// <term><c>flush</c></term>
/// <description><see langword="true"/> (default) or <see langword="false"/>. Corresponds to
/// <see cref="DefaultStore(string, bool, int, int, int, bool, bool)"/>'s <c>flush</c>
/// parameter.</description>
/// </item>
/// <item>
/// <term><c>readonly</c></term>
/// <description><see langword="true"/> or <see langword="false"/> (default). Corresponds to
/// <see cref="DefaultStore(string, int, int, bool)"/>'s <c>readOnly</c>
/// <see cref="DefaultStore(string, bool, int, int, int, bool, bool)"/>'s <c>readOnly</c>
/// parameter.</description>
/// </item>
/// <item>
Expand Down Expand Up @@ -106,16 +124,20 @@ public class DefaultStore : BaseStore
/// <param name="readOnly">Opens database readonly mode. Turned off by default.</param>
public DefaultStore(
string path,
bool journal = true,
int indexCacheSize = 50000,
int blockCacheSize = 512,
int txCacheSize = 1024,
bool readOnly = false)
bool flush = true,
bool readOnly = false
)
{
_logger = Log.ForContext<DefaultStore>();

if (path is null)
{
_root = new MemoryFileSystem();
_db = new LiteDatabase(new MemoryStream());
_db = new LiteDatabase(new MemoryStream(), disposeStream: true);
}
else
{
Expand All @@ -136,22 +158,20 @@ public DefaultStore(
var connectionString = new ConnectionString
{
Filename = Path.Combine(path, "index.ldb"),
Journal = journal,
CacheSize = indexCacheSize,
Flush = flush,
};

if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) &&
Type.GetType("Mono.Runtime") is null)
{
// macOS + .NETCore doesn't support shared lock.
connectionString.Connection = ConnectionType.Direct;
}
else
if (readOnly)
{
connectionString.Connection = ConnectionType.Shared;
connectionString.Mode = FileMode.ReadOnly;
}

if (readOnly)
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) &&
Type.GetType("Mono.Runtime") is null)
{
connectionString.ReadOnly = true;
// macOS + .NETCore doesn't support shared lock.
connectionString.Mode = FileMode.Exclusive;
}

_db = new LiteDatabase(connectionString);
Expand Down Expand Up @@ -212,7 +232,7 @@ public override void DeleteChainId(Guid chainId)
/// <inheritdoc />
public override Guid? GetCanonicalChainId()
{
ILiteCollection<BsonDocument> collection = _db.GetCollection<BsonDocument>("canon");
LiteCollection<BsonDocument> collection = _db.GetCollection<BsonDocument>("canon");
var docId = new BsonValue("canon");
BsonDocument doc = collection.FindById(docId);
if (doc is null)
Expand All @@ -228,7 +248,7 @@ public override void DeleteChainId(Guid chainId)
/// <inheritdoc />
public override void SetCanonicalChainId(Guid chainId)
{
ILiteCollection<BsonDocument> collection = _db.GetCollection<BsonDocument>("canon");
LiteCollection<BsonDocument> collection = _db.GetCollection<BsonDocument>("canon");
var docId = new BsonValue("canon");
byte[] idBytes = chainId.ToByteArray();
collection.Upsert(docId, new BsonDocument() { ["chainId"] = new BsonValue(idBytes) });
Expand Down Expand Up @@ -278,8 +298,8 @@ public override void ForkBlockIndexes(
Guid destinationChainId,
BlockHash branchpoint)
{
ILiteCollection<HashDoc> srcColl = IndexCollection(sourceChainId);
ILiteCollection<HashDoc> destColl = IndexCollection(destinationChainId);
LiteCollection<HashDoc> srcColl = IndexCollection(sourceChainId);
LiteCollection<HashDoc> destColl = IndexCollection(destinationChainId);

BlockHash? genesisHash = IterateIndexes(sourceChainId, 0, 1)
.Cast<BlockHash?>()
Expand All @@ -290,8 +310,8 @@ public override void ForkBlockIndexes(
return;
}

destColl.DeleteAll();
destColl.Insert(srcColl.FindAll().TakeWhile(i => !i.Hash.Equals(branchpoint)));
destColl.Delete(Query.All());
destColl.InsertBulk(srcColl.FindAll().TakeWhile(i => !i.Hash.Equals(branchpoint)));

AppendIndex(destinationChainId, branchpoint);
}
Expand Down Expand Up @@ -541,7 +561,7 @@ public override void DeleteTxIdBlockHashIndex(TxId txId, BlockHash blockHash)
/// <inheritdoc/>
public override IEnumerable<KeyValuePair<Address, long>> ListTxNonces(Guid chainId)
{
ILiteCollection<BsonDocument> collection = TxNonceCollection(chainId);
LiteCollection<BsonDocument> collection = TxNonceCollection(chainId);
foreach (BsonDocument doc in collection.FindAll())
{
if (doc.TryGetValue("_id", out BsonValue id) && id.IsBinary)
Expand All @@ -558,7 +578,7 @@ public override IEnumerable<KeyValuePair<Address, long>> ListTxNonces(Guid chain
/// <inheritdoc/>
public override long GetTxNonce(Guid chainId, Address address)
{
ILiteCollection<BsonDocument> collection = TxNonceCollection(chainId);
LiteCollection<BsonDocument> collection = TxNonceCollection(chainId);
var docId = new BsonValue(address.ToByteArray());
BsonDocument doc = collection.FindById(docId);

Expand All @@ -574,16 +594,16 @@ public override long GetTxNonce(Guid chainId, Address address)
public override void IncreaseTxNonce(Guid chainId, Address signer, long delta = 1)
{
long nextNonce = GetTxNonce(chainId, signer) + delta;
ILiteCollection<BsonDocument> collection = TxNonceCollection(chainId);
LiteCollection<BsonDocument> collection = TxNonceCollection(chainId);
var docId = new BsonValue(signer.ToByteArray());
collection.Upsert(docId, new BsonDocument() { ["v"] = new BsonValue(nextNonce) });
}

/// <inheritdoc/>
public override void ForkTxNonces(Guid sourceChainId, Guid destinationChainId)
{
ILiteCollection<BsonDocument> srcColl = TxNonceCollection(sourceChainId);
ILiteCollection<BsonDocument> destColl = TxNonceCollection(destinationChainId);
LiteCollection<BsonDocument> srcColl = TxNonceCollection(sourceChainId);
LiteCollection<BsonDocument> destColl = TxNonceCollection(destinationChainId);
destColl.InsertBulk(srcColl.FindAll());
}

Expand All @@ -610,7 +630,7 @@ public override void PruneOutdatedChains(bool noopWithoutCanon = false)
/// <inheritdoc />
public override BlockCommit? GetChainBlockCommit(Guid chainId)
{
ILiteCollection<BsonDocument> collection = CommitCollection(chainId);
LiteCollection<BsonDocument> collection = CommitCollection(chainId);
var docId = new BsonValue("c");
BsonDocument doc = collection.FindById(docId);
return doc is { } d && d.TryGetValue("v", out BsonValue v)
Expand All @@ -621,7 +641,7 @@ public override void PruneOutdatedChains(bool noopWithoutCanon = false)
/// <inheritdoc />
public override void PutChainBlockCommit(Guid chainId, BlockCommit blockCommit)
{
ILiteCollection<BsonDocument> collection = CommitCollection(chainId);
LiteCollection<BsonDocument> collection = CommitCollection(chainId);
var docId = new BsonValue("c");
BsonDocument doc = collection.FindById(docId);
collection.Upsert(
Expand Down Expand Up @@ -720,14 +740,20 @@ internal static string FormatChainId(Guid chainId) =>
private static (IStore Store, IStateStore StateStore) Loader(Uri storeUri)
{
NameValueCollection query = HttpUtility.ParseQueryString(storeUri.Query);
bool journal = query.GetBoolean("journal", true);
int indexCacheSize = query.GetInt32("index-cache", 50000);
int blockCacheSize = query.GetInt32("block-cache", 512);
int txCacheSize = query.GetInt32("tx-cache", 1024);
bool flush = query.GetBoolean("flush", true);
bool readOnly = query.GetBoolean("readonly");
string statesKvPath = query.Get("states-dir") ?? StatesKvPathDefault;
var store = new DefaultStore(
storeUri.LocalPath,
journal,
indexCacheSize,
blockCacheSize,
txCacheSize,
flush,
readOnly);
var stateStore = new TrieStateStore(
new DefaultKeyValueStore(Path.Combine(storeUri.LocalPath, statesKvPath)));
Expand Down Expand Up @@ -817,13 +843,13 @@ private UPath TxExecutionPath(TxExecution txExecution) =>
private UPath TxIdBlockHashIndexPath(in TxId txid, in BlockHash blockHash) =>
TxPath(txid) / blockHash.ToString();

private ILiteCollection<HashDoc> IndexCollection(in Guid chainId) =>
private LiteCollection<HashDoc> IndexCollection(in Guid chainId) =>
_db.GetCollection<HashDoc>($"{IndexColPrefix}{FormatChainId(chainId)}");

private ILiteCollection<BsonDocument> TxNonceCollection(Guid chainId) =>
private LiteCollection<BsonDocument> TxNonceCollection(Guid chainId) =>
_db.GetCollection<BsonDocument>($"{TxNonceIdPrefix}{FormatChainId(chainId)}");

private ILiteCollection<BsonDocument> CommitCollection(in Guid chainId) =>
private LiteCollection<BsonDocument> CommitCollection(in Guid chainId) =>
_db.GetCollection<BsonDocument>($"{CommitColPrefix}{FormatChainId(chainId)}");

private class HashDoc
Expand Down
2 changes: 1 addition & 1 deletion Libplanet.Store/Libplanet.Store.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<PackageReference Include="Bencodex" Version="0.16.0" />
<PackageReference Include="Caching.dll" Version="1.4.0.1" />
<PackageReference Include="ImmutableTrie" Version="1.0.0-alpha" />
<PackageReference Include="LiteDB" Version="5.0.15" />
<PackageReference Include="LiteDB" Version="4.1.4" />
<PackageReference Include="Planetarium.LruCacheNet" Version="1.2.0" />
<PackageReference Include="Serilog" Version="2.8.0" />
<PackageReference Include="Zio" Version="0.7.4" />
Expand Down

0 comments on commit 90e7057

Please sign in to comment.