From 80f7ffaa8e17280ce68c906b112b155f9a726ff7 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 4 Nov 2021 12:03:58 +0100 Subject: [PATCH] p2p Network Node implemented --- Network/NetwokNode.js | 6 ++++ .../Network/NT/Modules/WebSocketsInterface.js | 2 +- Projects/Network/SA/Globals/Memory.js | 3 +- Projects/Network/SA/Modules/P2PNetwork.js | 35 +++++++++++++++++++ Projects/Network/SA/Modules/P2PNetworkNode.js | 29 +++++++++++++++ Projects/Network/SA/Modules/UserProfiles.js | 12 +++++-- Projects/ProjectsSchema.json | 12 +++++++ 7 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 Projects/Network/SA/Modules/P2PNetwork.js create mode 100644 Projects/Network/SA/Modules/P2PNetworkNode.js diff --git a/Network/NetwokNode.js b/Network/NetwokNode.js index 22a04fbdb..74ffc6326 100644 --- a/Network/NetwokNode.js +++ b/Network/NetwokNode.js @@ -3,6 +3,7 @@ exports.newNetworkNode = function newNetworkNode() { let thisObject = { socialGraphService: undefined, storage: undefined, + p2pMetwork: undefined, run: run } @@ -24,5 +25,10 @@ exports.newNetworkNode = function newNetworkNode() { */ thisObject.storage = NT.projects.socialTrading.modules.storage.newSocialTradingModulesStorage() thisObject.storage.initialize() + /* + We can join now the P2P Network as a new peer there. + */ + thisObject.p2pMetwork = SA.projects.network.modules.p2pMetwork.newNetworkModulesP2PNetwork() + await thisObject.p2pMetwork.initialize() } } diff --git a/Projects/Network/NT/Modules/WebSocketsInterface.js b/Projects/Network/NT/Modules/WebSocketsInterface.js index ebad1b2cc..4efcaef16 100644 --- a/Projects/Network/NT/Modules/WebSocketsInterface.js +++ b/Projects/Network/NT/Modules/WebSocketsInterface.js @@ -304,7 +304,7 @@ exports.newNetworkModulesWebSocketsInterface = function newNetworkModulesWebSock /* The signature gives us the blockchain account, and the account the user profile. */ - let witnessUserProfile = SA.projects.network.globals.memory.maps.USER_PROFILES_BY_BLOCHAIN_ACCOUNT.get(caller.blockchainAccount) + let witnessUserProfile = SA.projects.network.globals.memory.maps.USER_PROFILES_BY_BLOKCHAIN_ACCOUNT.get(caller.blockchainAccount) if (witnessUserProfile === undefined) { let response = { diff --git a/Projects/Network/SA/Globals/Memory.js b/Projects/Network/SA/Globals/Memory.js index 7c86e1224..38cb701f4 100644 --- a/Projects/Network/SA/Globals/Memory.js +++ b/Projects/Network/SA/Globals/Memory.js @@ -6,9 +6,10 @@ exports.newNetworkGlobalsMemory = function () { maps: { USER_PROFILES_BY_ID: new Map(), // Here we will store the user profiles by User Profile Id. USER_PROFILES_BY_HANDLE: new Map(), // Here we will store the user profiles by User Profile Handle. - USER_PROFILES_BY_BLOCHAIN_ACCOUNT: new Map() // Here we will store the user profiles by Blockchain Account. + USER_PROFILES_BY_BLOKCHAIN_ACCOUNT: new Map() // Here we will store the user profiles by Blockchain Account. }, arrays: { + P2P_NETWORK_NODES: [] // Here we will store the p2p nodes deined under Signing Account node types. } } diff --git a/Projects/Network/SA/Modules/P2PNetwork.js b/Projects/Network/SA/Modules/P2PNetwork.js new file mode 100644 index 000000000..8006a6925 --- /dev/null +++ b/Projects/Network/SA/Modules/P2PNetwork.js @@ -0,0 +1,35 @@ +exports.newNetworkModulesP2PNetwork = function newNetworkModulesP2PNetwork() { + /* + This module represents the P2P Network and it is needed to enable the connection + of this current node to other peers. + */ + let thisObject = { + joinTheNetworkAsPeer: joinTheNetworkAsPeer, + /* Framework Functions */ + initialize: initialize, + finalize: finalize + } + + return thisObject + + function finalize() { + + } + + async function initialize() { + /* + We will setup here the maps and arryas we will need to operate within the P2P Network. + */ + let thisP2PNodeId = SA.secrets.map.get(global.env.P2P_NETWORK_NODE_SIGNING_ACCOUNT).signingAccountNodeId + for (let i = 0; i < SA.projects.network.globals.memory.arrays.P2P_NETWORK_NODES.length; i++) { + let p2pNetworkNode = SA.projects.network.globals.memory.arrays.P2P_NETWORK_NODES[i] + + } + } + + function joinTheNetworkAsPeer() { + /* + To join the network we will need to thorugh the list of Network Nodes and pick one to connect to. + */ + } +} \ No newline at end of file diff --git a/Projects/Network/SA/Modules/P2PNetworkNode.js b/Projects/Network/SA/Modules/P2PNetworkNode.js new file mode 100644 index 000000000..a3a821653 --- /dev/null +++ b/Projects/Network/SA/Modules/P2PNetworkNode.js @@ -0,0 +1,29 @@ +exports.newNetworkModulesP2PNetworkNode = function newNetworkModulesP2PNetworkNode() { + /* + This module represents the P2P Network Node. + */ + let thisObject = { + + node: undefined, + userPofile: undefined, + + /* Framework Functions */ + initialize: initialize, + finalize: finalize + } + + return thisObject + + function finalize() { + thisObject.node = undefined + thisObject.userPofile = undefined + } + + async function initialize(node, userPofile) { + /* + We will setup here the maps and arryas we will need to operate within the P2P Network. + */ + thisObject.node = node + thisObject.userPofile = userPofile + } +} \ No newline at end of file diff --git a/Projects/Network/SA/Modules/UserProfiles.js b/Projects/Network/SA/Modules/UserProfiles.js index 346b0b604..746912e69 100644 --- a/Projects/Network/SA/Modules/UserProfiles.js +++ b/Projects/Network/SA/Modules/UserProfiles.js @@ -53,7 +53,7 @@ exports.newNetworkModulesUserProfiles = function newNetworkModulesUserProfiles() SA.projects.network.globals.memory.maps.USER_PROFILES_BY_ID.set(userProfileId, userProfile) SA.projects.network.globals.memory.maps.USER_PROFILES_BY_HANDLE.set(userHandle, userProfile) - SA.projects.network.globals.memory.maps.USER_PROFILES_BY_BLOCHAIN_ACCOUNT.set(blockchainAccount, userProfile) + SA.projects.network.globals.memory.maps.USER_PROFILES_BY_BLOKCHAIN_ACCOUNT.set(blockchainAccount, userProfile) /* Each User Profile might have Signing Accounts, meaning accounts that can be siging on behalf of the User Profile. @@ -65,7 +65,15 @@ exports.newNetworkModulesUserProfiles = function newNetworkModulesUserProfiles() let signatureObject = config.signature let web3 = new SA.nodeModules.web3() let blockchainAccount = web3.eth.accounts.recover(signatureObject) - SA.projects.network.globals.memory.maps.USER_PROFILES_BY_BLOCHAIN_ACCOUNT.set(blockchainAccount, userProfile) + SA.projects.network.globals.memory.maps.USER_PROFILES_BY_BLOKCHAIN_ACCOUNT.set(blockchainAccount, userProfile) + /* + If the Signing Account is for a P2P node, we will add the node to the array of available nodes at the p2p network. + */ + if (signingAccount.signingAccountChild.type === "P2P Network Node") { + p2pNetworkNode = SA.projects.network.modules.p2pMetworkNode.newNetworkModulesP2PNetworkNode() + p2pMetworkNode.initialize(signingAccount.signingAccountChild, userPofile) + SA.projects.network.globals.memory.arrays.P2P_NETWORK_NODES.push(p2pNetworkNode) + } } } } diff --git a/Projects/ProjectsSchema.json b/Projects/ProjectsSchema.json index 8952ce848..b281f405f 100644 --- a/Projects/ProjectsSchema.json +++ b/Projects/ProjectsSchema.json @@ -1407,6 +1407,18 @@ "propertyName": "userProfiles", "functionName": "newNetworkModulesUserProfiles", "fileName": "UserProfiles.js" + }, + { + "name": "P2P Network", + "propertyName": "p2pNetwork", + "functionName": "newSocialTradingModulesP2PNetwork", + "fileName": "P2PNetwork.js" + }, + { + "name": "P2P Network Node", + "propertyName": "p2pNetworkNode", + "functionName": "newSocialTradingModulesP2PNetworkNode", + "fileName": "P2PNetworkNode.js" } ], "utilities": [