Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into sm/github-measurement
Browse files Browse the repository at this point in the history
  • Loading branch information
smiasojed committed Nov 6, 2023
2 parents 427f044 + 79d84e3 commit c2dde2b
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 39 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ older nodes, please do the following:
1. Mark the old `ink_env::function()` (which depends on the imported `[seal0]` function)
with the `#[deprecated]` attribute. Please, specify the `since` field with the ink!
version which will introduce the new API. We will leave this function in for backwards
compatibility purposes. Specifing some additional helpful info in the `note` field
compatibility purposes. Specifying some additional helpful info in the `note` field
could also be a good idea.
2. Name the new function (which depends on the `[seal1] function()`) in a descriptive
way, like `ink_env::function_whats_special()`.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ cfg-if = { version = "1.0" }
contract-build = { version = "3.2.0" }
darling = { version = "0.20.3" }
derive_more = { version = "0.99.17", default-features = false }
drink = { version = "=0.5.3" }
drink = { version = "=0.5.4" }
either = { version = "1.5", default-features = false }
funty = { version = "2.0.0" }
heck = { version = "0.4.0" }
Expand Down
4 changes: 2 additions & 2 deletions crates/e2e/macro/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ impl InkE2ETest {
let client_building = match self.test.config.backend() {
Backend::Full => build_full_client(&environment, exec_build_contracts),
#[cfg(any(test, feature = "drink"))]
Backend::RuntimeOnly => {
build_runtime_client(exec_build_contracts, self.test.config.runtime())
Backend::RuntimeOnly { runtime } => {
build_runtime_client(exec_build_contracts, runtime)
}
};

Expand Down
40 changes: 22 additions & 18 deletions crates/e2e/macro/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@
// limitations under the License.

/// The type of the architecture that should be used to run test.
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default, darling::FromMeta)]
#[derive(Clone, Eq, PartialEq, Debug, Default, darling::FromMeta)]
#[darling(rename_all = "snake_case")]
pub enum Backend {
/// The standard approach with running dedicated single-node blockchain in a
/// background process.
#[default]
Full,

/// The lightweight approach skipping node layer.
///
/// This runs a runtime emulator within `TestExternalities` (using drink! library) in
/// the same process as the test.
#[cfg(any(test, feature = "drink"))]
RuntimeOnly,
RuntimeOnly { runtime: Option<syn::Path> },
}

/// The End-to-End test configuration.
Expand All @@ -44,10 +46,6 @@ pub struct E2EConfig {
/// The type of the architecture that should be used to run test.
#[darling(default)]
backend: Backend,
/// The runtime to use for the runtime only test.
#[cfg(any(test, feature = "drink"))]
#[darling(default)]
runtime: Option<syn::Path>,
}

impl E2EConfig {
Expand All @@ -73,13 +71,7 @@ impl E2EConfig {

/// The type of the architecture that should be used to run test.
pub fn backend(&self) -> Backend {
self.backend
}

/// The runtime to use for the runtime only test.
#[cfg(any(test, feature = "drink"))]
pub fn runtime(&self) -> Option<syn::Path> {
self.runtime.clone()
self.backend.clone()
}
}

Expand All @@ -97,8 +89,7 @@ mod tests {
let input = quote! {
additional_contracts = "adder/Cargo.toml flipper/Cargo.toml",
environment = crate::CustomEnvironment,
backend = "runtime_only",
runtime = ::drink::MinimalRuntime,
backend(runtime_only()),
};
let config =
E2EConfig::from_list(&NestedMeta::parse_meta_list(input).unwrap()).unwrap();
Expand All @@ -111,10 +102,23 @@ mod tests {
config.environment(),
Some(syn::parse_quote! { crate::CustomEnvironment })
);
assert_eq!(config.backend(), Backend::RuntimeOnly);

assert_eq!(config.backend(), Backend::RuntimeOnly { runtime: None });
}

#[test]
fn config_works_with_custom_backend() {
let input = quote! {
backend(runtime_only(runtime = ::ink_e2e::MinimalRuntime)),
};
let config =
E2EConfig::from_list(&NestedMeta::parse_meta_list(input).unwrap()).unwrap();

assert_eq!(
config.runtime(),
Some(syn::parse_quote! { ::drink::MinimalRuntime })
config.backend(),
Backend::RuntimeOnly {
runtime: Some(syn::parse_quote! { ::ink_e2e::MinimalRuntime })
}
);
}
}
2 changes: 1 addition & 1 deletion crates/ink/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ pub fn test(attr: TokenStream, item: TokenStream) -> TokenStream {
/// #[ink(extension = 4, handle_status = false)]
/// fn access(key: &[u8]) -> Option<Access>;
///
/// /// Unlocks previously aquired permission to access key.
/// /// Unlocks previously acquired permission to access key.
/// ///
/// /// # Errors
/// ///
Expand Down
2 changes: 1 addition & 1 deletion crates/ink/tests/ui/chain_extension/E-01-simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub trait RuntimeReadWrite {
#[ink(extension = 4, handle_status = false)]
fn access(key: &[u8]) -> Option<Access>;

/// Unlocks previously aquired permission to access key.
/// Unlocks previously acquired permission to access key.
///
/// # Errors
///
Expand Down
39 changes: 30 additions & 9 deletions crates/storage/src/lazy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,25 @@ where

/// Try to read the `value` from the contract storage.
///
/// To successfully retrieve the `value`, the encoded `key` and `value`
/// must both fit into the static buffer together.
///
/// Returns:
/// - `Some(Ok(_))` if `value` was received from storage and could be decoded.
/// - `Some(Err(_))` if the encoded length of `value` exceeds the static buffer size.
/// - `Some(Err(_))` if retrieving the `value` would exceed the static buffer size.
/// - `None` if there was no value under this storage key.
pub fn try_get(&self) -> Option<ink_env::Result<V>> {
let encoded_length: usize = ink_env::contains_contract_storage(&KeyType::KEY)?
let key_size = <Key as Storable>::encoded_size(&KeyType::KEY);

if key_size >= ink_env::BUFFER_SIZE {
return Some(Err(ink_env::Error::BufferTooSmall))
}

let value_size: usize = ink_env::contains_contract_storage(&KeyType::KEY)?
.try_into()
.expect("targets of less than 32bit pointer size are not supported; qed");

if encoded_length > ink_env::BUFFER_SIZE {
if key_size.saturating_add(value_size) > ink_env::BUFFER_SIZE {
return Some(Err(ink_env::Error::BufferTooSmall))
}

Expand All @@ -175,9 +184,13 @@ where

/// Try to set the given `value` to the contract storage.
///
/// Fails if `value` exceeds the static buffer size.
/// To successfully store the `value`, the encoded `key` and `value`
/// must fit into the static buffer together.
pub fn try_set(&mut self, value: &V) -> ink_env::Result<()> {
if value.encoded_size() > ink_env::BUFFER_SIZE {
let key_size = <Key as Storable>::encoded_size(&KeyType::KEY);
let value_size = <V as Storable>::encoded_size(value);

if key_size.saturating_add(value_size) > ink_env::BUFFER_SIZE {
return Err(ink_env::Error::BufferTooSmall)
};

Expand Down Expand Up @@ -317,9 +330,13 @@ mod tests {
#[test]
fn fallible_storage_works_for_fitting_data() {
ink_env::test::run_test::<ink_env::DefaultEnvironment, _>(|_| {
let mut storage: Lazy<[u8; ink_env::BUFFER_SIZE]> = Lazy::new();
// The default `Key` is an 4 byte int
const KEY_SIZE: usize = 4;
const VALUE_SIZE: usize = ink_env::BUFFER_SIZE - KEY_SIZE;

let value = [0u8; ink_env::BUFFER_SIZE];
let mut storage: Lazy<[u8; VALUE_SIZE]> = Lazy::new();

let value = [0u8; VALUE_SIZE];
assert_eq!(storage.try_set(&value), Ok(()));
assert_eq!(storage.try_get(), Some(Ok(value)));

Expand All @@ -331,9 +348,13 @@ mod tests {
#[test]
fn fallible_storage_fails_gracefully_for_overgrown_data() {
ink_env::test::run_test::<ink_env::DefaultEnvironment, _>(|_| {
let mut storage: Lazy<[u8; ink_env::BUFFER_SIZE + 1]> = Lazy::new();
// The default `Key` is an 4 byte int
const KEY_SIZE: usize = 4;
const VALUE_SIZE: usize = ink_env::BUFFER_SIZE - KEY_SIZE + 1;

let mut storage: Lazy<[u8; VALUE_SIZE]> = Lazy::new();

let value = [0u8; ink_env::BUFFER_SIZE + 1];
let value = [0u8; VALUE_SIZE];
assert_eq!(storage.try_get(), None);
assert_eq!(storage.try_set(&value), Err(ink_env::Error::BufferTooSmall));

Expand Down
6 changes: 3 additions & 3 deletions integration-tests/e2e-runtime-only-backend/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub mod flipper {
/// - flip the flipper
/// - get the flipper's value
/// - assert that the value is `true`
#[ink_e2e::test(backend = "runtime_only")]
#[ink_e2e::test(backend(runtime_only()))]
async fn it_works<Client: E2EBackend>(mut client: Client) -> E2EResult<()> {
// given
const INITIAL_VALUE: bool = false;
Expand Down Expand Up @@ -88,7 +88,7 @@ pub mod flipper {
/// - transfer some funds to the contract using runtime call
/// - get the contract's balance again
/// - assert that the contract's balance increased by the transferred amount
#[ink_e2e::test(backend = "runtime_only")]
#[ink_e2e::test(backend(runtime_only()))]
async fn runtime_call_works() -> E2EResult<()> {
// given
let mut constructor = FlipperRef::new(false);
Expand Down Expand Up @@ -141,7 +141,7 @@ pub mod flipper {
}

/// Just instantiate a contract using non-default runtime.
#[ink_e2e::test(backend = "runtime_only", runtime = ink_e2e::MinimalRuntime)]
#[ink_e2e::test(backend(runtime_only(runtime = ink_e2e::MinimalRuntime)))]
async fn custom_runtime<Client: E2EBackend>(mut client: Client) -> E2EResult<()> {
client
.instantiate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub mod integration_flipper {
Self::new(Default::default())
}

/// Attemps to create a new integration_flipper smart contract initialized with
/// Attempts to create a new integration_flipper smart contract initialized with
/// the given value.
#[ink(constructor)]
pub fn try_new(succeed: bool) -> Result<Self, FlipperError> {
Expand Down
2 changes: 1 addition & 1 deletion linting/src/storage_never_freed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ fn find_collection_fields(cx: &LateContext, storage_struct_id: ItemId) -> Fields
result
}

/// Reports the given field defintion
/// Reports the given field definition
fn report_field(cx: &LateContext, field_info: &FieldInfo) {
if_chain! {
if let Node::Field(field) = cx.tcx.hir().get_by_def_id(field_info.did);
Expand Down
2 changes: 1 addition & 1 deletion linting/src/strict_balance_equality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ impl<'tcx> TransferFunction<'_, 'tcx> {
return
};

// Handle `PartialEq` functions that implement comparsion for non-primitive types,
// Handle `PartialEq` functions that implement comparison for non-primitive types,
// including references like `&i32`.
if_chain! {
if init_taints.len() == 2;
Expand Down

0 comments on commit c2dde2b

Please sign in to comment.