Skip to content

Conversation

@ascjones
Copy link
Collaborator

@ascjones ascjones commented Jun 25, 2020

Rel paritytech/substrate#6478.

Adds a gas arg to ext_gas_price for compatibility with the above PR.

Note: the offchain impl attempts to emulate the runtime calculation by simply multiplying the provided gas amount by the gas price configure in the offchain chain_spec.

@codecov-commenter
Copy link

codecov-commenter commented Jun 26, 2020

Codecov Report

Merging #461 into master will increase coverage by 0.12%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #461      +/-   ##
==========================================
+ Coverage   86.67%   86.80%   +0.12%     
==========================================
  Files         107      107              
  Lines        4219     4250      +31     
==========================================
+ Hits         3657     3689      +32     
+ Misses        562      561       -1     
Impacted Files Coverage Δ
core/src/env/arithmetic.rs 100.00% <ø> (ø)
core/src/env/api.rs 87.50% <100.00%> (+2.88%) ⬆️
core/src/env/engine/off_chain/db/chain_spec.rs 80.00% <100.00%> (+3.80%) ⬆️
core/src/env/engine/off_chain/impls.rs 93.33% <100.00%> (+1.02%) ⬆️
core/src/env/engine/off_chain/mod.rs 76.92% <100.00%> (+0.60%) ⬆️
core/src/env/engine/off_chain/test_api.rs 67.85% <100.00%> (+5.35%) ⬆️
core/src/env/engine/off_chain/typed_encoded.rs 75.67% <0.00%> (+1.76%) ⬆️
core/src/hash/accumulator.rs 92.59% <0.00%> (+14.81%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 65e094e...d8a7e98. Read the comment docs.

@ascjones ascjones marked this pull request as ready for review June 26, 2020 13:59
@ascjones ascjones requested a review from Robbepop June 26, 2020 13:59
fn gas_price<T: EnvTypes>(&mut self) -> Result<T::Balance> {
fn gas_price<T: EnvTypes>(&mut self, _gas: u64) -> Result<T::Balance> {
self.chain_spec
.gas_price::<T>()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be cool if there was an actual conversion config for the off-chain environment so that the returned amount at least scales linearly to the input.

Comment on lines +113 to +122
+ TryFrom<u32>
+ TryFrom<u64>
+ TryFrom<u128>
+ TryFrom<usize>
+ TryInto<u16>
+ TryInto<u32>
+ TryInto<u64>
+ TryInto<u128>
+ TryInto<usize>
// Further trait bounds from the original BaseArithmetic trait
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why we need this for this PR?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this diverging from what Substrate bounds its types?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why we need this for this PR?

The TryFrom<u64> is needed to convert the gas: u64 arg into EnvTypes::Balance.

Isn't this diverging from what Substrate bounds its types?

It's converging, I thought I would add the other Try* bounds while I was there: https://github.com/paritytech/substrate/blob/4be954a8463e6f33e0ec854c4b8bcd940af260fc/primitives/arithmetic/src/traits.rs#L44

Comment on lines -95 to +99
+ From<u32>
+ From<u8>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why this change is required for this PR?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this brings it into line with the substrate BaseArithmetic. The From<u32> bound is now defined in the AtLeast32Bit trait, and it is assumed for BaseArtithmetic trait that we are least dealing with u8

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why this change is required for this PR?

It's not strictly required for this PR, it's just something I overlooked in #463.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay so am I right that the above changes actually are aligning ink! further with Substrate traits? Just want to be sure about this because I really am not familiar with the Substrate tests but getting traits righti is really really important.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes exactly.

We are still missing a few more e.g. the rest of the Checked* traits and UniqueSaturatedFrom/ UniqueSaturatedInto, but these are definitely out of scope for this PR.

impl<T: Sized, S: TryFrom<T> + Bounded + Sized> UniqueSaturatedFrom<T> for S {
	fn unique_saturated_from(t: T) -> Self {
		S::try_from(t).unwrap_or_else(|_| Bounded::max_value())
	}
}

impl<T: Bounded + Sized, S: TryInto<T> + Sized> UniqueSaturatedInto<T> for S {
	fn unique_saturated_into(self) -> T {
		self.try_into().unwrap_or_else(|_| Bounded::max_value())
	}
}

fn gas_price<T: EnvTypes>(&mut self) -> Result<T::Balance> {
self.chain_spec
/// Emulates gas price calculation
fn gas_price<T: EnvTypes>(&mut self, gas: u64) -> Result<T::Balance> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to have a test for this function somewhere, since the logic now got more complex and logic surrounding cost analysis should be considered criticial imho. Besides this the code looks sound to me.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth adding tests for the offchain environment?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theoretically yes they are worth. However, the off-chain environment currently lacks tests all over the place because originally it was not intended to stay for long since at the time of writing some standalone Seal implementation was still deemed to be realistic. Consider it a hack that came to stay.

Copy link
Collaborator Author

@ascjones ascjones Jul 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test 👇

@ascjones ascjones merged commit 2456fdd into master Jul 2, 2020
@ascjones ascjones deleted the aj-gas-price branch July 2, 2020 12:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants