diff --git a/.env.copy b/.env.copy index 55f68266..f701f0df 100644 --- a/.env.copy +++ b/.env.copy @@ -3,5 +3,6 @@ DB_PASSWORD= DB_NAME= DB_HOST= DATALAYER_URL=https://localhost:8562 +WALLET_URL=https://localhost:9256 USE_SIMULATOR=false PICKLIST_URL=https://climate-warehouse.s3.us-west-2.amazonaws.com/public/picklists.json \ No newline at end of file diff --git a/src/datalayer/index.js b/src/datalayer/index.js index 4353803b..3af9a67e 100644 --- a/src/datalayer/index.js +++ b/src/datalayer/index.js @@ -1,2 +1,3 @@ export * from './writeService'; export * from './syncService'; +export * from './wallet'; diff --git a/src/datalayer/persistance.js b/src/datalayer/persistance.js index fe872e3a..181c4bdb 100644 --- a/src/datalayer/persistance.js +++ b/src/datalayer/persistance.js @@ -71,7 +71,7 @@ export const pushChangeListToDataLayer = async (storeId, changelist) => { console.log(data); - throw new Error(data.error); + return false; }; export const getRoots = async (storeIds) => { diff --git a/src/datalayer/simulator.js b/src/datalayer/simulator.js index 6657c84c..e34a0f1f 100644 --- a/src/datalayer/simulator.js +++ b/src/datalayer/simulator.js @@ -78,7 +78,7 @@ export const getRoot = async (storeId) => { ); return Promise.resolve({ hash: null, - status: 1, + status: 2, success: false, }); } @@ -91,7 +91,7 @@ export const getRoot = async (storeId) => { return Promise.resolve({ hash, - status: 1, + status: 2, success: true, }); }; @@ -124,7 +124,7 @@ export const getRoots = async (storeIds) => { .update(JSON.stringify(simulatorTable)) .digest('hex')}`, id: storeId, - status: 1, + status: 2, }; } @@ -132,7 +132,7 @@ export const getRoots = async (storeIds) => { return { hash: 0, id: storeId, - status: 1, + status: 2, }; }), success: true, diff --git a/src/datalayer/syncService.js b/src/datalayer/syncService.js index 31aa18cd..eb75eaed 100644 --- a/src/datalayer/syncService.js +++ b/src/datalayer/syncService.js @@ -115,6 +115,8 @@ export const dataLayerWasUpdated = async () => { rootResponse = await dataLayer.getRoots(subscribedOrgIds); } + console.log(rootResponse); + if (!rootResponse.success) { return []; } @@ -125,8 +127,9 @@ export const dataLayerWasUpdated = async () => { ); if (org) { + console.log(rootHash); // store has been updated if its confirmed and the hash has changed - return rootHash.status === 1 && org.registryHash != rootHash.hash; + return rootHash.status === 2 && org.registryHash != rootHash.hash; } return false; diff --git a/src/datalayer/wallet.js b/src/datalayer/wallet.js new file mode 100644 index 00000000..d1628dbb --- /dev/null +++ b/src/datalayer/wallet.js @@ -0,0 +1,43 @@ +import fs from 'fs'; +import path from 'path'; +import request from 'request-promise'; +import os from 'os'; + +process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0; + +const rpcUrl = process.env.WALLET_URL; + +const getBaseOptions = () => { + const homeDir = os.homedir(); + const certFile = path.resolve( + `${homeDir}/.chia/mainnet/config/ssl/wallet/private_wallet.crt`, + ); + const keyFile = path.resolve( + `${homeDir}/.chia/mainnet/config/ssl/wallet/private_wallet.key`, + ); + + const baseOptions = { + method: 'POST', + cert: fs.readFileSync(certFile), + key: fs.readFileSync(keyFile), + }; + + return baseOptions; +}; + +export const hasUnconfirmedTransactions = async () => { + const options = { + url: `${rpcUrl}/get_transactions`, + body: JSON.stringify({}), + }; + + const response = await request(Object.assign({}, getBaseOptions(), options)); + + const data = JSON.parse(response); + + if (data.success) { + return data.transactions.some((transaction) => !transaction.confirmed); + } + + return false; +}; diff --git a/src/datalayer/writeService.js b/src/datalayer/writeService.js index e7f0510f..b4621ca9 100644 --- a/src/datalayer/writeService.js +++ b/src/datalayer/writeService.js @@ -1,4 +1,5 @@ import * as dataLayer from './persistance'; +import * as wallet from './wallet'; import * as simulator from './simulator'; import { encodeHex } from '../utils/datalayer-utils'; @@ -37,8 +38,10 @@ const pushChangesWhenStoreIsAvailable = async (storeId, changeList) => { if (process.env.USE_SIMULATOR === 'true') { return simulator.pushChangeListToDataLayer(storeId, changeList); } else { + const hasUnconfirmedTransactions = + await wallet.hasUnconfirmedTransactions(); const storeExistAndIsConfirmed = await dataLayer.getRoot(storeId); - if (storeExistAndIsConfirmed) { + if (!hasUnconfirmedTransactions && storeExistAndIsConfirmed) { const success = await dataLayer.pushChangeListToDataLayer( storeId, changeList, diff --git a/src/models/organizations/organizations.model.js b/src/models/organizations/organizations.model.js index bce5aff0..275f7eb1 100644 --- a/src/models/organizations/organizations.model.js +++ b/src/models/organizations/organizations.model.js @@ -56,7 +56,7 @@ class Organization extends Model { }); //sync the registry store - syncDataLayer(newRegistryId, { + await syncDataLayer(newRegistryId, { [dataVersion]: registryVersionId, }); diff --git a/src/models/organizations/organizations.stub.json b/src/models/organizations/organizations.stub.json index 5b272a2b..952beb42 100644 --- a/src/models/organizations/organizations.stub.json +++ b/src/models/organizations/organizations.stub.json @@ -16,9 +16,9 @@ "isHome": false }, { - "orgUid": "37af613ae4547d3061f210aaad8cf92be7ddd199a8f344a0173bd95d150210a9", - "name": "Alexa Registry", - "registryId": "1dbe4373d76a60916a885fa662de042eb93149a9562ba99713428780bd443d8b", + "orgUid": "26462fbf4a10175dbc6945dd3fda35e05da845532f585d79097e06d77ef7165e", + "name": "Kyles Org Registry", + "registryId": "eb9f9440f11f2705dd97da824f6f27079cbde3ad4ff4252539f17c8f0c08a4f9", "icon": "https://climate-warehouse.s3.us-west-2.amazonaws.com/public/orgs/denmark.svg", "subscribed": true, "isHome": false diff --git a/src/utils/data-assertions.js b/src/utils/data-assertions.js index ff4e601b..80301336 100644 --- a/src/utils/data-assertions.js +++ b/src/utils/data-assertions.js @@ -4,7 +4,7 @@ import _ from 'lodash'; import { Organization, Unit, Project, Staging } from '../models'; import { transformSerialNumberBlock } from '../utils/helpers'; -import { dataLayerAvailable } from '../datalayer'; +import { dataLayerAvailable, hasUnconfirmedTransactions } from '../datalayer'; export const assertDataLayerAvailable = async () => { const isAvailable = await dataLayerAvailable(); @@ -15,12 +15,7 @@ export const assertDataLayerAvailable = async () => { }; export const assetNoPendingCommits = async () => { - const pendingCommits = await Staging.findAll({ - where: { commited: true }, - raw: true, - }); - - if (pendingCommits.length > 0) { + if (await hasUnconfirmedTransactions()) { throw new Error( 'You currently have changes pending on the blockchain. Please wait for them to propagate before making more changes', );