Skip to content

Commit 8c1eca5

Browse files
feat: allow twin to connect to a relay (#570)
1 parent c8fc850 commit 8c1eca5

File tree

20 files changed

+321
-154
lines changed

20 files changed

+321
-154
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# 4. Twin relay address
2+
3+
Date: 2023-01-11
4+
5+
## Status
6+
7+
Accepted
8+
9+
## Context
10+
11+
See [here](https://github.com/threefoldtech/tfchain/issues/569) for more details.
12+
13+
## Decision
14+
15+
We decided to rework the twin Object, removing:
16+
17+
- Version
18+
- IP
19+
20+
and Adding:
21+
22+
- Relay
23+
- Pk (PublicKey)
24+
25+
We removed the version because we don't use that anymore. The IP becomes a relay address, this is some dns name where the twin can be contacted through. This will be set by RMB.
26+
The Pk fields is a public key used for encrypting twin messages, with this public key anyone can verify the source.

substrate-node/Cargo.lock

Lines changed: 4 additions & 3 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/mock.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@ use pallet_tfgrid::{
99
interface::{InterfaceIp, InterfaceMac, InterfaceName},
1010
node::{Location, SerialNumber},
1111
terms_cond::TermsAndConditions,
12-
twin::TwinIp,
13-
DocumentHashInput, DocumentLinkInput, TwinIpInput,
14-
};
15-
use pallet_tfgrid::{
16-
CityNameInput, CountryNameInput, Gw4Input, Ip4Input, LatitudeInput, LongitudeInput,
12+
CityNameInput, CountryNameInput, DocumentHashInput, DocumentLinkInput, Gw4Input, Ip4Input,
13+
LatitudeInput, LongitudeInput, PkInput, RelayInput,
1714
};
1815
use pallet_timestamp;
1916
use sp_core::H256;
@@ -123,7 +120,6 @@ parameter_types! {
123120

124121
pub(crate) type TestTermsAndConditions = TermsAndConditions<Test>;
125122

126-
pub(crate) type TestTwinIp = TwinIp<Test>;
127123
pub(crate) type TestFarmName = FarmName<Test>;
128124

129125
pub(crate) type TestInterfaceName = InterfaceName<Test>;
@@ -142,7 +138,6 @@ impl pallet_tfgrid::Config for Test {
142138
type NodeChanged = NodeChanged;
143139
type PublicIpModifier = PublicIpModifierType;
144140
type TermsAndConditions = TestTermsAndConditions;
145-
type TwinIp = TestTwinIp;
146141
type FarmName = TestFarmName;
147142
type MaxFarmNameLength = MaxFarmNameLength;
148143
type MaxFarmPublicIps = MaxFarmPublicIps;
@@ -203,8 +198,12 @@ pub(crate) fn get_document_hash_input(document_hash_input: &[u8]) -> DocumentHas
203198
BoundedVec::try_from(document_hash_input.to_vec()).expect("Invalid document hash input.")
204199
}
205200

206-
pub(crate) fn get_twin_ip_input(twin_ip_input: &[u8]) -> TwinIpInput {
207-
BoundedVec::try_from(twin_ip_input.to_vec()).expect("Invalid twin ip input.")
201+
pub(crate) fn get_relay_input(relay_input: &[u8]) -> RelayInput {
202+
Some(BoundedVec::try_from(relay_input.to_vec()).expect("Invalid relay input."))
203+
}
204+
205+
pub(crate) fn get_public_key_input(pk_input: &[u8]) -> PkInput {
206+
Some(BoundedVec::try_from(pk_input.to_vec()).expect("Invalid document hash input."))
208207
}
209208

210209
pub(crate) fn get_public_ip_ip_input(ip_input: &[u8]) -> Ip4Input {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -834,10 +834,13 @@ pub fn prepare_twin(account_id: u64) {
834834
get_document_hash_input(b"some_hash"),
835835
));
836836

837-
let ip = get_twin_ip_input(b"::1");
837+
let relay = get_relay_input(b"somerelay.io");
838+
let pk =
839+
get_public_key_input(b"0x6c8fd181adc178cea218e168e8549f0b0ff30627c879db9eac4318927e87c901");
838840
assert_ok!(TfgridModule::create_twin(
839841
RuntimeOrigin::signed(account_id),
840-
ip
842+
relay,
843+
pk
841844
));
842845
}
843846

substrate-node/pallets/pallet-smart-contract/src/migrations/v7.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use crate::*;
2-
#[cfg(feature = "try-runtime")]
3-
use codec::{Decode, Encode};
42
use frame_support::{traits::OnRuntimeUpgrade, weights::Weight};
53
use log::debug;
64
use sp_std::collections::btree_map::BTreeMap;
@@ -31,7 +29,7 @@ impl<T: Config> OnRuntimeUpgrade for FixTwinLockedBalances<T> {
3129
}
3230

3331
#[cfg(feature = "try-runtime")]
34-
fn post_upgrade(pre_contracts_count: Vec<u8>) -> Result<(), &'static str> {
32+
fn post_upgrade(_: Vec<u8>) -> Result<(), &'static str> {
3533
debug!("current pallet version: {:?}", PalletVersion::<T>::get());
3634
assert!(PalletVersion::<T>::get() >= types::StorageVersion::V7);
3735

substrate-node/pallets/pallet-smart-contract/src/mock.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ use pallet_tfgrid::{
1717
node::{CityName, CountryName},
1818
node::{Location, SerialNumber},
1919
terms_cond::TermsAndConditions,
20-
twin::TwinIp,
2120
CityNameInput, CountryNameInput, DocumentHashInput, DocumentLinkInput, Gw4Input, Ip4Input,
22-
LatitudeInput, LongitudeInput, TwinIpInput,
21+
LatitudeInput, LongitudeInput, PkInput, RelayInput,
2322
};
2423
use parking_lot::RwLock;
2524
use sp_core::{
@@ -198,7 +197,6 @@ parameter_types! {
198197

199198
pub(crate) type TestTermsAndConditions = TermsAndConditions<TestRuntime>;
200199

201-
pub(crate) type TestTwinIp = TwinIp<TestRuntime>;
202200
pub(crate) type TestFarmName = FarmName<TestRuntime>;
203201

204202
pub(crate) type TestInterfaceName = InterfaceName<TestRuntime>;
@@ -217,7 +215,6 @@ impl pallet_tfgrid::Config for TestRuntime {
217215
type NodeChanged = NodeChanged;
218216
type PublicIpModifier = PublicIpModifierType;
219217
type TermsAndConditions = TestTermsAndConditions;
220-
type TwinIp = TestTwinIp;
221218
type FarmName = TestFarmName;
222219
type MaxFarmNameLength = MaxFarmNameLength;
223220
type MaxFarmPublicIps = MaxFarmPublicIps;
@@ -372,8 +369,12 @@ pub(crate) fn get_document_hash_input(document_hash_input: &[u8]) -> DocumentHas
372369
BoundedVec::try_from(document_hash_input.to_vec()).expect("Invalid document hash input.")
373370
}
374371

375-
pub(crate) fn get_twin_ip_input(twin_ip_input: &[u8]) -> TwinIpInput {
376-
BoundedVec::try_from(twin_ip_input.to_vec()).expect("Invalid twin ip input.")
372+
pub(crate) fn get_relay_input(relay_input: &[u8]) -> RelayInput {
373+
Some(BoundedVec::try_from(relay_input.to_vec()).expect("Invalid relay input."))
374+
}
375+
376+
pub(crate) fn get_public_key_input(pk_input: &[u8]) -> PkInput {
377+
Some(BoundedVec::try_from(pk_input.to_vec()).expect("Invalid document hash input."))
377378
}
378379

379380
pub(crate) fn get_public_ip_ip_input(public_ip_ip_input: &[u8]) -> Ip4Input {

substrate-node/pallets/pallet-smart-contract/src/tests.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3361,8 +3361,14 @@ pub fn create_twin(origin: AccountId) {
33613361
get_document_hash_input(b"some_hash"),
33623362
));
33633363

3364-
let ip = get_twin_ip_input(b"::1");
3365-
assert_ok!(TfgridModule::create_twin(RuntimeOrigin::signed(origin), ip));
3364+
let relay = get_relay_input(b"somerelay.io");
3365+
let pk =
3366+
get_public_key_input(b"0x6c8fd181adc178cea218e168e8549f0b0ff30627c879db9eac4318927e87c901");
3367+
assert_ok!(TfgridModule::create_twin(
3368+
RuntimeOrigin::signed(origin),
3369+
relay,
3370+
pk
3371+
));
33663372
}
33673373

33683374
fn create_farming_policies() {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ frame-try-runtime = { git = "https://github.com/paritytech/substrate", branch =
4242
hex-literal = "0.3.1"
4343
pallet-membership = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false }
4444
pallet-collective = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false }
45+
env_logger = "*"
4546

4647
[features]
4748
default = ['std']

substrate-node/pallets/pallet-tfgrid/src/lib.rs

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ pub mod interface;
3939
pub mod migrations;
4040
pub mod node;
4141
pub mod terms_cond;
42-
pub mod twin;
4342

4443
// Definition of the pallet logic, to be aggregated at runtime definition
4544
// through `construct_runtime`.
@@ -191,9 +190,9 @@ pub mod pallet {
191190

192191
pub type TwinIndex = u32;
193192
pub type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
194-
type TwinInfoOf<T> = types::Twin<<T as Config>::TwinIp, AccountIdOf<T>>;
195-
pub type TwinIpInput = BoundedVec<u8, ConstU32<{ twin::MAX_IP_LENGTH }>>;
196-
pub type TwinIpOf<T> = <T as Config>::TwinIp;
193+
type TwinInfoOf<T> = types::Twin<AccountIdOf<T>>;
194+
pub type RelayInput = Option<BoundedVec<u8, ConstU32<{ types::MAX_RELAY_LENGTH }>>>;
195+
pub type PkInput = Option<BoundedVec<u8, ConstU32<{ types::MAX_PK_LENGTH }>>>;
197196

198197
#[pallet::storage]
199198
#[pallet::getter(fn twins)]
@@ -294,15 +293,6 @@ pub mod pallet {
294293
+ TryFrom<TermsAndConditionsInput<Self>, Error = Error<Self>>
295294
+ MaxEncodedLen;
296295

297-
/// The type of a twin IP.
298-
type TwinIp: FullCodec
299-
+ Debug
300-
+ PartialEq
301-
+ Clone
302-
+ TypeInfo
303-
+ TryFrom<TwinIpInput, Error = Error<Self>>
304-
+ MaxEncodedLen;
305-
306296
/// The type of a farm name.
307297
type FarmName: FullCodec
308298
+ Debug
@@ -413,8 +403,8 @@ pub mod pallet {
413403
EntityUpdated(TfgridEntity<T>),
414404
EntityDeleted(u32),
415405

416-
TwinStored(types::Twin<T::TwinIp, T::AccountId>),
417-
TwinUpdated(types::Twin<T::TwinIp, T::AccountId>),
406+
TwinStored(types::Twin<T::AccountId>),
407+
TwinUpdated(types::Twin<T::AccountId>),
418408

419409
TwinEntityStored(u32, u32, Vec<u8>),
420410
TwinEntityRemoved(u32, u32),
@@ -492,9 +482,9 @@ pub mod pallet {
492482

493483
FarmingPolicyNotExists,
494484

495-
TwinIpTooShort,
496-
TwinIpTooLong,
497-
InvalidTwinIp,
485+
RelayTooShort,
486+
RelayTooLong,
487+
InvalidRelay,
498488

499489
FarmNameTooShort,
500490
FarmNameTooLong,
@@ -563,6 +553,7 @@ pub mod pallet {
563553
InvalidDocumentHashInput,
564554

565555
InvalidPublicConfig,
556+
InvalidRelayAddress,
566557
}
567558

568559
#[pallet::genesis_config]
@@ -1397,7 +1388,11 @@ pub mod pallet {
13971388

13981389
#[pallet::call_index(17)]
13991390
#[pallet::weight(<T as Config>::WeightInfo::create_twin())]
1400-
pub fn create_twin(origin: OriginFor<T>, ip: TwinIpInput) -> DispatchResultWithPostInfo {
1391+
pub fn create_twin(
1392+
origin: OriginFor<T>,
1393+
relay: RelayInput,
1394+
pk: PkInput,
1395+
) -> DispatchResultWithPostInfo {
14011396
let account_id = ensure_signed(origin)?;
14021397

14031398
ensure!(
@@ -1413,14 +1408,19 @@ pub mod pallet {
14131408
let mut twin_id = TwinID::<T>::get();
14141409
twin_id = twin_id + 1;
14151410

1416-
let twin_ip = Self::get_twin_ip(ip)?;
1411+
if let Some(relay_addr) = relay.clone() {
1412+
ensure!(
1413+
Self::validate_relay_address(relay_addr.into()),
1414+
Error::<T>::InvalidRelayAddress
1415+
);
1416+
}
14171417

1418-
let twin = types::Twin::<T::TwinIp, T::AccountId> {
1419-
version: TFGRID_TWIN_VERSION,
1418+
let twin = types::Twin::<T::AccountId> {
14201419
id: twin_id,
14211420
account_id: account_id.clone(),
1421+
relay,
14221422
entities: Vec::new(),
1423-
ip: twin_ip,
1423+
pk,
14241424
};
14251425

14261426
Twins::<T>::insert(&twin_id, &twin);
@@ -1436,7 +1436,11 @@ pub mod pallet {
14361436

14371437
#[pallet::call_index(18)]
14381438
#[pallet::weight(100_000_000 + T::DbWeight::get().writes(1).ref_time() + T::DbWeight::get().reads(3).ref_time())]
1439-
pub fn update_twin(origin: OriginFor<T>, ip: TwinIpInput) -> DispatchResultWithPostInfo {
1439+
pub fn update_twin(
1440+
origin: OriginFor<T>,
1441+
relay: RelayInput,
1442+
pk: PkInput,
1443+
) -> DispatchResultWithPostInfo {
14401444
let account_id = ensure_signed(origin)?;
14411445

14421446
let twin_id =
@@ -1450,9 +1454,15 @@ pub mod pallet {
14501454
Error::<T>::UnauthorizedToUpdateTwin
14511455
);
14521456

1453-
let twin_ip = Self::get_twin_ip(ip)?;
1457+
if let Some(relay_addr) = relay.clone() {
1458+
ensure!(
1459+
Self::validate_relay_address(relay_addr.into()),
1460+
Error::<T>::InvalidRelayAddress
1461+
);
1462+
}
14541463

1455-
twin.ip = twin_ip;
1464+
twin.relay = relay;
1465+
twin.pk = pk;
14561466

14571467
Twins::<T>::insert(&twin_id, &twin);
14581468

@@ -2171,11 +2181,6 @@ impl<T: Config> Pallet<T> {
21712181
Ok(parsed_terms_cond)
21722182
}
21732183

2174-
fn get_twin_ip(ip: TwinIpInput) -> Result<TwinIpOf<T>, DispatchErrorWithPostInfo> {
2175-
let ip_parsed = <T as Config>::TwinIp::try_from(ip)?;
2176-
Ok(ip_parsed)
2177-
}
2178-
21792184
fn get_farm_name(name: FarmNameInput<T>) -> Result<FarmNameOf<T>, DispatchErrorWithPostInfo> {
21802185
let name_parsed = <T as Config>::FarmName::try_from(name)?;
21812186
Ok(name_parsed)
@@ -2299,6 +2304,34 @@ impl<T: Config> Pallet<T> {
22992304
let parsed_serial_number = <T as Config>::SerialNumber::try_from(serial_number)?;
23002305
Ok(parsed_serial_number)
23012306
}
2307+
2308+
fn validate_relay_address(relay_input: Vec<u8>) -> bool {
2309+
if relay_input.len() == 0 {
2310+
return false;
2311+
}
2312+
2313+
if relay_input[relay_input.len() - 1] == b'.' {
2314+
return false;
2315+
}
2316+
2317+
let mut prev_idx = 0;
2318+
2319+
for (idx, c) in relay_input.iter().enumerate() {
2320+
match c {
2321+
b'.' => {
2322+
if idx == 0 || idx - prev_idx == 1 {
2323+
return false;
2324+
} else {
2325+
prev_idx = idx
2326+
}
2327+
}
2328+
b'a'..=b'z' | b'A'..=b'Z' | b'-' | b'_' => (),
2329+
_ => return false,
2330+
}
2331+
}
2332+
2333+
true
2334+
}
23022335
}
23032336

23042337
impl<T: Config> tfchain_support::traits::Tfgrid<T::AccountId, T::FarmName> for Pallet<T> {

substrate-node/pallets/pallet-tfgrid/src/migrations/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ pub mod v11;
44
pub mod v12;
55
pub mod v13;
66
pub mod v14;
7+
pub mod v15;

0 commit comments

Comments
 (0)