Skip to content

Commit

Permalink
Merge pull request #2250 from dusk-network/issue-2230-owner-only-calls
Browse files Browse the repository at this point in the history
Issue 2230 owner only calls
  • Loading branch information
miloszm authored Sep 4, 2024
2 parents a7fa425 + 38f57aa commit 538a4ef
Show file tree
Hide file tree
Showing 9 changed files with 424 additions and 22 deletions.
4 changes: 4 additions & 0 deletions contracts/bob/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
rusk-abi = { version = "0.13.0-rc", path = "../../rusk-abi", features = ["debug"] }
execution-core = { version = "0.1.0", path = "../../execution-core" }
rkyv = { version = "0.7", default-features = false, features = ["size_32"] }
bytecheck = { version = "0.6", default-features = false }
dusk-bytes = "0.1"
15 changes: 15 additions & 0 deletions contracts/bob/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ mod wasm {
rusk_abi::wrap_call(arg_len, |n| STATE.init(n))
}

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

#[no_mangle]
unsafe fn owner_reset(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |(sig, msg)| STATE.owner_reset(sig, msg))
}

#[no_mangle]
unsafe fn ping(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |()| STATE.ping())
Expand All @@ -39,4 +49,9 @@ mod wasm {
unsafe fn value(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |()| STATE.value())
}

#[no_mangle]
unsafe fn nonce(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |()| STATE.nonce())
}
}
58 changes: 57 additions & 1 deletion contracts/bob/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,35 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

extern crate alloc;
use alloc::string::String;
use bytecheck::CheckBytes;
use dusk_bytes::Serializable;
use execution_core::{
signatures::bls::{PublicKey as BlsPublicKey, Signature as BlsSignature},
ContractId,
};
use rkyv::{Archive, Deserialize, Serialize};

#[derive(Debug, Clone, Archive, Serialize, Deserialize)]
#[archive_attr(derive(CheckBytes))]
pub struct OwnerMessage {
contract_id: ContractId,
args: u8,
fname: String,
nonce: u64,
}

/// Bob contract.
#[derive(Debug, Clone)]
pub struct Bob {
value: u8,
nonce: u64,
}

impl Bob {
pub const fn new() -> Self {
Self { value: 0 }
Self { value: 0, nonce: 0 }
}

#[allow(dead_code)]
Expand All @@ -24,6 +44,38 @@ impl Bob {
impl Bob {
pub fn init(&mut self, n: u8) {
self.value = n;
self.nonce = 0;
}

pub fn reset(&mut self, n: u8) {
self.value = n;
}

pub fn owner_reset(&mut self, sig: BlsSignature, msg: OwnerMessage) {
let mut granted = false;
let message_bytes = rkyv::to_bytes::<_, 4096>(&msg)
.expect("Message should serialize correctly")
.to_vec();

let owner_bytes = rusk_abi::self_owner_raw();
if let Ok(owner) = BlsPublicKey::from_bytes(&owner_bytes) {
if self.nonce == msg.nonce
&& msg.fname == "owner_reset"
&& msg.contract_id == rusk_abi::self_id()
&& rusk_abi::verify_bls(message_bytes, owner, sig)
{
self.owner_only_function(msg.args);
self.nonce += 1;
granted = true;
}
}
if !granted {
panic!("method restricted only to the owner")
}
}

fn owner_only_function(&mut self, args: u8) {
self.value = args;
}

pub fn ping(&mut self) {}
Expand All @@ -35,4 +87,8 @@ impl Bob {
pub fn value(&mut self) -> u8 {
self.value
}

pub fn nonce(&mut self) -> u64 {
self.nonce
}
}
5 changes: 2 additions & 3 deletions contracts/host_fn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use execution_core::{
PublicKey as SchnorrPublicKey, Signature as SchnorrSignature,
},
},
transfer::phoenix::PublicKey as PhoenixPublicKey,
BlsScalar,
};

Expand Down Expand Up @@ -72,11 +71,11 @@ impl HostFnTest {
rusk_abi::block_height()
}

pub fn owner(&self) -> PhoenixPublicKey {
pub fn owner(&self) -> BlsPublicKey {
rusk_abi::self_owner()
}

pub fn owner_raw(&self) -> [u8; PhoenixPublicKey::SIZE] {
pub fn owner_raw(&self) -> [u8; BlsPublicKey::SIZE] {
rusk_abi::self_owner_raw()
}
}
Expand Down
2 changes: 2 additions & 0 deletions rusk-abi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Update `piecrust-uplink` to `0.15`
- Change dependencies declarations enforce bytecheck [#1371]
- Update dusk dependencies [#1609]
- Made owner methods compatible with BLS key [#2230]

## [0.11.0] - 2023-10-12

Expand Down Expand Up @@ -200,6 +201,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add LICENSE
- Add README.md

[#2230]: https://github.com/dusk-network/rusk/issues/2230
[#1710]: https://github.com/dusk-network/rusk/issues/1710
[#1630]: https://github.com/dusk-network/rusk/issues/1630
[#1609]: https://github.com/dusk-network/rusk/issues/1609
Expand Down
14 changes: 6 additions & 8 deletions rusk-abi/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use execution_core::{
PublicKey as SchnorrPublicKey, Signature as SchnorrSignature,
},
},
transfer::phoenix::PublicKey as PhoenixPublicKey,
BlsScalar, ContractId,
};
use piecrust_uplink::{host_query, meta_data};
Expand Down Expand Up @@ -68,27 +67,26 @@ pub fn block_height() -> u64 {
/// 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).
pub fn owner(contract: ContractId) -> Option<PhoenixPublicKey> {
pub fn owner(contract: ContractId) -> Option<BlsPublicKey> {
owner_raw(contract).map(|buf| {
PhoenixPublicKey::from_bytes(&buf)
BlsPublicKey::from_bytes(&buf)
.expect("Owner should deserialize correctly")
})
}

/// Query self owner of a given contract.
/// Panics if owner is not a valid public key (should never happen).
pub fn self_owner() -> PhoenixPublicKey {
pub fn self_owner() -> BlsPublicKey {
let buf = self_owner_raw();
PhoenixPublicKey::from_bytes(&buf)
.expect("Owner should deserialize correctly")
BlsPublicKey::from_bytes(&buf).expect("Owner should deserialize correctly")
}

/// Query raw "to_bytes" serialization of the owner of a given contract.
pub fn owner_raw(contract: ContractId) -> Option<[u8; PhoenixPublicKey::SIZE]> {
pub fn owner_raw(contract: ContractId) -> Option<[u8; BlsPublicKey::SIZE]> {
piecrust_uplink::owner(contract)
}

/// Query raw "to_bytes" serialization of the self owner.
pub fn self_owner_raw() -> [u8; PhoenixPublicKey::SIZE] {
pub fn self_owner_raw() -> [u8; BlsPublicKey::SIZE] {
piecrust_uplink::self_owner()
}
17 changes: 7 additions & 10 deletions rusk-abi/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,12 @@ use execution_core::{
PublicKey as SchnorrPublicKey, SecretKey as SchnorrSecretKey,
},
},
transfer::phoenix::{
PublicKey as PhoenixPublicKey, SecretKey as PhoenixSecretKey,
},
BlsScalar, ContractId,
};
use ff::Field;
use rusk_abi::{ContractData, Session, VM};

const POINT_LIMIT: u64 = 0x1000000;
const POINT_LIMIT: u64 = 0x4000000;
const CHAIN_ID: u8 = 0xFA;

#[test]
Expand Down Expand Up @@ -369,11 +366,11 @@ fn block_height() {
assert_eq!(height, HEIGHT);
}

fn get_owner() -> &'static PhoenixPublicKey {
static OWNER: OnceLock<PhoenixPublicKey> = OnceLock::new();
fn get_owner() -> &'static BlsPublicKey {
static OWNER: OnceLock<BlsPublicKey> = OnceLock::new();
OWNER.get_or_init(|| {
let sk = PhoenixSecretKey::random(&mut OsRng);
PhoenixPublicKey::from(&sk)
let sk = BlsSecretKey::random(&mut OsRng);
BlsPublicKey::from(&sk)
})
}

Expand All @@ -383,7 +380,7 @@ fn owner_raw() {
rusk_abi::new_ephemeral_vm().expect("Instantiating VM should succeed");
let (mut session, contract_id) = instantiate(&vm, 0);

let owner: [u8; 64] = session
let owner: [u8; 96] = session
.call(contract_id, "contract_owner_raw", get_owner(), POINT_LIMIT)
.expect("Query should succeed")
.data;
Expand All @@ -397,7 +394,7 @@ fn owner() {
rusk_abi::new_ephemeral_vm().expect("Instantiating VM should succeed");
let (mut session, contract_id) = instantiate(&vm, 0);

let owner: PhoenixPublicKey = session
let owner: BlsPublicKey = session
.call(contract_id, "contract_owner", get_owner(), POINT_LIMIT)
.expect("Query should succeed")
.data;
Expand Down
1 change: 1 addition & 0 deletions rusk/tests/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
pub mod contract_deployment;
pub mod gas_behavior;
pub mod multi_transfer;
pub mod owner_calls;
pub mod stake;
pub mod transfer;
pub mod unspendable;
Loading

0 comments on commit 538a4ef

Please sign in to comment.