diff --git a/runtime/tests/cli_integration_tests_roaming.rs b/runtime/tests/cli_integration_tests_roaming.rs index 23e6d3718..dd5123d50 100644 --- a/runtime/tests/cli_integration_tests_roaming.rs +++ b/runtime/tests/cli_integration_tests_roaming.rs @@ -622,6 +622,214 @@ mod tests { device_profile_vendorid: b"1000".to_vec(), }) ); + + // MMD-1022 - Spec for how M2 Pro will interact on blockchain + + // Set an account balance to each account for transaction fees + assert_ok!(Balances::set_balance(Origin::root(), 1, 1_000, 0)); + assert_ok!(Balances::set_balance(Origin::root(), 2, 1_000, 0)); + assert_ok!(Balances::set_balance(Origin::root(), 3, 1_000, 0)); + assert_ok!(Balances::set_balance(Origin::root(), 4, 1_000, 0)); + assert_ok!(Balances::set_balance(Origin::root(), 5, 1_000, 0)); + assert_ok!(Balances::set_balance(Origin::root(), 6, 1_000, 0)); + assert_ok!(Balances::set_balance(Origin::root(), 7, 1_000, 0)); + assert_ok!(Balances::set_balance(Origin::root(), 8, 1_000, 0)); + assert_ok!(Balances::set_balance(Origin::root(), 9, 1_000, 0)); + assert_ok!(Balances::set_balance(Origin::root(), 10, 1_000, 0)); + assert_ok!(Balances::set_balance(Origin::root(), 11, 1_000, 0)); + assert_ok!(Balances::set_balance(Origin::root(), 12, 1_000, 0)); + + // Identity for accounts and their hardware devices + + let sudo = Origin::root(); + let mxc_account_0_id = 0; + let mxc_account_signed = Origin::signed(mxc_account_0_id); + + let user_account_1_id = 1; + let user_account_1_signed = Origin::signed(user_account_1_id); + // Identity info of account_id 1 + let user_account_1_info = Some(IdentityInfo { + email: b"luke@mxc.org".to_vec() + }); + assert_ok!(Identity::set_identity(registrar, user_account_1_info)); // Set by registrar + + let user_account_2_id = 12; + let user_account_2_signed = Origin::signed(user_account_2_id); + // Identity info of account_id 2 + let user_account_2_info = Some(IdentityInfo { + email: b"test@mxc.org".to_vec() + }); + assert_ok!(Identity::set_identity(registrar, user_account_2_info)); // Set by registrar + // assert_ok!(Identity::add_sub(user_account_1_signed, user_account_2_id, user_account_2_info); + + // Registrar needs to already exist before `register_gateway` or `register_device` is called + let registrar_account_id = 11; + let registrar = Origin::signed(registrar_account_id); + let registrar_index = 0u64; // Registrar Index + + assert_eq!(Identity::add_registrar(sudo.clone(), registrar.clone()), registrar_index); // Sudo required + let registrar_judgement_fee = 100u32; + assert_ok!(Identity::set_fee(sudo.clone(), registrar_index, registrar_judgement_fee.clone())); // Sudo required + assert_ok!(Identity::set_fields(sudo.clone(), registrar_index, registrar.clone(), IdentityFields::Email)); // Sudo required + let max_fee = 1u32; + + // TODO - the below `create` needs to create an account id for the object (i.e. operator_account_id) + assert_ok!(RoamingOperatorTestModule::create(mxc_account_signed)); + let mxc_roaming_operator_id = 2; // Hard-coded. Generated by above line + // TODO - the below `create` needs to create an account id for the object (i.e. network_account_id) + assert_ok!(RoamingNetworkTestModule::create(mxc_account_signed)); + let mxc_roaming_network_id = 3; // Hard-coded. Generated by above line + assert_ok!(RoamingNetworkTestModule::is_roaming_network_owner(mxc_roaming_network_id, mxc_account_0_id)); + // Assign each network to the MXC network operator, by the owner/creator of the MXC network operator + assert_ok!(RoamingNetworkTestModule::assign_network_to_operator(mxc_account_signed, mxc_roaming_network_id, mxc_roaming_operator_id)); + assert_eq!(RoamingNetworkTestModule::roaming_network_owner(mxc_account_0_id), Some(mxc_roaming_network_id)); + // TODO - the below `create` needs to create an account id for the object (i.e. network_server_account_id) + assert_ok!(RoamingNetworkServerTestModule::create(mxc_account_signed)); // Supernode + let mxc_network_server_id = 4; // Hard-coded. Generated by above line + + // Call Functions + assert_ok!(RoamingNetworkProfileTestModule::create(mxc_account_signed)); + let mxc_roaming_network_profile_id = 5; // Hard-coded. Generated by above line + assert_eq!(RoamingNetworkProfileTestModule::roaming_network_profile_owner(mxc_roaming_network_profile_id), Some(mxc_roaming_network_id)); + assert_ok!(RoamingNetworkProfileTestModule::assign_network_profile_to_network(mxc_account_signed, mxc_roaming_network_profile_id, mxc_roaming_network_id)); + assert_ok!(RoamingNetworkProfileTestModule::assign_network_profile_to_operator(mxc_account_signed, mxc_roaming_network_profile_id, mxc_roaming_operator_id)); + assert_ok!(RoamingNetworkProfileTestModule::set_device_access_allowed(mxc_account_signed, mxc_roaming_network_profile_id, true)); + // Network Profile 0 - Whitelist MXC + assert_ok!(RoamingNetworkProfileTestModule::add_whitelisted_network( + mxc_account_signed, + mxc_roaming_network_profile_id, // network_profile_id + mxc_roaming_network_id, // network_id + )); + + // Identity is assigned to user wallet upon gateway registration + // ** Gateway calls this extrinsic function ** + assert_ok!(RoamingGatewayTestModule::register_gateway(user_account_1_signed)); + // ** Important note: Behind the scenes, the above function should do all the following internally + // so we need to provide `register_gateway` with sufficient arguments + // TODO - the below `create` needs to create an account id for the object (i.e. user_organization_account_id) + assert_ok!(RoamingOrganizationTestModule::create(user_account_1_signed)); + let user_organization_id = 6; // Hard-coded. Generated by above line + // TODO - add this extrinsic function `assign_user_to_organization` + assert_ok!(RoamingGatewayTestModule::assign_user_to_organization(user_account_1_signed, user_account_1_id, user_organization_id)); + // TODO - add this roaming gateway pallet + // Create Gateway + // TODO - the below `create` needs to create an account id for the object (i.e. gateway_account_id) + assert_ok!(RoamingGatewayTestModule::create(user_account_1_signed)); + // M2 Pro + let gateway_account_id = 7; // Hard-coded. Generated by above line + let gateway_account_signed = Origin::signed(gateway_account_id); + assert_eq!(RoamingGatewayTestModule::roaming_gateway_owner(gateway_account_id), Some(7)); + assert_ok!(RoamingGatewayTestModule::assign_gateway_to_organization(user_account_1_signed, gateway_account_id, user_organization_id)); + assert_ok!(RoamingGatewayTestModule::assign_gateway_to_network_server(mxc_account_signed, gateway_account_id, mxc_network_server_id)); + assert_ok!(RoamingGatewayProfileTestModule::create(user_account_1_signed)); + let gateway_profile_id = 8; // Hard-coded. Generated by above line + assert_ok!(RoamingGatewayProfileTestModule::assign_gateway_profile_to_gateway(user_account_1_signed, gateway_profile_id, gateway_account_id)); + assert_ok!(RoamingGatewayProfileTestModule::set_config( + user_account_1_signed, + gateway_profile_id, + Some(b"1234".to_vec()), // gateway_profile_mac + Some(b"1000".to_vec()), // gateway_profile_vendorid + )); + let gateway_profile_settings_0_id = 0; // Hard-coded. Generated by above line + + // Identity info of hardware gateway (gateway) + let fetched_gateway_profile_setting = >::get(gateway_profile_settings_0_id); + // https://dev.datahighway.com/docs/en/whitepaper#gateway-setup--staking + let gateway_account_roaming_data = ( + supernode_home_account_id: >::get(0), + gateway_id_mac: fetched_gateway_profile_setting.gateway_profile_mac.unwrap(), + gateway_profile_vendorid: fetched_gateway_profile_setting.gateway_profile_vendorid.unwrap(), + ); // Tuple + // TODO - how do we store this using Identity pallet, as it says that values greater than u32 + // such as account_id's that are u64 will be truncated but we want to associate the account_id + // https://github.com/paritytech/substrate/blob/master/frame/identity/src/lib.rs + let gateway_account_additional: Vec> = Vec::new(); + gateway_account_additional.push(gateway_account_id, gateway_account_roaming_data); + // Reference: https://github.com/paritytech/substrate/blob/master/frame/identity/src/lib.rs#L255 + let gateway_account_info = Some(IdentityInfo { + additional: gateway_account_additional + }); + assert_ok!(Identity::set_identity(registrar, gateway_account_info)); // Set by registrar + // Identity is assigned to user wallet upon device registration + assert_ok!(Identity::add_sub(user_account_1_signed, gateway_account_id, gateway_account_info); + // Request judgement of user wallet identity by the registrar + // to confirm that their sub-identities (end device and gateway) do infact belong to the user + assert_ok!(Identity::request_judgement(user_account_1_signed, registrar_index, max_fee)); + assert_ok!(Identity::provide_judgement(registrar, registrar_index, user_account_1_signed, registrar_judgement_fee.clone())); + + // Identity is assigned to user wallet upon device registration + // ** Device calls this extrinsic function ** + assert_ok!(RoamingDeviceTestModule::register_device(user_account_1_signed)); + // Create End Device + // TODO - the below `create` needs to create an account id for the object (i.e. device_account_id) + assert_ok!(RoamingDeviceTestModule::create(user_account_1_signed)); + let device_account_id = 9; // Hard-coded. Generated by above line + let device_account_signed = Origin::signed(device_account_id); + assert_eq!(RoamingDeviceTestModule::roaming_device_owner(device_account_id), Some(9)); + assert_ok!(RoamingDeviceTestModule::assign_device_to_organization(user_account_1_signed, device_account_id, user_organization_id)); + assert_ok!(RoamingDeviceTestModule::assign_device_to_network_server(mxc_account_signed, device_account_id, mxc_network_server_id)); + // Note: We could create the RoamingDeviceProfile using the signed RoamingDevice account instead of the user + // to simplify the associations, but for now we will leave it this way for now and call `assign_..` to make the assocations instead. + assert_ok!(RoamingDeviceProfileTestModule::create(user_account_1_signed)); + let device_profile_id = 10; // Hard-coded. Generated by above line + assert_ok!(RoamingDeviceProfileTestModule::assign_device_profile_to_device(user_account_1_signed, device_profile_id, device_account_id)); + assert_ok!(RoamingDeviceProfileTestModule::set_config( + user_account_1_signed, + device_profile_id, + Some(b"1234".to_vec()), // device_profile_devaddr + Some(b"5678".to_vec()), // device_profile_deveui + Some(b"6789".to_vec()), // device_profile_joineui + Some(b"1000".to_vec()), // device_profile_vendorid + )); + let device_profile_settings_0_id = 0; // Hard-coded. Generated by above line + + // Identity info of hardware device (end device) + let fetched_device_profile_setting = >::get(device_profile_settings_0_id); + let device_account_roaming_data = ( + roaming_device_index: fetched_device_profile_setting.roaming_device_index, + roaming_device_profile_index: fetched_device_profile_setting.roaming_device_profile_index, + roaming_device_profile_devaddr: fetched_device_profile_setting.device_profile_devaddr, + roaming_device_profile_vendorid: fetched_device_profile_setting.device_profile_vendorid, + ); // Tuple + // TODO - how do we store this using Identity pallet, as it says that values greater than u32 + // such as account_id's that are u64 will be truncated but we want to associate the account_id + // https://github.com/paritytech/substrate/blob/master/frame/identity/src/lib.rs + let device_account_additional: Vec> = Vec::new(); + device_account_additional.push(device_account_signed, device_account_roaming_data); + // Reference: https://github.com/paritytech/substrate/blob/master/frame/identity/src/lib.rs#L255 + let device_account_info = Some(IdentityInfo { + additional: device_account_additional + }); + assert_ok!(Identity::set_identity(registrar, device_account_info)); // Set by registrar + + // Identity is assigned to user wallet upon gateway registration + assert_ok!(Identity::add_sub(user_account_1_signed, device_account_id, device_account_info); + // Request judgement of user wallet identity by the registrar + // to confirm that their sub-identities (end device and gateway) do infact belong to the user + assert_ok!(Identity::request_judgement(user_account_1_signed, registrar_index, max_fee)); + assert_ok!(Identity::provide_judgement(registrar, registrar_index, user_account_1_signed, registrar_judgement_fee.clone())); + + + // Transfer identity ownership of M2 Pro Gateway to a different user + // ** Gateway calls this extrinsic function ** + // TODO - the below `transfer_gateway_owner` extrinsic needs to be added + assert_ok!(RoamingGatewayTestModule::transfer_gateway_owner(user_account_1_signed, gateway_account_id, user_account_2_id)); + assert_eq!(RoamingGatewayTestModule::roaming_gateway_owner(gateway_account_id), Some(user_account_2_id)); + // Note: If a user sells hardware, we need them to remove the hardware from being one of their sub-identities + assert_ok!(Identity::remove_sub(user_account_1_signed, gateway_account_id); + assert_ok!(Identity::add_sub(user_account_2_id, gateway_account_id, gateway_account_info); + + // Transfer identity ownership of end device to a different user + // ** Device calls this extrinsic function ** + // TODO - the below `transfer_device_owner` extrinsic needs to be added + assert_ok!(RoamingDeviceTestModule::transfer_device_owner(user_account_1_signed, device_account_id, user_account_2_id)); + assert_eq!(RoamingDeviceTestModule::roaming_device_owner(device_account_id), Some(user_account_2_id)); + // Note: If a user sells hardware, we need them to remove the hardware from being one of their sub-identities + assert_ok!(Identity::remove_sub(user_account_1_signed, device_account_id); + assert_ok!(Identity::add_sub(user_account_2_id, device_account_id, device_account_info); + + // TODO - use Proxy to allow another user to make calls on behalf of an identity + // assert_ok!(Proxy::add_proxy(sudo.clone(), 3, ProxyType::Any, 1)); }); } }