Initial commit
[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 # This is also very basic example how to use Stratum protocol in native Python
6
7 import socket
8 import json
9 import sys
10 import argparse
11 import time
12
13 start = time.time()
14
15 parser = argparse.ArgumentParser(description='Send notification to Stratum instance about new bitcoin block.')
16 parser.add_argument('--password', dest='password', type=str, help='use admin password from Stratum server config')
17 parser.add_argument('--host', dest='host', type=str, default='localhost', help='hostname of Stratum mining instance')
18 parser.add_argument('--port', dest='port', type=int, default=3333, help='port of Stratum mining instance')
19
20 args = parser.parse_args()
21
22 if args.password == None:
23         parser.print_help()
24         sys.exit()
25         
26 message = {'id': 1, 'method': 'mining.update_block', 'params': [args.password]}
27
28 try:
29     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
30     s.connect((args.host, args.port))
31     s.sendall(json.dumps(message)+"\n")
32     data = s.recv(16000)
33     s.close()
34 except IOError:
35     print "blocknotify: Cannot connect to the pool"
36     sys.exit()
37
38 for line in data.split("\n"):
39     if not line.strip():
40         # Skip last line which doesn't contain any message
41         continue
42
43     message = json.loads(line)
44     if message['id'] == 1:
45         if message['result'] == True:
46                 print "blocknotify: done in %.03f sec" % (time.time() - start)
47         else:
48             print "blocknotify: Error during request:", message['error'][1]
49     else:
50         print "blocknotify: Unexpected message from the server:", message