From 37e3216a88dbee1b5124c0702f3d5015a3f07288 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Mon, 21 Feb 2022 18:06:16 -0500 Subject: [PATCH] feat: assert wallet is synced --- src/controllers/organization.controller.js | 17 ++++++++++++++-- src/controllers/project.controller.js | 12 +++++------ src/controllers/staging.controller.js | 8 ++++++-- src/controllers/units.controller.js | 14 ++++++------- src/datalayer/wallet.js | 23 ++++++++++++++++++++++ src/tasks/sync-organizations.js | 2 ++ src/utils/data-assertions.js | 12 ++++++++++- 7 files changed, 70 insertions(+), 18 deletions(-) diff --git a/src/controllers/organization.controller.js b/src/controllers/organization.controller.js index d8f0d9aa..1f82c84e 100644 --- a/src/controllers/organization.controller.js +++ b/src/controllers/organization.controller.js @@ -1,6 +1,10 @@ import { Organization } from '../models/organizations'; -import { assertHomeOrgExists } from '../utils/data-assertions'; +import { + assertHomeOrgExists, + assertWalletIsSynced, + assertDataLayerAvailable, +} from '../utils/data-assertions'; export const findAll = async (req, res) => { return res.json(await Organization.getOrgsMap()); @@ -8,6 +12,9 @@ export const findAll = async (req, res) => { export const create = async (req, res) => { try { + await assertDataLayerAvailable(); + await assertWalletIsSynced(); + const myOrganization = await Organization.getHomeOrg(); if (myOrganization) { @@ -32,8 +39,12 @@ export const create = async (req, res) => { // eslint-disable-next-line export const importOrg = async (req, res) => { - const { orgUid, ip, port } = req.body; try { + await assertDataLayerAvailable(); + await assertWalletIsSynced(); + + const { orgUid, ip, port } = req.body; + res.json({ message: 'Importing and subscribing organization this can take a few mins.', @@ -51,6 +62,8 @@ export const importOrg = async (req, res) => { export const subscribeToOrganization = async (req, res) => { try { + await assertDataLayerAvailable(); + await assertWalletIsSynced(); await assertHomeOrgExists(); await Organization.subscribeToOrganization(req.body.orgUid); diff --git a/src/controllers/project.controller.js b/src/controllers/project.controller.js index 634324f5..86536354 100644 --- a/src/controllers/project.controller.js +++ b/src/controllers/project.controller.js @@ -16,7 +16,7 @@ import { assertProjectRecordExists, assertCsvFileInRequest, assertHomeOrgExists, - assetNoPendingCommits, + assertNoPendingCommits, assertRecordExistance, assertDataLayerAvailable, } from '../utils/data-assertions'; @@ -34,7 +34,7 @@ export const create = async (req, res) => { try { await assertDataLayerAvailable(); await assertHomeOrgExists(); - await assetNoPendingCommits(); + await assertNoPendingCommits(); const newRecord = _.cloneDeep(req.body); // When creating new projects assign a uuid to is so @@ -187,7 +187,7 @@ export const updateFromXLS = async (req, res) => { try { await assertDataLayerAvailable(); await assertHomeOrgExists(); - await assetNoPendingCommits(); + await assertNoPendingCommits(); const { files } = req; @@ -217,7 +217,7 @@ export const update = async (req, res) => { try { await assertDataLayerAvailable(); await assertHomeOrgExists(); - await assetNoPendingCommits(); + await assertNoPendingCommits(); const originalRecord = await assertProjectRecordExists( req.body.warehouseProjectId, @@ -306,7 +306,7 @@ export const destroy = async (req, res) => { try { await assertDataLayerAvailable(); await assertHomeOrgExists(); - await assetNoPendingCommits(); + await assertNoPendingCommits(); const originalRecord = await assertProjectRecordExists( req.body.warehouseProjectId, @@ -337,7 +337,7 @@ export const batchUpload = async (req, res) => { try { await assertDataLayerAvailable(); await assertHomeOrgExists(); - await assetNoPendingCommits(); + await assertNoPendingCommits(); const csvFile = assertCsvFileInRequest(req); await createProjectRecordsFromCsv(csvFile); diff --git a/src/controllers/staging.controller.js b/src/controllers/staging.controller.js index 2c6643ee..ab34187d 100644 --- a/src/controllers/staging.controller.js +++ b/src/controllers/staging.controller.js @@ -4,7 +4,9 @@ import { Staging } from '../models'; import { assertStagingRecordExists, assertHomeOrgExists, - assetNoPendingCommits, + assertNoPendingCommits, + assertWalletIsSynced, + assertDataLayerAvailable, } from '../utils/data-assertions'; export const findAll = async (req, res) => { @@ -40,7 +42,9 @@ export const findAll = async (req, res) => { export const commit = async (req, res) => { try { await assertHomeOrgExists(); - await assetNoPendingCommits(); + await assertDataLayerAvailable(); + await assertWalletIsSynced(); + await assertNoPendingCommits(); await Staging.pushToDataLayer(_.get(req, 'query.table', null)); res.json({ message: 'Staging Table committed to full node' }); diff --git a/src/controllers/units.controller.js b/src/controllers/units.controller.js index 986a3833..00c880b0 100644 --- a/src/controllers/units.controller.js +++ b/src/controllers/units.controller.js @@ -18,7 +18,7 @@ import { assertSumOfSplitUnitsIsValid, assertCsvFileInRequest, assertHomeOrgExists, - assetNoPendingCommits, + assertNoPendingCommits, assertRecordExistance, assertDataLayerAvailable, } from '../utils/data-assertions'; @@ -36,7 +36,7 @@ import xlsx from 'node-xlsx'; export const create = async (req, res) => { try { await assertDataLayerAvailable(); - await assetNoPendingCommits(); + await assertNoPendingCommits(); await assertHomeOrgExists(); const newRecord = _.cloneDeep(req.body); @@ -223,7 +223,7 @@ export const updateFromXLS = async (req, res) => { try { await assertDataLayerAvailable(); await assertHomeOrgExists(); - await assetNoPendingCommits(); + await assertNoPendingCommits(); const { files } = req; @@ -250,7 +250,7 @@ export const update = async (req, res) => { try { await assertDataLayerAvailable(); await assertHomeOrgExists(); - await assetNoPendingCommits(); + await assertNoPendingCommits(); const originalRecord = await assertUnitRecordExists( req.body.warehouseUnitId, @@ -336,7 +336,7 @@ export const destroy = async (req, res) => { try { await assertDataLayerAvailable(); await assertHomeOrgExists(); - await assetNoPendingCommits(); + await assertNoPendingCommits(); const originalRecord = await assertUnitRecordExists( req.body.warehouseUnitId, @@ -366,7 +366,7 @@ export const split = async (req, res) => { try { await assertDataLayerAvailable(); await assertHomeOrgExists(); - await assetNoPendingCommits(); + await assertNoPendingCommits(); const originalRecord = await assertUnitRecordExists( req.body.warehouseUnitId, @@ -443,7 +443,7 @@ export const batchUpload = async (req, res) => { try { await assertDataLayerAvailable(); await assertHomeOrgExists(); - await assetNoPendingCommits(); + await assertNoPendingCommits(); const csvFile = assertCsvFileInRequest(req); await createUnitRecordsFromCsv(csvFile); diff --git a/src/datalayer/wallet.js b/src/datalayer/wallet.js index ae136c48..511e1ac8 100644 --- a/src/datalayer/wallet.js +++ b/src/datalayer/wallet.js @@ -25,6 +25,28 @@ const getBaseOptions = () => { return baseOptions; }; +const walletIsSynced = async () => { + const options = { + url: `${rpcUrl}/get_sync_status`, + body: JSON.stringify({ + genesis_initialized: true, + success: true, + synced: true, + syncing: false, + }), + }; + + const response = await request(Object.assign({}, getBaseOptions(), options)); + + const data = JSON.parse(response); + + if (data.success) { + return data.synced; + } + + return false; +}; + const hasUnconfirmedTransactions = async () => { const options = { url: `${rpcUrl}/get_transactions`, @@ -44,4 +66,5 @@ const hasUnconfirmedTransactions = async () => { export default { hasUnconfirmedTransactions, + walletIsSynced, }; diff --git a/src/tasks/sync-organizations.js b/src/tasks/sync-organizations.js index 9410c798..a076e425 100644 --- a/src/tasks/sync-organizations.js +++ b/src/tasks/sync-organizations.js @@ -1,5 +1,7 @@ import { SimpleIntervalJob, Task } from 'toad-scheduler'; import { Organization } from '../models'; +import dotenv from 'dotenv'; +dotenv.config(); const task = new Task('sync-organizations', () => { console.log('Subscribing to default organizations'); diff --git a/src/utils/data-assertions.js b/src/utils/data-assertions.js index a28cf8db..a44e88db 100644 --- a/src/utils/data-assertions.js +++ b/src/utils/data-assertions.js @@ -14,7 +14,7 @@ export const assertDataLayerAvailable = async () => { } }; -export const assetNoPendingCommits = async () => { +export const assertNoPendingCommits = async () => { if (process.env.USE_SIMULATOR === 'true') { const pendingCommits = await Staging.findAll({ where: { commited: true }, @@ -35,6 +35,16 @@ export const assetNoPendingCommits = async () => { } }; +export const assertWalletIsSynced = async () => { + if (process.env.USE_SIMULATOR === 'false') { + if (!(await datalayer.walletIsSynced())) { + throw new Error( + 'Your wallet is syncing, please wait for it to sync and try again', + ); + } + } +}; + export const assertRecordExistance = async (Model, pk) => { const record = await Model.findByPk(pk); if (!record) {