Skip to content

Commit

Permalink
Merge pull request #587 from Chia-Network/develop
Browse files Browse the repository at this point in the history
fix: remove typo in governance method and add gov id to meta table
  • Loading branch information
MichaelTaylor3D authored Jun 2, 2022
2 parents 075cc36 + ae7a342 commit 00d743b
Show file tree
Hide file tree
Showing 3 changed files with 387 additions and 383 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "climate-warehouse",
"version": "1.0.8",
"version": "1.0.9",
"private": true,
"bin": "build/server.js",
"type": "module",
Expand Down
298 changes: 149 additions & 149 deletions src/datalayer/writeService.js
Original file line number Diff line number Diff line change
@@ -1,149 +1,149 @@
import _ from 'lodash';

import * as dataLayer from './persistance';
import wallet from './wallet';
import * as simulator from './simulator';
import { encodeHex, decodeHex } from '../utils/datalayer-utils';
import { getConfig } from '../utils/config-loader';
import { logger } from '../config/logger.cjs';

logger.info('climate-warehouse:datalayer:writeService');

const { USE_SIMULATOR } = getConfig().APP;

const createDataLayerStore = async () => {
let storeId;
if (USE_SIMULATOR) {
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) => {
return {
action: 'insert',
key: encodeHex(key),
value: encodeHex(data[key]),
};
});

await pushChangesWhenStoreIsAvailable(storeId, changeList, failedCallback);
};

const retry = (storeId, changeList, failedCallback, retryAttempts) => {
logger.info(`Retrying pushing to store ${storeId}: ${retryAttempts}`);
if (retryAttempts >= 60) {
logger.info(
'Could not push changelist to datalayer after retrying 10 times',
);
failedCallback();
return;
}

setTimeout(async () => {
await pushChangesWhenStoreIsAvailable(
storeId,
changeList,
failedCallback,
retryAttempts + 1,
);
}, 30000);
};

const pushChangesWhenStoreIsAvailable = async (
storeId,
changeList,
failedCallback = _.noop,
retryAttempts = 0,
) => {
if (USE_SIMULATOR) {
return simulator.pushChangeListToDataLayer(storeId, changeList);
} else {
const hasUnconfirmedTransactions =
await wallet.hasUnconfirmedTransactions();

const storeExistAndIsConfirmed = await dataLayer.getRoot(storeId);

if (!hasUnconfirmedTransactions && storeExistAndIsConfirmed) {
logger.info(
`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,
changeList,
);

if (!success) {
logger.info(
`RPC failed when pushing to store ${storeId}, attempting retry.`,
);
retry(storeId, changeList, failedCallback, retryAttempts);
}
} else {
retry(storeId, changeList, failedCallback, retryAttempts);
}
}
};

const pushDataLayerChangeList = (storeId, changeList, failedCallback) => {
pushChangesWhenStoreIsAvailable(storeId, changeList, failedCallback);
};

const dataLayerAvailable = async () => {
if (USE_SIMULATOR) {
return simulator.dataLayerAvailable();
} else {
return dataLayer.dataLayerAvailable();
}
};

export default {
dataLayerAvailable,
pushDataLayerChangeList,
syncDataLayer,
createDataLayerStore,
};
import _ from 'lodash';

import * as dataLayer from './persistance';
import wallet from './wallet';
import * as simulator from './simulator';
import { encodeHex, decodeHex } from '../utils/datalayer-utils';
import { getConfig } from '../utils/config-loader';
import { logger } from '../config/logger.cjs';

logger.info('climate-warehouse:datalayer:writeService');

const { USE_SIMULATOR } = getConfig().APP;

const createDataLayerStore = async () => {
let storeId;
if (USE_SIMULATOR) {
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) => {
return {
action: 'insert',
key: encodeHex(key),
value: encodeHex(data[key]),
};
});

await pushChangesWhenStoreIsAvailable(storeId, changeList, failedCallback);
};

const retry = (storeId, changeList, failedCallback, retryAttempts) => {
logger.info(`Retrying pushing to store ${storeId}: ${retryAttempts}`);
if (retryAttempts >= 60) {
logger.info(
'Could not push changelist to datalayer after retrying 10 times',
);
failedCallback();
return;
}

setTimeout(async () => {
await pushChangesWhenStoreIsAvailable(
storeId,
changeList,
failedCallback,
retryAttempts + 1,
);
}, 30000);
};

const pushChangesWhenStoreIsAvailable = async (
storeId,
changeList,
failedCallback = _.noop,
retryAttempts = 0,
) => {
if (USE_SIMULATOR) {
return simulator.pushChangeListToDataLayer(storeId, changeList);
} else {
const hasUnconfirmedTransactions =
await wallet.hasUnconfirmedTransactions();

const storeExistAndIsConfirmed = await dataLayer.getRoot(storeId);

if (!hasUnconfirmedTransactions && storeExistAndIsConfirmed) {
logger.info(
`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,
changeList,
);

if (!success) {
logger.info(
`RPC failed when pushing to store ${storeId}, attempting retry.`,
);
retry(storeId, changeList, failedCallback, retryAttempts);
}
} else {
retry(storeId, changeList, failedCallback, retryAttempts);
}
}
};

const pushDataLayerChangeList = (storeId, changeList, failedCallback) => {
pushChangesWhenStoreIsAvailable(storeId, changeList, failedCallback);
};

const dataLayerAvailable = async () => {
if (USE_SIMULATOR) {
return simulator.dataLayerAvailable();
} else {
return dataLayer.dataLayerAvailable();
}
};

export default {
dataLayerAvailable,
pushDataLayerChangeList,
syncDataLayer,
createDataLayerStore,
};
Loading

0 comments on commit 00d743b

Please sign in to comment.