From bfb890f2f1c5dcc1131128f343df3977b424e7a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Leegwater=20Sim=C3=B5es?= Date: Mon, 1 Jul 2024 12:18:51 +0200 Subject: [PATCH 1/3] uplink: fix removal of economic protocol --- piecrust-uplink/CHANGELOG.md | 4 +++ piecrust-uplink/src/abi/state.rs | 49 -------------------------------- 2 files changed, 4 insertions(+), 49 deletions(-) diff --git a/piecrust-uplink/CHANGELOG.md b/piecrust-uplink/CHANGELOG.md index 07941c85..d5a4da5f 100644 --- a/piecrust-uplink/CHANGELOG.md +++ b/piecrust-uplink/CHANGELOG.md @@ -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 diff --git a/piecrust-uplink/src/abi/state.rs b/piecrust-uplink/src/abi/state.rs index f5ef14a3..a0b1251c 100644 --- a/piecrust-uplink/src/abi/state.rs +++ b/piecrust-uplink/src/abi/state.rs @@ -19,8 +19,6 @@ use crate::{ SCRATCH_BUF_BYTES, }; -const LEN_U64: usize = core::mem::size_of::(); - pub mod arg_buf { use crate::ARGBUF_LEN; use core::ptr; @@ -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(); } } @@ -260,51 +256,6 @@ pub fn owner(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 { - 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() -> [u8; N] { unsafe { ext::owner(ptr::null()) }; From 1fd24ab8542f8ec07464af42167e53a21f5d08ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Leegwater=20Sim=C3=B5es?= Date: Mon, 1 Jul 2024 12:20:38 +0200 Subject: [PATCH 2/3] piecrust: fix incomplete economic protocol removal --- piecrust/CHANGELOG.md | 4 ++ piecrust/src/contract.rs | 24 ------- piecrust/src/imports.rs | 69 ------------------ piecrust/src/imports/wasm32.rs | 14 ---- piecrust/src/imports/wasm64.rs | 14 ---- piecrust/src/session.rs | 11 +-- piecrust/src/store/session.rs | 2 - piecrust/tests/metadata.rs | 128 --------------------------------- 8 files changed, 5 insertions(+), 261 deletions(-) diff --git a/piecrust/CHANGELOG.md b/piecrust/CHANGELOG.md index 20378d99..42bd34ba 100644 --- a/piecrust/CHANGELOG.md +++ b/piecrust/CHANGELOG.md @@ -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 diff --git a/piecrust/src/contract.rs b/piecrust/src/contract.rs index 49306bdc..ab7d4824 100644 --- a/piecrust/src/contract.rs +++ b/piecrust/src/contract.rs @@ -17,8 +17,6 @@ pub struct ContractData<'a, A> { pub(crate) contract_id: Option, pub(crate) constructor_arg: Option<&'a A>, pub(crate) owner: Option>, - pub(crate) free_limit: Option, - pub(crate) free_price_hint: Option<(u64, u64)>, } // `()` is done on purpose, since by default it should be that the constructor @@ -33,8 +31,6 @@ impl<'a> ContractData<'a, ()> { contract_id: None, constructor_arg: None, owner: None, - free_limit: None, - free_price_hint: None, } } } @@ -49,8 +45,6 @@ pub struct ContractDataBuilder<'a, A> { contract_id: Option, owner: Option>, constructor_arg: Option<&'a A>, - free_limit: Option, - free_price_hint: Option<(u64, u64)>, } impl<'a, A> ContractDataBuilder<'a, A> { @@ -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, } } @@ -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, } } } @@ -105,8 +83,6 @@ impl<'a, A> ContractDataBuilder<'a, A> { pub struct ContractMetadata { pub contract_id: ContractId, pub owner: Vec, - pub free_limit: Option, - pub free_price_hint: Option<(u64, u64)>, } #[derive(Clone)] diff --git a/piecrust/src/imports.rs b/piecrust/src/imports.rs index 90474f26..41481d00 100644 --- a/piecrust/src/imports.rs +++ b/piecrust/src/imports.rs @@ -23,8 +23,6 @@ use crate::Error; pub const GAS_PASS_PCT: u64 = 93; -const LEN_U64: usize = core::mem::size_of::(); - pub(crate) struct Imports; impl Imports { @@ -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), @@ -475,65 +465,6 @@ fn owner(mut fenv: Caller, mod_id_ofs: usize) -> WasmtimeResult { } } -fn free_limit(mut fenv: Caller, mod_id_ofs: usize) -> WasmtimeResult { - 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, - mod_id_ofs: usize, -) -> WasmtimeResult { - 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) { let env = fenv.data_mut(); let self_id = env.self_contract_id().to_owned(); diff --git a/piecrust/src/imports/wasm32.rs b/piecrust/src/imports/wasm32.rs index 98f22d4b..4963f461 100644 --- a/piecrust/src/imports/wasm32.rs +++ b/piecrust/src/imports/wasm32.rs @@ -56,17 +56,3 @@ pub(crate) fn emit( pub(crate) fn owner(fenv: Caller, mod_id_ofs: u32) -> WasmtimeResult { imports::owner(fenv, mod_id_ofs as usize) } - -pub(crate) fn free_limit( - fenv: Caller, - mod_id_ofs: u32, -) -> WasmtimeResult { - imports::free_limit(fenv, mod_id_ofs as usize) -} - -pub(crate) fn free_price_hint( - fenv: Caller, - mod_id_ofs: u32, -) -> WasmtimeResult { - imports::free_price_hint(fenv, mod_id_ofs as usize) -} diff --git a/piecrust/src/imports/wasm64.rs b/piecrust/src/imports/wasm64.rs index 76ea610e..06f729db 100644 --- a/piecrust/src/imports/wasm64.rs +++ b/piecrust/src/imports/wasm64.rs @@ -56,17 +56,3 @@ pub(crate) fn emit( pub(crate) fn owner(fenv: Caller, mod_id_ofs: u64) -> WasmtimeResult { imports::owner(fenv, mod_id_ofs as usize) } - -pub(crate) fn free_limit( - fenv: Caller, - mod_id_ofs: u64, -) -> WasmtimeResult { - imports::free_limit(fenv, mod_id_ofs as usize) -} - -pub(crate) fn free_price_hint( - fenv: Caller, - mod_id_ofs: u64, -) -> WasmtimeResult { - imports::free_price_hint(fenv, mod_id_ofs as usize) -} diff --git a/piecrust/src/session.rs b/piecrust/src/session.rs index 4b769d68..b9e32137 100644 --- a/piecrust/src/session.rs +++ b/piecrust/src/session.rs @@ -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) @@ -270,8 +268,6 @@ impl Session { arg: Option>, owner: Vec, gas_limit: u64, - free_limit: Option, - free_price_hint: Option<(u64, u64)>, ) -> Result<(), Error> { if self.inner.contract_session.contract_deployed(contract_id) { return Err(InitalizationError( @@ -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 diff --git a/piecrust/src/store/session.rs b/piecrust/src/store/session.rs index 5c454b2d..d8c506cd 100644 --- a/piecrust/src/store/session.rs +++ b/piecrust/src/store/session.rs @@ -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); diff --git a/piecrust/tests/metadata.rs b/piecrust/tests/metadata.rs index 2b33f44a..1c70e02b 100644 --- a/piecrust/tests/metadata.rs +++ b/piecrust/tests/metadata.rs @@ -123,131 +123,3 @@ fn owner_of() -> Result<(), Error> { Ok(()) } - -#[test] -fn free_limit_and_price_hint_of() -> Result<(), Error> { - const OWNER_0: [u8; 33] = [3u8; 33]; - const OWNER_1: [u8; 33] = [4u8; 33]; - - const EXPECTED_FREE_LIMIT_0: u64 = 10_000_000; - const EXPECTED_FREE_LIMIT_1: u64 = 20_000_000; - const EXPECTED_FREE_PRICE_HINT_0: (u64, u64) = (2, 1); - const EXPECTED_FREE_PRICE_HINT_1: (u64, u64) = (3, 1); - - const CONTRACT_ID_0: ContractId = ContractId::from_bytes([1; 32]); - const CONTRACT_ID_1: ContractId = ContractId::from_bytes([2; 32]); - const CONTRACT_ID_2: ContractId = ContractId::from_bytes([3; 32]); - - let vm = VM::ephemeral()?; - - let mut session = vm.session(SessionData::builder())?; - - session.deploy( - contract_bytecode!("metadata"), - ContractData::builder() - .owner(OWNER_0) - .contract_id(CONTRACT_ID_0) - .free_limit(EXPECTED_FREE_LIMIT_0) - .free_price_hint(EXPECTED_FREE_PRICE_HINT_0), - LIMIT, - )?; - session.deploy( - contract_bytecode!("metadata"), - ContractData::builder() - .owner(OWNER_1) - .contract_id(CONTRACT_ID_1) - .free_limit(EXPECTED_FREE_LIMIT_1) - .free_price_hint(EXPECTED_FREE_PRICE_HINT_1), - LIMIT, - )?; - - let free_limit = session - .call::<_, Option>( - CONTRACT_ID_0, - "read_free_limit_of", - &CONTRACT_ID_1, - LIMIT, - )? - .data; - - assert_eq!( - free_limit, - Some(EXPECTED_FREE_LIMIT_1), - "contract 0 should think that contract 1 has the correct free limit" - ); - - let free_limit = session - .call::<_, Option>( - CONTRACT_ID_1, - "read_free_limit_of", - &CONTRACT_ID_0, - LIMIT, - )? - .data; - - assert_eq!( - free_limit, - Some(EXPECTED_FREE_LIMIT_0), - "contract 1 should think that contract 0 has the correct free limit" - ); - - let free_limit = session - .call::<_, Option>( - CONTRACT_ID_0, - "read_free_limit_of", - &CONTRACT_ID_2, - LIMIT, - )? - .data; - - assert_eq!( - free_limit, - None, - "contract 0 should think that the free_limit of a non-existing contract is None" ); - - let free_price_hint = session - .call::<_, Option<(u64, u64)>>( - CONTRACT_ID_0, - "read_free_price_hint_of", - &CONTRACT_ID_1, - LIMIT, - )? - .data; - - assert_eq!( - free_price_hint, - Some(EXPECTED_FREE_PRICE_HINT_1), - "contract 0 should think that contract 1 has the correct free price hint" ); - - let free_price_hint = session - .call::<_, Option<(u64, u64)>>( - CONTRACT_ID_1, - "read_free_price_hint_of", - &CONTRACT_ID_0, - LIMIT, - )? - .data; - - assert_eq!( - free_price_hint, - Some(EXPECTED_FREE_PRICE_HINT_0), - "contract 1 should think that contract 0 has the correct free price - hint" - ); - - let free_price_hint = session - .call::<_, Option<(u64, u64)>>( - CONTRACT_ID_0, - "read_free_price_hint_of", - &CONTRACT_ID_2, - LIMIT, - )? - .data; - - assert_eq!( - free_price_hint, - None, - "contract 0 should think that the free price hint of a non-existing contract is none" ); - - Ok(()) -} From d2fbb4318a47f19169de651dd9e0ffb656b36da7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Leegwater=20Sim=C3=B5es?= Date: Mon, 1 Jul 2024 12:21:02 +0200 Subject: [PATCH 3/3] contracts: remove `free_limit` and `free_price_hint` --- contracts/metadata/src/lib.rs | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/contracts/metadata/src/lib.rs b/contracts/metadata/src/lib.rs index 0b35b2a2..162dfcb4 100644 --- a/contracts/metadata/src/lib.rs +++ b/contracts/metadata/src/lib.rs @@ -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 { - 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 @@ -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)) -}