diff --git a/packages/portalnetwork/test/wire/utp/utp.spec.ts b/packages/portalnetwork/test/wire/utp/utp.spec.ts index 908f900ef..f4924dda9 100644 --- a/packages/portalnetwork/test/wire/utp/utp.spec.ts +++ b/packages/portalnetwork/test/wire/utp/utp.spec.ts @@ -3,11 +3,13 @@ import debug from 'debug' import { assert, describe, it } from 'vitest' import { + FindContentReadRequest, NetworkId, Packet, PacketType, PortalNetwork, PortalNetworkUTP, + ReadSocket, RequestCode, UtpSocketType, encodeWithVariantPrefix, @@ -19,6 +21,7 @@ import { ContentWriter } from '../../../src/wire/utp/Socket/ContentWriter.js' import { WriteSocket } from '../../../src/wire/utp/Socket/WriteSocket.js' import { ENR } from '@chainsafe/enr' +import { RequestManager } from '../../../src/wire/utp/PortalNetworkUtp/requestManager.js' const sampleSize = 50000 const enr = ENR.decodeTxt( @@ -211,3 +214,76 @@ describe('PortalNetworkUTP test', async () => { ) }) }) +describe('RequestManager', () => { + it('should order packets correctly', async () => { + const client = await PortalNetwork.create({ + bindAddress: enr.getLocationMultiaddr('udp')!.nodeAddress().address, + }) + const mgr = new RequestManager(enr.nodeId, debug('test')) + const req1 = new FindContentReadRequest({ + network: client.network()['0x500b']!, + socket: new ReadSocket({ + utp: client.uTP, + networkId: NetworkId.HistoryNetwork, + enr, + connectionId: 0, + sndId: 0, + rcvId: 0, + seqNr: 0, + ackNr: 0, + type: UtpSocketType.READ, + logger: debug('test'), + }), + connectionId: 0, + requestManager: mgr, + requestCode: RequestCode.FINDCONTENT_READ, + contentKeys: [], + content: Buffer.from('test'), + }) + const packet1 = Packet.fromOpts({ + header: { + seqNr: 0, + pType: PacketType.ST_DATA, + version: 1, + connectionId: 0, + extension: 0, + timestampMicroseconds: 0, + timestampDifferenceMicroseconds: 0, + wndSize: 0, + ackNr: 123, + }, + }) + const packet2 = Packet.fromOpts({ + header: { + seqNr: 0, + pType: PacketType.ST_SYN, + version: 1, + connectionId: 0, + extension: 0, + timestampMicroseconds: 0, + timestampDifferenceMicroseconds: 0, + wndSize: 0, + ackNr: 121, + }, + }) + const packet3 = Packet.fromOpts({ + header: { + seqNr: 0, + pType: PacketType.ST_DATA, + version: 1, + connectionId: 0, + extension: 0, + timestampMicroseconds: 0, + timestampDifferenceMicroseconds: 0, + wndSize: 0, + ackNr: 125, + }, + }) + void mgr.handleNewRequest(req1.connectionId, req1) + mgr.masterPacketQueue.push(packet2) + mgr.currentPacket = packet3 + void mgr.handlePacket(packet1.encode()) + assert.equal(mgr.masterPacketQueue.length, 2) + assert.deepEqual(mgr.masterPacketQueue[0], packet2) + }) +})