This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
contracts: Don't rely on reserved balances keeping an account alive #13369
Merged
Merged
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b0f14f2
Move storage deposits to their own account
athei 50ab7fb
Take ed for contract's account from origin
athei 1190527
Apply suggestions from code review
athei 1ee7dd6
Update stale docs
athei 4c57c30
Use 16 bytes prefix for address derivation
athei 01240db
Update frame/contracts/src/address.rs
athei 1cf7fde
Merge branch 'master' into at/deposit-rework
athei c9797c0
Fix merge
athei 39305d7
".git/.scripts/commands/bench/bench.sh" pallet dev pallet_contracts
a066564
Update frame/contracts/primitives/src/lib.rs
athei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // This file is part of Substrate. | ||
|
|
||
| // Copyright (C) 2018-2022 Parity Technologies (UK) Ltd. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! Functions that deal with address derivation. | ||
|
|
||
| use crate::{CodeHash, Config}; | ||
| use codec::{Decode, Encode}; | ||
| use sp_runtime::traits::{Hash, TrailingZeroInput}; | ||
|
|
||
| /// Provides the contract address generation method. | ||
| /// | ||
| /// See [`DefaultAddressGenerator`] for the default implementation. | ||
| /// | ||
| /// # Note for implementors | ||
| /// | ||
| /// 1. Make sure that there are no collisions, different inputs never lead to the same output. | ||
| /// 2. Make sure that the same inputs lead to the same output. | ||
| pub trait AddressGenerator<T: Config> { | ||
| /// The address of a contract based on the given instantiate parameters. | ||
| /// | ||
| /// Changing the formular for an already deployed chain is fine as long as no collisons | ||
| /// with the old formular. Changes only affect existing contracts. | ||
| fn contract_address( | ||
| deploying_address: &T::AccountId, | ||
| code_hash: &CodeHash<T>, | ||
| input_data: &[u8], | ||
| salt: &[u8], | ||
| ) -> T::AccountId; | ||
|
|
||
| /// The address of the deposit account of `contract_addr`. | ||
| /// | ||
| /// The address is generated once on instantiation and then stored in the contracts | ||
| /// metadata. Hence changes do only affect newly created contracts. | ||
| fn deposit_address(contract_addr: &T::AccountId) -> T::AccountId; | ||
| } | ||
|
|
||
| /// Default address generator. | ||
| /// | ||
| /// This is the default address generator used by contract instantiation. Its result | ||
| /// is only dependant on its inputs. It can therefore be used to reliably predict the | ||
| /// address of a contract. This is akin to the formula of eth's CREATE2 opcode. There | ||
| /// is no CREATE equivalent because CREATE2 is strictly more powerful. | ||
| /// Formula: | ||
| /// `hash("contract_addr_v1" ++ deploying_address ++ code_hash ++ input_data ++ salt)` | ||
| pub struct DefaultAddressGenerator; | ||
|
|
||
| impl<T: Config> AddressGenerator<T> for DefaultAddressGenerator { | ||
| /// Formular: `hash("contract_addr_v1" ++ deploying_address ++ code_hash ++ input_data ++ salt)` | ||
| fn contract_address( | ||
| deploying_address: &T::AccountId, | ||
| code_hash: &CodeHash<T>, | ||
| input_data: &[u8], | ||
| salt: &[u8], | ||
| ) -> T::AccountId { | ||
| let entropy = (b"contract_addr_v1", deploying_address, code_hash, input_data, salt) | ||
| .using_encoded(T::Hashing::hash); | ||
| Decode::decode(&mut TrailingZeroInput::new(entropy.as_ref())) | ||
| .expect("infinite length input; no invalid inputs for type; qed") | ||
| } | ||
|
|
||
| /// Formular: `hash("contract_deposit_v1" ++ contract_addr)` | ||
athei marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| fn deposit_address(contract_addr: &T::AccountId) -> T::AccountId { | ||
| let entropy = (b"contract_deposit_v1", contract_addr).using_encoded(T::Hashing::hash); | ||
xermicus marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Decode::decode(&mut TrailingZeroInput::new(entropy.as_ref())) | ||
| .expect("infinite length input; no invalid inputs for type; qed") | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.