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