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

Trusted state completer #946

Merged
merged 12 commits into from
Aug 13, 2020
Merged

Conversation

dahlia
Copy link
Contributor

@dahlia dahlia commented Aug 6, 2020

This patch is continued from the previous patch #934 and addresses the idea sketched in the issue #929.

However, in general I became to believe this approach is not appropriate. Here I elaborate:

  • Rendering and unrendering whole actions between two tips on reorg anyway requires all states of every step, and it means the idea of cherry-picking only recent states is pointless for this case.

  • Even if we decide to fetch (trust) all states from peers for every step between two tips on reorg, it rather takes longer time than recalculating all states.

  • We are slowly moving towards chain-interior-states (from chain-exterior-states, the status quo): Inserting a state hash into the BlockHeader #931, PreEvaluationHash on Block<T> and BlockHeader #935, and Introduce Merkle Patricia Trie #939. Eventually, we won't need any trust for reusing states calculated by peers, after all.

Hence, although I prepared this patch for a quite long time, I doubt this works well for the problem addressed by the issue #929. 🤔

@dahlia dahlia added network Related to networking (Libplanet.Net) storage Related to storage (Libplanet.Store) labels Aug 6, 2020
@dahlia dahlia self-assigned this Aug 6, 2020
@codecov
Copy link

codecov bot commented Aug 6, 2020

Codecov Report

Merging #946 into main will increase coverage by 0.14%.
The diff coverage is 88.85%.

@@            Coverage Diff             @@
##             main     #946      +/-   ##
==========================================
+ Coverage   87.98%   88.13%   +0.14%     
==========================================
  Files         269      276       +7     
  Lines       24872    25270     +398     
==========================================
+ Hits        21883    22271     +388     
- Misses       1531     1540       +9     
- Partials     1458     1459       +1     
Impacted Files Coverage Δ
Libplanet.Tests/TestUtils.cs 88.39% <0.00%> (-0.06%) ⬇️
...lanet/Blockchain/IncompleteBlockStatesException.cs 37.50% <0.00%> (ø)
Libplanet/Blocks/Block.cs 85.61% <ø> (ø)
Libplanet/Net/Swarm.TrustedStateCompleter.cs 66.25% <66.25%> (ø)
Libplanet/Net/Swarm.MessageHandlers.cs 85.71% <85.71%> (ø)
Libplanet/Blockchain/BlockChain.cs 90.98% <88.23%> (+0.82%) ⬆️
Libplanet/Net/Swarm.cs 86.26% <94.44%> (+0.20%) ⬆️
Libplanet.Tests/Blockchain/BlockChainTest.cs 98.36% <97.61%> (-0.03%) ⬇️
Libplanet.Tests/Net/Messages/BlockStatesTest.cs 100.00% <100.00%> (ø)
Libplanet.Tests/Net/SwarmTest.Fixtures.cs 100.00% <100.00%> (ø)
... and 21 more

@dahlia dahlia force-pushed the trusted-state-completer branch 2 times, most recently from 5186eac to 6354245 Compare August 8, 2020 07:05
@riemannulus riemannulus changed the base branch from master to main August 10, 2020 04:25
@dahlia dahlia changed the title WIP: Trusted state completer Trusted state completer Aug 10, 2020
@dahlia dahlia marked this pull request as ready for review August 10, 2020 07:39
@dahlia dahlia force-pushed the trusted-state-completer branch 2 times, most recently from 772b1f6 to ed9b1f8 Compare August 11, 2020 15:17
@dahlia
Copy link
Contributor Author

dahlia commented Aug 12, 2020

@planetarium/libplanet Please review this.

continue;
}

BlockChain.Store.SetBlockStates(blockStates.BlockHash, blockStates.States);
Copy link
Member

Choose a reason for hiding this comment

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

Is it fine without any of the mutex?

Copy link
Contributor Author

@dahlia dahlia Aug 12, 2020

Choose a reason for hiding this comment

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

Rather it does not work (gets dead-locked) if we try to lock the BlockChain._rwlock here, because BlockChain<T>.GetState()/GetBalance() methods guarantee their stateCompleter to be called only in the read lock:

_rwlock.EnterUpgradeableReadLock();
try
{
stateReference = Store.LookupStateReference(Id, key, block);
if (stateReference is null)
{
return null;
}
HashDigest<SHA256> hashValue = stateReference.Item1;
IImmutableDictionary<string, IValue> blockStates = Store.GetBlockStates(hashValue);
if (blockStates is null)
{
return rawStateCompleter(this, hashValue);
}
return blockStates.TryGetValue(key, out IValue state) ? state : null;
}
finally
{
_rwlock.ExitUpgradeableReadLock();
}

Copy link
Member

Choose a reason for hiding this comment

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

I see... then it seems that we should upgrade _rwlock before calling rawStateCompleter in BlockChain<T>.GetRawState().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not all state completers update the blockchain, but only Recalculate completers do. Those implementations have already entered the write lock by themselves.

_rwlock.EnterWriteLock();
try
{
SetStates(block, evaluations, buildStateReferences: false);
}
finally
{
_rwlock.ExitWriteLock();
}

Copy link
Member

@longfin longfin Aug 12, 2020

Choose a reason for hiding this comment

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

Then, shouldn't these state completers upgrade _rwlocks? (like Recalculate)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@longfin I'm going to address this problem in the next separated pull request!

{
StateCompleter = (blockChain, blockHash, address) =>
{
FillTrustedBlockStates(blockHash).Wait(cancellationToken);
Copy link
Member

@longfin longfin Aug 12, 2020

Choose a reason for hiding this comment

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

It can be a trouble if StateCompleter is called in an async method... but it doesn't seem translatable to async because StateCompleter and related features are all synchronous. 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's what I'd grappled with, but I gave up and decided to request for comments about this problem…

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@longfin I'm going to address this problem in the next separated pull request!

Libplanet/Net/Swarm.TrustedStateCompleter.cs Outdated Show resolved Hide resolved
Libplanet/Net/Swarm.TrustedStateCompleter.cs Outdated Show resolved Hide resolved
Libplanet/Net/Swarm.TrustedStateCompleter.cs Show resolved Hide resolved
Libplanet/Blockchain/BlockChain.cs Outdated Show resolved Hide resolved
@riemannulus riemannulus reopened this Aug 12, 2020
@riemannulus riemannulus self-requested a review August 12, 2020 07:37
riemannulus
riemannulus previously approved these changes Aug 12, 2020
limebell
limebell previously approved these changes Aug 12, 2020
@dahlia dahlia dismissed stale reviews from limebell and riemannulus via f0e715a August 12, 2020 09:39
dahlia added a commit to dahlia/libplanet that referenced this pull request Aug 12, 2020
@dahlia
Copy link
Contributor Author

dahlia commented Aug 13, 2020

@planetarium/libplanet Please review this again, folks!

@longfin longfin merged commit 30bd5d7 into planetarium:main Aug 13, 2020
dahlia added a commit to dahlia/libplanet that referenced this pull request Aug 13, 2020
dahlia added a commit to dahlia/libplanet that referenced this pull request Aug 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
network Related to networking (Libplanet.Net) storage Related to storage (Libplanet.Store)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants