State Module
- class hivemind.state.HivemindState(cid: str | None = None)[source]
Bases:
IPFSDictChain
A class representing the current state of a Hivemind voting issue.
This class manages the state of a voting issue, including options, opinions, and voting results. It handles the addition of new options and opinions, calculates voting results, and manages restrictions on who can vote.
- Variables:
hivemind_id (str | None) – The IPFS hash of the associated hivemind issue
_issue – The associated hivemind issue object
option_cids (List[str]) – List of option CIDs
opinion_cids (List[Dict[str, Any]]) – List of dictionaries containing opinions for each question
signatures (Dict[str, Dict[str, Dict[str, int]]]) – Dictionary mapping addresses to their signatures
participants (Dict[str, Any]) – Dictionary mapping addresses to their participation data
selected (List[str]) – List of options that have been selected
final (bool) – Whether the hivemind is finalized
- __init__(cid: str | None = None) None [source]
Initialize a new HivemindState.
- Parameters:
cid (str) – The IPFS multihash of the state
- add_opinion(timestamp: int, opinion_hash: str, address: str, signature: str) None [source]
Add an opinion to the hivemind state.
- add_option(timestamp: int, option_hash: str, address: str | None = None, signature: str | None = None) None [source]
Add an option to the hivemind state.
- add_predefined_options() Dict[str, Dict[str, Any]] [source]
Add predefined options to the hivemind state.
- add_signature(address: str, timestamp: int, message: str, signature: str) None [source]
Add a signature to the hivemind state.
- calculate_results(question_index: int = 0) Dict[str, Dict[str, float]] [source]
Calculate the results of the hivemind.
- compare(a: str, b: str, opinion_hash: str) str | None [source]
Helper function to compare 2 Option objects against each other based on a given Opinion.
- consensus(question_index: int = 0) Any [source]
Get the consensus of the hivemind.
- Parameters:
question_index (int) – The index of the question (default=0)
- Returns:
The consensus value
- Return type:
Any
- contributions(results: Dict[str, Dict[str, float]], question_index: int = 0) Dict[str, float] [source]
Get the contributions of the participants.
- get_opinion(cid: str) HivemindOpinion [source]
Get an opinion by its CID.
- Parameters:
cid (str) – The IPFS multihash of the opinion
- Returns:
The opinion object
- Return type:
- get_option(cid: str) HivemindOption [source]
Get an option by its CID.
- Parameters:
cid (str) – The IPFS multihash of the option
- Returns:
The option object
- Return type:
- get_options() List[HivemindOption] [source]
Get list of hivemind options.
- Returns:
List of HivemindOption objects
- Return type:
List[HivemindOption]
- get_sorted_options(question_index: int = 0) List[HivemindOption] [source]
Get the sorted list of options.
- Parameters:
question_index (int) – The index of the question (default=0)
- Returns:
List of HivemindOption objects sorted by highest score
- Return type:
List[HivemindOption]
- hivemind_issue() HivemindIssue [source]
Get the associated hivemind issue.
- Returns:
The associated hivemind issue object
- Return type:
- info() str [source]
Get the information of the hivemind.
- Returns:
A string containing the information of the hivemind
- Return type:
- load(cid: str) None [source]
Load the hivemind state from IPFS.
- Parameters:
cid (str) – The IPFS multihash of the state
- Returns:
None
- options_info() str [source]
Get the information of the options.
- Returns:
A string containing the information of the options
- Return type:
- ranked_consensus(question_index: int = 0) List[Any] [source]
Get the ranked consensus of the hivemind.
- Parameters:
question_index (int) – The index of the question (default=0)
- Returns:
List of consensus values
- Return type:
List[Any]
- results() List[Dict[str, Dict[str, float]]] [source]
Get the results of the hivemind.
- Returns:
The results of the hivemind
- Return type:
Any
- results_info(results: Dict[str, Dict[str, float]], question_index: int = 0) str [source]
Get the results information of the hivemind.
- select_consensus(timestamp: int | None = None, address: str | None = None, signature: str | None = None) List[str] [source]
Select the consensus of the hivemind.
This method selects the option with the highest consensus for each question and sets it as the selected option. If the on_selection property of the hivemind issue is set, it will perform the specified action.
- Parameters:
- Returns:
List of selected option CIDs
- Return type:
List[str]
- Raises:
ValueError – If the hivemind is already finalized
ValueError – If the address is not the author of the hivemind
- set_hivemind_issue(issue_cid: str) None [source]
Set the associated hivemind issue.
- Parameters:
issue_cid (str) – IPFS hash of the hivemind issue
- Returns:
None