Skip to content
This repository has been archived by the owner on Dec 13, 2019. It is now read-only.

Basic approach to one off migration #2563

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@counterfactual/node",
"version": "0.3.2",
"version": "0.3.3",
"main": "dist/index.js",
"iife": "dist/index.iife.js",
"types": "dist/src/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from "./types";
export * from "./methods/errors";
export * from "rpc-server";
export { getNetworkEnum, EthereumNetworkName } from "./network-configuration";
export { migrateToPatch1 } from "./migrations/patch-1";
123 changes: 123 additions & 0 deletions packages/node/src/migrations/patch-1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { Node } from "@counterfactual/types";
import { bigNumberify, BigNumberish } from "ethers/utils";

import { StateChannelJSON } from "../models/state-channel";

const DB_NAMESPACE_CHANNEL = "channel";
const DB_NAMESPACE_WITHDRAWALS = "multisigAddressToWithdrawalCommitment";
const DB_NAMESPACE_ALL_COMMITMENTS = "allCommitments";

const bigNumberIshToString = (x: BigNumberish) => bigNumberify(x).toHexString();

export const migrateToPatch1 = async (
storeService: Node.IStoreService,
storeKeyPrefix: string
) => {
const stateChannelsMap: {
[multisigAddress: string]: StateChannelJSON;
} = await storeService.get(`${storeKeyPrefix}/${DB_NAMESPACE_CHANNEL}`);

for (const multisigAddress in stateChannelsMap) {
/**
* Update proposal:
* https://github.com/counterfactual/monorepo/pull/2542/files
*/

const proposals = stateChannelsMap[multisigAddress].proposedAppInstances;

for (let i = 0; i < proposals.length; i += 1) {
const proposal = proposals[i][1];
stateChannelsMap[multisigAddress].proposedAppInstances[i][1] = {
...proposal,
initiatorDeposit: bigNumberIshToString(proposal.initiatorDeposit),
responderDeposit: bigNumberIshToString(proposal.responderDeposit),
timeout: bigNumberIshToString(proposal.timeout)
};
}

/**
* Update interpreter params
* https://github.com/counterfactual/monorepo/pull/2544/files#diff-95c2150ee50c44be12700a72fd6fbf73R32
*/

const apps = stateChannelsMap[multisigAddress].appInstances;

for (let i = 0; i < apps.length; i += 1) {
const app = apps[i][1];

if (app.twoPartyOutcomeInterpreterParams) {
stateChannelsMap[multisigAddress].appInstances[
i
][1].twoPartyOutcomeInterpreterParams = {
...app.twoPartyOutcomeInterpreterParams!,
amount: bigNumberIshToString(
app.twoPartyOutcomeInterpreterParams!.amount
)
};
}

if (app.singleAssetTwoPartyCoinTransferInterpreterParams) {
stateChannelsMap[multisigAddress].appInstances[
i
][1].singleAssetTwoPartyCoinTransferInterpreterParams = {
...app.singleAssetTwoPartyCoinTransferInterpreterParams!,
limit: bigNumberIshToString(
app.singleAssetTwoPartyCoinTransferInterpreterParams!.limit
)
};
}

if (app.multiAssetMultiPartyCoinTransferInterpreterParams) {
stateChannelsMap[multisigAddress].appInstances[
i
][1].multiAssetMultiPartyCoinTransferInterpreterParams = {
...app.multiAssetMultiPartyCoinTransferInterpreterParams!,
limit: app.multiAssetMultiPartyCoinTransferInterpreterParams.limit.map(
bigNumberIshToString!
)
};
}
}

/**
* Delete createdAt
* https://github.com/counterfactual/monorepo/pull/2541/files
*/
delete stateChannelsMap[multisigAddress]["createdAt"];

/**
* https://github.com/counterfactual/monorepo/pull/2538/files
*/

const agreements =
stateChannelsMap[multisigAddress]
.singleAssetTwoPartyIntermediaryAgreements;

for (let i = 0; i < agreements.length; i += 1) {
const agreement = agreements[i][1];
stateChannelsMap[
multisigAddress
].singleAssetTwoPartyIntermediaryAgreements[i][1] = {
...agreements[i][1],
capitalProvided: bigNumberIshToString(agreement.capitalProvided)
};
}
}

const withdrawals = await storeService.get(
`${storeKeyPrefix}/${DB_NAMESPACE_WITHDRAWALS}`
);

const commitments = await storeService.get(
`${storeKeyPrefix}/${DB_NAMESPACE_ALL_COMMITMENTS}`
);

const sharedData = {
commitments,
withdrawals,
stateChannelsMap,
version: 1
};

await storeService.set([{ path: storeKeyPrefix, value: sharedData }]);
};
19 changes: 13 additions & 6 deletions packages/node/src/store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NetworkContext, Node, SolidityValueType } from "@counterfactual/types";
import { Node, SolidityValueType } from "@counterfactual/types";
import { solidityKeccak256 } from "ethers/utils";

import {
Expand All @@ -15,6 +15,7 @@ import {
import { prettyPrintObject } from "./utils";

interface SharedData {
version: 1; // TODO: Add better versioning & migrations tooling
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From looking around it seems like there are various ways of versioning a db schema.

Though I'm kinda confused as to why the version number is being put in SharedData? I'm also confused how this handles migrations overall if there are no typed schemas between the versions?

Maybe we should have a separate blob that holds both the version number and the schema at the corresponding version number. The migration script would then just diff n-1 and n to apply the migration.

Copy link
Contributor Author

@snario snario Oct 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StoreData = { schemaVersion: number, data: SharedData } ?

stateChannelsMap: { [multisigAddress: string]: StateChannelJSON };
commitments: { [specialHash: string]: any[] };
withdrawals: { [multisigAddress: string]: Node.MinimalTransaction };
Expand All @@ -26,6 +27,7 @@ interface SharedData {
*/
export class Store {
public sharedData: SharedData = {
version: 1,
stateChannelsMap: {},
commitments: {},
withdrawals: {}
Expand All @@ -37,11 +39,16 @@ export class Store {
) {}

public async connectDB() {
this.sharedData = (await this.storeService.get(this.storeKeyPrefix)) || {
stateChannelsMap: {},
commitments: {},
withdrawals: {}
};
this.sharedData = Object.assign(
{},
await this.storeService.get(this.storeKeyPrefix),
{
version: 1,
stateChannelsMap: {},
commitments: {},
withdrawals: {}
}
);
}

public async persistDB() {
Expand Down
2 changes: 1 addition & 1 deletion packages/simple-hub-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
"dependencies": {
"@counterfactual/firebase-client": "0.0.7",
"@counterfactual/node": "0.3.2",
"@counterfactual/node": "0.3.3",
"@counterfactual/types": "0.0.45",
"@counterfactual/typescript-typings": "0.1.3",
"@ebryn/jsonapi-ts": "0.1.17",
Expand Down