Skip to content

Commit

Permalink
feat: check for unconfirmed transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Feb 15, 2022
1 parent 766b0b6 commit 9e34945
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 18 deletions.
1 change: 1 addition & 0 deletions .env.copy
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions src/datalayer/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './writeService';
export * from './syncService';
export * from './wallet';
2 changes: 1 addition & 1 deletion src/datalayer/persistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
8 changes: 4 additions & 4 deletions src/datalayer/simulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const getRoot = async (storeId) => {
);
return Promise.resolve({
hash: null,
status: 1,
status: 2,
success: false,
});
}
Expand All @@ -91,7 +91,7 @@ export const getRoot = async (storeId) => {

return Promise.resolve({
hash,
status: 1,
status: 2,
success: true,
});
};
Expand Down Expand Up @@ -124,15 +124,15 @@ export const getRoots = async (storeIds) => {
.update(JSON.stringify(simulatorTable))
.digest('hex')}`,
id: storeId,
status: 1,
status: 2,
};
}

// no hash for simulated external org tables (they dont exist in simulator)
return {
hash: 0,
id: storeId,
status: 1,
status: 2,
};
}),
success: true,
Expand Down
5 changes: 4 additions & 1 deletion src/datalayer/syncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ export const dataLayerWasUpdated = async () => {
rootResponse = await dataLayer.getRoots(subscribedOrgIds);
}

console.log(rootResponse);

if (!rootResponse.success) {
return [];
}
Expand All @@ -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;
Expand Down
43 changes: 43 additions & 0 deletions src/datalayer/wallet.js
Original file line number Diff line number Diff line change
@@ -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;
};
5 changes: 4 additions & 1 deletion src/datalayer/writeService.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/models/organizations/organizations.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Organization extends Model {
});

//sync the registry store
syncDataLayer(newRegistryId, {
await syncDataLayer(newRegistryId, {
[dataVersion]: registryVersionId,
});

Expand Down
6 changes: 3 additions & 3 deletions src/models/organizations/organizations.stub.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 2 additions & 7 deletions src/utils/data-assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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',
);
Expand Down

0 comments on commit 9e34945

Please sign in to comment.