Skip to content

Per default refetch the chain tip at most every 20 seconds #211

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

Merged
merged 5 commits into from
Apr 18, 2023

Conversation

nielstron
Copy link
Contributor

At the current state, every access to protocol params causes at least one chain tip fetch. Due to the potential delay of this request, often another protocol params fetch is triggered as well.

This significantly (and mostly unnecessarily) slows down transaction building. This fix caches the chain tip for a default of 20 seconds (the average slot time), making sure that subsequent calls to _is_chain_tip_updated return without any delay, drastically speeding up tx building.

@nielstron
Copy link
Contributor Author

nielstron commented Apr 16, 2023

Using the opshin-starter-kit as an example

Before

(opshin-starter-kit-py3.8) ➜  opshin-starter-kit git:(main) ✗ time python3 src/off_chain/make_vest.py owner beneficiary
transaction id: 4a26f76b7354d0d0f1c232dc187822701a1fe3faa565ffdc94a08ff9282eb51f
Cardanoscan: https://preview.cardanoscan.io/transaction/4a26f76b7354d0d0f1c232dc187822701a1fe3faa565ffdc94a08ff9282eb51f
python3 src/off_chain/make_vest.py owner beneficiary  0.60s user 0.08s system 1% cpu 43.842 total

After

(opshin-starter-kit-py3.8) ➜  opshin-starter-kit git:(main) ✗ time python3 src/off_chain/make_vest.py owner beneficiary
transaction id: 7042a915046c87c8cd1d7b953f09e3738fb3c93c585730f9546b7000fc32d018
Cardanoscan: https://preview.cardanoscan.io/transaction/7042a915046c87c8cd1d7b953f09e3738fb3c93c585730f9546b7000fc32d018
python3 src/off_chain/make_vest.py owner beneficiary  0.27s user 0.04s system 3% cpu 7.921 total

This is a speed up from 40s to 7s and the script only builds a simple transaction!

At the current state, every access to protocol params causes at least
one chain tip fetch. Due to the potential delay of this request, often
another protocol params fetch is triggered as well.

This significantly (and mostly unnecessarily) slows down transaction building.
This fix caches the chain tip for a default of 20 seconds (the average
slot time), making sure that subsequent calls to _is_chain_tip_updated
return without any delay, drastically speeding up tx building.
@nielstron nielstron force-pushed the feat/cache_chain_tip branch from 5804264 to 8b8269a Compare April 16, 2023 21:43
@codecov-commenter
Copy link

codecov-commenter commented Apr 16, 2023

Codecov Report

Merging #211 (8b8269a) into main (6ec8623) will decrease coverage by 0.07%.
The diff coverage is 50.00%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@            Coverage Diff             @@
##             main     #211      +/-   ##
==========================================
- Coverage   85.60%   85.53%   -0.07%     
==========================================
  Files          26       26              
  Lines        2882     2889       +7     
  Branches      693      694       +1     
==========================================
+ Hits         2467     2471       +4     
- Misses        305      308       +3     
  Partials      110      110              
Impacted Files Coverage Δ
pycardano/backend/ogmios.py 58.79% <50.00%> (-0.06%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Collaborator

@cffls cffls left a comment

Choose a reason for hiding this comment

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

Nice! I can image how much queries this optimization will reduce :D

@nielstron
Copy link
Contributor Author

An optimal design would have a few threads that asynchronousmy fetch the chain tip, the protocol params, the epoch and such and fetch it. Then access to those values is instant and they are almost never outdated.

@cffls
Copy link
Collaborator

cffls commented Apr 17, 2023

The test failed. I had a fix but can't push to your fork. Could you pull the fix into your fork?

@nielstron
Copy link
Contributor Author

Neat, multi-player PR

@cffls cffls merged commit 1bcf1f1 into Python-Cardano:main Apr 18, 2023
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.

3 participants