Browse Source

don't use bitcoinrpc anymore

283
ThomasV 13 years ago
parent
commit
9f4eb222ac
  1. 10
      server/README
  2. 41
      server/patches/bitcoinrpc_connection.py.diff
  3. 38
      server/server.py

10
server/README

@ -7,9 +7,10 @@ Language: Python
Features: Features:
* The server uses a patched version of the Bitcoin daemon that can * The server uses a patched version of the Bitcoin daemon that can
forward transactions, and bitcoin-abe * The server code is open forward transactions, and bitcoin-abe
source. Anyone can run a server, removing single points of failure
concerns. * The server code is open source. Anyone can run a server, removing
single points of failure concerns.
* The server knows which set of Bitcoin addresses belong to the same * The server knows which set of Bitcoin addresses belong to the same
wallet, which might raise concerns about anonymity. However, it should wallet, which might raise concerns about anonymity. However, it should
@ -22,8 +23,7 @@ INSTALL
2. install bitcoin-abe : https://github.com/jtobey/bitcoin-abe 2. install bitcoin-abe : https://github.com/jtobey/bitcoin-abe
3. download bitcoin-python: https://github.com/laanwj/bitcoin-python 3. install jsonrpclib: code.google.com/p/jsonrpclib/
patch it with bitcoinrpc_connection.py.diff
4. launch the server: nohup python -u server.py > /var/log/electrum.log & 4. launch the server: nohup python -u server.py > /var/log/electrum.log &

41
server/patches/bitcoinrpc_connection.py.diff

@ -1,41 +0,0 @@
diff --git a/src/bitcoinrpc/connection.py b/src/bitcoinrpc/connection.py
index 960cf5d..79e04bf 100644
--- a/src/bitcoinrpc/connection.py
+++ b/src/bitcoinrpc/connection.py
@@ -67,7 +67,35 @@ class BitcoinConnection(object):
return self.proxy.getblockcount()
except JSONRPCException,e:
raise _wrap_exception(e.error)
-
+
+ def getmemorypool(self):
+ """sign"""
+ try:
+ return self.proxy.getmemorypool()
+ except JSONRPCException,e:
+ raise _wrap_exception(e.error)
+
+ def importtransaction(self, tx):
+ """sign"""
+ try:
+ return self.proxy.importtransaction(tx)
+ except JSONRPCException,e:
+ raise _wrap_exception(e.error)
+
+ def signmessage(self, address,message):
+ """sign"""
+ try:
+ return self.proxy.signmessage(address, message)
+ except JSONRPCException,e:
+ raise _wrap_exception(e.error)
+
+ def verifymessage(self, address, sig, message):
+ """verif"""
+ try:
+ return self.proxy.verifymessage(address, sig, message )
+ except JSONRPCException,e:
+ raise _wrap_exception(e.error)
+
def getblocknumber(self):
"""
Returns the block number of the latest block in the longest block chain.

38
server/server.py

@ -27,13 +27,14 @@ Todo:
import time, socket, operator, thread, ast, sys,re import time, socket, operator, thread, ast, sys,re
import psycopg2, binascii import psycopg2, binascii
import bitcoinrpc
from Abe.abe import hash_to_address, decode_check_address from Abe.abe import hash_to_address, decode_check_address
from Abe.DataStore import DataStore as Datastore_class from Abe.DataStore import DataStore as Datastore_class
from Abe import DataStore, readconf, BCDataStream, deserialize, util, base58 from Abe import DataStore, readconf, BCDataStream, deserialize, util, base58
import ConfigParser import ConfigParser
from json import dumps, loads
import urllib
config = ConfigParser.ConfigParser() config = ConfigParser.ConfigParser()
# set some defaults, which will be overwritten by the config file # set some defaults, which will be overwritten by the config file
@ -57,6 +58,7 @@ except:
print "Could not read electrum.conf. I will use the default values." print "Could not read electrum.conf. I will use the default values."
password = config.get('server','password') password = config.get('server','password')
bitcoind_url = 'http://%s:%s@%s:%s/' % ( config.get('bitcoind','user'), config.get('bitcoind','password'), config.get('bitcoind','host'), config.get('bitcoind','port'))
stopping = False stopping = False
block_number = -1 block_number = -1
@ -317,15 +319,18 @@ class MyStore(Datastore_class):
def send_tx(tx): def send_tx(tx):
import bitcoinrpc postdata = dumps({"method": 'importtransaction', 'params': [tx], 'id':'jsonrpc'})
conn = bitcoinrpc.connect_to_local() respdata = urllib.urlopen(bitcoind_url, postdata).read()
try: try:
v = conn.importtransaction(tx) v = loads(respdata)['result']
except: except:
v = "error: transaction rejected by memorypool" v = "error: transaction rejected by memorypool"
return v return v
def random_string(N): def random_string(N):
import random, string import random, string
return ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(N)) return ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(N))
@ -364,7 +369,7 @@ def get_cache(pw,addr):
def cmd_poll(session_id): def cmd_poll(session_id):
session = sessions.get(session_id) session = sessions.get(session_id)
if session is None: if session is None:
print time.asctime(), "session not found", session_id, ipaddr print time.asctime(), "session not found", session_id
out = repr( (-1, {})) out = repr( (-1, {}))
else: else:
t1 = time.time() t1 = time.time()
@ -412,7 +417,7 @@ def new_session(addresses, version, ipaddr):
sessions[session_id]['last_time'] = time.time() sessions[session_id]['last_time'] = time.time()
return out return out
def update_session(session_id,addresses): def update_session(session_id,addresses,ipaddr):
print time.strftime("[%d/%m/%Y-%H:%M:%S]"), "update session", ipaddr, addresses[0] if addresses else addresses, len(addresses) print time.strftime("[%d/%m/%Y-%H:%M:%S]"), "update session", ipaddr, addresses[0] if addresses else addresses, len(addresses)
sessions[session_id]['addresses'] = {} sessions[session_id]['addresses'] = {}
for a in addresses: for a in addresses:
@ -488,7 +493,7 @@ def do_command(cmd, data, ipaddr):
except: except:
print "error" print "error"
return None return None
out = update_session(session_id,addresses) out = update_session(session_id,addresses,ipaddr)
elif cmd == 'bccapi_login': elif cmd == 'bccapi_login':
@ -570,12 +575,12 @@ def do_command(cmd, data, ipaddr):
def memorypool_update(store): def memorypool_update(store):
ds = BCDataStream.BCDataStream() ds = BCDataStream.BCDataStream()
store.mempool_keys = [] store.mempool_keys = []
conn = bitcoinrpc.connect_to_local()
try: postdata = dumps({"method": 'getmemorypool', 'params': [], 'id':'jsonrpc'})
v = conn.getmemorypool() respdata = urllib.urlopen(bitcoind_url, postdata).read()
except: v = loads(respdata)['result']
print "cannot contact bitcoin daemon"
return
v = v['transactions'] v = v['transactions']
for hextx in v: for hextx in v:
ds.clear() ds.clear()
@ -651,13 +656,14 @@ def jsonrpc_thread(store):
# see http://code.google.com/p/jsonrpclib/ # see http://code.google.com/p/jsonrpclib/
from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer
server = SimpleJSONRPCServer(('localhost', 8080)) server = SimpleJSONRPCServer(('localhost', 8080))
server.register_function(store.get_history, 'history')
server.register_function(lambda : peer_list.values(), 'peers') server.register_function(lambda : peer_list.values(), 'peers')
server.register_function(cmd_stop, 'stop') server.register_function(cmd_stop, 'stop')
server.register_function(cmd_load, 'load') server.register_function(cmd_load, 'load')
server.register_function(lambda : block_number, 'blocks') server.register_function(lambda : block_number, 'blocks')
server.register_function(clear_cache, 'clear_cache') server.register_function(clear_cache, 'clear_cache')
server.register_function(get_cache, 'get_cache') server.register_function(get_cache, 'get_cache')
server.register_function(send_tx, 'blockchain.transaction.broadcast')
server.register_function(store.get_history, 'blockchain.address.get_history')
server.serve_forever() server.serve_forever()
@ -681,7 +687,9 @@ if __name__ == '__main__':
elif cmd == 'get_cache': elif cmd == 'get_cache':
out = server.get_cache(password,sys.argv[2]) out = server.get_cache(password,sys.argv[2])
elif cmd == 'h': elif cmd == 'h':
out = server.history(sys.argv[2]) out = server.blockchain.address.get_history(sys.argv[2])
elif cmd == 'tx':
out = server.blockchain.transaction.broadcast(sys.argv[2])
elif cmd == 'b': elif cmd == 'b':
out = server.blocks() out = server.blocks()
print out print out

Loading…
Cancel
Save