rename function; fix get_status
[electrum-server.git] / server.py
index 4173828..b8f69c7 100755 (executable)
--- a/server.py
+++ b/server.py
@@ -75,14 +75,14 @@ class MyStore(Datastore_class):
             _hash = store.binout(row[6])
             address = hash_to_address(chr(0), _hash)
             if self.tx_cache.has_key(address):
-                #print "cache: invalidating", address
+                print "cache: invalidating", address
                 self.tx_cache.pop(address)
         outrows = self.get_tx_outputs(txid, False)
         for row in outrows:
             _hash = store.binout(row[6])
             address = hash_to_address(chr(0), _hash)
             if self.tx_cache.has_key(address):
-                #print "cache: invalidating", address
+                print "cache: invalidating", address
                 self.tx_cache.pop(address)
 
     def safe_sql(self,sql, params=(), lock=True):
@@ -196,7 +196,7 @@ class MyStore(Datastore_class):
               JOIN pubkey ON (pubkey.pubkey_id = txout.pubkey_id)
              WHERE pubkey.pubkey_hash = ? """, (dbhash,))
 
-    def get_txpoints(self, addr):
+    def get_history(self, addr):
         
         if config.get('server','cache') == 'yes':
             cached_version = self.tx_cache.get( addr ) 
@@ -205,7 +205,8 @@ class MyStore(Datastore_class):
 
         version, binaddr = decode_check_address(addr)
         if binaddr is None:
-            return "err"
+            return None
+
         dbhash = self.binin(binaddr)
         rows = []
         rows += self.get_address_out_rows( dbhash )
@@ -251,10 +252,14 @@ class MyStore(Datastore_class):
             tx_hash = self.hashout_hex(tx_hash)
             if tx_hash in known_tx:
                 continue
+
+            # this means that pending transactions were added to the db, even if they are not returned by getmemorypool
+            address_has_mempool = True
+
+            # this means pending transactions are returned by getmemorypool
             if tx_hash not in self.mempool_keys:
                 continue
 
-            address_has_mempool = True
             #print "mempool", tx_hash
             txpoint = {
                     "nTime":    0,
@@ -309,7 +314,6 @@ class MyStore(Datastore_class):
 
 
 
-
 def send_tx(tx):
     import bitcoinrpc
     conn = bitcoinrpc.connect_to_local()
@@ -351,7 +355,7 @@ def client_thread(ipaddr,conn):
         try:
             cmd, data = ast.literal_eval(msg[:-1])
         except:
-            print "syntax error", repr(msg)
+            print "syntax error", repr(msg), ipaddr
             conn.close()
             return
 
@@ -366,7 +370,7 @@ def client_thread(ipaddr,conn):
                     version = "old"
                 else:
                     version, addresses = ast.literal_eval(data)
-                    version = "v"+version
+                    if version[0]=="0": version = "v" + version
             except:
                 print "error", data
                 conn.close()
@@ -412,7 +416,7 @@ def client_thread(ipaddr,conn):
                     if store.tx_cache.get( addr ) is not None: k += 1
 
                     # get addtess status, i.e. the last block for that address.
-                    tx_points = store.get_txpoints(addr)
+                    tx_points = store.get_history(addr)
                     if not tx_points:
                         status = None
                     else:
@@ -420,7 +424,7 @@ def client_thread(ipaddr,conn):
                         status = lastpoint['blk_hash']
                         # this is a temporary hack; move it up once old clients have disappeared
                         if status == 'mempool' and session['version'] != "old":
-                            status = status + ':%s'% lastpoint['tx_hash']
+                            status = status + ':%d'% len(tx_points)
 
                     last_status = addresses.get( addr )
                     if last_status != status:
@@ -436,7 +440,7 @@ def client_thread(ipaddr,conn):
         elif cmd == 'h': 
             # history
             address = data
-            out = repr( store.get_txpoints( address ) )
+            out = repr( store.get_history( address ) )
 
         elif cmd == 'load': 
             if config.get('server','password') == data:
@@ -450,7 +454,7 @@ def client_thread(ipaddr,conn):
 
         elif cmd =='clear_cache':
             if config.get('server','password') == data:
-                self.tx_cache = {}
+                store.tx_cache = {}
                 out = 'ok'
             else:
                 out = 'wrong password'
@@ -494,12 +498,12 @@ def client_thread(ipaddr,conn):
         conn.close()
     
 
-ds = BCDataStream.BCDataStream()
 
 
 
 
 def memorypool_update(store):
+    ds = BCDataStream.BCDataStream()
     store.mempool_keys = []
     conn = bitcoinrpc.connect_to_local()
     try:
@@ -513,10 +517,11 @@ def memorypool_update(store):
         ds.write(hextx.decode('hex'))
         tx = deserialize.parse_Transaction(ds)
         tx['hash'] = util.double_sha256(tx['tx'])
-        store.mempool_keys.append(tx['hash'][::-1].encode('hex'))
+        tx_hash = tx['hash'][::-1].encode('hex')
+        store.mempool_keys.append(tx_hash)
         if store.tx_find_id_and_value(tx):
             pass
-        else:            
+        else:
             store.import_tx(tx, False)
 
     store.commit()
@@ -583,16 +588,17 @@ if __name__ == '__main__':
 
     if len(sys.argv)>1:
         cmd = sys.argv[1]
+        pw = config.get('server','password')
         if cmd == 'load':
-            request = "('load','%s')#"%config.get('server','password')
+            request = "('load','%s')#"%pw
         elif cmd == 'peers':
             request = "('peers','')#"
         elif cmd == 'stop':
-            request = "('stop','%s')#"%config.get('server','password')
+            request = "('stop','%s')#"%pw
         elif cmd == 'clear_cache':
-            request = "('clear_cache','%s')#"%config.get('server','password')
+            request = "('clear_cache','%s')#"%pw
         elif cmd == 'get_cache':
-            request = "('get_cache',('%s','%s'))#"%(config.get('server','password'),sys.argv[2])
+            request = "('get_cache',('%s','%s'))#"%(pw,sys.argv[2])
         elif cmd == 'h':
             request = "('h','%s')#"%sys.argv[2]
         elif cmd == 'b':