diff --git a/.eslintignore b/.eslintignore index f208f45df1239a957a319274cb070ed7c3f1e7fd..c9abbdbe3fa17718b198f4592fa057a852f7ae32 100644 --- a/.eslintignore +++ b/.eslintignore @@ -5,5 +5,6 @@ app/lib/db/*.js app/lib/dto/*.js app/lib/indexer.js app/lib/common.js +app/lib/dal/drivers/*.js test/blockchain/*.js test/blockchain/lib/*.js \ No newline at end of file diff --git a/.gitignore b/.gitignore index 304462b9e97a24e302a04c323e942e4111fbfa4b..19d6ac634cfd6f45ca53a7430ed824df44444f15 100644 --- a/.gitignore +++ b/.gitignore @@ -41,4 +41,5 @@ app/lib/computation/*.js.map app/lib/common.js* app/lib/db/*.js* app/lib/dto/*.js* -app/lib/indexer.js* \ No newline at end of file +app/lib/indexer.js* +app/lib/dal/drivers/*.js* \ No newline at end of file diff --git a/app/lib/dal/drivers/SQLiteDriver.ts b/app/lib/dal/drivers/SQLiteDriver.ts new file mode 100644 index 0000000000000000000000000000000000000000..e2af704fa145086173d042b1a1b27ba14224d2c5 --- /dev/null +++ b/app/lib/dal/drivers/SQLiteDriver.ts @@ -0,0 +1,100 @@ +const qfs = require('q-io/fs') +const sqlite3 = require("sqlite3").verbose() + +const MEMORY_PATH = ':memory:' + +export class SQLiteDriver { + + private logger:any + private dbPromise: Promise<any> | null = null + + constructor( + private path:string + ) { + this.logger = require('../../logger')('driver') + } + + getDB(): Promise<any> { + if (!this.dbPromise) { + this.dbPromise = (async () => { + this.logger.debug('Opening SQLite database "%s"...', this.path) + let sqlite = new sqlite3.Database(this.path) + await new Promise<any>((resolve) => sqlite.once('open', resolve)) + // Database is opened + + // Force case sensitiveness on LIKE operator + const sql = 'PRAGMA case_sensitive_like=ON' + await new Promise<any>((resolve, reject) => sqlite.exec(sql, (err:any) => { + if (err) return reject(Error('SQL error "' + err.message + '" on INIT queries "' + sql + '"')) + return resolve() + })) + + // Database is ready + return sqlite + })() + } + return this.dbPromise + } + + async executeAll(sql:string, params:any[]): Promise<any[]> { + const db = await this.getDB() + return new Promise<any>((resolve, reject) => db.all(sql, params, (err:any, rows:any[]) => { + if (err) { + return reject(Error('SQL error "' + err.message + '" on query "' + sql + '"')) + } else { + return resolve(rows) + } + })) + } + + async executeSql(sql:string): Promise<void> { + const db = await this.getDB() + return new Promise<void>((resolve, reject) => db.exec(sql, (err:any) => { + if (err) { + return reject(Error('SQL error "' + err.message + '" on query "' + sql + '"')) + } else { + return resolve() + } + })) + } + + async destroyDatabase(): Promise<void> { + this.logger.debug('Removing SQLite database...') + await this.closeConnection() + if (this.path !== MEMORY_PATH) { + await qfs.remove(this.path) + } + this.logger.debug('Database removed') + } + + async closeConnection(): Promise<void> { + if (!this.dbPromise) { + return + } + const db = await this.getDB() + if (process.platform === 'win32') { + db.open // For an unknown reason, we need this line. + } + await new Promise((resolve, reject) => { + this.logger.debug('Trying to close SQLite...') + db.on('close', () => { + this.logger.info('Database closed.') + this.dbPromise = null + resolve() + }) + db.on('error', (err:any) => { + if (err && err.message === 'SQLITE_MISUSE: Database is closed') { + this.dbPromise = null + return resolve() + } + reject(err) + }) + try { + db.close() + } catch (e) { + this.logger.error(e) + throw e + } + }) + } +} diff --git a/app/lib/dal/drivers/sqlite.js b/app/lib/dal/drivers/sqlite.js deleted file mode 100644 index 0e19b0be4ae5cb5d54327cb518db9fa730349eb9..0000000000000000000000000000000000000000 --- a/app/lib/dal/drivers/sqlite.js +++ /dev/null @@ -1,100 +0,0 @@ -"use strict"; - -const co = require('co'); -const qfs = require('q-io/fs'); -const sqlite3 = require("sqlite3").verbose(); - -module.exports = function NewSqliteDriver(path) { - return new SQLiteDriver(path); -}; - -const MEMORY_PATH = ':memory:'; - -function SQLiteDriver(path) { - - const logger = require('../../logger')('driver'); - - const that = this; - let dbPromise = null; - - function getDB() { - return dbPromise || (dbPromise = co(function*() { - logger.debug('Opening SQLite database "%s"...', path); - let sqlite = new sqlite3.Database(path); - yield new Promise((resolve) => sqlite.once('open', resolve)); - // Database is opened - - // Force case sensitiveness on LIKE operator - const sql = 'PRAGMA case_sensitive_like=ON;' - yield new Promise((resolve, reject) => sqlite.exec(sql, (err) => { - if (err) return reject(Error('SQL error "' + err.message + '" on INIT queries "' + sql + '"')) - return resolve() - })); - - // Database is ready - return sqlite; - })); - } - - this.executeAll = (sql, params) => co(function*() { - const db = yield getDB(); - return new Promise((resolve, reject) => db.all(sql, params, (err, rows) => { - if (err) { - return reject(Error('SQL error "' + err.message + '" on query "' + sql + '"')); - } else { - return resolve(rows); - } - })); - }); - - this.executeSql = (sql) => co(function*() { - const db = yield getDB(); - return new Promise((resolve, reject) => db.exec(sql, (err) => { - if (err) { - return reject(Error('SQL error "' + err.message + '" on query "' + sql + '"')); - } else { - return resolve(); - } - })); - }); - - this.destroyDatabase = () => co(function*() { - logger.debug('Removing SQLite database...'); - yield that.closeConnection(); - if (path !== MEMORY_PATH) { - yield qfs.remove(path); - } - logger.debug('Database removed'); - }); - - this.closeConnection = () => co(function*() { - if (!dbPromise) { - return; - } - const db = yield getDB(); - if (process.platform === 'win32') { - db.open; // For an unknown reason, we need this line. - } - yield new Promise((resolve, reject) => { - logger.debug('Trying to close SQLite...'); - db.on('close', () => { - logger.info('Database closed.'); - dbPromise = null; - resolve(); - }); - db.on('error', (err) => { - if (err && err.message === 'SQLITE_MISUSE: Database is closed') { - dbPromise = null; - return resolve(); - } - reject(err); - }); - try { - db.close(); - } catch (e) { - logger.error(e); - throw e; - } - }); - }); -} diff --git a/app/lib/system/directory.js b/app/lib/system/directory.js index 931dcd296c6b2274d651d8709a345d90747ca0ad..f2fde3484e4537c60a6c8b9e4b65a8eeb356f2cc 100644 --- a/app/lib/system/directory.js +++ b/app/lib/system/directory.js @@ -7,7 +7,7 @@ const cfs = require('../cfs'); const Q = require('q'); const qfs = require('q-io/fs'); const fs = require('fs'); -const driver = require("../dal/drivers/sqlite"); +const SQLiteDriver = require("../dal/drivers/SQLiteDriver").SQLiteDriver const DEFAULT_DOMAIN = "duniter_default"; const DEFAULT_HOME = (process.platform == 'win32' ? process.env.USERPROFILE : process.env.HOME) + '/.config/duniter/'; @@ -50,11 +50,11 @@ const dir = module.exports = { const home = params.home; yield someDelayFix(); if (isMemory) { - params.dbf = () => driver(':memory:'); + params.dbf = () => new SQLiteDriver(':memory:'); params.wotb = require('../wot').memoryInstance(); } else { const sqlitePath = path.join(home, dir.DUNITER_DB_NAME + '.db'); - params.dbf = () => driver(sqlitePath); + params.dbf = () => new SQLiteDriver(sqlitePath); const wotbFilePath = path.join(home, dir.WOTB_FILE); let existsFile = yield qfs.exists(wotbFilePath); if (!existsFile) { diff --git a/test/blockchain/basic-blockchain.js b/test/blockchain/basic-blockchain.js deleted file mode 100644 index e330cb64fd6db937d28a52a66deff187c63460e4..0000000000000000000000000000000000000000 --- a/test/blockchain/basic-blockchain.js +++ /dev/null @@ -1,145 +0,0 @@ -"use strict"; -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 BasicBlockchain_1 = require("../../app/lib/blockchain/BasicBlockchain"); -const ArrayBlockchain_1 = require("./lib/ArrayBlockchain"); -const SqlBlockchain_1 = require("../../app/lib/blockchain/SqlBlockchain"); -const assert = require('assert'); -const BIndexDAL = require('../../app/lib/dal/sqliteDAL/index/BIndexDAL'); -const MetaDAL = require('../../app/lib/dal/sqliteDAL/MetaDAL'); -const sqlite = require('../../app/lib/dal/drivers/sqlite'); -let blockchain, emptyBlockchain; -describe('Basic Memory Blockchain', () => { - before(() => { - blockchain = new BasicBlockchain_1.BasicBlockchain(new ArrayBlockchain_1.ArrayBlockchain()); - emptyBlockchain = new BasicBlockchain_1.BasicBlockchain(new ArrayBlockchain_1.ArrayBlockchain()); - }); - it('should be able to push 3 blocks and read them', () => __awaiter(this, void 0, void 0, function* () { - yield blockchain.pushBlock({ name: 'A' }); - yield blockchain.pushBlock({ name: 'B' }); - yield blockchain.pushBlock({ name: 'C' }); - const HEAD0 = yield blockchain.head(); - const HEAD1 = yield blockchain.head(1); - const HEAD2 = yield blockchain.head(2); - const BLOCK0 = yield blockchain.getBlock(0); - const BLOCK1 = yield blockchain.getBlock(1); - const BLOCK2 = yield blockchain.getBlock(2); - assert.equal(HEAD0.name, 'C'); - assert.equal(HEAD1.name, 'B'); - assert.equal(HEAD2.name, 'A'); - assert.deepEqual(HEAD2, BLOCK0); - assert.deepEqual(HEAD1, BLOCK1); - assert.deepEqual(HEAD0, BLOCK2); - })); - it('should be able to read a range', () => __awaiter(this, void 0, void 0, function* () { - const range1 = yield blockchain.headRange(2); - assert.equal(range1.length, 2); - assert.equal(range1[0].name, 'C'); - assert.equal(range1[1].name, 'B'); - const range2 = yield blockchain.headRange(6); - assert.equal(range2.length, 3); - assert.equal(range2[0].name, 'C'); - assert.equal(range2[1].name, 'B'); - assert.equal(range2[2].name, 'A'); - })); - it('should have a good height', () => __awaiter(this, void 0, void 0, function* () { - const height1 = yield blockchain.height(); - yield blockchain.pushBlock({ name: 'D' }); - const height2 = yield blockchain.height(); - const height3 = yield emptyBlockchain.height(); - assert.equal(height1, 3); - assert.equal(height2, 4); - assert.equal(height3, 0); - })); - it('should be able to revert blocks', () => __awaiter(this, void 0, void 0, function* () { - const reverted = yield blockchain.revertHead(); - const height2 = yield blockchain.height(); - assert.equal(height2, 3); - assert.equal(reverted.name, 'D'); - })); -}); -describe('Basic SQL Blockchain', () => { - before(() => __awaiter(this, void 0, void 0, function* () { - { - const db = new sqlite(':memory:'); - const bindexDAL = new BIndexDAL(db); - const metaDAL = new MetaDAL(db); - yield bindexDAL.init(); - yield metaDAL.init(); - yield metaDAL.exec('CREATE TABLE txs (id INTEGER null);'); - yield metaDAL.exec('CREATE TABLE idty (id INTEGER null);'); - yield metaDAL.exec('CREATE TABLE cert (id INTEGER null);'); - yield metaDAL.exec('CREATE TABLE membership (id INTEGER null);'); - yield metaDAL.exec('CREATE TABLE block (fork INTEGER null);'); - yield metaDAL.upgradeDatabase({}); - const dal = { bindexDAL }; - blockchain = new BasicBlockchain_1.BasicBlockchain(new SqlBlockchain_1.SQLBlockchain(dal)); - } - { - const db = new sqlite(':memory:'); - const bindexDAL = new BIndexDAL(db); - const metaDAL = new MetaDAL(db); - yield bindexDAL.init(); - yield metaDAL.init(); - yield metaDAL.exec('CREATE TABLE txs (id INTEGER null);'); - yield metaDAL.exec('CREATE TABLE idty (id INTEGER null);'); - yield metaDAL.exec('CREATE TABLE cert (id INTEGER null);'); - yield metaDAL.exec('CREATE TABLE membership (id INTEGER null);'); - yield metaDAL.exec('CREATE TABLE block (fork INTEGER null);'); - yield metaDAL.upgradeDatabase({}); - const dal = { bindexDAL }; - emptyBlockchain = new BasicBlockchain_1.BasicBlockchain(new SqlBlockchain_1.SQLBlockchain(dal)); - } - })); - it('should be able to push 3 blocks and read them', () => __awaiter(this, void 0, void 0, function* () { - yield blockchain.pushBlock({ number: 0, version: 1, bsize: 0, hash: 'H', issuer: 'I', time: 1, membersCount: 1, issuersCount: 1, issuersFrame: 1, issuersFrameVar: 1, avgBlockSize: 0, medianTime: 1, dividend: 10, mass: 100, unitBase: 0, powMin: 0, udTime: 0, udReevalTime: 0, diffNumber: 1, speed: 1, massReeval: 1 }); - yield blockchain.pushBlock({ number: 1, version: 1, bsize: 0, hash: 'H', issuer: 'I', time: 1, membersCount: 1, issuersCount: 1, issuersFrame: 1, issuersFrameVar: 1, avgBlockSize: 0, medianTime: 1, dividend: 10, mass: 100, unitBase: 0, powMin: 0, udTime: 0, udReevalTime: 0, diffNumber: 1, speed: 1, massReeval: 1 }); - yield blockchain.pushBlock({ number: 2, version: 1, bsize: 0, hash: 'H', issuer: 'I', time: 1, membersCount: 1, issuersCount: 1, issuersFrame: 1, issuersFrameVar: 1, avgBlockSize: 0, medianTime: 1, dividend: 10, mass: 100, unitBase: 0, powMin: 0, udTime: 0, udReevalTime: 0, diffNumber: 1, speed: 1, massReeval: 1 }); - const HEAD0 = yield blockchain.head(); - const HEAD1 = yield blockchain.head(1); - const HEAD2 = yield blockchain.head(2); - const BLOCK0 = yield blockchain.getBlock(0); - const BLOCK1 = yield blockchain.getBlock(1); - const BLOCK2 = yield blockchain.getBlock(2); - assert.equal(HEAD0.number, 2); - assert.equal(HEAD1.number, 1); - assert.equal(HEAD2.number, 0); - assert.deepEqual(HEAD2, BLOCK0); - assert.deepEqual(HEAD1, BLOCK1); - assert.deepEqual(HEAD0, BLOCK2); - })); - it('should be able to read a range', () => __awaiter(this, void 0, void 0, function* () { - const range1 = yield blockchain.headRange(2); - assert.equal(range1.length, 2); - assert.equal(range1[0].number, 2); - assert.equal(range1[1].number, 1); - const range2 = yield blockchain.headRange(6); - assert.equal(range2.length, 3); - assert.equal(range2[0].number, 2); - assert.equal(range2[1].number, 1); - assert.equal(range2[2].number, 0); - })); - it('should have a good height', () => __awaiter(this, void 0, void 0, function* () { - const height1 = yield blockchain.height(); - yield blockchain.pushBlock({ number: 3, version: 1, bsize: 0, hash: 'H', issuer: 'I', time: 1, membersCount: 1, issuersCount: 1, issuersFrame: 1, issuersFrameVar: 1, avgBlockSize: 0, medianTime: 1, dividend: 10, mass: 100, unitBase: 0, powMin: 0, udTime: 0, udReevalTime: 0, diffNumber: 1, speed: 1, massReeval: 1 }); - const height2 = yield blockchain.height(); - const height3 = yield emptyBlockchain.height(); - assert.equal(height1, 3); - assert.equal(height2, 4); - assert.equal(height3, 0); - })); - it('should be able to revert blocks', () => __awaiter(this, void 0, void 0, function* () { - const reverted = yield blockchain.revertHead(); - const height2 = yield blockchain.height(); - assert.equal(height2, 3); - assert.equal(reverted.number, 3); - })); -}); -//# sourceMappingURL=basic-blockchain.js.map \ No newline at end of file diff --git a/test/blockchain/basic-blockchain.ts b/test/blockchain/basic-blockchain.ts index 9ee9845571a73a693eae787ea9877d8794bb160a..9869d3ee9b4b7f877eb081c989ff815821aff529 100644 --- a/test/blockchain/basic-blockchain.ts +++ b/test/blockchain/basic-blockchain.ts @@ -1,11 +1,11 @@ import {BasicBlockchain} from "../../app/lib/blockchain/BasicBlockchain" import {ArrayBlockchain} from "./lib/ArrayBlockchain" import {SQLBlockchain} from "../../app/lib/blockchain/SqlBlockchain" +import {SQLiteDriver} from "../../app/lib/dal/drivers/SQLiteDriver" const assert = require('assert') const BIndexDAL = require('../../app/lib/dal/sqliteDAL/index/BIndexDAL') const MetaDAL = require('../../app/lib/dal/sqliteDAL/MetaDAL') -const sqlite = require('../../app/lib/dal/drivers/sqlite') let blockchain:BasicBlockchain, emptyBlockchain:BasicBlockchain @@ -71,7 +71,7 @@ describe('Basic SQL Blockchain', () => { before(async () => { { - const db = new sqlite(':memory:') + const db = new SQLiteDriver(':memory:') const bindexDAL = new BIndexDAL(db) const metaDAL = new MetaDAL(db) @@ -90,7 +90,7 @@ describe('Basic SQL Blockchain', () => { blockchain = new BasicBlockchain(new SQLBlockchain(dal)) } { - const db = new sqlite(':memory:') + const db = new SQLiteDriver(':memory:') const bindexDAL = new BIndexDAL(db) const metaDAL = new MetaDAL(db) diff --git a/test/blockchain/indexed-blockchain.js b/test/blockchain/indexed-blockchain.js deleted file mode 100644 index d118df935621992a0cd283d580c93d7f5f2bd044..0000000000000000000000000000000000000000 --- a/test/blockchain/indexed-blockchain.js +++ /dev/null @@ -1,429 +0,0 @@ -"use strict"; -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 ArrayBlockchain_1 = require("./lib/ArrayBlockchain"); -const IndexedBlockchain_1 = require("../../app/lib/blockchain/IndexedBlockchain"); -const MemoryIndex_1 = require("./lib/MemoryIndex"); -const SqlIndex_1 = require("../../app/lib/blockchain/SqlIndex"); -const assert = require('assert'); -const sqlite = require('../../app/lib/dal/drivers/sqlite'); -describe('Indexed Blockchain', () => { - describe('MemoryIndex', () => { - let blockchain; - describe('PK on one field', () => { - before(() => { - blockchain = new IndexedBlockchain_1.IndexedBlockchain(new ArrayBlockchain_1.ArrayBlockchain(), new MemoryIndex_1.MemoryIndex(), 'writtenOn', { - iindex: { - pk: 'name', - remove: 'expired' - }, - zindex: { - pk: 'name' - } - }); - }); - it('should be able to index data', () => __awaiter(this, void 0, void 0, function* () { - yield blockchain.recordIndex({ - iindex: [ - { name: 'A', status: 'OK', writtenOn: 23000, events: 0, member: false }, - { name: 'A', status: 'OK', writtenOn: 23000, events: 4 }, - { name: 'A', status: 'OK', writtenOn: 23000, events: 5, member: true }, - { name: 'A', status: 'OK', writtenOn: 23601 }, - { name: 'A', status: 'OK', writtenOn: 23888 }, - { name: 'A', status: 'OK', writtenOn: 23889 }, - { name: 'B', status: 'OK', writtenOn: 23000, events: 1, member: false }, - { name: 'B', status: 'KO', writtenOn: 23000, events: null }, - { name: 'C', status: 'KO', writtenOn: 23500 }, - { name: 'D', status: 'KO', writtenOn: 23500 }, - { name: 'D', status: 'KO', writtenOn: 23521, expired: true } - ] - }); - })); - it('should be able to reduce data', () => __awaiter(this, void 0, void 0, function* () { - const reducedA = yield blockchain.indexReduce('iindex', { name: 'A' }); - const reducedB = yield blockchain.indexReduce('iindex', { name: 'B' }); - assert.deepEqual(reducedA, { name: 'A', status: 'OK', writtenOn: 23889, events: 5, member: true }); - assert.deepEqual(reducedB, { name: 'B', status: 'KO', writtenOn: 23000, events: 1, member: false }); - })); - it('should be able to count data', () => __awaiter(this, void 0, void 0, function* () { - const countAi = yield blockchain.indexCount('iindex', { name: 'A' }); - const countBi = yield blockchain.indexCount('iindex', { name: 'B' }); - const countCi = yield blockchain.indexCount('iindex', { name: 'C' }); - const countDi = yield blockchain.indexCount('iindex', { name: 'D' }); - const countBz = yield blockchain.indexCount('zindex', { name: 'B' }); - assert.equal(countAi, 6); - assert.equal(countBi, 2); - assert.equal(countCi, 1); - assert.equal(countDi, 2); - assert.equal(countBz, 0); - })); - it('should be able to reduce grouped data', () => __awaiter(this, void 0, void 0, function* () { - const reducedBy = yield blockchain.indexReduceGroupBy('iindex', { writtenOn: 23000 }, ['name']); - assert.deepEqual(reducedBy, [ - { name: 'A', status: 'OK', writtenOn: 23000, events: 5, member: true }, - { name: 'B', status: 'KO', writtenOn: 23000, events: 1, member: false } - ]); - })); - it('should be able to trim data', () => __awaiter(this, void 0, void 0, function* () { - // The number of records should decrease - yield blockchain.indexTrim(23601); - const countAi = yield blockchain.indexCount('iindex', { name: 'A' }); - const countBi = yield blockchain.indexCount('iindex', { name: 'B' }); - const countCi = yield blockchain.indexCount('iindex', { name: 'C' }); - const countDi = yield blockchain.indexCount('iindex', { name: 'D' }); - const countBz = yield blockchain.indexCount('zindex', { name: 'B' }); - assert.equal(countAi, 4); - assert.equal(countBi, 1); - assert.equal(countCi, 1); - assert.equal(countDi, 0); // Expired = remove rows on trim - assert.equal(countBz, 0); - const reducedAi = yield blockchain.indexReduce('iindex', { name: 'A' }); - const reducedBi = yield blockchain.indexReduce('iindex', { name: 'B' }); - const reducedCi = yield blockchain.indexReduce('iindex', { name: 'C' }); - const reducedDi = yield blockchain.indexReduce('iindex', { name: 'D' }); - const reducedBz = yield blockchain.indexReduce('zindex', { name: 'B' }); - assert.deepEqual(reducedAi, { name: 'A', status: 'OK', writtenOn: 23889, events: 5, member: true }); - assert.deepEqual(reducedBi, { name: 'B', status: 'KO', writtenOn: 23000, events: 1, member: false }); - assert.deepEqual(reducedCi, { name: 'C', status: 'KO', writtenOn: 23500 }); - assert.deepEqual(reducedDi, {}); - assert.deepEqual(reducedBz, {}); - })); - }); - describe('PK on two fields', () => { - before(() => { - blockchain = new IndexedBlockchain_1.IndexedBlockchain(new ArrayBlockchain_1.ArrayBlockchain(), new MemoryIndex_1.MemoryIndex(), 'writtenOn', { - iindex: { - pk: ['id', 'pos'], - remove: 'expired' - }, - zindex: { - pk: 'name' - } - }); - }); - it('should be able to index data', () => __awaiter(this, void 0, void 0, function* () { - yield blockchain.recordIndex({ - iindex: [ - { id: 'A', pos: 0, status: 'OK', writtenOn: 23000, events: 0, member: false }, - { id: 'A', pos: 0, status: 'OK', writtenOn: 23000, events: 4 }, - { id: 'A', pos: 0, status: 'OK', writtenOn: 23000, events: 5, member: true }, - { id: 'A', pos: 0, status: 'OK', writtenOn: 23601 }, - { id: 'A', pos: 1, status: 'OK', writtenOn: 23888 }, - { id: 'A', pos: 2, status: 'OK', writtenOn: 23889 }, - { id: 'B', pos: 0, status: 'OK', writtenOn: 23000, events: 1, member: false }, - { id: 'B', pos: 0, status: 'KO', writtenOn: 23000, events: null }, - { id: 'C', pos: 0, status: 'KO', writtenOn: 23500 }, - { id: 'D', pos: 0, status: 'KO', writtenOn: 23500 }, - { id: 'D', pos: 1, status: 'KO', writtenOn: 23521, expired: true } - ] - }); - })); - it('should be able to reduce data', () => __awaiter(this, void 0, void 0, function* () { - const reducedA = yield blockchain.indexReduce('iindex', { id: 'A', pos: 0 }); - const reducedB = yield blockchain.indexReduce('iindex', { id: 'B', pos: 0 }); - assert.deepEqual(reducedA, { id: 'A', pos: 0, status: 'OK', writtenOn: 23601, events: 5, member: true }); - assert.deepEqual(reducedB, { id: 'B', pos: 0, status: 'KO', writtenOn: 23000, events: 1, member: false }); - })); - it('should be able to count data', () => __awaiter(this, void 0, void 0, function* () { - const countAi = yield blockchain.indexCount('iindex', { id: 'A', pos: 0 }); - const countBi = yield blockchain.indexCount('iindex', { id: 'B', pos: 0 }); - const countCi = yield blockchain.indexCount('iindex', { id: 'C', pos: 0 }); - const countDi = yield blockchain.indexCount('iindex', { id: 'D', pos: 0 }); - const countBz = yield blockchain.indexCount('zindex', { id: 'B', pos: 0 }); - assert.equal(countAi, 4); - assert.equal(countBi, 2); - assert.equal(countCi, 1); - assert.equal(countDi, 1); - assert.equal(countBz, 0); - })); - it('should be able to reduce grouped data', () => __awaiter(this, void 0, void 0, function* () { - const reducedBy = yield blockchain.indexReduceGroupBy('iindex', { writtenOn: 23000 }, ['id', 'pos']); - assert.deepEqual(reducedBy, [ - { id: 'A', pos: 0, status: 'OK', writtenOn: 23000, events: 5, member: true }, - { id: 'B', pos: 0, status: 'KO', writtenOn: 23000, events: 1, member: false } - ]); - })); - it('should be able to trim data', () => __awaiter(this, void 0, void 0, function* () { - // The number of records should decrease - yield blockchain.indexTrim(23601); - const countAi = yield blockchain.indexCount('iindex', { id: 'A', pos: 0 }); - const countBi = yield blockchain.indexCount('iindex', { id: 'B', pos: 0 }); - const countCi = yield blockchain.indexCount('iindex', { id: 'C', pos: 0 }); - const countDi = yield blockchain.indexCount('iindex', { id: 'D', pos: 0 }); - const countBz = yield blockchain.indexCount('zindex', { id: 'B', pos: 0 }); - assert.equal(countAi, 2); - assert.equal(countBi, 1); - assert.equal(countCi, 1); - assert.equal(countDi, 1); // Not expired! - assert.equal(countBz, 0); - const reducedAi = yield blockchain.indexReduce('iindex', { id: 'A', pos: 0 }); - const reducedBi = yield blockchain.indexReduce('iindex', { id: 'B', pos: 0 }); - const reducedCi = yield blockchain.indexReduce('iindex', { id: 'C', pos: 0 }); - const reducedDi = yield blockchain.indexReduce('iindex', { id: 'D', pos: 0 }); - const reducedBz = yield blockchain.indexReduce('zindex', { id: 'B', pos: 0 }); - assert.deepEqual(reducedAi, { id: 'A', pos: 0, status: 'OK', writtenOn: 23601, events: 5, member: true }); - assert.deepEqual(reducedBi, { id: 'B', pos: 0, status: 'KO', writtenOn: 23000, events: 1, member: false }); - assert.deepEqual(reducedCi, { id: 'C', pos: 0, status: 'KO', writtenOn: 23500 }); - assert.deepEqual(reducedDi, { id: 'D', pos: 0, status: 'KO', writtenOn: 23500 }); - assert.deepEqual(reducedBz, {}); - })); - }); - }); - describe('SqlIndex', () => { - let blockchain; - describe('PK on one field', () => { - before(() => { - const db = new sqlite(':memory:'); - blockchain = new IndexedBlockchain_1.IndexedBlockchain(new ArrayBlockchain_1.ArrayBlockchain(), new SqlIndex_1.SQLIndex(db, { - iindex: { - sqlFields: [ - 'name CHAR(1) NULL', - 'status CHAR(2) NULL', - 'writtenOn INTEGER NULL', - 'events INTEGER NULL', - 'member INTEGER NULL', - 'expired INTEGER NULL' - ], - fields: [ - 'name', - 'status', - 'writtenOn', - 'events', - 'member', - 'expired' - ], - booleans: ['member', 'expired'] - }, - zindex: { - sqlFields: [ - 'name CHAR(1) NULL', - 'status CHAR(2) NULL', - 'writtenOn INTEGER NULL', - 'events INTEGER NULL', - 'member INTEGER NULL', - 'expired INTEGER NULL' - ], - fields: [ - 'name', - 'status', - 'writtenOn', - 'events', - 'member', - 'expired' - ], - booleans: ['member', 'expired'] - } - }), 'writtenOn', { - iindex: { - pk: 'name', - remove: 'expired' - }, - zindex: { - pk: 'name' - } - }); - }); - it('should be able to index data', () => __awaiter(this, void 0, void 0, function* () { - yield blockchain.recordIndex({ - iindex: [ - { name: 'A', status: 'OK', writtenOn: 23000, events: 0, member: false }, - { name: 'A', status: 'OK', writtenOn: 23000, events: 4 }, - { name: 'A', status: 'OK', writtenOn: 23000, events: 5, member: true }, - { name: 'A', status: 'OK', writtenOn: 23601 }, - { name: 'A', status: 'OK', writtenOn: 23888 }, - { name: 'A', status: 'OK', writtenOn: 23889 }, - { name: 'B', status: 'OK', writtenOn: 23000, events: 1, member: false }, - { name: 'B', status: 'KO', writtenOn: 23000, events: null }, - { name: 'C', status: 'KO', writtenOn: 23500 }, - { name: 'D', status: 'KO', writtenOn: 23500 }, - { name: 'D', status: 'KO', writtenOn: 23521, expired: true } - ] - }); - })); - it('should be able to reduce data', () => __awaiter(this, void 0, void 0, function* () { - const reducedA = yield blockchain.indexReduce('iindex', { name: 'A' }); - const reducedB = yield blockchain.indexReduce('iindex', { name: 'B' }); - assert.deepEqual(reducedA, { name: 'A', status: 'OK', writtenOn: 23889, events: 5, member: true }); - assert.deepEqual(reducedB, { name: 'B', status: 'KO', writtenOn: 23000, events: 1, member: false }); - })); - it('should be able to count data', () => __awaiter(this, void 0, void 0, function* () { - const countAi = yield blockchain.indexCount('iindex', { name: 'A' }); - const countBi = yield blockchain.indexCount('iindex', { name: 'B' }); - const countCi = yield blockchain.indexCount('iindex', { name: 'C' }); - const countDi = yield blockchain.indexCount('iindex', { name: 'D' }); - const countBz = yield blockchain.indexCount('zindex', { name: 'B' }); - assert.equal(countAi, 6); - assert.equal(countBi, 2); - assert.equal(countCi, 1); - assert.equal(countDi, 2); - assert.equal(countBz, 0); - })); - it('should be able to reduce grouped data', () => __awaiter(this, void 0, void 0, function* () { - const reducedBy = yield blockchain.indexReduceGroupBy('iindex', { writtenOn: 23000 }, ['name']); - assert.deepEqual(reducedBy, [ - { name: 'A', status: 'OK', writtenOn: 23000, events: 5, member: true }, - { name: 'B', status: 'KO', writtenOn: 23000, events: 1, member: false } - ]); - })); - it('should be able to trim data', () => __awaiter(this, void 0, void 0, function* () { - // The number of records should decrease - yield blockchain.indexTrim(23601); - const countAi = yield blockchain.indexCount('iindex', { name: 'A' }); - const countBi = yield blockchain.indexCount('iindex', { name: 'B' }); - const countCi = yield blockchain.indexCount('iindex', { name: 'C' }); - const countDi = yield blockchain.indexCount('iindex', { name: 'D' }); - const countBz = yield blockchain.indexCount('zindex', { name: 'B' }); - assert.equal(countAi, 4); - assert.equal(countBi, 1); - assert.equal(countCi, 1); - assert.equal(countDi, 0); // Expired = remove rows on trim - assert.equal(countBz, 0); - const reducedAi = yield blockchain.indexReduce('iindex', { name: 'A' }); - const reducedBi = yield blockchain.indexReduce('iindex', { name: 'B' }); - const reducedCi = yield blockchain.indexReduce('iindex', { name: 'C' }); - const reducedDi = yield blockchain.indexReduce('iindex', { name: 'D' }); - const reducedBz = yield blockchain.indexReduce('zindex', { name: 'B' }); - assert.deepEqual(reducedAi, { name: 'A', status: 'OK', writtenOn: 23889, events: 5, member: true }); - assert.deepEqual(reducedBi, { name: 'B', status: 'KO', writtenOn: 23000, events: 1, member: false }); - assert.deepEqual(reducedCi, { name: 'C', status: 'KO', writtenOn: 23500 }); - assert.deepEqual(reducedDi, {}); - assert.deepEqual(reducedBz, {}); - })); - }); - describe('PK on two fields', () => { - before(() => { - const db = new sqlite(':memory:'); - blockchain = new IndexedBlockchain_1.IndexedBlockchain(new ArrayBlockchain_1.ArrayBlockchain(), new SqlIndex_1.SQLIndex(db, { - iindex: { - sqlFields: [ - 'id INTEGER NULL', - 'pos INTEGER NULL', - 'name CHAR(1) NULL', - 'status CHAR(2) NULL', - 'writtenOn INTEGER NULL', - 'events INTEGER NULL', - 'member INTEGER NULL', - 'expired INTEGER NULL' - ], - fields: [ - 'id', - 'pos', - 'name', - 'status', - 'writtenOn', - 'events', - 'member', - 'expired' - ], - booleans: ['member', 'expired'] - }, - zindex: { - sqlFields: [ - 'id INTEGER NULL', - 'pos INTEGER NULL', - 'name CHAR(1) NULL', - 'status CHAR(2) NULL', - 'writtenOn INTEGER NULL', - 'events INTEGER NULL', - 'member INTEGER NULL', - 'expired INTEGER NULL' - ], - fields: [ - 'id', - 'pos', - 'name', - 'status', - 'writtenOn', - 'events', - 'member', - 'expired' - ], - booleans: ['member', 'expired'] - } - }), 'writtenOn', { - iindex: { - pk: ['id', 'pos'], - remove: 'expired' - }, - zindex: { - pk: 'name' - } - }); - }); - it('should be able to index data', () => __awaiter(this, void 0, void 0, function* () { - yield blockchain.recordIndex({ - iindex: [ - { id: 'A', pos: 0, status: 'OK', writtenOn: 23000, events: 0, member: false }, - { id: 'A', pos: 0, status: 'OK', writtenOn: 23000, events: 4 }, - { id: 'A', pos: 0, status: 'OK', writtenOn: 23000, events: 5, member: true }, - { id: 'A', pos: 0, status: 'OK', writtenOn: 23601 }, - { id: 'A', pos: 1, status: 'OK', writtenOn: 23888 }, - { id: 'A', pos: 2, status: 'OK', writtenOn: 23889 }, - { id: 'B', pos: 0, status: 'OK', writtenOn: 23000, events: 1, member: false }, - { id: 'B', pos: 0, status: 'KO', writtenOn: 23000, events: null }, - { id: 'C', pos: 0, status: 'KO', writtenOn: 23500 }, - { id: 'D', pos: 0, status: 'KO', writtenOn: 23500 }, - { id: 'D', pos: 1, status: 'KO', writtenOn: 23521, expired: true } - ] - }); - })); - it('should be able to reduce data', () => __awaiter(this, void 0, void 0, function* () { - const reducedA = yield blockchain.indexReduce('iindex', { id: 'A', pos: 0 }); - const reducedB = yield blockchain.indexReduce('iindex', { id: 'B', pos: 0 }); - assert.deepEqual(reducedA, { id: 'A', pos: 0, status: 'OK', writtenOn: 23601, events: 5, member: true }); - assert.deepEqual(reducedB, { id: 'B', pos: 0, status: 'KO', writtenOn: 23000, events: 1, member: false }); - })); - it('should be able to count data', () => __awaiter(this, void 0, void 0, function* () { - const countAi = yield blockchain.indexCount('iindex', { id: 'A', pos: 0 }); - const countBi = yield blockchain.indexCount('iindex', { id: 'B', pos: 0 }); - const countCi = yield blockchain.indexCount('iindex', { id: 'C', pos: 0 }); - const countDi = yield blockchain.indexCount('iindex', { id: 'D', pos: 0 }); - const countBz = yield blockchain.indexCount('zindex', { id: 'B', pos: 0 }); - assert.equal(countAi, 4); - assert.equal(countBi, 2); - assert.equal(countCi, 1); - assert.equal(countDi, 1); - assert.equal(countBz, 0); - })); - it('should be able to reduce grouped data', () => __awaiter(this, void 0, void 0, function* () { - const reducedBy = yield blockchain.indexReduceGroupBy('iindex', { writtenOn: 23000 }, ['id', 'pos']); - assert.deepEqual(reducedBy, [ - { id: 'A', pos: 0, status: 'OK', writtenOn: 23000, events: 5, member: true }, - { id: 'B', pos: 0, status: 'KO', writtenOn: 23000, events: 1, member: false } - ]); - })); - it('should be able to trim data', () => __awaiter(this, void 0, void 0, function* () { - // The number of records should decrease - yield blockchain.indexTrim(23601); - const countAi = yield blockchain.indexCount('iindex', { id: 'A', pos: 0 }); - const countBi = yield blockchain.indexCount('iindex', { id: 'B', pos: 0 }); - const countCi = yield blockchain.indexCount('iindex', { id: 'C', pos: 0 }); - const countDi = yield blockchain.indexCount('iindex', { id: 'D', pos: 0 }); - const countBz = yield blockchain.indexCount('zindex', { id: 'B', pos: 0 }); - assert.equal(countAi, 2); - assert.equal(countBi, 1); - assert.equal(countCi, 1); - assert.equal(countDi, 1); // Not expired! - assert.equal(countBz, 0); - const reducedAi = yield blockchain.indexReduce('iindex', { id: 'A', pos: 0 }); - const reducedBi = yield blockchain.indexReduce('iindex', { id: 'B', pos: 0 }); - const reducedCi = yield blockchain.indexReduce('iindex', { id: 'C', pos: 0 }); - const reducedDi = yield blockchain.indexReduce('iindex', { id: 'D', pos: 0 }); - const reducedBz = yield blockchain.indexReduce('zindex', { id: 'B', pos: 0 }); - assert.deepEqual(reducedAi, { id: 'A', pos: 0, status: 'OK', writtenOn: 23601, events: 5, member: true }); - assert.deepEqual(reducedBi, { id: 'B', pos: 0, status: 'KO', writtenOn: 23000, events: 1, member: false }); - assert.deepEqual(reducedCi, { id: 'C', pos: 0, status: 'KO', writtenOn: 23500 }); - assert.deepEqual(reducedDi, { id: 'D', pos: 0, status: 'KO', writtenOn: 23500 }); - assert.deepEqual(reducedBz, {}); - })); - }); - }); -}); -//# sourceMappingURL=indexed-blockchain.js.map \ No newline at end of file diff --git a/test/blockchain/indexed-blockchain.ts b/test/blockchain/indexed-blockchain.ts index 31edf7393a84fccd64d17cf8574bac43e657b007..5eed0353d6fd79e51fe6dc31adb016045f285e94 100644 --- a/test/blockchain/indexed-blockchain.ts +++ b/test/blockchain/indexed-blockchain.ts @@ -3,9 +3,9 @@ import {ArrayBlockchain} from "./lib/ArrayBlockchain" import {IndexedBlockchain} from "../../app/lib/blockchain/IndexedBlockchain" import {MemoryIndex} from "./lib/MemoryIndex" import {SQLIndex} from "../../app/lib/blockchain/SqlIndex" +import {SQLiteDriver} from "../../app/lib/dal/drivers/SQLiteDriver" const assert = require('assert') -const sqlite = require('../../app/lib/dal/drivers/sqlite') describe('Indexed Blockchain', () => { @@ -193,7 +193,7 @@ describe('Indexed Blockchain', () => { describe('PK on one field', () => { before(() => { - const db = new sqlite(':memory:') + const db = new SQLiteDriver(':memory:') blockchain = new IndexedBlockchain(new ArrayBlockchain(), new SQLIndex(db, { iindex: { sqlFields: [ @@ -319,7 +319,7 @@ describe('Indexed Blockchain', () => { describe('PK on two fields', () => { before(() => { - const db = new sqlite(':memory:') + const db = new SQLiteDriver(':memory:') blockchain = new IndexedBlockchain(new ArrayBlockchain(), new SQLIndex(db, { iindex: { sqlFields: [ diff --git a/test/blockchain/misc-sql-blockchain.js b/test/blockchain/misc-sql-blockchain.js deleted file mode 100644 index 4ea2a99c2ff6bf301e6eb2512ad83e1eecb7da6f..0000000000000000000000000000000000000000 --- a/test/blockchain/misc-sql-blockchain.js +++ /dev/null @@ -1,196 +0,0 @@ -"use strict"; -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 MiscIndexedBlockchain_1 = require("../../app/lib/blockchain/MiscIndexedBlockchain"); -const ArrayBlockchain_1 = require("./lib/ArrayBlockchain"); -const assert = require('assert'); -const MIndexDAL = require('../../app/lib/dal/sqliteDAL/index/MIndexDAL'); -const IIndexDAL = require('../../app/lib/dal/sqliteDAL/index/IIndexDAL'); -const SIndexDAL = require('../../app/lib/dal/sqliteDAL/index/SIndexDAL'); -const CIndexDAL = require('../../app/lib/dal/sqliteDAL/index/CIndexDAL'); -const MetaDAL = require('../../app/lib/dal/sqliteDAL/MetaDAL'); -const sqlite = require('../../app/lib/dal/drivers/sqlite'); -describe('MISC SQL Blockchain', () => { - let blockchain; - before(() => __awaiter(this, void 0, void 0, function* () { - const db = new sqlite(':memory:'); - const mindexDAL = new MIndexDAL(db); - const iindexDAL = new IIndexDAL(db); - const sindexDAL = new SIndexDAL(db); - const cindexDAL = new CIndexDAL(db); - const metaDAL = new MetaDAL(db); - yield mindexDAL.init(); - yield iindexDAL.init(); - yield sindexDAL.init(); - yield cindexDAL.init(); - yield metaDAL.init(); - // Ghost tables required for DB upgrade - yield metaDAL.exec('CREATE TABLE txs (id INTEGER null);'); - yield metaDAL.exec('CREATE TABLE idty (id INTEGER null);'); - yield metaDAL.exec('CREATE TABLE cert (id INTEGER null);'); - yield metaDAL.exec('CREATE TABLE membership (id INTEGER null);'); - yield metaDAL.exec('CREATE TABLE block (fork INTEGER null);'); - yield metaDAL.exec('CREATE TABLE b_index (id INTEGER null);'); - yield metaDAL.upgradeDatabase({}); - blockchain = new MiscIndexedBlockchain_1.MiscIndexedBlockchain(new ArrayBlockchain_1.ArrayBlockchain(), mindexDAL, iindexDAL, sindexDAL, cindexDAL); - })); - describe('MINDEX', () => { - it('should be able to index data', () => __awaiter(this, void 0, void 0, function* () { - yield blockchain.recordIndex({ - m_index: [ - { op: 'CREATE', pub: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', created_on: '0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855', written_on: '0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855', writtenOn: 0, expires_on: 1520544727, expired_on: null, revokes_on: 1552102327, revoked_on: null, leaving: false, revocation: null, chainable_on: null }, - { op: 'UPDATE', pub: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', created_on: '0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855', written_on: '3-0000611A1018A322624853A8AE10D0EBFF3C6AEE37BF4DE5354C720049C22BD1', writtenOn: 3, expires_on: 1520544728, expired_on: null, revokes_on: 1520544728, revoked_on: null, leaving: false, revocation: null, chainable_on: null }, - { op: 'UPDATE', pub: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', created_on: '0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855', written_on: '4-0000090B7059E7BF5DD2CCA5E4F0330C1DA42C5DCBD2D1364B99B3FF89DE6744', writtenOn: 4, expires_on: 1520544729, expired_on: null, revokes_on: 1520544729, revoked_on: null, leaving: false, revocation: null, chainable_on: null } - ] - }); - })); - it('should be able to reduce data', () => __awaiter(this, void 0, void 0, function* () { - const reducedG5 = yield blockchain.indexReduce('m_index', { pub: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT' }); - assert.deepEqual(reducedG5, { op: 'UPDATE', pub: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', created_on: '0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855', written_on: '4-0000090B7059E7BF5DD2CCA5E4F0330C1DA42C5DCBD2D1364B99B3FF89DE6744', writtenOn: 4, expires_on: 1520544729, revokes_on: 1520544729, leaving: false }); - })); - it('should be able to count data', () => __awaiter(this, void 0, void 0, function* () { - const countG5 = yield blockchain.indexCount('m_index', { pub: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT' }); - assert.equal(countG5, 3); - })); - it('should be able to reduce grouped data', () => __awaiter(this, void 0, void 0, function* () { - const reducedBy = yield blockchain.indexReduceGroupBy('m_index', { created_on: '0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855' }, ['op', 'pub']); - assert.deepEqual(reducedBy, [ - { op: 'CREATE', pub: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', created_on: '0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855', written_on: '0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855', writtenOn: 0, expires_on: 1520544727, revokes_on: 1552102327, leaving: false }, - { op: 'UPDATE', pub: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', created_on: '0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855', written_on: '4-0000090B7059E7BF5DD2CCA5E4F0330C1DA42C5DCBD2D1364B99B3FF89DE6744', writtenOn: 4, expires_on: 1520544729, revokes_on: 1520544729, leaving: false } - ]); - })); - it('should be able to trim data', () => __awaiter(this, void 0, void 0, function* () { - // The number of records should decrease - yield blockchain.indexTrim(4); - const countG5 = yield blockchain.indexCount('m_index', { pub: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT' }); - assert.equal(countG5, 2); - const reducedG5 = yield blockchain.indexReduce('m_index', { pub: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT' }); - assert.deepEqual(reducedG5, { op: 'UPDATE', pub: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', created_on: '0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855', written_on: '4-0000090B7059E7BF5DD2CCA5E4F0330C1DA42C5DCBD2D1364B99B3FF89DE6744', writtenOn: 4, expires_on: 1520544729, revokes_on: 1520544729, leaving: false }); - })); - }); - describe('IINDEX', () => { - it('should be able to index data', () => __awaiter(this, void 0, void 0, function* () { - yield blockchain.recordIndex({ - i_index: [ - { op: 'CREATE', uid: 'pseudo', pub: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', hash: '1505A45A5EEBFC3AFAD1475A4739C8447A79420A83340559CE5A49F9891167BB', sig: '2vfmih7xhW/QLJ85PZH1Tc6j5fooIXca+yr/esnt0yvdk5LhEKrOB32JFqCctAoRNwpRjBdZ2Q8l15+In1rrDg==', created_on: '0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855', written_on: '0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855', writtenOn: 0, member: true, wasMember: true, kick: false, wotb_id: 164 }, - { op: 'UPDATE', uid: 'pseudo', pub: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', hash: '1505A45A5EEBFC3AFAD1475A4739C8447A79420A83340559CE5A49F9891167BB', sig: '2vfmih7xhW/QLJ85PZH1Tc6j5fooIXca+yr/esnt0yvdk5LhEKrOB32JFqCctAoRNwpRjBdZ2Q8l15+In1rrDg==', created_on: '0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855', written_on: '33396-000009C936CD6F7C5672C1E6D36159E0BEA2B394F386CA0EBA7E73662A09BB43', writtenOn: 33396, member: false, wasMember: true, kick: false, wotb_id: 164 }, - { op: 'UPDATE', uid: 'pseudo', pub: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', hash: '1505A45A5EEBFC3AFAD1475A4739C8447A79420A83340559CE5A49F9891167BB', sig: '2vfmih7xhW/QLJ85PZH1Tc6j5fooIXca+yr/esnt0yvdk5LhEKrOB32JFqCctAoRNwpRjBdZ2Q8l15+In1rrDg==', created_on: '0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855', written_on: '40000-000006C311D2677D101116287718395A2CBB7B94389004D19B0E4AC6DCE2DE5F', writtenOn: 40000, member: true, wasMember: true, kick: false, wotb_id: 164 } - ] - }); - })); - it('should be able to reduce data', () => __awaiter(this, void 0, void 0, function* () { - const reducedG5 = yield blockchain.indexReduce('i_index', { pub: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT' }); - assert.deepEqual(reducedG5, { op: 'UPDATE', uid: 'pseudo', pub: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', hash: '1505A45A5EEBFC3AFAD1475A4739C8447A79420A83340559CE5A49F9891167BB', sig: '2vfmih7xhW/QLJ85PZH1Tc6j5fooIXca+yr/esnt0yvdk5LhEKrOB32JFqCctAoRNwpRjBdZ2Q8l15+In1rrDg==', created_on: '0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855', written_on: '40000-000006C311D2677D101116287718395A2CBB7B94389004D19B0E4AC6DCE2DE5F', writtenOn: 40000, member: true, wasMember: true, kick: false, wotb_id: 164 }); - })); - it('should be able to count data', () => __awaiter(this, void 0, void 0, function* () { - const countG5 = yield blockchain.indexCount('i_index', { pub: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT' }); - assert.equal(countG5, 3); - })); - it('should be able to reduce grouped data', () => __awaiter(this, void 0, void 0, function* () { - const reducedBy = yield blockchain.indexReduceGroupBy('i_index', { created_on: '0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855' }, ['op', 'pub']); - assert.deepEqual(reducedBy, [ - { op: 'CREATE', uid: 'pseudo', pub: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', hash: '1505A45A5EEBFC3AFAD1475A4739C8447A79420A83340559CE5A49F9891167BB', sig: '2vfmih7xhW/QLJ85PZH1Tc6j5fooIXca+yr/esnt0yvdk5LhEKrOB32JFqCctAoRNwpRjBdZ2Q8l15+In1rrDg==', created_on: '0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855', written_on: '0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855', writtenOn: 0, member: true, wasMember: true, kick: false, wotb_id: 164 }, - { op: 'UPDATE', uid: 'pseudo', pub: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', hash: '1505A45A5EEBFC3AFAD1475A4739C8447A79420A83340559CE5A49F9891167BB', sig: '2vfmih7xhW/QLJ85PZH1Tc6j5fooIXca+yr/esnt0yvdk5LhEKrOB32JFqCctAoRNwpRjBdZ2Q8l15+In1rrDg==', created_on: '0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855', written_on: '40000-000006C311D2677D101116287718395A2CBB7B94389004D19B0E4AC6DCE2DE5F', writtenOn: 40000, member: true, wasMember: true, kick: false, wotb_id: 164 } - ]); - })); - it('should be able to trim data', () => __awaiter(this, void 0, void 0, function* () { - // The number of records should decrease - yield blockchain.indexTrim(40000); - const countG5 = yield blockchain.indexCount('i_index', { pub: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT' }); - assert.equal(countG5, 2); - const reducedG5 = yield blockchain.indexReduce('i_index', { pub: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT' }); - assert.deepEqual(reducedG5, { op: 'UPDATE', uid: 'pseudo', pub: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', hash: '1505A45A5EEBFC3AFAD1475A4739C8447A79420A83340559CE5A49F9891167BB', sig: '2vfmih7xhW/QLJ85PZH1Tc6j5fooIXca+yr/esnt0yvdk5LhEKrOB32JFqCctAoRNwpRjBdZ2Q8l15+In1rrDg==', created_on: '0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855', written_on: '40000-000006C311D2677D101116287718395A2CBB7B94389004D19B0E4AC6DCE2DE5F', writtenOn: 40000, member: true, wasMember: true, kick: false, wotb_id: 164 }); - })); - }); - describe('SINDEX', () => { - it('should be able to index data', () => __awaiter(this, void 0, void 0, function* () { - yield blockchain.recordIndex({ - s_index: [ - // Dividend - { op: 'CREATE', tx: null, identifier: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', pos: 35820, created_on: null, written_on: '35820-00000B363BC8F761EB5343660592D50B872FE1140B350C9780EF5BC6F9DD000B', writtenOn: 35820, written_time: 1500000000, amount: 500, base: 0, locktime: 0, consumed: false, conditions: 'SIG(CPEaW4BGNaBdx6FbAxjNQ9Po2apnX2bDvBXJT9yaZUMc)' }, - { op: 'UPDATE', tx: null, identifier: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', pos: 35820, created_on: null, written_on: '35821-0000053C7B54AEFAEC4FCFB2763202ECD8382A635340BD622EDBC0CCC553F763', writtenOn: 35821, written_time: 1500000001, amount: 500, base: 0, locktime: 0, consumed: true, conditions: 'SIG(CPEaW4BGNaBdx6FbAxjNQ9Po2apnX2bDvBXJT9yaZUMc)' }, - // Transaction - { op: 'CREATE', tx: 'D01432C6D7D078CB566C08886FD92CA5D158433D7A8D971124973625BFAB78D9', identifier: 'D01432C6D7D078CB566C08886FD92CA5D158433D7A8D971124973625BFAB78D9', pos: 1, created_on: '33958-0000009CEC38916882EF46C40069EF227F1D7CB4B34EAD5298D84B6658FBB9FB', written_on: '30196-00000A8ABF13284452CD56C9DEC68124D4A31CE1BDD06819EB22E070EBDE1D2D', writtenOn: 30196, written_time: 1499000000, amount: 301, base: 0, locktime: 0, consumed: false, conditions: 'SIG(CPEaW4BGNaBdx6FbAxjNQ9Po2apnX2bDvBXJT9yaZUMc)' }, - { op: 'UPDATE', tx: '3926D234037264654D9C4A2D44CDDC18998DC48262F3677F23DA5BA81BD530EA', identifier: 'D01432C6D7D078CB566C08886FD92CA5D158433D7A8D971124973625BFAB78D9', pos: 1, created_on: '33958-0000009CEC38916882EF46C40069EF227F1D7CB4B34EAD5298D84B6658FBB9FB', written_on: '30197-0000009CEC38916882EF46C40069EF227F1D7CB4B34EAD5298D84B6658FBB9FB', writtenOn: 30197, written_time: 1499000001, amount: 301, base: 0, locktime: 0, consumed: true, conditions: 'SIG(CPEaW4BGNaBdx6FbAxjNQ9Po2apnX2bDvBXJT9yaZUMc)' } - ] - }); - })); - it('should be able to reduce data', () => __awaiter(this, void 0, void 0, function* () { - const reducedUD = yield blockchain.indexReduce('s_index', { identifier: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', pos: 35820 }); - const reducedTX = yield blockchain.indexReduce('s_index', { identifier: 'D01432C6D7D078CB566C08886FD92CA5D158433D7A8D971124973625BFAB78D9', pos: 1 }); - assert.deepEqual(reducedUD, { op: 'UPDATE', identifier: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', pos: 35820, written_on: '35821-0000053C7B54AEFAEC4FCFB2763202ECD8382A635340BD622EDBC0CCC553F763', writtenOn: 35821, written_time: 1500000001, amount: 500, base: 0, locktime: 0, consumed: true, conditions: 'SIG(CPEaW4BGNaBdx6FbAxjNQ9Po2apnX2bDvBXJT9yaZUMc)' }); - assert.deepEqual(reducedTX, { op: 'UPDATE', tx: '3926D234037264654D9C4A2D44CDDC18998DC48262F3677F23DA5BA81BD530EA', identifier: 'D01432C6D7D078CB566C08886FD92CA5D158433D7A8D971124973625BFAB78D9', pos: 1, created_on: '33958-0000009CEC38916882EF46C40069EF227F1D7CB4B34EAD5298D84B6658FBB9FB', written_on: '30197-0000009CEC38916882EF46C40069EF227F1D7CB4B34EAD5298D84B6658FBB9FB', writtenOn: 30197, written_time: 1499000001, amount: 301, base: 0, locktime: 0, consumed: true, conditions: 'SIG(CPEaW4BGNaBdx6FbAxjNQ9Po2apnX2bDvBXJT9yaZUMc)' }); - })); - it('should be able to count data', () => __awaiter(this, void 0, void 0, function* () { - const countUD = yield blockchain.indexCount('s_index', { identifier: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', pos: 35820 }); - const countTX = yield blockchain.indexCount('s_index', { identifier: 'D01432C6D7D078CB566C08886FD92CA5D158433D7A8D971124973625BFAB78D9', pos: 1 }); - assert.equal(countUD, 2); - assert.equal(countTX, 2); - })); - it('should be able to reduce grouped data', () => __awaiter(this, void 0, void 0, function* () { - const reducedBy = yield blockchain.indexReduceGroupBy('s_index', { pos: { $gt: 0 } }, ['identifier', 'pos']); - assert.deepEqual(reducedBy, [ - { op: 'UPDATE', tx: '3926D234037264654D9C4A2D44CDDC18998DC48262F3677F23DA5BA81BD530EA', identifier: 'D01432C6D7D078CB566C08886FD92CA5D158433D7A8D971124973625BFAB78D9', pos: 1, created_on: '33958-0000009CEC38916882EF46C40069EF227F1D7CB4B34EAD5298D84B6658FBB9FB', written_on: '30197-0000009CEC38916882EF46C40069EF227F1D7CB4B34EAD5298D84B6658FBB9FB', writtenOn: 30197, written_time: 1499000001, amount: 301, base: 0, locktime: 0, consumed: true, conditions: 'SIG(CPEaW4BGNaBdx6FbAxjNQ9Po2apnX2bDvBXJT9yaZUMc)' }, - { op: 'UPDATE', identifier: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', pos: 35820, written_on: '35821-0000053C7B54AEFAEC4FCFB2763202ECD8382A635340BD622EDBC0CCC553F763', writtenOn: 35821, written_time: 1500000001, amount: 500, base: 0, locktime: 0, consumed: true, conditions: 'SIG(CPEaW4BGNaBdx6FbAxjNQ9Po2apnX2bDvBXJT9yaZUMc)' } - ]); - })); - it('should be able to trim data', () => __awaiter(this, void 0, void 0, function* () { - // The number of records should decrease - yield blockchain.indexTrim(35000); - const countUD = yield blockchain.indexCount('s_index', { identifier: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', pos: 35820 }); - const countTX = yield blockchain.indexCount('s_index', { identifier: 'D01432C6D7D078CB566C08886FD92CA5D158433D7A8D971124973625BFAB78D9', pos: 1 }); - assert.equal(countUD, 2); - assert.equal(countTX, 0); // This index removes the lines marked `consumed` - const reducedUD = yield blockchain.indexReduce('s_index', { identifier: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', pos: 35820 }); - assert.deepEqual(reducedUD, { op: 'UPDATE', identifier: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', pos: 35820, written_on: '35821-0000053C7B54AEFAEC4FCFB2763202ECD8382A635340BD622EDBC0CCC553F763', writtenOn: 35821, written_time: 1500000001, amount: 500, base: 0, locktime: 0, consumed: true, conditions: 'SIG(CPEaW4BGNaBdx6FbAxjNQ9Po2apnX2bDvBXJT9yaZUMc)' }); - })); - }); - describe('CINDEX', () => { - it('should be able to index data', () => __awaiter(this, void 0, void 0, function* () { - yield blockchain.recordIndex({ - c_index: [ - { op: 'CREATE', issuer: 'D9D2zaJoWYWveii1JRYLVK3J4Z7ZH3QczoKrnQeiM6mx', receiver: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', created_on: 0, written_on: '0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855', writtenOn: 0, sig: 'MYWlBd2Hw3T/59BDz9HZECBuZ984C23F5lqUGluIUXsvXjsY4xKNqcN2x75s9rn++u4GEzZov6OznLZiHtbAAQ==', expires_on: 1552102327, expired_on: 0, chainable_on: 1489419127 }, - { op: 'UPDATE', issuer: 'D9D2zaJoWYWveii1JRYLVK3J4Z7ZH3QczoKrnQeiM6mx', receiver: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', created_on: 0, written_on: '9-0000092C94D0257C61A2504092440600487B2C8BEE73F9C8763C9F351543887D', writtenOn: 9, sig: 'MYWlBd2Hw3T/59BDz9HZECBuZ984C23F5lqUGluIUXsvXjsY4xKNqcN2x75s9rn++u4GEzZov6OznLZiHtbAAQ==', expires_on: 1552102327, expired_on: 1552102400, chainable_on: 1489419127 }, - { op: 'CREATE', issuer: 'EV4yZXAgmDd9rMsRCSH2MK7RHWty7CDB9tmHku3iRnEB', receiver: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', created_on: 11, written_on: '11-000019EC1161FC9FB1848A58A3137B9CD9A919E86B2394B9682BBC3FADB1AF1F', writtenOn: 11, sig: 'plFuA1vgXJh0CQ9MVCmOgfTfFb5u3qICMfgxVJEsyco+lmZTxaKuruSsRdhw3YZgJgfU6YwC+ta/RcgLF6DvDA==', expires_on: 1556866334, expired_on: 0, chainable_on: 1494184082 } - ] - }); - })); - it('should be able to reduce data', () => __awaiter(this, void 0, void 0, function* () { - const reducedC1 = yield blockchain.indexReduce('c_index', { issuer: 'D9D2zaJoWYWveii1JRYLVK3J4Z7ZH3QczoKrnQeiM6mx', receiver: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', created_on: 0 }); - const reducedC2 = yield blockchain.indexReduce('c_index', { issuer: 'EV4yZXAgmDd9rMsRCSH2MK7RHWty7CDB9tmHku3iRnEB', receiver: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', created_on: 11 }); - assert.deepEqual(reducedC1, { op: 'UPDATE', issuer: 'D9D2zaJoWYWveii1JRYLVK3J4Z7ZH3QczoKrnQeiM6mx', receiver: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', created_on: 0, written_on: '9-0000092C94D0257C61A2504092440600487B2C8BEE73F9C8763C9F351543887D', writtenOn: 9, sig: 'MYWlBd2Hw3T/59BDz9HZECBuZ984C23F5lqUGluIUXsvXjsY4xKNqcN2x75s9rn++u4GEzZov6OznLZiHtbAAQ==', expires_on: 1552102327, expired_on: 1552102400, chainable_on: 1489419127 }); - assert.deepEqual(reducedC2, { op: 'CREATE', issuer: 'EV4yZXAgmDd9rMsRCSH2MK7RHWty7CDB9tmHku3iRnEB', receiver: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', created_on: 11, written_on: '11-000019EC1161FC9FB1848A58A3137B9CD9A919E86B2394B9682BBC3FADB1AF1F', writtenOn: 11, sig: 'plFuA1vgXJh0CQ9MVCmOgfTfFb5u3qICMfgxVJEsyco+lmZTxaKuruSsRdhw3YZgJgfU6YwC+ta/RcgLF6DvDA==', expires_on: 1556866334, expired_on: 0, chainable_on: 1494184082 }); - })); - it('should be able to count data', () => __awaiter(this, void 0, void 0, function* () { - const countC1 = yield blockchain.indexCount('c_index', { issuer: 'D9D2zaJoWYWveii1JRYLVK3J4Z7ZH3QczoKrnQeiM6mx', receiver: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', created_on: 0 }); - const countC2 = yield blockchain.indexCount('c_index', { issuer: 'EV4yZXAgmDd9rMsRCSH2MK7RHWty7CDB9tmHku3iRnEB', receiver: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', created_on: 11 }); - assert.equal(countC1, 2); - assert.equal(countC2, 1); - })); - it('should be able to reduce grouped data', () => __awaiter(this, void 0, void 0, function* () { - const reducedBy = yield blockchain.indexReduceGroupBy('c_index', { created_on: { $gte: 0 } }, ['issuer', 'receiver', 'created_on']); - assert.deepEqual(reducedBy, [ - { op: 'UPDATE', issuer: 'D9D2zaJoWYWveii1JRYLVK3J4Z7ZH3QczoKrnQeiM6mx', receiver: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', created_on: 0, written_on: '9-0000092C94D0257C61A2504092440600487B2C8BEE73F9C8763C9F351543887D', writtenOn: 9, sig: 'MYWlBd2Hw3T/59BDz9HZECBuZ984C23F5lqUGluIUXsvXjsY4xKNqcN2x75s9rn++u4GEzZov6OznLZiHtbAAQ==', expires_on: 1552102327, expired_on: 1552102400, chainable_on: 1489419127 }, - { op: 'CREATE', issuer: 'EV4yZXAgmDd9rMsRCSH2MK7RHWty7CDB9tmHku3iRnEB', receiver: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', created_on: 11, written_on: '11-000019EC1161FC9FB1848A58A3137B9CD9A919E86B2394B9682BBC3FADB1AF1F', writtenOn: 11, sig: 'plFuA1vgXJh0CQ9MVCmOgfTfFb5u3qICMfgxVJEsyco+lmZTxaKuruSsRdhw3YZgJgfU6YwC+ta/RcgLF6DvDA==', expires_on: 1556866334, expired_on: 0, chainable_on: 1494184082 } - ]); - })); - it('should be able to trim data', () => __awaiter(this, void 0, void 0, function* () { - // The number of records should decrease - yield blockchain.indexTrim(10); - const countC1 = yield blockchain.indexCount('c_index', { issuer: 'D9D2zaJoWYWveii1JRYLVK3J4Z7ZH3QczoKrnQeiM6mx', receiver: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', created_on: 0 }); - const countC2 = yield blockchain.indexCount('c_index', { issuer: 'EV4yZXAgmDd9rMsRCSH2MK7RHWty7CDB9tmHku3iRnEB', receiver: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', created_on: 11 }); - assert.equal(countC1, 0); // This index removes the lines marked `expired_on` - assert.equal(countC2, 1); - const reducedC2 = yield blockchain.indexReduce('c_index', { issuer: 'EV4yZXAgmDd9rMsRCSH2MK7RHWty7CDB9tmHku3iRnEB', receiver: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', created_on: 11 }); - assert.deepEqual(reducedC2, { op: 'CREATE', issuer: 'EV4yZXAgmDd9rMsRCSH2MK7RHWty7CDB9tmHku3iRnEB', receiver: 'G5P7k5t764ybGfFGLnEAwwMDz6y2U4afagAmyJXgKFyT', created_on: 11, written_on: '11-000019EC1161FC9FB1848A58A3137B9CD9A919E86B2394B9682BBC3FADB1AF1F', writtenOn: 11, sig: 'plFuA1vgXJh0CQ9MVCmOgfTfFb5u3qICMfgxVJEsyco+lmZTxaKuruSsRdhw3YZgJgfU6YwC+ta/RcgLF6DvDA==', expires_on: 1556866334, expired_on: 0, chainable_on: 1494184082 }); - })); - }); -}); -//# sourceMappingURL=misc-sql-blockchain.js.map \ No newline at end of file diff --git a/test/blockchain/misc-sql-blockchain.ts b/test/blockchain/misc-sql-blockchain.ts index dc7004991c4267dc8f3bcf0c89a212f22e38a8f2..1b2988ff285dd7b7ee13b0308f154b6cc7bb16c3 100644 --- a/test/blockchain/misc-sql-blockchain.ts +++ b/test/blockchain/misc-sql-blockchain.ts @@ -1,6 +1,7 @@ "use strict"; import {MiscIndexedBlockchain} from "../../app/lib/blockchain/MiscIndexedBlockchain" import {ArrayBlockchain} from "./lib/ArrayBlockchain" +import {SQLiteDriver} from "../../app/lib/dal/drivers/SQLiteDriver" const assert = require('assert') const MIndexDAL = require('../../app/lib/dal/sqliteDAL/index/MIndexDAL') @@ -8,7 +9,6 @@ const IIndexDAL = require('../../app/lib/dal/sqliteDAL/index/IIndexDAL') const SIndexDAL = require('../../app/lib/dal/sqliteDAL/index/SIndexDAL') const CIndexDAL = require('../../app/lib/dal/sqliteDAL/index/CIndexDAL') const MetaDAL = require('../../app/lib/dal/sqliteDAL/MetaDAL') -const sqlite = require('../../app/lib/dal/drivers/sqlite') describe('MISC SQL Blockchain', () => { @@ -16,7 +16,7 @@ describe('MISC SQL Blockchain', () => { before(async () => { - const db = new sqlite(':memory:') + const db = new SQLiteDriver(':memory:') const mindexDAL = new MIndexDAL(db) const iindexDAL = new IIndexDAL(db) diff --git a/test/fast/database.js b/test/fast/database.js index eaaef4919e352c9322cf50c46fe27a4e523133de..c562700d2ae2854ef9dde823d84897240d1ee91e 100644 --- a/test/fast/database.js +++ b/test/fast/database.js @@ -3,7 +3,7 @@ const co = require('co'); const tmp = require('tmp'); const should = require('should'); -const sqlite = require('../../app/lib/dal/drivers/sqlite'); +const SQLiteDriver = require('../../app/lib/dal/drivers/SQLiteDriver').SQLiteDriver const MEMORY = ':memory:'; const FILE = tmp.fileSync().name + '.db'; // We add an suffix to avoid Windows-locking of the file by the `tmp` module @@ -29,7 +29,7 @@ describe("SQLite driver", function() { let rows; it('should be openable and closable on will', () => co(function*() { - const driver = sqlite(MEMORY); + const driver = new SQLiteDriver(MEMORY) yield driver.executeSql(CREATE_TABLE_SQL); rows = yield driver.executeAll(SELECT_FROM_TABLE, []); rows.should.have.length(0); @@ -64,7 +64,7 @@ describe("SQLite driver", function() { describe("File", function() { - const driver = sqlite(FILE); + const driver = new SQLiteDriver(FILE); let rows; it('should be able to open a new one', () => co(function*() {