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

BREAKING: Make non-internal associated functions into methods #146

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl Contract {
}

pub fn owner_only(&self) {
Self::require_owner();
self.require_owner();

// ...
}
Expand Down
2 changes: 1 addition & 1 deletion macros/src/pause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub fn expand(meta: PauseMeta) -> Result<TokenStream, darling::Error> {
#[#near_sdk::near]
impl #imp #me::pause::PauseExternal for #ident #ty #wher {
fn paus_is_paused(&self) -> bool {
<Self as #me::pause::Pause>::is_paused()
#me::pause::Pause::is_paused(self)
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion macros/src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub fn expand(meta: UpgradeMeta) -> Result<TokenStream, darling::Error> {
HookBody::Empty => Some(quote! {}), // empty implementation
HookBody::Custom => None, // user-provided implementation
HookBody::Owner => Some(quote! {
<Self as #me::owner::Owner>::require_owner();
#me::owner::Owner::require_owner(self);
}),
HookBody::Role(role) => Some(quote! {
#me::rbac::Rbac::require_role(self, &#role);
Expand Down
36 changes: 21 additions & 15 deletions src/approval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,11 @@ where
fn get_config() -> C;

/// Get a request by ID
fn get_request(request_id: u32) -> Option<ActionRequest<A, S>>;
fn get_request(&self, request_id: u32) -> Option<ActionRequest<A, S>>;

/// Must be called before using the Approval construct. Can only be called
/// once.
fn init(config: C);
fn init(&mut self, config: C);

/// Creates a new action request initialized with the given approval state
fn create_request(
Expand All @@ -191,7 +191,10 @@ where

/// Is the given request ID able to be executed if such a request were to
/// be initiated by an authorized account?
fn is_approved_for_execution(request_id: u32) -> Result<(), C::ExecutionEligibilityError>;
fn is_approved_for_execution(
&self,
request_id: u32,
) -> Result<(), C::ExecutionEligibilityError>;

/// Tries to approve the action request designated by the given request ID
/// with the given arguments. Panics if the request ID does not exist.
Expand Down Expand Up @@ -219,11 +222,11 @@ where
.unwrap_or_else(|| env::panic_str(NOT_INITIALIZED))
}

fn get_request(request_id: u32) -> Option<ActionRequest<A, S>> {
fn get_request(&self, request_id: u32) -> Option<ActionRequest<A, S>> {
Self::slot_request(request_id).read()
}

fn init(config: C) {
fn init(&mut self, config: C) {
require!(
Self::slot_config().swap(&config).is_none(),
ALREADY_INITIALIZED,
Expand Down Expand Up @@ -260,7 +263,7 @@ where
request_id: u32,
) -> Result<A::Output, ExecutionError<C::AuthorizationError, C::ExecutionEligibilityError>>
{
Self::is_approved_for_execution(request_id)
self.is_approved_for_execution(request_id)
.map_err(ExecutionError::ExecutionEligibility)?;

let predecessor = env::predecessor_account_id();
Expand All @@ -279,7 +282,10 @@ where
Ok(result)
}

fn is_approved_for_execution(request_id: u32) -> Result<(), C::ExecutionEligibilityError> {
fn is_approved_for_execution(
&self,
request_id: u32,
) -> Result<(), C::ExecutionEligibilityError> {
let request = Self::slot_request(request_id).read().unwrap();

let config = Self::get_config();
Expand Down Expand Up @@ -385,9 +391,9 @@ mod tests {
impl Contract {
#[init]
pub fn new(threshold: u8) -> Self {
let contract = Self {};
let mut contract = Self {};

<Self as ApprovalManager<_, _, _>>::init(MultisigConfig { threshold });
ApprovalManager::init(&mut contract, MultisigConfig { threshold });

contract
}
Expand Down Expand Up @@ -499,16 +505,16 @@ mod tests {
.unwrap();

assert_eq!(request_id, 0);
assert!(Contract::is_approved_for_execution(request_id).is_err());
assert!(contract.is_approved_for_execution(request_id).is_err());

contract.approve_request(request_id).unwrap();

assert!(Contract::is_approved_for_execution(request_id).is_err());
assert!(contract.is_approved_for_execution(request_id).is_err());

predecessor(&charlie);
contract.approve_request(request_id).unwrap();

assert!(Contract::is_approved_for_execution(request_id).is_ok());
assert!(contract.is_approved_for_execution(request_id).is_ok());

assert_eq!(contract.execute_request(request_id).unwrap(), "hello");
}
Expand Down Expand Up @@ -597,15 +603,15 @@ mod tests {
predecessor(&bob);
contract.approve_request(request_id).unwrap();

assert!(Contract::is_approved_for_execution(request_id).is_ok());
assert!(contract.is_approved_for_execution(request_id).is_ok());

contract.remove_role(&alice, &Role::Multisig);

assert!(Contract::is_approved_for_execution(request_id).is_err());
assert!(contract.is_approved_for_execution(request_id).is_err());

predecessor(&charlie);
contract.approve_request(request_id).unwrap();

assert!(Contract::is_approved_for_execution(request_id).is_ok());
assert!(contract.is_approved_for_execution(request_id).is_ok());
}
}
22 changes: 13 additions & 9 deletions src/approval/simple_multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,9 @@ mod tests {
impl Contract {
#[init]
pub fn new() -> Self {
<Self as ApprovalManager<_, _, _>>::init(Configuration::new(2, 10000));
Self {}
let mut contract = Self {};
ApprovalManager::init(&mut contract, Configuration::new(2, 10000));
contract
}

pub fn obtain_multisig_permission(&mut self) {
Expand Down Expand Up @@ -328,22 +329,22 @@ mod tests {
let request_id = contract.create(true);

assert_eq!(request_id, 0);
assert!(Contract::is_approved_for_execution(request_id).is_err());
assert!(contract.is_approved_for_execution(request_id).is_err());

predecessor(&alice);
contract.approve(request_id);

assert!(Contract::is_approved_for_execution(request_id).is_err());
assert!(contract.is_approved_for_execution(request_id).is_err());

predecessor(&charlie);
contract.approve(request_id);

assert!(Contract::is_approved_for_execution(request_id).is_ok());
assert!(contract.is_approved_for_execution(request_id).is_ok());

predecessor(&bob);
contract.approve(request_id);

assert!(Contract::is_approved_for_execution(request_id).is_ok());
assert!(contract.is_approved_for_execution(request_id).is_ok());

assert_eq!(contract.execute(request_id), "hello");
}
Expand All @@ -361,7 +362,8 @@ mod tests {

contract.approve(request_id);

let created_at = Contract::get_request(request_id)
let created_at = contract
.get_request(request_id)
.unwrap()
.approval_state
.created_at_nanoseconds;
Expand Down Expand Up @@ -389,7 +391,8 @@ mod tests {

contract.approve(request_id);

let created_at = Contract::get_request(request_id)
let created_at = contract
.get_request(request_id)
.unwrap()
.approval_state
.created_at_nanoseconds;
Expand Down Expand Up @@ -418,7 +421,8 @@ mod tests {

contract.approve(request_id);

let created_at = Contract::get_request(request_id)
let created_at = contract
.get_request(request_id)
.unwrap()
.approval_state
.created_at_nanoseconds;
Expand Down
8 changes: 4 additions & 4 deletions src/escrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,23 @@ pub trait EscrowInternal {
}

/// Inner function to retrieve the slot keyed by it's `Self::Id`
fn locked_slot(&self, id: &Self::Id) -> Slot<Self::State> {
fn slot_locked(id: &Self::Id) -> Slot<Self::State> {
Self::root().field(StorageKey::Locked(id))
}

/// Read the state from the slot
fn get_locked(&self, id: &Self::Id) -> Option<Self::State> {
self.locked_slot(id).read()
Self::slot_locked(id).read()
}

/// Set the state at `id` to `locked`
fn set_locked(&mut self, id: &Self::Id, locked: &Self::State) {
self.locked_slot(id).write(locked);
Self::slot_locked(id).write(locked);
}

/// Clear the state at `id`
fn set_unlocked(&mut self, id: &Self::Id) {
self.locked_slot(id).remove();
Self::slot_locked(id).remove();
}
}

Expand Down
54 changes: 20 additions & 34 deletions src/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,7 @@ pub trait Owner {
/// Updates proposed owner without any checks or emitting events.
fn update_proposed_unchecked(&mut self, new: Option<AccountId>);

/// Same as require_owner but as a method.
fn assert_owner(&self);

/// Initializes the contract owner. Can only be called once.
///
/// Emits an `OwnerEvent::Transfer` event.
/// Rejects if the predecessor is not the current owner.
///
/// # Examples
///
Expand All @@ -131,19 +126,18 @@ pub trait Owner {
///
/// #[near]
/// impl Contract {
/// #[init]
/// pub fn new(owner_id: AccountId) -> Self {
/// let mut contract = Self {};
///
/// Owner::init(&mut contract, &owner_id);
/// pub fn owner_only_function(&self) {
/// self.require_owner();
///
/// contract
/// // ...
/// }
/// }
/// ```
fn init(&mut self, owner_id: &AccountId);
fn require_owner(&self);

/// Requires the predecessor to be the owner.
/// Initializes the contract owner. Can only be called once.
///
/// Emits an `OwnerEvent::Transfer` event.
///
/// # Examples
///
Expand All @@ -157,14 +151,17 @@ pub trait Owner {
///
/// #[near]
/// impl Contract {
/// pub fn owner_only(&self) {
/// Self::require_owner();
/// #[init]
/// pub fn new(owner_id: AccountId) -> Self {
/// let mut contract = Self {};
///
/// // ...
/// Owner::init(&mut contract, &owner_id);
///
/// contract
/// }
/// }
/// ```
fn require_owner();
fn init(&mut self, owner_id: &AccountId);

/// Removes the contract's owner. Can only be called by the current owner.
///
Expand Down Expand Up @@ -226,7 +223,7 @@ impl<T: OwnerInternal> Owner for T {
proposed_owner.set(new.as_ref());
}

fn assert_owner(&self) {
fn require_owner(&self) {
require!(
&env::predecessor_account_id()
== Self::slot_owner()
Expand All @@ -253,26 +250,15 @@ impl<T: OwnerInternal> Owner for T {
.emit();
}

fn require_owner() {
require!(
&env::predecessor_account_id()
== Self::slot_owner()
.read()
.as_ref()
.unwrap_or_else(|| env::panic_str(NO_OWNER_FAIL_MESSAGE)),
ONLY_OWNER_FAIL_MESSAGE,
);
}

fn renounce_owner(&mut self) {
Self::require_owner();
self.require_owner();

self.update_proposed(None);
self.update_owner(None);
}

fn propose_owner(&mut self, account_id: Option<AccountId>) {
Self::require_owner();
self.require_owner();

self.update_proposed(account_id);
}
Expand Down Expand Up @@ -312,7 +298,7 @@ pub mod hooks {
C: Owner,
{
fn hook<R>(contract: &mut C, _args: &A, f: impl FnOnce(&mut C) -> R) -> R {
contract.assert_owner();
contract.require_owner();
f(contract)
}
}
Expand Down Expand Up @@ -377,7 +363,7 @@ mod tests {
}

pub fn owner_only(&self) {
Self::require_owner();
self.require_owner();
}
}

Expand Down
Loading
Loading