Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove abusable code from subnet template #88

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
63 changes: 39 additions & 24 deletions neurons/miner.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,36 +88,51 @@ async def blacklist(
- Reject if the hotkey is not a registered entity within the metagraph.
- Consider blacklisting entities that are not validators or have insufficient stake.

In practice it would be wise to blacklist requests from entities that are not validators, or do not have
In practice you MUST blacklist requests from entities that are not validators, or do not have
enough stake. This can be checked via metagraph.S and metagraph.validator_permit. You can always attain
the uid of the sender via a metagraph.hotkeys.index( synapse.dendrite.hotkey ) call.
the uid of the sender via a metagraph.hotkeys.index( synapse.dendrite.hotkey ) call after checking that the type of dendrite.hotkey is not None.

Otherwise, allow the request to be processed further.
"""
# TODO(developer): Define how miners should blacklist requests.
uid = self.metagraph.hotkeys.index(synapse.dendrite.hotkey)
if (
not self.config.blacklist.allow_non_registered
and synapse.dendrite.hotkey not in self.metagraph.hotkeys
):
# Ignore requests from un-registered entities.
# TODO (developer): Define how miners should blacklist requests.
try:
# check for missing hotkey malformed request to avoid throwing ValueError on metagraph.hotkeys.index (unhandled exception)
if not synapse.dendrite.hotkey:
return True, "Missing hotkey/Malformed request"

# Ignore requests from un-registered entities unless permitted.
if (
not self.config.blacklist.allow_non_registered
and synapse.dendrite.hotkey not in self.metagraph.hotkeys
):
bt.logging.trace(
f"Blacklisting un-registered hotkey {synapse.dendrite.hotkey}"
)
return True, "Unrecognized hotkey"

uid = self.metagraph.hotkeys.index(synapse.dendrite.hotkey)

if self.config.blacklist.force_validator_permit:
# If the config is set to force validator permit, then we should only allow requests from validators.
if not self.metagraph.validator_permit[uid]:
bt.logging.warning(
f"Blacklisting a request from non-validator hotkey {synapse.dendrite.hotkey}"
)
return True, "Non-validator hotkey"

stake = self.metagraph.S[uid].item()

# ignore minimal weight validators that should not be directly contacting a miner's synpase due to the lack of weight setting capability.
if stake < self.config.blacklist.minimum_stake:
return True, "Minimal stake validator"

bt.logging.trace(
f"Blacklisting un-registered hotkey {synapse.dendrite.hotkey}"
f"Not Blacklisting recognized hotkey {synapse.dendrite.hotkey}"
)
return True, "Unrecognized hotkey"

if self.config.blacklist.force_validator_permit:
# If the config is set to force validator permit, then we should only allow requests from validators.
if not self.metagraph.validator_permit[uid]:
bt.logging.warning(
f"Blacklisting a request from non-validator hotkey {synapse.dendrite.hotkey}"
)
return True, "Non-validator hotkey"

bt.logging.trace(
f"Not Blacklisting recognized hotkey {synapse.dendrite.hotkey}"
)
return False, "Hotkey recognized!"
return False, "Hotkey recognized!"
except Exception as e:
bt.logging.error(f"Exception in blacklist_fn: {str(e)}")
return True, "Blacklist error"
cxmplex marked this conversation as resolved.
Show resolved Hide resolved

async def priority(self, synapse: template.protocol.Dummy) -> float:
"""
Expand Down
7 changes: 7 additions & 0 deletions template/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ def add_miner_args(cls, parser):
default=False,
)

parser.add_argument(
"--blacklist.minimum_stake",
type=int,
help="If set, we will force incoming requests to have a weight settable stake.",
default=1024,
)

parser.add_argument(
"--wandb.project_name",
type=str,
Expand Down