mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-27 10:27:59 +00:00
Preimage registrar and Scheduler integration (#10356)
* initial idea * more * fix compile * add clear and request logic * improve some docs * Add and implement trait * continuing to improve * refcount type * infallible system preimage upload * fmt * fix requests * Make it simple * Make it simple * Formatting * Initial draft * request when scheduled * Docs * Scheduler good * Scheduler good * Scheduler tests working * Add new files * Missing stuff * Repotting, add weights. * Add some tests to preimage pallet * More tests * Fix benchmarks * preimage benchmarks * All preimage benchmarks * Tidy cargo * Update weights.rs * Allow hash provision in benchmarks * Initial work on new benchmarks for Scheduler * Tests working, refactor looks good * Tests for new Scheduler functionality * Use real weight, make tests work with runtimes without Preimage * Rename * Update benchmarks * Formatting * Formatting * Fix weird formatting * Update frame/preimage/src/lib.rs * Fix try-runtime build * Fixes * Fixes * Update frame/support/src/traits/tokens/currency.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * Update frame/support/src/traits/tokens/currency/reservable.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * Update frame/support/src/traits/tokens/imbalance.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * Update frame/preimage/src/mock.rs Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com> * Update frame/scheduler/src/lib.rs Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com> * Update frame/preimage/src/lib.rs * Fixes * Fixes * Formatting * Fixes * Fixes * cargo run --quiet --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_scheduler --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/scheduler/src/weights.rs --template=./.maintain/frame-weight-template.hbs * cargo run --quiet --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_preimage --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/preimage/src/weights.rs --template=./.maintain/frame-weight-template.hbs Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com> Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com> Co-authored-by: Parity Bot <admin@parity.io>
This commit is contained in:
@@ -0,0 +1,233 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2017-2021 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! # Scheduler tests.
|
||||
|
||||
use super::*;
|
||||
use crate::mock::*;
|
||||
|
||||
use frame_support::{assert_noop, assert_ok};
|
||||
use pallet_balances::Error as BalancesError;
|
||||
|
||||
#[test]
|
||||
fn user_note_preimage_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(Preimage::note_preimage(Origin::signed(2), vec![1]));
|
||||
assert_eq!(Balances::reserved_balance(2), 3);
|
||||
assert_eq!(Balances::free_balance(2), 97);
|
||||
|
||||
let h = hashed([1]);
|
||||
assert!(Preimage::have_preimage(&h));
|
||||
assert_eq!(Preimage::get_preimage(&h), Some(vec![1]));
|
||||
|
||||
assert_noop!(
|
||||
Preimage::note_preimage(Origin::signed(2), vec![1]),
|
||||
Error::<Test>::AlreadyNoted
|
||||
);
|
||||
assert_noop!(
|
||||
Preimage::note_preimage(Origin::signed(0), vec![2]),
|
||||
BalancesError::<Test>::InsufficientBalance
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn manager_note_preimage_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(Preimage::note_preimage(Origin::signed(1), vec![1]));
|
||||
assert_eq!(Balances::reserved_balance(1), 0);
|
||||
assert_eq!(Balances::free_balance(1), 100);
|
||||
|
||||
let h = hashed([1]);
|
||||
assert!(Preimage::have_preimage(&h));
|
||||
assert_eq!(Preimage::get_preimage(&h), Some(vec![1]));
|
||||
|
||||
assert_noop!(
|
||||
Preimage::note_preimage(Origin::signed(1), vec![1]),
|
||||
Error::<Test>::AlreadyNoted
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn user_unnote_preimage_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(Preimage::note_preimage(Origin::signed(2), vec![1]));
|
||||
assert_noop!(
|
||||
Preimage::unnote_preimage(Origin::signed(3), hashed([1])),
|
||||
Error::<Test>::NotAuthorized
|
||||
);
|
||||
assert_noop!(
|
||||
Preimage::unnote_preimage(Origin::signed(2), hashed([2])),
|
||||
Error::<Test>::NotNoted
|
||||
);
|
||||
|
||||
assert_ok!(Preimage::unnote_preimage(Origin::signed(2), hashed([1])));
|
||||
assert_noop!(
|
||||
Preimage::unnote_preimage(Origin::signed(2), hashed([1])),
|
||||
Error::<Test>::NotNoted
|
||||
);
|
||||
|
||||
let h = hashed([1]);
|
||||
assert!(!Preimage::have_preimage(&h));
|
||||
assert_eq!(Preimage::get_preimage(&h), None);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn manager_unnote_preimage_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(Preimage::note_preimage(Origin::signed(1), vec![1]));
|
||||
assert_ok!(Preimage::unnote_preimage(Origin::signed(1), hashed([1])));
|
||||
assert_noop!(
|
||||
Preimage::unnote_preimage(Origin::signed(1), hashed([1])),
|
||||
Error::<Test>::NotNoted
|
||||
);
|
||||
|
||||
let h = hashed([1]);
|
||||
assert!(!Preimage::have_preimage(&h));
|
||||
assert_eq!(Preimage::get_preimage(&h), None);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn manager_unnote_user_preimage_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(Preimage::note_preimage(Origin::signed(2), vec![1]));
|
||||
assert_noop!(
|
||||
Preimage::unnote_preimage(Origin::signed(3), hashed([1])),
|
||||
Error::<Test>::NotAuthorized
|
||||
);
|
||||
assert_noop!(
|
||||
Preimage::unnote_preimage(Origin::signed(2), hashed([2])),
|
||||
Error::<Test>::NotNoted
|
||||
);
|
||||
|
||||
assert_ok!(Preimage::unnote_preimage(Origin::signed(1), hashed([1])));
|
||||
|
||||
let h = hashed([1]);
|
||||
assert!(!Preimage::have_preimage(&h));
|
||||
assert_eq!(Preimage::get_preimage(&h), None);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn requested_then_noted_preimage_cannot_be_unnoted() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(Preimage::note_preimage(Origin::signed(1), vec![1]));
|
||||
assert_ok!(Preimage::request_preimage(Origin::signed(1), hashed([1])));
|
||||
assert_noop!(
|
||||
Preimage::unnote_preimage(Origin::signed(1), hashed([1])),
|
||||
Error::<Test>::Requested
|
||||
);
|
||||
|
||||
let h = hashed([1]);
|
||||
assert!(Preimage::have_preimage(&h));
|
||||
assert_eq!(Preimage::get_preimage(&h), Some(vec![1]));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn request_note_order_makes_no_difference() {
|
||||
let one_way = new_test_ext().execute_with(|| {
|
||||
assert_ok!(Preimage::request_preimage(Origin::signed(1), hashed([1])));
|
||||
assert_ok!(Preimage::note_preimage(Origin::signed(1), vec![1]));
|
||||
(
|
||||
StatusFor::<Test>::iter().collect::<Vec<_>>(),
|
||||
PreimageFor::<Test>::iter().collect::<Vec<_>>(),
|
||||
)
|
||||
});
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(Preimage::note_preimage(Origin::signed(1), vec![1]));
|
||||
assert_ok!(Preimage::request_preimage(Origin::signed(1), hashed([1])));
|
||||
let other_way = (
|
||||
StatusFor::<Test>::iter().collect::<Vec<_>>(),
|
||||
PreimageFor::<Test>::iter().collect::<Vec<_>>(),
|
||||
);
|
||||
assert_eq!(one_way, other_way);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn requested_then_user_noted_preimage_is_free() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(Preimage::request_preimage(Origin::signed(1), hashed([1])));
|
||||
assert_ok!(Preimage::note_preimage(Origin::signed(2), vec![1]));
|
||||
assert_eq!(Balances::reserved_balance(2), 0);
|
||||
assert_eq!(Balances::free_balance(2), 100);
|
||||
|
||||
let h = hashed([1]);
|
||||
assert!(Preimage::have_preimage(&h));
|
||||
assert_eq!(Preimage::get_preimage(&h), Some(vec![1]));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn request_user_note_order_makes_no_difference() {
|
||||
let one_way = new_test_ext().execute_with(|| {
|
||||
assert_ok!(Preimage::request_preimage(Origin::signed(1), hashed([1])));
|
||||
assert_ok!(Preimage::note_preimage(Origin::signed(2), vec![1]));
|
||||
(
|
||||
StatusFor::<Test>::iter().collect::<Vec<_>>(),
|
||||
PreimageFor::<Test>::iter().collect::<Vec<_>>(),
|
||||
)
|
||||
});
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(Preimage::note_preimage(Origin::signed(2), vec![1]));
|
||||
assert_ok!(Preimage::request_preimage(Origin::signed(1), hashed([1])));
|
||||
let other_way = (
|
||||
StatusFor::<Test>::iter().collect::<Vec<_>>(),
|
||||
PreimageFor::<Test>::iter().collect::<Vec<_>>(),
|
||||
);
|
||||
assert_eq!(one_way, other_way);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unrequest_preimage_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(Preimage::request_preimage(Origin::signed(1), hashed([1])));
|
||||
assert_ok!(Preimage::request_preimage(Origin::signed(1), hashed([1])));
|
||||
assert_ok!(Preimage::note_preimage(Origin::signed(2), vec![1]));
|
||||
assert_noop!(
|
||||
Preimage::unrequest_preimage(Origin::signed(1), hashed([2])),
|
||||
Error::<Test>::NotRequested
|
||||
);
|
||||
|
||||
assert_ok!(Preimage::unrequest_preimage(Origin::signed(1), hashed([1])));
|
||||
assert!(Preimage::have_preimage(&hashed([1])));
|
||||
|
||||
assert_ok!(Preimage::unrequest_preimage(Origin::signed(1), hashed([1])));
|
||||
assert_noop!(
|
||||
Preimage::unrequest_preimage(Origin::signed(1), hashed([1])),
|
||||
Error::<Test>::NotRequested
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn user_noted_then_requested_preimage_is_refunded_once_only() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(Preimage::note_preimage(Origin::signed(2), vec![1; 3]));
|
||||
assert_ok!(Preimage::note_preimage(Origin::signed(2), vec![1]));
|
||||
assert_ok!(Preimage::request_preimage(Origin::signed(1), hashed([1])));
|
||||
assert_ok!(Preimage::unrequest_preimage(Origin::signed(1), hashed([1])));
|
||||
// Still have reserve from `vec[1; 3]`.
|
||||
assert_eq!(Balances::reserved_balance(2), 5);
|
||||
assert_eq!(Balances::free_balance(2), 95);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user