Newer
Older
// This file is part of Substrate.
// Copyright (C) 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.
#![cfg(test)]
use crate::{core_mask::*, mock::*, *};
use frame_support::{
assert_noop, assert_ok,
traits::nonfungible::{Inspect as NftInspect, Mutate, Transfer},
BoundedVec,
};
use frame_system::RawOrigin::Root;
Bastian Köcher
committed
use pretty_assertions::assert_eq;
use sp_runtime::{traits::Get, TokenError};
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use CoreAssignment::*;
use CoretimeTraceItem::*;
use Finality::*;
#[test]
fn basic_initialize_works() {
TestExt::new().execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
assert_eq!(CoretimeTrace::get(), vec![]);
assert_eq!(Broker::current_timeslice(), 0);
});
}
#[test]
fn drop_region_works() {
TestExt::new().endow(1, 1000).execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
let region = Broker::do_purchase(1, u64::max_value()).unwrap();
assert_ok!(Broker::do_assign(region, Some(1), 1001, Provisional));
advance_to(11);
assert_noop!(Broker::do_drop_region(region), Error::<Test>::StillValid);
advance_to(12);
// assignment worked.
let just_1001 = vec![(Task(1001), 57600)];
let just_pool = vec![(Pool, 57600)];
assert_eq!(
CoretimeTrace::get(),
vec![
(6, AssignCore { core: 0, begin: 8, assignment: just_1001, end_hint: None }),
(12, AssignCore { core: 0, begin: 14, assignment: just_pool, end_hint: None }),
]
);
// `region` still exists as it was never finalized.
assert_eq!(Regions::<Test>::iter().count(), 1);
assert_ok!(Broker::do_drop_region(region));
assert_eq!(Regions::<Test>::iter().count(), 0);
assert_noop!(Broker::do_drop_region(region), Error::<Test>::UnknownRegion);
});
}
#[test]
fn drop_renewal_works() {
TestExt::new().endow(1, 1000).execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
let region = Broker::do_purchase(1, u64::max_value()).unwrap();
assert_ok!(Broker::do_assign(region, Some(1), 1001, Final));
advance_to(11);
let e = Error::<Test>::StillValid;
assert_noop!(Broker::do_drop_renewal(region.core, region.begin + 3), e);
advance_to(12);
assert_eq!(AllowedRenewals::<Test>::iter().count(), 1);
assert_ok!(Broker::do_drop_renewal(region.core, region.begin + 3));
assert_eq!(AllowedRenewals::<Test>::iter().count(), 0);
let e = Error::<Test>::UnknownRenewal;
assert_noop!(Broker::do_drop_renewal(region.core, region.begin + 3), e);
});
}
#[test]
fn drop_contribution_works() {
TestExt::new().contribution_timeout(3).endow(1, 1000).execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
let region = Broker::do_purchase(1, u64::max_value()).unwrap();
// Place region in pool. Active in pool timeslices 4, 5, 6 = rcblocks 8, 10, 12; we
// expect the contribution record to timeout 3 timeslices following 7 = 14
//
// Due to the contribution_timeout being configured for 3 timeslices, the contribution
// can only be discarded at timeslice 10, i.e. rcblock 20.
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
assert_ok!(Broker::do_pool(region, Some(1), 1, Final));
assert_eq!(InstaPoolContribution::<Test>::iter().count(), 1);
advance_to(19);
assert_noop!(Broker::do_drop_contribution(region), Error::<Test>::StillValid);
advance_to(20);
assert_ok!(Broker::do_drop_contribution(region));
assert_eq!(InstaPoolContribution::<Test>::iter().count(), 0);
assert_noop!(Broker::do_drop_contribution(region), Error::<Test>::UnknownContribution);
});
}
#[test]
fn drop_history_works() {
TestExt::new()
.contribution_timeout(4)
.endow(1, 1000)
.endow(2, 30)
.execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
let mut region = Broker::do_purchase(1, u64::max_value()).unwrap();
// Place region in pool. Active in pool timeslices 4, 5, 6 = rcblocks 8, 10, 12; we
// expect to make/receive revenue reports on blocks 10, 12, 14.
assert_ok!(Broker::do_pool(region, Some(1), 1, Final));
assert_ok!(Broker::do_purchase_credit(2, 30, 2));
advance_to(6);
// In the stable state with no pending payouts, we expect to see 3 items in
// InstaPoolHistory here since there is a latency of 1 timeslice (for generating the
// revenue report), the forward notice period (equivalent to another timeslice) and a
// block between the revenue report being requested and the response being processed.
assert_eq!(InstaPoolHistory::<Test>::iter().count(), 3);
advance_to(7);
// One block later, the most recent report will have been processed, so the effective
// queue drops to 2 items.
assert_eq!(InstaPoolHistory::<Test>::iter().count(), 2);
advance_to(8);
assert_eq!(InstaPoolHistory::<Test>::iter().count(), 3);
assert_ok!(TestCoretimeProvider::spend_instantaneous(2, 10));
advance_to(10);
assert_eq!(InstaPoolHistory::<Test>::iter().count(), 3);
assert_ok!(TestCoretimeProvider::spend_instantaneous(2, 10));
advance_to(12);
assert_eq!(InstaPoolHistory::<Test>::iter().count(), 4);
assert_ok!(TestCoretimeProvider::spend_instantaneous(2, 10));
advance_to(14);
assert_eq!(InstaPoolHistory::<Test>::iter().count(), 5);
advance_to(16);
assert_eq!(InstaPoolHistory::<Test>::iter().count(), 6);
advance_to(17);
assert_noop!(Broker::do_drop_history(u32::MAX), Error::<Test>::StillValid);
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
assert_noop!(Broker::do_drop_history(region.begin), Error::<Test>::StillValid);
advance_to(18);
assert_eq!(InstaPoolHistory::<Test>::iter().count(), 6);
// Block 18 is 8 blocks ()= 4 timeslices = contribution timeout) after first region.
// Its revenue should now be droppable.
assert_ok!(Broker::do_drop_history(region.begin));
assert_eq!(InstaPoolHistory::<Test>::iter().count(), 5);
assert_noop!(Broker::do_drop_history(region.begin), Error::<Test>::NoHistory);
advance_to(19);
region.begin += 1;
assert_noop!(Broker::do_drop_history(region.begin), Error::<Test>::StillValid);
advance_to(20);
assert_ok!(Broker::do_drop_history(region.begin));
assert_eq!(InstaPoolHistory::<Test>::iter().count(), 4);
assert_noop!(Broker::do_drop_history(region.begin), Error::<Test>::NoHistory);
advance_to(21);
region.begin += 1;
assert_noop!(Broker::do_drop_history(region.begin), Error::<Test>::StillValid);
advance_to(22);
assert_ok!(Broker::do_drop_history(region.begin));
assert_eq!(InstaPoolHistory::<Test>::iter().count(), 3);
assert_noop!(Broker::do_drop_history(region.begin), Error::<Test>::NoHistory);
});
}
#[test]
fn request_core_count_works() {
TestExt::new().execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 0));
assert_ok!(Broker::request_core_count(RuntimeOrigin::root(), 1));
advance_to(12);
let assignment = vec![(Pool, 57600)];
assert_eq!(
CoretimeTrace::get(),
vec![(12, AssignCore { core: 0, begin: 14, assignment, end_hint: None })],
);
});
}
#[test]
fn transfer_works() {
TestExt::new().endow(1, 1000).execute_with(|| {
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
let region = Broker::do_purchase(1, u64::max_value()).unwrap();
assert_ok!(<Broker as Transfer<_>>::transfer(®ion.into(), &2));
assert_eq!(<Broker as NftInspect<_>>::owner(®ion.into()), Some(2));
assert_noop!(Broker::do_assign(region, Some(1), 1001, Final), Error::<Test>::NotOwner);
assert_ok!(Broker::do_assign(region, Some(2), 1002, Final));
});
}
Loading full blame...