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

fix(pyth-solana-receiver): improve perf and security #2222

Merged

Conversation

tejasbadadare
Copy link
Contributor

Purpose

Performance and security improvements to the SVM TWAP feature.

Implementation details

  • Perf optimization: Reused slot_diff calculation in calculate_twap
  • Security optimization: Previously, the SDK's get_twap_no_older_than function only checked that the twap's end_time was more recent than maximum_age. However, the window size of the twap wasn't checked. This could result in a scenario where an attacker could frontrun a transaction that consumes a hardcoded TwapUpdate PDA. The attacker could overwrite the account with a verified twap of an unexpected window (very long/short). This could have the effect of manipulating, for example, the LTV ratio calculation of a lending protocol.
    • The check has a +/- 1 second tolerance to account for Solana block time variations.

Testing

Updated SDK test suite

Copy link

vercel bot commented Jan 3, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
api-reference ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 3, 2025 6:48pm
proposals ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 3, 2025 6:48pm
staking ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 3, 2025 6:48pm
2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
component-library ⬜️ Ignored (Inspect) Visit Preview Jan 3, 2025 6:48pm
insights ⬜️ Ignored (Inspect) Visit Preview Jan 3, 2025 6:48pm

@tejasbadadare tejasbadadare changed the title Tb/solana receiver program/optimize perf and security pyth-solana-receiver: optimize perf and security Jan 3, 2025
@tejasbadadare tejasbadadare changed the title pyth-solana-receiver: optimize perf and security fix(pyth-solana-receiver): optimize perf and security Jan 3, 2025
@tejasbadadare tejasbadadare changed the title fix(pyth-solana-receiver): optimize perf and security fix(pyth-solana-receiver): improve perf and security Jan 3, 2025

// Ensure the twap window size is as expected
// Allow for +/- 1 second tolerance to account for the imprecision introduced by Solana block times
const TOLERANCE: i64 = 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't get why you need TOLERANCE here. There should be at least one VAA per second with 400 milliseconds blocktimes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My understanding was that there's a VAA roughly every 400ms. That means that if I request a 1 second TWAP (t0=1000 and t1=2000), the start VAA might have timestamp 800 and the end VAA might have timestamp 2000. That would result in a 1200ms window. If i had a strict check for window size, it would fail since 1200 != 1000. This is why i added the tolerance... but now i'm noticing that the start_time and end_time precision is in seconds and not milliseconds, so this shouldn't matter! Will go ahead and remove the tolerance. Does this check out?

Copy link
Contributor

@guibescos guibescos left a comment

Choose a reason for hiding this comment

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

I'd remove the +- tolerance, let me know if you disagree

Copy link
Collaborator

@ali-bahjati ali-bahjati left a comment

Choose a reason for hiding this comment

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

Nice!

Copy link
Contributor

@guibescos guibescos left a comment

Choose a reason for hiding this comment

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

tyvm

@tejasbadadare tejasbadadare merged commit c716a93 into main Jan 7, 2025
8 checks passed
@tejasbadadare tejasbadadare deleted the tb/solana_receiver_program/optimize_perf_and_security branch January 7, 2025 17:39
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