Skip to content

Commit

Permalink
Implement rust logics for permissioned signer
Browse files Browse the repository at this point in the history
  • Loading branch information
runtian-zhou committed Sep 5, 2024
1 parent 070a951 commit 703921b
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
5 changes: 5 additions & 0 deletions aptos-move/framework/src/natives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod hash;
pub mod object;
pub mod object_code_deployment;
pub mod randomness;
pub mod permissioned_signer;
pub mod state_storage;
pub mod string_utils;
pub mod transaction_context;
Expand Down Expand Up @@ -91,6 +92,10 @@ pub fn all_natives(
"dispatchable_fungible_asset",
dispatchable_fungible_asset::make_all(builder)
);
add_natives_from_module!(
"permissioned_signer",
permissioned_signer::make_all(builder)
);

if inject_create_signer_for_gov_sim {
add_natives_from_module!(
Expand Down
106 changes: 106 additions & 0 deletions aptos-move/framework/src/natives/permissioned_signer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0
use aptos_native_interface::{
safely_pop_arg, RawSafeNative, SafeNativeBuilder, SafeNativeContext, SafeNativeError,
SafeNativeResult,
};
use move_vm_runtime::native_functions::NativeFunction;
use move_vm_types::{
loaded_data::runtime_types::Type,
values::{StructRef, Value, SignerRef},
};
use smallvec::{smallvec, SmallVec};
use std::collections::VecDeque;

/***************************************************************************************************
* native fun is_permissioned_signer
*
* Returns true if the signer passed in is a permissioned signer
* gas cost: base_cost
*
**************************************************************************************************/
fn native_is_permissioned_signer(
_context: &mut SafeNativeContext,
_ty_args: Vec<Type>,
mut arguments: VecDeque<Value>,
) -> SafeNativeResult<SmallVec<[Value; 1]>> {
debug_assert!(arguments.len() == 1);

Check warning on line 27 in aptos-move/framework/src/natives/permissioned_signer.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/framework/src/natives/permissioned_signer.rs#L22-L27

Added lines #L22 - L27 were not covered by tests

let s_arg = safely_pop_arg!(arguments, SignerRef);

Check warning on line 29 in aptos-move/framework/src/natives/permissioned_signer.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/framework/src/natives/permissioned_signer.rs#L29

Added line #L29 was not covered by tests

// context.charge()?;
let result = s_arg.is_permissioned()?;

Check warning on line 32 in aptos-move/framework/src/natives/permissioned_signer.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/framework/src/natives/permissioned_signer.rs#L32

Added line #L32 was not covered by tests


Ok(smallvec![Value::bool(result)])
}

Check warning on line 36 in aptos-move/framework/src/natives/permissioned_signer.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/framework/src/natives/permissioned_signer.rs#L35-L36

Added lines #L35 - L36 were not covered by tests

/***************************************************************************************************
* native fun permission_signer
*
* Returns the permission signer if the signer passed in is a permissioned signer
* gas cost: base_cost
*
**************************************************************************************************/
fn native_permission_signer(
_context: &mut SafeNativeContext,
_ty_args: Vec<Type>,
mut arguments: VecDeque<Value>,
) -> SafeNativeResult<SmallVec<[Value; 1]>> {
debug_assert!(arguments.len() == 1);

Check warning on line 50 in aptos-move/framework/src/natives/permissioned_signer.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/framework/src/natives/permissioned_signer.rs#L45-L50

Added lines #L45 - L50 were not covered by tests

let s_arg = safely_pop_arg!(arguments, SignerRef);

Check warning on line 52 in aptos-move/framework/src/natives/permissioned_signer.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/framework/src/natives/permissioned_signer.rs#L52

Added line #L52 was not covered by tests

// context.charge()?;
if !s_arg.is_permissioned()? {
return Err(SafeNativeError::Abort { abort_code: 3 });
}

Ok(smallvec![s_arg.permissioned_signer()?])
}

Check warning on line 60 in aptos-move/framework/src/natives/permissioned_signer.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/framework/src/natives/permissioned_signer.rs#L55-L60

Added lines #L55 - L60 were not covered by tests

/***************************************************************************************************
* native fun signer_from_permissioned
*
* Returns the permission signer from a master signer.
* gas cost: base_cost
*
**************************************************************************************************/
fn native_signer_from_permissioned(
_context: &mut SafeNativeContext,
_ty_args: Vec<Type>,
mut arguments: VecDeque<Value>,
) -> SafeNativeResult<SmallVec<[Value; 1]>> {
debug_assert!(arguments.len() == 1);

Check warning on line 74 in aptos-move/framework/src/natives/permissioned_signer.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/framework/src/natives/permissioned_signer.rs#L69-L74

Added lines #L69 - L74 were not covered by tests

let s_arg = safely_pop_arg!(arguments, StructRef);

Check warning on line 76 in aptos-move/framework/src/natives/permissioned_signer.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/framework/src/natives/permissioned_signer.rs#L76

Added line #L76 was not covered by tests

// context.charge()?;

Ok(smallvec![s_arg.read_ref()?])
}

Check warning on line 81 in aptos-move/framework/src/natives/permissioned_signer.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/framework/src/natives/permissioned_signer.rs#L80-L81

Added lines #L80 - L81 were not covered by tests

/***************************************************************************************************
* module
*
**************************************************************************************************/
pub fn make_all(
builder: &SafeNativeBuilder,
) -> impl Iterator<Item = (String, NativeFunction)> + '_ {
let natives = [
(
"is_permissioned_signer",
native_is_permissioned_signer as RawSafeNative,
),
(
"permission_signer",
native_permission_signer
),
(
"signer_from_permissioned",
native_signer_from_permissioned
),
];

builder.make_named_natives(natives)
}
32 changes: 32 additions & 0 deletions third_party/move/move-vm/types/src/values/values_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ impl Container {
fn signer(x: AccountAddress) -> Self {
Container::Struct(Rc::new(RefCell::new(vec![ValueImpl::Address(x)])))
}

fn permissioned_signer(x: AccountAddress, permission_address: AccountAddress) -> Self {
Container::Struct(Rc::new(RefCell::new(vec![ValueImpl::Address(x), ValueImpl::Address(permission_address)])))
}

Check warning on line 291 in third_party/move/move-vm/types/src/values/values_impl.rs

View check run for this annotation

Codecov / codecov/patch

third_party/move/move-vm/types/src/values/values_impl.rs#L289-L291

Added lines #L289 - L291 were not covered by tests
}

/***************************************************************************************
Expand Down Expand Up @@ -1060,6 +1064,30 @@ impl SignerRef {
pub fn borrow_signer(&self) -> PartialVMResult<Value> {
Ok(Value(self.0.borrow_elem(0)?))
}

pub fn is_permissioned(&self) -> PartialVMResult<bool> {
match &self.0 {
ContainerRef::Local(Container::Struct(s)) => {
Ok(s.borrow().len() == 2)

Check warning on line 1071 in third_party/move/move-vm/types/src/values/values_impl.rs

View check run for this annotation

Codecov / codecov/patch

third_party/move/move-vm/types/src/values/values_impl.rs#L1068-L1071

Added lines #L1068 - L1071 were not covered by tests
}
_ => Err(
PartialVMError::new(StatusCode::UNKNOWN_INVARIANT_VIOLATION_ERROR)
.with_message(format!("unexpected signer value: {:?}", self)),
)

Check warning on line 1076 in third_party/move/move-vm/types/src/values/values_impl.rs

View check run for this annotation

Codecov / codecov/patch

third_party/move/move-vm/types/src/values/values_impl.rs#L1073-L1076

Added lines #L1073 - L1076 were not covered by tests
}
}

Check warning on line 1078 in third_party/move/move-vm/types/src/values/values_impl.rs

View check run for this annotation

Codecov / codecov/patch

third_party/move/move-vm/types/src/values/values_impl.rs#L1078

Added line #L1078 was not covered by tests

pub fn permissioned_signer(&self) -> PartialVMResult<Value> {
match &self.0 {
ContainerRef::Local(Container::Struct(s)) if s.borrow().len() == 2 => {
Ok(Value::signer(*s.borrow()[1].as_value_ref::<AccountAddress>()?))

Check warning on line 1083 in third_party/move/move-vm/types/src/values/values_impl.rs

View check run for this annotation

Codecov / codecov/patch

third_party/move/move-vm/types/src/values/values_impl.rs#L1080-L1083

Added lines #L1080 - L1083 were not covered by tests
}
_ => Err(
PartialVMError::new(StatusCode::UNKNOWN_INVARIANT_VIOLATION_ERROR)
.with_message(format!("unexpected signer value: {:?}", self)),
)

Check warning on line 1088 in third_party/move/move-vm/types/src/values/values_impl.rs

View check run for this annotation

Codecov / codecov/patch

third_party/move/move-vm/types/src/values/values_impl.rs#L1085-L1088

Added lines #L1085 - L1088 were not covered by tests
}
}

Check warning on line 1090 in third_party/move/move-vm/types/src/values/values_impl.rs

View check run for this annotation

Codecov / codecov/patch

third_party/move/move-vm/types/src/values/values_impl.rs#L1090

Added line #L1090 was not covered by tests
}

/***************************************************************************************
Expand Down Expand Up @@ -1221,6 +1249,10 @@ impl Value {
Self(ValueImpl::Container(Container::signer(x)))
}

pub fn permissioned_signer(x: AccountAddress, permission_address: AccountAddress) -> Self {
Self(ValueImpl::Container(Container::permissioned_signer(x, permission_address)))
}

Check warning on line 1254 in third_party/move/move-vm/types/src/values/values_impl.rs

View check run for this annotation

Codecov / codecov/patch

third_party/move/move-vm/types/src/values/values_impl.rs#L1252-L1254

Added lines #L1252 - L1254 were not covered by tests

/// Create a "unowned" reference to a signer value (&signer) for populating the &signer in
/// execute function
pub fn signer_reference(x: AccountAddress) -> Self {
Expand Down

0 comments on commit 703921b

Please sign in to comment.