Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle LangError from instantiate #1512

Merged
merged 90 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
90 commits
Select commit Hold shift + click to select a range
8796a62
Handle `LangError` from instantiate (fails for success case)
HCastano Nov 19, 2022
95b80c4
Change generic in `CreateBuilder` to be more consistent
HCastano Nov 25, 2022
a49fcb7
Remove extra generic parameter
HCastano Nov 25, 2022
8312fa8
Remove generic return type parameter from `CreateBuidler` codegen
HCastano Nov 25, 2022
6e70be0
Hardcode assumption that `instantiate` returns a `ConstructorResult`
HCastano Nov 25, 2022
2fd4a82
Update `CreateBuilder` codegen to just return `Self`
HCastano Nov 25, 2022
4d1f2bf
Remove generic usage to fix formatting
HCastano Nov 28, 2022
f107cb1
Unwrap `ConstructorResult` in `contract-ref` E2E test
HCastano Nov 28, 2022
b6de9fc
Clean up some comments
HCastano Nov 28, 2022
5d221c3
Bring back the assumption that we expect an `AccountId`
HCastano Nov 28, 2022
7d94f75
Remove unused method
HCastano Nov 28, 2022
798f946
Update doc tests for new builder pattern
HCastano Nov 28, 2022
18d0922
Clean up some comments
HCastano Nov 28, 2022
06bba81
Fix Clippy warning
HCastano Nov 28, 2022
612537e
Fix typo
HCastano Nov 28, 2022
2175174
Add `try_instantiate` method to `CreateBuilder`
HCastano Nov 28, 2022
44a8324
Remove unneeded `unwrap`
HCastano Nov 29, 2022
84d257d
Remove debug logging
HCastano Nov 29, 2022
ffcca2c
Update doc test
HCastano Nov 29, 2022
91cd98f
Fix some typos
HCastano Nov 29, 2022
e701230
Mention panicking behaviour of `instantiate` methods
HCastano Nov 29, 2022
f2d85dd
Improve error messages from wrong `returns()` type
HCastano Nov 29, 2022
addcda2
Actually check return values from `call_instantiate`
HCastano Nov 29, 2022
4c99daf
Add test showing a reverting constructor with `Ok` in error buffer
HCastano Nov 29, 2022
99c859b
Check that we're only returning `LangError`s if the contract reverted
HCastano Nov 29, 2022
323ed2d
Clean up the manual encoding test a bit
HCastano Nov 30, 2022
b6fc625
Add test for constructors which return a contract level error
HCastano Dec 1, 2022
ac6b197
Add `CreateBuilder` message which calls a fallible constructor
HCastano Dec 3, 2022
b06a9f8
Add test which calls falliable constructor for success case
HCastano Dec 5, 2022
7d664a0
Get verbose `instantiate_contract_with_result` decoding past typechecker
HCastano Dec 5, 2022
f874c97
Add `try_instantiate_with_result` to `CreateBuilder`
HCastano Dec 5, 2022
c29895a
Clean up decoding logic for output from `seal_instatiate`
HCastano Dec 5, 2022
dbb079e
Small cleanups in `call-builder` E2E tests
HCastano Dec 5, 2022
6125690
RustFmt `env_access`
HCastano Dec 5, 2022
aa4f897
Remove unused import
HCastano Dec 5, 2022
053218f
Flip decoding logic so that it's more strict initially
HCastano Dec 6, 2022
77c790f
Add test which revert a fallible constructor
HCastano Dec 6, 2022
713ad1e
Remove note about removing `assert` statement
HCastano Dec 6, 2022
64a711c
Check return value from fallible constructor tests
HCastano Dec 6, 2022
54fcf90
Merge branch 'master' into hc-get-lang-error-from-create-builder
HCastano Dec 6, 2022
c17fc3b
Update E2E Builder typedef to match changes
HCastano Dec 6, 2022
32f4a75
Update E2E test for new call syntax
HCastano Dec 6, 2022
a3cfbb8
Use `selector_bytes!` macro in more places
HCastano Dec 6, 2022
283e182
Change order to accounts used in tests
HCastano Dec 6, 2022
cc98baf
Update function names to use `fallible`
HCastano Dec 6, 2022
f01b5ca
Add note about docs
HCastano Dec 6, 2022
c794ec9
Merge branch 'master' into hc-get-lang-error-from-create-builder
HCastano Dec 7, 2022
b4290cb
Update `ContractRef` codegen to use fallible constructor return types
HCastano Dec 7, 2022
d47bf8f
Stop returning an `AccountId` directly from `CreateBuilder::try_insta…
HCastano Dec 7, 2022
b720ede
Add panicking version of `try_instantiate_fallible`
HCastano Dec 7, 2022
75cad9e
Add test for using fallible constructors through ContractRefs
HCastano Dec 7, 2022
cf2516f
Add test for instantiation failure too
HCastano Dec 7, 2022
f9b3004
Add `instantiate_fallible` to `CreateParams`
HCastano Dec 7, 2022
d511b3f
Add a couple of missing docs
HCastano Dec 7, 2022
550b728
Convert `call-builder` test return type to `AccountId`
HCastano Dec 7, 2022
1668ae0
Extract reverted fallible constructor fn for testing
ascjones Dec 8, 2022
acf23b9
Fmt
ascjones Dec 8, 2022
e69e7ce
Add fallible_constructor_reverted_lang_error FAILs
ascjones Dec 8, 2022
f084763
Rename tests
ascjones Dec 8, 2022
74f2392
Add test for a decode error
ascjones Dec 8, 2022
e3a99f9
Rename some tests
ascjones Dec 8, 2022
56e8d52
Make `Result` types more explicit
HCastano Dec 8, 2022
50c227b
Add another test
HCastano Dec 8, 2022
8ce1d92
Clean up decoding match statement
HCastano Dec 8, 2022
c291b3c
Andrew was right
HCastano Dec 9, 2022
f0561e5
Small cleanups to naming and imports
HCastano Dec 11, 2022
1742a01
Couple more import and comment fixes
HCastano Dec 11, 2022
25d8326
Use decode trait method directly
HCastano Dec 11, 2022
24e9424
Fix `call-builder` E2E tests
HCastano Dec 11, 2022
e7bba67
Remove leading colons from non-codegen contexts
HCastano Dec 11, 2022
ebd42f3
Add doc test for `instantiate_fallible_contract`
HCastano Dec 11, 2022
0a99f16
Add doc test to `build_create` function
HCastano Dec 11, 2022
6cf60e6
Remove leftover trait bound
HCastano Dec 11, 2022
581291b
Remove a few more leading colons
HCastano Dec 11, 2022
95f9e6e
Panic in case where we get `Ok` encoded into error buffer
HCastano Dec 12, 2022
0d40825
Add some links to env docs
HCastano Dec 12, 2022
c8e5cfa
Add more docs to `call-builder` E2E tests
HCastano Dec 12, 2022
1e323f4
Use correct path in `call-builder` docs
HCastano Dec 12, 2022
0b31392
Merge branch 'master' into hc-get-lang-error-from-create-builder
ascjones Jan 18, 2023
9a14e21
Use return_value() method in e2e test
ascjones Jan 18, 2023
69946de
Merge branch 'master' into hc-get-lang-error-from-create-builder
HCastano Jan 20, 2023
37d8e80
Fix `call-builder` E2E test compilation
HCastano Jan 20, 2023
0c568b5
Fix `contract-ref` E2E test compilation
HCastano Jan 20, 2023
7d20051
Fix some of the comment links
HCastano Jan 21, 2023
eeb7b85
Unwrap errors from default `instantiate_fallible` codepath
HCastano Jan 21, 2023
658fed1
Fix `contract-ref` E2E test
HCastano Jan 21, 2023
aac62db
Wrap long line
HCastano Jan 21, 2023
619cc71
Remove TODO
HCastano Jan 21, 2023
dd41485
Fix instatiation doc test
HCastano Jan 21, 2023
b41ca87
Merge branch 'master' into hc-get-lang-error-from-create-builder
HCastano Jan 21, 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
2 changes: 1 addition & 1 deletion crates/e2e/src/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub type CreateBuilderPartial<E, Args, R> = CreateBuilder<
Unset<<E as Environment>::Balance>,
Set<ExecutionInput<Args>>,
Unset<ink_env::call::state::Salt>,
R,
Set<ReturnType<R>>,
>;

/// Get the encoded constructor arguments from the partially initialized `CreateBuilder`
Expand Down
48 changes: 43 additions & 5 deletions crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,40 @@ where
/// # Note
///
/// This is a low level way to instantiate another smart contract.
/// Prefer to use the ink! guided and type safe approach to using this.
///
/// Prefer to use methods on a `ContractRef` or the [`CreateBuilder`](`crate::call::CreateBuilder`)
/// through [`build_create`](`crate::call::build_create`) instead.
///
/// # Errors
///
/// - If the code hash is invalid.
/// - If the arguments passed to the instantiation process are invalid.
/// - If the instantiation process traps.
/// - If the instantiation process runs out of gas.
/// - If given insufficient endowment.
/// - 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>>
where
E: Environment,
Args: scale::Encode,
Salt: AsRef<[u8]>,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::instantiate_contract::<E, Args, Salt, R>(instance, params)
})
}

/// Attempts to instantiate another contract, returning the instantiation result back to the
/// caller.
///
/// # Note
///
/// This is a low level way to instantiate another smart contract.
///
/// Prefer to use methods on a `ContractRef` or the [`CreateBuilder`](`crate::call::CreateBuilder`)
/// through [`build_create`](`crate::call::build_create`) instead.
///
/// # Errors
///
Expand All @@ -321,16 +354,21 @@ where
/// - If the instantiation process runs out of gas.
/// - If given insufficient endowment.
/// - If the returned account ID failed to decode properly.
pub fn instantiate_contract<E, Args, Salt, C>(
params: &CreateParams<E, Args, Salt, C>,
) -> Result<E::AccountId>
pub fn instantiate_fallible_contract<E, Args, Salt, R, ContractError>(
params: &CreateParams<E, Args, Salt, R>,
) -> Result<
ink_primitives::ConstructorResult<core::result::Result<E::AccountId, ContractError>>,
>
where
E: Environment,
Args: scale::Encode,
Salt: AsRef<[u8]>,
ContractError: scale::Decode,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::instantiate_contract::<E, Args, Salt, C>(instance, params)
TypedEnvBackend::instantiate_fallible_contract::<E, Args, Salt, R, ContractError>(
instance, params,
)
})
}

Expand Down
26 changes: 23 additions & 3 deletions crates/env/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,15 +439,35 @@ pub trait TypedEnvBackend: EnvBackend {
/// # Note
///
/// For more details visit: [`instantiate_contract`][`crate::instantiate_contract`]
fn instantiate_contract<E, Args, Salt, C>(
fn instantiate_contract<E, Args, Salt, R>(
&mut self,
params: &CreateParams<E, Args, Salt, C>,
) -> Result<E::AccountId>
params: &CreateParams<E, Args, Salt, R>,
) -> Result<ink_primitives::ConstructorResult<E::AccountId>>
where
E: Environment,
Args: scale::Encode,
Salt: AsRef<[u8]>;

/// Attempts to instantiate another contract, returning the instantiation result back to the
/// caller.
///
/// # Note
///
/// For more details visit: [`instantiate_fallible_contract`][`crate::instantiate_fallible_contract`]
fn instantiate_fallible_contract<E, Args, Salt, R, ContractError>(
&mut self,
params: &CreateParams<E, Args, Salt, R>,
) -> Result<
ink_primitives::ConstructorResult<
core::result::Result<E::AccountId, ContractError>,
>,
>
where
E: Environment,
Args: scale::Encode,
Salt: AsRef<[u8]>,
ContractError: scale::Decode;

/// Terminates a smart contract.
///
/// # Note
Expand Down
11 changes: 7 additions & 4 deletions crates/env/src/call/call_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ where
/// # Note
///
/// On failure this returns an outer [`ink::env::Error`][`crate::Error`] or inner
/// [`ink_primitives::LangError`], both of which can be handled by the caller.
/// [`ink::primitives::LangError`][`ink_primitives::LangError`], both of which can be handled
/// by the caller.
pub fn try_invoke(&self) -> Result<ink_primitives::MessageResult<R>, crate::Error> {
crate::invoke_contract(self)
}
Expand All @@ -150,7 +151,7 @@ where
///
/// # Panics
///
/// This method panics if it encounters an [`ink::env::Error`][`crate::Error`] If you want to
/// This method panics if it encounters an [`ink::env::Error`][`crate::Error`]. If you want to
/// handle those use the [`try_invoke`][`CallParams::try_invoke`] method instead.
pub fn invoke(&self) -> R {
crate::invoke_contract_delegate(self).unwrap_or_else(|env_error| {
Expand Down Expand Up @@ -691,7 +692,8 @@ where
/// # Note
///
/// On failure this returns an outer [`ink::env::Error`][`crate::Error`] or inner
/// [`ink_primitives::LangError`], both of which can be handled by the caller.
/// [`ink::primitives::LangError`][`ink_primitives::LangError`], both of which can be handled
/// by the caller.
pub fn try_invoke(self) -> Result<ink_primitives::MessageResult<()>, Error> {
self.params().try_invoke()
}
Expand Down Expand Up @@ -750,7 +752,8 @@ where
/// # Note
///
/// On failure this returns an outer [`ink::env::Error`][`crate::Error`] or inner
/// [`ink_primitives::LangError`], both of which can be handled by the caller.
/// [`ink::primitives::LangError`][`ink_primitives::LangError`], both of which can be handled
/// by the caller.
pub fn try_invoke(self) -> Result<ink_primitives::MessageResult<R>, Error> {
self.params().try_invoke()
}
Expand Down
Loading