fix race.

This commit is contained in:
Mark Nadal 2018-01-20 22:11:47 -08:00
parent a5275172af
commit b895143a60
13 changed files with 487 additions and 22 deletions

23
gun.js
View File

@ -999,6 +999,10 @@
//cat.ack = cat.ack || coat.ack;
}
}
if(node_ === cat.get && change && change['#']){
// TODO: Potential bug? What if (soul.field = pointer) gets changed to (soul.field = primitive), we still need to clear out / wipe /reset (soul.field._) to have _id = nothing, or puts might have false positives (revert back to old soul).
cat._id = change['#'];
}
if(u === change){
ev.to.next(at);
if(cat.soul){ return }
@ -1128,7 +1132,7 @@
var tmp = at.map, root = at.root._;
at.map = null;
if(!root.now || !root.now[at.id]){
if((u === msg.put && !msg['@']) && null === tmp){ return }
if((!msg['@']) && null === tmp){ return }
}
if(u === tmp && Gun.val.rel.is(at.put)){ return } // TODO: Bug? Threw second condition in for a particular test, not sure if a counter example is tested though.
obj_map(tmp, function(proxy){
@ -1374,6 +1378,7 @@
var tmp = cat.root._.now; obj.del(cat.root._, 'now');
var tmp2 = cat.root._.stop;
(as.ref._).now = true;
//console.log("PUT!", as.env.graph);
(as.ref._).on('out', {
gun: as.ref, put: as.out = as.env.graph, opt: as.opt, '#': ask
});
@ -1414,20 +1419,22 @@
}, {as: as, at: at});
}
function soul(at, ev){ var as = this.as, cat = as.at; as = as.as;
function soul(msg, ev){ var as = this.as, cat = as.at; as = as.as;
//ev.stun(); // TODO: BUG!?
if(!at.gun || !at.gun._.back){ return } // TODO: Handle
if(!msg.gun || !msg.gun._.back){ return } // TODO: Handle
var at = msg.gun._, at_ = at;
var _id = (msg.put||empty)['#'];
ev.off();
at = (at.gun._.back._); // go up 1!
var id = id || Gun.node.soul(cat.obj) || Gun.node.soul(at.put) || Gun.val.rel.is(at.put) || (as.gun.back('opt.uuid') || Gun.text.random)(); // TODO: BUG!? Do we really want the soul of the object given to us? Could that be dangerous?
at = (msg.gun._.back._); // go up 1!
var id = id || Gun.node.soul(cat.obj) || Gun.node.soul(at.put) || Gun.val.rel.is(at.put) || _id || at_._id || (as.gun.back('opt.uuid') || Gun.text.random)(); // TODO: BUG!? Do we really want the soul of the object given to us? Could that be dangerous?
if(!id){ // polyfill async uuid for SEA
at.gun.back('opt.uuid')(function(err, id){ // TODO: improve perf without anonymous callback
if(err){ return Gun.log(err) } // TODO: Handle error.
solve(at, id, cat, as);
solve(at, at_._id = at_._id || id, cat, as);
});
return;
}
solve(at, id, cat, as);
solve(at, at_._id = at_._id || id, cat, as);
}
function solve(at, id, cat, as){
@ -1475,7 +1482,7 @@
} else {
//as.data = obj_put({}, as.gun._.get, as.data);
if(node_ == at.get){
as.soul = (at.put||empty)['#'];
as.soul = (at.put||empty)['#'] || at._id;
}
as.soul = as.soul || at.soul || cat.soul || (opt.uuid || cat.root._.opt.uuid || Gun.text.random)();
}

2
gun.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -1,7 +1,7 @@
{
"name": "gun",
"version": "0.9.8",
"description": "Graph engine",
"version": "0.9.9",
"description": "A realtime, decentralized, offline-first, graph data synchronization engine.",
"main": "index.js",
"browser": "gun.min.js",
"scripts": {

1
sea.js
View File

@ -151,6 +151,7 @@
return new Promise(function(resolve, reject){
// load all public keys associated with the username alias we want to log in with.
querygunaliases(alias, root).then(function(aliases){
// TODO: BUG! Occasionally returns [], need to add line to auto-requery!
// then attempt to log into each one until we find ours!
// (if two users have the same username AND the same password... that would be bad)
aliases.forEach(function(one, index){

View File

@ -85,6 +85,10 @@ function input(at){
//cat.ack = cat.ack || coat.ack;
}
}
if(node_ === cat.get && change && change['#']){
// TODO: Potential bug? What if (soul.field = pointer) gets changed to (soul.field = primitive), we still need to clear out / wipe /reset (soul.field._) to have _id = nothing, or puts might have false positives (revert back to old soul).
cat._id = change['#'];
}
if(u === change){
ev.to.next(at);
if(cat.soul){ return }
@ -214,7 +218,7 @@ function not(at, msg){
var tmp = at.map, root = at.root._;
at.map = null;
if(!root.now || !root.now[at.id]){
if((u === msg.put && !msg['@']) && null === tmp){ return }
if((!msg['@']) && null === tmp){ return }
}
if(u === tmp && Gun.val.rel.is(at.put)){ return } // TODO: Bug? Threw second condition in for a particular test, not sure if a counter example is tested though.
obj_map(tmp, function(proxy){

View File

@ -104,6 +104,7 @@ function batch(){ var as = this;
var tmp = cat.root._.now; obj.del(cat.root._, 'now');
var tmp2 = cat.root._.stop;
(as.ref._).now = true;
//console.log("PUT!", as.env.graph);
(as.ref._).on('out', {
gun: as.ref, put: as.out = as.env.graph, opt: as.opt, '#': ask
});
@ -144,20 +145,22 @@ function map(v,f,n, at){ var as = this;
}, {as: as, at: at});
}
function soul(at, ev){ var as = this.as, cat = as.at; as = as.as;
function soul(msg, ev){ var as = this.as, cat = as.at; as = as.as;
//ev.stun(); // TODO: BUG!?
if(!at.gun || !at.gun._.back){ return } // TODO: Handle
if(!msg.gun || !msg.gun._.back){ return } // TODO: Handle
var at = msg.gun._, at_ = at;
var _id = (msg.put||empty)['#'];
ev.off();
at = (at.gun._.back._); // go up 1!
var id = id || Gun.node.soul(cat.obj) || Gun.node.soul(at.put) || Gun.val.rel.is(at.put) || (as.gun.back('opt.uuid') || Gun.text.random)(); // TODO: BUG!? Do we really want the soul of the object given to us? Could that be dangerous?
at = (msg.gun._.back._); // go up 1!
var id = id || Gun.node.soul(cat.obj) || Gun.node.soul(at.put) || Gun.val.rel.is(at.put) || _id || at_._id || (as.gun.back('opt.uuid') || Gun.text.random)(); // TODO: BUG!? Do we really want the soul of the object given to us? Could that be dangerous?
if(!id){ // polyfill async uuid for SEA
at.gun.back('opt.uuid')(function(err, id){ // TODO: improve perf without anonymous callback
if(err){ return Gun.log(err) } // TODO: Handle error.
solve(at, id, cat, as);
solve(at, at_._id = at_._id || id, cat, as);
});
return;
}
solve(at, id, cat, as);
solve(at, at_._id = at_._id || id, cat, as);
}
function solve(at, id, cat, as){
@ -205,7 +208,7 @@ function any(at, ev){
} else {
//as.data = obj_put({}, as.gun._.get, as.data);
if(node_ == at.get){
as.soul = (at.put||empty)['#'];
as.soul = (at.put||empty)['#'] || at._id;
}
as.soul = as.soul || at.soul || cat.soul || (opt.uuid || cat.root._.opt.uuid || Gun.text.random)();
}

View File

@ -213,6 +213,7 @@ if(typeof common !== "undefined"){ common.exports = Gun }
module.exports = Gun;
/*Gun.on('opt', function(ctx){ // FOR TESTING PURPOSES
// G and F!!!!!
this.to.next(ctx);
if(ctx.once){ return }
ctx.on('node', function(msg){

View File

@ -3692,6 +3692,67 @@ describe('Gun', function(){
}, 100);
}, 100);
});
it('users map map who said map on', function(done){
this.timeout(1000 * 60 * 5);
localStorage.clear();
var gun = Gun();
gun.get('users').put({
alice: {_:{'#':'alias/alice'},
'pub/asdf': {_:{'#':'pub/asdf'},
pub: 'asdf'
}
},
bob: {_:{'#':'alias/bob'},
'pub/fdsa': {_:{'#':'pub/fdsa'},
pub: 'fdsa'
}
}
});
var check = {}, c = 0, end;
//console.log(check);
gun.get('users').map().map()
.get('who').get('said').map().on(function(msg){
if(check[msg.num]){
//console.log("!!!!", msg.num, "!!!!");
}
check[msg.num] = false;
c++;
clearTimeout(end); end = setTimeout(function(){
//console.log("?", c, check);
if(Gun.obj.map(check, function(v){ if(v){ return v } })){ return }
done();
},100);
});
var said = gun.get('pub/asdf').get('who').get('said');
function run(i){
//console.log("----", i, "----");
//2 === i && (console.debug.i = 1) && console.debug(1, '======= what happens?');
said.set({
what: i + " Hello world!",
num: i,
who: 'asdf',
id: 'alice',
});
}
var i = 0, m = 9, to = setInterval(function frame(){
if(m <= i){
clearTimeout(to);
return;
}
i++;
check[i] = true;
run(i);
}, 1);
});
return;
it('Nested listener should be called', function(done){

View File

@ -62,7 +62,7 @@ describe("Load test "+ config.browsers +" browser(s) across "+ config.servers +"
}
}
console.log(port, " connect to ", peers);
var gun = Gun({file: env.i+'data', peers: peers, web: server});
var gun = Gun({file: env.i+'data', peers: peers, web: server, localStorage: false});
server.listen(port, function(){
test.done();
});

314
test/panic/scale.js Normal file
View File

@ -0,0 +1,314 @@
var config = {
IP: require('ip').address(),
port: 8080,
servers: 2,
browsers: 2,
each: 500,
burst: 2, // do not go below 1!
wait: 1,
route: {
'/': __dirname + '/index.html',
'/gun.js': __dirname + '/../../gun.js',
'/jquery.js': __dirname + '/../../examples/jquery.js',
'/cryptomodules.js': __dirname + '/../../lib/cryptomodules.js',
'/sea.js': __dirname + '/../../sea.js'
}
}
var panic = require('panic-server');
panic.server().on('request', function(req, res){
config.route[req.url] && require('fs').createReadStream(config.route[req.url]).pipe(res);
}).listen(config.port);
var clients = panic.clients;
var manager = require('panic-manager')();
manager.start({
clients: Array(config.servers).fill().map(function(u, i){
return {
type: 'node',
port: config.port + (i + 1)
}
}),
panic: 'http://' + config.IP + ':' + config.port
});
// Now lets divide our clients into "servers" and "browsers".
var servers = clients.filter('Node.js');
var browsers = clients.excluding(servers);
// Sweet! Now we can start the tests.
// PANIC works with Mocha and other testing libraries!
// So it is easy to use PANIC.
describe("Stress test GUN with SEA users causing PANIC!", function(){
this.timeout(10 * 60 * 1000);
it("Servers have joined!", function(){
return servers.atLeast(config.servers);
});
it("GUN has spawned!", function(){
// Once they are, we need to actually spin up the gun server.
var tests = [], i = 0;
servers.each(function(client){
// for each server peer, tell it to run this code:
tests.push(client.run(function(test){
// NOTE: Despite the fact this LOOKS like we're in a closure...
// it is not! This code is actually getting run
// in a DIFFERENT machine or process!
var env = test.props;
// As a result, we have to manually pass it scope.
test.async();
// Clean up from previous test.
try{ require('fs').unlinkSync(env.i+'data') }catch(e){}
var server = require('http').createServer(function(req, res){
res.end("I am "+ env.i +"!");
});
// Launch the server and start gun!
var Gun = require('gun');
// Attach the server to gun.
var gun = Gun({file: env.i+'data', web: server, localStorage: false});
server.listen(env.config.port + env.i, function(){
// This server peer is now done with the test!
// It has successfully launched.
test.done();
});
}, {i: i += 1, config: config}));
});
// NOW, this is very important:
// Do not proceed to the next test until
// every single server (in different machines/processes)
// have ALL successfully launched.
return Promise.all(tests);
});
it(config.browsers +" browser(s) have joined!", function(){
console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!");
return browsers.atLeast(config.browsers);
});
it("Browsers load SEA!", function(){
var tests = [], i = 0;
browsers.each(function(client, id){
tests.push(client.run(function(test){
test.async();
//console.log("load?");
function load(src, cb){
var script = document.createElement('script');
script.onload = cb; script.src = src;
document.head.appendChild(script);
}
load('cryptomodules.js', function(){
load('sea.js', function(){
test.done();
});
});
}, {i: i += 1, config: config}));
});
return Promise.all(tests);
});
it("Browsers initialized gun!", function(){
var tests = [], ids = {}, i = 0;
// Let us create a list of all the browsers IDs connected.
// This will later let each browser check against every other browser.
browsers.each(function(client, id){
ids[id] = 1;
});
browsers.each(function(client, id){
tests.push(client.run(function(test){
localStorage.clear();
var env = window.env = test.props;
var peers = [], i = env.config.servers;
while(i--){
// For the total number of servers listed in the configuration
// Add their URL into an array.
peers.push('http://'+ env.config.IP + ':' + (env.config.port + (i + 1)) + '/gun');
}
var gun = window.gun = Gun(peers);
var user = window.user = gun.user();
var go = window.go = {num: 0, total: 0, users: {}, pub: {}};
window.ID = env.id;
go.check = Gun.obj.map(env.ids, function(v,id,t){
// for each browser ID
// they will be saving X number of messages each.
go.users[id] = true; // set an outstanding flag to check against.
var i = env.config.each;
while(i--){
// So add a deterministic key we can check against.
t(id + (i + 1), 1);
// And count up the total number of messages we expect for all.
go.total += 1;
}
});
console.log(peers, go);
}, {i: i += 1, id: id, ids: ids, config: config}));
});
return Promise.all(tests);
});
it("All users created!", function(){
var tests = [], ids = {}, i = 0;
browsers.each(function(client, id){
ids[id] = 1;
});
browsers.each(function(client, id){
tests.push(client.run(function(test){
test.async();
gun.on('secure', function(at){
/* enforce some rules about shared app level data */
if(!at.put || !at.put.users){ return }
var no;
Gun.node.is(at.put.users, function(val, key){
Gun.SEA.read(val, false, function(val){
if('alias/'+key === Gun.val.rel.is(val)){ return }
no = true;
})
if(no){ return no }
});
if(no){ return }
this.to.next(at);
});
var unsafepassword = 'asdf'+ID;
console.log("sign in and up:", ID);
window.user.create(ID, unsafepassword, function(ack){
if(ack.err || !ack.pub){ return }
window.pub = ack.pub;
gun.get('users').get(ID).put(gun.get('alias/'+ID));
console.log("signed up", ack.pub);
console.debug.j = 1;
window.user.auth(ID, unsafepassword, function(ack){
console.debug.j = 0;
console.log("signed in", ack);
if(ack.err || !ack.pub){ return }
test.done();
});
});
}, {i: i += 1, id: id, ids: ids, config: config}));
});
return Promise.all(tests);
});
it("Start reading and sending messages!", function(){
var tests = [], ids = {}, i = 0;
browsers.each(function(client, id){
ids[id] = 1;
});
browsers.each(function(client, id){
tests.push(client.run(function(test){
test.async();
gun.get('users').map().map()
.get('who').get('said').map().on(function(msg){
check(msg);
});
var said = user.get('who').get('said');
function run(i){
said.set({
what: i + " Hello world! ~ " + pub.slice(0,3),
num: i,
who: pub,
id: ID,
});/*, function(ack){
if(ack.err){ return }
test.done();
});*/
}
/* TODO: sometimes sign in hangs */
console.log("<<<<< START >>>>>");
var i = 0, to = setInterval(function frame(a, b){
if(!b && 2 <= (b = env.config.burst)){
while(--b){
frame(i, true);
}
return;
}
if(env.config.each <= i){
clearTimeout(to);
return;
}
run(i += 1);
}, env.config.wait || 1);
var col = $("<div>").css({width: 250, position: 'relative', float: 'left', border: 'solid 1px black'}), cols = {};
var report = $("<div>").css({position: 'fixed', top: 0, right: 0, background: 'white', padding: 10}).text(" / "+ go.total +" Verified").prependTo('body');
var reportc = $('<span>').text(0).prependTo(report);
var last = $("<div>").text("Processing: ").css({border: "solid 1px black"}).appendTo("body");
last = $("<span>").text(" ").appendTo(last);
function check(msg){
var who;
if(!go.pub[msg.who]){
go.pub[msg.who] = msg.id;
go.users[msg.id] = false;
//who = cols[msg.id] = col.clone(true).appendTo('body');
//who.prepend("<input value='"+ msg.who +"'>");
//who.prepend("<input value='"+ msg.id +"'>");
}
if(!go.check[msg.id + msg.num]){
return;
}
go.check[msg.id + msg.num] = false;
clearTimeout(end.to); end.to = setTimeout(end, 100);
reportc.text(++go.num);
last.text(msg.what);
//who = cols[msg.id];
//$("<div>").css({border: 'solid 1px blue'}).text(msg.what).appendTo(who);
}
function end(){
var wait = Gun.obj.map(go.users, function(v){ if(v){ return true }});
if(wait){ return }
var more = Gun.obj.map(go.check, function(v){ if(v){ return true }});
if(more){ return }
test.done();
}
}, {i: i += 1, id: id, ids: ids, config: config}));
});
return Promise.all(tests);
});
/* MODEL TEST
it("Browsers initialized gun!", function(){
var tests = [], ids = {}, i = 0;
browsers.each(function(client, id){
ids[id] = 1;
});
browsers.each(function(client, id){
tests.push(client.run(function(test){
// code here
}, {i: i += 1, id: id, ids: ids, config: config}));
});
return Promise.all(tests);
});
*/
it("All finished!", function(done){
console.log("Done! Cleaning things up...");
setTimeout(function(){
done();
},1000);
});
after("Everything shut down.", function(){
browsers.run(function(){
//location.reload();
//setTimeout(function(){
//}, 15 * 1000);
});
return servers.run(function(){
process.exit();
});
});
});

View File

@ -112,7 +112,6 @@ describe("Make sure SEA syncs correctly", function(){
if(ack.err || !ack.pub){ return }
window.user.auth('alice', 'xyzabcmnopq', function(ack){
if(ack.err || !ack.pub){ return }
test.done();
user.get('who').get('said').set({
what: "Hello world!"
}, function(ack){

View File

@ -28,7 +28,7 @@
<button id="share">Share</button>
<a id="share-result"></a>
<script src="../json2.js"></script>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="../../examples/jquery.js"></script>
<script src="benchmark.js"></script>
<script src="ptsd.js"></script>
<script src="../../gun.js"></script>

75
test/say.html Normal file
View File

@ -0,0 +1,75 @@
<script src="../examples/jquery.js"></script>
<script src="../gun.js"></script>
<script src="../lib/cryptomodules.js"></script>
<script src="../sea.js"></script>
<script>
;(function(){
localStorage.clear();sessionStorage.clear();
var gun = window.gun = Gun();
var user = gun.user();
gun.on('secure', function(at){
/* enforce some rules about shared app level data */
if(!at.put || !at.put.users){ return }
var no;
Gun.node.is(at.put.users, function(val, key){
Gun.SEA.read(val, false, function(val){
if('alias/'+key === Gun.val.rel.is(val)){ return }
no = true;
})
if(no){ return no }
});
if(no){ return }
this.to.next(at);
});
user.create('alice', 'unsafepassword', login);
function login(ack){
console.log("login...");
window.pub = ack.pub;
gun.get('users').get('alice').put(gun.get('alias/alice'));
user.auth('alice', 'unsafepassword', sub);
}
function sub(){
console.log("subscribe...");
gun.get('users').map().map()
.get('who').get('said').map().on(function(msg){
if(!check[msg.num]){ console.log(msg.num); return } check[msg.num] = false;
console.log("!!!!!!!", msg.num ,"!!!!!!!");
$("<div>").css({border: '1px solid black'}).text(msg.what).appendTo('body');
});
//window.user = gun.get('pub/'+window.pub);
write();
}
function write(data){
console.log("write...");
var said = window.said = user.get('who').get('said');
window.ID = 'alice';
function run(i){
said.set({
what: i + " Hello world! ~ " + pub.slice(0,3),
num: i,
who: pub,
id: ID,
});
}
window.check = {};
var i = 0, m = 9, to = setInterval(function frame(){
if(m <= i){
clearTimeout(to);
return;
}
i++;
check[i] = true;
run(i);
}, 1);
}
}());
</script>