Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
working \o/
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Mar 20, 2017
1 parent 69dc9e7 commit 6fe86d5
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 236 deletions.
37 changes: 13 additions & 24 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const gulp = require('gulp')
const parallel = require('async/parallel')
const series = require('async/series')
const createTempRepo = require('./test/utils/create-repo-node.js')
const IPFS = require('./src/core')
const HTTPAPI = require('./src/http-api')
const leftPad = require('left-pad')

Expand All @@ -16,31 +15,21 @@ let nodes = []
function spawnDaemon (num, callback) {
num = leftPad(num, 3, 0)

// const node = new IPFS({
// repo: createTempRepo(),
// init: {
// bits: 1024
// },
// start: false,
// EXPERIMENTAL: {
// pubsub: true
// },
const config = {
Addresses: {
Swarm: [
`/ip4/127.0.0.1/tcp/10${num}`,
`/ip4/127.0.0.1/tcp/20${num}/ws`
],
API: `/ip4/127.0.0.1/tcp/31${num}`,
Gateway: `/ip4/127.0.0.1/tcp/32${num}`
},
Discovery: {
MDNS: {
Enabled: false
}
const config = {
Addresses: {
Swarm: [
`/ip4/127.0.0.1/tcp/10${num}`,
`/ip4/127.0.0.1/tcp/20${num}/ws`
],
API: `/ip4/127.0.0.1/tcp/31${num}`,
Gateway: `/ip4/127.0.0.1/tcp/32${num}`
},
Discovery: {
MDNS: {
Enabled: false
}
}
// })
}

const daemon = new HTTPAPI(createTempRepo(), config)
nodes.push(daemon)
Expand Down
2 changes: 1 addition & 1 deletion src/cli/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ aliases.forEach((alias) => {
const args = process.argv.slice(2)

// Need to skip to avoid locking the daemon
if (args[0] === 'daemon') {
if (args[0] === 'daemon' || args[0] === 'init') {
return cli.help().strict(false).completion().parse(args)
}

Expand Down
4 changes: 3 additions & 1 deletion src/cli/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ exports.getIPFS = (callback) => {
throw err
})

callback(null, node, cleanup)
node.once('ready', () => {
callback(null, node, cleanup)
})
}

exports.getRepoPath = () => {
Expand Down
26 changes: 15 additions & 11 deletions src/core/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ module.exports = (self) => {
(cb) => self._repo.exists(cb),
(exists, cb) => {
if (exists && !repoOpen) {
return self._repo.open(cb)
return series([
(cb) => self._repo.open(cb),
(cb) => self.preStart(cb)
], cb)
}
cb()
}
Expand All @@ -35,6 +38,7 @@ module.exports = (self) => {
if (err) {
self.emit('error', err)
}
self.emit('ready')
self.log('boot:done', err)
}

Expand All @@ -61,10 +65,10 @@ module.exports = (self) => {

if (setConfig) {
self.log('boot:setConfig')
// if (!hasRepo) {
// console.log('WARNING, trying to set config on uninitialized repo, maybe forgot to set "init: true"')
// } else {
tasks.push((cb) => {
if (!hasRepo) {
console.log('WARNING, trying to set config on uninitialized repo, maybe forgot to set "init: true"')
} else {
tasks.push((cb) => {
waterfall([
(cb) => self.config.get(cb),
(config, cb) => {
Expand All @@ -73,17 +77,17 @@ module.exports = (self) => {
}
], cb)
})
// }
}
}

if (doStart) {
self.log('boot:doStart')
// if (!hasRepo) {
// console.log('WARNING, trying to start ipfs node on uninitialized repo, maybe forgot to set "init: true"')
// return done(new Error('Uninitalized repo'))
// } else {
if (!hasRepo) {
console.log('WARNING, trying to start ipfs node on uninitialized repo, maybe forgot to set "init: true"')
return done(new Error('Uninitalized repo'))
} else {
tasks.push((cb) => self.start(cb))
// }
}
}

series(tasks, done)
Expand Down
27 changes: 16 additions & 11 deletions src/core/components/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ module.exports = function init (self) {
opts = {}
}

const done = (err, res) => {
if (err) {
self.emit('error', err)
return callback(err)
}

self.state.initialized()
self.emit('init')
callback(null, res)
}

if (self.state.state !== 'uninitalized') {
return done(new Error('Not able to init from state: ' + self.state.state))
}

self.state.init()
self.log('init')

Expand Down Expand Up @@ -74,16 +89,6 @@ module.exports = function init (self) {
cb(null, true)
})
}
], (err, res) => {
if (err) {
self.log('init failed', err)
return callback(err)
}
self._state.daemon = 'initialized'
self.emit('init')
self.log('init done')
self.state.initialized()
callback(null, res)
})
], done)
}
}
66 changes: 27 additions & 39 deletions src/core/components/pre-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,33 @@ const mafmt = require('mafmt')
*/
module.exports = function preStart (self) {
return (callback) => {
if (self._repo.closed) {
if (self._awaitInit) {
self._repo.once('init', start)
} else {
self._repo.once('open', start)
self.log('pre-start')

waterfall([
(cb) => self._repo.config.get(cb),
(config, cb) => {
const privKey = config.Identity.PrivKey

peerId.createFromPrivKey(privKey, (err, id) => {
cb(err, config, id)
})
},
(config, id, cb) => {
self._peerInfo = new PeerInfo(id)

config.Addresses.Swarm.forEach((addr) => {
let ma = multiaddr(addr)

if (!mafmt.IPFS.matches(ma)) {
ma = ma.encapsulate('/ipfs/' +
self._peerInfo.id.toB58String())
}

self._peerInfo.multiaddr.add(ma)
})

cb()
}
} else {
start()
}

function start () {
self.log('pre-start')

waterfall([
(cb) => self._repo.config.get(cb),
(config, cb) => {
const privKey = config.Identity.PrivKey

peerId.createFromPrivKey(privKey, (err, id) => {
cb(err, config, id)
})
},
(config, id, cb) => {
self._peerInfo = new PeerInfo(id)

config.Addresses.Swarm.forEach((addr) => {
let ma = multiaddr(addr)

if (!mafmt.IPFS.matches(ma)) {
ma = ma.encapsulate('/ipfs/' +
self._peerInfo.id.toB58String())
}

self._peerInfo.multiaddr.add(ma)
})

cb()
}
], callback)
}
], callback)
}
}
92 changes: 42 additions & 50 deletions src/core/components/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,59 @@
const series = require('async/series')
const Bitswap = require('ipfs-bitswap')
const FloodSub = require('libp2p-floodsub')
const once = require('once')

module.exports = (self) => {
return (callback) => {
callback = once(callback) || function noop () {}
self.once('error', callback)
self.once('start', callback)
callback = callback || function noop () {}

if (self.state.state !== 'stopped') {
self.log('sub', self.state.state)
const sub = self.state.on('stopped', () => {
sub.off()
start()
})
} else {
start()
const done = (err) => {
if (err) {
self.emit('error', err)
return callback(err)
}

self.state.started()
self.emit('start')
callback()
}

function start () {
self.log('starting')
self.state.start()
const done = (err) => {
if (err) {
return self.emit('error', err)
}
if (self.state.state !== 'stopped') {
return done(new Error('Not able to start from state: ' + self.state.state))
}

self.state.started()
self.emit('start')
}
self.log('starting')
self.state.start()

series([
(cb) => {
if (self._repo.closed) {
self._repo.open(cb)
} else {
cb()
}
},
(cb) => self.preStart(cb),
(cb) => self.libp2p.start(cb)
], (err) => {
if (err) {
return done(err)
series([
(cb) => {
if (self._repo.closed) {
self._repo.open(cb)
} else {
cb()
}
},
(cb) => self.preStart(cb),
(cb) => self.libp2p.start(cb)
], (err) => {
if (err) {
return done(err)
}

self._bitswap = new Bitswap(
self._libp2pNode,
self._repo.blockstore,
self._peerInfoBook
)
self._bitswap = new Bitswap(
self._libp2pNode,
self._repo.blockstore,
self._peerInfoBook
)

self._bitswap.start()
self._blockService.goOnline(self._bitswap)
self._bitswap.start()
self._blockService.goOnline(self._bitswap)

if (self._options.EXPERIMENTAL.pubsub) {
self._pubsub = new FloodSub(self._libp2pNode)
self._pubsub.start(done)
} else {
done()
}
})
}
if (self._options.EXPERIMENTAL.pubsub) {
self._pubsub = new FloodSub(self._libp2pNode)
self._pubsub.start(done)
} else {
done()
}
})
}
}
Loading

0 comments on commit 6fe86d5

Please sign in to comment.