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

feat: introduce txid->blockhash index on IStore #1328

Merged
merged 10 commits into from
Jun 14, 2021

Conversation

kfangw
Copy link
Contributor

@kfangw kfangw commented Jun 3, 2021

closes #1294

APIs

Following APIs are added to IStore

void PutTxIdBlockHashIndex(TxId txId, BlockHash blockHash);
BlockHash? GetFirstTxIdBlockHashIndex(TxId txId);
IEnumerable<BlockHash> IterateTxIdBlockHashIndex(TxId txId);
void DeleteTxIdBlockHashIndex(TxId txId, BlockHash blockHash);

Contract

  • There exists only one record for a pair <TxId, BlockHash>.
  • There are multiple BlockHash paired with the same TxId (consider reorg situation.)

@kfangw kfangw self-assigned this Jun 3, 2021
@kfangw kfangw changed the title [wip] feat: introduce txid->blockhash index on IStore feat: introduce txid->blockhash index on IStore Jun 3, 2021
@codecov
Copy link

codecov bot commented Jun 3, 2021

Codecov Report

Merging #1328 (0783fe8) into main (0033ed7) will decrease coverage by 9.10%.
The diff coverage is 53.04%.

❗ Current head 0783fe8 differs from pull request most recent head a15bf0d. Consider uploading reports for the commit a15bf0d to get more accurate results

@@            Coverage Diff             @@
##             main    #1328      +/-   ##
==========================================
- Coverage   86.03%   76.93%   -9.11%     
==========================================
  Files         361      253     -108     
  Lines       32457    16945   -15512     
==========================================
- Hits        27925    13036   -14889     
- Misses       2747     3380     +633     
+ Partials     1785      529    -1256     
Impacted Files Coverage Δ
...bplanet.Explorer/Controllers/ExplorerController.cs 0.00% <0.00%> (ø)
...rer/Controllers/GenericControllerNameConvention.cs 0.00% <0.00%> (ø)
Libplanet.Explorer/Controllers/GraphQLBody.cs 0.00% <0.00%> (ø)
Libplanet.Explorer/ExplorerStartup.cs 0.00% <0.00%> (ø)
Libplanet.Explorer/GraphTypes/NodeStateType.cs 0.00% <0.00%> (ø)
...ibplanet.Explorer/Interfaces/IBlockChainContext.cs 0.00% <0.00%> (ø)
Libplanet.Explorer/Queries/BlockQuery.cs 0.00% <0.00%> (ø)
Libplanet.Explorer/Queries/ExplorerQuery.cs 0.00% <0.00%> (ø)
Libplanet.Explorer/Queries/TransactionQuery.cs 0.00% <0.00%> (ø)
Libplanet.Explorer/Store/AddressRefDoc.cs 0.00% <0.00%> (ø)
... and 243 more

@kfangw
Copy link
Contributor Author

kfangw commented Jun 3, 2021

/rebase

@kfangw kfangw marked this pull request as ready for review June 3, 2021 08:42
Copy link
Member

@limebell limebell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update this branch to main and resolve conflicts. 🙇‍♂️

@limebell limebell self-requested a review June 4, 2021 01:54
limebell
limebell previously approved these changes Jun 4, 2021
@longfin
Copy link
Member

longfin commented Jun 5, 2021

/rebase

@kfangw kfangw marked this pull request as draft June 7, 2021 01:31
@kfangw kfangw force-pushed the feat/txbindex branch 2 times, most recently from 0807d29 to cc49741 Compare June 7, 2021 01:34
@kfangw kfangw marked this pull request as ready for review June 7, 2021 01:34
@auto-assign auto-assign bot requested review from dahlia and limebell June 7, 2021 01:34
dahlia
dahlia previously approved these changes Jun 7, 2021
public override void PutTxIdBlockHashIndex(Guid chainId, TxId txId, BlockHash blockHash)
{
var cf = GetColumnFamily(_chainDb, chainId);
_chainDb.Put(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we decide to store these indexes on chainDb, we also need to add a new method for forking because we can delete the previous chainDb after forking...

Or, how about using the separated DB for storing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking that It(forking) is handled by other codebases.
And I realized that the chain id may not be needed. So the forking also not needed.
please see the patch 5f43072

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if we don't put it with ColumnFamily, there is no matter. 😄

@kfangw
Copy link
Contributor Author

kfangw commented Jun 7, 2021

if we decide to store these indexes on chainDb, we also need to add a new method for forking because we can delete the previous chainDb after forking...

Or, how about using the separated DB for storing?

The chain id may not be needed for this change. Please see the patch 5f43072

public void PutTxIdBlockHashIndex(TxId txId, BlockHash blockHash);
public bool HasTxIdBlockHashIndex(TxId txId);
public BlockHash? GetTxIdBlockHashIndex(TxId txId);
public void DeleteTxIdBlockHashIndex(TxId txId);

IMHO, TxId->BlockHash index can be handled like TxExecution.
What do you think about this? @longfin @dahlia

@kfangw
Copy link
Contributor Author

kfangw commented Jun 8, 2021

The APIs were changed according to your advice @longfin

chain id is removed from the argument, and let the multiple pair <TxId, BlockHash> which share the same TxId.
Hence, IterateTxIdBlockHashIndex method is introduced to traverse the list of pairs by using the prefix(TxId).
And GetTxIdBlockHashIndex is changed to GetFirstTxIdBlockHashIndex method, which returns the first element or null if the list is empty.

void PutTxIdBlockHashIndex(TxId txId, BlockHash blockHash);
BlockHash? GetFirstTxIdBlockHashIndex(TxId txId);
IEnumerable<BlockHash> IterateTxIdBlockHashIndex(TxId txId);
void DeleteTxIdBlockHashIndex(TxId txId, BlockHash blockHash);

/// <inheritdoc cref="BaseStore.PutTxIdBlockHashIndex(TxId, BlockHash)"/>
public override void PutTxIdBlockHashIndex(TxId txId, BlockHash blockHash)
{
_txExecutionDb.Put(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it OK for you to share the same db with TxExecution? @dahlia
IMHO, txExecutionDB consists of transient and deterministic data. So they can share the same db. #1321

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kfangw
Copy link
Contributor Author

kfangw commented Jun 9, 2021

PTAL

@kfangw
Copy link
Contributor Author

kfangw commented Jun 14, 2021

PTAL. @longfin @dahlia @greymistcube @limebell @riemannulus
Can I merge it?

CI passed except for Codecov test coverage.
Codecov coverage degradation is caused by the exclusion of test projects.

@longfin
Copy link
Member

longfin commented Jun 14, 2021

Can you tell me when GetFirstTxIdBlockHashIndex() is useful? IMO, IterateTxIdBlockHashIndex() is sufficient in the general case.

longfin
longfin previously approved these changes Jun 14, 2021
Copy link
Member

@longfin longfin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything else looks fine.

limebell
limebell previously approved these changes Jun 14, 2021
@kfangw
Copy link
Contributor Author

kfangw commented Jun 14, 2021

Can you tell me when GetFirstTxIdBlockHashIndex() is useful? IMO, IterateTxIdBlockHashIndex() is sufficient in the general case.

The API responds to the caller that a transaction is included in at least one block or not. It has a similar use case like hasXXX API.
As you mentioned, the caller can discover the fact thru the iterator API.
However, IMHO, this API may useful who want to know the fact without iterating them.

@kfangw kfangw dismissed stale reviews from limebell and longfin via a15bf0d June 14, 2021 01:45
@kfangw
Copy link
Contributor Author

kfangw commented Jun 14, 2021

/rebase

@longfin longfin requested review from dahlia, limebell and longfin June 14, 2021 01:55
@kfangw kfangw merged commit eeaa686 into planetarium:main Jun 14, 2021
@kfangw kfangw deleted the feat/txbindex branch June 14, 2021 02:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Build TxIdBlockHash index in IStore
4 participants