Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.94 KB | None | 0 0
  1.     def _action(self, message):
  2.         """
  3.        Метод обработки запросов от других пользователей
  4.        :param message: <obj> Объект запроса
  5.        :return:
  6.        """
  7.         type_message = self.__json__.get_prop(message, 'header.type').split('_')
  8.  
  9.         if type_message[0] == 'lerner':
  10.             if type_message[1] == 'check-active':
  11.                 self.__send_active(self.__json__.get_prop(message, 'header.id'), 'learner')
  12.  
  13.         elif type_message[0] == 'transaction':
  14.             transaction = self.__json__.get_prop(message, 'data')
  15.  
  16.             if (self.__transactions__.verify_transaction(transaction))== True:
  17.                 #Берём хеш транзакции и далее его хешируем
  18.                 #Это используется для наименования файлов
  19.                 transaction_hash = self.__json__.get_prop(message, 'hash')
  20.                 name_file_transaction = hashlib.sha1((transaction_hash).encode()).hexdigest()
  21.  
  22.                 #TODO: Нужно по красивее сделать путь к папке (да и вообще не понятно правильно ли я прописал путь)
  23.                 #Кто нибудь помогите с путями!!!
  24.  
  25.                 #Создаём файл
  26.                 path_transaction = '../../data/blockchain/raw_transactions/' + name_file_transaction
  27.                 self.__files___.create_file(path_transaction)
  28.  
  29.                 #Записываем транзакцию в файл
  30.                 self.__json__.set_json_in_file(path_transaction, transaction)
  31.  
  32.             else:
  33.                 pass
  34.  
  35.         elif type_message[0] == 'block':
  36.             block = self.__json__.get_prop(message, 'data')
  37.  
  38.             if (self.__blockchain__.check_block(block)) == True:
  39.  
  40.                 #Находим номер последнего элемента
  41.                 str_mass = os.listdir('../../data/blockchain/blocks')
  42.                 int_mass = result = [int(item) for item in str_mass]
  43.                 last_item = max(int_mass)
  44.  
  45.                 name_file_block = last_item + 1
  46.                 path_block = '../../data/blockchain/blocks/' + name_file_block
  47.                 self.__files___.create_file(path_block)
  48.                 self.__json__.set_json_in_file(path_block, block)
  49.  
  50.                 #далее нужно пропарсить блок и найти транзакции созданий пользователей
  51.                 #что бы записать их в users
  52.  
  53.                 #Это массив транзакций
  54.                 mass_transactions = self.__json__.get_prop(message, 'data.transactions')
  55.  
  56.                 for transaction in mass_transactions:
  57.                     type_transaction = self.__json__.get_prop(transaction, 'type')
  58.  
  59.                     if type_transaction == 'new_user':
  60.                         #Создание
  61.                         user_address = self.__json__.get_prop(message, 'data.transactions.sender')
  62.                         user_address_hash = hashlib.sha1((user_address).encode()).hexdigest()
  63.                         path_new_user_dir = '../../data/blockchain/users/' + user_address_hash
  64.                         self.__directorys__.create_directory(path_new_user_dir)
  65.  
  66.                         #Запись
  67.                         path_new_user_file = path_new_user_dir + "/data"
  68.                         self.__files___.create_file(path_new_user_file)
  69.                         self.__json__.set_json_in_file( path_new_user_file, transaction)
  70.                        
  71.                         map_json = {"address": user_address,
  72.                                     "hash_address": user_address_hash}
  73.                         path_map = "'../../data/blockchain/users/map"
  74.                         self.__json__.set_json_in_file(path_map, map_json)
  75.             else:
  76.                 pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement