Skip to content

Commit c787c7b

Browse files
authored
Merge pull request #3044 from opentensor/fix/roman/balances
Fix bug if `block==0`
2 parents 09e3921 + 55cefa8 commit c787c7b

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

bittensor/core/async_subtensor.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ async def determine_block_hash(
343343
# Return the appropriate value.
344344
if block_hash:
345345
return block_hash
346-
if block:
346+
if block is not None:
347347
return await self.get_block_hash(block)
348348
return None
349349

@@ -1235,6 +1235,9 @@ async def get_balances(
12351235
"""
12361236
if reuse_block:
12371237
block_hash = self.substrate.last_block_hash
1238+
elif block_hash is None and block is None:
1239+
# Neither block nor block_hash provided, default to head
1240+
block_hash = await self.get_block_hash()
12381241
else:
12391242
block_hash = await self.determine_block_hash(block, block_hash, reuse_block)
12401243
calls = [
@@ -1306,7 +1309,7 @@ async def get_block_hash(self, block: Optional[int] = None) -> str:
13061309
Notes:
13071310
See also: <https://docs.learnbittensor.org/glossary#block>
13081311
"""
1309-
if block:
1312+
if block is not None:
13101313
return await self._get_block_hash(block)
13111314
else:
13121315
return await self.substrate.get_chain_head()

bittensor/core/subtensor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ def get_block_hash(self, block: Optional[int] = None) -> str:
746746
data. It is crucial for verifying transactions, ensuring data consistency, and maintaining the
747747
trustworthiness of the blockchain.
748748
"""
749-
if block:
749+
if block is not None:
750750
return self._get_block_hash(block)
751751
else:
752752
return self.substrate.get_chain_head()

bittensor/utils/mock/subtensor_mock.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ def query_subtensor(
581581
) -> MockSubtensorValue:
582582
if params is None:
583583
params = []
584-
if block:
584+
if block is not None:
585585
if self.block_number < block:
586586
raise Exception("Cannot query block in the future")
587587

@@ -622,7 +622,7 @@ def query_map_subtensor(
622622
"""
623623
if params is None:
624624
params = []
625-
if block:
625+
if block is not None:
626626
if self.block_number < block:
627627
raise Exception("Cannot query block in the future")
628628

@@ -668,7 +668,7 @@ def query_map_subtensor(
668668
def query_constant(
669669
self, module_name: str, constant_name: str, block: Optional[int] = None
670670
) -> Optional[object]:
671-
if block:
671+
if block is not None:
672672
if self.block_number < block:
673673
raise Exception("Cannot query block in the future")
674674

@@ -697,7 +697,7 @@ def get_current_block(self) -> int:
697697
# ==== Balance RPC methods ====
698698

699699
def get_balance(self, address: str, block: int = None) -> "Balance":
700-
if block:
700+
if block is not None:
701701
if self.block_number < block:
702702
raise Exception("Cannot query block in the future")
703703

@@ -732,7 +732,7 @@ def neuron_for_uid(
732732
if uid is None:
733733
return NeuronInfo.get_null_neuron()
734734

735-
if block:
735+
if block is not None:
736736
if self.block_number < block:
737737
raise Exception("Cannot query block in the future")
738738

@@ -958,7 +958,7 @@ def neuron_for_uid_lite(
958958
if uid is None:
959959
return NeuronInfoLite.get_null_neuron()
960960

961-
if block:
961+
if block is not None:
962962
if self.block_number < block:
963963
raise Exception("Cannot query block in the future")
964964

0 commit comments

Comments
 (0)