From ae1c7d984ac9c63255dc28ba7bf77f5ce758bac4 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Fri, 3 May 2024 10:06:05 +0200 Subject: [PATCH] Fixed shard computation --- pbcabi/binaryreader.py | 18 ++++++++++++------ pbcabi/data.py | 4 +--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/pbcabi/binaryreader.py b/pbcabi/binaryreader.py index 2c2153c..bbe81e8 100644 --- a/pbcabi/binaryreader.py +++ b/pbcabi/binaryreader.py @@ -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: diff --git a/pbcabi/data.py b/pbcabi/data.py index 6b0cd17..67ee7e5 100644 --- a/pbcabi/data.py +++ b/pbcabi/data.py @@ -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}'