Skip to content

Commit

Permalink
feat: sync governance data
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Mar 24, 2022
1 parent eb88ab5 commit 0b3d58a
Show file tree
Hide file tree
Showing 24 changed files with 707 additions and 35 deletions.
4 changes: 4 additions & 0 deletions .env.copy
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ USE_SIMULATOR=false
PICKLIST_URL=https://climate-warehouse.s3.us-west-2.amazonaws.com/public/picklists.json
DEFAULT_ORGANIZATIONS=https://climate-warehouse.s3.us-west-2.amazonaws.com/public/cw-organizations.json
READ_ONLY=false
IS_GOVERNANCE_BODY=false
API_KEY=
GOVERANCE_BODY_ID=
GOVERNANCE_BODY_IP=
GOVERNANCE_BODY_PORT=
85 changes: 79 additions & 6 deletions src/controllers/governance.controller.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,98 @@
import { Governance } from '../models';

import {
assertIsActiveGovernanceBody,
assertIfReadOnlyMode,
assertDataLayerAvailable,
assertWalletIsAvailable,
assertWalletIsSynced,
assertCanBeGovernanceBody,
} from '../utils/data-assertions';

export const findAll = async (req, res) => {
try {
const results = await Governance.findAll();
return res.json(results);
} catch (error) {
res.status(400).json({
message: 'Can not retreive issuances',
message: 'Can not retreive Governance Data',
error: error.message,
});
}
};

// eslint-disable-next-line
export const createGoveranceBody = async (req, res) => {};
export const createGoveranceBody = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertWalletIsAvailable();
await assertWalletIsSynced();
await assertCanBeGovernanceBody();

// eslint-disable-next-line
export const setDefaultOrgList = async (req, res) => {};
await Governance.createGoveranceBody();

return res.json({
message:
'Setting up new Governance Body on this node, this can tae a few mins',
});
} catch (error) {
res.status(400).json({
message: 'Cant update default orgs',
error: error.message,
});
}
};

// eslint-disable-next-line
export const setPickList = async (req, res) => {};
export const setDefaultOrgList = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertWalletIsAvailable();
await assertWalletIsSynced();
await assertIsActiveGovernanceBody();

const orgList = JSON.stringify(req.body);

await Governance.updateGoveranceBodyData([
{ key: 'orgList', value: orgList },
]);

return res.json({
message: 'Committed this new organization list to the datalayer',
});
} catch (error) {
console.trace(error);
res.status(400).json({
message: 'Cant update default orgs',
error: error.message,
});
}
};

// eslint-disable-next-line
export const subscribeToGovernanceBody = async (req, res) => {};
export const setPickList = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertWalletIsAvailable();
await assertWalletIsSynced();
await assertIsActiveGovernanceBody();

const pickList = JSON.stringify(req.body);

await Governance.updateGoveranceBodyData([
{ key: 'pickList', value: pickList },
]);

return res.json({
message: 'Committed this pick list to the datalayer',
});
} catch (error) {
res.status(400).json({
message: 'Cant update picklist',
error: error.message,
});
}
};
1 change: 1 addition & 0 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * as OrganizationController from './organization.controller';
export * as IssuanceController from './issuance.controller';
export * as LabelController from './label.controller';
export * as AuditController from './audit.controller';
export * as GovernanceController from './governance.controller';
5 changes: 5 additions & 0 deletions src/database/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import CreateOrganizationTable from './20220122164836-create-organization-table'
import CreateEstimationTable from './20220127190529-create-estimation-table';
import CreateAuditTable from './20220222204323-create-audit-table';
import CreateMetaTable from './20220119211024-create-meta-table';
import CreateGoveranceTable from './20220315134151-create-governance-table';

export const migrations = [
{
Expand Down Expand Up @@ -99,4 +100,8 @@ export const migrations = [
migration: AddOrgUidIndex,
name: '20220115134849-add-orgUid-index.js',
},
{
migration: CreateGoveranceTable,
name: '20220315134151-create-governance-table',
},
];
2 changes: 2 additions & 0 deletions src/datalayer/persistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export const createDataLayerStore = async () => {

const data = JSON.parse(response);

console.log(data);

if (data.success) {
return data.id;
}
Expand Down
1 change: 1 addition & 0 deletions src/datalayer/writeService.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const pushChangesWhenStoreIsAvailable = async (
const storeExistAndIsConfirmed = await dataLayer.getRoot(storeId);

if (!hasUnconfirmedTransactions && storeExistAndIsConfirmed) {
console.log('pushing to datalayer', { storeId, changeList });
const success = await dataLayer.pushChangeListToDataLayer(
storeId,
changeList,
Expand Down
4 changes: 2 additions & 2 deletions src/models/governance/governance.mock.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import stub from './meta.stub.json';
import stub from './governance.stub.json';

export const MetaMock = {
export const GovernanceMock = {
findAll: () => stub,
findOne: (id) => {
return stub.find((record) => record.id == id);
Expand Down
64 changes: 57 additions & 7 deletions src/models/governance/governance.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,51 @@ class Governance extends Model {

await Meta.upsert({
metaKey: 'goveranceBodyId',
goveranceBodyId,
metaValue: goveranceBodyId,
});

return goveranceBodyId;
}

static async sync() {
const { GOVERANCE_BODY_ID, GOVERNANCE_BODY_IP, GOVERNANCE_BODY_PORT } =
process.env;

if (!GOVERANCE_BODY_ID || !GOVERNANCE_BODY_IP || !GOVERNANCE_BODY_PORT) {
throw new Error('Missing information in env to sync Governance data');
}

const governanceData = await datalayer.getSubscribedStoreData(
GOVERANCE_BODY_ID,
GOVERNANCE_BODY_IP,
GOVERNANCE_BODY_PORT,
);

const updates = [];

if (governanceData.orgList) {
updates.push({
metaKey: 'orgList',
metaValue: governanceData.orgList,
confirmed: true,
});
}

if (governanceData.pickList) {
updates.push({
metaKey: 'pickList',
metaValue: governanceData.pickList,
confirmed: true,
});
}

await Promise.all(updates.map(async (update) => Governance.upsert(update)));
}

static async updateGoveranceBodyData(keyValueArray) {
const goveranceBodyId = await Meta.find({
const goveranceBodyId = await Meta.findOne({
where: { metaKey: 'goveranceBodyId' },
raw: true,
});

if (!goveranceBodyId) {
Expand All @@ -38,7 +74,7 @@ class Governance extends Model {
);
}

const existingRecords = Governance.findAll({ raw: true });
const existingRecords = await Governance.findAll({ raw: true });

const changeList = [];

Expand All @@ -53,13 +89,20 @@ class Governance extends Model {
metaValue: keyValue.value,
confirmed: false,
});

changeList.push(
keyValueToChangeList(keyValue.key, keyValue.value, valueExists),
...keyValueToChangeList(keyValue.key, keyValue.value, valueExists),
);
}),
);

const rollbackChangesIfFailed = async () => {
console.log('Reverting Goverance Records');
await Governance.destroy({
where: {},
truncate: true,
});

await Promise.all(
existingRecords.map(async (record) => await Governance.upsert(record)),
);
Expand All @@ -77,15 +120,22 @@ class Governance extends Model {
);
};

await datalayer.pushDataLayerChangeList(goveranceBodyId, changeList);
await datalayer.pushDataLayerChangeList(
goveranceBodyId.metaValue,
changeList,
);

datalayer.getStoreData(goveranceBodyId, onConfirm, rollbackChangesIfFailed);
datalayer.getStoreData(
goveranceBodyId.metaValue,
onConfirm,
rollbackChangesIfFailed,
);
}
}

Governance.init(ModelTypes, {
sequelize,
modelName: 'meta',
modelName: 'governance',
freezeTableName: true,
timestamps: false,
createdAt: false,
Expand Down
6 changes: 5 additions & 1 deletion src/models/governance/governance.modeltypes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ module.exports = {
metaKey: {
type: Sequelize.STRING,
unique: true,
required: true,
},
metaValue: {
type: Sequelize.STRING,
required: true,
},
metaValue: Sequelize.STRING,
confirmed: {
type: Sequelize.BOOLEAN,
defaultValue: false,
Expand Down
Loading

0 comments on commit 0b3d58a

Please sign in to comment.