This commit is contained in:
parent
cae659638a
commit
ae1c7d984a
|
@ -26,6 +26,12 @@ class BinaryReader:
|
|||
self.size = len(buf)
|
||||
self.position = 0
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return 'BinaryReader[{} / {}]'.format(self.position, self.size)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return repr(self)
|
||||
|
||||
def readBytes(self, num_bytes: int) -> bytes:
|
||||
'''
|
||||
Reads a number of bytes from stream.
|
||||
|
@ -86,9 +92,9 @@ class BinaryReader:
|
|||
'''
|
||||
# TODO: Test!
|
||||
result = self.readUIntBigEndian(num_bytes)
|
||||
half = 2**(num_bytes * 8)
|
||||
if result > half:
|
||||
result -= half
|
||||
full = 2**(num_bytes * 8)
|
||||
if result >= full//2:
|
||||
result -= full
|
||||
return result
|
||||
|
||||
def readUIntLittleEndian(self, num_bytes: int) -> int:
|
||||
|
@ -107,9 +113,9 @@ class BinaryReader:
|
|||
'''
|
||||
# TODO: Test!
|
||||
result = self.readUIntLittleEndian(num_bytes)
|
||||
half = 2**(num_bytes * 8)
|
||||
if result > half:
|
||||
result -= half
|
||||
full = 2**(num_bytes * 8)
|
||||
if result >= full//2:
|
||||
result -= full
|
||||
return result
|
||||
|
||||
def readLeb128(self) -> int:
|
||||
|
|
|
@ -95,9 +95,7 @@ class BlockchainAddress(ByteData):
|
|||
return repr(self)
|
||||
|
||||
def shard_id(self, num_shards: int) -> str:
|
||||
reader = BinaryReader(self.data)
|
||||
reader.readBytes(17)
|
||||
shard = reader.readUInt32BigEndian()
|
||||
shard = BinaryReader(self.data[17:]).readSignedIntBigEndian(4)
|
||||
shard = abs(shard) % num_shards
|
||||
return f'Shard{shard}'
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user