Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Removed updated addresses from IRichStore #3562

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ To be released.
now one can choose its representation format. [[#3560]]
- (Libplanet.Explorer) Added `HelperQuery`, a collection of utility like
queries. [[#3561]]
- (Libplanet.Explorer) Removed `IRichStore.StoreUpdatedAddressReferences()`
and `IterateUpdatedAddressReferences()` interface methods. [[#3562]]
- (Libplanet.Explorer) Removed `involvedAddress` argument from all
`TransactionQuery` query methods. [[#3562]]

[#3559]: https://github.com/planetarium/libplanet/pull/3559
[#3560]: https://github.com/planetarium/libplanet/pull/3560
[#3561]: https://github.com/planetarium/libplanet/pull/3561
[#3562]: https://github.com/planetarium/libplanet/pull/3562


Version 3.9.2
Expand Down
37 changes: 9 additions & 28 deletions Libplanet.Explorer.Tests/Queries/TransactionQueryGeneratedTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,100 +112,88 @@ private async Task AssertTransactionsQueryPermutation(
Address? involvedAddress)
{
await AssertAgainstTransactionsQuery(
blocksToTest, signer, involvedAddress, false, null, null);
blocksToTest, signer, false, null, null);
await AssertAgainstTransactionsQuery(
blocksToTest, signer, involvedAddress, true, null, null);
blocksToTest, signer, true, null, null);
await AssertAgainstTransactionsQuery(
blocksToTest, signer, involvedAddress, false, blocksToTest.Length / 4, null);
blocksToTest, signer, false, blocksToTest.Length / 4, null);
await AssertAgainstTransactionsQuery(
blocksToTest,
signer,
involvedAddress,
false,
blocksToTest.Length / 4 - blocksToTest.Length,
null);
Assert.Equal<IEnumerable<(string Id, string? BlockHash)>>(
await ExecuteTransactionsQueryAsync(
signer, involvedAddress, false, blocksToTest.Length / 4, null),
signer, false, blocksToTest.Length / 4, null),
await ExecuteTransactionsQueryAsync(
signer,
involvedAddress,
false,
blocksToTest.Length / 4 - blocksToTest.Length,
null));
await AssertAgainstTransactionsQuery(
blocksToTest, signer, involvedAddress, true, blocksToTest.Length / 4, null);
blocksToTest, signer, true, blocksToTest.Length / 4, null);
await AssertAgainstTransactionsQuery(
blocksToTest,
signer,
involvedAddress,
true,
blocksToTest.Length / 4 - blocksToTest.Length,
null);
Assert.Equal<IEnumerable<(string Id, string? BlockHash)>>(
await ExecuteTransactionsQueryAsync(
signer, involvedAddress, true, blocksToTest.Length / 4, null),
signer, true, blocksToTest.Length / 4, null),
await ExecuteTransactionsQueryAsync(
signer,
involvedAddress,
true,
blocksToTest.Length / 4 - blocksToTest.Length,
null));
await AssertAgainstTransactionsQuery(
blocksToTest, signer, involvedAddress, false, null, blocksToTest.Length / 4);
blocksToTest, signer, false, null, blocksToTest.Length / 4);
await AssertAgainstTransactionsQuery(
blocksToTest, signer, involvedAddress, true, null, blocksToTest.Length / 4);
blocksToTest, signer, true, null, blocksToTest.Length / 4);
await AssertAgainstTransactionsQuery(
blocksToTest,
signer,
involvedAddress,
false,
blocksToTest.Length / 3,
blocksToTest.Length / 4);
await AssertAgainstTransactionsQuery(
blocksToTest,
signer,
involvedAddress,
false,
blocksToTest.Length / 3 - blocksToTest.Length,
blocksToTest.Length / 4);
Assert.Equal<IEnumerable<(string Id, string? BlockHash)>>(
await ExecuteTransactionsQueryAsync(
signer,
involvedAddress,
false,
blocksToTest.Length / 3,
blocksToTest.Length / 4),
await ExecuteTransactionsQueryAsync(
signer,
involvedAddress,
false,
blocksToTest.Length / 3 - blocksToTest.Length,
blocksToTest.Length / 4));
await AssertAgainstTransactionsQuery(
blocksToTest,
signer,
involvedAddress,
true,
blocksToTest.Length / 3,
blocksToTest.Length / 4);
await AssertAgainstTransactionsQuery(
blocksToTest,
signer,
involvedAddress,
true,
blocksToTest.Length / 3 - blocksToTest.Length,
blocksToTest.Length / 4);
Assert.Equal<IEnumerable<(string Id, string? BlockHash)>>(
await ExecuteTransactionsQueryAsync(
signer,
involvedAddress,
true,
blocksToTest.Length / 3,
blocksToTest.Length / 4),
await ExecuteTransactionsQueryAsync(
signer,
involvedAddress,
true,
blocksToTest.Length / 3 - blocksToTest.Length,
blocksToTest.Length / 4));
Expand All @@ -214,7 +202,6 @@ await ExecuteTransactionsQueryAsync(
private async Task AssertAgainstTransactionsQuery(
IReadOnlyList<Block> blocksToTest,
Address? signer,
Address? involvedAddress,
bool desc,
int? offset,
int? limit)
Expand All @@ -237,10 +224,6 @@ private async Task AssertAgainstTransactionsQuery(
{
txs = txs.Where(tx => tx.Signer.Equals(signerVal));
}
else if (involvedAddress is { } involvedAddressVal)
{
txs = txs.Where(tx => tx.UpdatedAddresses.Contains(involvedAddressVal));
}

if (limit is { } limitVal)
{
Expand All @@ -249,7 +232,7 @@ private async Task AssertAgainstTransactionsQuery(

var expected = txs.ToImmutableArray();
var actual =
await ExecuteTransactionsQueryAsync(signer, involvedAddress, desc, offset, limit);
await ExecuteTransactionsQueryAsync(signer, desc, offset, limit);

foreach (var i in Enumerable.Range(0, actual.Length))
{
Expand All @@ -269,7 +252,6 @@ await Source.Index.GetContainedBlockHashByTxIdAsync(expected[i].Id)
private async Task<ImmutableArray<(string Id, string? BlockHash)>>
ExecuteTransactionsQueryAsync(
Address? signer,
Address? involvedAddress,
bool desc,
int? offset,
int? limit)
Expand All @@ -278,7 +260,6 @@ await Source.Index.GetContainedBlockHashByTxIdAsync(expected[i].Id)
{{
transactions(
{(signer is { } signerVal ? @$"signer: ""{signerVal}""" : "")}
{(involvedAddress is { } invVal ? @$"involvedAddress: ""{invVal}""" : "")}
desc: {(desc ? "true" : "false")}
offset: {offset?.ToString(CultureInfo.InvariantCulture) ?? "0"}
{(limit is { } limitVal ? $"limit: {limitVal}" : "")}
Expand Down
22 changes: 5 additions & 17 deletions Libplanet.Explorer/Queries/ExplorerQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ internal static IEnumerable<Block> ListBlocks(
}

internal static IEnumerable<Transaction> ListTransactions(
Address? signer, Address? involved, bool desc, long offset, int? limit)
Address? signer, bool desc, long offset, int? limit)
{
Block tip = Chain.Tip;
long tipIndex = tip.Index;
Expand All @@ -121,12 +121,6 @@ internal static IEnumerable<Transaction> ListTransactions(
.IterateSignerReferences(
(Address)signer, desc, (int)offset, limit ?? int.MaxValue);
}
else if (!(involved is null))
{
txIds = richStore
.IterateUpdatedAddressReferences(
(Address)involved, desc, (int)offset, limit ?? int.MaxValue);
}
else
{
txIds = richStore
Expand All @@ -149,7 +143,7 @@ internal static IEnumerable<Transaction> ListTransactions(
{
foreach (var tx in desc ? block.Transactions.Reverse() : block.Transactions)
{
if (IsValidTransaction(tx, signer, involved))
if (IsValidTransaction(tx, signer))
{
yield return tx;
limit--;
Expand All @@ -165,7 +159,7 @@ internal static IEnumerable<Transaction> ListTransactions(
}

internal static IEnumerable<Transaction> ListStagedTransactions(
Address? signer, Address? involved, bool desc, int offset, int? limit)
Address? signer, bool desc, int offset, int? limit)
{
if (offset < 0)
{
Expand All @@ -175,7 +169,7 @@ internal static IEnumerable<Transaction> ListStagedTransactions(
}

var stagedTxs = Chain.StagePolicy.Iterate(Chain)
.Where(tx => IsValidTransaction(tx, signer, involved))
.Where(tx => IsValidTransaction(tx, signer))
.Skip(offset);

stagedTxs = desc ? stagedTxs.OrderByDescending(tx => tx.Timestamp)
Expand Down Expand Up @@ -208,19 +202,13 @@ private static Block GetNextBlock(Block block, bool desc)

private static bool IsValidTransaction(
Transaction tx,
Address? signer,
Address? involved)
Address? signer)
{
if (signer is { } signerVal)
{
return tx.Signer.Equals(signerVal);
}

if (involved is { } involvedVal)
{
return tx.UpdatedAddresses.Contains(involvedVal);
}

return true;
}
}
Expand Down
15 changes: 1 addition & 14 deletions Libplanet.Explorer/Queries/TransactionQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ public TransactionQuery(IBlockChainContext context)
Name = "signer",
DefaultValue = null,
},
new QueryArgument<AddressType>
{
Name = "involvedAddress",
DefaultValue = null,
},
new QueryArgument<BooleanGraphType>
{
Name = "desc",
Expand All @@ -56,12 +51,11 @@ public TransactionQuery(IBlockChainContext context)
resolve: context =>
{
var signer = context.GetArgument<Address?>("signer");
var involved = context.GetArgument<Address?>("involvedAddress");
bool desc = context.GetArgument<bool>("desc");
long offset = context.GetArgument<long>("offset");
int? limit = context.GetArgument<int?>("limit");

return ExplorerQuery.ListTransactions(signer, involved, desc, offset, limit);
return ExplorerQuery.ListTransactions(signer, desc, offset, limit);
}
);

Expand All @@ -73,11 +67,6 @@ public TransactionQuery(IBlockChainContext context)
Name = "signer",
DefaultValue = null,
},
new QueryArgument<AddressType>
{
Name = "involvedAddress",
DefaultValue = null,
},
new QueryArgument<BooleanGraphType>
{
Name = "desc",
Expand All @@ -93,14 +82,12 @@ public TransactionQuery(IBlockChainContext context)
resolve: context =>
{
var signer = context.GetArgument<Address?>("signer");
var involved = context.GetArgument<Address?>("involvedAddress");
bool desc = context.GetArgument<bool>("desc");
int offset = context.GetArgument<int>("offset");
int? limit = context.GetArgument<int?>("limit", null);

return ExplorerQuery.ListStagedTransactions(
signer,
involved,
desc,
offset,
limit
Expand Down
11 changes: 0 additions & 11 deletions Libplanet.Explorer/Store/IRichStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,5 @@ IEnumerable<TxId> IterateSignerReferences(
bool desc,
int offset = 0,
int limit = int.MaxValue);

void StoreUpdatedAddressReferences(
TxId txId,
long txNonce,
Address updatedAddress);

IEnumerable<TxId> IterateUpdatedAddressReferences(
Address updatedAddress,
bool desc,
int offset = 0,
int limit = int.MaxValue);
}
}
38 changes: 0 additions & 38 deletions Libplanet.Explorer/Store/LiteDBRichStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class LiteDBRichStore : IRichStore
{
private const string TxRefCollectionName = "block_ref";
private const string SignerRefCollectionName = "signer_ref";
private const string UpdatedAddressRefCollectionName = "updated_address_ref";

private readonly MemoryStream _memoryStream;
private readonly LiteDatabase _db;
Expand Down Expand Up @@ -298,10 +297,6 @@ public void PutTransaction(Transaction tx)
{
_store.PutTransaction(tx);
StoreSignerReferences(tx.Id, tx.Nonce, tx.Signer);
foreach (var updatedAddress in tx.UpdatedAddresses)
{
StoreUpdatedAddressReferences(tx.Id, tx.Nonce, updatedAddress);
}
}

public void StoreTxReferences(TxId txId, in BlockHash blockHash, long blockIndex)
Expand Down Expand Up @@ -364,36 +359,6 @@ public IEnumerable<TxId> IterateSignerReferences(
return collection.Find(query, offset, limit).Select(doc => doc.TxId);
}

public void StoreUpdatedAddressReferences(
TxId txId,
long txNonce,
Address updatedAddress)
{
var collection = UpdatedAddressRefCollection();
collection.Upsert(new AddressRefDoc
{
Address = updatedAddress, TxNonce = txNonce, TxId = txId,
});
collection.EnsureIndex(nameof(AddressRefDoc.AddressString));
collection.EnsureIndex(nameof(AddressRefDoc.TxNonce));
}

public IEnumerable<TxId> IterateUpdatedAddressReferences(
Address updatedAddress,
bool desc,
int offset = 0,
int limit = int.MaxValue)
{
var collection = UpdatedAddressRefCollection();
var order = desc ? Query.Descending : Query.Ascending;
var addressString = updatedAddress.ToHex().ToLowerInvariant();
var query = Query.And(
Query.All(nameof(AddressRefDoc.TxNonce), order),
Query.EQ(nameof(AddressRefDoc.AddressString), addressString)
);
return collection.Find(query, offset, limit).Select(doc => doc.TxId);
}

public void Dispose()
{
if (!_disposed)
Expand All @@ -410,8 +375,5 @@ private LiteCollection<TxRefDoc> TxRefCollection() =>

private LiteCollection<AddressRefDoc> SignerRefCollection() =>
_db.GetCollection<AddressRefDoc>(SignerRefCollectionName);

private LiteCollection<AddressRefDoc> UpdatedAddressRefCollection() =>
_db.GetCollection<AddressRefDoc>(UpdatedAddressRefCollectionName);
}
}
Loading
Loading