1
0

Fixed shard computation
Some checks failed
Python Package / Package (push) Failing after 24s

This commit is contained in:
Jon Michael Aanes 2024-05-03 10:06:05 +02:00
parent cae659638a
commit ae1c7d984a
2 changed files with 13 additions and 9 deletions

View File

@ -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:

View File

@ -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}'