Skip to content

Commit

Permalink
Fixed JSON imports and Plots generation in simulation tool
Browse files Browse the repository at this point in the history
  • Loading branch information
u-hubar committed Jan 16, 2024
1 parent a75f950 commit 72fdcc3
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions tools/knowledge-assets-distribution-simulation/simulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import D3Node from 'd3-node';
import * as d3 from 'd3';
import sharp from 'sharp';
import { readFile } from 'fs/promises';
import { createRequire } from 'module';
import { create as createLibP2PKey, createFromPrivKey } from 'peer-id';
import BlockchainModuleManagerMock from './mocks/blockchain-module-manager-mock.js';
import HashingService from '../../src/service/hashing-service.js';
import ProximityScoringService from '../../src/service/proximity-scoring-service.js';
import Logger from '../../src/logger/logger.js';
import configjson from '../../config/config.json';
import pjson from '../../package.json';

const require = createRequire(import.meta.url);
const configjson = require('../../config/config.json');
const pjson = require('../../package.json');

function getConfig() {
let config;
Expand Down Expand Up @@ -73,15 +76,20 @@ function generateRandomHashes(numberOfHashes) {
return numberOfHashes === 1 ? hashes[0] : hashes;
}

async function generateRandomNodes(numberOfNodes, hashFunctionId = 1) {
async function generateRandomNodes(
numberOfNodes,
stakeMin = 50000,
stakeMax = 1000000,
hashFunctionId = 1,
) {
const nodes = [];

for (let i = 0; i < numberOfNodes; i += 1) {
const libp2pPrivKey = (await createLibP2PKey({ bits: 1024, keyType: 'RSA' })).toJSON()
.privKey;
const nodeId = (await createFromPrivKey(libp2pPrivKey)).toB58String();
const sha256 = await hashingService.callHashFunction(hashFunctionId, nodeId);
const stake = String(generateRandomNumber(50000, 5000000));
const stake = String(generateRandomNumber(stakeMin, stakeMax));

nodes.push({ nodeId, sha256, stake });
}
Expand All @@ -101,6 +109,7 @@ async function readJsonFromFile(filePath) {

function convertSvgToJpg(svgString, outputImageName) {
sharp(Buffer.from(svgString))
.flatten({ background: '#FFFFFF' })
.jpeg({ quality: 100 })
.toBuffer()
.then((buffer) => {
Expand Down Expand Up @@ -242,7 +251,7 @@ function generateScatterPlot(data, metric, outputImageName) {
.style('text-anchor', 'middle')
.text(`Knowledge Assets ${metric}`);

svg.append('g').attr('transform', `translate(0, ${height})`).call(d3.axisBottom(x));
svg.append('g').attr('transform', `translate(0, ${height})`).call(d3.axisBottom(x).ticks(15));

svg.append('g').call(d3.axisLeft(y).ticks(30));

Expand Down

0 comments on commit 72fdcc3

Please sign in to comment.