def _on_results_collected(self, accountant_results):
     """
     """
     fails = {}
     unique_coins = {}
     for success, result_tuple in accountant_results:
         accountant_idurl, response_packet = result_tuple
         if not success:
             fails[accountant_idurl] = fails.get(accountant_idurl, 0) + 1
             continue
         if response_packet is None:
             fails[accountant_idurl] = fails.get(accountant_idurl, 0) + 1
             continue
         coins_list = coins_io.read_coins_from_packet(response_packet)
         if not coins_list:
             fails[accountant_idurl] = fails.get(accountant_idurl, 0) + 1
             continue
         for coin in coins_list:
             coin_hash = coin['miner']['hash']
             if coin_hash not in unique_coins:
                 unique_coins[coin_hash] = coin
             if coins_io.coin_to_string(unique_coins[coin_hash]
                                        ) != coins_io.coin_to_string(coin):
                 fails[accountant_idurl] = fails.get(accountant_idurl,
                                                     0) + 1
     if not unique_coins:
         if _Debug:
             lg.out(
                 _DebugLevel,
                 'contract_chain_node._on_results_collected : NO COINS FOUND'
             )
         self.result.callback([])
         self._close()
         return None
     if fails:
         lg.warn('conflicting results from accountants: %s' % fails)
         # TODO: find some simple way to establish consensus between accountants
         # if one accountant is cheating or failing, coins from his machine must be filtered out
         # probably here we can trigger a process to replace bad accountants
         self.result.errback(Exception(str(fails)))
         self._close()
         return None
     if _Debug:
         lg.out(
             _DebugLevel,
             'contract_chain_node._on_results_collected : %d COINS FOUND' %
             len(unique_coins))
     self.result.callback(unique_coins.values())
     return None
 def _mine(self, coin_json, difficulty, simplification, starter_length,
           starter_limit):
     data_dump = coins_io.coin_to_string(coin_json)
     starter = self._build_starter(starter_length)
     on = 0
     while True:
         if self._stop_marker():
             if _Debug:
                 lg.out(
                     _DebugLevel,
                     'coins_miner._mine STOPPED, stop marker returned True')
             return None
         check = starter + str(on)
         check += data_dump
         hexdigest = self._build_hash(check)
         if difficulty == self._get_hash_complexity(hexdigest,
                                                    simplification):
             # SOLVED!
             break
         on += 1
         if on > starter_limit:
             starter = self._build_starter(starter_length)
             on = 0
     coin_json['miner'].update({
         'hash': hexdigest,
         'starter': starter + str(on),
         'mined': utime.utcnow_to_sec1970(),
     })
     return coin_json
Exemple #3
0
def work_with_known_difficulty(coin_json,
                               difficulty,
                               simplification=2,
                               starter_length=10,
                               starter_limit=99999,
                               stop_marker=None,
                               prev_hash=''):
    coin_json['miner'] = {
        'idurl': my_id.getIDURL().to_bin(),
    }
    # data_dump = json.dumps(data)
    data_dump = coins_io.coin_to_string(coin_json)
    starter = build_starter(starter_length)
    on = 0
    while True:
        if stop_marker is not None and callable(stop_marker):
            if stop_marker():
                return None
        check = starter + str(on)
        # if coin_json is not None:
        #     check += data_dump
        hexdigest = build_hash(check)
        if difficulty == get_hash_complexity(hexdigest, simplification):
            break
        on += 1
        if on > starter_limit:
            starter = build_starter(starter_length)
            on = 0
    coin_json['miner'].update({
        'hash': hexdigest,
        'prev': prev_hash,
        'starter': starter + str(on),
        'mined': utime.utcnow_to_sec1970(),
    })
    return coin_json
 def _run(self, signed_coin, difficulty, simplification, starter_length, starter_limit):
     acoin = coins_io.get_coin_base(signed_coin)
     data_dump = coins_io.coin_to_string(acoin)
     starter = self._build_starter(starter_length)
     on = 0
     while True:
         if self._stop_marker():
             if _Debug:
                 lg.out(_DebugLevel, "coins_miner._run STOPPED, stop marker returned True")
             return None
         check = starter + str(on)
         check += data_dump
         hexdigest = self._build_hash(check)
         if difficulty != self._get_hash_complexity(hexdigest, simplification):
             on += 1
             if on > starter_limit:
                 starter = self._build_starter(starter_length)
                 on = 0
             continue
         result = {"starter": starter + str(on), "hash": hexdigest, "mined": utime.utcnow_to_sec1970()}
         result.update(signed_coin)
         return result