Skip to content

Commit 3077055

Browse files
feat: only fire billing on valdiator that created block (#555)
1 parent bec4591 commit 3077055

File tree

11 files changed

+386
-158
lines changed

11 files changed

+386
-158
lines changed

substrate-node/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

substrate-node/pallets/pallet-dao/Cargo.toml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,3 @@ std = [
5353
'scale-info/std',
5454
'serde/std',
5555
]
56-
runtime-benchmarks = [
57-
"frame-benchmarking",
58-
"frame-support/runtime-benchmarks",
59-
"frame-system/runtime-benchmarks"
60-
]

substrate-node/pallets/pallet-smart-contract/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@ tfchain-support = { path = "../../support", default-features = false }
4141
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
4242
frame-try-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false, optional = true }
4343
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
44+
pallet-authorship = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
45+
parking_lot = '0.12.1'
4446

4547
# Benchmarking
4648
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false, optional = true }
4749

4850
[dev-dependencies]
49-
parking_lot = '0.12.1'
5051
sp-keystore = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
5152
env_logger = "*"
5253

@@ -72,6 +73,7 @@ std = [
7273
]
7374
runtime-benchmarks = [
7475
"frame-benchmarking",
76+
"pallet-balances/runtime-benchmarks",
7577
"frame-support/runtime-benchmarks",
7678
"frame-system/runtime-benchmarks",
7779
]
Lines changed: 169 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,84 @@
1-
// This file is part of Substrate.
2-
3-
// Copyright (C) 2022 Threefold Tech
4-
// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.
5-
// SPDX-License-Identifier: Apache-2.0
6-
7-
// Licensed under the Apache License, Version 2.0 (the "License");
8-
// you may not use this file except in compliance with the License.
9-
// You may obtain a copy of the License at
10-
//
11-
// http://www.apache.org/licenses/LICENSE-2.0
12-
//
13-
// Unless required by applicable law or agreed to in writing, software
14-
// distributed under the License is distributed on an "AS IS" BASIS,
15-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16-
// See the License for the specific language governing permissions and
17-
// limitations under the License.
18-
19-
//! Vesting pallet benchmarking.
20-
211
#![cfg(feature = "runtime-benchmarks")]
222

233
use super::*;
244

25-
use crate::Module as SmartContractModule;
26-
use frame_benchmarking::{account, benchmarks, whitelisted_caller};
27-
use frame_support::traits::{Box, Vec};
5+
use crate::Pallet as SmartContractModule;
6+
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite, whitelisted_caller};
7+
use frame_support::assert_ok;
288
use frame_system::RawOrigin;
29-
use sp_std::vec;
9+
use sp_std::{
10+
convert::{TryFrom, TryInto},
11+
fmt::Debug,
12+
vec,
13+
};
3014

31-
use tfchain_support::types::{Location, Resources, PublicIP};
32-
use pallet_tfgrid;
15+
use pallet_tfgrid::{
16+
types::{self as pallet_tfgrid_types, LocationInput},
17+
CityNameInput, CountryNameInput, DocumentHashInput, DocumentLinkInput, LatitudeInput,
18+
LongitudeInput, ResourcesInput, TwinIpInput,
19+
};
20+
use tfchain_support::{resources::Resources, types::IP4};
21+
const GIGABYTE: u64 = 1024 * 1024 * 1024;
3322

3423
benchmarks! {
24+
where_clause {
25+
where
26+
<T as pallet_timestamp::Config>::Moment: TryFrom<u64>,
27+
<<T as pallet_timestamp::Config>::Moment as TryFrom<u64>>::Error: Debug,
28+
}
29+
3530
create_node_contract {
3631
let a1: T::AccountId = account("Alice", 0, 0);
3732
prepare_farm_and_node::<T>(a1);
3833

3934
let caller: T::AccountId = whitelisted_caller();
4035
create_twin::<T>(caller.clone());
41-
}: _ (RawOrigin::Signed(caller.clone()), 1, "some_data".as_bytes().to_vec(), "hash".as_bytes().to_vec(), 1)
36+
}: _ (RawOrigin::Signed(
37+
caller.clone()),
38+
1,
39+
"858f8fb2184b15ecb8c0be8b95398c81".as_bytes().to_vec().try_into().unwrap(),
40+
"some_data".as_bytes().to_vec().try_into().unwrap(),
41+
1,
42+
None
43+
)
4244
verify {
43-
let contract = SmartContractModule::<T>::contracts(1);
45+
let contract = SmartContractModule::<T>::contracts(1).unwrap();
4446
assert_eq!(
4547
contract.contract_id, 1
4648
);
4749
}
4850

49-
add_reports {
51+
add_nru_reports {
52+
let stamp: u64 = 1628082000 * 1000;
53+
pallet_timestamp::Pallet::<T>::set_timestamp(stamp.try_into().unwrap());
54+
55+
let a1: T::AccountId = account("Alice", 0, 0);
56+
prepare_farm_and_node::<T>(a1.clone());
57+
58+
let caller: T::AccountId = whitelisted_caller();
59+
create_twin::<T>(caller.clone());
60+
create_contract::<T>(caller.clone());
61+
62+
let mut reports = Vec::new();
63+
64+
reports.push(types::NruConsumption {
65+
contract_id: 1,
66+
timestamp: 1628082000 * 1000,
67+
window: 1000,
68+
nru: 10 * GIGABYTE,
69+
});
70+
71+
}: _ (RawOrigin::Signed(a1.clone()), reports)
72+
verify {
73+
let contract = SmartContractModule::<T>::contracts(1).unwrap();
74+
assert_eq!(
75+
contract.contract_id, 1
76+
);
77+
}
78+
79+
bill_contract_for_block {
80+
let stamp: u64 = 1628082000 * 1000;
81+
pallet_timestamp::Pallet::<T>::set_timestamp(stamp.try_into().unwrap());
5082
let a1: T::AccountId = account("Alice", 0, 0);
5183
prepare_farm_and_node::<T>(a1.clone());
5284

@@ -56,26 +88,32 @@ benchmarks! {
5688

5789
let mut reports = Vec::new();
5890

59-
let gigabyte = 1000 * 1000 * 1000;
6091
reports.push(types::Consumption {
6192
contract_id: 1,
6293
cru: 2,
6394
hru: 0,
64-
mru: 8 * gigabyte,
65-
sru: 25 * gigabyte,
95+
mru: 8 * GIGABYTE,
96+
sru: 25 * GIGABYTE,
6697
nru: 0,
6798
timestamp: 0,
6899
});
69100

70-
}: _ (RawOrigin::Signed(a1.clone()), reports)
101+
push_contract_resources::<T>(a1.clone());
102+
103+
let stamp: u64 = 1628082000 * 1000 * 10 * 6000;
104+
pallet_timestamp::Pallet::<T>::set_timestamp(stamp.try_into().unwrap());
105+
// run_to_block::<T>(10);
106+
}: _ (RawOrigin::Signed(a1.clone()), 1)
71107
verify {
72-
let contract = SmartContractModule::<T>::contracts(1);
108+
let contract = SmartContractModule::<T>::contracts(1).unwrap();
73109
assert_eq!(
74110
contract.contract_id, 1
75111
);
76112
}
77113
}
78114

115+
impl_benchmark_test_suite! {Pallet, crate::tests::new_test_ext(), crate::tests::Test}
116+
79117
#[cfg(test)]
80118
mod benchmarktests {
81119
use super::*;
@@ -86,70 +124,66 @@ mod benchmarktests {
86124
fn test_benchmarks() {
87125
new_test_ext().execute_with(|| {
88126
assert_ok!(test_benchmark_create_node_contract::<TestRuntime>());
89-
assert_ok!(test_benchmark_add_reports::<TestRuntime>());
127+
assert_ok!(test_benchmark_add_nru_reports::<TestRuntime>());
90128
});
91129
}
92130
}
93131

94132
pub fn create_twin<T: Config>(source: T::AccountId) {
95-
let document = "some_link".as_bytes().to_vec();
96-
let hash = "some_hash".as_bytes().to_vec();
97-
98-
pallet_tfgrid::Module::<T>::user_accept_tc(
133+
assert_ok!(pallet_tfgrid::Pallet::<T>::user_accept_tc(
99134
RawOrigin::Signed(source.clone()).into(),
100-
document.clone(),
101-
hash.clone(),
102-
)
103-
.unwrap();
104-
let ip = "10.2.3.3";
105-
pallet_tfgrid::Module::<T>::create_twin(
135+
get_document_link_input(b"some_link"),
136+
get_document_hash_input(b"some_hash"),
137+
));
138+
139+
let ip = get_twin_ip_input(b"::1");
140+
assert_ok!(pallet_tfgrid::Pallet::<T>::create_twin(
106141
RawOrigin::Signed(source).into(),
107-
ip.as_bytes().to_vec(),
108-
)
109-
.unwrap();
142+
ip
143+
));
110144
}
111145

112146
pub fn prepare_farm_and_node<T: Config>(source: T::AccountId) {
113147
create_twin::<T>(source.clone());
114148
prepare_farm::<T>(source.clone());
115149

116-
// random location
117-
let location = Location {
118-
longitude: "12.233213231".as_bytes().to_vec(),
119-
latitude: "32.323112123".as_bytes().to_vec(),
150+
let resources = ResourcesInput {
151+
hru: 1024 * GIGABYTE,
152+
sru: 512 * GIGABYTE,
153+
cru: 8,
154+
mru: 16 * GIGABYTE,
120155
};
121156

122-
let resources = Resources {
123-
hru: 1,
124-
sru: 1,
125-
cru: 1,
126-
mru: 1,
157+
// random location
158+
let location = LocationInput {
159+
city: get_city_name_input(b"Ghent"),
160+
country: get_country_name_input(b"Belgium"),
161+
latitude: get_latitude_input(b"12.233213231"),
162+
longitude: get_longitude_input(b"32.323112123"),
127163
};
128164

129-
let country = "Belgium".as_bytes().to_vec();
130-
let city = "Ghent".as_bytes().to_vec();
131-
pallet_tfgrid::Module::<T>::create_node(
132-
RawOrigin::Signed(source).into(),
165+
assert_ok!(pallet_tfgrid::Pallet::<T>::create_node(
166+
RawOrigin::Signed(source.clone()).into(),
133167
1,
134168
resources,
135169
location,
136-
country,
137-
city,
138-
Vec::new(),
170+
Vec::new().try_into().unwrap(),
139171
false,
140172
false,
141-
"some_serial".as_bytes().to_vec(),
142-
)
143-
.unwrap();
173+
None,
174+
));
144175
}
145176

146177
pub fn prepare_farm<T: Config>(source: T::AccountId) {
147-
let farm_name = "test_farm";
178+
let farm_name = "testfarm";
148179
let mut pub_ips = Vec::new();
149-
pub_ips.push(PublicIP {
150-
ip: "1.1.1.0".as_bytes().to_vec(),
151-
gateway: "1.1.1.1".as_bytes().to_vec(),
152-
contract_id: 0,
180+
pub_ips.push(IP4 {
181+
ip: "185.206.122.33/24".as_bytes().to_vec().try_into().unwrap(),
182+
gw: "185.206.122.1".as_bytes().to_vec().try_into().unwrap(),
183+
});
184+
pub_ips.push(IP4 {
185+
ip: "185.206.122.34/24".as_bytes().to_vec().try_into().unwrap(),
186+
gw: "185.206.122.1".as_bytes().to_vec().try_into().unwrap(),
153187
});
154188

155189
let su_policy = pallet_tfgrid_types::Policy {
@@ -180,7 +214,7 @@ pub fn prepare_farm<T: Config>(source: T::AccountId) {
180214
let x1 = account("ferdie", 0, 2);
181215
let x2 = account("eve", 0, 3);
182216

183-
pallet_tfgrid::Module::<T>::create_pricing_policy(
217+
assert_ok!(pallet_tfgrid::Pallet::<T>::create_pricing_policy(
184218
RawOrigin::Root.into(),
185219
"policy_1".as_bytes().to_vec(),
186220
su_policy,
@@ -191,24 +225,72 @@ pub fn prepare_farm<T: Config>(source: T::AccountId) {
191225
domain_name_policy,
192226
x1,
193227
x2,
194-
80
195-
)
196-
.unwrap();
228+
80,
229+
));
197230

198-
pallet_tfgrid::Module::<T>::create_farm(
231+
assert_ok!(pallet_tfgrid::Pallet::<T>::create_farm(
199232
RawOrigin::Signed(source).into(),
200-
farm_name.as_bytes().to_vec(),
201-
pub_ips.clone(),
202-
)
203-
.unwrap();
233+
farm_name.as_bytes().to_vec().try_into().unwrap(),
234+
pub_ips.clone().try_into().unwrap(),
235+
));
204236
}
205237

206238
pub fn create_contract<T: Config>(source: T::AccountId) {
207-
SmartContractModule::<T>::create_node_contract(
239+
assert_ok!(SmartContractModule::<T>::create_node_contract(
208240
RawOrigin::Signed(source).into(),
209241
1,
210-
"some_data123".as_bytes().to_vec(),
211-
"hash123".as_bytes().to_vec(),
212-
0
213-
).unwrap()
214-
}
242+
"858f8fb2184b15ecb8c0be8b95398c81"
243+
.as_bytes()
244+
.to_vec()
245+
.try_into()
246+
.unwrap(),
247+
"some_data123".as_bytes().to_vec().try_into().unwrap(),
248+
0,
249+
None,
250+
));
251+
}
252+
253+
pub fn push_contract_resources<T: Config>(source: T::AccountId) {
254+
let contract_resources = vec![types::ContractResources {
255+
contract_id: 1,
256+
used: Resources {
257+
sru: 150 * GIGABYTE,
258+
cru: 16,
259+
mru: 8 * GIGABYTE,
260+
hru: 0,
261+
},
262+
}];
263+
264+
assert_ok!(SmartContractModule::<T>::report_contract_resources(
265+
RawOrigin::Signed(source).into(),
266+
contract_resources,
267+
));
268+
}
269+
270+
pub(crate) fn get_city_name_input(city_input: &[u8]) -> CityNameInput {
271+
BoundedVec::try_from(city_input.to_vec()).expect("Invalid city name input.")
272+
}
273+
274+
pub(crate) fn get_country_name_input(country_input: &[u8]) -> CountryNameInput {
275+
BoundedVec::try_from(country_input.to_vec()).expect("Invalid country name input.")
276+
}
277+
278+
pub(crate) fn get_latitude_input(latitude_input: &[u8]) -> LatitudeInput {
279+
BoundedVec::try_from(latitude_input.to_vec()).expect("Invalid latitude input.")
280+
}
281+
282+
pub(crate) fn get_longitude_input(longitude_input: &[u8]) -> LongitudeInput {
283+
BoundedVec::try_from(longitude_input.to_vec()).expect("Invalid longitude input.")
284+
}
285+
286+
pub(crate) fn get_document_link_input(document_link_input: &[u8]) -> DocumentLinkInput {
287+
BoundedVec::try_from(document_link_input.to_vec()).expect("Invalid document link input.")
288+
}
289+
290+
pub(crate) fn get_document_hash_input(document_hash_input: &[u8]) -> DocumentHashInput {
291+
BoundedVec::try_from(document_hash_input.to_vec()).expect("Invalid document hash input.")
292+
}
293+
294+
pub(crate) fn get_twin_ip_input(twin_ip_input: &[u8]) -> TwinIpInput {
295+
BoundedVec::try_from(twin_ip_input.to_vec()).expect("Invalid twin ip input.")
296+
}

0 commit comments

Comments
 (0)