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

Commit

Permalink
💚 Build fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Rahul Sethuram authored and Rahul Sethuram committed Sep 11, 2020
1 parent f151850 commit 01b547e
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 50 deletions.
35 changes: 24 additions & 11 deletions modules/engine/src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import {
UpdateType,
MultisigCommitment,
IMessagingService,
ChainProviders,
SetupUpdateDetails,
FullChannelState,
IChannelSigner,
} from "@connext/vector-types";
import { BigNumber } from "ethers";
import { BigNumber, constants } from "ethers";
import { Evt } from "evt";
import { ChannelSigner } from "@connext/vector-utils";

import { ChannelUpdateError } from "./errors";
import { VectorMessage, VectorChannelMessage, VectorErrorMessage } from "./types";
Expand Down Expand Up @@ -155,8 +158,8 @@ export async function inbound(
message: VectorMessage,
storeService: IStoreService,
messagingService: IMessagingService,
signer: ChannelSigner,
providerUrl: string,
signer: IChannelSigner,
chainProviders: ChainProviders,
stateEvt: Evt<ChannelState>,
errorEvt: Evt<ChannelUpdateError>,
): Promise<void> {
Expand All @@ -175,7 +178,7 @@ export async function inbound(
storeService,
messagingService,
signer,
providerUrl,
chainProviders,
stateEvt,
errorEvt,
);
Expand All @@ -191,8 +194,8 @@ async function processChannelMessage(
message: VectorChannelMessage,
storeService: IStoreService,
messagingService: IMessagingService,
signer: ChannelSigner,
providerUrl: string,
signer: IChannelSigner,
chainProviders: ChainProviders,
stateEvt: Evt<ChannelState>,
errorEvt: Evt<ChannelUpdateError>,
): Promise<void> {
Expand All @@ -215,7 +218,7 @@ async function processChannelMessage(

// Get our latest stored state + active transfers
const transferInitialStates = await storeService.getTransferInitialStates(requestedUpdate.channelAddress);
let storedState: ChannelState = await storeService.getChannelState(requestedUpdate.channelAddress);
let storedState: FullChannelState = await storeService.getChannelState(requestedUpdate.channelAddress);
if (!storedState) {
// NOTE: the creation update MUST have a nonce of 1 not 0!
// You may not be able to find a channel state IFF the channel is
Expand All @@ -226,16 +229,26 @@ async function processChannelMessage(
new ChannelUpdateError(ChannelUpdateError.reasons.ChannelNotFound, requestedUpdate, storedState),
);
}
requestedUpdate.details as SetupUpdateDetails;
// Create an empty channel state
storedState = {
channelAddress: requestedUpdate.channelAddress,
participants: [requestedUpdate.fromIdentifier, signer.publicIdentifier],
chainId: (await signer.provider.getNetwork()).chainId,
latestNonce: "0",
networkContext: (requestedUpdate.details as SetupUpdateDetails).networkContext,
assetIds: [],
balances: [],
lockedValue: [],
merkleRoot: constants.HashZero,
nonce: 0,
publicIdentifiers: [requestedUpdate.fromIdentifier, signer.publicIdentifier],
timeout: (requestedUpdate.details as SetupUpdateDetails).timeout,
latestUpdate: undefined,
latestDepositNonce: 0,
};
}

const providerUrl = chainProviders[storedState.networkContext.chainId];

// Assume that our stored state has nonce `k`, and the update
// has nonce `n`, and `k` is the latest double signed state for you. The
// following cases exist:
Expand Down Expand Up @@ -276,7 +289,7 @@ async function processChannelMessage(
}

// Get the difference between the stored and received nonces
const diff = BigNumber.from(requestedUpdate.nonce).sub(storedState.latestNonce);
const diff = BigNumber.from(requestedUpdate.nonce).sub(storedState.nonce);

// If we are ahead, or even, do not process update
if (diff.lte(0)) {
Expand Down
43 changes: 30 additions & 13 deletions modules/engine/src/update.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { VectorChannel } from "@connext/vector-contracts";
import { ChannelSigner, getSignerAddressFromPublicIdentifier } from "@connext/vector-utils";
import { getSignerAddressFromPublicIdentifier } from "@connext/vector-utils";
import { Contract, BigNumber, utils, constants } from "ethers";
import {
UpdateType,
Expand All @@ -10,6 +10,7 @@ import {
Balance,
LockedValueType,
TransferState,
IChannelSigner,
} from "@connext/vector-types";

import { validate } from "./validate";
Expand Down Expand Up @@ -117,9 +118,7 @@ export async function generateUpdate<T extends UpdateType>(
params: UpdateParams<T>,
state: FullChannelState,
transferInitialStates: TransferState[],
// NOTE: this is a heavy database query but is required on every update
// due to the general nature of the `validate` function.
signer: ChannelSigner,
signer: IChannelSigner,
providerUrl: string, // TODO: can this be derived from signer?
): Promise<ChannelUpdate<T>> {
// Only in the case of setup should the state be undefined
Expand All @@ -135,15 +134,33 @@ export async function generateUpdate<T extends UpdateType>(
break;
}
case UpdateType.deposit: {
update = await generateDepositUpdate(state, params as UpdateParams<"deposit">, signer, transferInitialStates, providerUrl);
update = await generateDepositUpdate(
state,
params as UpdateParams<"deposit">,
signer,
transferInitialStates,
providerUrl,
);
break;
}
case UpdateType.create: {
update = await generateCreateUpdate(state, params as UpdateParams<"create">, signer, transferInitialStates, providerUrl);
update = await generateCreateUpdate(
state,
params as UpdateParams<"create">,
signer,
transferInitialStates,
providerUrl,
);
break;
}
case UpdateType.resolve: {
update = await generateResolveUpdate(state, params as UpdateParams<"resolve">, signer, transferInitialStates, providerUrl);
update = await generateResolveUpdate(
state,
params as UpdateParams<"resolve">,
signer,
transferInitialStates,
providerUrl,
);
break;
}
default: {
Expand All @@ -157,7 +174,7 @@ export async function generateUpdate<T extends UpdateType>(

async function generateSetupUpdate(
params: UpdateParams<"setup">,
signer: ChannelSigner,
signer: IChannelSigner,
providerUrl: string,
): Promise<ChannelUpdate<"setup">> {
// During channel creation, you have no channel state, so create
Expand Down Expand Up @@ -200,7 +217,7 @@ async function generateSetupUpdate(
async function generateDepositUpdate(
state: FullChannelState,
params: UpdateParams<"deposit">,
signer: ChannelSigner,
signer: IChannelSigner,
transferInitialStates: TransferState[],
providerUrl: string,
): Promise<ChannelUpdate<"deposit">> {
Expand Down Expand Up @@ -248,7 +265,7 @@ async function generateDepositUpdate(
async function generateCreateUpdate(
state: FullChannelState,
params: UpdateParams<"create">,
signer: ChannelSigner,
signer: IChannelSigner,
transferInitialStates: TransferState[],
providerUrl: string,
): Promise<ChannelUpdate<"create">> {
Expand Down Expand Up @@ -301,7 +318,7 @@ async function generateCreateUpdate(
async function generateResolveUpdate(
state: FullChannelState,
params: UpdateParams<"resolve">,
signer: ChannelSigner,
signer: IChannelSigner,
transferInitialStates: TransferState[],
providerUrl: string,
): Promise<ChannelUpdate<"resolve">> {
Expand Down Expand Up @@ -358,7 +375,7 @@ async function generateResolveUpdate(
// not for the update that exists
async function generateSignedChannelCommitment(
newState: FullChannelState,
signer: ChannelSigner,
signer: IChannelSigner,
): Promise<ChannelCommitmentData> {
const { publicIdentifiers, networkContext, ...core } = newState;
const unsigned: ChannelCommitmentData = {
Expand Down Expand Up @@ -391,7 +408,7 @@ function hashCommitment(commitment: ChannelCommitmentData): string {
function generateBaseUpdate<T extends UpdateType>(
state: FullChannelState,
params: UpdateParams<T>,
signer: ChannelSigner,
signer: IChannelSigner,
): Pick<ChannelUpdate<T>, "channelAddress" | "nonce" | "fromIdentifier" | "toIdentifier" | "type"> {
// Create the update with all the things that are constant
// between update types
Expand Down
25 changes: 14 additions & 11 deletions modules/engine/src/vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import {
ResolveTransferParams,
ILockService,
IMessagingService,
ChainProviders,
IChannelSigner,
} from "@connext/vector-types";
import { ChannelSigner } from "@connext/vector-utils";
import { Evt } from "evt";

import * as sync from "./sync";
Expand All @@ -21,28 +22,29 @@ export class Vector {
private channelStateEvt = Evt.create<ChannelState>();
private channelErrorEvt = Evt.create<InboundChannelError>();

constructor(
// make it private so the only way to create the class is to use `connect`
private constructor(
private messagingService: IMessagingService,
private lockService: ILockService,
private storeService: IStoreService,
private signer: ChannelSigner,
public providerUrl: string, // TODO: can this be derived from signer
private signer: IChannelSigner,
public chainProviders: ChainProviders,
) {
this.messagingService = messagingService;
this.storeService = storeService;
this.lockService = lockService;
this.signer = signer;
this.providerUrl = providerUrl;
this.chainProviders = chainProviders;
}

static connect(
messagingService: IMessagingService,
lockService: ILockService,
storeService: IStoreService,
signer: ChannelSigner,
providerUrl: string,
signer: IChannelSigner,
chainProviders: ChainProviders,
): Promise<Vector> {
const node = new Vector(messagingService, lockService, storeService, signer, providerUrl);
const node = new Vector(messagingService, lockService, storeService, signer, chainProviders);

// Handles up asynchronous services and checks to see that
// channel is `setup` plus is not in dispute
Expand All @@ -66,10 +68,11 @@ export class Vector {
// NOTE: This is a heavy query, but is required on every update (even if it
// is not a transfer) due to the general nature of the `validate` api
const transferInitialStates = await this.storeService.getTransferInitialStates(params.channelAddress);
const update = await generateUpdate(params, state, transferInitialStates, this.signer, this.providerUrl);
const providerUrl = this.chainProviders[state.networkContext.chainId];
const update = await generateUpdate(params, state, transferInitialStates, this.signer, providerUrl);
await sync.outbound(
update,
this.providerUrl,
providerUrl,
this.storeService,
this.messagingService,
this.channelStateEvt,
Expand All @@ -89,7 +92,7 @@ export class Vector {
this.storeService,
this.messagingService,
this.signer,
this.providerUrl,
this.chainProviders,
this.channelStateEvt,
this.channelErrorEvt,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
IMessagingService,
ILockService,
IChannelSigner,
ChainProviders,
} from "@connext/vector-types";
import { Vector } from "@connext/vector-engine";

Expand All @@ -22,10 +23,11 @@ export class WalletService implements IWalletService {
private readonly messaging: IMessagingService,
private readonly lock: ILockService,
private readonly signer: IChannelSigner,
private readonly chainProviders: ChainProviders,
) {}

async connect(): Promise<void> {
this.vector = await Vector.connect(this.messaging, this.lock, this.store, this.signer);
this.vector = await Vector.connect(this.messaging, this.lock, this.store, this.signer, this.chainProviders);
}

getPublicIdentifier(): string {
Expand Down
12 changes: 9 additions & 3 deletions modules/isomorphic-node/src/frameworks/register.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { asClass, asValue, createContainer, InjectionMode, asFunction } from "awilix";
import joi from "joi";
import { ILockService, IStoreService, IChannelSigner } from "@connext/vector-types";
import { ILockService, IStoreService, IChannelSigner, ChainProviders } from "@connext/vector-types";

import { IsomorphicNode, IIsomorphicNode } from "../core/app";
import { CreateChannelUseCase } from "../core/usecases/create-channel/create-channel.usecase";
Expand All @@ -16,8 +16,14 @@ export interface IIsomorphicNodeConfig {
lockService: ILockService;
storeService: IStoreService;
signer: IChannelSigner;
chainProviders: ChainProviders;
}
export const registerWithConfig = ({ lockService, storeService, signer }: IIsomorphicNodeConfig): IIsomorphicNode => {
export const registerWithConfig = ({
lockService,
storeService,
signer,
chainProviders,
}: IIsomorphicNodeConfig): IIsomorphicNode => {
container.register({
// Node_modules
joi: asValue(joi),
Expand All @@ -31,7 +37,7 @@ export const registerWithConfig = ({ lockService, storeService, signer }: IIsomo

// shared services
walletService: asFunction(({ messagingService }) => {
return new WalletService(storeService, messagingService, lockService, signer);
return new WalletService(storeService, messagingService, lockService, signer, chainProviders);
}).singleton(),
messagingService: asClass(TempNatsMessagingService).singleton(),

Expand Down
Binary file removed modules/server-node/connext-store.db
Binary file not shown.
14 changes: 7 additions & 7 deletions modules/server-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
},
"devDependencies": {
"@prisma/cli": "2.6.2",
"@types/chai": "^4.2.12",
"@types/chai-as-promised": "^7.1.3",
"@types/mocha": "^8.0.3",
"@types/chai": "4.2.12",
"@types/chai-as-promised": "7.1.3",
"@types/mocha": "8.0.3",
"@types/node": "14.6.4",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"mocha": "^8.1.3",
"ts-mocha": "^7.0.0",
"chai": "4.2.0",
"chai-as-promised": "7.1.1",
"mocha": "8.1.3",
"ts-mocha": "7.0.0",
"typescript": "4.0.2"
}
}
1 change: 1 addition & 0 deletions modules/server-node/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ model Balance {
@@map(name: "balance")
id String @id @default(uuid())
participant String
assetId String
to String
amount String
lockedValue String
Expand Down
8 changes: 4 additions & 4 deletions modules/types/src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export interface ChannelUpdateDetailsMap {
[UpdateType.setup]: SetupUpdateDetails;
}

type CreateUpdateDetails = {
export type CreateUpdateDetails = {
transferId: string;
transferDefinition: Address;
transferTimeout: string;
Expand All @@ -162,7 +162,7 @@ type CreateUpdateDetails = {

// NOTE: proof data can be reconstructed, do we need to pass it around?
// what does it mean
type ResolveUpdateDetails = {
export type ResolveUpdateDetails = {
transferId: string;
transferDefinition: Address;
transferResolver: any; //TODO
Expand All @@ -171,12 +171,12 @@ type ResolveUpdateDetails = {
merkleRoot: string;
};

type DepositUpdateDetails = {
export type DepositUpdateDetails = {
latestDepositNonce: number;
};

// TODO: verify these are correct
type SetupUpdateDetails = {
export type SetupUpdateDetails = {
timeout: string;
networkContext: NetworkContext;
};
Loading

0 comments on commit 01b547e

Please sign in to comment.