Skip to content
Snippets Groups Projects
Commit fd405dd3 authored by Cédric Moreau's avatar Cédric Moreau
Browse files

Fixing #374 Certifications Sandbox

parent 07e9d376
Branches
Tags
No related merge requests found
......@@ -54,7 +54,8 @@ module.exports = {
ALREADY_UP_TO_DATE: { httpCode: 400, uerr: { ucode: 1004, message: "Already up-to-date" }},
WRONG_DOCUMENT: { httpCode: 400, uerr: { ucode: 1005, message: "Document has unkown fields or wrong line ending format" }},
HTTP_LIMITATION: { httpCode: 503, uerr: { ucode: 1006, message: "This URI has reached its maximum usage quota. Please retry later." }},
SANDBOX_IS_FULL: { httpCode: 503, uerr: { ucode: 1007, message: "The sandbox for this document is full. Please retry with another document or retry later." }},
SANDBOX_FOR_IDENTITY_IS_FULL: { httpCode: 503, uerr: { ucode: 1007, message: "The identities' sandbox is full. Please retry with another document or retry later." }},
SANDBOX_FOR_CERT_IS_FULL: { httpCode: 503, uerr: { ucode: 1008, message: "The certifications' sandbox is full. Please retry with another document or retry later." }},
HTTP_PARAM_PUBKEY_REQUIRED: { httpCode: 400, uerr: { ucode: 1101, message: "Parameter `pubkey` is required" }},
HTTP_PARAM_IDENTITY_REQUIRED: { httpCode: 400, uerr: { ucode: 1102, message: "Parameter `identity` is required" }},
......
......@@ -5,6 +5,7 @@
const Q = require('q');
const co = require('co');
const AbstractSQLite = require('./AbstractSQLite');
const SandBox = require('./SandBox');
module.exports = CertDAL;
......@@ -116,4 +117,31 @@ function CertDAL(db) {
'SET expired = NULL ' +
'WHERE expired = ' + onNumber);
});
/**************************
* SANDBOX STUFF
*/
this.getSandboxCertifications = () => that.query('SELECT ' +
'* ' +
'FROM ' + that.table + ' ' +
'WHERE expired IS NULL ' +
'AND written_block IS NULL ' +
'ORDER BY block_number ASC ' +
'LIMIT ' + (that.sandbox.maxSize), []);
this.sandbox = new SandBox(30, this.getSandboxCertifications.bind(this), (compared, reference) => {
if (compared.block_number > reference.block_number) {
return -1;
}
else if (compared.block_number < reference.block_number) {
return 1;
}
else {
return 0;
}
});
this.getSandboxRoom = () => this.sandbox.getSandboxRoom();
this.setSandboxSize = (maxSize) => this.sandbox.maxSize = maxSize;
}
\ No newline at end of file
......@@ -96,7 +96,7 @@ function IdentityService () {
}
idty.ref_block = parseInt(idty.buid.split('-')[0]);
if (!(yield dal.idtyDAL.sandbox.acceptNewSandBoxEntry(idty, conf.pair && conf.pair.pub))) {
throw constants.ERRORS.SANDBOX_IS_FULL;
throw constants.ERRORS.SANDBOX_FOR_IDENTITY_IS_FULL;
}
yield dal.savePendingIdentity(idty);
logger.info('✔ IDTY %s %s', idty.pubkey, idty.uid);
......@@ -150,14 +150,11 @@ function IdentityService () {
});
let existingCert = yield dal.existsCert(mCert);
if (!existingCert) {
try {
if (!(yield dal.certDAL.sandbox.acceptNewSandBoxEntry(mCert, conf.pair && conf.pair.pub))) {
throw constants.ERRORS.SANDBOX_FOR_CERT_IS_FULL;
}
yield dal.registerNewCertification(new Certification(mCert));
logger.info('✔ CERT %s', mCert.from);
} catch (e) {
// TODO: This is weird...
logger.error(e);
logger.info('✔ CERT %s', mCert.from);
}
}
} else {
logger.info('✘ CERT %s %s', cert.from, cert.err);
......
......@@ -8,9 +8,13 @@ const commit = require('./tools/commit');
const until = require('./tools/until');
const toolbox = require('./tools/toolbox');
const multicaster = require('../../app/lib/streams/multicaster');
const limiter = require('../../app/lib/system/limiter');
limiter.noLimit();
const s1 = toolbox.server({
idtyWindow: 10,
certWindow: 10,
pair: {
pub: 'HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd',
sec: '51w4fEShBk1jCMauWu4mLpmDVfHksKmWcygpxriqCEZizbtERA6de4STKRkQBpxmMUwsKXRjSzuQ8ECwmqN1u2DP'
......@@ -58,10 +62,14 @@ describe("Sandboxes", function() {
yield s2.initWithDAL().then(bma).then((bmapi) => bmapi.openConnections());
yield s3.initWithDAL().then(bma).then((bmapi) => bmapi.openConnections());
s1.dal.idtyDAL.setSandboxSize(3);
s1.dal.certDAL.setSandboxSize(7);
s2.dal.idtyDAL.setSandboxSize(10);
s3.dal.idtyDAL.setSandboxSize(3);
}));
describe('Identities', () => {
it('should i1, i2, i3', () => co(function *() {
(yield s1.dal.idtyDAL.getSandboxRoom()).should.equal(3);
yield i1.createIdentity();
......@@ -156,6 +164,42 @@ describe("Sandboxes", function() {
}));
});
describe('Certifications', () => {
it('should accept i4->i7(0),i4->i8(0),i4->i9(0)', () => co(function *() {
s1.dal.certDAL.setSandboxSize(3);
(yield s1.dal.certDAL.getSandboxRoom()).should.equal(3);
yield i4.cert(i7);
yield i4.cert(i8);
yield i4.cert(i9);
(yield s1.dal.certDAL.getSandboxRoom()).should.equal(0);
}));
it('should reject i4->i10(0)', () => shouldThrow(co(function *() {
(yield s1.dal.certDAL.getSandboxRoom()).should.equal(0);
yield i4.cert(i10);
})));
it('should accept a certification from the same key as server, always', () => co(function *() {
(yield s1.dal.certDAL.getSandboxRoom()).should.equal(0);
yield i1.cert(i8);
}));
it('should make room as certs get expired', () => co(function *() {
yield s1.commit({
time: now + 1000
});
yield s1.commit({
time: now + 1000
});
yield s1.commit({
time: now + 1000
});
(yield s1.dal.certDAL.getSandboxRoom()).should.equal(3);
}));
});
});
function shouldThrow(promise) {
return promise.should.be.rejected();
}
......@@ -95,7 +95,7 @@ function User (uid, options, node) {
this.cert = (user, fromServer) => co(function*() {
const cert = yield that.makeCert(user, fromServer);
yield Q.nfcall(post, '/wot/certify', {
yield doPost('/wot/certify', {
"cert": cert.getRaw()
});
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment