Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
3758e09
Remove fallible create_builder.rs methods
ascjones Jan 13, 2023
cdcc502
WIP experiment
ascjones Jan 14, 2023
6f59077
InstantiateResult blanket impl for T and Result
ascjones Jan 14, 2023
baecba9
Introduce ContractRef type parameter
ascjones Jan 14, 2023
af69cd5
Fix up env access
ascjones Jan 14, 2023
e134a8c
WIP...
ascjones Jan 14, 2023
115dd9f
Make it compile
ascjones Jan 15, 2023
718d475
Add ContractStorage parameter
ascjones Jan 16, 2023
bb5042e
Remove commented out instantiate_fallible_contract
ascjones Jan 16, 2023
528cfe0
Convert to env Error in helper
ascjones Jan 16, 2023
7b4c5e9
Return Decode errors in case of invalid Result first byte
ascjones Jan 16, 2023
4de38f5
Fix impls::instantiate_contract
ascjones Jan 16, 2023
09545fe
Remove ContractStorage generic parameter
ascjones Jan 17, 2023
2ef624d
Fix env access
ascjones Jan 17, 2023
02f97b1
Use generated constructor ref, introduces update_selector
ascjones Jan 17, 2023
39b45b0
Fix e2e
ascjones Jan 17, 2023
dc2f7bb
Remove commented out code
ascjones Jan 17, 2023
39ab13f
Typos
ascjones Jan 18, 2023
60e7e90
Clippy
ascjones Jan 18, 2023
43c2fe0
Rename some instantiate_fallible
ascjones Jan 18, 2023
bf3055c
Restore `returns` method
ascjones Jan 18, 2023
322ab26
Remove ContractReference Result impl
ascjones Jan 19, 2023
e61a12b
WIP implementing ConstructorReturnType
ascjones Jan 19, 2023
62052a6
Reorder ContractRef parameter, move ContractRef and ContractEnv trait…
ascjones Jan 19, 2023
2ed5093
Fmt and fix
ascjones Jan 19, 2023
df4a067
Remove E param from build_create
ascjones Jan 19, 2023
0e69431
Fix up build_create
ascjones Jan 20, 2023
e8feae9
Fix up e2e creat builder
ascjones Jan 20, 2023
2265258
Implement ContstructorReturnType for the storage_ident
ascjones Jan 20, 2023
10d7268
Fmt
ascjones Jan 20, 2023
ea65c47
Fix envaccess test
ascjones Jan 20, 2023
258c27b
Fully qualify Result in macro
ascjones Jan 20, 2023
08dc991
More fully qualify Result in macro
ascjones Jan 20, 2023
4be979d
Fix up build_create examples
ascjones Jan 20, 2023
0994c67
Add test for different combos of Self and struct name
ascjones Jan 20, 2023
f1f4930
Fix ui test
ascjones Jan 20, 2023
12f51dc
Fmt
ascjones Jan 20, 2023
1bceab3
Remove unused assoc type
ascjones Jan 20, 2023
8e0a9ef
Change error fn to return Option<Result>
ascjones Jan 20, 2023
aea1238
Remove commented out code
ascjones Jan 20, 2023
163fa6b
Fmt
ascjones Jan 20, 2023
1dd00d6
ConstructorReturnType comments
ascjones Jan 20, 2023
327967d
Fix `contract-ref` E2E test compilation
HCastano Jan 20, 2023
0bb7a8d
Fix up return types after merge
ascjones Jan 20, 2023
75ab4e6
Fmt
ascjones Jan 20, 2023
77a5bab
Clippy
ascjones Jan 20, 2023
7d03708
Fix create_builder tests
ascjones Jan 20, 2023
ca47caf
Fix cross-contract compile test
HCastano Jan 21, 2023
2cc8a22
Clean up some comments
HCastano Jan 21, 2023
fa0436a
Remove outdated doc
HCastano Jan 21, 2023
016b102
Update comment
HCastano Jan 21, 2023
9655072
Another comment fix
HCastano Jan 21, 2023
7aecb5f
Wrap long line
HCastano Jan 21, 2023
6f92033
Remove TODO
HCastano Jan 21, 2023
498c640
Bump `contract-metadata`
HCastano Jan 21, 2023
8e60ec2
Fix `CreateBuilder` compilation
HCastano Jan 24, 2023
a9c0223
Fix one of the doc tests
HCastano Jan 24, 2023
db1a808
Clean up doc tests a bit
HCastano Jan 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
WIP experiment
  • Loading branch information
ascjones authored and HCastano committed Jan 24, 2023
commit cdcc50201553f1c93e026af8da792b9a322ee67d
5 changes: 4 additions & 1 deletion crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use crate::{
CallParams,
CreateParams,
DelegateCall,
FromAccountId,
utils::ConstructorOutputValue,
},
engine::{
EnvInstance,
Expand Down Expand Up @@ -325,11 +327,12 @@ where
/// - If the returned account ID failed to decode properly.
pub fn instantiate_contract<E, Args, Salt, R>(
params: &CreateParams<E, Args, Salt, R>,
) -> Result<ink_primitives::ConstructorResult<E::AccountId>>
) -> Result<ink_primitives::ConstructorResult<ConstructorOutputValue<R, E>>>
where
E: Environment,
Args: scale::Encode,
Salt: AsRef<[u8]>,
R: FromAccountId<E>,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::instantiate_contract::<E, Args, Salt, R>(instance, params)
Expand Down
10 changes: 8 additions & 2 deletions crates/env/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ use crate::{
Call,
CallParams,
CreateParams,
FromAccountId,
DelegateCall,
utils::{
ConstructorOutput,
ConstructorOutputValue,
}
},
hash::{
CryptoHash,
Expand Down Expand Up @@ -442,11 +447,12 @@ pub trait TypedEnvBackend: EnvBackend {
fn instantiate_contract<E, Args, Salt, R>(
&mut self,
params: &CreateParams<E, Args, Salt, R>,
) -> Result<ink_primitives::ConstructorResult<E::AccountId>>
) -> Result<ink_primitives::ConstructorResult<ConstructorOutputValue<R, E>>>
where
E: Environment,
Args: scale::Encode,
Salt: AsRef<[u8]>;
Salt: AsRef<[u8]>,
R: FromAccountId<E>,;

/// Attempts to instantiate another contract, returning the instantiation result back to the
/// caller.
Expand Down
96 changes: 96 additions & 0 deletions crates/env/src/call/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
//! Utilities, types and abstractions common to call and instantiation routines.

use core::marker::PhantomData;
use crate::call::FromAccountId;
use crate::Environment;

/// Represents a return type.
///
Expand All @@ -38,6 +40,100 @@ impl<T> Default for ReturnType<T> {
}
}

mod private {
/// Seals the implementation of `ConstructorReturnType`.
pub trait Sealed {}
}

/// Guards against using invalid contract initializer types.
///
/// # Note
///
/// Currently the only allowed types are `()` and `Result<(), E>`
/// where `E` is some unspecified error type.
/// If the contract initializer returns `Result::Err` the utility
/// method that is used to initialize an ink! smart contract will
/// revert the state of the contract instantiation.
pub trait ConstructorOutput<C, Env: Environment>: private::Sealed {
/// Is `true` if `Self` is `Result<C, E>`.
const IS_RESULT: bool = false;

/// Reflects the output type of the dispatchable ink! constructor.
type Output;

/// The error type of the constructor return type.
///
/// # Note
///
/// For infallible constructors this is `()` whereas for fallible
/// constructors this is the actual return error type. Since we only ever
/// return a value in case of `Result::Err` the `Result::Ok` value type
/// does not matter.
type Error;

// todo: docs
fn from_account_id(account_id: Env::AccountId) -> Self::Output;

/// Converts the return value into a `Result` instance.
///
/// # Note
///
/// For infallible constructor returns this always yields `Ok`.
fn as_result(&self) -> Result<&C, &Self::Error>;
}

/// todo: comment
pub struct ConstructorOutputValue<T, Env: Environment>(T, PhantomData<Env>);

impl<T, Env> ConstructorOutputValue<T, Env>
where
Env: Environment,
{
pub fn new(val: T) -> Self {
Self(val, PhantomData)
}
}

impl<T, Env: Environment> private::Sealed for ConstructorOutputValue<T, Env> {}

impl<C, Env> ConstructorOutput<C, Env> for ConstructorOutputValue<C, Env>
where
C: FromAccountId<Env>,
Env: Environment,
{
type Output = C;
type Error = &'static ();

fn from_account_id(account_id: Env::AccountId) -> Self::Output {
C::from_account_id(account_id)
}

#[inline(always)]
fn as_result(&self) -> Result<&C, &Self::Error> {
Ok(&self.0)
}
}

impl<C, E, Env> ConstructorOutput<C, Env> for ConstructorOutputValue<Result<C, E>, Env>
where
C: FromAccountId<Env>,
Env: Environment,
{
const IS_RESULT: bool = true;

type Output = Result<C, E>;
type Error = E;

fn from_account_id(account_id: Env::AccountId) -> Self::Output {
Ok(C::from_account_id(account_id))
}

#[inline(always)]
fn as_result(&self) -> Result<&C, &Self::Error> {
self.0.as_ref()
}
}

/// A parameter that has been set to some value.
#[derive(Debug, Copy, Clone)]
pub struct Set<T>(pub T);
Expand Down
3 changes: 2 additions & 1 deletion crates/env/src/call/create_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use crate::{
call::{
utils::{
ConstructorOutput,
ConstructorOutputValue,
EmptyArgumentList,
ReturnType,
Set,
Expand Down Expand Up @@ -152,7 +154,6 @@ where
&self,
) -> Result<ink_primitives::ConstructorResult<R>, crate::Error> {
crate::instantiate_contract(self)
.map(|inner| inner.map(FromAccountId::from_account_id))
}
}

Expand Down
2 changes: 2 additions & 0 deletions crates/env/src/call/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ mod selector;
pub mod utils {
pub use super::{
common::{
ConstructorOutput,
ConstructorOutputValue,
ReturnType,
Set,
Unset,
Expand Down
8 changes: 7 additions & 1 deletion crates/env/src/engine/off_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ use crate::{
Call,
CallParams,
CreateParams,
FromAccountId,
DelegateCall,
utils::{
ConstructorOutput,
ConstructorOutputValue,
}
},
hash::{
Blake2x128,
Expand Down Expand Up @@ -472,11 +477,12 @@ impl TypedEnvBackend for EnvInstance {
fn instantiate_contract<E, Args, Salt, R>(
&mut self,
params: &CreateParams<E, Args, Salt, R>,
) -> Result<ink_primitives::ConstructorResult<E::AccountId>>
) -> Result<ink_primitives::ConstructorResult<ConstructorOutputValue<R, E>>>
where
E: Environment,
Args: scale::Encode,
Salt: AsRef<[u8]>,
R: FromAccountId<E>,
{
let _code_hash = params.code_hash();
let _gas_limit = params.gas_limit();
Expand Down
21 changes: 14 additions & 7 deletions crates/env/src/engine/on_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ use crate::{
CallParams,
CreateParams,
DelegateCall,
utils::{
ConstructorOutput,
ConstructorOutputValue,
}
},
hash::{
Blake2x128,
Expand All @@ -41,6 +45,7 @@ use crate::{
EnvBackend,
Environment,
Error,
FromAccountId,
FromLittleEndian,
Result,
ReturnFlags,
Expand Down Expand Up @@ -482,11 +487,12 @@ impl TypedEnvBackend for EnvInstance {
fn instantiate_contract<E, Args, Salt, R>(
&mut self,
params: &CreateParams<E, Args, Salt, R>,
) -> Result<ink_primitives::ConstructorResult<E::AccountId>>
) -> Result<ink_primitives::ConstructorResult<ConstructorOutputValue<R>>>
where
E: Environment,
Args: scale::Encode,
Salt: AsRef<[u8]>,
R: FromAccountId<E>,
{
let mut scoped = self.scoped_buffer();
let gas_limit = params.gas_limit();
Expand All @@ -513,17 +519,18 @@ impl TypedEnvBackend for EnvInstance {
match instantiate_result {
Ok(()) => {
let account_id = scale::Decode::decode(&mut &out_address[..])?;
Ok(Ok(account_id))
Ok(Ok(<R as FromAccountId<E>>::from_account_id(account_id)))
}
Err(ext::Error::CalleeReverted) => {
// We don't wrap manually with an extra `Err` like we do in the `Ok` case since the
// buffer already comes back in the form of `Err(LangError)` (assuming it's encoded
// by the ink! codegen and not the contract).
let out = ink_primitives::ConstructorResult::<E::AccountId>::decode(
&mut &out_return_value[..],
)?;
assert!(out.is_err(), "The callee reverted, but did not encode an error in the output buffer.");
Ok(out)
// let out = ink_primitives::ConstructorResult::<E::AccountId>::decode(
// &mut &out_return_value[..],
// )?;
// assert!(out.is_err(), "The callee reverted, but did not encode an error in the output buffer.");
// Ok(out)
todo!("custom decoding based on ConstructorOutputValue<R>")
}
Err(actual_error) => Err(actual_error.into()),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,8 @@ impl ContractRef<'_> {
::ink::env::call::utils::Unset<Balance>,
::ink::env::call::utils::Set<::ink::env::call::ExecutionInput<#arg_list>>,
::ink::env::call::utils::Unset<::ink::env::call::state::Salt>,
::ink::env::call::utils::Set<::ink::env::call::utils::ReturnType<#ret_type>>,
::ink::env::call::utils::Set<::ink::env::call::utils::ConstructorOutputValue<#ret_type>>,
Self,
> {
::ink::env::call::build_create::<Environment>()
.exec_input(
Expand All @@ -431,7 +432,6 @@ impl ContractRef<'_> {
.push_arg(#input_bindings)
)*
)
.returns::<#ret_type>()
}
)
}
Expand Down