Skip to content
This repository was archived by the owner on May 5, 2022. It is now read-only.

Commit 1ee52bf

Browse files
committed
fix: address comments
1 parent a6a3023 commit 1ee52bf

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

src/connection.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,15 @@ export interface ConnectionOpts {
9393
getExpiry?: (destination: string) => Date,
9494
/**
9595
* Callback for the consumer to perform accounting and choose to fulfill an incoming ILP Prepare,
96-
* given the identifier of the connection, sequence of the packet, and amount received.
96+
* given the amount received, a unique identifier for the packet, and `connectionTag`.
9797
*
9898
* If the returned Promise is resolved, the ILP Prepare will be fulfilled; if it is rejected,
9999
* the ILP Prepare will be rejected. The ILP Fulfill will be immediately sent back after
100100
* the Promise is resolved.
101101
*
102-
* Note: the `packetId` should be used for uniqueness, but a misbehaving sender can trigger duplicates.
103-
* If receiving a duplicate packet ID, that packet should be ignored and rejected.
102+
* Note: a misbehaving sender can trigger duplicate packetIds, which should be ignored and rejected.
104103
*/
105-
shouldFulfill?: (packetAmount: Long, packetId: string, connectionTag?: string) => Promise<void>,
104+
shouldFulfill?: (packetAmount: Long, packetId: Buffer, connectionTag?: string) => Promise<void>,
106105
}
107106

108107
export interface BuildConnectionOpts extends ConnectionOpts {
@@ -197,14 +196,14 @@ export class Connection extends EventEmitter {
197196
protected _totalDelivered: Long
198197
protected _lastPacketExchangeRate: Rational
199198
protected getExpiry: (destination: string) => Date
200-
protected shouldFulfill?: (packetAmount: Long, packetId: string, connectionTag?: string) => Promise<void>
199+
protected shouldFulfill?: (packetAmount: Long, packetId: Buffer, connectionTag?: string) => Promise<void>
201200

202201
constructor (opts: NewConnectionOpts) {
203202
super()
204203

205204
// Use the same connectionId for logging on both client & server
206205
const lastAddressSegment = opts.destinationAccount ? opts.destinationAccount.split('.').slice(-1)[0] : undefined
207-
this.connectionId = (opts.connectionId || lastAddressSegment || uuid()).slice(0, 8)
206+
this.connectionId = (opts.connectionId || lastAddressSegment || uuid()).replace(/[-_]/g, '').slice(0, 8)
208207

209208
this.plugin = opts.plugin
210209
this._sourceAccount = opts.sourceAccount
@@ -652,7 +651,7 @@ export class Connection extends EventEmitter {
652651
// Allow consumer to choose to fulfill each packet and/or perform other logic before fulfilling
653652
if (this.shouldFulfill && incomingAmount.greaterThan(0)) {
654653
const packetId = await cryptoHelper.generateIncomingPacketId(this.sharedSecret, requestPacket.sequence)
655-
await this.shouldFulfill(incomingAmount, packetId.toString(), this.connectionTag).catch(async err => {
654+
await this.shouldFulfill(incomingAmount, packetId, this.connectionTag).catch(async err => {
656655
this.removeIncomingHold(incomingAmount)
657656
this.log.debug('application declined to fulfill packet %s:', requestPacket.sequence, err)
658657
await throwFinalApplicationError()

test/connection.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,11 +1252,11 @@ describe('Connection', function () {
12521252
* If any other packets call it, the test will fail.
12531253
*/
12541254

1255-
const shouldFulfillSpy = sinon.spy(async (amount: Long, packetId: string, connectionTag: string) => {
1255+
const shouldFulfillSpy = sinon.spy(async (amount: Long, packetId: Buffer, connectionTag: string) => {
12561256
assert.equal(actualConnectionTag, connectionTag)
12571257

1258-
assert.isFalse(packetIds.has(packetId)) // Test that the packet Ids are unique
1259-
packetIds.add(packetId)
1258+
assert.isFalse(packetIds.has(packetId.toString())) // Test that the packet Ids are unique
1259+
packetIds.add(packetId.toString())
12601260

12611261
const amountNum = amount.toNumber()
12621262
if (shouldFulfillSpy.callCount === 1) {

0 commit comments

Comments
 (0)