Commit 869662e1 authored by Cédric Moreau's avatar Cédric Moreau

[fix] #1037 Migrate all the "rules/"

parent 927eb092
......@@ -11,8 +11,7 @@ app/lib/dal/sqliteDAL/index/*.js
app/lib/dal/fileDALs/*.js
app/lib/dal/fileDAL.js
app/service/*.js
app/lib/rules/local_rules.js
app/lib/rules/global_rules.js
app/lib/rules/*.js
app/modules/wizard.js
app/modules/router.js
app/modules/revert.js
......
......@@ -47,8 +47,7 @@ app/lib/dal/sqliteDAL/*.js*
app/lib/dal/sqliteDAL/index/*.js*
app/lib/dal/fileDALs/*.js*
app/lib/dal/fileDAL.js*
app/lib/rules/local_rules*.js*
app/lib/rules/global_rules*.js*
app/lib/rules/*.js*
app/lib/logger*js*
app/service/*.js*
app/lib/wot.js*
......
......@@ -5,9 +5,9 @@ import {ConfDTO} from "../dto/ConfDTO"
import {BlockDTO} from "../dto/BlockDTO"
import {DBHead} from "../db/DBHead"
import {DBBlock} from "../db/DBBlock"
import {CHECK} from "../rules/index"
const _ = require('underscore')
const rules = require('../rules')
const common = require('duniter-common')
const Block = require('../entity/block')
const Identity = require('../entity/identity')
......@@ -21,13 +21,13 @@ export class DuniterBlockchain extends MiscIndexedBlockchain {
super(blockchainStorage, dal.mindexDAL, dal.iindexDAL, dal.sindexDAL, dal.cindexDAL)
}
async checkBlock(block:BlockDTO, withPoWAndSignature:boolean, conf: ConfDTO, dal:any) {
static async checkBlock(block:BlockDTO, withPoWAndSignature:boolean, conf: ConfDTO, dal:any) {
const index = Indexer.localIndex(block, conf)
if (withPoWAndSignature) {
await rules.CHECK.ASYNC.ALL_LOCAL(block, conf, index)
await CHECK.ASYNC.ALL_LOCAL(block, conf, index)
}
else {
await rules.CHECK.ASYNC.ALL_LOCAL_BUT_POW(block, conf, index)
await CHECK.ASYNC.ALL_LOCAL_BUT_POW(block, conf, index)
}
const HEAD = await Indexer.completeGlobalScope(block, conf, index, dal);
const HEAD_1 = await dal.bindexDAL.head(1);
......
......@@ -102,7 +102,7 @@ export class BlockchainContext {
}
checkBlock(block: BlockDTO, withPoWAndSignature = true): Promise<any> {
return this.blockchain.checkBlock(block, withPoWAndSignature, this.conf, this.dal)
return DuniterBlockchain.checkBlock(block, withPoWAndSignature, this.conf, this.dal)
}
async addBlock(obj: BlockDTO, index: any = null, HEAD: DBHead | null = null): Promise<any> {
......
......@@ -240,7 +240,7 @@ export class QuickSynchronizer {
await this.dal.walletDAL.insertBatch(walletsToRecord)
// Last block: cautious mode to trigger all the INDEX expiry mechanisms
const { index, HEAD } = await this.blockchain.checkBlock(dto, constants.WITH_SIGNATURES_AND_POW, this.conf, this.dal)
const { index, HEAD } = await DuniterBlockchain.checkBlock(dto, constants.WITH_SIGNATURES_AND_POW, this.conf, this.dal)
await this.blockchain.pushTheBlock(dto, index, HEAD, this.conf, this.dal, this.logger)
// Clean temporary variables
......
......@@ -6,6 +6,7 @@ import {RevocationDTO} from "./dto/RevocationDTO"
import {CertificationDTO} from "./dto/CertificationDTO"
import {TransactionDTO} from "./dto/TransactionDTO"
import {DBHead} from "./db/DBHead"
import {LOCAL_RULES_HELPERS} from "./rules/local_rules"
const co = require('co');
const _ = require('underscore');
......@@ -1975,7 +1976,7 @@ async function checkCertificationIsValid (block: BlockDTO, cert: CindexEntry, fi
function txSourceUnlock(ENTRY:SindexEntry, source:SindexEntry, HEAD: DBHead) {
const tx = ENTRY.txObj;
let sigResults = require('./rules').HELPERS.getSigResult(tx, 'a');
let sigResults = LOCAL_RULES_HELPERS.getSigResult(tx)
let unlocksForCondition = [];
let unlocksMetadata: any = {};
let unlockValues = ENTRY.unlock;
......
......@@ -5,12 +5,12 @@ import {FileDAL} from "../dal/fileDAL"
import {DBBlock} from "../db/DBBlock"
import {DBIdentity} from "../dal/sqliteDAL/IdentityDAL"
import {TransactionDTO} from "../dto/TransactionDTO"
import * as local_rules from "./local_rules"
const co = require('co');
const _ = require('underscore');
const common = require('duniter-common');
const indexer = require('../indexer').Indexer
const local_rules = require('../rules/local_rules');
const constants = common.constants
const keyring = common.keyring
......@@ -19,8 +19,6 @@ const Identity = common.document.Identity
const Transaction = common.document.Transaction
const unlock = common.txunlock
export const rules:any = {}
// Empty logger by default
let logger = {
debug: (...args:any[]) => {},
......@@ -29,9 +27,9 @@ let logger = {
// TODO: all the global rules should be replaced by index rule someday
rules.FUNCTIONS = {
export const GLOBAL_RULES_FUNCTIONS = {
checkIdentitiesAreWritable: async (block:BlockDTO, conf:ConfDTO, dal:FileDAL) => {
checkIdentitiesAreWritable: async (block:{ identities:string[], version: number }, conf:ConfDTO, dal:FileDAL) => {
let current = await dal.getCurrentBlockOrNull();
for (const obj of block.identities) {
let idty = Identity.fromInline(obj);
......@@ -53,7 +51,7 @@ rules.FUNCTIONS = {
return true;
},
checkSourcesAvailability: async (block:BlockDTO, conf:ConfDTO, dal:FileDAL, alsoCheckPendingTransactions:boolean) => {
checkSourcesAvailability: async (block:{ transactions:TransactionDTO[], medianTime: number }, conf:ConfDTO, dal:FileDAL, alsoCheckPendingTransactions:boolean) => {
const txs = block.transactions
const current = await dal.getCurrentBlockOrNull();
for (const tx of txs) {
......@@ -96,7 +94,7 @@ rules.FUNCTIONS = {
if (block.medianTime - dbSrc.written_time < tx.locktime) {
throw constants.ERRORS.LOCKTIME_PREVENT;
}
let sigResults = local_rules.HELPERS.getSigResult(tx);
let sigResults = local_rules.LOCAL_RULES_HELPERS.getSigResult(tx);
let unlocksForCondition = [];
let unlocksMetadata:any = {};
let unlockValues = unlocks[k];
......@@ -155,7 +153,7 @@ rules.FUNCTIONS = {
}
}
rules.HELPERS = {
export const GLOBAL_RULES_HELPERS = {
// Functions used in an external context too
checkMembershipBlock: (ms:any, current:DBBlock, conf:ConfDTO, dal:FileDAL) => checkMSTarget(ms, current ? { number: current.number + 1} : { number: 0 }, conf, dal),
......@@ -179,7 +177,7 @@ rules.HELPERS = {
checkExistsPubkey: (pub:string, dal:FileDAL) => dal.getWrittenIdtyByPubkey(pub),
checkSingleTransaction: (tx:TransactionDTO, block:BlockDTO, conf:ConfDTO, dal:FileDAL, alsoCheckPendingTransactions:boolean) => rules.FUNCTIONS.checkSourcesAvailability({
checkSingleTransaction: (tx:TransactionDTO, block:{ medianTime: number }, conf:ConfDTO, dal:FileDAL, alsoCheckPendingTransactions:boolean) => GLOBAL_RULES_FUNCTIONS.checkSourcesAvailability({
transactions: [tx],
medianTime: block.medianTime
}, conf, dal, alsoCheckPendingTransactions),
......
"use strict";
const common = require('duniter-common');
const constants = common.constants
module.exports = {
maxAcceleration
}
function maxAcceleration (conf) {
let maxGenTime = Math.ceil(conf.avgGenTime * constants.POW_DIFFICULTY_RANGE_RATIO);
return Math.ceil(maxGenTime * conf.medianTimeBlocks);
Object.defineProperty(exports, "__esModule", { value: true });
const common = require('duniter-common');
const constants = common.constants;
function maxAcceleration(conf) {
let maxGenTime = Math.ceil(conf.avgGenTime * constants.POW_DIFFICULTY_RANGE_RATIO);
return Math.ceil(maxGenTime * conf.medianTimeBlocks);
}
exports.maxAcceleration = maxAcceleration;
//# sourceMappingURL=helpers.js.map
\ No newline at end of file
import {ConfDTO} from "../dto/ConfDTO"
const common = require('duniter-common');
const constants = common.constants
export function maxAcceleration (conf:ConfDTO) {
let maxGenTime = Math.ceil(conf.avgGenTime * constants.POW_DIFFICULTY_RANGE_RATIO);
return Math.ceil(maxGenTime * conf.medianTimeBlocks);
}
"use strict";
const _ = require('underscore');
const co = require('co');
const common = require('duniter-common');
const local_rules = require('../rules/local_rules')
const global_rules = require('../rules/global_rules').rules
const Block = common.document.Block
let rules = {};
rules.LOCAL = local_rules.FUNCTIONS;
rules.GLOBAL = global_rules.FUNCTIONS;
rules.HELPERS = {};
_.extend(rules.HELPERS, local_rules.HELPERS);
_.extend(rules.HELPERS, global_rules.HELPERS);
rules.ALIAS = {
ALL_LOCAL: (block, conf, index) => co(function *() {
yield rules.LOCAL.checkParameters(block);
yield rules.LOCAL.checkProofOfWork(block);
yield rules.LOCAL.checkInnerHash(block);
yield rules.LOCAL.checkPreviousHash(block);
yield rules.LOCAL.checkPreviousIssuer(block);
yield rules.LOCAL.checkUnitBase(block);
yield rules.LOCAL.checkBlockSignature(block);
yield rules.LOCAL.checkBlockTimes(block, conf);
yield rules.LOCAL.checkIdentitiesSignature(block);
yield rules.LOCAL.checkIdentitiesUserIDConflict(block, conf, index);
yield rules.LOCAL.checkIdentitiesPubkeyConflict(block, conf, index);
yield rules.LOCAL.checkIdentitiesMatchJoin(block, conf, index);
yield rules.LOCAL.checkMembershipUnicity(block, conf, index);
yield rules.LOCAL.checkRevokedUnicity(block, conf, index);
yield rules.LOCAL.checkRevokedAreExcluded(block, conf, index);
yield rules.LOCAL.checkMembershipsSignature(block);
yield rules.LOCAL.checkPubkeyUnicity(block);
yield rules.LOCAL.checkCertificationOneByIssuer(block, conf, index);
yield rules.LOCAL.checkCertificationUnicity(block, conf, index);
yield rules.LOCAL.checkCertificationIsntForLeaverOrExcluded(block, conf, index);
yield rules.LOCAL.checkTxVersion(block);
yield rules.LOCAL.checkTxIssuers(block);
yield rules.LOCAL.checkTxSources(block);
yield rules.LOCAL.checkTxRecipients(block);
yield rules.LOCAL.checkTxAmounts(block);
yield rules.LOCAL.checkTxSignature(block);
}),
ALL_LOCAL_BUT_POW_AND_SIGNATURE: (block, conf, index) => co(function *() {
yield rules.LOCAL.checkParameters(block);
yield rules.LOCAL.checkInnerHash(block);
yield rules.LOCAL.checkPreviousHash(block);
yield rules.LOCAL.checkPreviousIssuer(block);
yield rules.LOCAL.checkUnitBase(block);
yield rules.LOCAL.checkBlockTimes(block, conf);
yield rules.LOCAL.checkIdentitiesSignature(block);
yield rules.LOCAL.checkIdentitiesUserIDConflict(block, conf, index);
yield rules.LOCAL.checkIdentitiesPubkeyConflict(block, conf, index);
yield rules.LOCAL.checkIdentitiesMatchJoin(block, conf, index);
yield rules.LOCAL.checkMembershipUnicity(block, conf, index);
yield rules.LOCAL.checkRevokedUnicity(block, conf, index);
yield rules.LOCAL.checkRevokedAreExcluded(block, conf, index);
yield rules.LOCAL.checkMembershipsSignature(block);
yield rules.LOCAL.checkPubkeyUnicity(block);
yield rules.LOCAL.checkCertificationOneByIssuer(block, conf, index);
yield rules.LOCAL.checkCertificationUnicity(block, conf, index);
yield rules.LOCAL.checkCertificationIsntForLeaverOrExcluded(block, conf, index);
yield rules.LOCAL.checkTxVersion(block);
yield rules.LOCAL.checkTxIssuers(block);
yield rules.LOCAL.checkTxSources(block);
yield rules.LOCAL.checkTxRecipients(block);
yield rules.LOCAL.checkTxAmounts(block);
yield rules.LOCAL.checkTxSignature(block);
})
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const local_rules_1 = require("./local_rules");
const common = require('duniter-common');
const Block = common.document.Block;
exports.ALIAS = {
ALL_LOCAL: (block, conf, index) => __awaiter(this, void 0, void 0, function* () {
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkParameters(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkProofOfWork(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkInnerHash(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkPreviousHash(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkPreviousIssuer(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkUnitBase(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkBlockSignature(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkBlockTimes(block, conf);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkIdentitiesSignature(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkIdentitiesUserIDConflict(block, conf, index);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkIdentitiesPubkeyConflict(block, conf, index);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkIdentitiesMatchJoin(block, conf, index);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkMembershipUnicity(block, conf, index);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkRevokedUnicity(block, conf, index);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkRevokedAreExcluded(block, conf, index);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkMembershipsSignature(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkPubkeyUnicity(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkCertificationOneByIssuer(block, conf, index);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkCertificationUnicity(block, conf, index);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkCertificationIsntForLeaverOrExcluded(block, conf, index);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkTxVersion(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkTxIssuers(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkTxSources(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkTxRecipients(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkTxAmounts(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkTxSignature(block);
}),
ALL_LOCAL_BUT_POW_AND_SIGNATURE: (block, conf, index) => __awaiter(this, void 0, void 0, function* () {
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkParameters(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkInnerHash(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkPreviousHash(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkPreviousIssuer(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkUnitBase(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkBlockTimes(block, conf);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkIdentitiesSignature(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkIdentitiesUserIDConflict(block, conf, index);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkIdentitiesPubkeyConflict(block, conf, index);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkIdentitiesMatchJoin(block, conf, index);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkMembershipUnicity(block, conf, index);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkRevokedUnicity(block, conf, index);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkRevokedAreExcluded(block, conf, index);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkMembershipsSignature(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkPubkeyUnicity(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkCertificationOneByIssuer(block, conf, index);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkCertificationUnicity(block, conf, index);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkCertificationIsntForLeaverOrExcluded(block, conf, index);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkTxVersion(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkTxIssuers(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkTxSources(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkTxRecipients(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkTxAmounts(block);
yield local_rules_1.LOCAL_RULES_FUNCTIONS.checkTxSignature(block);
})
};
rules.CHECK = {
ASYNC: {
ALL_LOCAL: checkLocal(rules.ALIAS.ALL_LOCAL),
ALL_LOCAL_BUT_POW: checkLocal(rules.ALIAS.ALL_LOCAL_BUT_POW_AND_SIGNATURE)
}
exports.CHECK = {
ASYNC: {
ALL_LOCAL: checkLocal(exports.ALIAS.ALL_LOCAL),
ALL_LOCAL_BUT_POW: checkLocal(exports.ALIAS.ALL_LOCAL_BUT_POW_AND_SIGNATURE)
}
};
function checkLocal(contract) {
return (b, conf, index, done) => {
return co(function *() {
try {
const block = Block.fromJSON(b);
yield contract(block, conf, index);
done && done();
} catch (err) {
if (done) return done(err);
throw err;
}
return (b, conf, index, done = undefined) => __awaiter(this, void 0, void 0, function* () {
try {
const block = Block.fromJSON(b);
yield contract(block, conf, index);
done && done();
}
catch (err) {
if (done)
return done(err);
throw err;
}
});
};
}
module.exports = rules;
//# sourceMappingURL=index.js.map
\ No newline at end of file
"use strict";
import {BlockDTO} from "../dto/BlockDTO"
import {ConfDTO} from "../dto/ConfDTO"
import {IndexEntry} from "../indexer"
import {LOCAL_RULES_FUNCTIONS} from "./local_rules"
const common = require('duniter-common');
const Block = common.document.Block
export const ALIAS = {
ALL_LOCAL: async (block:BlockDTO, conf:ConfDTO, index:IndexEntry[]) => {
await LOCAL_RULES_FUNCTIONS.checkParameters(block);
await LOCAL_RULES_FUNCTIONS.checkProofOfWork(block);
await LOCAL_RULES_FUNCTIONS.checkInnerHash(block);
await LOCAL_RULES_FUNCTIONS.checkPreviousHash(block);
await LOCAL_RULES_FUNCTIONS.checkPreviousIssuer(block);
await LOCAL_RULES_FUNCTIONS.checkUnitBase(block);
await LOCAL_RULES_FUNCTIONS.checkBlockSignature(block);
await LOCAL_RULES_FUNCTIONS.checkBlockTimes(block, conf);
await LOCAL_RULES_FUNCTIONS.checkIdentitiesSignature(block);
await LOCAL_RULES_FUNCTIONS.checkIdentitiesUserIDConflict(block, conf, index);
await LOCAL_RULES_FUNCTIONS.checkIdentitiesPubkeyConflict(block, conf, index);
await LOCAL_RULES_FUNCTIONS.checkIdentitiesMatchJoin(block, conf, index);
await LOCAL_RULES_FUNCTIONS.checkMembershipUnicity(block, conf, index);
await LOCAL_RULES_FUNCTIONS.checkRevokedUnicity(block, conf, index);
await LOCAL_RULES_FUNCTIONS.checkRevokedAreExcluded(block, conf, index);
await LOCAL_RULES_FUNCTIONS.checkMembershipsSignature(block);
await LOCAL_RULES_FUNCTIONS.checkPubkeyUnicity(block);
await LOCAL_RULES_FUNCTIONS.checkCertificationOneByIssuer(block, conf, index);
await LOCAL_RULES_FUNCTIONS.checkCertificationUnicity(block, conf, index);
await LOCAL_RULES_FUNCTIONS.checkCertificationIsntForLeaverOrExcluded(block, conf, index);
await LOCAL_RULES_FUNCTIONS.checkTxVersion(block);
await LOCAL_RULES_FUNCTIONS.checkTxIssuers(block);
await LOCAL_RULES_FUNCTIONS.checkTxSources(block);
await LOCAL_RULES_FUNCTIONS.checkTxRecipients(block);
await LOCAL_RULES_FUNCTIONS.checkTxAmounts(block);
await LOCAL_RULES_FUNCTIONS.checkTxSignature(block);
},
ALL_LOCAL_BUT_POW_AND_SIGNATURE: async (block:BlockDTO, conf:ConfDTO, index:IndexEntry[]) => {
await LOCAL_RULES_FUNCTIONS.checkParameters(block);
await LOCAL_RULES_FUNCTIONS.checkInnerHash(block);
await LOCAL_RULES_FUNCTIONS.checkPreviousHash(block);
await LOCAL_RULES_FUNCTIONS.checkPreviousIssuer(block);
await LOCAL_RULES_FUNCTIONS.checkUnitBase(block);
await LOCAL_RULES_FUNCTIONS.checkBlockTimes(block, conf);
await LOCAL_RULES_FUNCTIONS.checkIdentitiesSignature(block);
await LOCAL_RULES_FUNCTIONS.checkIdentitiesUserIDConflict(block, conf, index);
await LOCAL_RULES_FUNCTIONS.checkIdentitiesPubkeyConflict(block, conf, index);
await LOCAL_RULES_FUNCTIONS.checkIdentitiesMatchJoin(block, conf, index);
await LOCAL_RULES_FUNCTIONS.checkMembershipUnicity(block, conf, index);
await LOCAL_RULES_FUNCTIONS.checkRevokedUnicity(block, conf, index);
await LOCAL_RULES_FUNCTIONS.checkRevokedAreExcluded(block, conf, index);
await LOCAL_RULES_FUNCTIONS.checkMembershipsSignature(block);
await LOCAL_RULES_FUNCTIONS.checkPubkeyUnicity(block);
await LOCAL_RULES_FUNCTIONS.checkCertificationOneByIssuer(block, conf, index);
await LOCAL_RULES_FUNCTIONS.checkCertificationUnicity(block, conf, index);
await LOCAL_RULES_FUNCTIONS.checkCertificationIsntForLeaverOrExcluded(block, conf, index);
await LOCAL_RULES_FUNCTIONS.checkTxVersion(block);
await LOCAL_RULES_FUNCTIONS.checkTxIssuers(block);
await LOCAL_RULES_FUNCTIONS.checkTxSources(block);
await LOCAL_RULES_FUNCTIONS.checkTxRecipients(block);
await LOCAL_RULES_FUNCTIONS.checkTxAmounts(block);
await LOCAL_RULES_FUNCTIONS.checkTxSignature(block);
}
}
export const CHECK = {
ASYNC: {
ALL_LOCAL: checkLocal(ALIAS.ALL_LOCAL),
ALL_LOCAL_BUT_POW: checkLocal(ALIAS.ALL_LOCAL_BUT_POW_AND_SIGNATURE)
}
};
function checkLocal(contract:(block:BlockDTO, conf:ConfDTO, index:IndexEntry[]) => Promise<void>) {
return async (b:BlockDTO, conf:ConfDTO, index:IndexEntry[], done:any = undefined) => {
try {
const block = Block.fromJSON(b);
await contract(block, conf, index)
done && done();
} catch (err) {
if (done) return done(err);
throw err;
}
};
}
"use strict";
import {BlockDTO} from "../dto/BlockDTO"
import {ConfDTO} from "../dto/ConfDTO"
import {CindexEntry, MindexEntry, SindexEntry} from "../indexer"
import {CindexEntry, IndexEntry, Indexer, MindexEntry, SindexEntry} from "../indexer"
import {BaseDTO, TransactionDTO} from "../dto/TransactionDTO"
import {DBBlock} from "../db/DBBlock"
const _ = require('underscore');
const common = require('duniter-common');
const indexer = require('../indexer').Indexer;
const constants = common.constants
const hashf = common.hashf
......@@ -18,7 +17,7 @@ const Membership = common.document.Membership
const Transaction = common.document.Transaction
const maxAcceleration = require('./helpers').maxAcceleration
export const FUNCTIONS = {
export const LOCAL_RULES_FUNCTIONS = {
checkParameters: async (block:BlockDTO) => {
if (block.number == 0 && !block.parameters) {
......@@ -104,8 +103,8 @@ export const FUNCTIONS = {
return true;
},
checkIdentitiesUserIDConflict: async (block:BlockDTO, conf:ConfDTO, index:SindexEntry) => {
const creates = indexer.iindexCreate(index);
checkIdentitiesUserIDConflict: async (block:BlockDTO, conf:ConfDTO, index:IndexEntry[]) => {
const creates = Indexer.iindexCreate(index);
const uids = _.chain(creates).pluck('uid').uniq().value();
if (creates.length !== uids.length) {
throw Error('Block must not contain twice same identity uid');
......@@ -113,8 +112,8 @@ export const FUNCTIONS = {
return true;
},
checkIdentitiesPubkeyConflict: async (block:BlockDTO, conf:ConfDTO, index:SindexEntry) => {
const creates = indexer.iindexCreate(index);
checkIdentitiesPubkeyConflict: async (block:BlockDTO, conf:ConfDTO, index:IndexEntry[]) => {
const creates = Indexer.iindexCreate(index);
const pubkeys = _.chain(creates).pluck('pub').uniq().value();
if (creates.length !== pubkeys.length) {
throw Error('Block must not contain twice same identity pubkey');
......@@ -122,9 +121,9 @@ export const FUNCTIONS = {
return true;
},
checkIdentitiesMatchJoin: async (block:BlockDTO, conf:ConfDTO, index:SindexEntry) => {
const icreates = indexer.iindexCreate(index);
const mcreates = indexer.mindexCreate(index);
checkIdentitiesMatchJoin: async (block:BlockDTO, conf:ConfDTO, index:IndexEntry[]) => {
const icreates = Indexer.iindexCreate(index);
const mcreates = Indexer.mindexCreate(index);
for (const icreate of icreates) {
const matching = _(mcreates).filter({ pub: icreate.pub });
if (matching.length == 0) {
......@@ -134,9 +133,9 @@ export const FUNCTIONS = {
return true;
},
checkRevokedAreExcluded: async (block:BlockDTO, conf:ConfDTO, index:SindexEntry) => {
const iindex = indexer.iindex(index);
const mindex = indexer.mindex(index);
checkRevokedAreExcluded: async (block:BlockDTO, conf:ConfDTO, index:IndexEntry[]) => {
const iindex = Indexer.iindex(index);
const mindex = Indexer.mindex(index);
const revocations = _.chain(mindex)
.filter((row:MindexEntry) => row.op == constants.IDX_UPDATE && row.revoked_on !== null)
.pluck('pub')
......@@ -150,17 +149,17 @@ export const FUNCTIONS = {
return true;
},
checkRevokedUnicity: async (block:BlockDTO, conf:ConfDTO, index:SindexEntry) => {
checkRevokedUnicity: async (block:BlockDTO, conf:ConfDTO, index:IndexEntry[]) => {
try {
await FUNCTIONS.checkMembershipUnicity(block, conf, index);
await LOCAL_RULES_FUNCTIONS.checkMembershipUnicity(block, conf, index);
} catch (e) {
throw Error('A single revocation per member is allowed');
}
return true;
},
checkMembershipUnicity: async (block:BlockDTO, conf:ConfDTO, index:SindexEntry) => {
const mindex = indexer.mindex(index);
checkMembershipUnicity: async (block:BlockDTO, conf:ConfDTO, index:IndexEntry[]) => {
const mindex = Indexer.mindex(index);
const pubkeys = _.chain(mindex).pluck('pub').uniq().value();
if (pubkeys.length !== mindex.length) {
throw Error('Unicity constraint PUBLIC_KEY on MINDEX is not respected');
......@@ -239,9 +238,9 @@ export const FUNCTIONS = {
return true;
},
checkCertificationOneByIssuer: async (block:BlockDTO, conf:ConfDTO, index:SindexEntry) => {
checkCertificationOneByIssuer: async (block:BlockDTO, conf:ConfDTO, index:IndexEntry[]) => {
if (block.number > 0) {
const cindex = indexer.cindex(index);
const cindex = Indexer.cindex(index);
const certFromA = _.uniq(cindex.map((row:CindexEntry) => row.issuer));
if (certFromA.length !== cindex.length) {
throw Error('Block cannot contain two certifications from same issuer');
......@@ -250,8 +249,8 @@ export const FUNCTIONS = {
return true;
},
checkCertificationUnicity: async (block:BlockDTO, conf:ConfDTO, index:SindexEntry) => {
const cindex = indexer.cindex(index);
checkCertificationUnicity: async (block:BlockDTO, conf:ConfDTO, index:IndexEntry[]) => {
const cindex = Indexer.cindex(index);
const certAtoB = _.uniq(cindex.map((row:CindexEntry) => row.issuer + row.receiver));
if (certAtoB.length !== cindex.length) {
throw Error('Block cannot contain identical certifications (A -> B)');
......@@ -259,10 +258,10 @@ export const FUNCTIONS = {
return true;
},
checkCertificationIsntForLeaverOrExcluded: async (block:BlockDTO, conf:ConfDTO, index:SindexEntry) => {
const cindex = indexer.cindex(index);
const iindex = indexer.iindex(index);
const mindex = indexer.mindex(index);
checkCertificationIsntForLeaverOrExcluded: async (block:BlockDTO, conf:ConfDTO, index:IndexEntry[]) => {
const cindex = Indexer.cindex(index);
const iindex = Indexer.iindex(index);
const mindex = Indexer.mindex(index);
const certified = cindex.map((row:CindexEntry) => row.receiver);
for (const pub of certified) {
const exclusions = _(iindex).where({ op: constants.IDX_UPDATE, member: false, pub: pub });
......@@ -332,7 +331,7 @@ export const FUNCTIONS = {
throw Error('A transaction must have at least 1 source');
}
}
const sindex = indexer.localSIndex(dto);
const sindex = Indexer.localSIndex(dto);
const inputs = _.filter(sindex, (row:SindexEntry) => row.op == constants.IDX_UPDATE).map((row:SindexEntry) => [row.op, row.identifier, row.pos].join('-'));
if (inputs.length !== _.uniq(inputs).length) {
throw Error('It cannot exist 2 identical sources for transactions inside a given block');
......@@ -346,7 +345,7 @@ export const FUNCTIONS = {