Skip to content

Commit

Permalink
feat: Better DB - Use ephermal id for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
mkg20001 committed Dec 16, 2017
1 parent 7f442c2 commit 58b425d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ const NodeTrust = require('./src')

map(require('./test/ids.json'), Id.createFromJSON, (e, ids) => {
if (e) throw e
const peer = new Peer(ids[1])
Id.create((e, id) => {
if (e) throw e
const peer = new Peer(id)

listen.forEach(addr => peer.multiaddrs.add(addr))

Expand Down Expand Up @@ -69,4 +71,5 @@ map(require('./test/ids.json'), Id.createFromJSON, (e, ids) => {
})
})
})
})
})
1 change: 0 additions & 1 deletion server/src/config.dev.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"zone": "node.libp2p",
"expire": 20000,
"id": {
"id": "QmRQuY14GoeyDx5DoFWq9xnCteSz6pWFKcopvJspei5LXa",
"privKey": "CAASqQkwggSlAgEAAoIBAQDXW+yHJxrvsOVh6EqNowW0CNMZLvDX8Ai+Sy3zDf8LmgOtZaGvQW/5A07peETcYf/pBCw8p2eqZwzouVxXbPteYnGbLX5AV8edzb98PDIRFAb7tInRbzWcaIC6Bwh5hjiUC4smf2ZEuZl0St9zDGYFGYEPam6HSvIph1RfDPbMXU6X+AIqMXKiArc5K43BpEblNht432xR9ywDQUkJuoXM1NkERCjsE0VkO29MZ2IFM9yUp02JqGKNeubgwOsbfmbXsg715aiWoTiT6lTBJ5bgtydE56YO5jaKVuHsQw9u/QoqdygK6N9mK9vK/KANuveLOpQI9tXchHYwV1aY7VdFAgMBAAECggEBAKMTDoZBDFqYHvoGhf2+hmEPlZoqO2God1ZxYzf7Tzefxk0U+lvVN07ePHD6C1q+MqRl7oF3Wj5kjhfj1JK3CZks/k29Iy7hyVwrImaWxmq8OZV73ihjB7uKPn+fN8Gd61Xfb90U94Hu8M5oq89YgiA2cc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==",
Expand Down
44 changes: 44 additions & 0 deletions server/src/db/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict'

const EE = require('events').EventEmitter

class DB extends EE {
constructor (opt) {
super()
this.data = {}
this.maxAge = opt.maxAge
this.last_used = {}
setInterval(this._loop.bind(this), 100)
}
get (key) {
if (!this.data[key]) return false
this.last_used[key] = new Date().getTime()
return this.data[key]
}
get keys() {
return Object.keys(this.data)
}
peek (key) {
return this.data[key] || false
}
set (key, value) {
this.data[key] = value
this.last_used[key] = new Date().getTime()
}
remove (key) {
delete this.last_used[key]
return delete this.data[key]
}
_loop () {
const curTime = new Date().getTime()
const evictTime = curTime - this.maxAge
for (const key in this.data) {
if (this.last_used[key] <= evictTime) {
this.emit('evict', {key, value: this.data[key]})
this.remove(key)
}
}
}
}

module.exports = DB
3 changes: 1 addition & 2 deletions server/src/dns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const toDNS = {

module.exports = (swarm, config) => {
let dns
const nameRegEx = new RegExp('^ci[a-z]{53}\\.' + swarm.zone.replace(/\./g, '\\.') + '$', 'mi')
const nameRegEx = new RegExp('^ci[a-z0-9]+\\.' + swarm.zone.replace(/\./g, '\\.') + '$', 'mi')
const {db, dnsDB} = swarm
db.on('evict', ({key}) => {
dnsDB.remove(key)
Expand Down Expand Up @@ -75,7 +75,6 @@ module.exports = (swarm, config) => {
(id, cb) => {
swarm.getCN(id, (err, name) => {
if (err) return cb(err)
dns += '.'
cb(null, name)
})
},
Expand Down
8 changes: 4 additions & 4 deletions server/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const SECIO = require('libp2p-secio')

const protos = require('./protos')

const LRU = require('lru')
const DB = require('./db')

module.exports = function NodetrustServer (config) {
const self = this
Expand Down Expand Up @@ -52,9 +52,9 @@ module.exports = function NodetrustServer (config) {
max: 1000000,
maxAge: config.expire || 5 * 60 * 1000
}
swarm.db = new LRU(swarm.dbParam)
swarm.discoveryDB = new LRU(swarm.dbParam)
swarm.dnsDB = new LRU(swarm.dbParam)
swarm.db = new DB(swarm.dbParam)
swarm.discoveryDB = new DB(swarm.dbParam)
swarm.dnsDB = new DB(swarm.dbParam)

require('./ca')(swarm, config.ca)
require('./dns')(swarm, config.dns)
Expand Down

0 comments on commit 58b425d

Please sign in to comment.