Skip to content

Commit

Permalink
feat: wait for the singletons to confirm before resolving promise
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed May 27, 2022
1 parent f344469 commit ec93647
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 81 deletions.
10 changes: 4 additions & 6 deletions src/controllers/organization.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ export const createV2 = async (req, res) => {

const dataModelVersion = getDataModelVersion();

Organization.createHomeOrganization(name, icon, dataModelVersion);

return res.json({
message: 'New organization created successfully.',
orgId: await Organization.createHomeOrganization(
name,
icon,
dataModelVersion,
),
message:
'New organization created successfully. Please wait for it to be confirmed.',
});
}
} catch (error) {
Expand Down
35 changes: 22 additions & 13 deletions src/datalayer/persistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path from 'path';
import request from 'request-promise';
import os from 'os';
import { getConfig } from '../utils/config-loader';
import { decodeHex } from '../utils/datalayer-utils';

import { logger } from '../config/logger.cjs';

Expand Down Expand Up @@ -46,8 +47,6 @@ export const createDataLayerStore = async () => {
return data.id;
}

logger.info(data);

throw new Error(data.error);
};

Expand All @@ -67,21 +66,20 @@ export const pushChangeListToDataLayer = async (storeId, changelist) => {

const data = JSON.parse(response);

logger.info(options);
logger.info(data);

if (data.success) {
logger.info('Success!');
logger.info(
`Success!, Changes were submitted to the datalayer for storeId: ${storeId}`,
);
return true;
}

if (data.error.includes('Key already present')) {
logger.info('Success, I guess...');
logger.info(
`The datalayer key was already present, its possible your data was pushed to the datalayer but never broadcasted to the blockchain. This can create a mismatched state in your node.`,
);
return true;
}

logger.info(data);

return false;
} catch (error) {
logger.info('There was an error pushing your changes to the datalayer');
Expand Down Expand Up @@ -164,12 +162,24 @@ export const getStoreData = async (storeId, rootHash) => {

if (data.success) {
if (!_.isEmpty(data.keys_values)) {
logger.info(`Downloaded Data: ${JSON.stringify(data)}`);
logger.info(
`Downloaded Data: ${JSON.stringify(
data.keys_values.map((record) => {
return {
...record,
key: decodeHex(record.key),
value: /{([^*]*)}/.test(decodeHex(record.value))
? JSON.parse(decodeHex(record.value))
: decodeHex(record.value),
};
}),
null,
2,
)}`,
);
}
return data;
}

logger.info(data);
}

return false;
Expand All @@ -193,7 +203,6 @@ export const dataLayerAvailable = async () => {
return true;
}

logger.info(data);
return false;
} catch (error) {
return false;
Expand Down
42 changes: 40 additions & 2 deletions src/datalayer/writeService.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import _ from 'lodash';
import * as dataLayer from './persistance';
import wallet from './wallet';
import * as simulator from './simulator';
import { encodeHex } from '../utils/datalayer-utils';
import { encodeHex, decodeHex } from '../utils/datalayer-utils';
import { getConfig } from '../utils/config-loader';
import { logger } from '../config/logger.cjs';

Expand All @@ -17,11 +17,37 @@ const createDataLayerStore = async () => {
storeId = await simulator.createDataLayerStore();
} else {
storeId = await dataLayer.createDataLayerStore();

logger.info(
`Created storeId: ${storeId}, waiting for this to be confirmed on the blockchain.`,
);
await waitForStoreToBeConfirmed(storeId);
}

return storeId;
};

const waitForStoreToBeConfirmed = async (storeId, retry = 0) => {
if (retry > 120) {
throw new Error(
`Creating storeId: ${storeId} timed out. Its possible the transaction is stuck.`,
);
}

const storeExistAndIsConfirmed = await dataLayer.getRoot(storeId);

if (!storeExistAndIsConfirmed) {
logger.info(`Still waiting for ${storeId} to confirm`);
await new Promise((resolve) => {
setTimeout(() => {
resolve();
}, 30000);
});
return waitForStoreToBeConfirmed(storeId, retry + 1);
}
logger.info(`StoreId: ${storeId} has been confirmed. Congrats!`);
};

const syncDataLayer = async (storeId, data, failedCallback) => {
logger.info(`Syncing ${storeId}`);
const changeList = Object.keys(data).map((key) => {
Expand Down Expand Up @@ -71,7 +97,19 @@ const pushChangesWhenStoreIsAvailable = async (

if (!hasUnconfirmedTransactions && storeExistAndIsConfirmed) {
logger.info(
`pushing to datalayer ${storeId} ${JSON.stringify(changeList)}`,
`pushing to datalayer ${storeId} ${JSON.stringify(
changeList.map((change) => {
return {
action: change.action,
key: decodeHex(change.key),
value: /{([^*]*)}/.test(decodeHex(change.value))
? JSON.parse(decodeHex(change.value))
: decodeHex(change.value),
};
}),
null,
2,
)}`,
);
const success = await dataLayer.pushChangeListToDataLayer(
storeId,
Expand Down
125 changes: 65 additions & 60 deletions src/models/organizations/organizations.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,75 +59,82 @@ class Organization extends Model {
}

static async createHomeOrganization(name, icon, dataVersion = 'v1') {
const myOrganization = await Organization.getHomeOrg();

if (myOrganization) {
return myOrganization.orgUid;
}

const newOrganizationId = USE_SIMULATOR
? 'f1c54511-865e-4611-976c-7c3c1f704662'
: await datalayer.createDataLayerStore();
try {
logger.info('Creating New Organization, This could take a while.');
const myOrganization = await Organization.getHomeOrg();

const newRegistryId = await datalayer.createDataLayerStore();
const registryVersionId = await datalayer.createDataLayerStore();
if (myOrganization) {
return myOrganization.orgUid;
}

const revertOrganizationIfFailed = async () => {
logger.info('Reverting Failed Organization');
await Organization.destroy({ where: { orgUid: newOrganizationId } });
};

// sync the organization store
await datalayer.syncDataLayer(
newOrganizationId,
{
registryId: newRegistryId,
name,
icon,
},
revertOrganizationIfFailed,
);
const newOrganizationId = USE_SIMULATOR
? 'f1c54511-865e-4611-976c-7c3c1f704662'
: await datalayer.createDataLayerStore();

//sync the registry store
await datalayer.syncDataLayer(
newRegistryId,
{
[dataVersion]: registryVersionId,
},
revertOrganizationIfFailed,
);
const newRegistryId = await datalayer.createDataLayerStore();
const registryVersionId = await datalayer.createDataLayerStore();

await Organization.create({
orgUid: newOrganizationId,
registryId: registryVersionId,
isHome: true,
subscribed: USE_SIMULATOR,
name,
icon,
});
const revertOrganizationIfFailed = async () => {
logger.info('Reverting Failed Organization');
await Organization.destroy({ where: { orgUid: newOrganizationId } });
};

const onConfirm = () => {
logger.info('Organization confirmed, you are ready to go');
Organization.update(
// sync the organization store
await datalayer.syncDataLayer(
newOrganizationId,
{
subscribed: true,
registryId: newRegistryId,
name,
icon,
},
{ where: { orgUid: newOrganizationId } },
revertOrganizationIfFailed,
);
};

if (!USE_SIMULATOR) {
logger.info('Waiting for New Organization to be confirmed');
datalayer.getStoreData(
//sync the registry store
await datalayer.syncDataLayer(
newRegistryId,
onConfirm,
{
[dataVersion]: registryVersionId,
},
revertOrganizationIfFailed,
);
} else {
onConfirm();
}

return newOrganizationId;
await Organization.create({
orgUid: newOrganizationId,
registryId: registryVersionId,
isHome: true,
subscribed: USE_SIMULATOR,
name,
icon,
});

const onConfirm = () => {
logger.info('Organization confirmed, you are ready to go');
Organization.update(
{
subscribed: true,
},
{ where: { orgUid: newOrganizationId } },
);
};

if (!USE_SIMULATOR) {
logger.info('Waiting for New Organization to be confirmed');
datalayer.getStoreData(
newRegistryId,
onConfirm,
revertOrganizationIfFailed,
);
} else {
onConfirm();
}

return newOrganizationId;
} catch (error) {
logger.error(error.message);
logger.info('Reverting Failed Organization');
await Organization.destroy({ where: { isHome: true } });
}
}

// eslint-disable-next-line
Expand Down Expand Up @@ -187,15 +194,13 @@ class Organization extends Model {
logger.info('Subscribing to', orgUid, ip, port);
const orgData = await datalayer.getSubscribedStoreData(orgUid, ip, port);

logger.info(orgData);

if (!orgData.registryId) {
throw new Error(
'Currupted organization, no registryId on the datalayer, can not import',
);
}

logger.info('IMPORTING REGISTRY: ', orgData.registryId);
logger.info(`IMPORTING REGISTRY: ${orgData.registryId}`);

const registryData = await datalayer.getSubscribedStoreData(
orgData.registryId,
Expand Down Expand Up @@ -302,7 +307,7 @@ class Organization extends Model {
}),
);
} catch (error) {
logger.info(error);
logger.info(error.message);
}
};

Expand Down

0 comments on commit ec93647

Please sign in to comment.