From cafbc2f177806d14856081e7e1a4b4c227d56934 Mon Sep 17 00:00:00 2001 From: xermicus Date: Thu, 3 Oct 2024 10:08:19 +0200 Subject: [PATCH] only charge the storage item on instantiate Signed-off-by: xermicus --- substrate/frame/revive/src/storage.rs | 11 +++-------- substrate/frame/revive/src/tests.rs | 25 +++++++++++++++++-------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/substrate/frame/revive/src/storage.rs b/substrate/frame/revive/src/storage.rs index c62d148786d9..120bd6a3caf2 100644 --- a/substrate/frame/revive/src/storage.rs +++ b/substrate/frame/revive/src/storage.rs @@ -241,8 +241,9 @@ impl ContractInfo { /// 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) -> BalanceOf { + // 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::(None) .charge_or_zero(); @@ -251,13 +252,7 @@ impl ContractInfo { // 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 } diff --git a/substrate/frame/revive/src/tests.rs b/substrate/frame/revive/src/tests.rs index 71d730aedf4f..51647f896590 100644 --- a/substrate/frame/revive/src/tests.rs +++ b/substrate/frame/revive/src/tests.rs @@ -140,17 +140,22 @@ pub mod test_utils { pub fn contract_info_storage_deposit(addr: &H160) -> BalanceOf { 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::::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 @@ -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(); @@ -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!( ::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)) }); }