Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dguenther committed Nov 10, 2023
1 parent c6859f1 commit 5143935
Show file tree
Hide file tree
Showing 24 changed files with 118 additions and 54 deletions.
8 changes: 4 additions & 4 deletions config/tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"compilerOptions": {
"target": "ES2020",
"lib": ["dom", "dom.iterable", "ES2020"],
"target": "ES2022",
"lib": ["DOM", "ES2022"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "commonjs",
"moduleResolution": "node",
"module": "Node16",
"moduleResolution": "Node16",
"sourceMap": true,
"resolveJsonModule": true,
"composite": true,
Expand Down
4 changes: 2 additions & 2 deletions ironfish-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@oclif/test": "2.1.0",
"@types/blessed": "0.1.17",
"@types/inquirer": "8.2.5",
"@types/node": "18.11.16",
"@types/node": "20.9.0",
"@types/tar": "6.1.1",
"chai": "4.2.0",
"cross-env": "7.0.3",
Expand All @@ -37,7 +37,7 @@
"oclif": "2.6.0",
"rimraf": "^3.0.2",
"tsc-watch": "4.2.9",
"typescript": "4.3.4",
"typescript": "4.7.4",
"yarn": "^1.22.10"
},
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion ironfish-cli/src/commands/ceremony/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,15 @@ export default class Ceremony extends IronfishCommand {
try {
response = await axios.get(downloadLink, {
responseType: 'stream',
onDownloadProgress: (p: ProgressEvent) => {
onDownloadProgress: (p) => {
this.log('loaded', p.loaded, 'total', p.total)
},
})
} catch (e) {
this.error(ErrorUtils.renderError(e))
}

// @ts-expect-error Would rather comment out the type error than change the code to fix it
await pipeline(response.data, fileHandle.createWriteStream())

CliUx.ux.action.stop()
Expand Down
1 change: 1 addition & 0 deletions ironfish-cli/src/commands/restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ async function downloadFileTo(

data.on('error', (err) => reject(err))
data.on('end', () => resolve())
// @ts-expect-error Would rather comment out the type error than change the code to fix it
data.pipe(writer)

return promise
Expand Down
1 change: 1 addition & 0 deletions ironfish-cli/src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ async function matchesChecksum(file: string, checksum: string): Promise<boolean>
const stream = fs.createReadStream(file)
stream.on('end', resolve)
stream.on('error', reject)
// @ts-expect-error Would rather comment out the type error than change the code to fix it
stream.pipe(hasher, { end: false })
})

Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions ironfish-cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import { Interfaces } from '@oclif/core'

export interface ProgressBar {
progress: VoidFunction
progress: () => void
getTotal(): number
setTotal(totalValue: number): void
start(totalValue?: number, startValue?: number, payload?: Record<string, unknown>): void
stop: VoidFunction
stop: () => void
update(currentValue?: number, payload?: Record<string, unknown>): void
update(payload?: Record<string, unknown>): void
increment(delta?: number, payload?: Record<string, unknown>): void
Expand Down
3 changes: 2 additions & 1 deletion ironfish-cli/src/utils/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ async function downloadFromBucket(
if (response.Body) {
const fileHandle = await fsAsync.open(output, 'w')
const ws = fileHandle.createWriteStream()


// @ts-expect-error Would rather comment out the type error than change the code to fix it
await pipeline(response.Body as Readable, ws)

ws.close()
Expand Down
1 change: 1 addition & 0 deletions ironfish-cli/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "../config/tsconfig.base.json",
"compilerOptions": {
"lib": ["ES2022"],
"outDir": "build",
"rootDir": "./",
"tsBuildInfoFile": "./build/tsconfig.tsbuildinfo"
Expand Down
2 changes: 1 addition & 1 deletion ironfish-rust-nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@
"jest-jasmine2": "29.3.1",
"rimraf": "3.0.2",
"ts-jest": "29.0.3",
"typescript": "4.3.4"
"typescript": "4.7.4"
}
}
5 changes: 3 additions & 2 deletions ironfish/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@
"leveldown": "5.6.0",
"levelup": "4.4.0",
"lodash": "4.17.21",
"node-datachannel": "0.4.3",
"node-datachannel": "0.5.0",
"node-forge": "1.3.1",
"parse-json": "5.2.0",
"sqlite": "4.0.23",
"sqlite3": "5.1.6",
"tsimportlib": "0.0.5",
"uuid": "8.3.2",
"ws": "8.12.1",
"yup": "0.29.3"
Expand Down Expand Up @@ -95,7 +96,7 @@
"prettier": "2.3.2",
"ts-jest": "29.0.3",
"ts-node": "10.9.1",
"typescript": "4.3.5"
"typescript": "4.7.4"
},
"bugs": {
"url": "https://github.com/iron-fish/ironfish/issues"
Expand Down
4 changes: 3 additions & 1 deletion ironfish/src/network/peerNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import { BAN_SCORE, KnownBlockHashesValue, Peer } from './peers/peer'
import { PeerConnectionManager } from './peers/peerConnectionManager'
import { PeerManager } from './peers/peerManager'
import { TransactionFetcher } from './transactionFetcher'
import { IsomorphicWebSocketConstructor } from './types'
import { IsomorphicWebSocketConstructor, NodeDataChannelType } from './types'
import { getBlockSize } from './utils/serializers'
import { parseUrl, WebSocketAddress } from './utils/url'
import {
Expand Down Expand Up @@ -159,6 +159,7 @@ export class PeerNetwork {
identity: PrivateIdentity
agent?: string
webSocket: IsomorphicWebSocketConstructor
nodeDataChannel: NodeDataChannelType
listen?: boolean
port?: number
bootstrapNodes?: string[]
Expand Down Expand Up @@ -194,6 +195,7 @@ export class PeerNetwork {
VERSION_PROTOCOL,
options.chain,
options.webSocket,
options.nodeDataChannel,
options.networkId,
this.enableSyncing,
)
Expand Down
15 changes: 9 additions & 6 deletions ironfish/src/network/peers/connections/webRtcConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

import type { Logger } from '../../../logger'
// @ts-expect-error Allow type-only import https://github.com/microsoft/TypeScript/issues/49721
import type { DataChannel, DescriptionType, PeerConnection } from 'node-datachannel'
import colors from 'colors/safe'
import nodeDataChannel from 'node-datachannel'
import { Assert } from '../../../assert'
import { Event } from '../../../event'
import { MetricsMonitor } from '../../../metrics'
import { ErrorUtils } from '../../../utils'
import { parseNetworkMessage } from '../../messageRegistry'
import { NodeDataChannelType } from '../../types'
import { MAX_MESSAGE_SIZE } from '../../version'
import { Connection, ConnectionDirection, ConnectionType } from './connection'
import { NetworkError } from './errors'

export type SignalData =
| {
type: nodeDataChannel.DescriptionType
type: DescriptionType,
sdp: string
}
| CandidateSignal
Expand All @@ -35,8 +37,8 @@ type CandidateSignal = {
* LooseMessages instead of strings/data.
*/
export class WebRtcConnection extends Connection {
private readonly peer: nodeDataChannel.PeerConnection
private datachannel: nodeDataChannel.DataChannel | null = null
private readonly peer: PeerConnection
private datachannel: DataChannel | null = null

/**
* True if we've received an SDP message from the peer.
Expand All @@ -55,6 +57,7 @@ export class WebRtcConnection extends Connection {
onSignal = new Event<[SignalData]>()

constructor(
nodeDataChannel: NodeDataChannelType,
initiator: boolean,
logger: Logger,
metrics?: MetricsMonitor,
Expand Down Expand Up @@ -102,7 +105,7 @@ export class WebRtcConnection extends Connection {
})
})

this.peer.onDataChannel((dc: nodeDataChannel.DataChannel) => {
this.peer.onDataChannel((dc: DataChannel) => {
Assert.isNull(this.datachannel)
this.initializeDataChannel(dc)
})
Expand All @@ -112,7 +115,7 @@ export class WebRtcConnection extends Connection {
}
}

initializeDataChannel(dc: nodeDataChannel.DataChannel): void {
initializeDataChannel(dc: DataChannel): void {
this.datachannel = dc

this.datachannel.onOpen(() => {
Expand Down
6 changes: 5 additions & 1 deletion ironfish/src/network/peers/localPeer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Assert } from '../../assert'
import { Blockchain } from '../../blockchain'
import { Identity, PrivateIdentity, privateIdentityToIdentity } from '../identity'
import { IdentifyMessage } from '../messages/identify'
import { IsomorphicWebSocketConstructor } from '../types'
import { IsomorphicWebSocketConstructor, NodeDataChannelType } from '../types'

/**
* Wraps configuration needed for establishing connections with other peers
Expand All @@ -24,6 +24,8 @@ export class LocalPeer {
readonly version: number
// constructor for either a Node WebSocket or a browser WebSocket
readonly webSocket: IsomorphicWebSocketConstructor
// asynchronously imported WebRTC datachannel library
readonly nodeDataChannel: NodeDataChannelType
// the unique ID number of the network
readonly networkId: number
// true if the peer supports syncing and gossip messages
Expand All @@ -40,6 +42,7 @@ export class LocalPeer {
version: number,
chain: Blockchain,
webSocket: IsomorphicWebSocketConstructor,
nodeDataChannel: NodeDataChannelType,
networkId: number,
enableSyncing: boolean,
) {
Expand All @@ -52,6 +55,7 @@ export class LocalPeer {
this.enableSyncing = enableSyncing

this.webSocket = webSocket
this.nodeDataChannel = nodeDataChannel
this.port = null
this.name = null
}
Expand Down
32 changes: 23 additions & 9 deletions ironfish/src/network/peers/peerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { PeerListMessage } from '../messages/peerList'
import { PeerListRequestMessage } from '../messages/peerListRequest'
import { SignalMessage } from '../messages/signal'
import { SignalRequestMessage } from '../messages/signalRequest'
import { IsomorphicWebSocket } from '../types'
import { IsomorphicWebSocket, NodeDataChannelType } from '../types'
import { formatWebSocketAddress, WebSocketAddress } from '../utils'
import { VERSION_PROTOCOL_MIN } from '../version'
import { ConnectionRetry } from './connectionRetry'
Expand Down Expand Up @@ -275,7 +275,7 @@ export class PeerManager {
}

if (canInitiateWebRTC(this.localPeer.publicIdentity, peer.state.identity)) {
this.initWebRtcConnection(peer, true)
this.initWebRtcConnection(peer, this.localPeer.nodeDataChannel, true)
return true
}

Expand All @@ -284,7 +284,7 @@ export class PeerManager {
destinationIdentity: peer.state.identity,
})

const connection = this.initWebRtcConnection(peer, false)
const connection = this.initWebRtcConnection(peer, this.localPeer.nodeDataChannel, false)
connection.setState({ type: 'REQUEST_SIGNALING' })

const brokeringPeers = this.getBrokeringPeers(peer)
Expand Down Expand Up @@ -335,10 +335,20 @@ export class PeerManager {
* @param peer The peer to establish a connection with
* @param initiator Set to true if we are initiating a connection with `peer`
*/
private initWebRtcConnection(peer: Peer, initiator: boolean): WebRtcConnection {
const connection = new WebRtcConnection(initiator, this.logger, this.metrics, {
stunServers: this.stunServers,
})
private initWebRtcConnection(
peer: Peer,
nodeDataChannel: NodeDataChannelType,
initiator: boolean,
): WebRtcConnection {
const connection = new WebRtcConnection(
nodeDataChannel,
initiator,
this.logger,
this.metrics,
{
stunServers: this.stunServers,
},
)

connection.onSignal.on((data) => {
let errorMessage
Expand Down Expand Up @@ -1258,7 +1268,7 @@ export class PeerManager {
return
}

this.initWebRtcConnection(targetPeer, true)
this.initWebRtcConnection(targetPeer, this.localPeer.nodeDataChannel, true)
}

/**
Expand Down Expand Up @@ -1363,7 +1373,11 @@ export class PeerManager {
return
}

signalingConnection = this.initWebRtcConnection(signalingPeer, false)
signalingConnection = this.initWebRtcConnection(
signalingPeer,
this.localPeer.nodeDataChannel,
false,
)
} else {
signalingConnection = signalingPeer.state.connections.webRtc
}
Expand Down
14 changes: 13 additions & 1 deletion ironfish/src/network/testUtilities/mockLocalPeer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import { Blockchain } from '../../blockchain'
import { mockChain } from '../../testUtilities/mocks'
import { PrivateIdentity } from '../identity'
import { LocalPeer } from '../peers/localPeer'
import { NodeDataChannelType } from '../types'
import { VERSION_PROTOCOL } from '../version'
import { WebSocketClient } from '../webSocketClient'
import { mockPrivateIdentity } from './mockPrivateIdentity'

const mockNodeDataChannel: NodeDataChannelType = {} as unknown as NodeDataChannelType

/**
* Utility to create a fake "keypair" for testing the network layer
*/
Expand All @@ -24,5 +27,14 @@ export function mockLocalPeer({
version?: number
chain?: Blockchain
} = {}): LocalPeer {
return new LocalPeer(identity, agent, version, chain || mockChain(), WebSocketClient, 0, true)
return new LocalPeer(
identity,
agent,
version,
chain || mockChain(),
WebSocketClient,
mockNodeDataChannel,
0,
true,
)
}
3 changes: 3 additions & 0 deletions ironfish/src/network/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
// @ts-expect-error Allow type-only import https://github.com/microsoft/TypeScript/issues/49721
import type nodeDataChannel from 'node-datachannel'
import type { ErrorEvent as WSErrorEvent } from 'ws'
import { WebSocketClient } from './webSocketClient'

Expand Down Expand Up @@ -34,6 +36,7 @@ export enum NetworkMessageType {
NewTransactions = 83,
}

export type NodeDataChannelType = typeof nodeDataChannel
export type IsomorphicWebSocketConstructor = typeof WebSocket | typeof WebSocketClient
export type IsomorphicWebSocket = WebSocket | WebSocketClient
export type IsomorphicWebSocketErrorEvent = WSErrorEvent
2 changes: 1 addition & 1 deletion ironfish/src/network/webSocketClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { WEBSOCKET_OPTIONS } from './webSocketServer'
*/
export class WebSocketClient extends WSWebSocket {
constructor(
address: string | URL,
address: string,
protocols?: string | string[],
options?: WSWebSocket.ClientOptions | http.ClientRequestArgs,
) {
Expand Down
Loading

0 comments on commit 5143935

Please sign in to comment.