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

Chain execution for user-driven proposals #1490

Open
wants to merge 4 commits into
base: feat/identity-rebased-alpha
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,56 +12,79 @@ use iota_sdk::types::programmable_transaction_builder::ProgrammableTransactionBu
use iota_sdk::types::transaction::Argument;
use iota_sdk::types::transaction::ObjectArg;
use iota_sdk::types::transaction::ProgrammableTransaction;
use itertools::Itertools;
use move_core_types::ident_str;

use crate::rebased::iota::move_calls::utils;
use crate::rebased::proposals::BorrowAction;
use crate::rebased::utils::MoveType;

pub(crate) fn propose_borrow(
use super::ProposalContext;

fn borrow_proposal_impl(
identity: OwnedObjectRef,
capability: ObjectRef,
objects: Vec<ObjectID>,
expiration: Option<u64>,
package_id: ObjectID,
) -> Result<ProgrammableTransaction, anyhow::Error> {
) -> anyhow::Result<ProposalContext> {
let mut ptb = ProgrammableTransactionBuilder::new();
let cap_arg = ptb.obj(ObjectArg::ImmOrOwnedObject(capability))?;
let (delegation_token, borrow) = utils::get_controller_delegation(&mut ptb, cap_arg, package_id);
let identity_arg = utils::owned_ref_to_shared_object_arg(identity, &mut ptb, true)?;
let exp_arg = utils::option_to_move(expiration, &mut ptb, package_id)?;
let objects_arg = ptb.pure(objects)?;

let _proposal_id = ptb.programmable_move_call(
let proposal_id = ptb.programmable_move_call(
package_id,
ident_str!("identity").into(),
ident_str!("propose_borrow").into(),
vec![],
vec![identity_arg, delegation_token, exp_arg, objects_arg],
);

utils::put_back_delegation_token(&mut ptb, cap_arg, delegation_token, borrow, package_id);

Ok(ptb.finish())
Ok(ProposalContext {
ptb,
identity: identity_arg,
controller_cap: cap_arg,
delegation_token,
borrow,
proposal_id,
})
}

pub(crate) fn execute_borrow<F>(
pub(crate) fn propose_borrow(
identity: OwnedObjectRef,
capability: ObjectRef,
proposal_id: ObjectID,
objects: Vec<ObjectID>,
expiration: Option<u64>,
package_id: ObjectID,
) -> Result<ProgrammableTransaction, anyhow::Error> {
let ProposalContext {
mut ptb,
controller_cap,
delegation_token,
borrow,
..
} = borrow_proposal_impl(identity, capability, objects, expiration, package_id)?;

utils::put_back_delegation_token(&mut ptb, controller_cap, delegation_token, borrow, package_id);

Ok(ptb.finish())
}

fn execute_borrow_impl<F>(
ptb: &mut ProgrammableTransactionBuilder,
identity: Argument,
delegation_token: Argument,
proposal_id: Argument,
objects: Vec<IotaObjectData>,
intent_fn: F,
package: ObjectID,
) -> Result<ProgrammableTransaction, anyhow::Error>
) -> anyhow::Result<()>
where
F: FnOnce(&mut ProgrammableTransactionBuilder, &HashMap<ObjectID, (Argument, IotaObjectData)>),
{
let mut ptb = ProgrammableTransactionBuilder::new();
let identity = utils::owned_ref_to_shared_object_arg(identity, &mut ptb, true)?;
let controller_cap = ptb.obj(ObjectArg::ImmOrOwnedObject(capability))?;
let (delegation_token, borrow) = utils::get_controller_delegation(&mut ptb, controller_cap, package);
let proposal_id = ptb.pure(proposal_id)?;

// Get the proposal's action as argument.
let borrow_action = ptb.programmable_move_call(
package,
Expand All @@ -71,8 +94,6 @@ where
vec![identity, delegation_token, proposal_id],
);

utils::put_back_delegation_token(&mut ptb, controller_cap, delegation_token, borrow, package);

// Borrow all the objects specified in the action.
let obj_arg_map = objects
.into_iter()
Expand All @@ -96,7 +117,7 @@ where
.collect::<anyhow::Result<_>>()?;

// Apply the user-defined operation.
intent_fn(&mut ptb, &obj_arg_map);
intent_fn(ptb, &obj_arg_map);

// Put back all the objects.
obj_arg_map.into_values().for_each(|(obj_arg, obj_data)| {
Expand All @@ -121,5 +142,78 @@ where
vec![borrow_action],
);

Ok(())
}

pub(crate) fn execute_borrow<F>(
identity: OwnedObjectRef,
capability: ObjectRef,
proposal_id: ObjectID,
objects: Vec<IotaObjectData>,
intent_fn: F,
package: ObjectID,
) -> Result<ProgrammableTransaction, anyhow::Error>
where
F: FnOnce(&mut ProgrammableTransactionBuilder, &HashMap<ObjectID, (Argument, IotaObjectData)>),
{
let mut ptb = ProgrammableTransactionBuilder::new();
let identity = utils::owned_ref_to_shared_object_arg(identity, &mut ptb, true)?;
let controller_cap = ptb.obj(ObjectArg::ImmOrOwnedObject(capability))?;
let (delegation_token, borrow) = utils::get_controller_delegation(&mut ptb, controller_cap, package);
let proposal_id = ptb.pure(proposal_id)?;

execute_borrow_impl(
&mut ptb,
identity,
delegation_token,
proposal_id,
objects,
intent_fn,
package,
)?;

utils::put_back_delegation_token(&mut ptb, controller_cap, delegation_token, borrow, package);

Ok(ptb.finish())
}

pub(crate) fn create_and_execute_borrow<F>(
identity: OwnedObjectRef,
capability: ObjectRef,
objects: Vec<IotaObjectData>,
intent_fn: F,
expiration: Option<u64>,
package_id: ObjectID,
) -> anyhow::Result<ProgrammableTransaction>
where
F: FnOnce(&mut ProgrammableTransactionBuilder, &HashMap<ObjectID, (Argument, IotaObjectData)>),
{
let ProposalContext {
mut ptb,
controller_cap,
delegation_token,
borrow,
identity,
proposal_id,
} = borrow_proposal_impl(
identity,
capability,
objects.iter().map(|obj_data| obj_data.object_id).collect_vec(),
expiration,
package_id,
)?;

execute_borrow_impl(
&mut ptb,
identity,
delegation_token,
proposal_id,
objects,
intent_fn,
package_id,
)?;

utils::put_back_delegation_token(&mut ptb, controller_cap, delegation_token, borrow, package_id);

Ok(ptb.finish())
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,71 @@ use crate::rebased::iota::move_calls::utils;
use crate::rebased::proposals::ControllerExecution;
use crate::rebased::utils::MoveType;

pub(crate) fn propose_controller_execution(
use super::ProposalContext;

fn controller_execution_impl(
identity: OwnedObjectRef,
capability: ObjectRef,
controller_cap_id: ObjectID,
expiration: Option<u64>,
package_id: ObjectID,
) -> Result<ProgrammableTransaction, anyhow::Error> {
) -> anyhow::Result<ProposalContext> {
let mut ptb = ProgrammableTransactionBuilder::new();
let cap_arg = ptb.obj(ObjectArg::ImmOrOwnedObject(capability))?;
let (delegation_token, borrow) = utils::get_controller_delegation(&mut ptb, cap_arg, package_id);
let identity_arg = utils::owned_ref_to_shared_object_arg(identity, &mut ptb, true)?;
let controller_cap_id = ptb.pure(controller_cap_id)?;
let exp_arg = utils::option_to_move(expiration, &mut ptb, package_id)?;

let _proposal_id = ptb.programmable_move_call(
let proposal_id = ptb.programmable_move_call(
package_id,
ident_str!("identity").into(),
ident_str!("propose_controller_execution").into(),
vec![],
vec![identity_arg, delegation_token, controller_cap_id, exp_arg],
);

utils::put_back_delegation_token(&mut ptb, cap_arg, delegation_token, borrow, package_id);

Ok(ptb.finish())
Ok(ProposalContext {
ptb,
controller_cap: cap_arg,
delegation_token,
borrow,
identity: identity_arg,
proposal_id,
})
}

pub(crate) fn execute_controller_execution<F>(
pub(crate) fn propose_controller_execution(
identity: OwnedObjectRef,
capability: ObjectRef,
proposal_id: ObjectID,
controller_cap_id: ObjectID,
expiration: Option<u64>,
package_id: ObjectID,
) -> Result<ProgrammableTransaction, anyhow::Error> {
let ProposalContext {
mut ptb,
controller_cap,
delegation_token,
borrow,
..
} = controller_execution_impl(identity, capability, controller_cap_id, expiration, package_id)?;
utils::put_back_delegation_token(&mut ptb, controller_cap, delegation_token, borrow, package_id);

Ok(ptb.finish())
}

fn execute_controller_execution_impl<F>(
ptb: &mut ProgrammableTransactionBuilder,
identity: Argument,
proposal_id: Argument,
delegation_token: Argument,
borrowing_controller_cap_ref: ObjectRef,
intent_fn: F,
package: ObjectID,
) -> Result<ProgrammableTransaction, anyhow::Error>
) -> anyhow::Result<()>
where
F: FnOnce(&mut ProgrammableTransactionBuilder, &Argument),
{
let mut ptb = ProgrammableTransactionBuilder::new();
let identity = utils::owned_ref_to_shared_object_arg(identity, &mut ptb, true)?;
let controller_cap = ptb.obj(ObjectArg::ImmOrOwnedObject(capability))?;
let (delegation_token, borrow) = utils::get_controller_delegation(&mut ptb, controller_cap, package);
let proposal_id = ptb.pure(proposal_id)?;

// Get the proposal's action as argument.
let controller_execution_action = ptb.programmable_move_call(
package,
Expand All @@ -67,8 +88,6 @@ where
vec![identity, delegation_token, proposal_id],
);

utils::put_back_delegation_token(&mut ptb, controller_cap, delegation_token, borrow, package);

// Borrow the controller cap into this transaction.
let receiving = ptb.obj(ObjectArg::Receiving(borrowing_controller_cap_ref))?;
let borrowed_controller_cap = ptb.programmable_move_call(
Expand All @@ -80,7 +99,7 @@ where
);

// Apply the user-defined operation.
intent_fn(&mut ptb, &borrowed_controller_cap);
intent_fn(ptb, &borrowed_controller_cap);

// Put back the borrowed controller cap.
ptb.programmable_move_call(
Expand All @@ -91,5 +110,78 @@ where
vec![controller_execution_action, borrowed_controller_cap],
);

Ok(())
}

pub(crate) fn execute_controller_execution<F>(
identity: OwnedObjectRef,
capability: ObjectRef,
proposal_id: ObjectID,
borrowing_controller_cap_ref: ObjectRef,
intent_fn: F,
package: ObjectID,
) -> Result<ProgrammableTransaction, anyhow::Error>
where
F: FnOnce(&mut ProgrammableTransactionBuilder, &Argument),
{
let mut ptb = ProgrammableTransactionBuilder::new();
let identity = utils::owned_ref_to_shared_object_arg(identity, &mut ptb, true)?;
let controller_cap = ptb.obj(ObjectArg::ImmOrOwnedObject(capability))?;
let (delegation_token, borrow) = utils::get_controller_delegation(&mut ptb, controller_cap, package);
let proposal_id = ptb.pure(proposal_id)?;

execute_controller_execution_impl(
&mut ptb,
identity,
proposal_id,
delegation_token,
borrowing_controller_cap_ref,
intent_fn,
package,
)?;

utils::put_back_delegation_token(&mut ptb, controller_cap, delegation_token, borrow, package);

Ok(ptb.finish())
}

pub(crate) fn create_and_execute_controller_execution<F>(
identity: OwnedObjectRef,
capability: ObjectRef,
expiration: Option<u64>,
borrowing_controller_cap_ref: ObjectRef,
intent_fn: F,
package_id: ObjectID,
) -> anyhow::Result<ProgrammableTransaction>
where
F: FnOnce(&mut ProgrammableTransactionBuilder, &Argument),
{
let ProposalContext {
mut ptb,
controller_cap,
delegation_token,
borrow,
proposal_id,
identity,
} = controller_execution_impl(
identity,
capability,
borrowing_controller_cap_ref.0,
expiration,
package_id,
)?;

execute_controller_execution_impl(
&mut ptb,
identity,
proposal_id,
delegation_token,
borrowing_controller_cap_ref,
intent_fn,
package_id,
)?;

utils::put_back_delegation_token(&mut ptb, controller_cap, delegation_token, borrow, package_id);

Ok(ptb.finish())
}
11 changes: 11 additions & 0 deletions identity_iota_core/src/rebased/iota/move_calls/identity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ pub(crate) use config::*;
pub(crate) use controller_execution::*;
pub(crate) use create::*;
pub(crate) use deactivate::*;
use iota_sdk::types::programmable_transaction_builder::ProgrammableTransactionBuilder as Ptb;
use iota_sdk::types::transaction::Argument;
pub(crate) use send_asset::*;
pub(crate) use update::*;
pub(crate) use upgrade::*;

struct ProposalContext {
ptb: Ptb,
controller_cap: Argument,
delegation_token: Argument,
borrow: Argument,
identity: Argument,
proposal_id: Argument,
}
Loading
Loading