Skip to content

Commit

Permalink
add test for blacklisted stake
Browse files Browse the repository at this point in the history
  • Loading branch information
camfairchild committed Sep 29, 2022
1 parent 73a26d5 commit 427a3f2
Showing 1 changed file with 96 additions and 12 deletions.
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

0 comments on commit 427a3f2

Please sign in to comment.