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

CoinsRegistry: Refactor DescendantOf #11697

Merged

Conversation

kiminuo
Copy link
Collaborator

@kiminuo kiminuo commented Oct 16, 2023

No description provided.

@kiminuo kiminuo force-pushed the feature/2023-10-14-Simplify-ICoinsView branch from fa0c139 to 4feada5 Compare October 16, 2023 06:53
@@ -239,7 +240,7 @@ public void SwitchToUnconfirmFromBlock(Height blockHeight)
{
foreach (var coin in AsCoinsViewNoLock().AtBlockHeight(blockHeight))
{
var descendantCoins = DescendantOfAndSelf(coin);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

NoLock variant should be used here.

@@ -11,16 +11,10 @@ public interface ICoinsView : IEnumerable<SmartCoin>

ICoinsView Available();

ICoinsView ChildrenOf(SmartCoin coin);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Only needed for computing of descendants.

? EmptyCoinsView
: new CoinsView(Coins.Where(x => x.TransactionId == coin.SpenderTransaction.GetHash()));

public ICoinsView DescendantOf(SmartCoin coin)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Moved to CoinsRegistry

@kiminuo kiminuo force-pushed the feature/2023-10-14-Simplify-ICoinsView branch from 7c9741b to f0c9669 Compare October 16, 2023 10:47
@kiminuo kiminuo marked this pull request as ready for review October 16, 2023 13:30
@kiminuo kiminuo requested a review from turbolay October 16, 2023 13:30
Copy link
Collaborator

@turbolay turbolay left a comment

Choose a reason for hiding this comment

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

What do you think of having DescendantOf instead of DescendantOfAndSelf ? Or a boolean parameter. I know we currently don't use it, but I believe the API would be cleaner.

Otherwise the PR is great

private ImmutableArray<SmartCoin> DescendantOfAndSelfNoLock(SmartCoin coin)
{
HashSet<SmartCoin> allCoins = new(Coins);
allCoins.UnionWith(SpentCoins);
Copy link
Collaborator

Choose a reason for hiding this comment

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

AsAllCoinsViewNoLock() should be used here, I believe.

Copy link
Collaborator Author

@kiminuo kiminuo Oct 16, 2023

Choose a reason for hiding this comment

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

I'm hesitant here. You are right in the sense that we do that everywhere but to me it makes little sense really.

I think it would be better to have a set of all coins so that we can just iterate over it and that's it. It does not add anything to the memory because we would just store pointers.

Otherwise, it seems like we create the set of all coins again and again.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree that it makes little sense; we can introduce it here and change it in other places? I believe that in classes like this one, consistency is really important so it's easier to understand.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I agree that it makes little sense; we can introduce it here and change it in other places? I believe that in classes like this one, consistency is really important so it's easier to understand.

It would be great to fix it consistently everywhere or as much as possible. Yes.

IEnumerable<SmartCoin> Generator(SmartCoin parentCoin, bool addSelf)
{
IEnumerable<SmartCoin> childrenOf = parentCoin.SpenderTransaction is not null
? allCoins.Where(x => x.TransactionId == parentCoin.SpenderTransaction.GetHash()) // Inefficient.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This would be much cleaner and more efficient with #11516 's factoring. Anyway.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Could we just add the dictionary you had in that PR? As a small atomic PR?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we just add the dictionary you had in that PR? As a small atomic PR?

I will look into it, the smallest PR possible with the dictionary. This structure makes a lot of sense because the values are by tx anyway.

@kiminuo
Copy link
Collaborator Author

kiminuo commented Oct 16, 2023

What do you think of having DescendantOf instead of DescendantOfAndSelf ? Or a boolean parameter. I know we currently don't use it, but I believe the API would be cleaner.

I think it's a good idea, I'll try to address that.

@kiminuo kiminuo force-pushed the feature/2023-10-14-Simplify-ICoinsView branch from 3ac3519 to 9433aaf Compare October 17, 2023 06:28
@kiminuo kiminuo changed the title CoinsRegistry: Refactor DescendantOfAndSelfNoLock CoinsRegistry: Refactor DescendantOf Oct 17, 2023
adamPetho
adamPetho previously approved these changes Oct 17, 2023
Copy link
Collaborator

@adamPetho adamPetho left a comment

Choose a reason for hiding this comment

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

tACK, code LGTM

Copy link
Collaborator

@turbolay turbolay left a comment

Choose a reason for hiding this comment

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

Nice PR; utACK

Just one note: We are changing the interface, maybe some third party code is using it? I don't believe so but I was surprised in the past so...

@adamPetho adamPetho added the affiliate Could affect affiliates compatibility label Oct 17, 2023
@kiminuo
Copy link
Collaborator Author

kiminuo commented Oct 17, 2023

Just one note: We are changing the interface, maybe some third party code is using it? I don't believe so but I was surprised in the past so...

It's imho a similar case to #11696 (review). I'm working on testing that for that 3rd party project. And other than that, we need to try.

@kiminuo kiminuo merged commit 7b8df9d into WalletWasabi:master Oct 17, 2023
7 checks passed
@kiminuo kiminuo deleted the feature/2023-10-14-Simplify-ICoinsView branch October 17, 2023 17:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affiliate Could affect affiliates compatibility size/M
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants