Implement CompactSize helper
This commit is contained in:
parent
07b7a6e8d8
commit
acdfe06c87
|
@ -1,6 +1,8 @@
|
|||
#!/usr/bin/env python3
|
||||
import struct
|
||||
|
||||
from zc_utils import write_compact_size
|
||||
|
||||
MAX_MONEY = 21000000 * 100000000
|
||||
TX_EXPIRY_HEIGHT_THRESHOLD = 500000000
|
||||
|
||||
|
@ -89,7 +91,7 @@ class Script(object):
|
|||
return self._script
|
||||
|
||||
def __bytes__(self):
|
||||
return struct.pack('b', len(self._script)) + self._script
|
||||
return write_compact_size(len(self._script)) + self._script
|
||||
|
||||
|
||||
class OutPoint(object):
|
||||
|
@ -167,11 +169,11 @@ class Transaction(object):
|
|||
self.nVersionGroupId == OVERWINTER_VERSION_GROUP_ID and \
|
||||
self.nVersion == OVERWINTER_TX_VERSION
|
||||
|
||||
ret += struct.pack('b', len(self.vin))
|
||||
ret += write_compact_size(len(self.vin))
|
||||
for x in self.vin:
|
||||
ret += bytes(x)
|
||||
|
||||
ret += struct.pack('b', len(self.vout))
|
||||
ret += write_compact_size(len(self.vout))
|
||||
for x in self.vout:
|
||||
ret += bytes(x)
|
||||
|
||||
|
@ -180,7 +182,7 @@ class Transaction(object):
|
|||
ret += struct.pack('<I', self.nExpiryHeight)
|
||||
|
||||
if self.nVersion >= 2:
|
||||
ret += struct.pack('b', len(self.vJoinSplit))
|
||||
ret += write_compact_size(len(self.vJoinSplit))
|
||||
for jsdesc in self.vJoinSplit:
|
||||
ret += bytes(jsdesc)
|
||||
if len(self.vJoinSplit) > 0:
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
import struct
|
||||
|
||||
def write_compact_size(n):
|
||||
if n < 253:
|
||||
return struct.pack('B', n)
|
||||
elif n <= 0xFFFF:
|
||||
return struct.pack('B', 253) + struct.pack('<H', n)
|
||||
elif n <= 0xFFFFFFFF:
|
||||
return struct.pack('B', 254) + struct.pack('<I', n)
|
||||
else:
|
||||
return struct.pack('B', 255) + struct.pack('<Q', n)
|
||||
|
||||
assert write_compact_size(0) == b'\x00'
|
||||
assert write_compact_size(1) == b'\x01'
|
||||
assert write_compact_size(252) == b'\xFC'
|
||||
assert write_compact_size(253) == b'\xFD\xFD\x00'
|
||||
assert write_compact_size(254) == b'\xFD\xFE\x00'
|
||||
assert write_compact_size(255) == b'\xFD\xFF\x00'
|
||||
assert write_compact_size(256) == b'\xFD\x00\x01'
|
||||
assert write_compact_size(0xFFFE) == b'\xFD\xFE\xFF'
|
||||
assert write_compact_size(0xFFFF) == b'\xFD\xFF\xFF'
|
||||
assert write_compact_size(0x010000) == b'\xFE\x00\x00\x01\x00'
|
||||
assert write_compact_size(0x010001) == b'\xFE\x01\x00\x01\x00'
|
||||
assert write_compact_size(0xFFFFFFFE) == b'\xFE\xFE\xFF\xFF\xFF'
|
||||
assert write_compact_size(0xFFFFFFFF) == b'\xFE\xFF\xFF\xFF\xFF'
|
||||
assert write_compact_size(0x0100000000) == b'\xFF\x00\x00\x00\x00\x01\x00\x00\x00'
|
||||
assert write_compact_size(0xFFFFFFFFFFFFFFFF) == b'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF'
|
Loading…
Reference in New Issue