Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
304d84b
wip
pgherveou Jul 3, 2023
4e80ea6
fixes
pgherveou Jul 3, 2023
dc744af
rm comment
pgherveou Jul 3, 2023
5f9a226
join fns
pgherveou Jul 3, 2023
b6def6d
clippy
pgherveou Jul 4, 2023
76c43c2
Fix limits
pgherveou Jul 4, 2023
83e5f37
reduce diff
pgherveou Jul 4, 2023
e3c51ec
fix
pgherveou Jul 4, 2023
8c15372
fix
pgherveou Jul 4, 2023
c97708b
fix typo
pgherveou Jul 4, 2023
0180777
refactor store to use self
pgherveou Jul 4, 2023
89accc8
refactor run to take self by value
pgherveou Jul 4, 2023
0e61306
pass tests
pgherveou Jul 4, 2023
49c174e
rm comment
pgherveou Jul 4, 2023
9f53e36
fixes
pgherveou Jul 5, 2023
5036e88
fix typo
pgherveou Jul 5, 2023
09a9971
rm
pgherveou Jul 5, 2023
6248a64
fix fmt
pgherveou Jul 5, 2023
e137f0e
clippy
pgherveou Jul 5, 2023
672e92e
Merge branch 'master' of https://github.com/paritytech/substrate into…
Jul 5, 2023
68ba74d
".git/.scripts/commands/bench/bench.sh" pallet dev pallet_contracts
Jul 5, 2023
e8ceb0d
Update frame/contracts/src/lib.rs
pgherveou Jul 6, 2023
9eb7833
Update frame/contracts/src/wasm/mod.rs
pgherveou Jul 6, 2023
96e3dcf
Update frame/contracts/src/wasm/mod.rs
pgherveou Jul 6, 2023
a9df0cc
PR review, rm duplicate increment_refcount
pgherveou Jul 6, 2023
b291902
PR review
pgherveou Jul 6, 2023
bc2057d
Update frame/contracts/src/wasm/prepare.rs
pgherveou Jul 6, 2023
bbf2c5b
Add test for failing storage_deposit
pgherveou Jul 6, 2023
dff4758
fix lint
pgherveou Jul 6, 2023
5873b6f
wip
pgherveou Jul 3, 2023
679bb88
Delegate update take 2
pgherveou Jul 4, 2023
9664297
update
pgherveou Jul 4, 2023
ddaba76
fix migration
pgherveou Jul 4, 2023
d45c793
fix migration
pgherveou Jul 4, 2023
33a9d79
doc
pgherveou Jul 4, 2023
b5f35fc
fix lint
pgherveou Jul 5, 2023
f941e2a
update migration
pgherveou Jul 5, 2023
4ccb853
fix warning
pgherveou Jul 5, 2023
16744ba
Merge branch 'master' into pg/delegate_deposit
pgherveou Jul 7, 2023
30f5472
reformat comment
pgherveou Jul 7, 2023
649011d
regenerate weightInfo trait
pgherveou Jul 7, 2023
3d126ac
fix merge
pgherveou Jul 7, 2023
d9d85a2
Merge remote-tracking branch 'origin/master' into pg/delegate_deposit
Jul 7, 2023
4354a7f
Merge remote-tracking branch 'origin/master' into pg/delegate_deposit
Jul 11, 2023
7e811b2
Merge remote-tracking branch 'origin/master' into pg/delegate_deposit
Jul 11, 2023
81d674f
PR review
pgherveou Jul 14, 2023
16cab18
PR review
pgherveou Jul 14, 2023
f18194b
PR review remove optimisation
pgherveou Jul 14, 2023
0ddaacd
PR review fix return type
pgherveou Jul 14, 2023
b12319c
Apply suggestions from code review
pgherveou Jul 14, 2023
0b761d6
PR review pass CodeInfo and update docstring
pgherveou Jul 14, 2023
59af50e
Merge branch 'master' into pg/delegate_deposit
pgherveou Jul 14, 2023
c6ce4ba
PR review add code_info to the executable
pgherveou Jul 14, 2023
fe05151
rename info -> contract_info
pgherveou Jul 14, 2023
bdd7889
Update frame/contracts/src/exec.rs
pgherveou Jul 14, 2023
46a9617
Update frame/contracts/fixtures/add_remove_delegate_dependency.wat
pgherveou Jul 21, 2023
b224362
Update frame/contracts/src/migration/v13.rs
pgherveou Jul 21, 2023
f7d6b99
fix tests
pgherveou Jul 14, 2023
fdd4613
Fmt & fix tests
pgherveou Jul 21, 2023
a876168
Test Result<(), _> return type
pgherveou Jul 24, 2023
c81d6a0
Update frame/contracts/src/migration.rs
pgherveou Jul 24, 2023
3cbb616
Revert "Test Result<(), _> return type"
pgherveou Jul 24, 2023
de00196
add / update doc comments
pgherveou Jul 24, 2023
2ff6425
fix backticks
pgherveou Jul 24, 2023
564169b
Revert "Revert "Test Result<(), _> return type""
pgherveou Jul 24, 2023
a1da123
fix bench
pgherveou Jul 24, 2023
a8e8fa4
fix bench
pgherveou Jul 24, 2023
2cbe99f
fix
pgherveou Jul 24, 2023
59755eb
Update frame/contracts/src/storage/meter.rs
pgherveou Jul 24, 2023
6316417
rm stale comments
pgherveou Jul 24, 2023
0248734
Apply suggestions from code review
pgherveou Jul 25, 2023
e7862f0
PR suggestion
pgherveou Jul 25, 2023
25be4b0
Add missing doc
pgherveou Jul 25, 2023
9627c4c
fx lint
pgherveou Jul 25, 2023
ebb49fe
Merge branch 'master' of https://github.com/paritytech/substrate into…
Jul 25, 2023
e0fe397
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Jul 25, 2023
9a888e5
Update frame/contracts/src/lib.rs
pgherveou Jul 26, 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
fixes
  • Loading branch information
pgherveou committed Jul 5, 2023
commit 9f53e36ece3d48bd0639a2f56b822832e86e5674
8 changes: 5 additions & 3 deletions frame/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,7 @@ struct CallInput<T: Config> {
determinism: Determinism,
}

/// Reference to an existing code hash or a new wasm module.
enum WasmCode<T: Config> {
Wasm(WasmBlob<T>),
CodeHash(CodeHash<T>),
Expand Down Expand Up @@ -1468,6 +1469,7 @@ impl<T: Config> Pallet<T> {
Ok(CodeUploadReturnValue { code_hash: *module.code_hash(), deposit })
}

/// Upload new code and returns the wasm blob and deposit amount paid.
fn try_upload_code(
origin: T::AccountId,
code: Vec<u8>,
Expand All @@ -1481,11 +1483,11 @@ impl<T: Config> Pallet<T> {
debug_message.as_mut().map(|d| d.try_extend(msg.bytes()));
err
})?;
let deposit = module.open_deposit(&module.code_info());
let deposit = module.store_code()?;
if let Some(storage_deposit_limit) = storage_deposit_limit {
ensure!(storage_deposit_limit >= deposit, <Error<T>>::StorageDepositLimitExhausted);
}
module.store()?;

Ok((module, deposit))
}

Expand Down Expand Up @@ -1530,7 +1532,7 @@ impl<T: Config> Pallet<T> {
owner: T::AccountId,
) -> frame_support::dispatch::DispatchResult {
let schedule = T::Schedule::get();
WasmBlob::store_code_unchecked(code, &schedule, owner)?;
WasmBlob::<T>::from_code_unchecked(code, &schedule, owner)?.store_code()?;
Ok(())
}

Expand Down
92 changes: 32 additions & 60 deletions frame/contracts/src/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,33 +161,13 @@ impl<T: Config> WasmBlob<T> {
)
}

/// Store the code without instantiating it.
///
/// Otherwise the code is stored when [`<Self as Executable>::execute`][`Executable::execute`]
/// is called.
pub fn store(&mut self) -> DispatchResult {
self.store_code(false)
}

/// Remove the code from storage and refund the deposit to its owner.
///
/// Applies all necessary checks before removing the code.
pub fn remove(origin: &T::AccountId, code_hash: CodeHash<T>) -> DispatchResult {
Self::try_remove_code(origin, code_hash)
}

/// Returns whether there is a deposit to be paid for this module.
///
/// Returns `0` if the module is already in storage and hence no deposit will
/// be charged for storing it.
pub fn open_deposit(&self, code_info: &CodeInfo<T>) -> BalanceOf<T> {
if <CodeInfoOf<T>>::contains_key(self.code_hash()) {
0u32.into()
} else {
code_info.deposit
}
}

/// Creates and returns an instance of the supplied code.
///
/// This is either used for later executing a contract or for validation of a contract.
Expand Down Expand Up @@ -222,7 +202,7 @@ impl<T: Config> WasmBlob<T> {
// Query wasmi for memory limits specified in the module's import entry.
let memory_limits = contract.scan_imports::<T>(schedule)?;
// Here we allocate this memory in the _store_. It allocates _inital_ value, but allows it
// to grow up to maximum number of memory pages, if neccesary.
// to grow up to maximum number of memory pages, if necessary.
let qed = "We checked the limits versus our Schedule,
which specifies the max amount of memory pages
well below u16::MAX; qed";
Expand Down Expand Up @@ -250,47 +230,51 @@ impl<T: Config> WasmBlob<T> {
&self.code_info
}

/// Put the module blob into storage.
///
/// Increments the reference count of the in-storage `WasmBlob`, if it already exists in
/// storage.
fn store_code(&mut self, instantiated: bool) -> DispatchResult {
/// Put the module blob into storage, and returns the deposit paid for the storage.
pub fn store_code(&mut self) -> Result<BalanceOf<T>, Error<T>> {
let code_hash = self.code_hash().clone();
<CodeInfoOf<T>>::mutate(code_hash, |stored_code_info| {
match stored_code_info {
// Instantiate existing contract.
Some(stored_code_info) if instantiated => {
stored_code_info.refcount = stored_code_info.refcount.checked_add(1).expect(
"
refcount is 64bit. Generating this overflow would require to store
_at least_ 18 exabyte of data assuming that a contract consumes only
one byte of data. Any node would run out of storage space before hitting
this overflow;
qed
",
);
Ok(())
},
// Contract code is already stored in storage. Nothing to be done here.
Some(_) => Ok(()),
Some(_) => Ok(Default::default()),
// Upload a new contract code.
//
// We need to store the code and its code_info, and collect the deposit.
// This `None` case happens only in freshly uploaded modules. This means that
// the `owner` is always the origin of the current transaction.
None => {
// This `None` case happens only in freshly uploaded modules. This means that
// the `owner` is always the origin of the current transaction.
T::Currency::reserve(&self.code_info.owner, self.code_info.deposit)
let deposit = self.code_info.deposit;
T::Currency::reserve(&self.code_info.owner, deposit)
.map_err(|_| <Error<T>>::StorageDepositNotEnoughFunds)?;
self.code_info.refcount = if instantiated { 1 } else { 0 };
self.code_info.refcount = 0;
<PristineCode<T>>::insert(code_hash, &self.code);
*stored_code_info = Some(self.code_info.clone());
<Pallet<T>>::deposit_event(vec![code_hash], Event::CodeStored { code_hash });
Ok(())
Ok(deposit)
},
}
})
}

/// Increments the reference count of the in-storage `WasmBlob`
fn increment_refcount(&mut self) -> DispatchResult {
let code_hash = self.code_hash().clone();
<CodeInfoOf<T>>::mutate(code_hash, |stored_code_info| match stored_code_info {
Some(stored_code_info) => {
stored_code_info.refcount = stored_code_info.refcount.checked_add(1).expect(
"
refcount is 64bit. Generating this overflow would require to store
_at least_ 18 exabyte of data assuming that a contract consumes only
one byte of data. Any node would run out of storage space before hitting
this overflow;
qed
",
);
Ok(())
},
None => Err(<Error<T>>::CodeNotFound.into()),
})
}

/// Try to remove code together with all associated information.
fn try_remove_code(origin: &T::AccountId, code_hash: CodeHash<T>) -> DispatchResult {
<CodeInfoOf<T>>::try_mutate_exists(&code_hash, |existing| {
Expand Down Expand Up @@ -323,17 +307,6 @@ impl<T: Config> WasmBlob<T> {
Ok(code)
}

/// See [`Self::from_code_unchecked`].
#[cfg(feature = "runtime-benchmarks")]
pub fn store_code_unchecked(
code: Vec<u8>,
schedule: &Schedule<T>,
owner: T::AccountId,
) -> DispatchResult {
let executable = Self::from_code_unchecked(code, schedule, owner)?;
Self::store_code(executable, false)
}

/// Create the module without checking the passed code.
///
/// # Note
Expand All @@ -342,7 +315,7 @@ impl<T: Config> WasmBlob<T> {
/// our results. This also does not collect any deposit from the `owner`. Also useful
/// during testing when we want to deploy codes that do not pass the instantiation checks.
#[cfg(any(test, feature = "runtime-benchmarks"))]
fn from_code_unchecked(
pub fn from_code_unchecked(
code: Vec<u8>,
schedule: &Schedule<T>,
owner: T::AccountId,
Expand Down Expand Up @@ -442,9 +415,8 @@ impl<T: Config> Executable<T> for WasmBlob<T> {
Error::<T>::CodeRejected
})?;

// We store before executing so that the code hash is available in the constructor.
if let &ExportedFunction::Constructor = function {
self.store_code(true)?;
self.increment_refcount()?;
}

let result = exported_func.call(&mut store, &[], &mut []);
Expand Down