Skip to content
Permalink
Browse files

Changed backend to postgres. (INFURA#4)

  • Loading branch information...
kshinn authored and egalano committed Dec 23, 2018
1 parent 0d66b02 commit a80e2f095cbacb5c0aa0310d65d73fc97f86a296
Showing with 324 additions and 212 deletions.
  1. +4 −3 README.md
  2. +8 −9 docker-compose.yml
  3. +3 −3 app.js → index.js
  4. +72 −37 lib/db.js
  5. +233 −157 package-lock.json
  6. +4 −3 package.json
@@ -33,10 +33,11 @@ Email: [anything]
...
```
```
Database Info:
Hostname: mongo
Port: 27017
Hostname: pg
Port: 5432
Username: pguser
Password: 3th3r3um
Database Name: devp2p
```
@@ -1,17 +1,16 @@
version: '3.3'

services:

mongo:
image: mongo
restart: always
ports:
- 27017:27017
mongo-express:
image: mongo-express
pg:
image: postgres:11
restart: always
ports:
- 8081:8081
- 5432:5432
environment:
POSTGRES_USER: pguser
POSTGRES_PASSWORD: 3th3r3um
POSTGRES_DB: devp2p

metabase:
image: metabase/metabase
restart: always
@@ -13,7 +13,7 @@ const web3Url = `https://mainnet.infura.io/v3/${process.env.INFURA_ID}`
web3.setProvider(new web3.providers.HttpProvider(web3Url))

const EthPeer = db.EthPeer
const conn = db.getConnection()

const PRIVATE_KEY = randomBytes(32)
const BOOTNODES = require('ethereum-common').bootstrapNodes.map((node) => {
return {
@@ -99,14 +99,14 @@ rlpx.on('peer:added', (peer) => {
.then((infuraBlockNumber) => {
debug(`Infura Block: ${infuraBlockNumber}`)
b.infuraBlockNumber = infuraBlockNumber
b.infuraDrift = Math.abs(b.infuraBlockNumber - b.bestBlockNumber)
b.infuraDrift = Math.abs(b.infuraBlockNumber - b.bestBlockNumber) || 0
debug(`Found Drift: ${b.infuraDrift}`)
// db.on('error', console.error.bind(console, 'connection error:'))
b.save().then((ethpeer) => {
debug('Saved peer: ' + ethpeer.enode)
})
})
.catch((err) => {
.catch(function (err) {
console.error(err)
})
})
109 lib/db.js
@@ -1,49 +1,84 @@
const mongoose = require('mongoose')
const _ = require('lodash')
const Sequelize = require('sequelize')
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,
})
const EthPeerSchema = {
address: { type: Sequelize.STRING, primary_key: true },
bestBlockNumber: {type: Sequelize.INTEGER(11).UNSIGNED, default: 0},
bestHash: { type: Sequelize.STRING },
capabilities: { type: Sequelize.ARRAY(Sequelize.STRING) },
clientId:{type: Sequelize.STRING, index: true},
clientMeta1:{type: Sequelize.STRING, index: true},
clientMeta2:{type: Sequelize.STRING, index: true},
clientMeta3:{type: Sequelize.STRING, index: true},
clientMeta4:{type: Sequelize.STRING, index: true},
country: {type: Sequelize.STRING },
enode: {type: Sequelize.STRING, unique: true},
infuraBlockNumber: {type: Sequelize.INTEGER(11).UNSIGNED, default: 0},
infuraDrift: {type: Sequelize.INTEGER(11), default: 0},
latitude: {type: Sequelize.FLOAT },
longitude: {type: Sequelize.FLOAT },
numPeers: {type: Sequelize.INTEGER(11).UNSIGNED, default: 0},
port: {type: Sequelize.INTEGER(11).UNSIGNED},
timestamp:{type: Sequelize.DATE, index: true},
totalDifficulty: {type: Sequelize.STRING},
}

module.exports = (opts) => {
const options = _.defaults(opts || {}, {
dbUri: 'mongodb://localhost:27017',
dbUser: 'pguser',
dbPass: '3th3r3um',
dbHost: 'localhost',
dbPort: 5432,
dbName: 'devp2p'
})
const {dbUser, dbPass, dbHost, dbName} = options
const conn = new Sequelize(dbName, dbUser, dbPass, {
host: dbHost,
dialect: 'postgres',
operatorAliases: false,
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
});
conn.authenticate()
.then(() => {
debug('DB Connected...')
})
.catch((err) => {
console.error('Unable to connect to the database:', err);
})

// 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 EthPeer = conn.define('peers', EthPeerSchema, {
indexes: [
{
fields: ['bestBlockNumber'],
method: 'BTREE'
},
{
fields: ['enode'],
unique: true
},
{
fields: ['infuraBlockNumber'],
method: 'BTREE'
},
{
fields: ['infuraDrift'],
method: 'BTREE'
},
{ fields: ['timestamp'],
method: 'BTREE'
}
]
})

const getConnection = () => { return conn }
EthPeer.sync().then(() => {
debug('EthPeers table created')
})
const getConnection = () => { return conn }
return { EthPeer, getConnection }
}

0 comments on commit a80e2f0

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