Skip to content

Commit

Permalink
only charge the storage item on instantiate
Browse files Browse the repository at this point in the history
Signed-off-by: xermicus <cyrill@parity.io>
  • Loading branch information
xermicus committed Oct 3, 2024
1 parent 6fd2d01 commit cafbc2f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
11 changes: 3 additions & 8 deletions substrate/frame/revive/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,9 @@ impl<T: Config> ContractInfo<T> {
/// The base deposit is updated when the `code_hash` of the contract changes, as it depends on
/// the deposit paid to upload the contract's code.
pub fn update_base_deposit(&mut self, code_info: &CodeInfo<T>) -> BalanceOf<T> {
// The 2 items added are code info and immutable data
let info_deposit =
Diff { bytes_added: self.encoded_size() as u32, items_added: 1, ..Default::default() }
Diff { bytes_added: self.encoded_size() as u32, items_added: 2, ..Default::default() }
.update_contract::<T>(None)
.charge_or_zero();

Expand All @@ -251,13 +252,7 @@ impl<T: Config> ContractInfo<T> {
// to prevent abuse.
let upload_deposit = T::CodeHashLockupDepositPercent::get().mul_ceil(code_info.deposit());

// Immutable data is unique per contract and part of the base deposit.
let immutable_data_deposit =
T::DepositPerByte::get().saturating_mul(self.immutable_bytes.into());

let deposit = info_deposit
.saturating_add(upload_deposit)
.saturating_add(immutable_data_deposit);
let deposit = info_deposit.saturating_add(upload_deposit);
self.storage_base_deposit = deposit;
deposit
}
Expand Down
25 changes: 17 additions & 8 deletions substrate/frame/revive/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,22 @@ pub mod test_utils {
pub fn contract_info_storage_deposit(addr: &H160) -> BalanceOf<Test> {
let contract_info = self::get_contract(&addr);
let info_size = contract_info.encoded_size() as u64;
DepositPerByte::get()
let immutable_size = contract_info.immutable_bytes() as u64;
let info_deposit = DepositPerByte::get()
.saturating_mul(info_size)
.saturating_add(DepositPerItem::get())
.saturating_add(DepositPerItem::get());
let immutable_deposit = DepositPerByte::get()
.saturating_mul(immutable_size)
.saturating_add(DepositPerItem::get());
info_deposit.saturating_add(immutable_deposit)
}
pub fn expected_deposit(code_len: usize) -> u64 {
// For code_info, the deposit for max_encoded_len is taken.
let code_info_len = CodeInfo::<Test>::max_encoded_len() as u64;
// Calculate deposit to be reserved.
// We add 2 storage items: one for code, other for code_info
DepositPerByte::get().saturating_mul(code_len as u64 + code_info_len) +
DepositPerItem::get().saturating_mul(2)
DepositPerByte::get().saturating_mul(code_len as u64 + code_info_len)
+ DepositPerItem::get().saturating_mul(2)
}
pub fn ensure_stored(code_hash: sp_core::H256) -> usize {
// Assert that code_info is stored
Expand Down Expand Up @@ -3530,8 +3535,11 @@ mod run_tests {
// Set enough deposit limit for the child instantiate. This should succeed.
let result = builder::bare_call(addr_caller)
.origin(RuntimeOrigin::signed(BOB))
.storage_deposit_limit(callee_info_len + 2 + ED + 4)
.data((1u32, &code_hash_callee, U256::from(callee_info_len + 2 + ED + 3)).encode())
.storage_deposit_limit(callee_info_len + 2 + ED + 4 + 2)
.data(
(1u32, &code_hash_callee, U256::from(callee_info_len + 2 + ED + 3 + 2))
.encode(),
)
.build();

let returned = result.result.unwrap();
Expand All @@ -3548,12 +3556,13 @@ mod run_tests {
// - callee instantiation deposit = (callee_info_len + 2)
// - callee account ED
// - for writing an item of 1 byte to storage = 3 Balance
// - Immutable data storage item deposit
assert_eq!(
<Test as Config>::Currency::free_balance(&BOB),
1_000_000 - (callee_info_len + 2 + ED + 3)
1_000_000 - (callee_info_len + 2 + ED + 3 + 2)
);
// Check that deposit due to be charged still includes these 3 Balance
assert_eq!(result.storage_deposit.charge_or_zero(), (callee_info_len + 2 + ED + 3))
assert_eq!(result.storage_deposit.charge_or_zero(), (callee_info_len + 2 + ED + 3 + 2))
});
}

Expand Down

0 comments on commit cafbc2f

Please sign in to comment.