This repository has been archived by the owner on Sep 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
XVM MVP v2 #92
Merged
Merged
XVM MVP v2 #92
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
cc48aef
XVM interface refactoring
akru 8451ee7
Switch precompiles & chain-extension to XVMv2
akru 5340817
XVM gas handling prototype (#94)
akru 902fcbf
Remove chain-extension-trait package (#109)
akru 957f0e6
XVM types cargo deps fix
akru c2d862f
Build fixes
akru f8e19ed
Compilation fixes
akru 2124931
Fix formatting
akru 8483b6d
Merge remote-tracking branch 'origin/polkadot-v0.9.30' into feature/x…
akru 649faa1
Fix formatting
akru e0a7ab9
Fix compilation issues
akru f88d719
Fix cargo fmt
akru 72f8d69
Fix clippy errors
akru f1cbe67
Fix weight estimation
akru File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
use frame_support::weights::Weight; | ||
use frame_system::RawOrigin; | ||
use pallet_contracts::chain_extension::{ | ||
ChainExtension, Environment, Ext, InitState, RetVal, SysConfig, UncheckedFrom, | ||
|
@@ -52,27 +51,19 @@ where | |
|
||
match func_id { | ||
XvmFuncId::XvmCall => { | ||
// 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. | ||
let remaining_weight = env.ext().gas_meter().gas_left(); | ||
let charged_weight = env.charge_weight(remaining_weight)?; | ||
|
||
// Prepare parameters | ||
let caller = env.ext().caller().clone(); | ||
|
||
let XvmCallArgs { | ||
vm_id, | ||
to, | ||
input, | ||
metadata, | ||
} = env.read_as_unbounded(env.in_len())?; | ||
let XvmCallArgs { vm_id, to, input } = env.read_as_unbounded(env.in_len())?; | ||
|
||
// 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can delete these 2 unused values. |
||
let _value = env.ext().value_transferred(); | ||
let _gas_limit = env.ext().gas_meter().gas_left(); | ||
let xvm_context = XvmContext { | ||
id: T::VmId::from(vm_id), | ||
id: vm_id, | ||
max_weight: remaining_weight, | ||
env: None, | ||
}; | ||
|
||
|
@@ -81,12 +72,16 @@ where | |
xvm_context, | ||
to, | ||
input, | ||
metadata, | ||
); | ||
|
||
// TODO: We need to know how much of gas was spent in the other call and update the gas meter! | ||
// let consumed_xvm_weight = ...; | ||
// env.charge_weight(consumed_xvm_weight)?; | ||
// Adjust the actual weight used by the call if needed. | ||
let actual_weight = match call_result { | ||
Ok(e) => e.actual_weight, | ||
Err(e) => e.post_info.actual_weight, | ||
}; | ||
if let Some(actual_weight) = actual_weight { | ||
env.adjust_weight(charged_weight, actual_weight); | ||
} | ||
|
||
return match call_result { | ||
Err(e) => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it’s new astar-frame formatting settings |
||
RuntimeOrigin::signed(4) | ||
)); | ||
|
||
// but no more | ||
assert_noop!( | ||
|
@@ -132,7 +134,9 @@ fn cannot_unregister_candidate_if_too_few() { | |
new_test_ext().execute_with(|| { | ||
// reset desired candidates: | ||
<crate::DesiredCandidates<Test>>::put(1); | ||
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(4))); | ||
assert_ok!(CollatorSelection::register_as_candidate( | ||
RuntimeOrigin::signed(4) | ||
)); | ||
|
||
// can not remove too few | ||
assert_noop!( | ||
|
@@ -170,7 +174,9 @@ fn cannot_register_as_candidate_if_keys_not_registered() { | |
fn cannot_register_dupe_candidate() { | ||
new_test_ext().execute_with(|| { | ||
// can add 3 as candidate | ||
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(3))); | ||
assert_ok!(CollatorSelection::register_as_candidate( | ||
RuntimeOrigin::signed(3) | ||
)); | ||
let addition = CandidateInfo { | ||
who: 3, | ||
deposit: 10, | ||
|
@@ -194,7 +200,9 @@ fn cannot_register_as_candidate_if_poor() { | |
assert_eq!(Balances::free_balance(&33), 0); | ||
|
||
// works | ||
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(3))); | ||
assert_ok!(CollatorSelection::register_as_candidate( | ||
RuntimeOrigin::signed(3) | ||
)); | ||
|
||
// poor | ||
assert_noop!( | ||
|
@@ -217,8 +225,12 @@ fn register_as_candidate_works() { | |
assert_eq!(Balances::free_balance(&3), 100); | ||
assert_eq!(Balances::free_balance(&4), 100); | ||
|
||
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(3))); | ||
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(4))); | ||
assert_ok!(CollatorSelection::register_as_candidate( | ||
RuntimeOrigin::signed(3) | ||
)); | ||
assert_ok!(CollatorSelection::register_as_candidate( | ||
RuntimeOrigin::signed(4) | ||
)); | ||
|
||
assert_eq!(Balances::free_balance(&3), 90); | ||
assert_eq!(Balances::free_balance(&4), 90); | ||
|
@@ -231,11 +243,15 @@ fn register_as_candidate_works() { | |
fn leave_intent() { | ||
new_test_ext().execute_with(|| { | ||
// register a candidate. | ||
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(3))); | ||
assert_ok!(CollatorSelection::register_as_candidate( | ||
RuntimeOrigin::signed(3) | ||
)); | ||
assert_eq!(Balances::free_balance(3), 90); | ||
|
||
// register too so can leave above min candidates | ||
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(5))); | ||
assert_ok!(CollatorSelection::register_as_candidate( | ||
RuntimeOrigin::signed(5) | ||
)); | ||
assert_eq!(Balances::free_balance(5), 90); | ||
|
||
// cannot leave if not candidate. | ||
|
@@ -259,7 +275,9 @@ fn authorship_event_handler() { | |
|
||
// 4 is the default author. | ||
assert_eq!(Balances::free_balance(4), 100); | ||
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(4))); | ||
assert_ok!(CollatorSelection::register_as_candidate( | ||
RuntimeOrigin::signed(4) | ||
)); | ||
// triggers `note_author` | ||
Authorship::on_initialize(1); | ||
|
||
|
@@ -287,7 +305,9 @@ fn fees_edgecases() { | |
Balances::make_free_balance_be(&CollatorSelection::account_id(), 5); | ||
// 4 is the default author. | ||
assert_eq!(Balances::free_balance(4), 100); | ||
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(4))); | ||
assert_ok!(CollatorSelection::register_as_candidate( | ||
RuntimeOrigin::signed(4) | ||
)); | ||
// triggers `note_author` | ||
Authorship::on_initialize(1); | ||
|
||
|
@@ -319,7 +339,9 @@ fn session_management_works() { | |
assert_eq!(SessionHandlerCollators::get(), vec![1, 2]); | ||
|
||
// add a new collator | ||
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(3))); | ||
assert_ok!(CollatorSelection::register_as_candidate( | ||
RuntimeOrigin::signed(3) | ||
)); | ||
|
||
// session won't see this. | ||
assert_eq!(SessionHandlerCollators::get(), vec![1, 2]); | ||
|
@@ -348,8 +370,12 @@ fn kick_and_slash_mechanism() { | |
// Define slash destination account | ||
<crate::SlashDestination<Test>>::put(5); | ||
// add a new collator | ||
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(3))); | ||
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(4))); | ||
assert_ok!(CollatorSelection::register_as_candidate( | ||
RuntimeOrigin::signed(3) | ||
)); | ||
assert_ok!(CollatorSelection::register_as_candidate( | ||
RuntimeOrigin::signed(4) | ||
)); | ||
initialize_to_block(10); | ||
assert_eq!(CollatorSelection::candidates().len(), 2); | ||
initialize_to_block(20); | ||
|
@@ -377,8 +403,12 @@ fn kick_and_slash_mechanism() { | |
fn should_not_kick_mechanism_too_few() { | ||
new_test_ext().execute_with(|| { | ||
// add a new collator | ||
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(3))); | ||
assert_ok!(CollatorSelection::register_as_candidate(RuntimeOrigin::signed(5))); | ||
assert_ok!(CollatorSelection::register_as_candidate( | ||
RuntimeOrigin::signed(3) | ||
)); | ||
assert_ok!(CollatorSelection::register_as_candidate( | ||
RuntimeOrigin::signed(5) | ||
)); | ||
initialize_to_block(10); | ||
assert_eq!(CollatorSelection::candidates().len(), 2); | ||
initialize_to_block(20); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 😅
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed