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HD,+BAuHC","sourcesContent":["import BigNumber from 'bignumber.js'\nimport Accounts from './accounts'\nimport RoutingTable from './routing-table'\nimport RateBackend from './rate-backend'\nimport Config from './config'\nimport reduct = require('reduct')\nimport * as IlpPacket from 'ilp-packet'\nimport { create as createLogger } from '../common/log'\nconst log = createLogger('route-builder')\nconst {\n InsufficientTimeoutError,\n InvalidPacketError,\n PeerUnreachableError,\n UnreachableError\n} = IlpPacket.Errors\n\nexport default class RouteBuilder {\n protected accounts: Accounts\n protected routingTable: RoutingTable\n protected backend: RateBackend\n protected config: Config\n\n protected isTrivialRate: boolean\n\n constructor (deps: reduct.Injector) {\n this.accounts = deps(Accounts)\n this.routingTable = deps(RoutingTable)\n this.backend = deps(RateBackend)\n this.config = deps(Config)\n\n this.isTrivialRate =\n this.config.backend === 'one-to-one' &&\n this.config.spread === 0\n }\n\n getNextHop (sourceAccount: string, destinationAccount: string) {\n const route = this.routingTable.resolve(destinationAccount)\n\n if (!route) {\n log.debug('no route found. destinationAccount=' + destinationAccount)\n throw new UnreachableError('no route found. source=' + sourceAccount + ' destination=' + destinationAccount)\n }\n\n if (!this.config.reflectPayments && sourceAccount === route.nextHop) {\n log.debug('refusing to route payments back to sender. sourceAccount=%s destinationAccount=%s', sourceAccount, destinationAccount)\n throw new UnreachableError('refusing to route payments back to sender. sourceAccount=' + sourceAccount + ' destinationAccount=' + destinationAccount)\n }\n\n return route.nextHop\n }\n\n /**\n * @typedef {Object} NextHopPacketInfo\n * @property {string} nextHop Address of the next peer to forward the packet to\n * @property {Buffer} nextHopPacket Outgoing packet\n */\n\n /**\n * Get next ILP prepare packet.\n *\n * Given a previous ILP prepare packet, returns the next ILP prepare packet in\n * the chain.\n *\n * @param {string} sourceAccount ILP address of our peer who sent us the packet\n * @param {IlpPrepare} sourcePacket (Parsed packet that we received\n * @returns {NextHopPacketInfo} Account and packet for next hop\n */\n async getNextHopPacket (sourceAccount: string, sourcePacket: IlpPacket.IlpPrepare) {\n const {\n amount,\n executionCondition,\n expiresAt,\n destination,\n data\n } = sourcePacket\n\n log.trace(\n 'constructing next hop packet. sourceAccount=%s sourceAmount=%s destination=%s',\n sourceAccount, amount, destination\n )\n\n if (destination.length < 1) {\n throw new InvalidPacketError('missing destination.')\n }\n\n const nextHop = this.getNextHop(sourceAccount, destination)\n\n log.trace('determined next hop. nextHop=%s', nextHop)\n\n const rate = await this.backend.getRate(sourceAccount, nextHop)\n\n log.trace('determined local rate. rate=%s', rate)\n\n this._verifyPluginIsConnected(nextHop)\n\n const nextAmount = new BigNumber(amount).times(rate).integerValue(BigNumber.ROUND_FLOOR)\n\n return {\n nextHop,\n nextHopPacket: {\n amount: nextAmount.toString(),\n expiresAt: this._getDestinationExpiry(expiresAt),\n executionCondition,\n destination,\n data\n }\n }\n }\n\n _getDestinationExpiry (sourceExpiry: Date) {\n if (!sourceExpiry) {\n throw new TypeError('source expiry must be a Date')\n }\n const sourceExpiryTime = sourceExpiry.getTime()\n\n if (sourceExpiryTime < Date.now()) {\n throw new InsufficientTimeoutError('source transfer has already expired. sourceExpiry=' + sourceExpiry.toISOString() + ' currentTime=' + (new Date().toISOString()))\n }\n\n // We will set the next transfer's expiry based on the source expiry and our\n // minMessageWindow, but cap it at our maxHoldTime.\n const destinationExpiryTime = Math.min(sourceExpiryTime - this.config.minMessageWindow, Date.now() + this.config.maxHoldTime)\n\n if ((destinationExpiryTime - Date.now()) < this.config.minMessageWindow) {\n throw new InsufficientTimeoutError('source transfer expires too soon to complete payment. actualSourceExpiry=' + sourceExpiry.toISOString() + ' requiredSourceExpiry=' + (new Date(Date.now() + 2 * this.config.minMessageWindow).toISOString()) + ' currentTime=' + (new Date().toISOString()))\n }\n\n return new Date(destinationExpiryTime)\n }\n\n _verifyPluginIsConnected (account: string) {\n if (!this.accounts.getPlugin(account).isConnected()) {\n throw new PeerUnreachableError('no connection to account. account=' + account)\n }\n }\n}\n"]}
\No newline at end of file