Merge pull request #2 from luke-jr/bugfix_sernum
authorslush0 <marek@palatinus.cz>
Mon, 28 Jan 2013 23:51:07 +0000 (15:51 -0800)
committerslush0 <marek@palatinus.cz>
Mon, 28 Jan 2013 23:51:07 +0000 (15:51 -0800)
Bugfix: Serialized numbers are signed, so need an extra \x00 in the case of 0x80...-0xff... This was a reason for rejecting v2 blocks on testnet.

lib/util.py

index 3e88fd8..45da424 100644 (file)
@@ -188,11 +188,16 @@ def deser_uint256_be(f):
         r += t << (i * 32)
     return r
 
-def ser_number(num):
+def ser_number(n):
     # For encoding nHeight into coinbase
-    d = struct.pack("<I", num).rstrip("\x00")
-    return chr(len(d)) + d
-    
+    s = bytearray(b'\1')
+    while n > 127:
+        s[0] += 1
+        s.append(n % 256)
+        n //= 256
+    s.append(n)
+    return bytes(s)
+
 def script_to_address(addr):
     d = address_to_pubkeyhash(addr)
     if not d: