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

[fix] Enable the stake blacklist #931

Merged
merged 2 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bittensor/_neuron/text/core_server/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def time_check():

time_check()

#stake_check()
stake_check()

return False

Expand Down
108 changes: 96 additions & 12 deletions tests/unit_tests/bittensor_tests/test_neuron.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from atexit import register
from types import SimpleNamespace
import unittest
from unittest.mock import MagicMock, patch
from more_itertools import side_effect

Expand Down Expand Up @@ -77,9 +78,6 @@ def test_coreserver_reregister_flag_false_exit():
mock_wallet = bittensor.wallet.mock()
mock_wallet.config = config

class MockException(Exception):
pass

def exit_early(*args, **kwargs):
raise MockException('exit_early')

Expand Down Expand Up @@ -129,9 +127,6 @@ def test_coreserver_reregister_flag_true():
mock_wallet = bittensor.wallet.mock()
mock_wallet.config = config

class MockException(Exception):
pass

def exit_early(*args, **kwargs):
raise MockException('exit_early')

Expand Down Expand Up @@ -178,9 +173,6 @@ def test_corevalidator_reregister_flag_false_exit():
mock_wallet = bittensor.wallet.mock()
mock_wallet.config = config

class MockException(Exception):
pass

def exit_early(*args, **kwargs):
raise MockException('exit_early')

Expand Down Expand Up @@ -227,9 +219,6 @@ def test_corevalidator_reregister_flag_true():
mock_wallet = bittensor.wallet.mock()
mock_wallet.config = config

class MockException(Exception):
pass

def exit_early(*args, **kwargs):
raise MockException('exit_early')

Expand Down Expand Up @@ -260,5 +249,100 @@ def exit_early(*args, **kwargs):
# Should try to register the neuron
mock_register.assert_called_once()

class MockException(Exception):
pass

class TestBlacklist(unittest.TestCase):

@staticmethod
def construct_config():
defaults = bittensor.Config()
bittensor.subtensor.add_defaults( defaults )
bittensor.dendrite.add_defaults( defaults )
bittensor.axon.add_defaults( defaults )
bittensor.wallet.add_defaults( defaults )
bittensor.dataset.add_defaults( defaults )
bittensor.logging.add_defaults( defaults )
bittensor.wandb.add_defaults( defaults )
defaults.wandb.api_key = 'test'
defaults.neuron = bittensor.neurons.core_server.neuron.config()
defaults.neuron.learning_rate = 0.0001
defaults.neuron.momentum = 0.9

return defaults

def exit_early(self, *args, **kwargs):
raise MockException('exit_early')

def test_stake_blacklist(self):
import sys
sys.setrecursionlimit(100)

mock_hotkey = "0x0000000000000000000000000000000000000000"
mock_hotkey_1 = "0x0000000000000000000000000000000000000001"

mock_subtensor = MagicMock(
is_hotkey_registered=MagicMock(return_value=True),
)

mock_wallet = MagicMock(
reregister=MagicMock(),
is_registered=MagicMock(return_value=True),
)

mock_metagraph = MagicMock(
hotkeys=[
mock_hotkey,
mock_hotkey_1,
],
S=[
torch.tensor(100), # stake for mock_hotkey, uid 0
torch.tensor(1001), # stake for mock_hotkey_1, uid 1
]
)

mock_config = self.construct_config()

mock_config.neuron.blacklist = bittensor.Config()
mock_config.neuron.blacklist.stake = 1000 # blacklist if stake is less than 1000

mock_model_config = bittensor.neurons.core_server.server.config()
mock_model_config.neuron = MagicMock(
disable_blacklist = False
)

mock_model = MagicMock(
spec=bittensor.neurons.core_server.server,
config=mock_model_config,
)

with patch('bittensor.axon.__new__', side_effect=self.exit_early) as mock_new_axon:
with patch('bittensor.neurons.core_server.neuron.check_config', return_value=True):
with pytest.raises(MockException):
bittensor.neurons.core_server.serve(
config=mock_config,
model=MagicMock(
spec=bittensor.neurons.core_server.server,
device="cpu",
to=MagicMock(return_value=mock_model),
config=mock_model_config,
),
subtensor=mock_subtensor,
wallet=mock_wallet,
axon=None,
metagraph=mock_metagraph
)

# args, kwargs
_, kwargs = mock_new_axon.call_args
blacklist = kwargs['blacklist']

# Check that the blacklist rejects below min stake
assert blacklist(mock_hotkey, bittensor.proto.RequestType.FORWARD) == True

# Check that the blacklist accepts above min stake
assert blacklist(mock_hotkey_1, bittensor.proto.RequestType.FORWARD) == False


if __name__ == '__main__':
pass