Do not use mutables as default values!
[electrum-nvc.git] / lib / blockchain.py
index 08461d0..ac5ea14 100644 (file)
@@ -118,7 +118,7 @@ class Blockchain(threading.Thread):
             try:
                 assert prev_hash == header.get('prev_block_hash')
                 assert bits == header.get('bits')
-                assert eval('0x'+_hash) < target
+                assert int('0x'+_hash,16) < target
             except Exception:
                 return False
 
@@ -149,7 +149,7 @@ class Blockchain(threading.Thread):
             _hash = self.hash_header(header)
             assert previous_hash == header.get('prev_block_hash')
             assert bits == header.get('bits')
-            assert eval('0x'+_hash) < target
+            assert int('0x'+_hash,16) < target
 
             previous_header = header
             previous_hash = _hash 
@@ -157,36 +157,6 @@ class Blockchain(threading.Thread):
         self.save_chunk(index, data)
         print_error("validated chunk %d"%height)
 
-
-    def verify_header(self, header):
-        # add header to the blockchain file
-        # if there is a reorg, push it in a stack
-
-        height = header.get('block_height')
-
-        prev_header = self.read_header(height -1)
-        if not prev_header:
-            # return False to request previous header
-            return False
-
-        prev_hash = self.hash_header(prev_header)
-        bits, target = self.get_target(height/2016)
-        _hash = self.hash_header(header)
-        try:
-            assert prev_hash == header.get('prev_block_hash')
-            assert bits == header.get('bits')
-            assert eval('0x'+_hash) < target
-        except Exception:
-            # this can be caused by a reorg.
-            print_error("verify header failed"+ repr(header))
-            verifier.undo_verifications()
-
-            # return False to request previous header.
-            return False
-
-        self.save_header(header)
-        print_error("verify header:", _hash, height)
-        return True
         
 
     def header_to_string(self, res):
@@ -200,7 +170,7 @@ class Blockchain(threading.Thread):
 
 
     def header_from_string(self, s):
-        hex_to_int = lambda s: eval('0x' + s[::-1].encode('hex'))
+        hex_to_int = lambda s: int('0x' + s[::-1].encode('hex'), 16)
         h = {}
         h['version'] = hex_to_int(s[0:4])
         h['prev_block_hash'] = hash_encode(s[4:36])
@@ -271,7 +241,9 @@ class Blockchain(threading.Thread):
                 return h 
 
 
-    def get_target(self, index, chain=[]):
+    def get_target(self, index, chain=None):
+        if chain is None:
+            chain = []  # Do not use mutables as default values!
 
         max_target = 0x00000000FFFF0000000000000000000000000000000000000000000000000000
         if index == 0: return 0x1d00ffff, max_target
@@ -306,8 +278,8 @@ class Blockchain(threading.Thread):
             c = c[2:]
             i -= 1
 
-        c = eval('0x'+c[0:6])
-        if c > 0x800000: 
+        c = int('0x'+c[0:6],16)
+        if c >= 0x800000: 
             c /= 256
             i += 1
 
@@ -382,45 +354,24 @@ class Blockchain(threading.Thread):
 
 
     def get_and_verify_chunks(self, i, header, height):
-        requested_chunks = []
+
         queue = Queue.Queue()
         min_index = (self.local_height + 1)/2016
         max_index = (height + 1)/2016
-
-        for n in range(min_index, max_index + 1):
-            i.send([ ('blockchain.block.get_chunk',[n])], lambda i,r:queue.put(r))
-            requested_chunks.append(n)
-
-        print_error( "requested chunks:", requested_chunks )
-
-        while requested_chunks:
-            try:
-                r = queue.get(timeout=1)
-            except Queue.Empty:
+        n = min_index
+        while n < max_index + 1:
+            print_error( "Requesting chunk:", n )
+            r = i.synchronous_get([ ('blockchain.block.get_chunk',[n])])[0]
+            if not r: 
                 continue
-            if not r: continue
-
-            if r.get('error'):
-                print_error('Verifier received an error:', r)
-                continue
-
-            # 3. handle response
-            params = r['params']
-            result = r['result']
-
-            index = params[0]
             try:
-                self.verify_chunk(index, result)
+                self.verify_chunk(n, r)
+                n = n + 1
             except Exception:
-                print_error('Verify chunk failed!!')
-                return False
-            requested_chunks.remove(index)
+                print_error('Verify chunk failed!')
+                n = n - 1
+                if n < 0:
+                    return False
 
         return True
 
-
-
-
-
-
-