Skip to content

Commit 30cfa81

Browse files
authored
seal: Refactor ext_gas_price (#6478)
* seal: Refactor ext_gas_price * Remove seals dependency on pallet_transaction_payment * Add weight as an argument to ext_gas_price * Fixed documentation nits from review * Do not use unchecked math even in test code
1 parent bb2109c commit 30cfa81

File tree

6 files changed

+34
-32
lines changed

6 files changed

+34
-32
lines changed

Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ sp-sandbox = { version = "0.8.0-rc3", default-features = false, path = "../../pr
2525
frame-support = { version = "2.0.0-rc3", default-features = false, path = "../support" }
2626
frame-system = { version = "2.0.0-rc3", default-features = false, path = "../system" }
2727
pallet-contracts-primitives = { version = "2.0.0-rc3", default-features = false, path = "common" }
28-
pallet-transaction-payment = { version = "2.0.0-rc3", default-features = false, path = "../transaction-payment" }
2928

3029
[dev-dependencies]
3130
wabt = "0.9.2"
@@ -52,5 +51,4 @@ std = [
5251
"pwasm-utils/std",
5352
"wasmi-validation/std",
5453
"pallet-contracts-primitives/std",
55-
"pallet-transaction-payment/std",
5654
]

src/exec.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ use crate::rent;
2121
use crate::storage;
2222

2323
use sp_std::prelude::*;
24-
use sp_runtime::traits::{Bounded, Zero};
24+
use sp_runtime::traits::{Bounded, Zero, Convert};
2525
use frame_support::{
2626
storage::unhashed, dispatch::DispatchError,
2727
traits::{ExistenceRequirement, Currency, Time, Randomness},
28+
weights::Weight,
2829
};
2930

3031
pub type AccountIdOf<T> = <T as frame_system::Trait>::AccountId;
@@ -216,8 +217,8 @@ pub trait Ext {
216217
/// Returns `None` if the value doesn't exist.
217218
fn get_runtime_storage(&self, key: &[u8]) -> Option<Vec<u8>>;
218219

219-
/// Returns the price of one weight unit.
220-
fn get_weight_price(&self) -> BalanceOf<Self::T>;
220+
/// Returns the price for the specified amount of weight.
221+
fn get_weight_price(&self, weight: Weight) -> BalanceOf<Self::T>;
221222
}
222223

223224
/// Loader is a companion of the `Vm` trait. It loads an appropriate abstract
@@ -874,11 +875,8 @@ where
874875
unhashed::get_raw(&key)
875876
}
876877

877-
fn get_weight_price(&self) -> BalanceOf<Self::T> {
878-
use pallet_transaction_payment::Module as Payment;
879-
use sp_runtime::SaturatedConversion;
880-
let price = Payment::<T>::weight_to_fee_with_adjustment::<u128>(1);
881-
price.saturated_into()
878+
fn get_weight_price(&self, weight: Weight) -> BalanceOf<Self::T> {
879+
T::WeightPrice::convert(weight)
882880
}
883881
}
884882

src/lib.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ use sp_std::{prelude::*, marker::PhantomData, fmt::Debug};
102102
use codec::{Codec, Encode, Decode};
103103
use sp_runtime::{
104104
traits::{
105-
Hash, StaticLookup, Zero, MaybeSerializeDeserialize, Member,
105+
Hash, StaticLookup, Zero, MaybeSerializeDeserialize, Member, Convert,
106106
},
107107
RuntimeDebug,
108108
};
@@ -117,6 +117,7 @@ use frame_support::traits::{OnUnbalanced, Currency, Get, Time, Randomness};
117117
use frame_support::weights::GetDispatchInfo;
118118
use frame_system::{self as system, ensure_signed, RawOrigin, ensure_root};
119119
use pallet_contracts_primitives::{RentProjection, ContractAccessError};
120+
use frame_support::weights::Weight;
120121

121122
pub type CodeHash<T> = <T as frame_system::Trait>::Hash;
122123
pub type TrieId = Vec<u8>;
@@ -289,9 +290,10 @@ where
289290
}
290291
}
291292

292-
pub type BalanceOf<T> = <<T as pallet_transaction_payment::Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;
293+
pub type BalanceOf<T> =
294+
<<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;
293295
pub type NegativeImbalanceOf<T> =
294-
<<T as pallet_transaction_payment::Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::NegativeImbalance;
296+
<<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::NegativeImbalance;
295297

296298
parameter_types! {
297299
/// A reasonable default value for [`Trait::SignedClaimedHandicap`].
@@ -312,10 +314,13 @@ parameter_types! {
312314
pub const DefaultMaxValueSize: u32 = 16_384;
313315
}
314316

315-
pub trait Trait: frame_system::Trait + pallet_transaction_payment::Trait {
317+
pub trait Trait: frame_system::Trait {
316318
type Time: Time;
317319
type Randomness: Randomness<Self::Hash>;
318320

321+
/// The currency in which fees are paid and contract balances are held.
322+
type Currency: Currency<Self::AccountId>;
323+
319324
/// The outer call dispatch type.
320325
type Call:
321326
Parameter +
@@ -371,6 +376,10 @@ pub trait Trait: frame_system::Trait + pallet_transaction_payment::Trait {
371376

372377
/// The maximum size of a storage value in bytes.
373378
type MaxValueSize: Get<u32>;
379+
380+
/// Used to answer contracts's queries regarding the current weight price. This is **not**
381+
/// used to calculate the actual fee and is only for informational purposes.
382+
type WeightPrice: Convert<Weight, BalanceOf<Self>>;
374383
}
375384

376385
/// Simple contract address determiner.

src/tests.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use frame_support::{
3030
assert_ok, assert_err_ignore_postinfo, impl_outer_dispatch, impl_outer_event,
3131
impl_outer_origin, parameter_types, StorageMap, StorageValue,
3232
traits::{Currency, Get},
33-
weights::{Weight, PostDispatchInfo, IdentityFee},
33+
weights::{Weight, PostDispatchInfo},
3434
};
3535
use std::cell::RefCell;
3636
use frame_system::{self as system, EventRecord, Phase};
@@ -169,17 +169,10 @@ impl Convert<Weight, BalanceOf<Self>> for Test {
169169
}
170170
}
171171

172-
impl pallet_transaction_payment::Trait for Test {
173-
type Currency = Balances;
174-
type OnTransactionPayment = ();
175-
type TransactionByteFee = TransactionByteFee;
176-
type WeightToFee = IdentityFee<BalanceOf<Self>>;
177-
type FeeMultiplierUpdate = ();
178-
}
179-
180172
impl Trait for Test {
181173
type Time = Timestamp;
182174
type Randomness = Randomness;
175+
type Currency = Balances;
183176
type Call = Call;
184177
type DetermineContractAddress = DummyContractAddressFor;
185178
type Event = MetaEvent;
@@ -193,6 +186,7 @@ impl Trait for Test {
193186
type SurchargeReward = SurchargeReward;
194187
type MaxDepth = MaxDepth;
195188
type MaxValueSize = MaxValueSize;
189+
type WeightPrice = Self;
196190
}
197191

198192
type Balances = pallet_balances::Module<Test>;

src/wasm/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ mod tests {
162162
use hex_literal::hex;
163163
use assert_matches::assert_matches;
164164
use sp_runtime::DispatchError;
165+
use frame_support::weights::Weight;
165166

166167
const GAS_LIMIT: Gas = 10_000_000_000;
167168

@@ -373,8 +374,8 @@ mod tests {
373374
)
374375
)
375376
}
376-
fn get_weight_price(&self) -> BalanceOf<Self::T> {
377-
1312_u32.into()
377+
fn get_weight_price(&self, weight: Weight) -> BalanceOf<Self::T> {
378+
BalanceOf::<Self::T>::from(1312_u32).saturating_mul(weight.into())
378379
}
379380
}
380381

@@ -479,8 +480,8 @@ mod tests {
479480
fn get_runtime_storage(&self, key: &[u8]) -> Option<Vec<u8>> {
480481
(**self).get_runtime_storage(key)
481482
}
482-
fn get_weight_price(&self) -> BalanceOf<Self::T> {
483-
(**self).get_weight_price()
483+
fn get_weight_price(&self, weight: Weight) -> BalanceOf<Self::T> {
484+
(**self).get_weight_price(weight)
484485
}
485486
}
486487

@@ -1056,7 +1057,7 @@ mod tests {
10561057

10571058
const CODE_GAS_PRICE: &str = r#"
10581059
(module
1059-
(import "env" "ext_gas_price" (func $ext_gas_price))
1060+
(import "env" "ext_gas_price" (func $ext_gas_price (param i64)))
10601061
(import "env" "ext_scratch_size" (func $ext_scratch_size (result i32)))
10611062
(import "env" "ext_scratch_read" (func $ext_scratch_read (param i32 i32 i32)))
10621063
(import "env" "memory" (memory 1 1))
@@ -1072,7 +1073,7 @@ mod tests {
10721073
10731074
(func (export "call")
10741075
;; This stores the gas price in the scratch buffer
1075-
(call $ext_gas_price)
1076+
(call $ext_gas_price (i64.const 1))
10761077
10771078
;; assert $ext_scratch_size == 8
10781079
(call $assert

src/wasm/runtime.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -696,12 +696,14 @@ define_env!(Env, <E: Ext>,
696696
Ok(())
697697
},
698698

699-
// Stores the gas price for the current transaction into the scratch buffer.
699+
// Stores the price for the specified amount of gas in scratch buffer.
700700
//
701701
// The data is encoded as T::Balance. The current contents of the scratch buffer are overwritten.
702-
ext_gas_price(ctx) => {
702+
// It is recommended to avoid specifying very small values for `gas` as the prices for a single
703+
// gas can be smaller than one.
704+
ext_gas_price(ctx, gas: u64) => {
703705
ctx.scratch_buf.clear();
704-
ctx.ext.get_weight_price().encode_to(&mut ctx.scratch_buf);
706+
ctx.ext.get_weight_price(gas).encode_to(&mut ctx.scratch_buf);
705707
Ok(())
706708
},
707709

0 commit comments

Comments
 (0)