Skip to content

Commit b405c2b

Browse files
committed
Fix packet size calculation for normal packets
1 parent 3a41ec8 commit b405c2b

File tree

6 files changed

+44
-10
lines changed

6 files changed

+44
-10
lines changed

packages/portalnetwork/src/networks/history/history.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ import {
1717
FoundContent,
1818
HistoricalSummariesBlockProof,
1919
HistoryRadius,
20-
MAX_PACKET_SIZE,
20+
MAX_UDP_PACKET_SIZE,
2121
MessageCodes,
2222
PingPongPayloadExtensions,
2323
PortalWireMessageType,
2424
RequestCode,
2525
decodeHistoryNetworkContentKey,
2626
decodeReceipts,
2727
encodeClientInfo,
28+
getTalkReqOverhead,
2829
randUint16,
2930
reassembleBlock,
3031
saveReceipts,
@@ -439,7 +440,7 @@ export class HistoryNetwork extends BaseNetwork {
439440
const firstHeaderNumber = this.ephemeralHeaderIndex.getByValue(
440441
bytesToHex(contentKey.keyOpt.blockHash),
441442
)
442-
for (let x = 1; x < contentKey.keyOpt.ancestorCount; x++) {
443+
for (let x = 1; x <= contentKey.keyOpt.ancestorCount; x++) {
443444
// Determine if we have the ancestor header at block number `firstHeaderNumber - x`
444445
const ancestorNumber = firstHeaderNumber! - BigInt(x)
445446
const ancestorHash = this.ephemeralHeaderIndex.getByKey(ancestorNumber)
@@ -459,7 +460,7 @@ export class HistoryNetwork extends BaseNetwork {
459460
}
460461
}
461462
this.logger.extend('FOUNDCONTENT')(
462-
`found ${headersList.length} ancestor headers for ${bytesToHex(contentKey.keyOpt.blockHash)}`,
463+
`found ${headersList.length - 1} ancestor headers for ${bytesToHex(contentKey.keyOpt.blockHash)}`,
463464
)
464465
value = EphemeralHeaderPayload.serialize(headersList)
465466
}
@@ -468,7 +469,10 @@ export class HistoryNetwork extends BaseNetwork {
468469
}
469470
if (!value) {
470471
await this.enrResponse(decodedContentMessage.contentKey, src, requestId)
471-
} else if (value instanceof Uint8Array && value.length < MAX_PACKET_SIZE) {
472+
} else if (
473+
value instanceof Uint8Array &&
474+
value.length < MAX_UDP_PACKET_SIZE - getTalkReqOverhead(hexToBytes(this.networkId).byteLength)
475+
) {
472476
this.logger.extend('FOUNDCONTENT')(
473477
'Found value for requested content ' +
474478
bytesToHex(decodedContentMessage.contentKey) +

packages/portalnetwork/src/networks/network.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {
3535
ContentMessageType,
3636
ErrorPayload,
3737
HistoryRadius,
38-
MAX_PACKET_SIZE,
38+
MAX_UDP_PACKET_SIZE,
3939
MessageCodes,
4040
NodeLookup,
4141
PingPongErrorCodes,
@@ -48,6 +48,7 @@ import {
4848
encodeClientInfo,
4949
encodeWithVariantPrefix,
5050
generateRandomNodeIdAtDistance,
51+
getTalkReqOverhead,
5152
randUint16,
5253
shortId,
5354
} from '../index.js'
@@ -762,7 +763,10 @@ export abstract class BaseNetwork extends EventEmitter {
762763
const value = await this.findContentLocally(decodedContentMessage.contentKey)
763764
if (!value) {
764765
await this.enrResponse(decodedContentMessage.contentKey, src, requestId)
765-
} else if (value instanceof Uint8Array && value.length < MAX_PACKET_SIZE) {
766+
} else if (
767+
value instanceof Uint8Array &&
768+
value.length < MAX_UDP_PACKET_SIZE - getTalkReqOverhead(hexToBytes(this.networkId).byteLength)
769+
) {
766770
this.logger(
767771
'Found value for requested content ' +
768772
bytesToHex(decodedContentMessage.contentKey) +
@@ -818,7 +822,11 @@ export abstract class BaseNetwork extends EventEmitter {
818822
if (encodedEnrs.length > 0) {
819823
this.logger.extend('FINDCONTENT')(`Found ${encodedEnrs.length} closer to content`)
820824
// TODO: Add capability to send multiple TALKRESP messages if # ENRs exceeds packet size
821-
while (encodedEnrs.length > 0 && arrayByteLength(encodedEnrs) > MAX_PACKET_SIZE) {
825+
while (
826+
encodedEnrs.length > 0 &&
827+
arrayByteLength(encodedEnrs) >
828+
MAX_UDP_PACKET_SIZE - getTalkReqOverhead(hexToBytes(this.networkId).byteLength)
829+
) {
822830
// Remove ENRs until total ENRs less than 1200 bytes
823831
encodedEnrs.pop()
824832
}

packages/portalnetwork/src/wire/utp/Utils/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const AUTO_ACK_SMALLER_THAN_ACK_NUMBER: boolean = true
2424
export const MINIMUM_DIFFERENCE_TIMESTAMP_MICROSEC: number = 120000000
2525

2626
export const DEFAULT_PACKET_SIZE = 512
27-
export const MAX_PACKET_SIZE: number = 1225
27+
export const MAX_UDP_PACKET_SIZE: number = 1280
2828
export const MIN_PACKET_SIZE: number = 150
2929
export const MINIMUM_MTU: number = 576
3030
export const SEND_IN_BURST: boolean = true
@@ -34,7 +34,7 @@ export const MICROSECOND_WAIT_BETWEEN_BURSTS: number = 28000
3434
export const TIME_WAIT_AFTER_LAST_PACKET: number = 3000000
3535
export const ONLY_POSITIVE_GAIN: boolean = false
3636
export const DEBUG: boolean = false
37-
export const MAX_UTP_PACKET_LENGTH = MAX_PACKET_SIZE
37+
export const MAX_UTP_PACKET_LENGTH = MAX_UDP_PACKET_SIZE
3838
export const MAX_UDP_HEADER_LENGTH = 48
3939
export const DEF_HEADER_LENGTH = 20
4040

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './constants.js'
22
export * from './math.js'
33
export * from './variantPrefix.js'
4+
export * from './packet.js'
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Compute the number of bytes required for a TALKREQ message header
3+
* @param protocolIdLen is the length of the protocol ID
4+
* @returns the number of bytes required for a TALKREQ message header
5+
*
6+
* @note Shamelessly copied from [Fluffy](https://github.com/status-im/nimbus-eth1/blob/45767278174a48521de46f029f6e66dc526880f6/fluffy/network/wire/messages.nim#L179)
7+
*/
8+
9+
export const getTalkReqOverhead = (protocolIdLen: number): number => {
10+
return (
11+
16 + // IV size
12+
55 + // header size
13+
1 + // talkReq msg id
14+
3 + // rlp encoding outer list, max length will be encoded in 2 bytes
15+
9 + // request id (max = 8) + 1 byte from rlp encoding byte string
16+
protocolIdLen + // bytes length of protocolid (e.g. 0x500b for History Network)
17+
1 + // + 1 is necessary due to rlp encoding of byte string
18+
3 + // rlp encoding response byte string, max length in 2 bytes
19+
16 // HMAC
20+
)
21+
}

packages/portalnetwork/test/integration/ephemeralHeaders.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ describe('should be able to retrieve ephemeral headers from a peer', () => {
9797
assert.exists(res2)
9898
if ('content' in res2!) {
9999
const payload = EphemeralHeaderPayload.deserialize(res2.content)
100-
assert.equal(payload.length, 1, 'should only get a single ancestor')
100+
assert.equal(payload.length, 2, 'should only get a single ancestor')
101101
} else {
102102
assert.fail('Expected content in response')
103103
}

0 commit comments

Comments
 (0)