Skip to content
This repository was archived by the owner on Dec 27, 2022. It is now read-only.
Merged
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
7 changes: 0 additions & 7 deletions modules/browser-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { constructRpcRequest, hydrateProviders, NatsMessagingService } from "@co
import pino, { BaseLogger } from "pino";

import { BrowserStore } from "./services/store";
import { BrowserLockService } from "./services/lock";
import { DirectProvider, IframeChannelProvider, IRpcChannelProvider } from "./channelProvider";
import { BrowserNodeError } from "./errors";
export * from "./constants";
Expand Down Expand Up @@ -108,11 +107,6 @@ export class BrowserNode implements INodeService {
config.signer.publicIdentifier,
config.logger.child({ module: "BrowserStore" }),
);
const lock = new BrowserLockService(
config.signer.publicIdentifier,
messaging,
config.logger.child({ module: "BrowserLockService" }),
);
const chainService = new VectorChainService(
store,
chainJsonProviders,
Expand Down Expand Up @@ -146,7 +140,6 @@ export class BrowserNode implements INodeService {

const engine = await VectorEngine.connect(
messaging,
lock,
store,
config.signer,
chainService,
Expand Down
64 changes: 0 additions & 64 deletions modules/browser-node/src/services/lock.ts

This file was deleted.

55 changes: 0 additions & 55 deletions modules/engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Vector } from "@connext/vector-protocol";
import {
ChainAddresses,
IChannelSigner,
ILockService,
IMessagingService,
IVectorProtocol,
Result,
Expand All @@ -19,7 +18,6 @@ import {
ChannelRpcMethods,
IExternalValidation,
AUTODEPLOY_CHAIN_IDS,
FullChannelState,
EngineError,
UpdateType,
Values,
Expand Down Expand Up @@ -74,13 +72,11 @@ export class VectorEngine implements IVectorEngine {
private readonly vector: IVectorProtocol,
private readonly chainService: IVectorChainService,
private readonly chainAddresses: ChainAddresses,
private readonly lockService: ILockService,
private readonly logger: pino.BaseLogger,
) {}

static async connect(
messaging: IMessagingService,
lock: ILockService,
store: IEngineStore,
signer: IChannelSigner,
chainService: IVectorChainService,
Expand All @@ -92,7 +88,6 @@ export class VectorEngine implements IVectorEngine {
): Promise<VectorEngine> {
const vector = await Vector.connect(
messaging,
lock,
store,
signer,
chainService,
Expand All @@ -107,7 +102,6 @@ export class VectorEngine implements IVectorEngine {
vector,
chainService,
chainAddresses,
lock,
logger.child({ module: "VectorEngine" }),
);
await engine.setupListener(gasSubsidyPercentage);
Expand Down Expand Up @@ -140,59 +134,10 @@ export class VectorEngine implements IVectorEngine {
this.chainAddresses,
this.logger,
this.setup.bind(this),
this.acquireRestoreLocks.bind(this),
this.releaseRestoreLocks.bind(this),
gasSubsidyPercentage,
);
}

private async acquireRestoreLocks(channel: FullChannelState): Promise<Result<void, EngineError>> {
if (this.restoreLocks[channel.channelAddress]) {
// Has already been released, return undefined
return Result.ok(this.restoreLocks[channel.channelAddress]);
}
try {
const isAlice = channel.alice === this.signer.address;
const lockVal = await this.lockService.acquireLock(
channel.channelAddress,
isAlice,
isAlice ? channel.bobIdentifier : channel.aliceIdentifier,
);
this.restoreLocks[channel.channelAddress] = lockVal;
return Result.ok(undefined);
} catch (e) {
return Result.fail(
new RestoreError(RestoreError.reasons.AcquireLockError, channel.channelAddress, this.signer.publicIdentifier, {
acquireRestoreLockError: e.message,
}),
);
}
}

private async releaseRestoreLocks(channel: FullChannelState): Promise<Result<void, EngineError>> {
if (!this.restoreLocks[channel.channelAddress]) {
// Has already been released, return undefined
return Result.ok(undefined);
}
try {
const isAlice = channel.alice === this.signer.address;
await this.lockService.releaseLock(
channel.channelAddress,
this.restoreLocks[channel.channelAddress],
isAlice,
isAlice ? channel.bobIdentifier : channel.aliceIdentifier,
);
delete this.restoreLocks[channel.channelAddress];
return Result.ok(undefined);
} catch (e) {
return Result.fail(
new RestoreError(RestoreError.reasons.ReleaseLockError, channel.channelAddress, this.signer.publicIdentifier, {
releaseRestoreLockError: e.message,
}),
);
}
}

private async getConfig(): Promise<
Result<ChannelRpcMethodsResponsesMap[typeof ChannelRpcMethods.chan_getConfig], EngineError>
> {
Expand Down
104 changes: 1 addition & 103 deletions modules/engine/src/listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ export async function setupEngineListeners(
setup: (
params: EngineParams.Setup,
) => Promise<Result<ChannelRpcMethodsResponsesMap[typeof ChannelRpcMethods.chan_setup], EngineError>>,
acquireRestoreLocks: (channel: FullChannelState) => Promise<Result<void, EngineError>>,
releaseRestoreLocks: (channel: FullChannelState) => Promise<Result<void, EngineError>>,
gasSubsidyPercentage: number,
): Promise<void> {
// Set up listener for channel setup
Expand Down Expand Up @@ -175,107 +173,7 @@ export async function setupEngineListeners(
const method = "onReceiveRestoreStateMessage";
logger.debug({ method }, "Handling message");

// releases the lock, and acks to senders confirmation message
const releaseLockAndAck = async (channelAddress: string, postToEvt = false) => {
const channel = await store.getChannelState(channelAddress);
if (!channel) {
logger.error({ channelAddress }, "Failed to find channel to release lock");
return;
}
await releaseRestoreLocks(channel);
await messaging.respondToRestoreStateMessage(inbox, Result.ok(undefined));
if (postToEvt) {
// Post to evt
evts[EngineEvents.RESTORE_STATE_EVENT].post({
channelAddress: channel.channelAddress,
aliceIdentifier: channel.aliceIdentifier,
bobIdentifier: channel.bobIdentifier,
chainId: channel.networkContext.chainId,
});
}
return;
};

// Received error from counterparty
if (restoreData.isError) {
// releasing the lock should be done regardless of error
logger.error({ message: restoreData.getError()!.message, method }, "Error received from counterparty restore");
await releaseLockAndAck(restoreData.getError()!.context.channelAddress);
return;
}

const data = restoreData.getValue();
const [key] = Object.keys(data ?? []);
if (key !== "chainId" && key !== "channelAddress") {
logger.error({ data }, "Message malformed");
return;
}

if (key === "channelAddress") {
const { channelAddress } = data as { channelAddress: string };
await releaseLockAndAck(channelAddress, true);
return;
}

// Otherwise, they are looking to initiate a sync
let channel: FullChannelState | undefined;
const sendCannotRestoreFromError = (error: Values<typeof RestoreError.reasons>, context: any = {}) => {
return messaging.respondToRestoreStateMessage(
inbox,
Result.fail(
new RestoreError(error, channel?.channelAddress ?? "", signer.publicIdentifier, { ...context, method }),
),
);
};

// Get info from store to send to counterparty
const { chainId } = data as any;
try {
channel = await store.getChannelStateByParticipants(signer.publicIdentifier, from, chainId);
} catch (e) {
return sendCannotRestoreFromError(RestoreError.reasons.CouldNotGetChannel, {
storeMethod: "getChannelStateByParticipants",
chainId,
identifiers: [signer.publicIdentifier, from],
});
}
if (!channel) {
return sendCannotRestoreFromError(RestoreError.reasons.ChannelNotFound, { chainId });
}
let activeTransfers: FullTransferState[];
try {
activeTransfers = await store.getActiveTransfers(channel.channelAddress);
} catch (e) {
return sendCannotRestoreFromError(RestoreError.reasons.CouldNotGetActiveTransfers, {
storeMethod: "getActiveTransfers",
chainId,
channelAddress: channel.channelAddress,
});
}

// Acquire lock
const res = await acquireRestoreLocks(channel);
if (res.isError) {
return sendCannotRestoreFromError(RestoreError.reasons.AcquireLockError, {
acquireLockError: jsonifyError(res.getError()!),
});
}

// Send info to counterparty
logger.debug(
{
channel: channel.channelAddress,
nonce: channel.nonce,
activeTransfers: activeTransfers.map((a) => a.transferId),
},
"Sending counterparty state to sync",
);
await messaging.respondToRestoreStateMessage(inbox, Result.ok({ channel, activeTransfers }));

// Release lock on timeout regardless
setTimeout(() => {
releaseRestoreLocks(channel!);
}, 15_000);
throw new Error("call to protocol to add to internal queue");
},
);

Expand Down
6 changes: 0 additions & 6 deletions modules/engine/src/testing/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
getTestLoggers,
MemoryStoreService,
MemoryMessagingService,
MemoryLockService,
getRandomBytes32,
mkPublicIdentifier,
mkAddress,
Expand Down Expand Up @@ -51,7 +50,6 @@ describe("VectorEngine", () => {
it("should connect without validation", async () => {
const engine = await VectorEngine.connect(
Sinon.createStubInstance(MemoryMessagingService),
Sinon.createStubInstance(MemoryLockService),
storeService,
getRandomChannelSigner(),
chainService as IVectorChainService,
Expand All @@ -66,7 +64,6 @@ describe("VectorEngine", () => {
it("should connect with validation", async () => {
const engine = await VectorEngine.connect(
Sinon.createStubInstance(MemoryMessagingService),
Sinon.createStubInstance(MemoryLockService),
storeService,
getRandomChannelSigner(),
chainService as IVectorChainService,
Expand Down Expand Up @@ -156,7 +153,6 @@ describe("VectorEngine", () => {
it(test.name, async () => {
const engine = await VectorEngine.connect(
Sinon.createStubInstance(MemoryMessagingService),
Sinon.createStubInstance(MemoryLockService),
storeService,
getRandomChannelSigner(),
chainService as IVectorChainService,
Expand Down Expand Up @@ -195,7 +191,6 @@ describe("VectorEngine", () => {
it(test.name, async () => {
const engine = await VectorEngine.connect(
Sinon.createStubInstance(MemoryMessagingService),
Sinon.createStubInstance(MemoryLockService),
storeService,
getRandomChannelSigner(),
chainService as IVectorChainService,
Expand Down Expand Up @@ -809,7 +804,6 @@ describe("VectorEngine", () => {
it(test.name, async () => {
const engine = await VectorEngine.connect(
Sinon.createStubInstance(MemoryMessagingService),
Sinon.createStubInstance(MemoryLockService),
storeService,
getRandomChannelSigner(),
chainService as IVectorChainService,
Expand Down
4 changes: 0 additions & 4 deletions modules/engine/src/testing/listeners.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,6 @@ describe(testName, () => {
chainAddresses,
log,
() => Promise.resolve(Result.ok({} as any)),
acquireRestoreLockStub,
releaseRestoreLockStub,
gasSubsidyPercentage,
);

Expand Down Expand Up @@ -464,8 +462,6 @@ describe(testName, () => {
chainAddresses,
log,
() => Promise.resolve(Result.ok({} as any)),
acquireRestoreLockStub,
releaseRestoreLockStub,
50,
);

Expand Down
2 changes: 1 addition & 1 deletion modules/protocol/src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
MessagingError,
jsonifyError,
} from "@connext/vector-types";
import { getRandomBytes32, LOCK_TTL } from "@connext/vector-utils";
import { getRandomBytes32 } from "@connext/vector-utils";
import pino from "pino";

import { InboundChannelUpdateError, OutboundChannelUpdateError } from "./errors";
Expand Down
Loading