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

Adds sysvar_get lint #92

Merged
merged 2 commits into from
Mar 14, 2024
Merged

Adds sysvar_get lint #92

merged 2 commits into from
Mar 14, 2024

Conversation

S3v3ru5
Copy link
Contributor

@S3v3ru5 S3v3ru5 commented Mar 8, 2024

sysvar_lint README:

sysvar_get

What it does:

Lint warns uses of Sysvar::from_account_info and suggests to use Sysvar::get instead for
the sysvars implementing Sysvar::get function. The following sysvars implement Sysvar::get:

  • Clock
  • EpochRewards
  • EpochSchedule
  • Fees
  • LastRestartSlot
  • Rent

Why is this bad?

The Sysvar::from_account_info is less efficient than Sysvar::get because:

  • The from_account_info requires that Sysvar account is passed to the program wasting the limited space
    available to the transactions.
  • The from_account_info deserializes the Sysvar account data wasting the computation budget.

The Sysvar::from_account_info should be used if and only if the program interacts with an old program that
requires the sysvar account to be passed in CPI call. The program could avoid deserialization overhead by using
the passed Sysvar account in CPI (after verifying the ID) and using the Sysvar::get.

References:
solana_program/sysvar docs,
Anchor docs

Known problems:

None

Example:

    let clock_account = next_account_info(account_info_iter)?;
    let clock = clock::Clock::from_account_info(&clock_account)?;

Use instead:

    let clock = clock::Clock::get()?;

How the lint is implemented:

  • For every item
    • If item is a struct and has #[derive(Accounts)] macro
    • For each field in the struct
      • If field is of type Ty::Sysvar(T) and T is one of Clock, EpochRewards, EpochSchedule, Fees, LastRestartSlot, Rent
        • Then report the field and suggest to T::get().
  • For every function
    • If an expr in function calls T::x() where x is solana_program::Sysvar::from_account_info and
      T is one of sysvars that implements Sysvar::get() method.
      • report the call expr and suggest to use T::get().

libraries = [
{ path = "../../../../../solana-lints-dev", pattern = "lints/*" },
# { git = "https://github.com/crytic/solana-lints", pattern = "lints/*" },
]
Copy link
Member

Choose a reason for hiding this comment

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

Was this for testing? Maybe this can be removed?

# libraries = [
# { path = "../../../../../solana-lints-dev", pattern = "lints/*" },
# # { git = "https://github.com/crytic/solana-lints", pattern = "lints/*" },
# ]
Copy link
Member

Choose a reason for hiding this comment

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

Here also, maybe this can be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we should remove them

@S3v3ru5 S3v3ru5 merged commit de1a58b into arb-cpi-anchor Mar 14, 2024
2 of 3 checks passed
@S3v3ru5 S3v3ru5 deleted the new-sysvar-lint branch March 14, 2024 17:05
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.

2 participants