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

Commit

Permalink
Merge pull request #141 from interledgerjs/ko-money-event
Browse files Browse the repository at this point in the history
feat: emit raw Prepare on money event
  • Loading branch information
kincaidoneil committed Apr 30, 2020
2 parents add8372 + 3ac5c20 commit 629a990
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ export class Connection extends EventEmitter {

// Add incoming amounts to each stream
for (let { stream, amount } of amountsToReceive) {
stream._addToIncoming(amount)
stream._addToIncoming(amount, prepare)
}

// TODO make sure the queued frames aren't too big
Expand Down
5 changes: 3 additions & 2 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
checkedAdd,
checkedSubtract
} from './util/long'
import { IlpPrepare } from 'ilp-packet'

const DEFAULT_TIMEOUT = 60000

Expand Down Expand Up @@ -378,11 +379,11 @@ export class DataAndMoneyStream extends Duplex {
* (Used by the Connection class but not meant to be part of the public API)
* @private
*/
_addToIncoming (amount: Long): void {
_addToIncoming (amount: Long, prepare: IlpPrepare): void {
// If this overflows, it will als be caught (and handled) at the connection level.
this._totalReceived = checkedAdd(this._totalReceived, amount).sum
this.log.trace('received %s (totalReceived: %s)', amount, this._totalReceived)
this.emit('money', amount.toString())
this.emit('money', amount.toString(), prepare)
}

/**
Expand Down
12 changes: 8 additions & 4 deletions test/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,15 +510,19 @@ describe('Connection', function () {

describe('Sending Money', function () {
it('should send money', async function () {
const spy = sinon.spy()
const moneyEventSpy = sinon.spy()
const handleDataSpy = sinon.spy(this.serverPlugin, 'dataHandler')
this.serverConn.on('stream', (moneyStream: DataAndMoneyStream) => {
moneyStream.on('money', spy)
moneyStream.on('money', moneyEventSpy)
})
const clientStream = this.clientConn.createStream()
await clientStream.sendTotal(117)

assert.calledOnce(spy)
assert.calledWith(spy, '58')
assert.calledOnce(moneyEventSpy)
assert.calledWith(moneyEventSpy, '58')

// 2nd arg passed to `money` event handler should be the same ILP Prepare received by the plugin
assert.deepEqual(moneyEventSpy.getCall(0).args[1], IlpPacket.deserializeIlpPrepare(handleDataSpy.getCall(0).args[0]))
})
})

Expand Down

0 comments on commit 629a990

Please sign in to comment.