Skip to content

Commit

Permalink
feat: delete twin is not allowed (#586)
Browse files Browse the repository at this point in the history
  • Loading branch information
renauter authored Jan 31, 2023
1 parent a3f5b2e commit abe71a4
Show file tree
Hide file tree
Showing 11 changed files with 14 additions and 187 deletions.
8 changes: 0 additions & 8 deletions cli-tool/cmds/farm_cmds/delete.js

This file was deleted.

8 changes: 0 additions & 8 deletions cli-tool/cmds/twin_cmds/delete.js

This file was deleted.

21 changes: 5 additions & 16 deletions cli-tool/src/farms.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { getClient } = require('./client')
const { callback } = require('./util')

async function createFarm (args) {
async function createFarm(args) {
const { name, c: countryID, g: cityID, certificationType, a: url, m: mnemonic } = args

const client = await getClient(url, mnemonic)
Expand All @@ -17,23 +17,23 @@ async function createFarm (args) {
return client.createFarm(name, certificationTypeParsed, countryID, cityID, [publicIP], callback)
}

async function addPublicIP (args) {
async function addPublicIP(args) {
const { id, ip, gateway, a: url, m: mnemonic } = args

const client = await getClient(url, mnemonic)

return client.addFarmIp(id, ip, gateway, callback)
}

async function deletePublicIP (args) {
async function deletePublicIP(args) {
const { id, ip, a: url, m: mnemonic } = args

const client = await getClient(url, mnemonic)

return client.deleteFarmIp(id, ip, callback)
}

async function getFarm (args) {
async function getFarm(args) {
const { id, a: url } = args
const client = await getClient(url, '')

Expand All @@ -43,7 +43,7 @@ async function getFarm (args) {
process.exit(0)
}

async function listFarms (args) {
async function listFarms(args) {
const { a: url } = args
const client = await getClient(url, '')

Expand All @@ -53,21 +53,10 @@ async function listFarms (args) {
process.exit(0)
}

async function deleteFarm (args) {
const { id, a: url, m: mnemonic } = args
const client = await getClient(url, mnemonic)

await client.deleteFarmByID(id, callback)

console.log('farm deleted')
process.exit(0)
}

module.exports = {
createFarm,
getFarm,
listFarms,
deleteFarm,
addPublicIP,
deletePublicIP
}
21 changes: 5 additions & 16 deletions cli-tool/src/twins.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const { getClient } = require('./client')
const { callback } = require('./util')

async function createTwin (args) {
async function createTwin(args) {
const { ip, a: url, m: mnemonic } = args
const client = await getClient(url, mnemonic)

return client.createTwin(ip, callback)
}

async function getTwin (args) {
async function getTwin(args) {
const { a: url, id } = args

const client = await getClient(url, '')
Expand All @@ -18,7 +18,7 @@ async function getTwin (args) {
process.exit(0)
}

async function listTwins (args) {
async function listTwins(args) {
const { a: url } = args
const client = await getClient(url, '')

Expand All @@ -28,24 +28,14 @@ async function listTwins (args) {
process.exit(0)
}

async function deleteTwin (args) {
const { id, a: url, m: mnemonic } = args
const client = await getClient(url, mnemonic)

await client.deleteTwin(id, callback)

console.log('twin deleted')
process.exit(0)
}

async function createTwinEntity (twinID, entityID, signature, mnemonic, url, callback) {
async function createTwinEntity(twinID, entityID, signature, mnemonic, url, callback) {
const client = await getClient(url, mnemonic)

const create = await client.addTwinEntity(twinID, entityID, signature, callback)
return create
}

async function deleteTwinEntity (twinID, entityID, mnemonic, url, callback) {
async function deleteTwinEntity(twinID, entityID, mnemonic, url, callback) {
const client = await getClient(url, mnemonic)

const create = await client.removeTwinEntity(twinID, entityID, callback)
Expand All @@ -56,7 +46,6 @@ module.exports = {
createTwin,
getTwin,
listTwins,
deleteTwin,
createTwinEntity,
deleteTwinEntity
}
11 changes: 0 additions & 11 deletions substrate-node/pallets/pallet-smart-contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,17 +459,6 @@ pub mod pallet {
Self::_cancel_contract(account_id, contract_id, types::Cause::CanceledByUser)
}

// DEPRECATED
#[pallet::call_index(3)]
#[pallet::weight(10_000 + T::DbWeight::get().writes(1).ref_time())]
pub fn add_reports(
_origin: OriginFor<T>,
_reports: Vec<types::Consumption>,
) -> DispatchResultWithPostInfo {
// return error
Err(DispatchErrorWithPostInfo::from(Error::<T>::MethodIsDeprecated).into())
}

#[pallet::call_index(4)]
#[pallet::weight(10_000 + T::DbWeight::get().writes(1).ref_time())]
pub fn create_name_contract(
Expand Down
41 changes: 0 additions & 41 deletions substrate-node/pallets/pallet-smart-contract/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1236,47 +1236,6 @@ fn test_node_contract_billing_cycles_cancel_contract_during_cycle_works() {
});
}

#[test]
fn test_node_contract_billing_fails() {
let (mut ext, mut pool_state) = new_test_ext_with_pool_state(0);
ext.execute_with(|| {
run_to_block(1, Some(&mut pool_state));
// Creates a farm and node and sets the price of tft to 0 which raises an error later
prepare_farm_and_node();

assert_ok!(SmartContractModule::create_node_contract(
RuntimeOrigin::signed(bob()),
1,
generate_deployment_hash(),
get_deployment_data(),
1,
None
));

let contracts_to_bill_at_block = SmartContractModule::contract_to_bill_at_block(1);
assert_eq!(contracts_to_bill_at_block.len(), 1);

let contract_id = contracts_to_bill_at_block[0];

// delete twin to make the billing fail
assert_ok!(TfgridModule::delete_twin(
RuntimeOrigin::signed(bob()),
SmartContractModule::contracts(contract_id).unwrap().twin_id,
));

// the offchain worker should save the failed ids in local storage and try again
// in subsequent blocks (which will also fail)
for i in 1..3 {
pool_state.write().should_call_bill_contract(
1,
Err(Error::<TestRuntime>::TwinNotExists.into()),
1 + i * 10,
);
run_to_block(11 * i, Some(&mut pool_state));
}
});
}

#[test]
fn test_node_contract_billing_cycles_cancel_contract_during_cycle_without_balance_works() {
let (mut ext, mut pool_state) = new_test_ext_with_pool_state(0);
Expand Down
2 changes: 0 additions & 2 deletions substrate-node/pallets/pallet-tfgrid/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ Twins:

- create_twin(..)
- update_twin(..)
- delete_twin(..)

Entity-Twin Relation:

Expand All @@ -202,7 +201,6 @@ Farms:

- createFarm(..)
- updateFarm(..)
- deleteFarm(..)
- addFarmIp(..)
- removeFarmIp(..)

Expand Down
30 changes: 1 addition & 29 deletions substrate-node/pallets/pallet-tfgrid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -962,12 +962,6 @@ pub mod pallet {
}
}

#[pallet::call_index(7)]
#[pallet::weight(100_000_000 + T::DbWeight::get().writes(2).ref_time() + T::DbWeight::get().reads(2).ref_time())]
pub fn delete_farm(_origin: OriginFor<T>, _id: u32) -> DispatchResultWithPostInfo {
Err(DispatchErrorWithPostInfo::from(Error::<T>::MethodIsDeprecated).into())
}

#[pallet::call_index(8)]
#[pallet::weight(<T as Config>::WeightInfo::create_node())]
pub fn create_node(
Expand Down Expand Up @@ -1559,28 +1553,6 @@ pub mod pallet {
Ok(().into())
}

#[pallet::call_index(21)]
#[pallet::weight(100_000_000 + T::DbWeight::get().writes(2).ref_time() + T::DbWeight::get().reads(1).ref_time())]
pub fn delete_twin(origin: OriginFor<T>, twin_id: u32) -> DispatchResultWithPostInfo {
let account_id = ensure_signed(origin)?;

let twin = Twins::<T>::get(&twin_id).ok_or(Error::<T>::TwinNotExists)?;
// Make sure only the owner of this twin can call this method
ensure!(
twin.account_id == account_id,
Error::<T>::UnauthorizedToUpdateTwin
);

Twins::<T>::remove(&twin_id);

// remove twin id from this users map of twin ids
TwinIdByAccountID::<T>::remove(&account_id.clone());

Self::deposit_event(Event::TwinDeleted(twin_id));

Ok(().into())
}

#[pallet::call_index(22)]
#[pallet::weight(100_000_000 + T::DbWeight::get().writes(3).ref_time() + T::DbWeight::get().reads(2).ref_time())]
pub fn create_pricing_policy(
Expand Down Expand Up @@ -2352,4 +2324,4 @@ impl<T: Config> tfchain_support::traits::Tfgrid<T::AccountId, T::FarmName> for P
None => false,
}
}
}
}
36 changes: 0 additions & 36 deletions substrate-node/pallets/pallet-tfgrid/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,29 +153,6 @@ fn test_create_twin_works() {
});
}

#[test]
fn test_delete_twin_works() {
ExternalityBuilder::build().execute_with(|| {
assert_ok!(TfgridModule::user_accept_tc(
RuntimeOrigin::signed(alice()),
get_document_link_input(b"some_link"),
get_document_hash_input(b"some_hash"),
));

let ip = get_twin_ip_input(b"::1");
assert_ok!(TfgridModule::create_twin(
RuntimeOrigin::signed(alice()),
ip
));

let twin_id = 1;
assert_ok!(TfgridModule::delete_twin(
RuntimeOrigin::signed(alice()),
twin_id
));
});
}

#[test]
fn test_delete_node_works() {
ExternalityBuilder::build().execute_with(|| {
Expand Down Expand Up @@ -461,19 +438,6 @@ fn test_adding_misformatted_ip_to_farm_fails() {
});
}

#[test]
fn test_delete_farm_fails() {
ExternalityBuilder::build().execute_with(|| {
create_entity();
create_twin();
create_farm();
assert_noop!(
TfgridModule::delete_farm(RuntimeOrigin::signed(alice()), 1),
Error::<TestRuntime>::MethodIsDeprecated
);
});
}

#[test]
fn test_adding_ip_duplicate_to_farm_fails() {
ExternalityBuilder::build().execute_with(|| {
Expand Down
12 changes: 0 additions & 12 deletions substrate-node/tests/TfChainClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,6 @@ def update_twin(self, ip: str = "::1", port: int = DEFAULT_PORT, who: str = DEFA
self._sign_extrinsic_submit_check_response(
substrate, call, who, expected_events=expected_events)

def delete_twin(self, twin_id: int = 1, port: int = DEFAULT_PORT, who: str = DEFAULT_SIGNER):
substrate = self._connect_to_server(f"ws://127.0.0.1:{port}")

call = substrate.compose_call("TfgridModule", "delete_twin", {
"twin_id": twin_id})
expected_events = [{
"module_id": "TfgridModule",
"event_id": "TwinDeleted"
}]
self._sign_extrinsic_submit_check_response(
substrate, call, who, expected_events=expected_events)

def get_twin(self, id: int = 1, port: int = DEFAULT_PORT):
substrate = self._connect_to_server(f"ws://127.0.0.1:{port}")

Expand Down
11 changes: 3 additions & 8 deletions substrate-node/tests/integration_tests.robot
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ Test Start And Stop Network

Tear Down Multi Node Network

Test Create Update Delete Twin
[Documentation] Testing api calls (create, update, delete) for managing twins
Setup Multi Node Network log_name=test_create_update_delete_twin
Test Create Update Twin
[Documentation] Testing api calls (create, update) for managing twins
Setup Multi Node Network log_name=test_create_update_twin

User Accept Tc

Expand All @@ -77,11 +77,6 @@ Test Create Update Delete Twin
Should Not Be Equal ${twin} ${None}
Should Be Equal ${twin}[ip] 0000:0000:0000:0000:0000:0000:0000:0001

Delete Twin ${1}

${twin} = Get Twin ${1}
Should Be Equal ${twin} ${None}

Tear Down Multi Node Network

Test Create Update Farm
Expand Down

0 comments on commit abe71a4

Please sign in to comment.