Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: greedy forwarding #392

Merged
merged 2 commits into from
Oct 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/lib/quoter.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,27 +142,38 @@ class Quoter {
}

/**
* Note that this function, like most of the ilp-connector code, uses the terms
* source, destination, and final, to refer to what we would normally call
* incoming, outgoing, and destination.
*
* @param {IlpAddress} sourceLedger
* @param {IlpAddress} destination
* @param {Amount} sourceAmount
* @param {Amount} finalAmount
* @returns {Object}
*/
* findBestPathForSourceAmount (sourceLedger, destination, sourceAmount) {
const quote = yield this.quoteBySourceAmount({
* findBestPathForFinalAmount (sourceLedger, destination, finalAmount) {
// This obtains a quote from incoming/"source" ledger to destination/"final" ledger:
const quote = yield this.quoteByDestinationAmount({
sourceAccount: sourceLedger,
destinationAccount: destination,
sourceAmount: sourceAmount,
destinationAmount: finalAmount,
destinationHoldDuration: 10 // dummy value, only used if a remote quote is needed
})
if (!quote) return
if (!quote.hop) {
quote.hop = destination
}

// Now we isolate the head hop (incoming/"source" ledger to outgoing/"destination" ledger):
const headRoute = this.localTables.getLocalPairRoute(sourceLedger, quote.route.nextLedger)
const headCurve = headRoute.curve

return {
isFinal: !quote.hop,
destinationLedger: quote.route.nextLedger,
destinationCreditAccount: quote.hop,
destinationAmount: headCurve.amountAt(sourceAmount).toString(),
finalAmount: quote.liquidityCurve.amountAt(sourceAmount).toString()
sourceAmount: quote.sourceAmount,
destinationAmount: headCurve.amountAt(quote.sourceAmount).toString(),
finalAmount: quote.liquidityCurve.amountAt(quote.sourceAmount).toString()
}
}
}
Expand Down
27 changes: 10 additions & 17 deletions src/lib/route-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ class RouteBuilder {
ilpPacket.account, ilpPacket.amount)

const sourceLedger = sourceTransfer.ledger
const nextHop = yield this.quoter.findBestPathForSourceAmount(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is findBestPathForSourceAmount still used or was this the only call? If its the latter, the function should be removed.

sourceLedger, ilpPacket.account, sourceTransfer.amount)
const nextHop = yield this.quoter.findBestPathForFinalAmount(
sourceLedger, ilpPacket.account, ilpPacket.amount)
if (!nextHop) {
log.info('could not find quote for source transfer: ' + JSON.stringify(sourceTransfer))
throw new IncomingTransferError(ilpErrors.F02_Unreachable({
Expand All @@ -186,22 +186,15 @@ class RouteBuilder {
}
this._verifyLedgerIsConnected(nextHop.destinationLedger)

// Check if this connector can authorize the final transfer.
if (nextHop.isFinal) {
// Verify expectedFinalAmount ≤ actualFinalAmount
// As long as the fxSpread > slippage, the connector won't lose money.
const expectedFinalAmount = new BigNumber(ilpPacket.amount).times(1 - this.slippage)
if (expectedFinalAmount.greaterThan(nextHop.finalAmount)) {
throw new IncomingTransferError(ilpErrors.R01_Insufficient_Source_Amount({
message: 'Payment rate does not match the rate currently offered'
}))
}
// TODO: Verify atomic mode notaries are trusted
// TODO: Verify expiry is acceptable

nextHop.destinationCreditAccount = ilpPacket.account
nextHop.destinationAmount = ilpPacket.amount
// As long as the fxSpread > slippage, the connector won't lose money.
const expectedSourceAmount = new BigNumber(nextHop.sourceAmount).times(1 - this.slippage)
if (expectedSourceAmount.greaterThan(sourceTransfer.amount)) {
throw new IncomingTransferError(ilpErrors.R01_Insufficient_Source_Amount({
message: 'Payment rate does not match the rate currently offered'
}))
}
// TODO: Verify atomic mode notaries are trusted
// TODO: Verify expiry is acceptable

const noteToSelf = {
source_transfer_ledger: sourceTransfer.ledger,
Expand Down
2 changes: 1 addition & 1 deletion test/routeBuilderSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ describe('RouteBuilder', function () {
ledger: ledgerA,
direction: 'incoming',
account: aliceA,
amount: '98', // 98 ⇒ 49 = 50 * (1 - slippage)
amount: '99', // 99 * (1 - slippage) = 100 ⇒ 50
ilp: ilpPacket
})
assert.equal(destinationTransfer.ilp, ilpPacket)
Expand Down