Mise à jour de GitLab prévue ce samedi 8 mai 2021 à partir de 9h00 CET | GitLab upgrade planned this Saturday May 4th of 2021 from 9:00 AM CET

Commit b3ed38ca authored by Cédric Moreau's avatar Cédric Moreau

TDD: Block:Global: 'number' field should equal previous + 1

parent 1bdc3ca4
......@@ -22,6 +22,9 @@ function GlobalValidator (conf, dao) {
this.validate = function (block, done) {
async.series([
async.apply(checkNumber, block),
async.apply(checkPreviousHash, block),
async.apply(checkPreviousIssuer, block),
async.apply(checkIdentityUnicity, block),
async.apply(checkPubkeyUnicity, block),
async.apply(checkLeaversAreMembers, block),
......@@ -34,6 +37,12 @@ function GlobalValidator (conf, dao) {
], done);
};
/*****************************
*
* UTILITY FUNCTIONS
*
*****************************/
/**
* Get an identity, using global scope.
* Considers identity collision + existence have already been checked.
......@@ -111,6 +120,38 @@ function GlobalValidator (conf, dao) {
}, done);
}
/*****************************
*
* TESTING FUNCTIONS
*
*****************************/
function checkNumber (block, done) {
async.waterfall([
function (next){
dao.getCurrent(next);
},
function (current, next){
if (!current && block.number != 0)
next('Root block required first');
else if (current && block.number <= current.number)
next('Too late for this block');
else if (current && block.number > current.number + 1)
next('Too early for this block');
else
next();
},
], done);
}
function checkPreviousHash (block, done) {
done();
}
function checkPreviousIssuer (block, done) {
done();
}
function checkIdentityUnicity (block, done) {
async.forEach(block.identities, function(inlineIdentity, callback){
var idty = Identity.fromInline(inlineIdentity);
......
......@@ -691,6 +691,19 @@ A *fee* is considered *consumed* for a member if a transaction targeting this fe
A *transaction* is considered *consumed* for a public key if a transaction targeting this transaction was issued and where this public key was present in the outputs.
##### Number
* A block's `Number` must be exactly equal to previous block + 1.
* If blockchain is empty, `Number` must be `0` .
##### PreviousHash
* A block's `PreviousHash` must be exactly equal to previous block's computed hash (a.k.a Proof-of-Work). Note that this hash **must** start with ` powZeroMin` zeros.
##### PreviousIssuer
* A block's `PreviousIssuer` must be exactly equal to previous block's `Issuer` field.
##### Dates
* A block's `ConfirmedDate` has to be equal to *last* (in time value) confirmed date.
......
......@@ -703,4 +703,104 @@ module.exports = {
"HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd:F5PtTpt8QFYMGtpZaETygB2C2yxCSxH1UW1VopBNZ6qg:1411844659:kr2JA6wCGfbNKGpyM86BscsFk22aA9oiAon8mWRPl4G8UpJKZs3tjuPRAw5+04KLCRWl/TT1TumDCkeEjev7DA==\n" +
"Transactions:\n" +
"kNsKdC8eH0d4zdHh1djyMzRXjFrwk3Bc3M8wo4DV/7clE9J66K/U0FljyS79SI78ZZUPaVmrImKJ9SNiubCiBg==\n",
VALID_NEXT:
"Version: 1\n" +
"Type: Block\n" +
"Currency: beta_brousouf\n" +
"Nonce: 11\n" +
"Number: 3\n" +
"Date: 1411776000\n" +
"ConfirmedDate: 1411776000\n" +
"Issuer: HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd\n" +
"PreviousHash: 15978746968DB6BE3CDAF243E372FEB35F7B0924\n" +
"PreviousIssuer: G2CBgZBPLe6FSFUgpx2Jf1Aqsgta6iib3vmDRA1yLiqU\n" +
"MembersCount: 3\n" +
"Identities:\n" +
"Joiners:\n" +
"Leavers:\n" +
"Excluded:\n" +
"Certifications:\n" +
"Transactions:\n" +
"kNsKdC8eH0d4zdHh1djyMzRXjFrwk3Bc3M8wo4DV/7clE9J66K/U0FljyS79SI78ZZUPaVmrImKJ9SNiubCiBg==\n",
ROOT_BLOCK_REQUIRED:
"Version: 1\n" +
"Type: Block\n" +
"Currency: beta_brousouf\n" +
"Nonce: 11\n" +
"Number: 1\n" +
"Date: 1411776000\n" +
"ConfirmedDate: 1411776000\n" +
"Issuer: HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd\n" +
"PreviousHash: 15978746968DB6BE3CDAF243E372FEB35F7B0924\n" +
"PreviousIssuer: G2CBgZBPLe6FSFUgpx2Jf1Aqsgta6iib3vmDRA1yLiqU\n" +
"MembersCount: 3\n" +
"Identities:\n" +
"Joiners:\n" +
"Leavers:\n" +
"Excluded:\n" +
"Certifications:\n" +
"Transactions:\n" +
"kNsKdC8eH0d4zdHh1djyMzRXjFrwk3Bc3M8wo4DV/7clE9J66K/U0FljyS79SI78ZZUPaVmrImKJ9SNiubCiBg==\n",
SAME_BLOCK_NUMBER:
"Version: 1\n" +
"Type: Block\n" +
"Currency: beta_brousouf\n" +
"Nonce: 11\n" +
"Number: 50\n" +
"Date: 1411776000\n" +
"ConfirmedDate: 1411776000\n" +
"Issuer: HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd\n" +
"PreviousHash: 15978746968DB6BE3CDAF243E372FEB35F7B0924\n" +
"PreviousIssuer: G2CBgZBPLe6FSFUgpx2Jf1Aqsgta6iib3vmDRA1yLiqU\n" +
"MembersCount: 3\n" +
"Identities:\n" +
"Joiners:\n" +
"Leavers:\n" +
"Excluded:\n" +
"Certifications:\n" +
"Transactions:\n" +
"kNsKdC8eH0d4zdHh1djyMzRXjFrwk3Bc3M8wo4DV/7clE9J66K/U0FljyS79SI78ZZUPaVmrImKJ9SNiubCiBg==\n",
OLD_BLOCK_NUMBER:
"Version: 1\n" +
"Type: Block\n" +
"Currency: beta_brousouf\n" +
"Nonce: 11\n" +
"Number: 49\n" +
"Date: 1411776000\n" +
"ConfirmedDate: 1411776000\n" +
"Issuer: HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd\n" +
"PreviousHash: 15978746968DB6BE3CDAF243E372FEB35F7B0924\n" +
"PreviousIssuer: G2CBgZBPLe6FSFUgpx2Jf1Aqsgta6iib3vmDRA1yLiqU\n" +
"MembersCount: 3\n" +
"Identities:\n" +
"Joiners:\n" +
"Leavers:\n" +
"Excluded:\n" +
"Certifications:\n" +
"Transactions:\n" +
"kNsKdC8eH0d4zdHh1djyMzRXjFrwk3Bc3M8wo4DV/7clE9J66K/U0FljyS79SI78ZZUPaVmrImKJ9SNiubCiBg==\n",
FAR_FUTURE_BLOCK_NUMBER:
"Version: 1\n" +
"Type: Block\n" +
"Currency: beta_brousouf\n" +
"Nonce: 11\n" +
"Number: 52\n" +
"Date: 1411776000\n" +
"ConfirmedDate: 1411776000\n" +
"Issuer: HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd\n" +
"PreviousHash: 15978746968DB6BE3CDAF243E372FEB35F7B0924\n" +
"PreviousIssuer: G2CBgZBPLe6FSFUgpx2Jf1Aqsgta6iib3vmDRA1yLiqU\n" +
"MembersCount: 3\n" +
"Identities:\n" +
"Joiners:\n" +
"Leavers:\n" +
"Excluded:\n" +
"Certifications:\n" +
"Transactions:\n" +
"kNsKdC8eH0d4zdHh1djyMzRXjFrwk3Bc3M8wo4DV/7clE9J66K/U0FljyS79SI78ZZUPaVmrImKJ9SNiubCiBg==\n",
};
......@@ -22,6 +22,11 @@ describe("Block global coherence", function(){
done();
}));
it('a valid (next) block should not have any error', validate(blocks.VALID_NEXT, function (err, done) {
should.not.exist(err);
done();
}));
it('a block with certification of unknown pubkey should fail', validate(blocks.WRONGLY_SIGNED_CERTIFICATION, function (err, done) {
should.exist(err);
err.should.equal('Wrong signature for certification');
......@@ -70,6 +75,30 @@ describe("Block global coherence", function(){
done();
}));
it('a block with positive number while no root exists should fail', validate(blocks.ROOT_BLOCK_REQUIRED, function (err, done) {
should.exist(err);
err.should.equal('Root block required first');
done();
}));
it('a block with same number as current should fail', validate(blocks.SAME_BLOCK_NUMBER, function (err, done) {
should.exist(err);
err.should.equal('Too late for this block');
done();
}));
it('a block with older number than current should fail', validate(blocks.OLD_BLOCK_NUMBER, function (err, done) {
should.exist(err);
err.should.equal('Too late for this block');
done();
}));
it('a block with too far future number than current should fail', validate(blocks.FAR_FUTURE_BLOCK_NUMBER, function (err, done) {
should.exist(err);
err.should.equal('Too early for this block');
done();
}));
});
function validate (raw, callback) {
......@@ -165,4 +194,14 @@ function BlockCheckerDao (block) {
done(null, []);
}
this.getCurrent = function (done) {
if (block.number == 3) done(null, { number: 2 });
else if (block.number == 1) done(null, null);
else if (block.number == 50) done(null, { number: 50 });
else if (block.number == 49) done(null, { number: 50 });
else if (block.number == 52) done(null, { number: 50 });
else
done(null, null);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment