From a5364745027211c31e620caea59cac5427fd3cb2 Mon Sep 17 00:00:00 2001 From: johnreedv Date: Mon, 2 Dec 2024 18:13:29 -0800 Subject: [PATCH 1/3] add params --- bittensor/core/extrinsics/commit_reveal.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/bittensor/core/extrinsics/commit_reveal.py b/bittensor/core/extrinsics/commit_reveal.py index b789258c7..be82482b5 100644 --- a/bittensor/core/extrinsics/commit_reveal.py +++ b/bittensor/core/extrinsics/commit_reveal.py @@ -125,12 +125,18 @@ def commit_reveal_v3_extrinsic( netuid=netuid ) + tempo = subtensor.get_subnet_hyperparameters(netuid).tempo + current_block = subtensor.get_current_block() + # Encrypt `commit_hash` with t-lock and `get reveal_round` commit_for_reveal, reveal_round = get_encrypted_commit( uids=uids, weights=weights, - subnet_reveal_period_epochs=subnet_reveal_period_epochs, version_key=version_key, + tempo=tempo, + current_block=current_block, + netuid=netuid, + subnet_reveal_period_epochs=subnet_reveal_period_epochs, ) success, message = _do_commit_reveal_v3( From ea50baa3faa9d84af1f2a585d038d66f0888a299 Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Tue, 3 Dec 2024 17:45:09 +0200 Subject: [PATCH 2/3] More efficient use of hyperparam grabbing. --- bittensor/core/extrinsics/commit_reveal.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/bittensor/core/extrinsics/commit_reveal.py b/bittensor/core/extrinsics/commit_reveal.py index be82482b5..73c2f64ca 100644 --- a/bittensor/core/extrinsics/commit_reveal.py +++ b/bittensor/core/extrinsics/commit_reveal.py @@ -120,13 +120,14 @@ def commit_reveal_v3_extrinsic( # Reformat and normalize. uids, weights = convert_weights_and_uids_for_emit(uids, weights) - # Get subnet's reveal (in epochs) - subnet_reveal_period_epochs = subtensor.get_subnet_reveal_period_epochs( - netuid=netuid - ) - - tempo = subtensor.get_subnet_hyperparameters(netuid).tempo current_block = subtensor.get_current_block() + subnet_hyperparameters = subtensor.get_subnet_hyperparameters( + netuid, block=current_block + ) + tempo = subnet_hyperparameters.tempo + subnet_reveal_period_epochs = ( + subnet_hyperparameters.commit_reveal_weights_interval + ) # Encrypt `commit_hash` with t-lock and `get reveal_round` commit_for_reveal, reveal_round = get_encrypted_commit( From 084e3b49b90028523bfd49907bc6851f43867963 Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Tue, 3 Dec 2024 17:45:17 +0200 Subject: [PATCH 3/3] Updated tests. --- .../extrinsics/test_commit_reveal.py | 70 ++++++++++++++++--- 1 file changed, 62 insertions(+), 8 deletions(-) diff --git a/tests/unit_tests/extrinsics/test_commit_reveal.py b/tests/unit_tests/extrinsics/test_commit_reveal.py index a58600a32..01ed42f8b 100644 --- a/tests/unit_tests/extrinsics/test_commit_reveal.py +++ b/tests/unit_tests/extrinsics/test_commit_reveal.py @@ -1,4 +1,5 @@ from bittensor.core import subtensor as subtensor_module +from bittensor.core.chain_data import SubnetHyperparameters from bittensor.core.subtensor import Subtensor from bittensor.core.extrinsics import commit_reveal import pytest @@ -13,7 +14,40 @@ def subtensor(mocker): mocker.patch.object( subtensor_module, "SubstrateInterface", return_value=fake_substrate ) - return Subtensor() + yield Subtensor() + + +@pytest.fixture +def hyperparams(): + yield SubnetHyperparameters( + rho=0, + kappa=0, + immunity_period=0, + min_allowed_weights=0, + max_weight_limit=0.0, + tempo=0, + min_difficulty=0, + max_difficulty=0, + weights_version=0, + weights_rate_limit=0, + adjustment_interval=0, + activity_cutoff=0, + registration_allowed=False, + target_regs_per_interval=0, + min_burn=0, + max_burn=0, + bonds_moving_avg=0, + max_regs_per_block=0, + serving_rate_limit=0, + max_validators=0, + adjustment_alpha=0, + difficulty=0, + commit_reveal_weights_interval=0, + commit_reveal_weights_enabled=True, + alpha_high=0, + alpha_low=0, + liquid_alpha_enabled=False, + ) def test_do_commit_reveal_v3_success(mocker, subtensor): @@ -118,7 +152,7 @@ def test_do_commit_reveal_v3_failure_due_to_error(mocker, subtensor): assert result == (False, "Formatted error") -def test_commit_reveal_v3_extrinsic_success_with_torch(mocker, subtensor): +def test_commit_reveal_v3_extrinsic_success_with_torch(mocker, subtensor, hyperparams): """Test successful commit-reveal with torch tensors.""" # Preps fake_wallet = mocker.Mock(autospec=subtensor_module.Wallet) @@ -149,6 +183,12 @@ def test_commit_reveal_v3_extrinsic_success_with_torch(mocker, subtensor): mock_do_commit_reveal_v3 = mocker.patch.object( commit_reveal, "_do_commit_reveal_v3", return_value=(True, "Success") ) + mock_block = mocker.patch.object(subtensor, "get_current_block", return_value=1) + mock_hyperparams = mocker.patch.object( + subtensor, + "get_subnet_hyperparameters", + return_value=hyperparams, + ) # Call success, message = commit_reveal.commit_reveal_v3_extrinsic( @@ -163,16 +203,18 @@ def test_commit_reveal_v3_extrinsic_success_with_torch(mocker, subtensor): # Asserts assert success is True - assert message == "Success" + assert message == "reveal_round:1" mocked_convert_weights_and_uids_for_emit.assert_called_once_with( fake_uids, fake_weights ) - mocked_get_subnet_reveal_period_epochs.assert_called_once_with(netuid=fake_netuid) mocked_get_encrypted_commit.assert_called_once_with( uids=mocked_uids, weights=mocked_weights, - subnet_reveal_period_epochs=mocked_get_subnet_reveal_period_epochs.return_value, + subnet_reveal_period_epochs=mock_hyperparams.return_value.commit_reveal_weights_interval, version_key=commit_reveal.version_as_int, + tempo=mock_hyperparams.return_value.tempo, + netuid=fake_netuid, + current_block=mock_block.return_value, ) mock_do_commit_reveal_v3.assert_called_once_with( self=subtensor, @@ -185,7 +227,7 @@ def test_commit_reveal_v3_extrinsic_success_with_torch(mocker, subtensor): ) -def test_commit_reveal_v3_extrinsic_success_with_numpy(mocker, subtensor): +def test_commit_reveal_v3_extrinsic_success_with_numpy(mocker, subtensor, hyperparams): """Test successful commit-reveal with numpy arrays.""" # Preps fake_wallet = mocker.Mock(autospec=subtensor_module.Wallet) @@ -205,6 +247,12 @@ def test_commit_reveal_v3_extrinsic_success_with_numpy(mocker, subtensor): mock_do_commit = mocker.patch.object( commit_reveal, "_do_commit_reveal_v3", return_value=(True, "Committed!") ) + mocker.patch.object(subtensor, "get_current_block", return_value=1) + mocker.patch.object( + subtensor, + "get_subnet_hyperparameters", + return_value=hyperparams, + ) # Call success, message = commit_reveal.commit_reveal_v3_extrinsic( @@ -219,13 +267,13 @@ def test_commit_reveal_v3_extrinsic_success_with_numpy(mocker, subtensor): # Asserts assert success is True - assert message == "Committed!" + assert message == "reveal_round:0" mock_convert.assert_called_once_with(fake_uids, fake_weights) mock_encode_drand.assert_called_once() mock_do_commit.assert_called_once() -def test_commit_reveal_v3_extrinsic_response_false(mocker, subtensor): +def test_commit_reveal_v3_extrinsic_response_false(mocker, subtensor, hyperparams): """Test unsuccessful commit-reveal with torch.""" # Preps fake_wallet = mocker.Mock(autospec=subtensor_module.Wallet) @@ -250,6 +298,12 @@ def test_commit_reveal_v3_extrinsic_response_false(mocker, subtensor): mock_do_commit_reveal_v3 = mocker.patch.object( commit_reveal, "_do_commit_reveal_v3", return_value=(False, "Failed") ) + mocker.patch.object(subtensor, "get_current_block", return_value=1) + mocker.patch.object( + subtensor, + "get_subnet_hyperparameters", + return_value=hyperparams, + ) # Call success, message = commit_reveal.commit_reveal_v3_extrinsic(