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

Flags component #53

Open
encody opened this issue Sep 19, 2022 · 2 comments
Open

Flags component #53

encody opened this issue Sep 19, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@encody
Copy link
Contributor

encody commented Sep 19, 2022

Support multiple, binary state flags.

Inspired by the functionality of the pause component in Aurora's NEAR plugins repo: https://github.com/aurora-is-near/near-plugins#pausable

Discussion Points

  • No function decorators if the decorator would be equivalent to a simple function call (see Owner::require_owner). We're trying to stay away from Solidity-style implementations in favor of Rust.
  • Flags should be implemented as an enum, similar to how roles are for Rbac.
  • It is probably worth investigating whether bit packing (e.g. using bitflags) is as gas-efficient as checking for storage key existence. If flags are not purely binary, then this point is moot (bit packing is impossible). For example, bit packing would be impossible:
    enum MyFlags {
        PausedOne(u8),
        PausedTwo,
    }

Proposed Usage

(derives on MyFlags as of yet undetermined)

enum MyFlags {
    PausedOne,
    PausedTwo,
}

#[derive(Flags)]
#[flags(flags = "MyFlags", storage_key = "StorageKey::Flags")]
struct Contract {
    // ...
}

impl Contract {
    fn one(&self) {
        Self::require_flag_unset(MyFlags::PausedOne);
        // ...
    }

    fn set_one(&self, on: bool) {
        if on {
            Self::set_flag(MyFlags::PausedOne);
        } else {
            Self::unset_flag(MyFlags::PausedOne);
        }
    }

    // similar for MyFlags::PausedTwo
}
@encody encody added the enhancement New feature or request label Sep 19, 2022
@ryancwalsh
Copy link
Contributor

Cool. I'll be curious to hear more about this, such as what would be real-world examples that PausedOne, PausedTwo, represent.

@encody
Copy link
Contributor Author

encody commented Sep 19, 2022

@ryancwalsh Allow you to pause individual functions instead of the contract as a whole. That's the inspiration, but bit flags have a huge range of use cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants