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

VIP: Add private Visibility #4400

Open
pcaversaccio opened this issue Dec 16, 2024 · 0 comments
Open

VIP: Add private Visibility #4400

pcaversaccio opened this issue Dec 16, 2024 · 0 comments
Labels
VIP: Discussion Used to denote VIPs and more complex issues that are waiting discussion in a meeting

Comments

@pcaversaccio
Copy link
Collaborator

pcaversaccio commented Dec 16, 2024

Simple Summary

Support private visibility for functions, state variables as well as constants and immutables to restrict access exclusively to the declaring contract.

To frontrun your arguments against it, I know it's not pythonic :)

Motivation

Please note that the perspective shared here reflects (mostly) my viewpoint as a library author.

Adding private visibility for functions and state variables reinforces the principle of encapsulation within a module contract. Ok cool, but what does this exactly mean? Well, it prevents importing contracts (e.g. a contract using and implementing snekmate's erc20 contract) from directly altering critical variables (e.g. balances or allowances), safeguarding against unintended or harmful changes by the main contract implementoooor. By exposing controlled access through e.g. getters or internal setters when modification is appropriate, the module can retain both security and flexibility in this way. All these features are important to library writers.

Something like that is currently possible:

# pragma version ~=0.4.0
from ethereum.ercs import IERC20
from ethereum.ercs import IERC20Detailed
from snekmate.auth import ownable
from snekmate.tokens import erc20


initializes: ownable
initializes: erc20[ownable := ownable]
exports: (
    erc20.owner,
    erc20.IERC20,
    erc20.IERC20Detailed,
    erc20.mint,
    erc20.set_minter,
)


@deploy
def __init__():
    ownable.__init__()
    erc20.__init__("Vyper", "VY", 18, "Vyper", "1")
    # Modifies the `totalSupply` without updating the balances
    # (invariant "sum of total balances = total supply" breaks).
    # With `private` visibility, I could rewrite snekmate `erc20` in
    # a way, that users can't directly modify this variable without
    # breaking the invariant.
    erc20.totalSupply += 1000

Specification

Add the private visibility to state variable, immutable, constant definitions as well as to function declarations:

_CONSTANT: private(constant(bytes1)) = 0xFF
_IMMUTABLE: private(immutable(bytes1))
_balanceOf: private(HashMap[address, uint256])

@private
def _count():
    count += 1

Backwards Compatibility

This VIP introduces a new visibility decorator that is unavailable in older versions, making it inherently backward-incompatible.

Dependencies

N/A.

References

N/A.

Copyright

Copyright and related rights waived via CC0.

@pcaversaccio pcaversaccio added the needs triage needs triage label Dec 16, 2024
@pcaversaccio pcaversaccio changed the title Add private Visibility VIP: Add private Visibility Dec 16, 2024
@charles-cooper charles-cooper added VIP: Discussion Used to denote VIPs and more complex issues that are waiting discussion in a meeting and removed needs triage needs triage labels Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
VIP: Discussion Used to denote VIPs and more complex issues that are waiting discussion in a meeting
Projects
None yet
Development

No branches or pull requests

2 participants