Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Jul 13, 2017
1 parent 930625e commit e992c01
Show file tree
Hide file tree
Showing 22 changed files with 88 additions and 83 deletions.
23 changes: 10 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,21 @@ See https://ipfs.github.io/js-ipfs-bitswap
```sh
» tree src
src
├── components
│   ├── decision
│   │   ├── engine.js
│   │   ├── index.js
│   │   └── ledger.js
│   ├── network # Handles peerSet and open new conns
│   │   └── index.js
│   └── want-manager # Keeps track of all blocks the peer wants (not the others which it is connected)
│   ├── index.js
│   └── msg-queue.js # Messages to send queue, one per peer
├── constants.js
├── decision-engine
│   ├── index.js
│   └── ledger.js
├── index.js
└── types
├── message # (Type) message that is put in the wire
├── network.js # Handles peerSet and open new conns
├─── want-manager # Keeps track of all blocks the peer (self) wants
│   ├── index.js
│   └── msg-queue.js # Messages to send queue, one per peer
└─── types
├── message # (Type) message that is put in the wire
│   ├── entry.js
│   ├── index.js
│   └── message.proto.js
└── wantlist # (Type) track wanted blocks
└── wantlist # (Type) track wanted blocks
├── entry.js
└── index.js
```
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"pull-length-prefixed": "^1.3.0",
"pull-pushable": "^2.1.1",
"pull-stream": "^3.6.0",
"safe-buffer": "^5.1.1",
"varint-decoder": "^0.1.1"
},
"contributors": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const pullAllWith = require('lodash.pullallwith')
const log = debug('bitswap:engine')
log.error = debug('bitswap:engine:error')

const Message = require('../../types/message')
const Wantlist = require('../../types/wantlist')
const Message = require('../types/message')
const Wantlist = require('../types/wantlist')
const Ledger = require('./ledger')

const MAX_MESSAGE_SIZE = 512 * 1024
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const Wantlist = require('../../types/wantlist')
const Wantlist = require('../types/wantlist')

class Ledger {
constructor (peerId) {
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const map = require('async/map')
const once = require('once')

const CONSTANTS = require('./constants')
const WantManager = require('./components/want-manager')
const Network = require('./components/network')
const DecisionEngine = require('./components/decision-engine')
const WantManager = require('./want-manager')
const Network = require('./network')
const DecisionEngine = require('./decision-engine')

const log = debug('bitswap')
log.error = debug('bitswap:error')
Expand Down
4 changes: 2 additions & 2 deletions src/components/network.js → src/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const pull = require('pull-stream')
const waterfall = require('async/waterfall')
const each = require('async/each')

const Message = require('../types/message')
const CONSTANTS = require('../constants')
const Message = require('./types/message')
const CONSTANTS = require('./constants')
const debug = require('debug')
const log = debug('bitswap:network')
log.error = debug('bitswap:network:error')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

const debug = require('debug')

const Message = require('../../types/message')
const Wantlist = require('../../types/wantlist')
const CONSTANTS = require('../../constants')
const Message = require('../types/message')
const Wantlist = require('../types/wantlist')
const CONSTANTS = require('../constants')
const MsgQueue = require('./msg-queue')

const log = debug('bitswap:wantmanager')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const debug = require('debug')
const debounce = require('lodash.debounce')
const Message = require('../../types/message')
const Message = require('../types/message')

const log = debug('bitswap:wantmanager:queue')
log.error = debug('bitswap:wantmanager:queue:error')
Expand Down
12 changes: 7 additions & 5 deletions test/index-test.js → test/bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ const PeerBook = require('peer-book')
const Message = require('../src/types/message')
const Bitswap = require('../src')

const utils = require('./utils')
const makeBlock = utils.makeBlock
const mockNetwork = require('./utils/mock-network').mockNetwork
const applyNetwork = require('./utils/mock-network').applyNetwork

const makeBlock = require('./utils/make-block')

function hasBlocks (msg, store, cb) {
each(msg.blocks.values(), (b, cb) => {
Expand Down Expand Up @@ -256,7 +258,7 @@ module.exports = (repo) => {
it('block is added locally afterwards', (done) => {
const block = blocks[9]
const bs = new Bitswap(createLibp2pMock(), store)
const net = utils.mockNetwork()
const net = mockNetwork()

bs.network = net
bs.wm.network = net
Expand Down Expand Up @@ -328,7 +330,7 @@ module.exports = (repo) => {
}
}
bs1 = new Bitswap(libp2pMock, store)
utils.applyNetwork(bs1, n1)
applyNetwork(bs1, n1)
bs1.start()

let store2
Expand All @@ -338,7 +340,7 @@ module.exports = (repo) => {
(repo, cb) => {
store2 = repo.blocks
bs2 = new Bitswap(createLibp2pMock(), store2)
utils.applyNetwork(bs2, n2)
applyNetwork(bs2, n2)
bs2.start()
bs1._onPeerConnected(other)
bs2._onPeerConnected(me)
Expand Down
4 changes: 2 additions & 2 deletions test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ const repo = {
remove: removeRepos
}

require('./index-test')(repo)
require('./components/decision-engine/index-test')(repo)
require('./bitswap.js')(repo)
require('./decision-engine/decision-engine')(repo)
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ const waterfall = require('async/waterfall')
const eachSeries = require('async/eachSeries')
const CID = require('cids')
const multihashing = require('multihashing-async')
const Buffer = require('safe-buffer').Buffer

const Message = require('../../../src/types/message')
const DecisionEngine = require('../../../src/components/decision-engine')
const Message = require('../../src/types/message')
const DecisionEngine = require('../../src/decision-engine')

const mockNetwork = require('../../utils').mockNetwork
const mockNetwork = require('../utils/mock-network').mockNetwork

function messageToString (m) {
return Array.from(m[1].blocks.values())
Expand Down Expand Up @@ -64,7 +65,7 @@ module.exports = (repo) => {
const receiver = res[1]

map(_.range(1000), (i, cb) => {
const data = new Buffer(`this is message ${i}`)
const data = Buffer.from(`this is message ${i}`)
multihashing(data, 'sha2-256', (err, hash) => {
expect(err).to.not.exist()

Expand Down Expand Up @@ -132,7 +133,7 @@ module.exports = (repo) => {
function partnerWants (dEngine, values, partner, cb) {
const message = new Message(false)

map(values, (v, cb) => multihashing(new Buffer(v), 'sha2-256', cb), (err, hashes) => {
map(values, (v, cb) => multihashing(Buffer.from(v), 'sha2-256', cb), (err, hashes) => {
expect(err).to.not.exist()
hashes.forEach((hash, i) => {
message.addEntry(new CID(hash), Math.pow(2, 32) - 1 - i)
Expand All @@ -145,7 +146,7 @@ module.exports = (repo) => {
function partnerCancels (dEngine, values, partner, cb) {
const message = new Message(false)

map(values, (v, cb) => multihashing(new Buffer(v), 'sha2-256', cb), (err, hashes) => {
map(values, (v, cb) => multihashing(Buffer.from(v), 'sha2-256', cb), (err, hashes) => {
expect(err).to.not.exist()
hashes.forEach((hash) => {
message.cancel(new CID(hash))
Expand All @@ -160,12 +161,12 @@ module.exports = (repo) => {
waterfall([
(cb) => map(
alphabet,
(v, cb) => multihashing(new Buffer(v), 'sha2-256', cb),
(v, cb) => multihashing(Buffer.from(v), 'sha2-256', cb),
cb
),
(hashes, cb) => each(
hashes.map((h, i) => {
return new Block(new Buffer(alphabet[i]), new CID(h))
return new Block(Buffer.from(alphabet[i]), new CID(h))
}),
(b, cb) => repo.blocks.put(b, cb),
cb
Expand Down Expand Up @@ -211,7 +212,7 @@ module.exports = (repo) => {

it('splits large block messages', (done) => {
const data = _.range(10).map((i) => {
const b = new Buffer(1024 * 256)
const b = Buffer.alloc(1024 * 256)
b.fill(i)
return b
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ chai.use(require('dirty-chai'))
const expect = chai.expect
const PeerId = require('peer-id')

const Ledger = require('../../../src/components/decision-engine/ledger')
const Ledger = require('../../src/decision-engine/ledger')

describe('Ledger', () => {
let peerId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ const crypto = require('crypto')
const CID = require('cids')
const multihashing = require('multihashing-async')

const utils = require('../../utils')
const genBitswapNetwork = require('../utils/mock-network').genBitswapNetwork

describe('gen Bitswap network', function () {
// CI is very slow
this.timeout(300 * 1000)

it('retrieves local blocks', (done) => {
utils.genBitswapNetwork(1, (err, nodes) => {
genBitswapNetwork(1, (err, nodes) => {
expect(err).to.not.exist()

const node = nodes[0]
Expand Down Expand Up @@ -66,7 +66,7 @@ describe('gen Bitswap network', function () {
describe('distributed blocks', () => {
it('with 2 nodes', (done) => {
const n = 2
utils.genBitswapNetwork(n, (err, nodeArr) => {
genBitswapNetwork(n, (err, nodeArr) => {
expect(err).to.not.exist()
nodeArr.forEach((node) => {
expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const Node = require('../../libp2p-bundle')
const Node = require('../utils/libp2p-bundle')
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const lp = require('pull-length-prefixed')
Expand All @@ -13,11 +13,12 @@ const parallel = require('async/parallel')
const waterfall = require('async/waterfall')
const map = require('async/map')
const _ = require('lodash')
const utils = require('../../utils')
const makeBlock = require('../utils/make-block')

const Network = require('../../../src/components/network')
const Message = require('../../../src/types/message')
const Network = require('../../src/network')
const Message = require('../../src/types/message')

// TODO send this to utils
function createP2PNode (multiaddrs, options, callback) {
if (typeof options === 'function') {
callback = options
Expand Down Expand Up @@ -59,7 +60,7 @@ describe('network', () => {
(cb) => createP2PNode('/ip4/127.0.0.1/tcp/0', { bits: 1024 }, cb),
(cb) => createP2PNode('/ip4/127.0.0.1/tcp/0', { bits: 1024 }, cb),
(cb) => createP2PNode('/ip4/127.0.0.1/tcp/0', { bits: 1024 }, cb),
(cb) => map(_.range(2), (i, cb) => utils.makeBlock(cb), cb)
(cb) => map(_.range(2), (i, cb) => makeBlock(cb), cb)
], (err, results) => {
expect(err).to.not.exist()

Expand Down
8 changes: 4 additions & 4 deletions test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const repo = {
remove: removeRepos
}

require('./index-test')(repo)
require('./components/decision-engine/index-test')(repo)
require('./components/network/network.node.js')
require('./components/network/gen-bitswap-network.node.js')
require('./bitswap.js')(repo)
require('./decision-engine/decision-engine')(repo)
require('./network/network.node.js')
require('./network/gen-bitswap-network.node.js')
8 changes: 4 additions & 4 deletions test/types/message.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const map = require('async/map')
const CID = require('cids')
const isNode = require('detect-node')
const _ = require('lodash')

const Buffer = require('safe-buffer').Buffer
const loadFixture = require('aegir/fixtures')
const testDataPath = (isNode ? '../' : '') + 'test-data/serialized-from-go'
const rawMessageFullWantlist = loadFixture(__dirname, testDataPath + '/bitswap110-message-full-wantlist')
Expand All @@ -19,14 +19,14 @@ const rawMessageOneBlock = loadFixture(__dirname, testDataPath + '/bitswap110-me
const pbm = protobuf(require('../../src/types/message/message.proto'))

const BitswapMessage = require('../../src/types/message')
const utils = require('../utils')
const makeBlock = require('../utils/make-block')

describe('BitswapMessage', () => {
let blocks
let cids

before((done) => {
map(_.range(3), (i, cb) => utils.makeBlock(cb), (err, res) => {
map(_.range(3), (i, cb) => makeBlock(cb), (err, res) => {
expect(err).to.not.exist()
blocks = res
cids = blocks.map((b) => b.cid)
Expand Down Expand Up @@ -243,7 +243,7 @@ describe('BitswapMessage', () => {

describe('go interop', () => {
it('bitswap 1.0.0 message', (done) => {
const goEncoded = new Buffer('CioKKAoiEiAs8k26X7CjDiboOyrFueKeGxYeXB+nQl5zBDNik4uYJBAKGAA=', 'base64')
const goEncoded = Buffer.from('CioKKAoiEiAs8k26X7CjDiboOyrFueKeGxYeXB+nQl5zBDNik4uYJBAKGAA=', 'base64')

const msg = new BitswapMessage(false)
const cid = new CID('QmRN6wdp1S2A5EtjW9A3M1vKSBuQQGcgvuhoMUoEz4iiT5')
Expand Down
4 changes: 2 additions & 2 deletions test/types/wantlist.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ const _ = require('lodash')
const multihashing = require('multihashing-async')

const Wantlist = require('../../src/types/wantlist')
const utils = require('../utils')
const makeBlock = require('../utils/make-block')

describe('Wantlist', () => {
let wm
let blocks

before((done) => {
map(_.range(2), (i, cb) => utils.makeBlock(cb), (err, res) => {
map(_.range(2), (i, cb) => makeBlock(cb), (err, res) => {
expect(err).to.not.exist()
blocks = res
done()
Expand Down
File renamed without changes.
15 changes: 15 additions & 0 deletions test/utils/make-block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

const multihashing = require('multihashing-async')
const CID = require('cids')
const Block = require('ipfs-block')
const Buffer = require('safe-buffer').Buffer

module.exports = (callback) => {
const data = Buffer.from(`hello world ${Math.random()}`)

multihashing(data, 'sha2-256', (err, hash) => {
if (err) { return callback(err) }
callback(null, new Block(data, new CID(hash)))
})
}
15 changes: 1 addition & 14 deletions test/utils.js → test/utils/mock-network.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ const PeerInfo = require('peer-info')
const Node = require('./libp2p-bundle')
const os = require('os')
const Repo = require('ipfs-repo')
const multihashing = require('multihashing-async')
const CID = require('cids')
const Block = require('ipfs-block')

const Bitswap = require('../src')
const Bitswap = require('../../src')

exports.mockNetwork = (calls, done) => {
done = done || (() => {})
Expand Down Expand Up @@ -198,13 +195,3 @@ exports.genBitswapNetwork = (n, callback) => {
}
})
}

exports.makeBlock = (cb) => {
const data = new Buffer(`hello world ${Math.random()}`)
multihashing(data, 'sha2-256', (err, hash) => {
if (err) {
return cb(err)
}
cb(null, new Block(data, new CID(hash)))
})
}
Loading

0 comments on commit e992c01

Please sign in to comment.