Skip to content

Commit

Permalink
Merge pull request #2509 from opentensor/john/crv3
Browse files Browse the repository at this point in the history
add improved reveal-round params
  • Loading branch information
thewhaleking authored Dec 3, 2024
2 parents 588b778 + 084e3b4 commit 79a2c1c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 12 deletions.
15 changes: 11 additions & 4 deletions bittensor/core/extrinsics/commit_reveal.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,24 @@ 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
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(
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(
Expand Down
70 changes: 62 additions & 8 deletions tests/unit_tests/extrinsics/test_commit_reveal.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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(
Expand All @@ -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)
Expand All @@ -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(
Expand Down

0 comments on commit 79a2c1c

Please sign in to comment.