From 680b2d115addc70499e1bc8044d0b383176d8533 Mon Sep 17 00:00:00 2001 From: Adriano Rogowski Date: Wed, 21 Nov 2018 19:28:28 -0200 Subject: [PATCH] AXE - peer/soul subscribe. In this version the peers receive only data subscribed with gun.get(...soul...).once(...) TODO: 1) if the peer change the id, will lost the subscribes. Workaround for this is set the pid (unique) in: Gun(pid: 'mypeerid', peers:[...]) 2) If the superpeer die, the subscribes will be lost. Each peer must resubscribe all souls. --- axe.js | 31 +++++++++++++++++++++++++------ examples/axe.html | 40 ++++++++++++++++++++++++++++++++++++++++ examples/express.js | 6 +++++- gun.js | 3 ++- lib/super.js | 10 +++++++++- src/adapters/mesh.js | 3 +-- 6 files changed, 82 insertions(+), 11 deletions(-) create mode 100644 examples/axe.html diff --git a/axe.js b/axe.js index 40b3121c..a950e363 100644 --- a/axe.js +++ b/axe.js @@ -47,11 +47,11 @@ // 1. If any remembered peers or from last cache or extension // 2. Fallback to use hard coded peers from dApp // 3. Or any offered peers. - if(Gun.obj.empty(p)){ - Gun.obj.map(['http://localhost:8765/gun'/*, 'https://guntest.herokuapp.com/gun'*/], function(url){ - p[url] = {url: url, axe: {}}; - }); - } + //if(Gun.obj.empty(p)){ + // Gun.obj.map(['http://localhost:8765/gun'/*, 'https://guntest.herokuapp.com/gun'*/], function(url){ + // p[url] = {url: url, axe: {}}; + // }); + //} // Our current hypothesis is that it is most optimal // to take peers in a common network, and align // them in a line, where you only have left and right @@ -61,6 +61,25 @@ // in case the p2p linear latency is high. // Or there could be plenty of other better options. console.log("axe", at.opt); + if(at.opt.super){ + function verify(msg, send, at) { + var peers = Object.keys(p), puts = Object.keys(msg.put), i, j, peer; + var soul = puts[0]; /// TODO: verify all souls in puts. Copy the msg only with subscribed souls? + for (i=0; i < peers.length; ++i) { + peer = p[peers[i]]; + //if (peer.url) {console.log('AXE do not reject superpeers'); send(msg, peer); continue;} /// always send to superpeers? + if (!peer.id) {console.log('AXE peer without id: ', peer); continue;} + if (!Gun.subscribe[soul] || !Gun.subscribe[soul][peer.id]) { console.log('AXE SAY reject msg to peer: %s, soul: %s', peer.id, soul); continue; } + send(msg, peer); + } + } + AXE.say = function(msg, send, at) { + if (!msg.put) { send(msg); return; } + console.log('AXE HOOK!! ', msg); + verify(msg, send, at); + }; + /// TODO: remove peer from all Gun.subscribe. On `mesh.bye` event? + } if(at.opt.super){ at.on('in', USE('./lib/super', 1), at); } else { @@ -77,4 +96,4 @@ module.exports = AXE; })(USE, './axe'); -}()); \ No newline at end of file +}()); diff --git a/examples/axe.html b/examples/axe.html new file mode 100644 index 00000000..40dade3b --- /dev/null +++ b/examples/axe.html @@ -0,0 +1,40 @@ + + + + + + + Testing AXE + + + + + + + + + diff --git a/examples/express.js b/examples/express.js index 81e689c3..a516e18a 100644 --- a/examples/express.js +++ b/examples/express.js @@ -2,12 +2,16 @@ console.log("If module not found, install express globally `npm i express -g`!") var port = process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 8765; var express = require('express'); var Gun = require('..'); +require('../axe'); var app = express(); app.use(Gun.serve); app.use(express.static(__dirname)); var server = app.listen(port); -Gun({ file: 'data.json', web: server }); +var gun = Gun({ file: 'data', web: server }); + +global.Gun = Gun; /// make global to `node --inspect` - debug only +global.gun = gun; /// make global to `node --inspect` - debug only console.log('Server started on port ' + port + ' with /gun'); diff --git a/gun.js b/gun.js index d996771c..7ac767db 100644 --- a/gun.js +++ b/gun.js @@ -1941,6 +1941,7 @@ return; } // add hook for AXE? + if (Gun.AXE && opt && opt.super) { Gun.AXE.say(msg, mesh.say, this); return; } mesh.say(msg); } @@ -2210,4 +2211,4 @@ var noop = function(){}; })(USE, './adapters/websocket'); -}()); \ No newline at end of file +}()); diff --git a/lib/super.js b/lib/super.js index eb2f6368..3f94ff3d 100644 --- a/lib/super.js +++ b/lib/super.js @@ -12,10 +12,18 @@ } else { } + subscribe(soul, peer, msg); } to.next(msg); } + /// Store the subscribes + Gun.subscribe = {}; /// TODO: use Rad instead of plain object? + function subscribe(soul, peer, msg) { + if (!peer.id) { console.log('super jump peer without id: ', peer, msg); return; } /// TODO: this occurs in first subscription. Use peer reference or peer.wire.id? + Gun.subscribe[soul] = Gun.subscribe[soul] || {}; + Gun.subscribe[soul][peer.id] = 1; + } var empty = {}, u; if(Gun.window){ return } try{module.exports = input}catch(e){} -}()); \ No newline at end of file +}()); diff --git a/src/adapters/mesh.js b/src/adapters/mesh.js index 6c99a6e8..a94826f0 100644 --- a/src/adapters/mesh.js +++ b/src/adapters/mesh.js @@ -20,6 +20,7 @@ function Mesh(ctx){ return; } // add hook for AXE? + if (Gun.AXE && opt && opt.super) { Gun.AXE.say(msg, mesh.say, this); return; } mesh.say(msg); } @@ -228,5 +229,3 @@ Mesh.hash = function(s){ // via SO Object.keys = Object.keys || function(o){ return map(o, function(v,k,t){t(k)}) } try{ module.exports = Mesh }catch(e){} - - \ No newline at end of file