Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use WebSocket provider in ETH chain service and upgrade nitro-protocol #124

Merged
merged 7 commits into from
Sep 27, 2023
Merged
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
4 changes: 2 additions & 2 deletions packages/example-web-app/src/App.tsx
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import React, { useEffect } from 'react';
import assert from 'assert';

import { utils } from '@cerc-io/nitro-node';
import { JSONbigNative, hex2Bytes, DEFAULT_CHAIN_URL } from '@cerc-io/nitro-util';
import { JSONbigNative, hex2Bytes, DEFAULT_CHAIN_URL_WEBSOCKET } from '@cerc-io/nitro-util';

import contractAddresses from './nitro-addresses.json';
import logo from './logo.svg';
@@ -37,7 +37,7 @@ window.setupNode = async (name: string): Promise<utils.Nitro> => {

const nitro = await utils.Nitro.setupNode(
actor.privateKey,
DEFAULT_CHAIN_URL,
DEFAULT_CHAIN_URL_WEBSOCKET,
actor.chainPrivateKey,
contractAddresses,
peer,
2 changes: 1 addition & 1 deletion packages/nitro-node/package.json
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@
},
"dependencies": {
"@cerc-io/libp2p": "0.42.2-laconic-0.1.4",
"@cerc-io/nitro-protocol": "^2.0.0-alpha.4-ts-port-0.1.2",
"@cerc-io/nitro-protocol": "^2.0.0-alpha.4-ts-port-0.1.3",
"@cerc-io/nitro-util": "^0.1.11",
"@cerc-io/peer": "^0.2.58",
"@cerc-io/ts-channel": "1.0.3-ts-nitro-0.1.1",
8 changes: 5 additions & 3 deletions packages/nitro-node/src/channel/channel.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import assert from 'assert';
import _ from 'lodash';
import { Buffer } from 'buffer';
import { ethers } from 'ethers';

import {
fromJSON, toJSON, FieldDescription, Uint, Uint64, NitroSigner,
} from '@cerc-io/nitro-util';
import { Bytes32 } from '@cerc-io/nitro-protocol';

import { Signature } from '../crypto/signatures';
import { Destination } from '../types/destination';
@@ -29,7 +31,7 @@ interface ConstructorOptions extends FixedPartConstructorOptions {
interface OnChainDataConstructorOptions {
holdings?: Funds;
outcome?: Exit;
stateHash?: string
stateHash?: Bytes32
}

interface OffChainDataConstructorOptions {
@@ -42,15 +44,15 @@ class OnChainData {

outcome: Exit = new Exit();

stateHash: string = '';
stateHash: Bytes32 = ethers.utils.hexZeroPad([], 32);

constructor(params: OnChainDataConstructorOptions) {
Object.assign(this, params);
}

static jsonEncodingMap: Record<string, FieldDescription> = {
holdings: { type: 'class', value: Funds },
myIndex: { type: 'class', value: Exit },
outcome: { type: 'class', value: Exit },
stateHash: { type: 'string' },
};

3 changes: 2 additions & 1 deletion packages/nitro-node/src/channel/state/state.ts
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import {
getChannelId as utilGetChannelId,
State as NitroState,
hashState as utilHashState,
Bytes32,
} from '@cerc-io/nitro-protocol';
import {
FieldDescription, NitroSigner, Uint64, bytes2Hex, fromJSON, hex2Bytes, toJSON,
@@ -191,7 +192,7 @@ export class State {
}

// Hash returns the keccak256 hash of the State
hash(): string {
hash(): Bytes32 {
// Use hashState method from @statechannels/nitro-protocol
// Create NitroState instance from State
const state: NitroState = this._getNitroState();
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ import { ethers } from 'ethers';
import type { ReadChannel } from '@cerc-io/ts-channel';

import { Uint64 } from '@cerc-io/nitro-util';
import { Bytes32 } from '@cerc-io/nitro-protocol';

import { ChainTransaction, Objective } from '../../../protocols/interfaces';
import { Address } from '../../../types/types';
@@ -156,7 +157,7 @@ export class ChallengeRegisteredEvent extends CommonEvent {
}

// StateHash returns the statehash stored on chain at the time of the ChallengeRegistered Event firing.
stateHash(fp: FixedPart): string {
stateHash(fp: FixedPart): Bytes32 {
return stateFromFixedAndVariablePart(fp, this.canditate!).hash();
}

Original file line number Diff line number Diff line change
@@ -707,6 +707,9 @@ export class EthChainService implements ChainService {

async close(): Promise<void> {
this.cancel();
if (this.chain.provider instanceof providers.WebSocketProvider) {
await this.chain.provider.destroy();
}
await this.wg!.wait();
}
}
2 changes: 1 addition & 1 deletion packages/nitro-util/package.json
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@
"yargs": "^17.7.2"
},
"dependencies": {
"@cerc-io/nitro-protocol": "^2.0.0-alpha.4-ts-port-0.1.2",
"@cerc-io/nitro-protocol": "^2.0.0-alpha.4-ts-port-0.1.3",
"@cerc-io/ts-channel": "1.0.3-ts-nitro-0.1.1",
"assert": "^2.0.0",
"debug": "^4.3.4",
11 changes: 9 additions & 2 deletions packages/nitro-util/scripts/deploy-contracts.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import yargs from 'yargs';
import fs from 'fs';
import path from 'path';
import debug from 'debug';
import { providers } from 'ethers';
import { ethers, providers } from 'ethers';

import { DEFAULT_CHAIN_URL } from '../src/constants';
import { deployContracts } from '../src/deploy-contracts';
@@ -24,17 +24,24 @@ const getArgv = () => yargs.parserConfiguration({
describe: 'JSON file path to export addresses to',
default: './nitro-addresses.json',
},
key: {
alias: 'k',
type: 'string',
describe: 'Private key of deployer account',
},
}).argv;

async function main() {
const argv = getArgv();

const provider = new providers.JsonRpcProvider(argv.chainurl);
const signer = argv.key ? new ethers.Wallet(argv.key, provider) : provider.getSigner();

const [
nitroAdjudicatorAddress,
virtualPaymentAppAddress,
consensusAppAddress,
] = await deployContracts(provider.getSigner());
] = await deployContracts(signer);

const output = {
nitroAdjudicatorAddress,
11 changes: 9 additions & 2 deletions packages/nitro-util/scripts/deploy-token.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import yargs from 'yargs';
import debug from 'debug';
import { providers } from 'ethers';
import { ethers, providers } from 'ethers';

import { DEFAULT_CHAIN_URL } from '../src';
import { deployToken } from '../src/deploy-contracts';
@@ -17,13 +17,20 @@ const getArgv = () => yargs.parserConfiguration({
describe: 'RPC endpoint for the chain',
default: DEFAULT_CHAIN_URL,
},
key: {
alias: 'k',
type: 'string',
describe: 'Private key of deployer account',
},
}).argv;

async function main() {
const argv = getArgv();

const provider = new providers.JsonRpcProvider(argv.chainurl);
const tokenAddress = await deployToken(provider.getSigner(), tokenArtifact);
const signer = argv.key ? new ethers.Wallet(argv.key, provider) : provider.getSigner();

const tokenAddress = await deployToken(signer, tokenArtifact);

log('Token deployed to:', tokenAddress);
}
1 change: 1 addition & 0 deletions packages/nitro-util/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// RPC endpoint for the chain
// Chain should be running locally
export const DEFAULT_CHAIN_URL = 'http://127.0.0.1:8545';
export const DEFAULT_CHAIN_URL_WEBSOCKET = 'ws://127.0.0.1:8545';

// Zero address (ETH)
export const DEFAULT_ASSET = `0x${'00'.repeat(20)}`;
6 changes: 3 additions & 3 deletions packages/nitro-util/src/deploy-contracts.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ContractFactory, providers } from 'ethers';
import { ContractFactory, Signer } from 'ethers';

import nitroAdjudicatorArtifact from '@cerc-io/nitro-protocol/dist/artifacts/contracts/NitroAdjudicator.sol/NitroAdjudicator.json';
import consensusAppArtifact from '@cerc-io/nitro-protocol/dist/artifacts/contracts/ConsensusApp.sol/ConsensusApp.json';
import virtualPaymentAppArtifact from '@cerc-io/nitro-protocol/dist/artifacts/contracts/VirtualPaymentApp.sol/VirtualPaymentApp.json';

export async function deployContracts(signer: providers.JsonRpcSigner): Promise<[string, string, string]> {
export async function deployContracts(signer: Signer): Promise<[string, string, string]> {
const nitroAdjudicatorFactory = new ContractFactory(
nitroAdjudicatorArtifact.abi,
nitroAdjudicatorArtifact.bytecode,
@@ -29,7 +29,7 @@ export async function deployContracts(signer: providers.JsonRpcSigner): Promise<
return [nitroAdjudicator.address, virtualPaymentApp.address, consensusApp.address];
}

export async function deployToken(signer: providers.JsonRpcSigner, artifact: any): Promise<string> {
export async function deployToken(signer: Signer, artifact: any): Promise<string> {
const tokenFactory = new ContractFactory(
artifact.abi,
artifact.bytecode,
2 changes: 1 addition & 1 deletion packages/nitro-util/src/eth-client.ts
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ export class EthClient {

static dial(chainUrl: string): EthClient {
// Connect to the Ethereum provider
const provider = new ethers.providers.JsonRpcProvider(chainUrl);
const provider = new ethers.providers.WebSocketProvider(chainUrl);
return new EthClient(provider);
}

1 change: 0 additions & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
@@ -28,7 +28,6 @@
"cli": "DEBUG=ts-nitro:* ts-node src/index.ts",
"lint": "eslint .",
"test:e2e": "mocha test-e2e/**/*.test.ts --bail",
"test:deploy-contracts": "DEBUG=ts-nitro:* yarn ts-node test-e2e/scripts/deploy-contracts.ts",
"chain": "hardhat node",
"chain:anvil": "hardhat --config hardhat.anvil.config.ts node"
},
4 changes: 2 additions & 2 deletions packages/server/src/index.ts
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ import 'dotenv/config';

import { utils } from '@cerc-io/nitro-node';
import {
JSONbigNative, hex2Bytes, DEFAULT_CHAIN_URL, DEFAULT_ASSET,
JSONbigNative, hex2Bytes, DEFAULT_CHAIN_URL_WEBSOCKET, DEFAULT_ASSET,
} from '@cerc-io/nitro-util';

import { waitForMultiplePeers } from './utils/index';
@@ -40,7 +40,7 @@ const getArgv = () => yargs.parserConfiguration({
alias: 'c',
type: 'string',
describe: 'RPC endpoint for the chain',
default: DEFAULT_CHAIN_URL,
default: DEFAULT_CHAIN_URL_WEBSOCKET,
},
contracts: {
type: 'string',
4 changes: 2 additions & 2 deletions packages/server/test-e2e/integration.test.ts
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ import {
ChannelStatus, LedgerChannelBalance, PaymentChannelInfo, PaymentChannelBalance, ObjectiveResponse,
} from '@cerc-io/nitro-node';
import {
hex2Bytes, DEFAULT_CHAIN_URL, getBalanceByKey, getBalanceByAddress, deployContracts,
hex2Bytes, DEFAULT_CHAIN_URL, DEFAULT_CHAIN_URL_WEBSOCKET, getBalanceByKey, getBalanceByAddress, deployContracts,
} from '@cerc-io/nitro-util';

import {
@@ -53,7 +53,7 @@ async function createNode(actor: utils.Actor, contractAddresses: ContractAddress

const nitro = await utils.Nitro.setupNode(
actor.privateKey,
DEFAULT_CHAIN_URL,
DEFAULT_CHAIN_URL_WEBSOCKET,
actor.chainPrivateKey,
contractAddresses,
nodePeer,
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -1188,10 +1188,10 @@
wherearewe "^2.0.0"
xsalsa20 "^1.1.0"

"@cerc-io/nitro-protocol@^2.0.0-alpha.4-ts-port-0.1.2":
version "2.0.0-alpha.4-ts-port-0.1.2"
resolved "https://git.vdb.to/api/packages/cerc-io/npm/%40cerc-io%2Fnitro-protocol/-/2.0.0-alpha.4-ts-port-0.1.2/nitro-protocol-2.0.0-alpha.4-ts-port-0.1.2.tgz#6d2f893f5aa08dd5550447f04967b908f3f6d469"
integrity sha512-Cyx2+S/6BlAzvl+LZxwLjK2Y0H01f/kvTYUktdsGHx1eTWXTzS6FQ0nTVwJkKEcO8V/Y50+dc2PwvFXvk8iG9w==
"@cerc-io/nitro-protocol@^2.0.0-alpha.4-ts-port-0.1.3":
version "2.0.0-alpha.4-ts-port-0.1.3"
resolved "https://git.vdb.to/api/packages/cerc-io/npm/%40cerc-io%2Fnitro-protocol/-/2.0.0-alpha.4-ts-port-0.1.3/nitro-protocol-2.0.0-alpha.4-ts-port-0.1.3.tgz#586c56ba696857f5cb13cf988277933b1e163dab"
integrity sha512-jDZLpw2fvLGqFMNodLkykn/qTEdEyNhxRloKVgdWU973CScV85Q/j5QDsDA87aF4iDOqrgGJkgIs36+MDcXngA==
dependencies:
"@openzeppelin/contracts" "^4.7.3"
"@statechannels/exit-format" "^0.2.0"