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

ModifyNewCoins saves database lookups #6932

Merged
merged 3 commits into from Nov 18, 2015
Merged

Conversation

@morcos
Copy link
Member

morcos commented Nov 3, 2015

When processing a new transaction, in addition to spending the Coins of its txin's, it creates a new Coins for its outputs. The existing ModifyCoins function will first make sure this Coins does not already exist. It can not exist due to BIP 30, but because of that, the lookup can't be cached and always has to go to the database. Since we are creating the Coins to match the new tx anyway, there is no point in checking if it exists first anyway. However this should not be used for coinbase tx's in order to preserve the historical behavior of overwriting the two existing duplicate tx pairs.

In conjunction with #6931 this will help ConnectBlock be much more efficient with caching access to the database.

This still needs unit tests which exercise the new functionality.

When processing a new transaction, in addition to spending the Coins of its txin's it creates a new Coins for its outputs.  The existing ModifyCoins function will first make sure this Coins does not already exist.  It can not exist due to BIP 30, but because of that the lookup can't be cached and always has to go to the database.  Since we are creating the coins to match the new tx anyway, there is no point in checking if they exist first anyway.  However this should not be used for coinbase tx's in order to preserve the historical behavior of overwriting the two existing duplicate tx pairs.
@gmaxwell
Copy link
Contributor

gmaxwell commented Nov 3, 2015

ConceptACK, will review further and test.

@sipa
Copy link
Member

sipa commented Nov 11, 2015

Untested, code review ACK. Needs a unit test, though.

@sipa
Copy link
Member

sipa commented Nov 11, 2015

Together with #5967 it should be possible to also avoid the db read that's still done for coinbase transactions.

@morcos
Copy link
Member Author

morcos commented Nov 12, 2015

@sipa I'm not sure if this was the kind of unit test that you had in mind? I thought it was important to actually test UpdateCoins instead of modifyNewCoins directly because it's how it is used that matters.

This test passes on master, passes on this PR, but fails when UpdateCoins is changed to just mark coinbases as un-FRESH but still skip the lookup (assuming the assert is commented out to permit this).

However if #5967 is merged then the test passes once again.

@sipa
Copy link
Member

sipa commented Nov 12, 2015

@morcos Awesome test.

@morcos morcos force-pushed the morcos:newCoinsThinAir branch to 1cf3dd8 Nov 12, 2015
@morcos
Copy link
Member Author

morcos commented Nov 12, 2015

ok fixed the mess with the random nValue.

@sipa
Copy link
Member

sipa commented Nov 12, 2015

ACK

@jgarzik
Copy link
Contributor

jgarzik commented Nov 12, 2015

lightly tested ACK

}

if (insecure_rand() % 100 == 0) {
// Every 100 iterations, change the cache stack.

This comment has been minimized.

Copy link
@laanwj

laanwj Nov 13, 2015

Member

If the purpose is to do this every 100 iterations, why use random?
Either the comment or the code is wrong :)

This comment has been minimized.

Copy link
@sipa

sipa Nov 13, 2015

Member

It's copied from the test above that I wrote. It's intended to be random, so the comment is wrong :)

// Every 100 iterations, change the cache stack.
if (stack.size() > 0 && insecure_rand() % 2 == 0) {
stack.back()->Flush();
delete stack.back();

This comment has been minimized.

Copy link
@laanwj

laanwj Nov 13, 2015

Member

If this was in production code I'd prefer to use a RAII pointer type (such as boost::shared_ptr) inside stack instead of explicit delete, because of exception safety and memory leaks.
As it is just the tests, mehh.

This comment has been minimized.

Copy link
@sipa

sipa via email Nov 13, 2015

Member

This comment has been minimized.

Copy link
@laanwj

laanwj Nov 13, 2015

Member

Right - I think shared_ptr is the only boost pointer you can use inside containers.

Edit: with c++11 unique_ptr it should be possible, though, I guess we can change it by then :)

@laanwj
Copy link
Member

laanwj commented Nov 13, 2015

Code review ACK

@laanwj laanwj merged commit 1cf3dd8 into bitcoin:master Nov 18, 2015
1 check passed
1 check passed
continuous-integration/travis-ci/pr The Travis CI build passed
Details
laanwj added a commit that referenced this pull request Nov 18, 2015
1cf3dd8 Add unit test for UpdateCoins (Alex Morcos)
03c8282 Make CCoinsViewTest behave like CCoinsViewDB (Alex Morcos)
14470f9 ModifyNewCoins saves database lookups (Alex Morcos)
zkbot added a commit to zcash/zcash that referenced this pull request May 14, 2018
Bitcoin 0.12 performance improvements

Cherry-picked from the following upstream PRs:

- bitcoin/bitcoin#6918
- bitcoin/bitcoin#6932

Part of #2074.
zkbot added a commit to zcash/zcash that referenced this pull request May 14, 2018
Bitcoin 0.12 performance improvements

Cherry-picked from the following upstream PRs:

- bitcoin/bitcoin#6918
- bitcoin/bitcoin#6932

Part of #2074.
zkbot added a commit to zcash/zcash that referenced this pull request May 31, 2018
Bitcoin 0.12 performance improvements

Cherry-picked from the following upstream PRs:

- bitcoin/bitcoin#6918
- bitcoin/bitcoin#6932

Part of #2074.
zkbot added a commit to zcash/zcash that referenced this pull request Nov 30, 2018
Bitcoin 0.12 performance improvements

Cherry-picked from the following upstream PRs:

- bitcoin/bitcoin#6918
- bitcoin/bitcoin#6932

Part of #2074.
milesmanley added a commit to zelcash/zelcash that referenced this pull request Mar 5, 2019
Bitcoin 0.12 performance improvements

Cherry-picked from the following upstream PRs:

- bitcoin/bitcoin#6918
- bitcoin/bitcoin#6932

Part of #2074.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

5 participants
You can’t perform that action at this time.