Skip to content

Commit

Permalink
Merge pull request #518 from Chia-Network/feature/assert-chia-network
Browse files Browse the repository at this point in the history
feat: assert that chia network matches cw config file
  • Loading branch information
MichaelTaylor3D authored May 6, 2022
2 parents 5ac3af1 + 73152b3 commit 319e0de
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 51 deletions.
8 changes: 0 additions & 8 deletions src/controllers/governance.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { Governance } from '../models';
import {
assertIsActiveGovernanceBody,
assertIfReadOnlyMode,
assertDataLayerAvailable,
assertWalletIsAvailable,
assertWalletIsSynced,
assertCanBeGovernanceBody,
} from '../utils/data-assertions';
Expand Down Expand Up @@ -53,8 +51,6 @@ export const findPickList = async (req, res) => {
export const createGoveranceBody = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertWalletIsAvailable();
await assertWalletIsSynced();
await assertCanBeGovernanceBody();

Expand All @@ -76,8 +72,6 @@ export const createGoveranceBody = async (req, res) => {
export const setDefaultOrgList = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertWalletIsAvailable();
await assertWalletIsSynced();
await assertIsActiveGovernanceBody();

Expand All @@ -103,8 +97,6 @@ export const setDefaultOrgList = async (req, res) => {
export const setPickList = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertWalletIsAvailable();
await assertWalletIsSynced();
await assertIsActiveGovernanceBody();

Expand Down
16 changes: 0 additions & 16 deletions src/controllers/organization.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { Organization } from '../models/organizations';
import {
assertHomeOrgExists,
assertWalletIsSynced,
assertWalletIsAvailable,
assertDataLayerAvailable,
assertIfReadOnlyMode,
assertCanDeleteOrg,
} from '../utils/data-assertions';
Expand All @@ -20,8 +18,6 @@ export const findAll = async (req, res) => {
export const createV2 = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertWalletIsAvailable();
await assertWalletIsSynced();

const myOrganization = await Organization.getHomeOrg();
Expand Down Expand Up @@ -57,8 +53,6 @@ export const createV2 = async (req, res) => {
export const create = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertWalletIsAvailable();
await assertWalletIsSynced();

const myOrganization = await Organization.getHomeOrg();
Expand Down Expand Up @@ -86,8 +80,6 @@ export const create = async (req, res) => {
export const resetHomeOrg = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertWalletIsAvailable();
await assertWalletIsSynced();

await Promise.all([
Expand All @@ -113,8 +105,6 @@ export const resetHomeOrg = async (req, res) => {
export const importOrg = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertWalletIsAvailable();
await assertWalletIsSynced();

const { orgUid, ip, port } = req.body;
Expand All @@ -137,8 +127,6 @@ export const importOrg = async (req, res) => {
export const subscribeToOrganization = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertWalletIsAvailable();
await assertWalletIsSynced();
await assertHomeOrgExists();

Expand All @@ -159,8 +147,6 @@ export const deleteImportedOrg = async (req, res) => {
let transaction;
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertWalletIsAvailable();
await assertWalletIsSynced();
await assertHomeOrgExists();
await assertCanDeleteOrg(req.body.orgUid);
Expand Down Expand Up @@ -198,8 +184,6 @@ export const unsubscribeToOrganization = async (req, res) => {
let transaction;
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertWalletIsAvailable();
await assertWalletIsSynced();
await assertHomeOrgExists();

Expand Down
9 changes: 0 additions & 9 deletions src/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
assertHomeOrgExists,
assertNoPendingCommits,
assertRecordExistance,
assertDataLayerAvailable,
assertIfReadOnlyMode,
} from '../utils/data-assertions';

Expand All @@ -38,7 +37,6 @@ import { formatModelAssociationName } from '../utils/model-utils.js';
export const create = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assertNoPendingCommits();

Expand Down Expand Up @@ -105,7 +103,6 @@ export const create = async (req, res) => {

export const findAll = async (req, res) => {
try {
await assertDataLayerAvailable();
let { page, limit, search, orgUid, columns, xls } = req.query;
let where = orgUid != null && orgUid !== 'all' ? { orgUid } : undefined;

Expand Down Expand Up @@ -190,8 +187,6 @@ export const findAll = async (req, res) => {

export const findOne = async (req, res) => {
try {
await assertDataLayerAvailable();

const query = {
where: { warehouseProjectId: req.query.warehouseProjectId },
include: Project.getAssociatedModels().map(
Expand All @@ -211,7 +206,6 @@ export const findOne = async (req, res) => {
export const updateFromXLS = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assertNoPendingCommits();

Expand Down Expand Up @@ -242,7 +236,6 @@ export const updateFromXLS = async (req, res) => {
export const update = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assertNoPendingCommits();

Expand Down Expand Up @@ -327,7 +320,6 @@ export const update = async (req, res) => {
export const destroy = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assertNoPendingCommits();

Expand Down Expand Up @@ -359,7 +351,6 @@ export const destroy = async (req, res) => {
export const batchUpload = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assertNoPendingCommits();

Expand Down
4 changes: 0 additions & 4 deletions src/controllers/staging.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import {
assertHomeOrgExists,
assertNoPendingCommits,
assertWalletIsSynced,
assertWalletIsAvailable,
assertDataLayerAvailable,
assertIfReadOnlyMode,
assertStagingTableNotEmpty,
} from '../utils/data-assertions';
Expand Down Expand Up @@ -78,8 +76,6 @@ export const commit = async (req, res) => {
await assertIfReadOnlyMode();
await assertStagingTableNotEmpty();
await assertHomeOrgExists();
await assertDataLayerAvailable();
await assertWalletIsAvailable();
await assertWalletIsSynced();
await assertNoPendingCommits();

Expand Down
10 changes: 0 additions & 10 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
assertHomeOrgExists,
assertNoPendingCommits,
assertRecordExistance,
assertDataLayerAvailable,
assertIfReadOnlyMode,
} from '../utils/data-assertions';

Expand All @@ -39,7 +38,6 @@ import { formatModelAssociationName } from '../utils/model-utils.js';
export const create = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertNoPendingCommits();
await assertHomeOrgExists();

Expand Down Expand Up @@ -112,8 +110,6 @@ export const create = async (req, res) => {

export const findAll = async (req, res) => {
try {
await assertDataLayerAvailable();

let { page, limit, columns, orgUid, search, xls } = req.query;
let where = orgUid != null && orgUid !== 'all' ? { orgUid } : undefined;

Expand Down Expand Up @@ -201,7 +197,6 @@ export const findAll = async (req, res) => {

export const findOne = async (req, res) => {
try {
await assertDataLayerAvailable();
res.json(
await Unit.findByPk(req.query.warehouseUnitId, {
include: Unit.getAssociatedModels().map((association) => {
Expand All @@ -227,7 +222,6 @@ export const findOne = async (req, res) => {
export const updateFromXLS = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assertNoPendingCommits();

Expand Down Expand Up @@ -255,7 +249,6 @@ export const updateFromXLS = async (req, res) => {
export const update = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assertNoPendingCommits();

Expand Down Expand Up @@ -341,7 +334,6 @@ export const update = async (req, res) => {
export const destroy = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assertNoPendingCommits();

Expand Down Expand Up @@ -372,7 +364,6 @@ export const destroy = async (req, res) => {
export const split = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assertNoPendingCommits();

Expand Down Expand Up @@ -454,7 +445,6 @@ export const split = async (req, res) => {
export const batchUpload = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assertNoPendingCommits();

Expand Down
48 changes: 48 additions & 0 deletions src/datalayer/fullNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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 = getConfig().APP.WALLET_URL;

const getBaseOptions = () => {
const homeDir = os.homedir();
const certFile = path.resolve(
`${homeDir}/.chia/mainnet/config/ssl/full_node/private_full_node.crt`,
);
const keyFile = path.resolve(
`${homeDir}/.chia/mainnet/config/ssl/full_node/private_full_node.key`,
);

const baseOptions = {
method: 'POST',
cert: fs.readFileSync(certFile),
key: fs.readFileSync(keyFile),
};

return baseOptions;
};

const getActiveNetwork = async () => {
const options = {
url: `${rpcUrl}/get_network_info`,
body: JSON.stringify({}),
};

const response = await request(Object.assign({}, getBaseOptions(), options));

const data = JSON.parse(response);

if (data.success) {
return data;
}

return false;
};

export default {
getActiveNetwork,
};
2 changes: 2 additions & 0 deletions src/datalayer/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import writeService from './writeService';
import syncService from './syncService';
import wallet from './wallet';
import fullNode from './fullNode';

export default {
...writeService,
...syncService,
...wallet,
...fullNode,
};
2 changes: 0 additions & 2 deletions src/models/projects/projects.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,6 @@ class Project extends Model {
isUpdateComment,
);

console.log('#############', commentChangeList);

return {
projects: [
..._.get(insertChangeList, 'project', []),
Expand Down
20 changes: 20 additions & 0 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import { V1Router } from './v1';
import { sequelize } from '../database';
import { getConfig } from '../utils/config-loader';
import { logger } from '../config/logger.cjs';
import {
assertChiaNetworkMatchInConfiguration,
assertDataLayerAvailable,
assertWalletIsAvailable,
} from '../utils/data-assertions';

const { API_KEY, READ_ONLY } = getConfig().APP;
const app = express();
Expand All @@ -21,6 +26,21 @@ app.use(express.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(fileUpload());

// Common assertions on every endpoint
app.use(async function (req, res, next) {
try {
await assertChiaNetworkMatchInConfiguration();
await assertDataLayerAvailable();
await assertWalletIsAvailable();
next();
} catch (err) {
res.status(400).json({
message: 'Chia Exception',
error: err.message,
});
}
});

// Add optional API key if set in .env file
app.use(function (req, res, next) {
if (API_KEY && API_KEY !== '') {
Expand Down
16 changes: 15 additions & 1 deletion src/utils/data-assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,21 @@ import datalayer from '../datalayer';
import { formatModelAssociationName } from './model-utils.js';
import { getConfig } from '../utils/config-loader';

const { IS_GOVERNANCE_BODY, READ_ONLY, USE_SIMULATOR } = getConfig().APP;
const { IS_GOVERNANCE_BODY, READ_ONLY, USE_SIMULATOR, CHIA_NETWORK } =
getConfig().APP;

export const assertChiaNetworkMatchInConfiguration = async () => {
if (!USE_SIMULATOR) {
const networkInfo = await datalayer.getActiveNetwork();
const network = _.get(networkInfo, 'network_name', '');

if (!network.includes(CHIA_NETWORK)) {
throw new Error(
`Your node is on ${network} but your climate warehouse is set to ${CHIA_NETWORK}, please change your config so they match`,
);
}
}
};

export const assertCanBeGovernanceBody = async () => {
if (IS_GOVERNANCE_BODY !== 'true') {
Expand Down
1 change: 0 additions & 1 deletion src/utils/datalayer-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export const decodeHex = (str) => {
};

export const decodeDataLayerResponse = (data) => {
console.log('$$$$$$$$$$$$$$$$$', data);
return data.keys_values.map((item) => ({
key: decodeHex(item.key),
value: decodeHex(item.value),
Expand Down
Loading

0 comments on commit 319e0de

Please sign in to comment.