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 14, 2022
1 parent 766b0b6 commit c880960
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 10 deletions.
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": "65f701dc31b4fee8246448333b3f2149039c6f6952d13f455266ce87b1c52fe9",
"name": "Kyles Org Registry",
"registryId": "f5624ed03a81d8250743f20de5cc2930b6b2b6346e39b5435bf39c0d6d1c098a",
"icon": "https://climate-warehouse.s3.us-west-2.amazonaws.com/public/orgs/denmark.svg",
"subscribed": true,
"isHome": false
Expand Down

0 comments on commit c880960

Please sign in to comment.