new workflow
[electrum-server.git] / abe_backend.py
1 from Abe.abe import hash_to_address, decode_check_address
2 from Abe.DataStore import DataStore as Datastore_class
3 from Abe import DataStore, readconf, BCDataStream,  deserialize, util, base58
4
5 import psycopg2, binascii
6
7 import thread, traceback, sys, urllib, operator
8 from json import dumps, loads
9 from Queue import Queue
10 import time
11
12 class AbeStore(Datastore_class):
13
14     def __init__(self, config):
15         conf = DataStore.CONFIG_DEFAULTS
16         args, argv = readconf.parse_argv( [], conf)
17         args.dbtype = config.get('database','type')
18         if args.dbtype == 'sqlite3':
19             args.connect_args = { 'database' : config.get('database','database') }
20         elif args.dbtype == 'MySQLdb':
21             args.connect_args = { 'db' : config.get('database','database'), 'user' : config.get('database','username'), 'passwd' : config.get('database','password') }
22         elif args.dbtype == 'psycopg2':
23             args.connect_args = { 'database' : config.get('database','database') }
24
25         Datastore_class.__init__(self,args)
26
27         self.tx_cache = {}
28         self.mempool_keys = {}
29         self.bitcoind_url = 'http://%s:%s@%s:%s/' % ( config.get('bitcoind','user'), config.get('bitcoind','password'), config.get('bitcoind','host'), config.get('bitcoind','port'))
30
31         self.address_queue = Queue()
32
33         self.dblock = thread.allocate_lock()
34         self.block_number = -1
35         self.watched_addresses = []
36
37
38
39     def import_block(self, b, chain_ids=frozenset()):
40         block_id = super(AbeStore, self).import_block(b, chain_ids)
41         for pos in xrange(len(b['transactions'])):
42             tx = b['transactions'][pos]
43             if 'hash' not in tx:
44                 tx['hash'] = util.double_sha256(tx['tx'])
45             tx_id = self.tx_find_id_and_value(tx)
46             if tx_id:
47                 self.update_tx_cache(tx_id)
48             else:
49                 print "error: import_block: no tx_id"
50         return block_id
51
52
53     def update_tx_cache(self, txid):
54         inrows = self.get_tx_inputs(txid, False)
55         for row in inrows:
56             _hash = self.binout(row[6])
57             address = hash_to_address(chr(0), _hash)
58             if self.tx_cache.has_key(address):
59                 print "cache: invalidating", address
60                 self.tx_cache.pop(address)
61             self.address_queue.put(address)
62
63         outrows = self.get_tx_outputs(txid, False)
64         for row in outrows:
65             _hash = self.binout(row[6])
66             address = hash_to_address(chr(0), _hash)
67             if self.tx_cache.has_key(address):
68                 print "cache: invalidating", address
69                 self.tx_cache.pop(address)
70             self.address_queue.put(address)
71
72     def safe_sql(self,sql, params=(), lock=True):
73         try:
74             if lock: self.dblock.acquire()
75             ret = self.selectall(sql,params)
76             if lock: self.dblock.release()
77             return ret
78         except:
79             print "sql error", sql
80             return []
81
82     def get_tx_outputs(self, tx_id, lock=True):
83         return self.safe_sql("""SELECT
84                 txout.txout_pos,
85                 txout.txout_scriptPubKey,
86                 txout.txout_value,
87                 nexttx.tx_hash,
88                 nexttx.tx_id,
89                 txin.txin_pos,
90                 pubkey.pubkey_hash
91               FROM txout
92               LEFT JOIN txin ON (txin.txout_id = txout.txout_id)
93               LEFT JOIN pubkey ON (pubkey.pubkey_id = txout.pubkey_id)
94               LEFT JOIN tx nexttx ON (txin.tx_id = nexttx.tx_id)
95              WHERE txout.tx_id = %d 
96              ORDER BY txout.txout_pos
97         """%(tx_id), (), lock)
98
99     def get_tx_inputs(self, tx_id, lock=True):
100         return self.safe_sql(""" SELECT
101                 txin.txin_pos,
102                 txin.txin_scriptSig,
103                 txout.txout_value,
104                 COALESCE(prevtx.tx_hash, u.txout_tx_hash),
105                 prevtx.tx_id,
106                 COALESCE(txout.txout_pos, u.txout_pos),
107                 pubkey.pubkey_hash
108               FROM txin
109               LEFT JOIN txout ON (txout.txout_id = txin.txout_id)
110               LEFT JOIN pubkey ON (pubkey.pubkey_id = txout.pubkey_id)
111               LEFT JOIN tx prevtx ON (txout.tx_id = prevtx.tx_id)
112               LEFT JOIN unlinked_txin u ON (u.txin_id = txin.txin_id)
113              WHERE txin.tx_id = %d
114              ORDER BY txin.txin_pos
115              """%(tx_id,), (), lock)
116
117     def get_address_out_rows(self, dbhash):
118         return self.safe_sql(""" SELECT
119                 b.block_nTime,
120                 cc.chain_id,
121                 b.block_height,
122                 1,
123                 b.block_hash,
124                 tx.tx_hash,
125                 tx.tx_id,
126                 txin.txin_pos,
127                 -prevout.txout_value
128               FROM chain_candidate cc
129               JOIN block b ON (b.block_id = cc.block_id)
130               JOIN block_tx ON (block_tx.block_id = b.block_id)
131               JOIN tx ON (tx.tx_id = block_tx.tx_id)
132               JOIN txin ON (txin.tx_id = tx.tx_id)
133               JOIN txout prevout ON (txin.txout_id = prevout.txout_id)
134               JOIN pubkey ON (pubkey.pubkey_id = prevout.pubkey_id)
135              WHERE pubkey.pubkey_hash = ?
136                AND cc.in_longest = 1""", (dbhash,))
137
138     def get_address_out_rows_memorypool(self, dbhash):
139         return self.safe_sql(""" SELECT
140                 1,
141                 tx.tx_hash,
142                 tx.tx_id,
143                 txin.txin_pos,
144                 -prevout.txout_value
145               FROM tx 
146               JOIN txin ON (txin.tx_id = tx.tx_id)
147               JOIN txout prevout ON (txin.txout_id = prevout.txout_id)
148               JOIN pubkey ON (pubkey.pubkey_id = prevout.pubkey_id)
149              WHERE pubkey.pubkey_hash = ? """, (dbhash,))
150
151     def get_address_in_rows(self, dbhash):
152         return self.safe_sql(""" SELECT
153                 b.block_nTime,
154                 cc.chain_id,
155                 b.block_height,
156                 0,
157                 b.block_hash,
158                 tx.tx_hash,
159                 tx.tx_id,
160                 txout.txout_pos,
161                 txout.txout_value
162               FROM chain_candidate cc
163               JOIN block b ON (b.block_id = cc.block_id)
164               JOIN block_tx ON (block_tx.block_id = b.block_id)
165               JOIN tx ON (tx.tx_id = block_tx.tx_id)
166               JOIN txout ON (txout.tx_id = tx.tx_id)
167               JOIN pubkey ON (pubkey.pubkey_id = txout.pubkey_id)
168              WHERE pubkey.pubkey_hash = ?
169                AND cc.in_longest = 1""", (dbhash,))
170
171     def get_address_in_rows_memorypool(self, dbhash):
172         return self.safe_sql( """ SELECT
173                 0,
174                 tx.tx_hash,
175                 tx.tx_id,
176                 txout.txout_pos,
177                 txout.txout_value
178               FROM tx
179               JOIN txout ON (txout.tx_id = tx.tx_id)
180               JOIN pubkey ON (pubkey.pubkey_id = txout.pubkey_id)
181              WHERE pubkey.pubkey_hash = ? """, (dbhash,))
182
183     def get_history(self, addr):
184         
185         cached_version = self.tx_cache.get( addr )
186         if cached_version is not None:
187             return cached_version
188
189         version, binaddr = decode_check_address(addr)
190         if binaddr is None:
191             return None
192
193         dbhash = self.binin(binaddr)
194         rows = []
195         rows += self.get_address_out_rows( dbhash )
196         rows += self.get_address_in_rows( dbhash )
197
198         txpoints = []
199         known_tx = []
200
201         for row in rows:
202             try:
203                 nTime, chain_id, height, is_in, blk_hash, tx_hash, tx_id, pos, value = row
204             except:
205                 print "cannot unpack row", row
206                 break
207             tx_hash = self.hashout_hex(tx_hash)
208             txpoint = {
209                     "nTime":    int(nTime),
210                     "height":   int(height),
211                     "is_in":    int(is_in),
212                     "blk_hash": self.hashout_hex(blk_hash),
213                     "tx_hash":  tx_hash,
214                     "tx_id":    int(tx_id),
215                     "pos":      int(pos),
216                     "value":    int(value),
217                     }
218
219             txpoints.append(txpoint)
220             known_tx.append(self.hashout_hex(tx_hash))
221
222
223         # todo: sort them really...
224         txpoints = sorted(txpoints, key=operator.itemgetter("nTime"))
225
226         # read memory pool
227         rows = []
228         rows += self.get_address_in_rows_memorypool( dbhash )
229         rows += self.get_address_out_rows_memorypool( dbhash )
230         address_has_mempool = False
231
232         for row in rows:
233             is_in, tx_hash, tx_id, pos, value = row
234             tx_hash = self.hashout_hex(tx_hash)
235             if tx_hash in known_tx:
236                 continue
237
238             # this means that pending transactions were added to the db, even if they are not returned by getmemorypool
239             address_has_mempool = True
240
241             # this means pending transactions are returned by getmemorypool
242             if tx_hash not in self.mempool_keys:
243                 continue
244
245             #print "mempool", tx_hash
246             txpoint = {
247                     "nTime":    0,
248                     "height":   0,
249                     "is_in":    int(is_in),
250                     "blk_hash": 'mempool', 
251                     "tx_hash":  tx_hash,
252                     "tx_id":    int(tx_id),
253                     "pos":      int(pos),
254                     "value":    int(value),
255                     }
256             txpoints.append(txpoint)
257
258
259         for txpoint in txpoints:
260             tx_id = txpoint['tx_id']
261             
262             txinputs = []
263             inrows = self.get_tx_inputs(tx_id)
264             for row in inrows:
265                 _hash = self.binout(row[6])
266                 address = hash_to_address(chr(0), _hash)
267                 txinputs.append(address)
268             txpoint['inputs'] = txinputs
269             txoutputs = []
270             outrows = self.get_tx_outputs(tx_id)
271             for row in outrows:
272                 _hash = self.binout(row[6])
273                 address = hash_to_address(chr(0), _hash)
274                 txoutputs.append(address)
275             txpoint['outputs'] = txoutputs
276
277             # for all unspent inputs, I want their scriptpubkey. (actually I could deduce it from the address)
278             if not txpoint['is_in']:
279                 # detect if already redeemed...
280                 for row in outrows:
281                     if row[6] == dbhash: break
282                 else:
283                     raise
284                 #row = self.get_tx_output(tx_id,dbhash)
285                 # pos, script, value, o_hash, o_id, o_pos, binaddr = row
286                 # if not redeemed, we add the script
287                 if row:
288                     if not row[4]: txpoint['raw_scriptPubKey'] = row[1]
289
290         # cache result
291         if not address_has_mempool:
292             self.tx_cache[addr] = txpoints
293         
294         return txpoints
295
296
297     def get_status(self,addr):
298         # get address status, i.e. the last block for that address.
299         tx_points = self.get_history(addr)
300         if not tx_points:
301             status = None
302         else:
303             lastpoint = tx_points[-1]
304             status = lastpoint['blk_hash']
305             # this is a temporary hack; move it up once old clients have disappeared
306             if status == 'mempool': # and session['version'] != "old":
307                 status = status + ':%d'% len(tx_points)
308         return status
309
310
311
312     def memorypool_update(store):
313
314         ds = BCDataStream.BCDataStream()
315         previous_transactions = store.mempool_keys
316         store.mempool_keys = []
317
318         postdata = dumps({"method": 'getmemorypool', 'params': [], 'id':'jsonrpc'})
319
320         respdata = urllib.urlopen(store.bitcoind_url, postdata).read()
321         r = loads(respdata)
322         if r['error'] != None:
323             return
324
325         v = r['result'].get('transactions')
326         for hextx in v:
327             ds.clear()
328             ds.write(hextx.decode('hex'))
329             tx = deserialize.parse_Transaction(ds)
330             tx['hash'] = util.double_sha256(tx['tx'])
331             tx_hash = store.hashin(tx['hash'])
332
333             store.mempool_keys.append(tx_hash)
334             if store.tx_find_id_and_value(tx):
335                 pass
336             else:
337                 tx_id = store.import_tx(tx, False)
338                 store.update_tx_cache(tx_id)
339     
340         store.commit()
341
342
343     def send_tx(self,tx):
344         postdata = dumps({"method": 'importtransaction', 'params': [tx], 'id':'jsonrpc'})
345         respdata = urllib.urlopen(self.bitcoind_url, postdata).read()
346         r = loads(respdata)
347         if r['error'] != None:
348             out = "error: transaction rejected by memorypool\n"+tx
349         else:
350             out = r['result']
351         return out
352
353
354     def main_iteration(store):
355         try:
356             store.dblock.acquire()
357             store.catch_up()
358             store.memorypool_update()
359             block_number = store.get_block_number(1)
360
361         except IOError:
362             print "IOError: cannot reach bitcoind"
363             block_number = 0
364         except:
365             traceback.print_exc(file=sys.stdout)
366             block_number = 0
367         finally:
368             store.dblock.release()
369
370         return block_number
371
372     def watch_address(self, addr):
373         if addr not in self.watched_addresses:
374             self.watched_addresses.append(addr)
375
376     def run(self, processor):
377         
378         old_block_number = None
379         while not processor.shared.stopped():
380             self.block_number = self.main_iteration()
381
382             if self.block_number != old_block_number:
383                 old_block_number = self.block_number
384                 processor.push_response({ 'method':'numblocks.subscribe', 'result':self.block_number })
385
386             while True:
387                 try:
388                     addr = self.address_queue.get(False)
389                 except:
390                     break
391                 if addr in self.watched_addresses:
392                     status = self.get_status( addr )
393                     processor.push_response({ 'method':'address.subscribe', 'params':[addr], 'result':status })
394
395             time.sleep(10)