Skip to content

Commit

Permalink
chore: approval methods
Browse files Browse the repository at this point in the history
  • Loading branch information
encody committed May 3, 2024
1 parent 3df50f2 commit 2fd131c
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 47 deletions.
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
16 changes: 9 additions & 7 deletions workspaces-tests/src/bin/counter_multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ impl Contract {

#[init]
pub fn new() -> Self {
<Self as ApprovalManager<_, _, _>>::init(Configuration::new(
Self::THRESHOLD,
Self::VALIDITY_PERIOD_NANOSECONDS,
));
let mut contract = Self { counter: 0 };

Self { counter: 0 }
ApprovalManager::init(
&mut contract,
Configuration::new(Self::THRESHOLD, Self::VALIDITY_PERIOD_NANOSECONDS),
);

contract
}

pub fn obtain_multisig_permission(&mut self) {
Expand Down Expand Up @@ -97,11 +99,11 @@ impl Contract {
&self,
request_id: u32,
) -> Option<ActionRequest<CounterAction, simple_multisig::ApprovalState>> {
<Self as ApprovalManager<_, _, _>>::get_request(request_id)
ApprovalManager::get_request(self, request_id)
}

pub fn is_approved(&self, request_id: u32) -> bool {
<Self as ApprovalManager<_, _, _>>::is_approved_for_execution(request_id).is_ok()
self.is_approved_for_execution(request_id).is_ok()
}

pub fn execute(&mut self, request_id: u32) -> u32 {
Expand Down
14 changes: 8 additions & 6 deletions workspaces-tests/src/bin/native_multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ impl Contract {

#[init]
pub fn new() -> Self {
<Self as ApprovalManager<_, _, _>>::init(Configuration::new(
Self::APPROVAL_THRESHOLD,
Self::VALIDITY_PERIOD,
));
let mut contract = Self {};

Self {}
ApprovalManager::init(
&mut contract,
Configuration::new(Self::APPROVAL_THRESHOLD, Self::VALIDITY_PERIOD),
);

contract
}

pub fn obtain_multisig_permission(&mut self) {
Expand Down Expand Up @@ -67,7 +69,7 @@ impl Contract {
}

pub fn is_approved(&self, request_id: u32) -> bool {
<Contract as ApprovalManager<_, _, _>>::is_approved_for_execution(request_id).is_ok()
self.is_approved_for_execution(request_id).is_ok()
}

pub fn execute(&mut self, request_id: u32) -> Promise {
Expand Down
14 changes: 8 additions & 6 deletions workspaces-tests/src/bin/simple_multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,14 @@ impl Contract {

#[init]
pub fn new() -> Self {
<Self as ApprovalManager<_, _, _>>::init(Configuration::new(
Self::APPROVAL_THRESHOLD,
Self::VALIDITY_PERIOD,
));
let mut contract = Self {};

Self {}
ApprovalManager::init(
&mut contract,
Configuration::new(Self::APPROVAL_THRESHOLD, Self::VALIDITY_PERIOD),
);

contract
}

pub fn obtain_multisig_permission(&mut self) {
Expand All @@ -122,7 +124,7 @@ impl Contract {
}

pub fn is_approved(&self, request_id: u32) -> bool {
<Contract as ApprovalManager<_, _, _>>::is_approved_for_execution(request_id).is_ok()
self.is_approved_for_execution(request_id).is_ok()
}

pub fn execute(&mut self, request_id: u32) -> String {
Expand Down
9 changes: 5 additions & 4 deletions workspaces-tests/src/bin/upgrade_old_multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ pub struct Contract {
impl Contract {
#[init]
pub fn new() -> Self {
<Self as ApprovalManager<_, _, _>>::init(approval::simple_multisig::Configuration::new(
1, 0,
));

let mut contract = Self { foo: 0 };

ApprovalManager::init(
&mut contract,
approval::simple_multisig::Configuration::new(1, 0),
);

let predecessor = env::predecessor_account_id();

Owner::init(&mut contract, &predecessor);
Expand Down

0 comments on commit 2fd131c

Please sign in to comment.