Skip to content

Commit

Permalink
Move Bitcoin client into ~/bin and add task to enroll node in Bitnode…
Browse files Browse the repository at this point in the history
…s Incentive Program
  • Loading branch information
ayeowch committed May 20, 2015
1 parent 926d776 commit 8230572
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 15 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ Build and install Bitcoin client from source.
$ ./configure --without-gui --without-miniupnpc --disable-wallet
$ make
$ make check
$ sudo make install
$ mkdir ~/bin
$ cp src/bitcoind src/bitcoin-cli ~/bin/

The administration page and the public status page for your Bitnodes Hardware are powered by the same [Django](https://www.djangoproject.com/) project installed inside a virtualenv environment. Install virtualenv and pip to manage Python packages inside the virtualenv environment.

Expand Down Expand Up @@ -210,7 +211,7 @@ Use supervisor to start all the necessary processes. The administration page is

$ ./manage.py supervisor

To start or stop Bitcoin client manually using supervisor. The project expects the executable to exist at `/usr/local/bin/bitcoind`.
To start or stop Bitcoin client manually using supervisor. The project expects the executable to exist at `~/bin/bitcoind`.

$ ./manage.py supervisor start hardware-bitcoind
$ ./manage.py supervisor stop hardware-bitcoind
Expand All @@ -229,7 +230,7 @@ In order to run the project in debug mode, i.e. `settings.DEBUG=True`, bootstrap

To start celery without using supervisor.

$ celery worker -A hardware -B --loglevel=INFO
$ celery worker -A hardware -B --loglevel=DEBUG

Execute the following command to monitor all log files written by the project in production mode.

Expand Down
8 changes: 8 additions & 0 deletions hardware/administration/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
from django.contrib.sites.models import Site
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models.signals import post_save

from .tasks import register_node_task

B58CHARS = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'

Expand Down Expand Up @@ -97,6 +100,11 @@ def __str__(self):
return self.bitcoin_address


def register_node(sender, instance, **kwargs):
register_node_task.delay(instance.bitcoin_address)
post_save.connect(register_node, sender=BitcoinAddress)


class Bandwidth(models.Model):
"""
Only 1 instance of this model is allowed per site.
Expand Down
35 changes: 34 additions & 1 deletion hardware/administration/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import os
import platform
import pytz
import requests
import subprocess
import time
from celery.signals import celeryd_init
from datetime import datetime
from psutil import boot_time, disk_partitions, disk_usage, virtual_memory
Expand Down Expand Up @@ -120,6 +120,39 @@ def system_info_task():
cache.set('system_info', system_info, 3600)


@app.task
def register_node_task(bitcoin_address):
"""
Enrolls the node in the Bitnodes Incentive Program if it is receiving
incoming connections: https://getaddr.bitnodes.io/nodes/incentive/.
Node must be activated separately by owner from:
https://getaddr.bitnodes.io/nodes/<ADDRESS>-<PORT>/
"""
node_status = cache.get('node_status')
if node_status is None:
return
wan_address = node_status.get('wan_address', '')
port = node_status.get('port', '')
connections = node_status.get('connections', '')
if wan_address and port and connections and int(connections) > 8:
url = 'https://getaddr.bitnodes.io/api/v1/nodes/{}-{}/'.format(wan_address, port)
headers = {
'user-agent': settings.USER_AGENT,
'accept': 'application/json',
}
data = {
'bitcoin_address': bitcoin_address,
'url': 'http://{}'.format(wan_address),
}
try:
response = requests.post(url, headers=headers, data=data, verify=False)
except requests.exceptions.RequestException as err:
logger.debug(err)
else:
logger.debug(response.json())


@celeryd_init.connect
def startup_task(sender=None, conf=None, **kwargs):
cache.clear()
Expand Down
5 changes: 4 additions & 1 deletion hardware/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@
'Please set NETWORK_INTERFACE in settings.py manually.')

# Supervisor will not start Bitcoin client if it does not exist
BITCOIND = '/usr/local/bin/bitcoind'
BITCOIND = os.path.expanduser('~/bin/bitcoind')
if not os.path.isfile(BITCOIND):
BITCOIND = None

Expand Down Expand Up @@ -245,6 +245,9 @@
RPC_USER = _conf.get('bitcoind', 'rpcuser')
RPC_PASSWORD = _conf.get('bitcoind', 'rpcpassword')

# User agent to use for outgoing HTTP requests
USER_AGENT = 'bitnodes-hardware/1.0'

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
Expand Down
4 changes: 1 addition & 3 deletions hardware/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ def get_wan_address():
wan_address = cache.get('wan_address')
if wan_address is None:
url = 'https://dazzlepod.com/ip/me.json'
headers = {
'user-agent': 'bitnodes-hardware/1.0',
}
headers = {'user-agent': settings.USER_AGENT}
try:
response = requests.get(url, headers=headers)
except requests.exceptions.RequestException as err:
Expand Down
8 changes: 4 additions & 4 deletions poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def get(self):
self.bytes_recv = bytes_recv

return {
'sent_bps': int(self.sent_bps),
'recv_bps': int(self.recv_bps),
'o': int(self.sent_bps),
'i': int(self.recv_bps),
}

def _get_data(self):
Expand All @@ -120,8 +120,8 @@ def publish():
network_stat = NetworkStat(settings.NETWORK_INTERFACE)
while True:
tick = {
'timestamp': int(time.time() * 1000), # In ms
'network': network_stat.get(),
't': int(time.time() * 1000), # In ms
'net': network_stat.get(),
}
PollHandler.ticks.append(tick)
message = json.dumps([tick])
Expand Down
6 changes: 3 additions & 3 deletions templates/dashboard/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ <h4>Received<span class="pull-right text-muted"><span class="recv_kbps"></span><
var animate = false;

for (var i = 0; i < data_len; i++) {
var timestamp = data[i].timestamp;
var sent_kbps = data[i].network.sent_bps / 1000;
var recv_kbps = data[i].network.recv_bps / 1000;
var timestamp = data[i].t;
var sent_kbps = data[i].net.o / 1000;
var recv_kbps = data[i].net.i / 1000;

sentKbpsSeries.addPoint(
[timestamp, sent_kbps],
Expand Down

0 comments on commit 8230572

Please sign in to comment.