Helper script for ./bitcoind -blocknotify
[stratum-mining.git] / scripts / blocknotify.sh
1 #!/usr/bin/env python
2 # Send notification to Stratum mining instance on localhost that there's new bitcoin block
3 # You can use this script directly as an variable for -blocknotify argument:
4 #       ./bitcoind -blocknotify="blocknotify.sh --password admin_password"
5
6 import socket
7 import json
8 import sys
9 import argparse
10 import time
11
12 start = time.time()
13
14 parser = argparse.ArgumentParser(description='Send notification to Stratum instance about new bitcoin block.')
15 parser.add_argument('--password', dest='password', type=str, help='use admin password from Stratum server config')
16 parser.add_argument('--host', dest='host', type=str, default='localhost', help='hostname of Stratum mining instance')
17 parser.add_argument('--port', dest='port', type=int, default=3333, help='port of Stratum mining instance')
18
19 args = parser.parse_args()
20
21 if args.password == None:
22         parser.print_help()
23         sys.exit()
24         
25 message = {'id': 1, 'method': 'mining.update_block', 'params': [args.password]}
26
27 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
28 s.connect((args.host, args.port))
29 s.sendall(json.dumps(message)+"\n")
30 data = s.recv(16000)
31 s.close()
32
33 for line in data.split("\n"):
34     if not line.strip():
35         continue
36
37     message = json.loads(line)
38     if message['id'] == 1:
39         if message['result'] == True:
40                 print "blocknotify done in %.03f sec" % (time.time() - start)
41         else:
42             print "Error during request:", message['error'][1]
43     else:
44         print "Unexpected message from the server:", message