Skip to content
Open
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
5 changes: 4 additions & 1 deletion gittensor/cli/issue_commands/vote.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ def vote_list_validators(network: str, rpc_url: str, contract: str, as_json: boo
validators = client.get_validators()

n = len(validators)
required = (n // 2) + 1
# Consensus is undefined when no validators are whitelisted; expose 0 in JSON
# so dashboards can detect the empty-whitelist edge case rather than seeing
# an apparent 1-of-0 threshold.
required = (n // 2) + 1 if n > 0 else 0

if as_json:
emit_json(
Expand Down
60 changes: 60 additions & 0 deletions tests/cli/test_vote_list_consensus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# The MIT License (MIT)
# Copyright © 2025 Entrius

"""Regression tests for `gitt vote list --json` consensus_threshold field."""

import json
from unittest.mock import MagicMock, patch


def _invoke_vote_list_json(cli_root, runner, validators):
"""Run `gitt vote list --json` with the contract client mocked to return ``validators``."""
fake_client = MagicMock()
fake_client.get_validators.return_value = validators

with (
patch(
'gittensor.cli.issue_commands.vote._resolve_contract_and_network',
return_value=('5Fakeaddr', 'ws://x', 'test'),
),
patch('bittensor.Subtensor', return_value=MagicMock()),
patch(
'gittensor.validator.issue_competitions.contract_client.IssueCompetitionContractClient',
return_value=fake_client,
),
):
return runner.invoke(cli_root, ['vote', 'list', '--json'], catch_exceptions=False)


def test_vote_list_json_consensus_threshold_zero_when_no_validators(cli_root, runner):
"""When no validators are whitelisted, consensus_threshold must be 0, not 1."""
result = _invoke_vote_list_json(cli_root, runner, validators=[])

assert result.exit_code == 0
payload = json.loads(result.output)
assert payload == {
'success': True,
'validators': [],
'count': 0,
'consensus_threshold': 0,
}


def test_vote_list_json_consensus_threshold_majority_when_validators_present(cli_root, runner):
"""Sanity check: the existing (n // 2) + 1 majority is preserved for non-empty whitelists."""
result = _invoke_vote_list_json(cli_root, runner, validators=['hk1', 'hk2', 'hk3'])

assert result.exit_code == 0
payload = json.loads(result.output)
assert payload['count'] == 3
assert payload['consensus_threshold'] == 2


def test_vote_list_json_consensus_threshold_one_for_single_validator(cli_root, runner):
"""Sanity check: single-validator whitelist still requires 1 vote."""
result = _invoke_vote_list_json(cli_root, runner, validators=['lonely_hotkey'])

assert result.exit_code == 0
payload = json.loads(result.output)
assert payload['count'] == 1
assert payload['consensus_threshold'] == 1
Loading