Skip to content

Commit a28963d

Browse files
author
opendansor
authored
Merge pull request #2229 from opentensor/feature/opendansor/improve_child_hotkeys
Child hotkeys handle excess on normalization
2 parents 028ef8b + 20d942c commit a28963d

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

bittensor/extrinsics/staking.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# The MIT License (MIT)
22
# Copyright © 2021 Yuma Rao
33
# Copyright © 2023 Opentensor Foundation
4+
from math import floor
45

56
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
67
# documentation files (the “Software”), to deal in the Software without restriction, including without limitation
@@ -649,4 +650,20 @@ def normalize_children_and_proportions(
649650
"""
650651
total = sum(prop for prop, _ in children)
651652
u64_max = 2**64 - 1
652-
return [(int(prop * u64_max / total), child) for prop, child in children]
653+
normalized_children = [
654+
(int(floor(prop * (u64_max - 1) / total)), child) for prop, child in children
655+
]
656+
sum_norm = sum(prop for prop, _ in normalized_children)
657+
658+
# if the sum is more, subtract the excess from the first child
659+
if sum_norm > u64_max:
660+
if abs(sum_norm - u64_max) > 10:
661+
raise ValueError(
662+
"The sum of normalized proportions is out of the acceptable range."
663+
)
664+
normalized_children[0] = (
665+
normalized_children[0][0] - (sum_norm - (u64_max - 1)),
666+
normalized_children[0][1],
667+
)
668+
669+
return normalized_children

bittensor/utils/formatting.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ def float_to_u64(value: float) -> int:
6262
if not (0 <= value < 1):
6363
raise ValueError("Input value must be between 0 and 1")
6464

65-
# Convert the float to a u64 value
66-
return int(value * (2**64 - 1))
65+
# Convert the float to a u64 value, take the floor value
66+
return int(math.floor((value * (2**64 - 1)))) - 1
6767

6868

6969
def u64_to_float(value: int) -> float:

0 commit comments

Comments
 (0)