Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
[EV-693] [EV-736] Added simple username indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
antouhou committed Apr 12, 2018
1 parent 58763e1 commit 8d696e5
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ Dockerfile
build_docker_image.sh
npm-debug.log
node_modules
/vmn/stack.log
/vmn/stack-db.json
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ node_modules
dapinet.orbitdb
lib/config/config.json
test/rpc
/vmn/stack.log
/vmn/stack-db.json
2 changes: 1 addition & 1 deletion lib/rpcServer/commands/getDapContract.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const DashDrive = require('@dashevo/dash-schema/vmn');
const { DashDrive } = require('@dashevo/dash-schema/vmn');

// TODO: temporary dash drive from VMN
const dashDrive = new DashDrive();
Expand Down
3 changes: 0 additions & 3 deletions lib/rpcServer/commands/getUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ const insight = require('../../api/insight');
const getUser = async (args, callback) => {
try {
const usernameOrRegTxId = args[0];
if (!validate.username(usernameOrRegTxId).valid) {
return callback({ code: 400, message: 'Username is not valid' });
}
const user = await insight.getUser(usernameOrRegTxId);
return callback(null, user);
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion lib/rpcServer/commands/getUserDapContext.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const DashDrive = require('@dashevo/dash-schema/vmn');
const { DashDrive } = require('@dashevo/dash-schema/vmn');

// TODO: temporary dash drive from VMN
const dashDrive = new DashDrive();
Expand Down
2 changes: 1 addition & 1 deletion lib/rpcServer/commands/getUserDapSpace.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const DashDrive = require('@dashevo/dash-schema/vmn');
const { DashDrive } = require('@dashevo/dash-schema/vmn');

// TODO: temporary dash drive from VMN
const dashDrive = new DashDrive();
Expand Down
2 changes: 1 addition & 1 deletion lib/rpcServer/commands/searchDapContracts.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const DashDrive = require('@dashevo/dash-schema/vmn');
const { DashDrive } = require('@dashevo/dash-schema/vmn');

// TODO: temporary dash drive from VMN
const dashDrive = new DashDrive();
Expand Down
77 changes: 77 additions & 0 deletions lib/rpcServer/commands/searchUsers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const dashcore = require('../../api/dashcore').rpc;
const { Registration } = require('bitcore-lib-dash').Transaction.SubscriptionTransactions;
const union = require('lodash/union');

let usernameCache = [];
let lastSeenBlock = 1;
const maxLimit = 25;

async function indexUsernames() {
console.warn('USERNAME INDEXER IS JUST FOR DEVELOPMENT PURPOSES, UNTIL PROPER DASHCORE RPC METHOD PROVIDED');
console.log('Start username index');
let nextBlockExists = true;
let nextBlockHash = null;
while (nextBlockExists) {
const blockHash = nextBlockHash || await dashcore.getBlockHash(lastSeenBlock);
const block = await dashcore.getBlock(blockHash);
if (block) {
lastSeenBlock = block.height;
console.log(`Processing block ${block.height}`);
nextBlockHash = block.nextblockhash || null;
nextBlockExists = !!block.nextblockhash;
const transactionHashes = block.tx;
const transactions = await Promise
.all(transactionHashes.map(transactionHash => dashcore.getTransaction(transactionHash)));
const usernamesInBlock = transactions
.map(rawTransaction => new Registration(rawTransaction.hex))
.filter((regSubTx) => {
try {
const regData = regSubTx.getRegistrationData();
return !!regData.username;
} catch (e) {
return false;
}
})
.map(regSubTx => regSubTx.getRegistrationData().username);
usernameCache = union(usernameCache, usernamesInBlock);
if (usernamesInBlock.length) {
console.log(`${usernamesInBlock.length} usernames found: ${usernamesInBlock.join(', ')}`);
} else {
// console.log('No regTx in block found');
}
} else {
nextBlockExists = false;
}
}
}

/**
* Returns user
* @param args
* @param callback
*/
const searchUsers = async (args, callback) => {
try {
const pattern = args[0] || args.pattern;
let limit = args[1] || args.limit;
let offset = args[2] || args.offest;
if (typeof limit !== 'number' || limit > 25 || limit < 0) {
limit = maxLimit;
}
if (typeof offset !== 'number' || offset < 0) {
offset = 0;
}
// TODO: This is workaround until we have proper index in dashcore
await indexUsernames();
console.log(usernameCache);
const usernames = usernameCache.filter(username => !!username.match(pattern));
return callback(null, {
totalCount: usernames.length,
results: usernames.slice(offset, offset + limit),
});
} catch (e) {
return callback({ code: 400, message: e.message });
}
};

module.exports = searchUsers;
2 changes: 2 additions & 0 deletions lib/rpcServer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const getUserDapSpace = require('./commands/getUserDapSpace');
const getUserDapContext = require('./commands/getUserDapContext');
const getDapContract = require('./commands/getDapContract');
const searchDapContracts = require('./commands/searchDapContracts');
const searchUsers = require('./commands/searchUsers');

const commands = {
estimateFee,
Expand Down Expand Up @@ -63,6 +64,7 @@ const commands = {
getUserDapContext,
searchDapContracts,
getDapContract,
searchUsers,
};

const regtestOnlyCommands = {
Expand Down
Empty file added vmn/.keep
Empty file.
1 change: 1 addition & 0 deletions vmn/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This directory is temporary VMN's DashDrive storage location. Needs to be removed when actual DashDrive will be available.

0 comments on commit 8d696e5

Please sign in to comment.