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

CoinsByPrevOuts to TxIdsByPrevOuts #11790

Merged
merged 15 commits into from
Nov 1, 2023

Conversation

turbolay
Copy link
Collaborator

Next 2 commits from #11740
c0ef1e9
5fe794a

It makes a lot of sense IMO because the values in this cache are already grouped by TxId

@@ -118,16 +118,17 @@ private bool TryAddNoLock(SmartCoin coin)
{
hashSet = new();
CoinsByTransactionId.Add(coin.TransactionId, hashSet);

// Each prevOut of the transaction contributes to the existence of coins.
// This only has to be added once per transaction.
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is the reasoning for that?

Copy link
Collaborator

@kiminuo kiminuo Oct 25, 2023

Choose a reason for hiding this comment

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

It's immediately clear what behavior TryAddNoLock should have for double spends.

My approach would be to throw an UnreachableException whenever CoinsRegistry is used incorrectly. So adding another coin that spends already spent transaction output would lead to an exception being thrown. The reason being that caller failed to call the CoinsRegistry.Undo method.

It follows that

TxIdsByPrevOuts[input.PrevOut] = coin.TransactionId;

is forgiving implementation, while

if (!TxIdsByPrevOuts.TryAdd(input.PrevOut, coin.TransactionId)) throw ...

would be unforgiving (imho better).

I don't have time to check it more in detail right now but documenting this would be really valuable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Here the reason is that we will call this loop once per output, but each output references the same object SmartTransaction, so its inputs will be the same at each iteration.

I agree that it would be better to throw here. The reason I'm not doing that is to be safe and to follow the legacy behaviour of this cache, as it was already working like that prior to our recent changes to the CoinsRegistry. I have to think a bit more about the implications of throwing here. Also, why throw and don't call Undo?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, why throw and don't call Undo?

It would throw only if there is a serious bug in code somewhere. So one should definitely call Undo. However, if the cache gets corrupted, we should throw (in general). We don't and bugs get swallowed. That's what I meant really.

Copy link
Collaborator Author

@turbolay turbolay Oct 28, 2023

Choose a reason for hiding this comment

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

2bc1885 I had to fix some tests that were creating txs with several times the same OutPoint. I think it was proof that throwing here is indeed a good idea.

Copy link
Collaborator

@kiminuo kiminuo Oct 31, 2023

Choose a reason for hiding this comment

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

Well-spotted! Good work.

# Conflicts:
#	WalletWasabi/Blockchain/TransactionOutputs/CoinsRegistry.cs
@turbolay
Copy link
Collaborator Author

Sorry for the other problems with case, merging accident I believe!

@kiminuo
Copy link
Collaborator

kiminuo commented Oct 31, 2023

@turbolay Please check by changes if you agree with them. If the tests are green (locally they are for me), then I'll approve and we can merge.

Copy link
Collaborator Author

@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.

ACK

@kiminuo kiminuo merged commit 13c752f into WalletWasabi:master Nov 1, 2023
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants