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

Commit 6ebc5d6

Browse files
authored
Correctly queue proposals received (#2335)
1 parent 2db1dd0 commit 6ebc5d6

File tree

2 files changed

+63
-49
lines changed

2 files changed

+63
-49
lines changed

packages/node/src/message-handling/handle-node-message.ts

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
getOrCreateStateChannelBetweenVirtualAppParticipants
44
} from "../methods/app-instance/propose-install-virtual/operation";
55
import { NO_APP_INSTANCE_ID_TO_INSTALL } from "../methods/errors";
6+
import { executeFunctionWithinQueues } from "../methods/queued-execution";
67
import { AppInstanceProposal } from "../models";
78
import { RequestHandler } from "../request-handler";
89
import {
@@ -74,24 +75,29 @@ export async function handleReceivedProposalMessage(
7475
networkContext.MinimumViableMultisig
7576
);
7677

77-
const stateChannel = await store.getStateChannel(multisigAddress);
78-
79-
await store.addAppInstanceProposal(
80-
stateChannel,
81-
new AppInstanceProposal(
82-
{
83-
...params,
84-
proposedByIdentifier,
85-
initiatorDeposit: params.responderDeposit,
86-
initiatorDepositTokenAddress: params.responderDepositTokenAddress!,
87-
responderDeposit: params.initiatorDeposit!,
88-
responderDepositTokenAddress: params.initiatorDepositTokenAddress!
89-
},
90-
stateChannel
91-
)
78+
await executeFunctionWithinQueues(
79+
[await requestHandler.getShardedQueue(multisigAddress)],
80+
async () => {
81+
const stateChannel = await store.getStateChannel(multisigAddress);
82+
83+
await store.addAppInstanceProposal(
84+
stateChannel,
85+
new AppInstanceProposal(
86+
{
87+
...params,
88+
proposedByIdentifier,
89+
initiatorDeposit: params.responderDeposit,
90+
initiatorDepositTokenAddress: params.responderDepositTokenAddress!,
91+
responderDeposit: params.initiatorDeposit!,
92+
responderDepositTokenAddress: params.initiatorDepositTokenAddress!
93+
},
94+
stateChannel
95+
)
96+
);
97+
98+
await store.saveStateChannel(stateChannel.bumpProposedApps());
99+
}
92100
);
93-
94-
await store.saveStateChannel(stateChannel.bumpProposedApps());
95101
}
96102

97103
export async function handleRejectProposalMessage(
@@ -144,28 +150,40 @@ export async function handleReceivedProposeVirtualMessage(
144150
} as ProposeVirtualMessage
145151
);
146152
} else {
147-
const stateChannel = await getOrCreateStateChannelBetweenVirtualAppParticipants(
148-
proposedByIdentifier,
149-
publicIdentifier,
150-
intermediaryIdentifier,
151-
store,
152-
networkContext
153+
const multisigAddress = getCreate2MultisigAddress(
154+
[proposedByIdentifier, proposedToIdentifier],
155+
networkContext.ProxyFactory,
156+
networkContext.MinimumViableMultisig
153157
);
154158

155-
const proposal = new AppInstanceProposal(
156-
{
157-
...params,
158-
proposedByIdentifier,
159-
initiatorDeposit: responderDeposit,
160-
initiatorDepositTokenAddress: responderDepositTokenAddress!,
161-
responderDeposit: initiatorDeposit,
162-
responderDepositTokenAddress: initiatorDepositTokenAddress!
163-
},
164-
stateChannel
159+
await executeFunctionWithinQueues(
160+
[await requestHandler.getShardedQueue(multisigAddress)],
161+
async () => {
162+
const stateChannel = await getOrCreateStateChannelBetweenVirtualAppParticipants(
163+
multisigAddress,
164+
proposedByIdentifier,
165+
proposedToIdentifier,
166+
intermediaryIdentifier,
167+
store,
168+
networkContext
169+
);
170+
171+
await store.addVirtualAppInstanceProposal(
172+
new AppInstanceProposal(
173+
{
174+
...params,
175+
proposedByIdentifier,
176+
initiatorDeposit: responderDeposit,
177+
initiatorDepositTokenAddress: responderDepositTokenAddress!,
178+
responderDeposit: initiatorDeposit,
179+
responderDepositTokenAddress: initiatorDepositTokenAddress!
180+
},
181+
stateChannel
182+
)
183+
);
184+
185+
await store.saveStateChannel(stateChannel.bumpProposedApps());
186+
}
165187
);
166-
167-
await store.addVirtualAppInstanceProposal(proposal);
168-
169-
await store.saveStateChannel(stateChannel.bumpProposedApps());
170188
}
171189
}

packages/node/src/methods/app-instance/propose-install-virtual/operation.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@ export async function createProposedVirtualAppInstance(
2121
): Promise<string> {
2222
const { intermediaryIdentifier, proposedToIdentifier } = params;
2323

24+
const multisigAddress = getCreate2MultisigAddress(
25+
[myIdentifier, proposedToIdentifier],
26+
networkContext.ProxyFactory,
27+
networkContext.MinimumViableMultisig
28+
);
29+
2430
const channel = await getOrCreateStateChannelBetweenVirtualAppParticipants(
31+
multisigAddress,
2532
myIdentifier,
2633
proposedToIdentifier,
2734
intermediaryIdentifier,
@@ -67,18 +74,13 @@ export function getNextNodeAddress(
6774
}
6875

6976
export async function getOrCreateStateChannelBetweenVirtualAppParticipants(
77+
multisigAddress: string,
7078
initiatorXpub: string,
7179
responderXpub: string,
7280
hubXpub: string,
7381
store: Store,
7482
networkContext: NetworkContext
7583
): Promise<StateChannel> {
76-
const multisigAddress = getCreate2MultisigAddress(
77-
[initiatorXpub, responderXpub],
78-
networkContext.ProxyFactory,
79-
networkContext.MinimumViableMultisig
80-
);
81-
8284
try {
8385
return await store.getStateChannel(multisigAddress);
8486
} catch (e) {
@@ -88,12 +90,6 @@ export async function getOrCreateStateChannelBetweenVirtualAppParticipants(
8890
.includes(NO_STATE_CHANNEL_FOR_MULTISIG_ADDR(multisigAddress)) &&
8991
hubXpub !== undefined
9092
) {
91-
const multisigAddress = getCreate2MultisigAddress(
92-
[initiatorXpub, responderXpub],
93-
networkContext.ProxyFactory,
94-
networkContext.MinimumViableMultisig
95-
);
96-
9793
const stateChannel = StateChannel.createEmptyChannel(multisigAddress, [
9894
initiatorXpub,
9995
responderXpub

0 commit comments

Comments
 (0)