Skip to content
This repository has been archived by the owner on Sep 28, 2023. It is now read-only.

XVM MVP v2 #92

Merged
merged 14 commits into from
Nov 8, 2022
Merged

XVM MVP v2 #92

merged 14 commits into from
Nov 8, 2022

Conversation

akru
Copy link
Contributor

@akru akru commented Sep 7, 2022

Pull Request Summary

This PR containts XVM v2 prototype.

Feature list:

  • Contracts arguments encoding;
  • Error handling;
  • Return data handling;
  • XVM runtime interface & RPC.

Check list

  • added unit tests
  • updated documentation

@akru akru changed the base branch from polkadot-v0.9.28 to polkadot-v0.9.29 September 23, 2022 03:31
akru added 4 commits October 25, 2022 07:32
* Init commit

* Small fixes

* Chain extension & precompile modifications

* TODO handling

* Traces, improved gas tracking, fixed issues

* Doc to placeholder weight

* Doc & typos

* Change base

* Fix UT

* Update TODOs, cleanup

* Fix contract issues
* Remove chain-extension-trait package

* Dapps Staking CE optimizations

* Dapps Staking CE optimizations

* Rename dapps-staking CE in general way

* Derive Default for chain extension structs

* Implement Default trait for CEs

* Fix typo
@akru akru force-pushed the feature/xvm-pallet-v2 branch from 4d6aefe to 902fcbf Compare October 25, 2022 04:34
@akru akru force-pushed the feature/xvm-pallet-v2 branch from 5d72e25 to 957f0e6 Compare October 25, 2022 06:11
@akru akru marked this pull request as ready for review October 25, 2022 06:56
@akru akru changed the base branch from polkadot-v0.9.29 to polkadot-v0.9.30 November 2, 2022 02:53
// TODO: correct weight calculation directly from pallet!
let weight = Weight::from_ref_time(1_000_000_000);
env.charge_weight(weight)?;
// We need to immediately charge for the worst case scenario. Gas equals Weight in pallet-contracts context.
Copy link
Member

Choose a reason for hiding this comment

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

I was confused at first by this because it seems so familiar lol 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe gas charging should be rewritten for new weighs, including comments. Let’s do it in XVMv3

Copy link
Member

Choose a reason for hiding this comment

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

I meant to say that the reason this code looked so familiar is that I was the author. In the PR that added weight handling. xD

But it was like 2 months ago so I've forgotten about it.

This handling should be fine - some stuff needs to be benchmarked but that won't change the current weights too much.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@@ -117,7 +117,9 @@ fn cannot_register_candidate_if_too_many() {

// reset desired candidates:
<crate::DesiredCandidates<Test>>::put(1);
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(4)));
assert_ok!(CollatorSelection::register_as_candidate(
Copy link
Member

Choose a reason for hiding this comment

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

Why are unrelated files changed by the formatter? Are you using different settings?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess it’s new astar-frame formatting settings

Comment on lines 53 to 54
error: XvmError::ExecutionError(Vec::default()), // TODO: make error mapping make more sense
consumed_weight: 42u64, // TODO: res.actual_weight.map(|x| x.ref_time()).unwrap_or(context.max_weight),
Copy link
Member

Choose a reason for hiding this comment

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

We should go with worst case scenario here and specify consumed_weight: context.max_weight

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but it not works with new weighs, so, I just hack it to make it works.

Copy link
Member

Choose a reason for hiding this comment

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

Can you please explain more? What doesn't work?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe problem here is different types for Weight and context types. We should rework it using new substrate weight system.

Ok(Default::default())
Ok(XvmCallOk {
output: Default::default(), // TODO: Fill output vec with response from the call
consumed_weight: 42u64, // TODO: res.actual_weight.map(|x| x.ref_time()).unwrap_or(context.max_weight),
Copy link
Member

Choose a reason for hiding this comment

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

You should extract correct consumed_weight from the res.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but next time

Copy link
Member

Choose a reason for hiding this comment

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

Why next time?
Weights should be handled properly in v2, right?

And it's a very minor fix. I guess same question as above, but why is the "correct" code commented out?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately this code no more works correctly, it assumes that weight is a number, but it is struct now.
I just want merge this PR because it takes so long time and proper weighting isn’t the main aim of this PR.

Copy link
Member

Choose a reason for hiding this comment

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

I see - however, the struct that was introduced was simply a wrapper around the old u64 value.
That was the initial step, the next one being adding the PoV size.

For this particular case, in the PR, all you need to do is this:

consumed_weight: res.actual_weight.map(|x| x.ref_time()).unwrap_or(context.max_weight.ref_time())

Just add .ref_time() to the max_weight.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not that simple but fixed

Comment on lines 51 to 52
error: XvmError::ExecutionError(Vec::default()), // TODO: make error mapping make more sense
consumed_weight: 42u64, //TODO: e.post_info.actual_weight.ref_time().unwrap_or(gas_limit),
Copy link
Member

Choose a reason for hiding this comment

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

Same comment as for EVM counter-part, we should use context.max_weight here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

Comment on lines 60 to 62
Ok(XvmCallOk {
output: Default::default(), // TODO: Fill in with output from the call
consumed_weight: 42u64, //TODO: e.post_info.actual_weight.ref_time().unwrap_or(gas_limit),
Copy link
Member

Choose a reason for hiding this comment

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

Correct consumed weight can be extracted from the res.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For sure

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed


// TODO: rethink this? How do we get valid env in chain extension? Need to know which data to encode.
// gas limit, taking into account used gas so far?
let _origin_address = env.ext().address().clone();
Copy link
Member

Choose a reason for hiding this comment

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

I think you can delete these 2 unused values.
_origin_address will only be used in a case of an enum where contract can chose to use caller() or address()
_value will be used only when calling payable function will work

contracts/xvm/Cargo.toml Outdated Show resolved Hide resolved
@github-actions
Copy link

github-actions bot commented Nov 7, 2022

Code Coverage

Package Line Rate Branch Rate Health
precompiles/assets-erc20/src 73% 0%
frame/collator-selection/src 80% 0%
precompiles/utils/src/data 72% 0%
chain-extensions/types/dapps-staking/src 0% 0%
frame/block-reward/src 85% 0%
frame/dapps-staking/src 83% 0%
frame/dapps-staking/src/pallet 90% 0%
chain-extensions/dapps-staking/src 0% 0%
precompiles/xvm/src 94% 0%
frame/xc-asset-config/src 70% 0%
primitives/xcm/src 68% 0%
precompiles/xcm/src 84% 0%
precompiles/substrate-ecdsa/src 78% 0%
chain-extensions/rmrk/src 0% 0%
frame/pallet-xvm/src/pallet 34% 0%
frame/custom-signatures/src 57% 0%
frame/pallet-xvm/src 11% 0%
precompiles/utils/src 72% 0%
chain-extensions/types/rmrk/src 0% 0%
chain-extensions/xvm/src 0% 0%
precompiles/utils/macro/src 0% 0%
frame/pallet-xcm/src 65% 0%
chain-extensions/types/xvm/src 0% 0%
precompiles/sr25519/src 79% 0%
precompiles/dapps-staking/src 93% 0%
precompiles/utils/macro/tests 0% 0%
Summary 60% (2577 / 4328) 0% (0 / 0)

Minimum allowed line rate is 50%

Copy link
Member

@Dinonard Dinonard left a comment

Choose a reason for hiding this comment

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

LGTM!

@akru akru merged commit 772d63c into polkadot-v0.9.30 Nov 8, 2022
@akru akru deleted the feature/xvm-pallet-v2 branch November 8, 2022 12:39
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants