Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
print('Hivemind issue hash:', hivemind_issue_hash)
print('\nTest if initializing with a hivemind issue hash results in the correct hivemind issue')
second_hivemind_issue = HivemindIssue(multihash=hivemind_issue_hash)
# note small differences with unicode vs string text are possible but are ok
print(hivemind_issue.get())
print(second_hivemind_issue.get())
assert hivemind_issue.get() == second_hivemind_issue.get()
print('\n\n###############################')
print('#Hivemind state #')
print('###############################')
print('Test initializing hivemind state')
hivemind_state = HivemindState()
assert isinstance(hivemind_state, HivemindState)
print('\nstate hash', hivemind_state.save())
print('Change log:')
pprint(hivemind_state.change_log())
print('\nTest setting hivemind issue')
hivemind_state.set_hivemind_issue(issue_hash=hivemind_issue_hash)
assert hivemind_state.hivemind_issue_hash == hivemind_issue_hash
assert hivemind_state.hivemind_issue().get() == hivemind_issue.get()
print('\nstate hash', hivemind_state.save())
print('Change log:')
pprint(hivemind_state.change_log(max_depth=0))
print('Test initial options is an empty list')
print('\nTest if initializing with a hivemind issue hash results in the correct hivemind issue')
second_hivemind_issue = HivemindIssue(multihash=hivemind_issue_hash)
# note small differences with unicode vs string text are possible but are ok
print(hivemind_issue.get())
print(second_hivemind_issue.get())
assert hivemind_issue.get() == second_hivemind_issue.get()
print('\n\n###############################')
print('#Hivemind state #')
print('###############################')
print('Test initializing hivemind state')
hivemind_state = HivemindState()
assert isinstance(hivemind_state, HivemindState)
print('\nstate hash', hivemind_state.save())
print('Change log:')
pprint(hivemind_state.change_log())
print('\nTest setting hivemind issue')
hivemind_state.set_hivemind_issue(issue_hash=hivemind_issue_hash)
assert hivemind_state.hivemind_issue_hash == hivemind_issue_hash
assert hivemind_state.hivemind_issue().get() == hivemind_issue.get()
print('\nstate hash', hivemind_state.save())
print('Change log:')
pprint(hivemind_state.change_log(max_depth=0))
print('Test initial options is an empty list')
assert hivemind_state.options == []
hivemind_issue.set_answer_type(answer_type=option_type)
assert hivemind_issue.answer_type == option_type
hivemind_issue.set_tags(tags='mycompanyhash')
assert hivemind_issue.tags == 'mycompanyhash'
restrictions = {'addresses': [get_address_from_wallet(account=0, index=0), get_address_from_wallet(account=0, index=1)],
'options_per_address': 10}
hivemind_issue.set_restrictions(restrictions=restrictions)
print('')
hivemind_issue_hash = hivemind_issue.save()
print('Hivemind hash:', hivemind_issue_hash)
hivemind_state = HivemindState()
hivemind_state.set_hivemind_issue(issue_hash=hivemind_issue_hash)
assert hivemind_state.options == []
option_hashes = {}
option_values = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten']
for option_value in option_values:
print('adding option %s' % option_value)
option = HivemindOption()
option.set_hivemind_issue(hivemind_issue_hash=hivemind_issue_hash)
option.answer_type = option_type
option.set(value=option_value)
option_hashes[option_value] = option.save()
print('saved with ipfs hash %s' % option.multihash())
address = get_address_from_wallet(account=0, index=0)
def get_result(self):
ret = {}
for component_name, (hivemind_id, question_index) in self.components.items():
hivemind_state_hash = get_hivemind_state_hash(hivemind_id=hivemind_id)
hivemind_state = HivemindState(multihash=hivemind_state_hash)
value = hivemind_state.get_consensus(question_index=question_index)
LOG.info('Dictionary composite component %s: %s' % (component_name, value))
ret[component_name] = value
return ret
def get_result(self):
if not all(key in self.components for key in ('priority', 'number_of_recipients', 'budget', 'budget_slope')):
LOG.error('Distribution Composite hivemind does not contain all necessary components!')
return
priority_state_hash = get_hivemind_state_hash(self.components['priority'][0])
number_of_recipients_state_hash = get_hivemind_state_hash(self.components['number_of_recipients'][0])
budget_state_hash = get_hivemind_state_hash(self.components['budget'][0])
budget_slope_state_hash = get_hivemind_state_hash(self.components['budget_slope'][0])
priority_state = HivemindState(multihash=priority_state_hash)
number_of_recipients_state = HivemindState(multihash=number_of_recipients_state_hash)
budget_state = HivemindState(multihash=budget_state_hash)
budget_slope_state = HivemindState(multihash=budget_slope_state_hash)
# todo check if there is a consensus on each value
priority = priority_state.get_consensus(question_index=self.components['priority'][1])
number_of_recipients = number_of_recipients_state.get_consensus(question_index=self.components['number_of_recipients'][1])
budget = budget_state.get_consensus(question_index=self.components['budget'][1])
budget_slope = budget_slope_state.get_consensus(question_index=self.components['budget_slope'][1])
LOG.info('Distribution composite hivemind %s:' % self.composite_id)
LOG.info('Recipients ordered by priority: %s' % priority)
LOG.info('Number of recipients: %s' % number_of_recipients)
LOG.info('Recipients ordered by budget: %s' % budget)
LOG.info('Budget slope: %s' % budget_slope)
items_hivemind_state = HivemindState(multihash=items_hivemind_state_hash)
# consensus type should be 'Ranked'
if items_hivemind_state._hivemind_issue.consensus_type != 'Ranked':
raise Exception('items hivemind does not have a Ranked consensus type')
items = items_hivemind_state.get_consensus(question_index=question_index)
# if there is no consensus, set items to an empty list
if items is None:
items = []
# Get the maximum length of the list
hivemind_id, question_index = self.components['max_length']
max_length_hivemind_state_hash = get_hivemind_state_hash(hivemind_id=hivemind_id)
max_length_hivemind_state = HivemindState(multihash=max_length_hivemind_state_hash)
# consensus type should be 'Single' and answer type must be integer
if max_length_hivemind_state._hivemind_issue.consensus_type != 'Single':
raise Exception('max length hivemind does not have a Single consensus type')
if max_length_hivemind_state._hivemind_issue.answer_type != 'Integer':
raise Exception('max length hivemind does not have a Integer answer type')
max_length = max_length_hivemind_state.get_consensus(question_index=question_index)
# if max_length is None the entire list will be returned
return items[:max_length]
def set(self, opinionator, ranked_choice):
"""
Set the list of ranked option hashes
:param opinionator: The id of the person expressing the opinion
:param ranked_choice: A list of sorted option hashes
"""
if not isinstance(self._hivemind_state, HivemindState):
raise Exception('Hivemind state has not been set yet')
self.opinionator = opinionator
self.ranked_choice = ranked_choice
if not self.valid():
raise Exception('invalid ranked choice')
def set_hivemind_state(self, hivemind_state_hash):
self.hivemind_state_hash = hivemind_state_hash
self._hivemind_state = HivemindState(multihash=self.hivemind_state_hash)
def get_result(self):
if not all(key in self.components for key in ('priority', 'number_of_recipients', 'budget', 'budget_slope')):
LOG.error('Distribution Composite hivemind does not contain all necessary components!')
return
priority_state_hash = get_hivemind_state_hash(self.components['priority'][0])
number_of_recipients_state_hash = get_hivemind_state_hash(self.components['number_of_recipients'][0])
budget_state_hash = get_hivemind_state_hash(self.components['budget'][0])
budget_slope_state_hash = get_hivemind_state_hash(self.components['budget_slope'][0])
priority_state = HivemindState(multihash=priority_state_hash)
number_of_recipients_state = HivemindState(multihash=number_of_recipients_state_hash)
budget_state = HivemindState(multihash=budget_state_hash)
budget_slope_state = HivemindState(multihash=budget_slope_state_hash)
# todo check if there is a consensus on each value
priority = priority_state.get_consensus(question_index=self.components['priority'][1])
number_of_recipients = number_of_recipients_state.get_consensus(question_index=self.components['number_of_recipients'][1])
budget = budget_state.get_consensus(question_index=self.components['budget'][1])
budget_slope = budget_slope_state.get_consensus(question_index=self.components['budget_slope'][1])
LOG.info('Distribution composite hivemind %s:' % self.composite_id)
LOG.info('Recipients ordered by priority: %s' % priority)
LOG.info('Number of recipients: %s' % number_of_recipients)
LOG.info('Recipients ordered by budget: %s' % budget)
LOG.info('Budget slope: %s' % budget_slope)
recipients = priority[:number_of_recipients] if number_of_recipients is not None else []