Skip to content

Commit

Permalink
Fix #492 Have a more personalized difficulty to reach ~5 blocks/member
Browse files Browse the repository at this point in the history
  • Loading branch information
c-geek committed Oct 17, 2016
1 parent 49afbdc commit 722b5d8
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/controllers/blockchain.js
Expand Up @@ -101,7 +101,7 @@ function BlockchainBinding (server) {
const number = (current && current.number) || 0;
const issuers = yield server.dal.getUniqueIssuersBetween(number - 1 - conf.blocksRot, number - 1);
const difficulties = [];
const version = current ? current.version : 3;
const version = current ? current.version : constants.BLOCK_GENERATED_VERSION;
for (const issuer of issuers) {
const member = yield server.dal.getWrittenIdtyByPubkey(issuer);
const difficulty = yield rules.HELPERS.getTrialLevel(version, member.pubkey, conf, server.dal);
Expand Down
2 changes: 2 additions & 0 deletions app/lib/dal/fileDAL.js
Expand Up @@ -215,6 +215,8 @@ function FileDAL(params) {

this.getCountOfPoW = (issuer) => that.blockDAL.getCountOfBlocksIssuedBy(issuer);

this.getNbIssuedInFrame = (issuer, from) => that.blockDAL.getNbIssuedFrom(issuer, from);

this.getBlocksBetween = (start, end) => Q(this.blockDAL.getBlocks(Math.max(0, start), end));

this.getBlockCurrent = () => co(function*() {
Expand Down
5 changes: 5 additions & 0 deletions app/lib/dal/sqliteDAL/BlockDAL.js
Expand Up @@ -108,6 +108,11 @@ function BlockDAL(driver) {
return res[0].quantity;
});

this.getNbIssuedFrom = (issuer, from) => co(function *() {
let res = yield that.query('SELECT COUNT(*) as quantity FROM block WHERE issuer = ? and number >= ? and NOT fork', [issuer, from]);
return res[0].quantity;
});

this.getMoreRecentBlockWithTimeEqualBelow = (maxTime) => co(function *() {
return (yield that.query('SELECT * FROM block WHERE medianTime <= ? and NOT fork ORDER BY number DESC LIMIT 1', [maxTime]))[0];
});
Expand Down
11 changes: 10 additions & 1 deletion app/lib/rules/global_rules.js
Expand Up @@ -953,7 +953,16 @@ function getTrialLevel (version, issuer, conf, dal) {
last = { number: current.number };
}
const nbBlocksSince = current.number - last.number;
let personal_diff = Math.max(powMin, powMin * Math.floor(percentRot * nbPreviousIssuers / (1 + nbBlocksSince)));
let personal_diff;
if (version == 3) {
personal_diff = Math.max(powMin, powMin * Math.floor(percentRot * nbPreviousIssuers / (1 + nbBlocksSince)));
} else {
const from = current.number - current.issuersFrame + 1;
const nbBlocksIssuedInFrame = yield dal.getNbIssuedInFrame(issuer, from);
const personal_excess = Math.max(0, (nbBlocksIssuedInFrame / 5) - 1);
const personal_handicap = Math.floor(Math.log(1 + personal_excess) / Math.log(1.189));
personal_diff = powMin * (1 + Math.floor(percentRot * nbPreviousIssuers / (1 + nbBlocksSince))) + personal_handicap;
}
if (personal_diff + 1 % 16 == 0) {
personal_diff++;
}
Expand Down
5 changes: 4 additions & 1 deletion doc/Protocol.md
Expand Up @@ -1384,7 +1384,9 @@ This field counts the number of different block issuers between the `IssuersFram
To be valid, a block fingerprint (whole document + signature) must start with a specific number of zeros + a remaining mark character. Rules is the following, and **relative to a each particular member**:

```
PERSONAL_DIFF = MAX [ PoWMin ; PoWMin * FLOOR (percentRot * nbPreviousIssuers / (1 + nbBlocksSince)) ]
PERSONAL_EXCESS = MAX(0, (nbPersonalBlocksInFrame / 5) - 1)
PERSONAL_HANDICAP = FLOOR(LN(1 + PERSONAL_EXCESS) / LN(1.189))
PERSONAL_DIFF = PoWMin * (1 + FLOOR (percentRot * nbPreviousIssuers / (1 + nbBlocksSince))) + PERSONAL_HANDICAP
if (PERSONAL_DIFF + 1) % 16 == 0 then PERSONAL_DIFF = PERSONAL_DIFF + 1
Expand All @@ -1394,6 +1396,7 @@ NB_ZEROS = (PERSONAL_DIFFICULTY - REMAINDER) / 16

Where:

* `[nbPersonalBlocksInFrame]` is the number of blocks written by the member from block (current `number` - current `issuersFrame` + 1) to current block (included)
* `[PoWMin]` is the `PoWMin` value of incoming block
* `[percentRot]` is the protocol parameter
* `[nbPreviousIssuers] = DifferentIssuersCount(last block of issuer)`
Expand Down

0 comments on commit 722b5d8

Please sign in to comment.