Skip to content

Commit 8eeb063

Browse files
feat: implement type safety for tfgrid and smart contract (#361)
1 parent d208d08 commit 8eeb063

28 files changed

+2814
-1096
lines changed

substrate-node/Cargo.lock

Lines changed: 9 additions & 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/src/lib.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![cfg_attr(not(feature = "std"), no_std)]
22
#![recursion_limit = "128"]
33

4+
use pallet_tfgrid::pallet::{InterfaceOf, PubConfigOf};
45
use sp_runtime::traits::Hash;
56
use sp_std::prelude::*;
67

@@ -42,7 +43,10 @@ pub mod pallet {
4243
use super::*;
4344
use frame_support::pallet_prelude::*;
4445
use frame_system::pallet_prelude::*;
46+
use pallet_tfgrid::farm::FarmName;
47+
use pallet_tfgrid::pub_ip::{GatewayIP, PublicIP};
4548
use sp_std::convert::TryInto;
49+
use tfchain_support::types::PublicIP as SupportPublicIP;
4650

4751
#[pallet::config]
4852
pub trait Config:
@@ -66,8 +70,12 @@ pub mod pallet {
6670
/// The minimum amount of vetos to dissaprove a proposal
6771
type MinVetos: Get<u32>;
6872

69-
type Tfgrid: Tfgrid<Self::AccountId>;
70-
type NodeChanged: ChangeNode;
73+
type Tfgrid: Tfgrid<
74+
Self::AccountId,
75+
FarmName<Self>,
76+
SupportPublicIP<PublicIP<Self>, GatewayIP<Self>>,
77+
>;
78+
type NodeChanged: ChangeNode<PubConfigOf<Self>, InterfaceOf<Self>>;
7179

7280
/// Weight information for extrinsics in this pallet.
7381
type WeightInfo: WeightInfo;
@@ -510,8 +518,11 @@ impl<T: Config> Pallet<T> {
510518
}
511519
}
512520

513-
impl<T: Config> ChangeNode for Pallet<T> {
514-
fn node_changed(old_node: Option<&Node>, new_node: &Node) {
521+
impl<T: Config> ChangeNode<PubConfigOf<T>, InterfaceOf<T>> for Pallet<T> {
522+
fn node_changed(
523+
old_node: Option<&Node<PubConfigOf<T>, InterfaceOf<T>>>,
524+
new_node: &Node<PubConfigOf<T>, InterfaceOf<T>>,
525+
) {
515526
let new_node_weight = Self::get_node_weight(new_node.resources);
516527
match old_node {
517528
Some(node) => {
@@ -542,7 +553,7 @@ impl<T: Config> ChangeNode for Pallet<T> {
542553
};
543554
}
544555

545-
fn node_deleted(node: &Node) {
556+
fn node_deleted(node: &Node<PubConfigOf<T>, InterfaceOf<T>>) {
546557
let node_weight = Self::get_node_weight(node.resources);
547558
let mut farm_weight = FarmWeight::<T>::get(node.farm_id);
548559
farm_weight = farm_weight.checked_sub(node_weight).unwrap_or(0);

substrate-node/pallets/pallet-dao/src/mock.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ use frame_support::{construct_runtime, parameter_types, traits::ConstU32};
44
use frame_system::EnsureRoot;
55
use pallet_collective;
66
use pallet_tfgrid;
7+
use pallet_tfgrid::{
8+
farm::FarmName,
9+
pub_config::{Domain, GW4, GW6, IP4, IP6},
10+
pub_ip::{GatewayIP, PublicIP},
11+
twin::TwinIp,
12+
};
713
use pallet_timestamp;
814
use sp_core::H256;
915
use sp_runtime::{
@@ -72,13 +78,14 @@ parameter_types! {
7278
pub const MinVetos: u32 = 2;
7379
}
7480

81+
pub(crate) type PubConfig = pallet_tfgrid::pallet::PubConfigOf<Test>;
7582
pub struct NodeChanged;
76-
impl ChangeNode for NodeChanged {
77-
fn node_changed(old_node: Option<&Node>, new_node: &Node) {
83+
impl ChangeNode<PubConfig> for NodeChanged {
84+
fn node_changed(old_node: Option<&Node<PubConfig>>, new_node: &Node<PubConfig>) {
7885
DaoModule::node_changed(old_node, new_node)
7986
}
8087

81-
fn node_deleted(node: &Node) {
88+
fn node_deleted(node: &Node<PubConfig>) {
8289
DaoModule::node_deleted(node);
8390
}
8491
}
@@ -99,14 +106,32 @@ parameter_types! {
99106
pub const MaxFarmNameLength: u32 = 40;
100107
}
101108

109+
pub(crate) type TestTwinIp = TwinIp<Test>;
110+
pub(crate) type TestFarmName = FarmName<Test>;
111+
pub(crate) type TestPublicIP = PublicIP<Test>;
112+
pub(crate) type TestGatewayIP = GatewayIP<Test>;
113+
114+
pub(crate) type TestIP4 = IP4<Test>;
115+
pub(crate) type TestGW4 = GW4<Test>;
116+
pub(crate) type TestIP6 = IP6<Test>;
117+
pub(crate) type TestGW6 = GW6<Test>;
118+
pub(crate) type TestDomain = Domain<Test>;
119+
102120
impl pallet_tfgrid::Config for Test {
103121
type Event = Event;
104122
type RestrictedOrigin = EnsureRoot<Self::AccountId>;
105123
type WeightInfo = pallet_tfgrid::weights::SubstrateWeight<Test>;
106124
type NodeChanged = NodeChanged;
107-
type TwinIp = pallet_tfgrid::twin::TwinIp<Test>;
125+
type TwinIp = TestTwinIp;
126+
type FarmName = TestFarmName;
108127
type MaxFarmNameLength = MaxFarmNameLength;
109-
type FarmName = pallet_tfgrid::farm::FarmName<Test>;
128+
type PublicIP = TestPublicIP;
129+
type GatewayIP = TestGatewayIP;
130+
type IP4 = TestIP4;
131+
type GW4 = TestGW4;
132+
type IP6 = TestIP6;
133+
type GW6 = TestGW6;
134+
type Domain = TestDomain;
110135
}
111136

112137
impl pallet_timestamp::Config for Test {
@@ -147,6 +172,10 @@ impl pallet_membership::Config<pallet_membership::Instance1> for Test {
147172
type WeightInfo = pallet_membership::weights::SubstrateWeight<Test>;
148173
}
149174

175+
pub(crate) fn get_twin_ip(twin_ip_input: &[u8]) -> TestTwinIp {
176+
TwinIp::try_from(twin_ip_input.to_vec()).expect("Invalid twin ip input.")
177+
}
178+
150179
// Build genesis storage according to the mock runtime.
151180
pub fn new_test_ext() -> sp_io::TestExternalities {
152181
let mut t = frame_system::GenesisConfig::default()

substrate-node/pallets/pallet-dao/src/tests.rs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,8 @@ fn motion_approval_works() {
296296
assert_ok!(DaoModule::vote(Origin::signed(11), 2, hash.clone(), true));
297297

298298
// // Check farm certification type before we close
299-
let f1 = TfgridModule::farms(1);
300-
assert_eq!(f1.certification, FarmCertification::NotCertified);
299+
let farm_1 = TfgridModule::farms(1).unwrap();
300+
assert_eq!(farm_1.certification, FarmCertification::NotCertified);
301301

302302
System::set_block_number(5);
303303
assert_ok!(DaoModule::close(Origin::signed(2), hash.clone(), 0,));
@@ -366,8 +366,8 @@ fn motion_approval_works() {
366366
);
367367

368368
// // FarmCertification type of farm should be set to certified.
369-
let f1 = TfgridModule::farms(1);
370-
assert_eq!(f1.certification, FarmCertification::Gold);
369+
let farm_1 = TfgridModule::farms(1).unwrap();
370+
assert_eq!(farm_1.certification, FarmCertification::Gold);
371371
});
372372
}
373373

@@ -507,8 +507,8 @@ fn weighted_voting_works() {
507507
assert_ok!(DaoModule::vote(Origin::signed(11), 2, hash.clone(), false));
508508

509509
// // Check farm certification type before we close
510-
let f1 = TfgridModule::farms(1);
511-
assert_eq!(f1.certification, FarmCertification::NotCertified);
510+
let farm_1 = TfgridModule::farms(1).unwrap();
511+
assert_eq!(farm_1.certification, FarmCertification::NotCertified);
512512

513513
System::set_block_number(5);
514514
assert_ok!(DaoModule::close(Origin::signed(2), hash.clone(), 0,));
@@ -570,8 +570,8 @@ fn weighted_voting_works() {
570570
);
571571

572572
// FarmCertification type of farm should still be the same.
573-
let f1 = TfgridModule::farms(1);
574-
assert_eq!(f1.certification, FarmCertification::NotCertified);
573+
let farm_1 = TfgridModule::farms(1).unwrap();
574+
assert_eq!(farm_1.certification, FarmCertification::NotCertified);
575575
});
576576
}
577577

@@ -602,7 +602,7 @@ fn voting_tfgridmodule_call_works() {
602602
assert_ok!(DaoModule::vote(Origin::signed(11), 2, hash.clone(), true));
603603

604604
// Check connection price of node 1
605-
let n1 = TfgridModule::nodes(1);
605+
let n1 = TfgridModule::nodes(1).unwrap();
606606
assert_eq!(n1.connection_price, 80);
607607

608608
System::set_block_number(5);
@@ -677,7 +677,7 @@ fn voting_tfgridmodule_call_works() {
677677
// Connection price should have been modified, any new node should have set the new price
678678
prepare_twin(15);
679679
prepare_node(15, 1);
680-
let n3 = TfgridModule::nodes(3);
680+
let n3 = TfgridModule::nodes(3).unwrap();
681681
assert_eq!(n3.connection_price, 100);
682682
});
683683
}
@@ -780,10 +780,12 @@ pub fn prepare_twin(account_id: u64) {
780780
document.clone(),
781781
hash.clone(),
782782
));
783-
let ip = "::1";
784-
let ip_v = ip.as_bytes().to_vec();
785-
let ip_bv: BoundedVec<u8, ConstU32<39>> = BoundedVec::try_from(ip_v).unwrap();
786-
TfgridModule::create_twin(Origin::signed(account_id), ip_bv).unwrap();
783+
784+
let ip = get_twin_ip(b"::1");
785+
assert_ok!(TfgridModule::create_twin(
786+
Origin::signed(account_id),
787+
ip.clone().0
788+
));
787789
}
788790

789791
const GIGABYTE: u64 = 1024 * 1024 * 1024;
@@ -850,17 +852,20 @@ fn prepare_big_node(account_id: u64, farm_id: u32) {
850852
pub fn prepare_farm(account_id: u64, farm_name: Vec<u8>) {
851853
let mut pub_ips = Vec::new();
852854
pub_ips.push(PublicIP {
853-
ip: "1.1.1.0".as_bytes().to_vec(),
854-
gateway: "1.1.1.1".as_bytes().to_vec(),
855+
ip: "185.206.122.33/24".as_bytes().to_vec().try_into().unwrap(),
856+
gateway: "185.206.122.1".as_bytes().to_vec().try_into().unwrap(),
855857
contract_id: 0,
856858
});
857859

858-
let farm_name_bv = BoundedVec::try_from(farm_name).unwrap();
859-
TfgridModule::create_farm(Origin::signed(account_id), farm_name_bv, pub_ips.clone()).unwrap();
860+
assert_ok!(TfgridModule::create_farm(
861+
Origin::signed(account_id),
862+
farm_name.try_into().unwrap(),
863+
pub_ips.clone(),
864+
));
860865
}
861866

862867
fn create_farming_policies() {
863-
let name = "farm1".as_bytes().to_vec();
868+
let name = "farm_1".as_bytes().to_vec();
864869
assert_ok!(TfgridModule::create_farming_policy(
865870
RawOrigin::Root.into(),
866871
name,

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,11 @@ pallet-timestamp = { git = "https://github.com/paritytech/substrate", branch =
4040
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 }
43+
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
4344

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

47-
[dev-dependencies]
48-
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
49-
5048
[features]
5149
default = ['std']
5250
std = [
@@ -65,6 +63,7 @@ std = [
6563
'frame-benchmarking/std',
6664
'sp-io/std',
6765
'frame-try-runtime/std',
66+
'sp-core/std'
6867
]
6968
runtime-benchmarks = [
7069
"frame-benchmarking",
@@ -73,4 +72,4 @@ runtime-benchmarks = [
7372
]
7473
try-runtime = [
7574
"frame-support/try-runtime"
76-
]
75+
]

0 commit comments

Comments
 (0)