Skip to content

Commit

Permalink
Merge pull request #487 from Chia-Network/develop
Browse files Browse the repository at this point in the history
Hydrate main with develop
  • Loading branch information
MichaelTaylor3D authored Apr 22, 2022
2 parents 25be908 + ee38e88 commit 89491cb
Show file tree
Hide file tree
Showing 30 changed files with 2,469 additions and 2,028 deletions.
17 changes: 0 additions & 17 deletions .env.copy

This file was deleted.

1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ FROM node:16
WORKDIR /usr/src/app
RUN npm install -g json
COPY package*.json ./
COPY .env.copy ./.env
RUN json -I -f package.json -e "this.type=\"commonjs\""
RUN npm set-script prepare ""
RUN npm set-script requirements-check ""
Expand Down
3,998 changes: 2,096 additions & 1,902 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "climate-warehouse",
"version": "0.0.26",
"version": "0.0.27",
"private": true,
"bin": "build/server.js",
"type": "module",
Expand All @@ -19,7 +19,7 @@
"prepare": "husky install",
"build": "babel src --keep-file-extension --out-dir build --copy-files",
"build-migrations": "babel migrations --keep-file-extension --out-dir dist/migrations --copy-files",
"prepare-binary": "rm -rf dist && mkdir dist && cp .env.copy dist/.env",
"prepare-binary": "rm -rf dist && mkdir dist",
"create-win-x64-dist": "npm run build && npm run prepare-binary && pkg package.json -t node16-win-x64 --out-path dist",
"create-mac-x64-dist": "npm run build && npm run prepare-binary && pkg package.json -t node16-macos-x64 --out-path dist",
"create-linux-x64-dist": "npm run build && npm run prepare-binary && pkg package.json -t node16-linux-x64 --out-path dist"
Expand All @@ -37,6 +37,7 @@
"express-fileupload": "^1.2.1",
"express-joi-validation": "^5.0.0",
"joi": "^17.5.0",
"js-yaml": "^4.1.0",
"lodash": "^4.17.21",
"log-update": "^4.0.0",
"mysql2": "^2.3.3",
Expand Down
49 changes: 45 additions & 4 deletions src/config/config.cjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,51 @@
require('dotenv').config();

const _ = require('lodash');
const yaml = require('js-yaml');
const fs = require('fs');
const os = require('os');
const path = require('path');
const homeDir = os.homedir();
const defaultConfig = require('../utils/defaultConfig.json');

const persistanceFolder = `${homeDir}/.chia/climate-warehouse`;

// Adding this duplicate function here because im having trouble
// importing it in from utils folder
const getConfig = _.memoize(() => {
const configFile = path.resolve(
`${homeDir}/.chia/climate-warehouse/config.yaml`,
);

// First write it to chia home
if (!fs.existsSync(configFile)) {
try {
fs.writeFileSync(configFile, yaml.dump(defaultConfig), 'utf8');
} catch (err) {
// if it still doesnt exist that means we are in an env without write permissions
// so just load the default en
if (typeof process.env.USE_SIMULATOR === 'string') {
defaultConfig.APP.USE_SIMULATOR = process.env.USE_SIMULATOR === 'true';
}

console.log('Cant write config file, falling back to defaults');
return yaml.load(yaml.dump(defaultConfig));
}
}

try {
const yml = yaml.load(fs.readFileSync(configFile, 'utf8'));

if (typeof process.env.USE_SIMULATOR === 'string') {
yml.APP.USE_SIMULATOR = process.env.USE_SIMULATOR === 'true';
}

return yml;
} catch (e) {
console.log(e, `Config file not found at ${configFile}`);
}
});

module.exports = {
local: {
dialect: 'sqlite',
Expand All @@ -27,10 +68,10 @@ module.exports = {
logging: false,
},
mirror: {
username: process.env.DB_USERNAME || '',
password: process.env.DB_PASSWORD || '',
database: process.env.DB_NAME || '',
host: process.env.DB_HOST || '',
username: getConfig().MIRROR_DB.DB_USERNAME || '',
password: getConfig().MIRROR_DB.DB_PASSWORD || '',
database: getConfig().MIRROR_DB.DB_NAME || '',
host: getConfig().MIRROR_DB.DB_HOST || '',
dialect: 'mysql',
logging: false,
},
Expand Down
28 changes: 28 additions & 0 deletions src/controllers/governance.controller.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import _ from 'lodash';

import { Governance } from '../models';

import {
Expand All @@ -21,6 +23,32 @@ export const findAll = async (req, res) => {
}
};

export const findOrgList = async (req, res) => {
try {
const results = await Governance.findOne({ where: { metaKey: 'orgList' } });
return res.json(JSON.parse(_.get(results, 'metaValue', {})));
} catch (error) {
res.status(400).json({
message: 'Can not retreive Governance Data',
error: error.message,
});
}
};

export const findPickList = async (req, res) => {
try {
const results = await Governance.findOne({
where: { metaKey: 'pickList' },
});
return res.json(JSON.parse(_.get(results, 'metaValue', {})));
} catch (error) {
res.status(400).json({
message: 'Can not retreive Governance Data',
error: error.message,
});
}
};

// eslint-disable-next-line
export const createGoveranceBody = async (req, res) => {
try {
Expand Down
20 changes: 20 additions & 0 deletions src/controllers/organization.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,26 @@ export const create = async (req, res) => {
}
};

export const resetHomeOrg = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertWalletIsAvailable();
await assertWalletIsSynced();

await Organization.destroy({ where: { isHome: true } });

res.json({
message: 'Your home organization was reset, please create a new one.',
});
} catch (error) {
res.status(400).json({
message: 'Error resetting your organization',
error: error.message,
});
}
};

// eslint-disable-next-line
export const importOrg = async (req, res) => {
try {
Expand Down
6 changes: 5 additions & 1 deletion src/controllers/staging.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {

export const findAll = async (req, res) => {
try {
let { page, limit, type } = req.query;
let { page, limit, type, table } = req.query;

let pagination = paginationParams(page, limit);

Expand All @@ -33,6 +33,10 @@ export const findAll = async (req, res) => {
where = { failedCommit: true };
}

if (table) {
where.table = table;
}

let stagingData = await Staging.findAndCountAll({
distinct: true,
where,
Expand Down
18 changes: 11 additions & 7 deletions src/database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Debug from 'debug';
Debug.enable('climate-warehouse:mirror-database');
const log = Debug('climate-warehouse:mirror-database');
import mysql from 'mysql2/promise';
import { getConfig } from '../utils/config-loader';

import { migrations } from './migrations';
import { seeders } from './seeders';
Expand All @@ -26,7 +27,10 @@ export const safeMirrorDbHandler = (callback) => {
callback();
})
.catch(() => {
if (process.env.DB_HOST && process.env.DB_HOST !== '') {
if (
getConfig().MIRROR_DB.DB_HOST &&
getConfig().MIRROR_DB.DB_HOST !== ''
) {
log('Mirror DB not connected');
}
});
Expand Down Expand Up @@ -99,18 +103,18 @@ export const prepareDb = async () => {

if (
mirrorConfig == 'mirror' &&
process.env.DB_HOST &&
process.env.DB_HOST !== ''
getConfig().MIRROR_DB.DB_HOST &&
getConfig().MIRROR_DB.DB_HOST !== ''
) {
const connection = await mysql.createConnection({
host: process.env.DB_HOST,
host: getConfig().MIRROR_DB.DB_HOST,
port: 3306,
user: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
user: getConfig().MIRROR_DB.DB_USERNAME,
password: getConfig().MIRROR_DB.DB_PASSWORD,
});

await connection.query(
`CREATE DATABASE IF NOT EXISTS \`${process.env.DB_NAME}\`;`,
`CREATE DATABASE IF NOT EXISTS \`${getConfig().MIRROR_DB.DB_NAME}\`;`,
);

const db = new Sequelize(config[mirrorConfig]);
Expand Down
5 changes: 2 additions & 3 deletions src/datalayer/persistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fs from 'fs';
import path from 'path';
import request from 'request-promise';
import os from 'os';
import { getConfig } from '../utils/config-loader';

import Debug from 'debug';
Debug.enable('climate-warehouse:datalayer:persistance');
Expand All @@ -12,7 +13,7 @@ const log = Debug('climate-warehouse:datalayer:persistance');

process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;

const rpcUrl = process.env.DATALAYER_URL;
const rpcUrl = getConfig().APP.DATALAYER_URL;

const getBaseOptions = () => {
const homeDir = os.homedir();
Expand Down Expand Up @@ -197,8 +198,6 @@ export const dataLayerAvailable = async () => {
log(data);
return false;
} catch (error) {
log(error);

return false;
}
};
Expand Down
17 changes: 10 additions & 7 deletions src/datalayer/syncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import _ from 'lodash';

import { decodeHex, decodeDataLayerResponse } from '../utils/datalayer-utils';
import { Organization, Staging, ModelKeys } from '../models';
import { getConfig } from '../utils/config-loader';

import * as dataLayer from './persistance';
import * as simulator from './simulator';
Expand All @@ -10,6 +11,8 @@ import Debug from 'debug';
Debug.enable('climate-warehouse:datalayer:syncService');
const log = Debug('climate-warehouse:datalayer:syncService');

const { USE_SIMULATOR } = getConfig().APP;

const POLLING_INTERVAL = 5000;
const frames = ['-', '\\', '|', '/'];

Expand All @@ -36,7 +39,7 @@ const startDataLayerUpdatePolling = async () => {
const syncDataLayerStoreToClimateWarehouse = async (storeId, rootHash) => {
let storeData;

if (process.env.USE_SIMULATOR === 'true') {
if (USE_SIMULATOR) {
storeData = await simulator.getStoreData(storeId, rootHash);
} else {
storeData = await dataLayer.getStoreData(storeId, rootHash);
Expand Down Expand Up @@ -119,7 +122,7 @@ const dataLayerWasUpdated = async () => {
}

let rootResponse;
if (process.env.USE_SIMULATOR === 'true') {
if (USE_SIMULATOR) {
rootResponse = await simulator.getRoots(subscribedOrgIds);
} else {
rootResponse = await dataLayer.getRoots(subscribedOrgIds);
Expand Down Expand Up @@ -160,7 +163,7 @@ const dataLayerWasUpdated = async () => {
};

const subscribeToStoreOnDataLayer = async (storeId, ip, port) => {
if (process.env.USE_SIMULATOR === 'true') {
if (USE_SIMULATOR) {
return simulator.subscribeToStoreOnDataLayer(storeId, ip, port);
} else {
return dataLayer.subscribeToStoreOnDataLayer(storeId, ip, port);
Expand Down Expand Up @@ -192,7 +195,7 @@ const getSubscribedStoreData = async (
}
}

if (process.env.USE_SIMULATOR !== 'true') {
if (!USE_SIMULATOR) {
const storeExistAndIsConfirmed = await dataLayer.getRoot(storeId, true);
if (!storeExistAndIsConfirmed) {
log(`Retrying...`, retry + 1);
Expand All @@ -205,7 +208,7 @@ const getSubscribedStoreData = async (
}

let encodedData;
if (process.env.USE_SIMULATOR === 'true') {
if (USE_SIMULATOR) {
encodedData = await simulator.getStoreData(storeId);
} else {
encodedData = await dataLayer.getStoreData(storeId);
Expand All @@ -231,13 +234,13 @@ const getSubscribedStoreData = async (
};

const getRootHistory = (storeId) => {
if (process.env.USE_SIMULATOR !== 'true') {
if (!USE_SIMULATOR) {
return dataLayer.getRootHistory(storeId);
}
};

const getRootDiff = (storeId, root1, root2) => {
if (process.env.USE_SIMULATOR !== 'true') {
if (!USE_SIMULATOR) {
return dataLayer.getRootDiff(storeId, root1, root2);
}
};
Expand Down
5 changes: 3 additions & 2 deletions src/datalayer/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import fs from 'fs';
import path from 'path';
import request from 'request-promise';
import os from 'os';
import { getConfig } from '../utils/config-loader';

process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;

const rpcUrl = process.env.WALLET_URL;
const rpcUrl = getConfig().APP.WALLET_URL;

const getBaseOptions = () => {
const homeDir = os.homedir();
Expand Down Expand Up @@ -81,7 +82,7 @@ const hasUnconfirmedTransactions = async () => {
};

const getPublicAddress = async () => {
if (process.env.USE_SIMULATOR === 'true') {
if (getConfig().APP.USE_SIMULATOR) {
return Promise.resolve('xch33300ddsje98f33hkkdf9dfuSIMULATED_ADDRESS');
}

Expand Down
Loading

0 comments on commit 89491cb

Please sign in to comment.