Skip to content

Commit

Permalink
Merge pull request #369 from dusk-network/fix-econ-removal
Browse files Browse the repository at this point in the history
Fix incomplete economic protocol removal
  • Loading branch information
ureeves authored Jul 1, 2024
2 parents 4b4c298 + d2fbb43 commit 2d32532
Show file tree
Hide file tree
Showing 11 changed files with 9 additions and 332 deletions.
22 changes: 0 additions & 22 deletions contracts/metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,6 @@ impl Metadata {
pub fn read_owner_of(&self, id: ContractId) -> Option<[u8; 33]> {
uplink::owner(id)
}

/// Read the value of the given contract's free limit
pub fn read_free_limit_of(&self, id: ContractId) -> Option<u64> {
uplink::free_limit(id)
}

/// Read the value of the given contract's free price hint
pub fn read_free_price_hint_of(&self, id: ContractId) -> Option<(u64, u64)> {
uplink::free_price_hint(id)
}
}

/// Expose `Metadata::read_owner()` to the host
Expand All @@ -61,15 +51,3 @@ unsafe fn read_id(arg_len: u32) -> u32 {
unsafe fn read_owner_of(arg_len: u32) -> u32 {
uplink::wrap_call(arg_len, |id| STATE.read_owner_of(id))
}

/// Expose `Metadata::read_free_limit_of()` to the host
#[no_mangle]
unsafe fn read_free_limit_of(arg_len: u32) -> u32 {
uplink::wrap_call(arg_len, |id| STATE.read_free_limit_of(id))
}

/// Expose `Metadata::read_free_price_hint_of()` to the host
#[no_mangle]
unsafe fn read_free_price_hint_of(arg_len: u32) -> u32 {
uplink::wrap_call(arg_len, |id| STATE.read_free_price_hint_of(id))
}
4 changes: 4 additions & 0 deletions piecrust-uplink/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Fix incomplete removal of economic protocol functionality

## [0.14.0] - 2024-06-26

### Removed
Expand Down
49 changes: 0 additions & 49 deletions piecrust-uplink/src/abi/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ use crate::{
SCRATCH_BUF_BYTES,
};

const LEN_U64: usize = core::mem::size_of::<u64>();

pub mod arg_buf {
use crate::ARGBUF_LEN;
use core::ptr;
Expand Down Expand Up @@ -63,8 +61,6 @@ mod ext {
pub fn limit() -> u64;
pub fn spent() -> u64;
pub fn owner(contract_id: *const u8) -> i32;
pub fn free_limit(contract_id: *const u8) -> i32;
pub fn free_price_hint(contract_id: *const u8) -> i32;
pub fn self_id();
}
}
Expand Down Expand Up @@ -260,51 +256,6 @@ pub fn owner<const N: usize>(contract: ContractId) -> Option<[u8; N]> {
}
}

/// Returns given contract's free limit, if the contract exists and if it
/// has a free limit set.
pub fn free_limit(contract: ContractId) -> Option<u64> {
let contract_id_ptr = contract.as_bytes().as_ptr();
unsafe {
match ext::free_limit(contract_id_ptr) {
0 => None,
_ => with_arg_buf(|buf| {
if buf[0] == 0 {
None
} else {
let mut value_bytes = [0; LEN_U64];
value_bytes.copy_from_slice(&buf[1..LEN_U64 + 1]);
Some(u64::from_le_bytes(value_bytes))
}
}),
}
}
}

/// Returns given contract's free gas price hint, if the contract exists and
/// if it has a free price hint set.
pub fn free_price_hint(contract: ContractId) -> Option<(u64, u64)> {
let contract_id_ptr = contract.as_bytes().as_ptr();

unsafe {
match ext::free_price_hint(contract_id_ptr) {
0 => None,
_ => with_arg_buf(|buf| {
if buf[0] == 0 {
None
} else {
let mut value_bytes = [0; LEN_U64];
value_bytes.copy_from_slice(&buf[1..LEN_U64 + 1]);
let num = u64::from_le_bytes(value_bytes);
value_bytes
.copy_from_slice(&buf[LEN_U64 + 1..LEN_U64 * 2 + 1]);
let denom = u64::from_le_bytes(value_bytes);
Some((num, denom))
}
}),
}
}
}

/// Returns the current contract's owner.
pub fn self_owner<const N: usize>() -> [u8; N] {
unsafe { ext::owner(ptr::null()) };
Expand Down
4 changes: 4 additions & 0 deletions piecrust/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Fix incomplete removal of economic protocol functionality

## [0.21.0] - 2024-06-26

### Removed
Expand Down
24 changes: 0 additions & 24 deletions piecrust/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ pub struct ContractData<'a, A> {
pub(crate) contract_id: Option<ContractId>,
pub(crate) constructor_arg: Option<&'a A>,
pub(crate) owner: Option<Vec<u8>>,
pub(crate) free_limit: Option<u64>,
pub(crate) free_price_hint: Option<(u64, u64)>,
}

// `()` is done on purpose, since by default it should be that the constructor
Expand All @@ -33,8 +31,6 @@ impl<'a> ContractData<'a, ()> {
contract_id: None,
constructor_arg: None,
owner: None,
free_limit: None,
free_price_hint: None,
}
}
}
Expand All @@ -49,8 +45,6 @@ pub struct ContractDataBuilder<'a, A> {
contract_id: Option<ContractId>,
owner: Option<Vec<u8>>,
constructor_arg: Option<&'a A>,
free_limit: Option<u64>,
free_price_hint: Option<(u64, u64)>,
}

impl<'a, A> ContractDataBuilder<'a, A> {
Expand All @@ -66,8 +60,6 @@ impl<'a, A> ContractDataBuilder<'a, A> {
contract_id: self.contract_id,
owner: self.owner,
constructor_arg: Some(arg),
free_limit: self.free_limit,
free_price_hint: self.free_price_hint,
}
}

Expand All @@ -77,25 +69,11 @@ impl<'a, A> ContractDataBuilder<'a, A> {
self
}

/// Set the gas limit for the the free execution of contract's methods.
pub fn free_limit(mut self, free_limit: u64) -> Self {
self.free_limit = Some(free_limit);
self
}

/// Set the gas price hint for the the free execution of contract's methods.
pub fn free_price_hint(mut self, price_hint: (u64, u64)) -> Self {
self.free_price_hint = Some(price_hint);
self
}

pub fn build(self) -> ContractData<'a, A> {
ContractData {
contract_id: self.contract_id,
constructor_arg: self.constructor_arg,
owner: self.owner,
free_limit: self.free_limit,
free_price_hint: self.free_price_hint,
}
}
}
Expand All @@ -105,8 +83,6 @@ impl<'a, A> ContractDataBuilder<'a, A> {
pub struct ContractMetadata {
pub contract_id: ContractId,
pub owner: Vec<u8>,
pub free_limit: Option<u64>,
pub free_price_hint: Option<(u64, u64)>,
}

#[derive(Clone)]
Expand Down
69 changes: 0 additions & 69 deletions piecrust/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ use crate::Error;

pub const GAS_PASS_PCT: u64 = 93;

const LEN_U64: usize = core::mem::size_of::<u64>();

pub(crate) struct Imports;

impl Imports {
Expand Down Expand Up @@ -80,14 +78,6 @@ impl Imports {
false => Func::wrap(store, wasm32::owner),
true => Func::wrap(store, wasm64::owner),
},
"free_limit" => match is_64 {
false => Func::wrap(store, wasm32::free_limit),
true => Func::wrap(store, wasm64::free_limit),
},
"free_price_hint" => match is_64 {
false => Func::wrap(store, wasm32::free_price_hint),
true => Func::wrap(store, wasm64::free_price_hint),
},
"self_id" => Func::wrap(store, self_id),
#[cfg(feature = "debug")]
"hdebug" => Func::wrap(store, hdebug),
Expand Down Expand Up @@ -475,65 +465,6 @@ fn owner(mut fenv: Caller<Env>, mod_id_ofs: usize) -> WasmtimeResult<i32> {
}
}

fn free_limit(mut fenv: Caller<Env>, mod_id_ofs: usize) -> WasmtimeResult<i32> {
let instance = fenv.data().self_instance();
check_ptr(instance, mod_id_ofs, CONTRACT_ID_BYTES)?;
let env = fenv.data_mut();
match get_metadata(env, mod_id_ofs) {
None => Ok(0),
Some(metadata) => {
let len = instance.with_arg_buf_mut(|arg| {
match metadata.free_limit {
Some(free_limit) => {
arg[0] = 1;
arg[1..LEN_U64 + 1]
.copy_from_slice(&free_limit.to_le_bytes()[..]);
}
_ => {
arg[0] = 0;
arg[1..LEN_U64 + 1].fill(0);
}
}
LEN_U64 + 1
});

Ok(len as i32)
}
}
}

fn free_price_hint(
mut fenv: Caller<Env>,
mod_id_ofs: usize,
) -> WasmtimeResult<i32> {
let instance = fenv.data().self_instance();
check_ptr(instance, mod_id_ofs, CONTRACT_ID_BYTES)?;
let env = fenv.data_mut();
match get_metadata(env, mod_id_ofs) {
None => Ok(0),
Some(metadata) => {
let len = instance.with_arg_buf_mut(|arg| {
match metadata.free_price_hint {
Some((num, denom)) => {
arg[0] = 1;
arg[1..LEN_U64 + 1]
.copy_from_slice(&num.to_le_bytes()[..]);
arg[LEN_U64 + 1..LEN_U64 * 2 + 1]
.copy_from_slice(&denom.to_le_bytes()[..]);
}
_ => {
arg[0] = 0;
arg[1..LEN_U64 * 2 + 1].fill(0);
}
}
LEN_U64 * 2 + 1
});

Ok(len as i32)
}
}
}

fn self_id(mut fenv: Caller<Env>) {
let env = fenv.data_mut();
let self_id = env.self_contract_id().to_owned();
Expand Down
14 changes: 0 additions & 14 deletions piecrust/src/imports/wasm32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,3 @@ pub(crate) fn emit(
pub(crate) fn owner(fenv: Caller<Env>, mod_id_ofs: u32) -> WasmtimeResult<i32> {
imports::owner(fenv, mod_id_ofs as usize)
}

pub(crate) fn free_limit(
fenv: Caller<Env>,
mod_id_ofs: u32,
) -> WasmtimeResult<i32> {
imports::free_limit(fenv, mod_id_ofs as usize)
}

pub(crate) fn free_price_hint(
fenv: Caller<Env>,
mod_id_ofs: u32,
) -> WasmtimeResult<i32> {
imports::free_price_hint(fenv, mod_id_ofs as usize)
}
14 changes: 0 additions & 14 deletions piecrust/src/imports/wasm64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,3 @@ pub(crate) fn emit(
pub(crate) fn owner(fenv: Caller<Env>, mod_id_ofs: u64) -> WasmtimeResult<i32> {
imports::owner(fenv, mod_id_ofs as usize)
}

pub(crate) fn free_limit(
fenv: Caller<Env>,
mod_id_ofs: u64,
) -> WasmtimeResult<i32> {
imports::free_limit(fenv, mod_id_ofs as usize)
}

pub(crate) fn free_price_hint(
fenv: Caller<Env>,
mod_id_ofs: u64,
) -> WasmtimeResult<i32> {
imports::free_price_hint(fenv, mod_id_ofs as usize)
}
11 changes: 1 addition & 10 deletions piecrust/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,6 @@ impl Session {
.owner
.expect("Owner must be specified when deploying a contract"),
gas_limit,
deploy_data.free_limit,
deploy_data.free_price_hint,
)?;

Ok(contract_id)
Expand All @@ -270,8 +268,6 @@ impl Session {
arg: Option<Vec<u8>>,
owner: Vec<u8>,
gas_limit: u64,
free_limit: Option<u64>,
free_price_hint: Option<(u64, u64)>,
) -> Result<(), Error> {
if self.inner.contract_session.contract_deployed(contract_id) {
return Err(InitalizationError(
Expand All @@ -281,12 +277,7 @@ impl Session {

let wrapped_contract =
WrappedContract::new(&self.engine, bytecode, None::<&[u8]>)?;
let contract_metadata = ContractMetadata {
contract_id,
owner,
free_limit,
free_price_hint,
};
let contract_metadata = ContractMetadata { contract_id, owner };
let metadata_bytes = Self::serialize_data(&contract_metadata)?;

self.inner
Expand Down
2 changes: 0 additions & 2 deletions piecrust/src/store/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,6 @@ impl ContractSession {
new_contract_data.metadata.set_data(ContractMetadata {
contract_id: old_contract,
owner: new_contract_data.metadata.data().owner.clone(),
free_limit: new_contract_data.metadata.data().free_limit,
free_price_hint: new_contract_data.metadata.data().free_price_hint,
})?;

self.contracts.insert(old_contract, new_contract_data);
Expand Down
Loading

0 comments on commit 2d32532

Please sign in to comment.