Skip to content

Commit

Permalink
rusk-abi: improved owner and self_owner ABIs
Browse files Browse the repository at this point in the history
  • Loading branch information
miloszm committed May 8, 2024
1 parent 8cb1cb2 commit 1c99005
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
13 changes: 6 additions & 7 deletions rusk-abi/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use dusk_bytes::Serializable;
#[cfg(feature = "abi")]
use phoenix_core::PublicKey;
#[cfg(feature = "abi")]
use rkyv::{archived_root, Deserialize, Infallible};

pub use piecrust_uplink::*;

Expand Down Expand Up @@ -79,20 +77,21 @@ pub fn payment_info(
}

/// Query owner of a given contract.
/// Returns none if contract is not found.
/// Panics if owner is not a valid public key (should never happen).
#[cfg(feature = "abi")]
pub fn owner(contract: ContractId) -> Option<PublicKey> {
owner_raw(contract).map(|buf| {
let ret = unsafe { archived_root::<PublicKey>(buf.as_slice()) };
ret.deserialize(&mut Infallible).expect("Infallible")
PublicKey::from_bytes(&buf).expect("Owner should deserialize correctly")
})
}

/// Query owner of a given contract.
/// Query self owner of a given contract.
/// Panics if owner is not a valid public key (should never happen).
#[cfg(feature = "abi")]
pub fn self_owner() -> PublicKey {
let buf = self_owner_raw();
let ret = unsafe { archived_root::<PublicKey>(buf.as_slice()) };
ret.deserialize(&mut Infallible).expect("Infallible")
PublicKey::from_bytes(&buf).expect("Owner should deserialize correctly")
}

/// Query raw owner of a given contract.
Expand Down
11 changes: 10 additions & 1 deletion rusk-abi/tests/contracts/host_fn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ impl HostFnTest {
rusk_abi::block_height()
}

pub fn owner(&self) -> [u8; PublicKey::SIZE] {
pub fn owner(&self) -> PublicKey {
rusk_abi::self_owner()
}

pub fn owner_raw(&self) -> [u8; PublicKey::SIZE] {
rusk_abi::self_owner_raw()
}
}
Expand Down Expand Up @@ -114,6 +118,11 @@ unsafe fn contract_owner(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |_: ()| STATE.owner())
}

#[no_mangle]
unsafe fn contract_owner_raw(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |_: ()| STATE.owner_raw())
}

const PAYMENT_INFO: PaymentInfo = PaymentInfo::Transparent(None);

#[no_mangle]
Expand Down
18 changes: 16 additions & 2 deletions rusk-abi/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,15 +357,29 @@ fn get_owner() -> &'static PublicKey {
}

#[test]
fn owner() {
fn owner_raw() {
let vm =
rusk_abi::new_ephemeral_vm().expect("Instantiating VM should succeed");
let (mut session, contract_id) = instantiate(&vm, 0);

let owner: [u8; 64] = session
.call(contract_id, "contract_owner", get_owner(), POINT_LIMIT)
.call(contract_id, "contract_owner_raw", get_owner(), POINT_LIMIT)
.expect("Query should succeed")
.data;

assert_eq!(owner, get_owner().to_bytes());
}

#[test]
fn owner() {
let vm =
rusk_abi::new_ephemeral_vm().expect("Instantiating VM should succeed");
let (mut session, contract_id) = instantiate(&vm, 0);

let owner: PublicKey = session
.call(contract_id, "contract_owner", get_owner(), POINT_LIMIT)
.expect("Query should succeed")
.data;

assert_eq!(owner, get_owner().to_owned());
}

0 comments on commit 1c99005

Please sign in to comment.