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

Commit

Permalink
Remove proposals from Store DB
Browse files Browse the repository at this point in the history
  • Loading branch information
snario committed Sep 20, 2019
1 parent b80ce65 commit ef9d66b
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 122 deletions.
30 changes: 15 additions & 15 deletions packages/node/src/message-handling/handle-node-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,19 @@ export async function handleReceivedProposalMessage(
proposedByIdentifier
);

await store.addAppInstanceProposal(
stateChannel,
new AppInstanceProposal(
{
...params,
proposedByIdentifier,
initiatorDeposit: params.responderDeposit,
initiatorDepositTokenAddress: params.responderDepositTokenAddress!,
responderDeposit: params.initiatorDeposit!,
responderDepositTokenAddress: params.initiatorDepositTokenAddress!
},
stateChannel
)
const proposal = new AppInstanceProposal(
{
...params,
proposedByIdentifier,
initiatorDeposit: params.responderDeposit,
initiatorDepositTokenAddress: params.responderDepositTokenAddress!,
responderDeposit: params.initiatorDeposit!,
responderDepositTokenAddress: params.initiatorDepositTokenAddress!
},
stateChannel
);

await store.saveStateChannel(stateChannel.bumpProposedApps());
await store.saveStateChannel(stateChannel.addProposal(proposal));
}

export async function handleRejectProposalMessage(
Expand All @@ -52,5 +49,8 @@ export async function handleRejectProposalMessage(
const {
data: { appInstanceId }
} = receivedRejectProposalMessage;
await store.removeAppInstanceProposal(appInstanceId);

const stateChannel = await store.getChannelFromAppInstanceID(appInstanceId);

await store.saveStateChannel(stateChannel.removeProposal(appInstanceId));
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ export async function handleReceivedProtocolMessage(
}
}
if (proposal) {
await store.saveRealizedProposedAppInstance(proposal);
await store.saveStateChannel(
(await store.getChannelFromAppInstanceID(
appInstanceId
)).removeProposal(appInstanceId)
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ export async function installVirtual(
);
}

await store.saveRealizedProposedAppInstance(proposal);
await store.saveStateChannel(
(await store.getChannelFromAppInstanceID(appInstanceId)).removeProposal(
appInstanceId
)
);

return proposal;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class InstallController extends NodeController {
requestHandler: RequestHandler,
params: Node.InstallParams
): Promise<Node.InstallResult> {
const { store, protocolRunner, publicIdentifier } = requestHandler;
const { store, protocolRunner } = requestHandler;

const appInstanceProposal = await install(store, protocolRunner, params);

Expand Down
6 changes: 5 additions & 1 deletion packages/node/src/methods/app-instance/install/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ export async function install(
}
);

await store.saveRealizedProposedAppInstance(proposal);
await store.saveStateChannel(
(await store.getChannelFromAppInstanceID(appInstanceId)).removeProposal(
appInstanceId
)
);

return proposal;
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ export async function createProposedAppInstance(
stateChannel
);

await store.addAppInstanceProposal(stateChannel, appInstanceProposal);

await store.saveStateChannel(stateChannel.bumpProposedApps());
await store.saveStateChannel(stateChannel.addProposal(appInstanceProposal));

return appInstanceProposal.identityHash;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export default async function rejectInstallVirtualController(

const proposal = await store.getAppInstanceProposal(appInstanceId);

await store.removeAppInstanceProposal(appInstanceId);
const stateChannel = await store.getChannelFromAppInstanceID(appInstanceId);

await store.saveStateChannel(stateChannel.removeProposal(appInstanceId));

const rejectInstallVirtualMsg: RejectInstallVirtualMessage = {
from: publicIdentifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ export default class RejectInstallController extends NodeController {
return rejectInstallVirtualController(requestHandler, params);
}

await store.removeAppInstanceProposal(appInstanceId);
const stateChannel = await store.getChannelFromAppInstanceID(appInstanceId);

await store.saveStateChannel(stateChannel.removeProposal(appInstanceId));

const rejectProposalMsg: RejectProposalMessage = {
from: publicIdentifier,
Expand Down
58 changes: 50 additions & 8 deletions packages/node/src/models/state-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { xkeyKthAddress } from "../machine/xkeys";
import { Store } from "../store";
import { prettyPrintObject } from "../utils";

import { AppInstanceProposal, AppInstanceProposalJSON } from ".";
import { AppInstance } from "./app-instance";
import {
CoinTransferMap,
Expand Down Expand Up @@ -59,6 +60,7 @@ type SingleAssetTwoPartyIntermediaryAgreementJSON = {
export type StateChannelJSON = {
readonly multisigAddress: string;
readonly userNeuteredExtendedKeys: string[];
readonly proposedAppInstances: [string, AppInstanceProposalJSON][];
readonly appInstances: [string, AppInstanceJson][];
readonly singleAssetTwoPartyIntermediaryAgreements: [
string,
Expand All @@ -73,6 +75,10 @@ export class StateChannel {
constructor(
public readonly multisigAddress: string,
public readonly userNeuteredExtendedKeys: string[],
readonly proposedAppInstances: ReadonlyMap<
string,
AppInstanceProposal
> = new Map<string, AppInstanceProposal>([]),
readonly appInstances: ReadonlyMap<string, AppInstance> = new Map<
string,
AppInstance
Expand Down Expand Up @@ -168,10 +174,8 @@ export class StateChannel {
return this.getSigningKeysFor(this.monotonicNumProposedApps);
}

public bumpProposedApps(): StateChannel {
return this.build({
monotonicNumProposedApps: this.monotonicNumProposedApps + 1
});
public get hasFreeBalance(): boolean {
return !!this.freeBalanceAppInstance;
}

public get freeBalance(): AppInstance {
Expand All @@ -184,10 +188,6 @@ export class StateChannel {
);
}

public get hasFreeBalance(): boolean {
return !!this.freeBalanceAppInstance;
}

public getMultisigOwnerAddrOf(xpub: string): string {
const [alice, bob] = this.multisigOwners;

Expand Down Expand Up @@ -224,6 +224,7 @@ export class StateChannel {
multisigAddress?: string;
userNeuteredExtendedKeys?: string[];
appInstances?: ReadonlyMap<string, AppInstance>;
proposedAppInstances?: ReadonlyMap<string, AppInstanceProposal>;
singleAssetTwoPartyIntermediaryAgreements?: ReadonlyMap<
string,
SingleAssetTwoPartyIntermediaryAgreement
Expand All @@ -235,6 +236,7 @@ export class StateChannel {
return new StateChannel(
args.multisigAddress || this.multisigAddress,
args.userNeuteredExtendedKeys || this.userNeuteredExtendedKeys,
args.proposedAppInstances || this.proposedAppInstances,
args.appInstances || this.appInstances,
args.singleAssetTwoPartyIntermediaryAgreements ||
this.singleAssetTwoPartyIntermediaryAgreements,
Expand Down Expand Up @@ -303,6 +305,7 @@ export class StateChannel {
return new StateChannel(
multisigAddress,
userNeuteredExtendedKeys,
new Map<string, AppInstanceProposal>([]),
new Map<string, AppInstance>([]),
new Map<string, SingleAssetTwoPartyIntermediaryAgreement>(),
createFreeBalance(
Expand All @@ -321,6 +324,7 @@ export class StateChannel {
return new StateChannel(
multisigAddress,
userNeuteredExtendedKeys,
new Map<string, AppInstanceProposal>([]),
new Map<string, AppInstance>(),
new Map<string, SingleAssetTwoPartyIntermediaryAgreement>(),
// Note that this FreeBalance is undefined because a channel technically
Expand All @@ -330,6 +334,31 @@ export class StateChannel {
);
}

public addProposal(proposal: AppInstanceProposal) {
const proposedAppInstances = new Map<string, AppInstanceProposal>(
this.proposedAppInstances.entries()
);

proposedAppInstances.set(proposal.identityHash, proposal);

return this.build({
proposedAppInstances,
monotonicNumProposedApps: this.monotonicNumProposedApps + 1
});
}

public removeProposal(appInstanceId: string) {
const proposedAppInstances = new Map<string, AppInstanceProposal>(
this.proposedAppInstances.entries()
);

proposedAppInstances.delete(appInstanceId);

return this.build({
proposedAppInstances
});
}

public addAppInstance(appInstance: AppInstance) {
const appInstances = new Map<string, AppInstance>(
this.appInstances.entries()
Expand Down Expand Up @@ -492,6 +521,11 @@ export class StateChannel {
return {
multisigAddress: this.multisigAddress,
userNeuteredExtendedKeys: this.userNeuteredExtendedKeys,
proposedAppInstances: [...this.proposedAppInstances.entries()].map(
(proposal): [string, AppInstanceProposalJSON] => {
return [proposal[0], proposal[1].toJson()];
}
),
appInstances: [...this.appInstances.entries()].map((appInstanceEntry): [
string,
AppInstanceJson
Expand Down Expand Up @@ -521,6 +555,14 @@ export class StateChannel {
return new StateChannel(
json.multisigAddress,
json.userNeuteredExtendedKeys,
new Map(
[...Object.values(json.proposedAppInstances || [])].map((proposal): [
string,
AppInstanceProposal
] => {
return [proposal[0], AppInstanceProposal.fromJson(proposal[1])];
})
),
new Map(
[...Object.values(json.appInstances || [])].map((appInstanceEntry): [
string,
Expand Down
Loading

0 comments on commit ef9d66b

Please sign in to comment.