Skip to content

Commit

Permalink
feat: use forwarded payments
Browse files Browse the repository at this point in the history
  • Loading branch information
Michiel de Jong authored and michielbdejong committed Dec 5, 2017
1 parent 1ba272b commit 30bf014
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"eventemitter2": "^2.2.1",
"five-bells-shared": "^25.1.0",
"ilp": "~11.2.0",
"ilp-packet": "~1.3.0",
"ilp-packet": "~1.6.0",
"ilp-plugin-bells": "^15.0.0",
"ilp-routing": "~10.1.0",
"lodash": "^4.6.1",
Expand Down
19 changes: 10 additions & 9 deletions src/lib/route-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,14 @@ class RouteBuilder {
}
let ilpPacket
try {
ilpPacket = packet.deserializeIlpPayment(Buffer.from(sourceTransfer.ilp, 'base64'))
ilpPacket = packet.deserializeIlpPacket(Buffer.from(sourceTransfer.ilp, 'base64'))
} catch (err) {
log.debug('error parsing ILP packet: ' + sourceTransfer.ilp)
throw new IncomingTransferError(ilpErrors.F01_Invalid_Packet({
message: 'source transfer has invalid ILP packet'
}))
}
const destinationAddress = ilpPacket.account
const destinationAddress = ilpPacket.data.account
const myAddress = this.ledgers.getPlugin(sourceTransfer.ledger).getAccount()
if (startsWith(destinationAddress, myAddress)) {
log.debug(
Expand All @@ -173,19 +173,20 @@ class RouteBuilder {
}

log.debug('constructing transfer for ILP packet with account=%s amount=%s',
ilpPacket.account, ilpPacket.amount)
ilpPacket.data.account, ilpPacket.data.amount)

const sourceLedger = sourceTransfer.ledger

// If the ILP amount field is zero, it means we should forward the maximum
// we are willing to.
const nextHop = (ilpPacket.amount === '0')
? await this.quoter.findBestPathForSourceAmount(sourceLedger, ilpPacket.account, sourceTransfer.amount)
: await this.quoter.findBestPathForFinalAmount(sourceLedger, ilpPacket.account, ilpPacket.amount)
// If the ILP packet is of type Forwarded Payment, it means we should forward the maximum
// we are willing to. Note that this feature is experimental, and support for it may disappear
// or change at any time.
const nextHop = (ilpPacket.type === packet.Type.TYPE_ILP_FORWARDED_PAYMENT)
? await this.quoter.findBestPathForSourceAmount(sourceLedger, ilpPacket.data.account, sourceTransfer.amount)
: await this.quoter.findBestPathForFinalAmount(sourceLedger, ilpPacket.data.account, ilpPacket.data.amount)
if (!nextHop) {
log.info('could not find quote for source transfer: ' + JSON.stringify(sourceTransfer))
throw new IncomingTransferError(ilpErrors.F02_Unreachable({
message: 'No route found from: ' + sourceLedger + ' to: ' + ilpPacket.account
message: 'No route found from: ' + sourceLedger + ' to: ' + ilpPacket.data.account
}))
}
this._verifyLedgerIsConnected(nextHop.destinationLedger)
Expand Down
7 changes: 3 additions & 4 deletions test/paymentsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ describe('Payments', function () {
})
})

it('uses best rate when ilp packet amount = 0', async function () {
it('uses best rate when ilp packet is a forwarded payment', async function () {
const sendSpy = sinon.spy(this.mockPlugin2, 'sendTransfer')
await this.mockPlugin1.emitAsync('incoming_prepare', {
id: '5857d460-2a46-4545-8311-1539d99e78e8',
Expand All @@ -289,9 +289,8 @@ describe('Payments', function () {
amount: '100',
executionCondition: 'ni:///sha-256;I3TZF5S3n0-07JWH0s8ArsxPmVP6s-0d0SqxR6C3Ifk?fpt=preimage-sha-256&cost=6',
expiresAt: (new Date(START_DATE + 1000)).toISOString(),
ilp: packet.serializeIlpPayment({
account: 'mock.test2.bob',
amount: '0'
ilp: packet.serializeIlpForwardedPayment({
account: 'mock.test2.bob'
}).toString('base64')
})

Expand Down
7 changes: 3 additions & 4 deletions test/routeBuilderSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,9 @@ describe('RouteBuilder', function () {
assert.deepEqual(destinationTransfer.ilp, ilpPacket)
})

it('uses best rate when ilp packet amount = 0', async function () {
const ilpPacket = packet.serializeIlpPayment({
account: bobB,
amount: '0'
it('uses best rate when ilp packet is a forwarded payment', async function () {
const ilpPacket = packet.serializeIlpForwardedPayment({
account: bobB
}).toString('base64')
const destinationTransfer = await this.builder.getDestinationTransfer({
id: 'fd7ecefd-8eb8-4e16-b7c8-b67d9d6995f5',
Expand Down

0 comments on commit 30bf014

Please sign in to comment.