From 871d0d2b99635ede4b3467265a07bc44b427b414 Mon Sep 17 00:00:00 2001 From: Michael Garvin Date: Tue, 15 May 2018 08:37:34 -0700 Subject: [PATCH] feat: add bitswap.ledgerForPeer --- src/decision-engine/index.js | 16 ++++++++++++++++ src/index.js | 10 ++++++++++ test/bitswap-mock-internals.js | 19 +++++++++++++++++++ test/decision-engine/ledger.spec.js | 2 ++ 4 files changed, 47 insertions(+) diff --git a/src/decision-engine/index.js b/src/decision-engine/index.js index d502b0f4..dbe64160 100644 --- a/src/decision-engine/index.js +++ b/src/decision-engine/index.js @@ -135,6 +135,22 @@ class DecisionEngine { return this.ledgerMap.get(peerIdStr).wantlist.sortedEntries() } + ledgerForPeer (peerId) { + const peerIdStr = peerId.toB58String() + + const ledger = this.ledgerMap.get(peerIdStr) + if (!ledger) { + return null + } + return { + peer: ledger.partner.toB58String(), + value: ledger.debtRatio(), + sent: ledger.accounting.bytesSent, + recv: ledger.accounting.bytesRecv, + exchanged: ledger.exchangeCount + } + } + peers () { return Array.from(this.ledgerMap.values()).map((l) => l.partner) } diff --git a/src/index.js b/src/index.js index cee77cb8..4155055e 100644 --- a/src/index.js +++ b/src/index.js @@ -179,6 +179,16 @@ class Bitswap { return this.engine.wantlistForPeer(peerId) } + /** + * Return ledger information for a given `peerId` + * + * @param {PeerId} peerId + * @returns {?Object} + */ + ledgerForPeer (peerId) { + return this.engine.ledgerForPeer(peerId) + } + /** * Fetch a given block by cid. If the block is in the local * blockstore it is returned, otherwise the block is added to the wantlist and returned once another node sends it to us. diff --git a/test/bitswap-mock-internals.js b/test/bitswap-mock-internals.js index 24f91c17..af84b5dd 100644 --- a/test/bitswap-mock-internals.js +++ b/test/bitswap-mock-internals.js @@ -76,6 +76,13 @@ describe('bitswap with mocks', function () { expect(blocks[0].data).to.eql(b1.data) expect(blocks[1].data).to.eql(b2.data) + + const ledger = bs.ledgerForPeer(other) + expect(ledger.peer).to.equal(other.toB58String()) + expect(ledger.value).to.equal(0) + expect(ledger.sent).to.equal(0) + expect(ledger.recv).to.equal(96) + expect(ledger.exchanged).to.equal(2) done() }) }) @@ -381,4 +388,16 @@ describe('bitswap with mocks', function () { }) }) }) + + describe('ledgerForPeer', () => { + it('returns null for unknown peer', (done) => { + const bs = new Bitswap(mockLibp2pNode(), repo.blocks) + PeerId.create((err, id) => { + expect(err).to.not.exist() + const ledger = bs.ledgerForPeer(id) + expect(ledger).to.equal(null) + done() + }) + }) + }) }) diff --git a/test/decision-engine/ledger.spec.js b/test/decision-engine/ledger.spec.js index 44e02a79..a4ff3f34 100644 --- a/test/decision-engine/ledger.spec.js +++ b/test/decision-engine/ledger.spec.js @@ -28,6 +28,8 @@ describe('Ledger', () => { }) it('accounts', () => { + expect(ledger.debtRatio()).to.eql(0) + ledger.sentBytes(100) ledger.sentBytes(12000) ledger.receivedBytes(223432)