fix cache miss of cache hit

This commit is contained in:
Mark Nadal 2020-05-16 01:23:40 -07:00
parent 5a430bc812
commit 880947afe8
5 changed files with 352 additions and 14 deletions

30
gun.js
View File

@ -735,10 +735,10 @@
}
function put(msg){
if(!msg){ return }
var ctx = msg._||'', root = ctx.root = ((msg.$||'')._||'').root;
var ctx = msg._||'', root = ctx.root = ((ctx.$ = msg.$||'')._||'').root;
var put = msg.put, id = msg['#'], err, tmp;
var DBG = ctx.DBG = msg.DBG;
if(put['#'] && put['.']){ root.on('put', msg); return }
if(put['#'] && put['.']){ root.on('put', msg); return } // TODO: BUG! This needs to call HAM instead.
/*root.on(id, function(m){
console.log('ack:', m);
});*/
@ -777,10 +777,11 @@
setTimeout(function(){
ham(val, key, soul, state, msg);
}, to > MD? MD : to); // setTimeout Max Defer 32bit :(
if(!ctx.to){ root.on('in', {'@': msg['#'], err: to}) } ctx.to = 1;
if(!ctx.to){ root.on('in', {'@': msg['#'], err: to}) } ctx.to = 1; // TODO: This causes too many problems unless sending peers auto-retry.
return to;
}
return;
//return; // it should be this
if(!ctx.miss){ return } // but some chains have a cache miss that need to re-fire. // TODO: Improve in future.
}
(lot = ctx.lot||'').s++; lot.more++;
(ctx.stun || (ctx.stun = {}))[soul+key] = 1;
@ -808,7 +809,7 @@
function fire(ctx){
if(ctx.err){ return }
var stop = {};
var root = ctx.root, next = root.next||'', put = ctx.put, tmp;
var root = ((ctx.$||'')._||'').root, next = (root||'').next||'', put = ctx.put, tmp;
var S = +new Date;
//Gun.graph.is(put, function(node, soul){
for(var soul in put){ var node = put[soul]; // Gun.obj.native() makes this safe.
@ -931,6 +932,7 @@
node = Gun.graph.node(node);
tmp = (at||empty).ack;
var faith = function(){}; faith.ram = faith.faith = true; // HNPERF: We're testing performance improvement by skipping going through security again, but this should be audited.
faith.$ = msg.$;
DBG && (DBG.ga = +new Date);
root.on('in', {
'@': msg['#'],
@ -1331,6 +1333,9 @@
at.on('in', {get: at.get, put: Gun.val.link.ify(get['#']), $: at.$, '@': msg['@']});
return;
}
if(at.$ === (msg._||'').$){ // replying to self, for perf, skip ham/security tho needs audit.
(msg._).miss = (at.put === u);
}
Gun.on.put(msg);
}
var empty = {}, u;
@ -1631,9 +1636,12 @@
}
var G = String.fromCharCode(31);
function soul(id, as, msg, eve){
var as = as.as, path = as.p, ref = as.ref, cat = as.at; as = as.as;
var sat = ref.back(function(at){ return sat = at.soul || at.link || at.dub });
var pat = [sat || as.soul].concat(ref._.has || ref._.get || path)
var as = as.as, path = as.p, ref = as.ref, cat = as.at, pat = []; as = as.as;
ref.back(function(at){
if(sat = at.soul || at.link || at.dub){ return sat }
pat.push(at.has || at.get);
});
pat = [sat || as.soul].concat(pat.reverse());
var at = ((msg || {}).$ || {})._ || {};
id = at.dub = at.dub || id || Gun.node.soul(cat.obj) || Gun.node.soul(msg.put || at.put) || Gun.val.link.is(msg.put || at.put) || pat.join('/') /* || (function(){
return (as.soul+'.')+Gun.text.hash(path.join(G)).toString(32);
@ -2157,10 +2165,10 @@
var SMIA = 0;
var message, loop;
function each(peer){ mesh.say(message, peer) }
var say = mesh.say = function(msg, peer){
if(this && this.to){ this.to.next(msg) } // compatible with middleware adapters.
var say = mesh.say = function(msg, peer){ var tmp;
if((tmp = this) && (tmp = tmp.to) && tmp.next){ tmp.next(msg) } // compatible with middleware adapters.
if(!msg){ return false }
var id, hash, tmp, raw;
var id, hash, raw;
var DBG = msg.DBG, S; if(!peer){ S = +new Date ; DBG && (DBG.y = S) }
var meta = msg._||(msg._=function(){});
if(!(id = msg['#'])){ id = msg['#'] = Type.text.random(9) }

View File

@ -8,7 +8,7 @@ Gun.on('create', function(root){
var Radisk = (Gun.window && Gun.window.Radisk) || require('./radisk');
var Radix = Radisk.Radix;
var ST = 0;
// TODO: BUG! For RN storage, RN does not like the following require:
opt.store = opt.store || (!Gun.window && require('./rfs')(opt));
var dare = Radisk(opt), esc = String.fromCharCode(27);

View File

@ -1394,6 +1394,33 @@ describe('Gun', function(){
});
describe('predictable souls', function(){
it('public', function(done){
gun.get('z').get('y').get('x').put({c: {b: {a: 1}}}, function(){
if(done.c){ return } done.c = 1;
var g = gun._.graph;
expect(g['z']).to.be.ok();
expect(g['z/y']).to.be.ok();
expect(g['z/y/x']).to.be.ok();
expect(g['z/y/x/c']).to.be.ok();
expect(g['z/y/x/c/b']).to.be.ok();
done();
});
});
it('public mix', function(done){
var ref = gun.get('zasdf').put({a: 9});
var at = gun.get('zfdsa').get('y').get('x').get('c').put(ref);
at.get('foo').get('bar').put('yay');
ref.get('foo').get('ah').put(1, function(){
if(done.c){ return } done.c = 1;
var g = gun._.graph;
expect(Object.keys(g['zasdf']).sort()).to.be.eql(['_', 'a', 'foo'].sort());
expect(Object.keys(g['zasdf/foo']).sort()).to.be.eql(['_', 'bar', 'ah'].sort());
done();
});
});
});
describe('plural chains', function(){
this.timeout(9000);
it('uncached synchronous map on', function(done){

230
test/panic/thread.js Normal file
View File

@ -0,0 +1,230 @@
var config = {
IP: require('ip').address(),
port: 8765,
servers: 1,
browsers: 2,
route: {
'/': __dirname + '/index.html',
'/gun.js': __dirname + '/../../gun.js',
'/jquery.js': __dirname + '/../../examples/jquery.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
});
var servers = clients.filter('Node.js');
var server = servers.pluck(1);
var browsers = clients.excluding(servers);
var alice = browsers.pluck(1);
var bob = browsers.excluding(alice).pluck(1);
describe("Private Message Threading", function(){
//this.timeout(5 * 60 * 1000);
this.timeout(10 * 60 * 1000);
it("Servers have joined!", function(){
return servers.atLeast(config.servers);
});
it("GUN started!", function(){
return server.run(function(test){
var env = test.props;
test.async();
try{ require('fs').unlinkSync(env.i+'data') }catch(e){}
try{ require('fs').unlinkSync((env.i+1)+'data') }catch(e){}
try{ require('gun/lib/fsrm')(env.i+'data') }catch(e){}
try{ require('gun/lib/fsrm')((env.i+1)+'data') }catch(e){}
var port = env.config.port + env.i;
var server = require('http').createServer(function(req, res){
res.end("I am "+ env.i +"!");
});
var Gun = require('gun');
var gun = Gun({file: env.i+'data', web: server});
server.listen(port, function(){
test.done();
});
}, {i: 1, config: config});
});
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('sea.js', function(){
test.done();
});
}, {i: i += 1, config: config}));
});
return Promise.all(tests);
});
it("Browsers initiate user!", function(){
var tests = [], i = 0;
browsers.each(function(client, id){
tests.push(client.run(function(test){
test.async();
localStorage.clear();
var env = test.props;
console.log("I AM!!!!", env.i);
var gun = window.gun = Gun('http://'+ env.config.IP + ':' + (env.config.port + 1) + '/gun');
var alice = window.alice = {
"pub": "iYIONCL4rq6-SqmXHjDBoWBJIuhReIHy7MtzBp0zEiw.OXqW4c-FnmcbG2K9IM4avl8WyULHvZxQIyvgTH25PSo",
"priv": "5xIKfcjHSrPPr9u80YVJjDITMsif-MHYQyKY7xH-474",
"epub": "4Goj7wufxhuyc7IdIMInEOqXgNHaZi8mCWgDGrQIikI.2oTovqZJb8Ez-GB4VEIVU4ixWlshPwt-99hcZk9i63E",
"epriv": "6ut8nfxpGSBbyBoZso7QU7jyhiYGRgZZ4LPAtJvWQsQ"
};
var bob = window.bob = {
"pub": "xDAF7JiabamDhUiUFuh4dI8cjRM-yIZwYInzoKoOLlQ.qyBHHKvFhNs0BesfWmpkkg-AyBslWgQ5NtIjZjtzpRM",
"priv": "skNy4i4FGFZudqxgkPYMjnggylMu_fZnl-vLref1Hl0",
"epub": "ZiCAyWdAixUxYr0I4KRI2raWDXwj0dQltMvdR0_Eld8.Vjz_pGf2LE6i1Qi-8je9U42mnjoPyB50S2dmXZpnC_E",
"epriv": "JdGqj9E_VzfjgLdi0fpj1VjeP5tKPYfstpd1n9DQklg"
}
var me = window.me = (env.i-1)? bob : alice; // keep it DRY since both need to login.
var them = window.them = (env.i-1)? alice : bob;
window.count = 0;
window.check = {};
window.thread = function thread(node, my, their){
node.on(msgs => {
//console.log("THREAD!!!", msgs);
for(var time in msgs){
// don't load ones already loaded
if(thread[time]){ continue } thread[time] = 1;
//console.log("get...", time, msgs);
node.get(time).on(async function(chat){
if(!chat.msg){ return }
var msg = await SEA.decrypt(chat.msg, sec);
console.log('chat:::::::::::::::::', msg);
window.check[chat.pub + chat.count] = msg;
$('<li>').text(msg).appendTo('#chats');
//gun.get('pchat').get(my).get(their).get('new').get(time).put(null);
})
}
})
}
var to, fro, sec;
gun.user().auth(me, null, function(){
console.log("logged in");
start();
setTimeout(function(){
//return;
test.done();
}, 2000);
});
async function start(){
sec = window.sec = await SEA.secret(me.epub, them);
to = window.to = gun.user().get('pchat').get(them.pub);
fro = window.fro = gun.user(them.pub).get('pchat').get(me.pub);
// TODO: THIS SHOULD DO THE THREADING FOR US LOL!! We have to manually do it.
thread(to, me.pub, them.pub);
thread(fro, me.pub, them.pub);
}
window.send = async function send(text, my, their, me, cb){
var time = +new Date;
var enc = await SEA.encrypt(text, sec);
var msg = {
msg: enc,
pub: my.pub,
time: time,
count: ++window.count,
only_for_test: text
}
console.log('send', msg);
me.get(time).put(msg, cb);
}
console.log("*****", gun._.opt.pid);
}, {i: i += 1, config: config}));
});
return Promise.all(tests);
});
it("Alice send PM", function(){
return alice.run(function(test){
test.async();
console.log("I AM ALICE");
send("I am Alice!", window.alice, window.bob, window.to, function(ack){
console.log("Alice saved!", ack);
setTimeout(function(){
test.done();
}, 2000);
});
});
});
it("Bob send PM", function(){
return bob.run(function(test){
test.async();
console.log("I AM BOB");
send("I am Bob!", window.bob, window.alice, window.to, function(ack){
console.log("Bob saved!", ack);
setTimeout(function(){
test.done();
}, 2000);
});
})
});
it("Wait...", function(done){
setTimeout(done, 2000);
});
it("Alice & Bob both got each other PM", function(){
var tests = [], i = 0;
browsers.each(function(client, id){
tests.push(client.run(function(test){
if(Object.keys(window.check).length !== 2){ more_messages }
if(!window.check[window.me.pub + 1]){ no_me_message }
if(!window.check[window.them.pub + 1]){ no_me_message }
test.done();
}, {i: i += 1, 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

@ -413,10 +413,12 @@ describe('SEA', function(){
var msg = {what: 'hello world'};
user.create('xavier', 'password');
gun.on('auth', function(){
if(done.a){ return } done.a = 1;
var ref = user.get('who').get('all').set(msg);
var stub = user.get('stub').put({});
var tmp = ref._.dub || ref._.link;
setTimeout(function(){
user.get('who').put(1);
user.get('who').put(stub);
setTimeout(function(){
user.get('who').get('all').get(tmp).put({boom: 'ah'});
setTimeout(function(){
@ -431,7 +433,7 @@ describe('SEA', function(){
});
});
it('User\'s nodes must be signed when on user scope!', function(done) {
it("User's nodes must be signed when on user scope!", function(done) {
/// https://github.com/amark/gun/issues/850
/// https://github.com/amark/gun/issues/616
this.timeout(9000);
@ -450,6 +452,77 @@ describe('SEA', function(){
});
});
});
describe('predictable souls', function(){
var alice = {
"pub": "sT1s6lOaUgia5Aiy_Qg_Z4ubCCVFDyGVJsi-i0VmKJI.UTmwQrcKxkHfw0lFK2bkVDaYbd4_2T1Gj-MONFMostM",
"priv": "HUmmMsaphGuOsUHAGGg9HHrYOA5FCrsueY6QexE79AE",
"epub": "MIPYx3rdRJbJSvtan0ruwIjMYaB5W7t42MJ4U1Y2jsk.HFNKa-LoIp5MPI-KFXZhvANjhAxL8dzgXWzLVhewtuk",
"epriv": "7X9rN1NxDYi9jtNU7daIA33__KYEIw3bN5amI-Rc5sw"
};
it("user's", function(done){
var gun = Gun();
gun.on('auth', function(){
gun.user().get('z').get('y').get('x').put({c: {b: {a: 1}}}, function(ack){setTimeout(function(){
if(done.c){ return } done.c = 1;
var g = gun._.graph;
var p = '~'+alice.pub+'/';
console.log(1);
//console.log(p, g);
expect(g[p+'z']).to.be.ok();
expect(g[p+'z/y']).to.be.ok();
expect(g[p+'z/y/x']).to.be.ok();
expect(g[p+'z/y/x/c']).to.be.ok();
expect(g[p+'z/y/x/c/b']).to.be.ok();
done();
},200)});
});
gun.user().auth(alice);
});
it('user mix', function(done){
var gun = Gun();
gun.on('auth', function(){
if(done.a){ return } done.a = 1;
var ref = gun.user().get('zasdf').put({a: 9});
var at = gun.user().get('zfdsa').get('y').get('x').get('c').put(ref);
at.get('foo').get('bar').put('yay');
ref.get('foo').get('ah').put(1, function(){setTimeout(function(){
if(done.c){ return } done.c = 1;
var g = gun._.graph;
var p = '~'+alice.pub+'/';
//console.log(p, g);
console.log(2);
expect(Object.keys(g[p+'zasdf']).sort()).to.be.eql(['_', 'a', 'foo'].sort());
expect(Object.keys(g[p+'zasdf/foo']).sort()).to.be.eql(['_', 'bar', 'ah'].sort());
done();
},200)});
});
gun.user().auth(alice);
});
it('user thread', function(done){
// grr this doesn't properly replicate the issue I saw before
var gun = Gun();
gun.on('auth', async function(){
if(done.a){ return } done.a = 1;
var to = gun.user().get('pchat').get('their.pub');
var enc = await SEA.encrypt('hi', 'secret');
var msg = { msg: enc };
to.get('2020').put(msg, function(){
if(done.c){ return } done.c = 1;
var g = gun._.graph;
var p = '~'+alice.pub+'/';
console.log(3);
console.log(p, Object.keys(g[p+'pchat/their.pub/2020']||{}).sort());
expect(Object.keys(g[p+'pchat/their.pub/2020']).sort()).to.be.eql(['_', 'msg'].sort());
expect(g[p+'2020']).to.not.be.ok();
done();
});
});
gun.user().auth(alice);
});
});
});
})