Skip to content

Commit

Permalink
Merge pull request #2 from BCH-Consolidating-CoinJoin/unstable
Browse files Browse the repository at this point in the history
Testing sematic release
  • Loading branch information
christroutner committed Dec 1, 2018
2 parents cfd3973 + c6e82cc commit e92ee4f
Show file tree
Hide file tree
Showing 14 changed files with 3,959 additions and 7,808 deletions.
16 changes: 9 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,35 @@ RUN chown -R coinjoin .npm-global
RUN echo "export PATH=~/.npm-global/bin:$PATH" >> /home/coinjoin/.profile
RUN runuser -l coinjoin -c "npm config set prefix '~/.npm-global'"

# Expose the port the API will be served on.
EXPOSE 5000

# Switch to user account.
USER coinjoin
# Prep 'sudo' commands.
RUN echo 'password' | sudo -S pwd

# Clone the rest.bitcoin.com repository
WORKDIR /home/coinjoin
RUN git clone https://github.com/BCH-Consolidating-CoinJoin/ccoinjoin-server
RUN git clone https://github.com/BCH-Consolidating-CoinJoin/ccoinjoin-mirror

# Switch to the desired branch. `master` is usually stable,
# and `stage` has the most up-to-date changes.
WORKDIR /home/coinjoin/ccoinjoin-server
WORKDIR /home/coinjoin/ccoinjoin-mirror

# For development: switch to unstable branch
RUN git checkout unstable

# Install dependencies
RUN npm install

VOLUME /home/coinjoin/ccoinjoin-server/logs
VOLUME /home/coinjoin/ccoinjoin-mirror/logs

# Expose the port the API will be served on.
EXPOSE 4001
EXPOSE 4002
EXPOSE 4003
EXPOSE 5000

# Start the application.
COPY start-production start-production
COPY make-keys-dir-readable make-keys-dir-readable
CMD ["./start-production"]

#CMD ["npm", "start"]
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ This is a [koa api server](https://github.com/christroutner/babel-free-koa2-api-

[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com)

[![Build Status](https://travis-ci.org/BCH-Consolidating-CoinJoin/consolidating-coinjoin.svg?branch=master)](https://travis-ci.org/BCH-Consolidating-CoinJoin/consolidating-coinjoin)
[![Build Status](https://travis-ci.org/BCH-Consolidating-CoinJoin/ccoinjoin-mirror.svg?branch=master)](https://travis-ci.org/BCH-Consolidating-CoinJoin/ccoinjoin-mirror)

[![Coverage Status](https://coveralls.io/repos/github/BCH-Consolidating-CoinJoin/ccoinjoin-mirror/badge.svg?branch=master)](https://coveralls.io/github/BCH-Consolidating-CoinJoin/ccoinjoin-mirror?branch=master)

[![Coverage Status](https://coveralls.io/repos/github/BCH-Consolidating-CoinJoin/consolidating-coinjoin/badge.svg?branch=master)](https://coveralls.io/github/BCH-Consolidating-CoinJoin/consolidating-coinjoin?branch=master)


[![Greenkeeper badge](https://badges.greenkeeper.io/BCH-Consolidating-CoinJoin/consolidating-coinjoin.svg)](https://greenkeeper.io/)
[![Greenkeeper badge](https://badges.greenkeeper.io/BCH-Consolidating-CoinJoin/ccoinjoin-mirror.svg)](https://greenkeeper.io/)


## Requirements
Expand Down
80 changes: 80 additions & 0 deletions bin/server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
This server acts as an IPFS peer for connecting to the IPFS network and
mirroring the Consolidating CoinJoin database of peers. The workflow is as
follows:
-Initialize IPFS
-Connect to the boostrap IPFS nodes
-Connect to any previously validated IPFS nodes.
-Open the Consolidating CoinJoin database via OrbitDB
-Add the connection information for this peer to the database.
-Periodically review the database and validate peers listed in the DB. Validated
peers are added to this nodes verifiedPeers list.
-Periodically rebroadcast this peers connection information by adding a new
entry to the database.
*/

'use strict'

const Koa = require('koa')
const bodyParser = require('koa-bodyparser')
const convert = require('koa-convert')
Expand All @@ -9,6 +26,18 @@ const mount = require('koa-mount')
const serve = require('koa-static')
const cors = require('kcors')

const util = require('util')
util.inspect.defaultOptions = { depth: 1 }

// P2P library used to connect to find and connect to other peers in the IPFS network.
const P2P = require('../src/utils/p2p')

// Load the ccoinjoin-network library.
// const Network = require('../../ccoinjoin-network')
const Network = require('ccoinjoin-network')
const network = new Network()
const UPDATE_PERIOD = 1000 * 60 // 1 minute

// Winston logger
const wlogger = require('../src/utils/logging')

Expand Down Expand Up @@ -66,10 +95,61 @@ async function startServer () {
console.log(`Server started on ${config.port}`)
wlogger.info(`Server started on ${config.port}`)

if (process.env.COINJOIN_ENV !== 'test') {
// Connect to the IPFS network and subscribe to the DB.
await network.connectToIPFS()

// Initialze the P2P library
const p2p = new P2P(network)

// Connect to the known peers
await p2p.connectToPeers()

// Connect to the Consolidating CoinJoin OrbitDB.
await network.connectToOrbitDB(config.orbitDBAddr)

// Broadcast server information onto the network.
const writeHash = await network.writeDB(p2p.ipfsData)
console.log(`Added this information to the OrbitDB: ${JSON.stringify(p2p.ipfsData, null, 2)}`)
console.log(`writeHash: ${writeHash}`)

// Create a timer that periodically updates the server information on the DB.
setInterval(async function () {
console.log(`DB has synced: ${network.dbHasSynced}`)

const peers = await network.ipfs.swarm.peers()
console.log(`peers: ${peers.length}`)

// await network.writeDB(serverConfig)

let latest = await network.readDB()
const servers = getUniquePeers(latest)
console.log(`servers: ${JSON.stringify(servers, null, 2)}`)
}, UPDATE_PERIOD)
}

return app
}
// startServer()

// Gets the mutliaddr for unique peers
function getUniquePeers (dbRawData) {
const payloads = dbRawData.map(entry => entry.payload.value)
// console.log(`payloads: ${JSON.stringify(payloads, null, 2)}`)

const peers = payloads.map(entry => entry.peerHash)
// console.log(`peers: ${JSON.stringify(peers, null, 2)}`)

const uniquePeers = peers.filter(getUnique)
return uniquePeers
}

// A filter function for identifying unique entries.
// https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates
function getUnique (value, index, self) {
return self.indexOf(value) === index
}

// export default app
// module.exports = app
module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion build-image
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
docker build -t ccoinjoin .
docker build -t ccoinjoin-mirror .
9 changes: 8 additions & 1 deletion config/env/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,12 @@ export default {
module.exports = {
session: 'secret-boilerplate-token',
token: 'secret-jwt-token',
database: 'mongodb://localhost:27017/coinjoin-dev'
database: 'mongodb://localhost:27017/coinjoin-dev',
port: 5000,
ipfsData: {
peerHash: '',
multiaddr: '',
behindFirewall: true
},
orbitDBAddr: '/orbitdb/QmSFYQzb4MtowNsZ6hYhBqvQAdPxLUFGj9RH5XY32TTFvU/ccoinjoin'
}
22 changes: 8 additions & 14 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Start the testnet server with the command 'docker-compose up -d'

ccoinjoin-mongodb:
ccoinjoin-mirror-mongodb:
image: mongo
ports:
- "3500:27017" # <host port>:<container port>
Expand All @@ -9,24 +9,18 @@ ccoinjoin-mongodb:
command: mongod --smallfiles --logpath=/dev/null # -- quiet
restart: always

ccoinjoin:
ccoinjoin-mirror:
build: .
dockerfile: Dockerfile
links:
- ccoinjoin-mongodb
- ccoinjoin-mirror-mongodb
ports:
- "5000:5000" # <host port>:<container port>
- "4001:4001" # <host port>:<container port>
- "4002:4002"
- "4003:4003"
- "5000:5000"
volumes:
- ./logs:/home/coinjoin/consolidating-coinjoin/logs
- ./keys:/home/coinjoin/consolidating-coinjoin/keys
# - ./keys:/home/coinjoin/consolidating-coinjoin/keys

restart: always

tor:
image: goldy/tor-hidden-service
links:
- ccoinjoin
environment:
PORT_MAP: 80 # Map port to detected service
volumes:
- ./keys:/var/lib/tor/hidden_service/
Loading

0 comments on commit e92ee4f

Please sign in to comment.