Skip to content
Permalink
Browse files

Re-organized project. Some clean-up (INFURA#3)

  • Loading branch information...
kshinn authored and egalano committed Dec 22, 2018
1 parent 83d5c07 commit 0d66b02b500a5be7bfacfeab2d8d03bbb2af3b9e
Showing with 117 additions and 96 deletions.
  1. +1 −1 README.md
  2. +61 −90 app.js
  3. +1 −1 docker-compose.yml
  4. +49 −0 lib/db.js
  5. +3 −3 package-lock.json
  6. +2 −1 package.json
@@ -18,7 +18,7 @@ Monitor and record metrics from the DevP2P network in MongoDB and visualize the
```docker-compose up --detach```
##### Run the DevP2P monitor
```node app.js```
```npm start```
It will take a minute for the client to connect to DevP2P peers. Once it connects to the first peer it will save the node's info to MongoDB and you should be able to see data in your dashboard.
151 app.js
@@ -3,45 +3,17 @@ const os = require('os')
const Buffer = require('safe-buffer').Buffer
const { randomBytes } = require('crypto')
const devp2p = require('ethereumjs-devp2p')
const mongoose = require('mongoose')
const geoip = require('geoip-lite')
const debug = require('debug')('infura:dpt')
const Web3 = require('web3')

const web3 = new Web3()
const _ = require('lodash')
const db = require('./lib/db')()
const web3Url = `https://mainnet.infura.io/v3/${process.env.INFURA_ID}`
web3.setProvider(new web3.providers.HttpProvider(web3Url))

// Web3 Connection
const WEB3_URL = 'https://mainnet.infura.io/v3/09bcdf3656ee4a80b05a4a921358d109'
web3.setProvider(new web3.providers.HttpProvider(WEB3_URL))

// MongoDB Connection
const MONGO_URL = 'mongodb://localhost:27017'
var db = mongoose.connection

var Schema = mongoose.Schema
const peerSchema = new Schema({
enode: { type: String, index: true, unique: true },
address: { type: String, index: true },
latitude: Number,
longitude: Number,
country: String,
port: Number,
clientId: { type: String, index: true },
clientMeta1: { type: String, index: true },
clientMeta2: { type: String, index: true },
clientMeta3: { type: String, index: true },
clientMeta4: { type: String, index: true },
capabilities: Array,
bestHash: String,
totalDifficulty: String,
bestBlockNumber: { type: Number, index: true, default: 0 },
infuraBlockNumber: { type: Number, index: true, default: 0 },
infuraDrift: { type: Number, index: true, default: 0 },
numPeers: { type: Number, default: 0 },
timestamp: { type: Date, index: true }
})

var EthPeer = mongoose.model('Peer', peerSchema)

const EthPeer = db.EthPeer
const conn = db.getConnection()
const PRIVATE_KEY = randomBytes(32)
const BOOTNODES = require('ethereum-common').bootstrapNodes.map((node) => {
return {
@@ -84,62 +56,60 @@ const rlpx = new devp2p.RLPx(PRIVATE_KEY, {
rlpx.on('error', (err) => console.error(chalk.red(`RLPx error: ${err.stack || err}`)))

rlpx.on('peer:added', (peer) => {
var hello = peer.getHelloMessage()
var b = new EthPeer()
b.enode = hello.id.toString('hex')
b.address = peer._socket.remoteAddress
b.port = peer._socket.remotePort
var peerGeo = geoip.lookup(b.address)
if ((peerGeo) && (peerGeo.ll) && (peerGeo.ll.country)) {
b.country = peerGeo.ll.country
}
if ((peerGeo) && (peerGeo.city)) {
b.city = peerGeo.city
}
b.latitude = peerGeo.ll[0]
b.longitude = peerGeo.ll[1]
b.clientId = hello.clientId
var splitClientId = hello.clientId.split('/')
b.clientMeta1 = splitClientId[0]
b.clientMeta2 = splitClientId[1]
b.clientMeta3 = splitClientId[2]
b.clientMeta4 = splitClientId[3]
b.capabilities = hello.capabilities
b.timestamp = new Date()
const eth = peer.getProtocols()[0]
eth.sendStatus(myStatus)
eth.once('status', (peerStatus) => {
// console.log('received STATUS')
b.bestHash = '0x' + peerStatus.bestHash.toString('hex')
b.totalDifficulty = peerStatus.td.toString('hex')
web3.eth.getBlock(b.bestHash, false)
.then(function (block) {
if ((block) && (block.number)) {
b.bestBlockNumber = block.number
} else {
web3.eth.getBlockNumber()
.then(function (infuraBlockNumber) {
b.infuraBlockNumber = infuraBlockNumber
b.infuraDrift = Math.abs(b.infuraBlockNumber - b.bestBlockNumber)
mongoose.connect(MONGO_URL)
db.on('error', console.error.bind(console, 'connection error:'))
db.once('open', function () {
b.save()
console.log('Saved peer: ' + b.enode)
})
.catch(function (err) {
console.error(err)
})
})
.catch(function (err) {
console.log(err)
let hello = peer.getHelloMessage()
let peerGeo = geoip.lookup(peer._socket.remoteAddress)
let b = new EthPeer({
address: peer._socket.remoteAddress,
capabilitites: hello.capabilities,
clientId: hello.clientId,
enode: hello.id.toString('hex'),
port: peer._socket.remotePort,
timestamp: new Date(),
})

if (_.has(peerGeo, 'll.country')) {
b.country = peerGeo.ll.country
}
if (_.has(peerGeo, 'city')) {
b.city = peerGeo.city
}
b.latitude = peerGeo.ll[0]
b.longitude = peerGeo.ll[1]

var splitClientId = hello.clientId.split('/')
b.clientMeta1 = splitClientId[0]
b.clientMeta2 = splitClientId[1]
b.clientMeta3 = splitClientId[2]
b.clientMeta4 = splitClientId[3]

const eth = peer.getProtocols()[0]
eth.sendStatus(myStatus)
eth.once('status', (peerStatus) => {
debug(`Received status ${peer._socket.remoteAddress}`)
b.bestHash = '0x' + peerStatus.bestHash.toString('hex')
b.totalDifficulty = peerStatus.td.toString('hex')
web3.eth.getBlock(b.bestHash, false)
.then((block) => {
if (_.has(block, 'number')) {
debug(`Received: ${block.number}`)
b.bestBlockNumber = block.number
}
return web3.eth.getBlockNumber()
})
.then((infuraBlockNumber) => {
debug(`Infura Block: ${infuraBlockNumber}`)
b.infuraBlockNumber = infuraBlockNumber
b.infuraDrift = Math.abs(b.infuraBlockNumber - b.bestBlockNumber)
debug(`Found Drift: ${b.infuraDrift}`)
// db.on('error', console.error.bind(console, 'connection error:'))
b.save().then((ethpeer) => {
debug('Saved peer: ' + ethpeer.enode)
})
}
})
.catch(function (err) {
console.log(err)
})
})
})
.catch((err) => {
console.error(err)
})
})
})

rlpx.on('peer:removed', (peer) => {
@@ -149,6 +119,7 @@ rlpx.on('peer:removed', (peer) => {
})

for (let bootnode of BOOTNODES) {
debug(`Connecting to ${bootnode.address}`)
dpt.bootstrap(bootnode).catch((err) => console.error(chalk.bold.red(err.stack || err)))
}

@@ -1,4 +1,4 @@
version: '1.0'
version: '3.3'

services:

@@ -0,0 +1,49 @@
const mongoose = require('mongoose')
const _ = require('lodash')
const debug = require('debug')('infura:db')

const Schema = mongoose.Schema
const EthPeerSchema = new Schema({
address:{type: String, index: true},
bestBlockNumber: {type: Number, index: true, default: 0},
bestHash: String,
capabilities: Array,
clientId:{type: String, index: true},
clientMeta1:{type: String, index: true},
clientMeta2:{type: String, index: true},
clientMeta3:{type: String, index: true},
clientMeta4:{type: String, index: true},
country: String,
enode: {type: String, index: true, unique: true},
infuraBlockNumber: {type: Number, index: true, default: 0},
infuraDrift: {type: Number, index: true, default: 0},
latitude: Number,
longitude: Number,
numPeers: {type: Number, default: 0},
port: Number,
timestamp:{type: Date, index: true},
totalDifficulty: String,
})

module.exports = (opts) => {
const options = _.defaults(opts || {}, {
dbUri: 'mongodb://localhost:27017',
dbName: 'devp2p'
})

// Connect to the database
const connectionUri = `${options.dbUri}/${options.dbName}`
debug(`Connecting to ${connectionUri}`)
const conn = mongoose.createConnection(connectionUri, {useNewUrlParser: true})
conn.on('error', (err) => { console.error(err) })
conn.then(
() => { debug('DB Connected...')},
(err) => { debug(`DB Connection Error: ${err}`)}
)

// Connect the models
const EthPeer = conn.model('Peer', EthPeerSchema)

const getConnection = () => { return conn }
return { EthPeer, getConnection }
}

Some generated files are not rendered by default. Learn more.

@@ -17,6 +17,7 @@
"kinesalite": "^1.14.0",
"leveldown": "^4.0.1",
"levelup": "^3.1.1",
"lodash": "^4.17.11",
"lru-cache": "^4.1.5",
"moment": "^2.23.0",
"mongodb": "^3.1.10",
@@ -39,7 +40,7 @@
"eslint-plugin-standard": "^4.0.0"
},
"scripts": {
"start": "node app.js",
"start": "INFURA_ID=09bcdf3656ee4a80b05a4a921358d109 node app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": {

0 comments on commit 0d66b02

Please sign in to comment.
You can’t perform that action at this time.