Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottyPoi committed Aug 5, 2023
1 parent 3f92a20 commit d81ab41
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 52 deletions.
15 changes: 6 additions & 9 deletions packages/portalnetwork/src/wire/utp/PortalNetworkUtp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,15 @@ export class PortalNetworkUTP extends EventEmitter {
getRequestKey(connId: number, peerId: string): string {
const idA = connId + 1
const idB = connId - 1
const keyA = createSocketKey(peerId, connId, idA)
const keyB = createSocketKey(peerId, idA, connId)
const keyC = createSocketKey(peerId, idB, connId)
const keyD = createSocketKey(peerId, connId, idB)
for (const key of [keyA, keyB, keyC, keyD]) {
const keyA = createSocketKey(peerId, connId)
const keyB = createSocketKey(peerId, idA)
const keyC = createSocketKey(peerId, idB)
for (const key of [keyA, keyB, keyC]) {
if (this.openContentRequest.get(key) !== undefined) {
return key
}
}
throw new Error(
`Cannot Find Open Request for socketKey ${keyA} or ${keyB} or ${keyC} or ${keyD}`
)
throw new Error(`Cannot Find Open Request for socketKey ${keyA} or ${keyB} or ${keyC}`)
}

createPortalNetworkUTPSocket(
Expand Down Expand Up @@ -116,7 +113,7 @@ export class PortalNetworkUTP extends EventEmitter {
rcvId,
content
),
socketKey: createSocketKey(peerId, sndId, rcvId),
socketKey: createSocketKey(peerId, connectionId),
content: params.contents ? params.contents[0] : undefined,
contentKeys,
})
Expand Down
4 changes: 2 additions & 2 deletions packages/portalnetwork/src/wire/utp/PortalNetworkUtp/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export enum RequestCode {
'ACCEPT_READ' = 3,
}

export function createSocketKey(remoteAddr: string, sndId: number, rcvId: number) {
return `${remoteAddr.slice(0, 5)}-${sndId}-${rcvId}`
export function createSocketKey(remoteAddr: string, id: number) {
return `${remoteAddr}-${id}`
}
export interface INewRequest {
protocolId: ProtocolId
Expand Down
4 changes: 2 additions & 2 deletions packages/portalnetwork/src/wire/utp/Socket/UtpSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class UtpSocket extends EventEmitter {
}

setReader(startingSeqNr: number) {
this.reader = new ContentReader(startingSeqNr)
this.reader = new ContentReader(startingSeqNr - 1)
}

_clearTimeout() {
Expand Down Expand Up @@ -178,7 +178,7 @@ export class UtpSocket extends EventEmitter {
}
const packet = this.createPacket<PacketType.ST_DATA>({
pType: PacketType.ST_DATA,
connectionId: this.sndConnectionId,
connectionId: this.rcvConnectionId,
payload: bytes,
} as ICreateData)
await this.sendPacket<PacketType.ST_DATA>(packet, PacketType.ST_DATA)
Expand Down
2 changes: 1 addition & 1 deletion packages/portalnetwork/test/wire/utp/socket.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ tape('createPacket()', (t) => {
'DATA Packet extension should be none'
)
st.equal(write_data.header.connectionId, read.sndConnectionId, 'Packet sndId correctly set')
st.equal(write_data.header.seqNr, write.getSeqNr(), 'Packet seqNr correctly set')
st.equal(write_data.header.seqNr, write.getSeqNr() - 1, 'Packet seqNr correctly set')
st.equal(write_data.header.ackNr, write.ackNr, 'Packet ackNr correctly set')
st.equal(
toHexString(write_data.payload!),
Expand Down
70 changes: 32 additions & 38 deletions packages/portalnetwork/test/wire/utp/utp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ tape('uTP Reader/Writer tests', (t) => {
extension: HeaderExtension.none,
version: 1,
seqNr: 100,
connectionId: socket.sndConnectionId,
ackNr: socket.ackNr + 98,
connectionId: socket.rcvConnectionId,
ackNr: socket.ackNr,
timestampMicroseconds: packets.length * 200,
timestampDifferenceMicroseconds: 200,
wndSize: 512,
Expand All @@ -143,9 +143,18 @@ tape('uTP Reader/Writer tests', (t) => {
packets.forEach((packet) => {
socket.reader!.addPacket(packet)
})
st.equal(packets.length, socket.reader.packets.length, 'Packets added to reader')
st.equal(packets.length, socket.reader.inOrder.length, 'Packets added in order')
const _compiled = await socket.handleFinPacket(finPacket)
st.equal(
packets.length,
socket.reader.packets.length,
`${packets.length} Packets added to reader`
)
st.equal(
packets.length,
socket.reader.inOrder.length,
`${packets.length} Packets added in order`
)
await socket.handleFinPacket(finPacket)
const _compiled = await socket.reader.run()

st.deepEqual(Buffer.from(_compiled!), content, `Content Reader correctly recompiled content`)
const _reader2 = new ContentReader(2)
Expand Down Expand Up @@ -173,25 +182,26 @@ tape('uTP Reader/Writer tests', (t) => {
)
})

const connectionId = 1234
const _socket2 = uTP.createPortalNetworkUTPSocket(
protocolId,

RequestCode.FOUNDCONTENT_WRITE,
_peerId.toString(),
5678,
1234,
uTP.startingIdNrs(connectionId)[RequestCode.FOUNDCONTENT_WRITE].sndId,
uTP.startingIdNrs(connectionId)[RequestCode.FOUNDCONTENT_WRITE].rcvId,
content
)

const socketKey = createSocketKey(peerId.toString(), 1234, 5678)
const socketKey = createSocketKey(peerId.toString(), connectionId)
const contents = [encodeWithVariantPrefix(offerContents)]
const offer_socket = uTP.createPortalNetworkUTPSocket(
protocolId,

RequestCode.OFFER_WRITE,
peerId.toString(),
1234,
5678,
uTP.startingIdNrs(connectionId)[RequestCode.OFFER_WRITE].sndId,
uTP.startingIdNrs(connectionId)[RequestCode.OFFER_WRITE].rcvId,
contents[0]
)!
const offer = new ContentRequest({
Expand Down Expand Up @@ -240,8 +250,8 @@ tape('PortalNetworkUTP test', (t) => {
Buffer.from('test')
)
st.ok(socket, 'UTPSocket created by PortalNetworkUTP')
st.equal(socket.sndConnectionId, connectionId, 'UTPSocket has correct sndConnectionId')
st.equal(socket.rcvConnectionId, connectionId + 1, 'UTPSocket has correct rcvConnectionId')
st.equal(socket.sndConnectionId, connectionId + 1, 'UTPSocket has correct sndConnectionId')
st.equal(socket.rcvConnectionId, connectionId, 'UTPSocket has correct rcvConnectionId')
st.equal(socket.remoteAddress, '0xPeerAddress', 'UTPSocket has correct peerId')
st.equal(socket.type, UtpSocketType.WRITE, 'UTPSocket has correct requestCode')
st.deepEqual(socket.content, Buffer.from('test'), 'UTPSocket has correct content')
Expand All @@ -258,8 +268,8 @@ tape('PortalNetworkUTP test', (t) => {
socketIds[RequestCode.FINDCONTENT_READ].rcvId
)
st.equal(socket.type, UtpSocketType.READ, 'UTPSocket has correct requestCode')
st.equal(socket.sndConnectionId, connectionId + 1, 'UTPSocket has correct sndConnectionId')
st.equal(socket.rcvConnectionId, connectionId, 'UTPSocket has correct rcvConnectionId')
st.equal(socket.sndConnectionId, connectionId, 'UTPSocket has correct sndConnectionId')
st.equal(socket.rcvConnectionId, connectionId + 1, 'UTPSocket has correct rcvConnectionId')

st.equal(
socket.getSeqNr(),
Expand All @@ -281,8 +291,8 @@ tape('PortalNetworkUTP test', (t) => {
Buffer.from('test')
)
st.equal(socket.type, UtpSocketType.WRITE, 'UTPSocket has correct requestCode')
st.equal(socket.sndConnectionId, connectionId + 1, 'UTPSocket has correct sndConnectionId')
st.equal(socket.rcvConnectionId, connectionId, 'UTPSocket has correct rcvConnectionId')
st.equal(socket.sndConnectionId, connectionId, 'UTPSocket has correct sndConnectionId')
st.equal(socket.rcvConnectionId, connectionId + 1, 'UTPSocket has correct rcvConnectionId')

st.equal(
socket.getSeqNr(),
Expand All @@ -297,8 +307,8 @@ tape('PortalNetworkUTP test', (t) => {
socketIds[RequestCode.ACCEPT_READ].rcvId
)
st.equal(socket.type, UtpSocketType.READ, 'UTPSocket has correct requestCode')
st.equal(socket.sndConnectionId, connectionId, 'UTPSocket has correct sndConnectionId')
st.equal(socket.rcvConnectionId, connectionId + 1, 'UTPSocket has correct rcvConnectionId')
st.equal(socket.sndConnectionId, connectionId + 1, 'UTPSocket has correct sndConnectionId')
st.equal(socket.rcvConnectionId, connectionId, 'UTPSocket has correct rcvConnectionId')
st.equal(
socket.ackNr,
startingNrs[RequestCode.ACCEPT_READ].ackNr,
Expand All @@ -318,11 +328,7 @@ tape('PortalNetworkUTP test', (t) => {
contents: [fromHexString('0x1234')],
}
let contentRequest = await utp.handleNewRequest(params)
let requestKey = createSocketKey(
params.peerId,
socketIds[RequestCode.FOUNDCONTENT_WRITE].sndId,
socketIds[RequestCode.FOUNDCONTENT_WRITE].rcvId
)
let requestKey = createSocketKey(params.peerId, connectionId)
st.equal(
utp.getRequestKey(params.connectionId, params.peerId),
requestKey,
Expand Down Expand Up @@ -368,11 +374,7 @@ tape('PortalNetworkUTP test', (t) => {
)
params = { ...params, requestCode: RequestCode.FINDCONTENT_READ, contents: undefined }
contentRequest = await utp.handleNewRequest(params)
requestKey = createSocketKey(
params.peerId,
socketIds[RequestCode.FINDCONTENT_READ].sndId,
socketIds[RequestCode.FINDCONTENT_READ].rcvId
)
requestKey = createSocketKey(params.peerId, params.connectionId)
st.equal(
utp.getRequestKey(params.connectionId, params.peerId),
requestKey,
Expand Down Expand Up @@ -413,11 +415,7 @@ tape('PortalNetworkUTP test', (t) => {
contents: [fromHexString('0x1234')],
}
contentRequest = await utp.handleNewRequest(params)
requestKey = createSocketKey(
params.peerId,
socketIds[RequestCode.OFFER_WRITE].sndId,
socketIds[RequestCode.OFFER_WRITE].rcvId
)
requestKey = createSocketKey(params.peerId, params.connectionId)
st.equal(
utp.getRequestKey(params.connectionId, params.peerId),
requestKey,
Expand Down Expand Up @@ -459,11 +457,7 @@ tape('PortalNetworkUTP test', (t) => {
)
params = { ...params, requestCode: RequestCode.ACCEPT_READ, contents: undefined }
contentRequest = await utp.handleNewRequest(params)
requestKey = createSocketKey(
params.peerId,
socketIds[RequestCode.ACCEPT_READ].sndId,
socketIds[RequestCode.ACCEPT_READ].rcvId
)
requestKey = createSocketKey(params.peerId, params.connectionId)
st.equal(
utp.getRequestKey(params.connectionId, params.peerId),
requestKey,
Expand Down

0 comments on commit d81ab41

Please sign in to comment.