Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def _action(self, message):
- """
- Метод обработки запросов от других пользователей
- :param message: <obj> Объект запроса
- :return:
- """
- type_message = self.__json__.get_prop(message, 'header.type').split('_')
- if type_message[0] == 'lerner':
- if type_message[1] == 'check-active':
- self.__send_active(self.__json__.get_prop(message, 'header.id'), 'learner')
- elif type_message[0] == 'transaction':
- transaction = self.__json__.get_prop(message, 'data')
- if (self.__transactions__.verify_transaction(transaction))== True:
- #Берём хеш транзакции и далее его хешируем
- #Это используется для наименования файлов
- transaction_hash = self.__json__.get_prop(message, 'hash')
- name_file_transaction = hashlib.sha1((transaction_hash).encode()).hexdigest()
- #TODO: Нужно по красивее сделать путь к папке (да и вообще не понятно правильно ли я прописал путь)
- #Кто нибудь помогите с путями!!!
- #Создаём файл
- path_transaction = '../../data/blockchain/raw_transactions/' + name_file_transaction
- self.__files___.create_file(path_transaction)
- #Записываем транзакцию в файл
- self.__json__.set_json_in_file(path_transaction, transaction)
- else:
- pass
- elif type_message[0] == 'block':
- block = self.__json__.get_prop(message, 'data')
- if (self.__blockchain__.check_block(block)) == True:
- #Находим номер последнего элемента
- str_mass = os.listdir('../../data/blockchain/blocks')
- int_mass = result = [int(item) for item in str_mass]
- last_item = max(int_mass)
- name_file_block = last_item + 1
- path_block = '../../data/blockchain/blocks/' + name_file_block
- self.__files___.create_file(path_block)
- self.__json__.set_json_in_file(path_block, block)
- #далее нужно пропарсить блок и найти транзакции созданий пользователей
- #что бы записать их в users
- #Это массив транзакций
- mass_transactions = self.__json__.get_prop(message, 'data.transactions')
- for transaction in mass_transactions:
- type_transaction = self.__json__.get_prop(transaction, 'type')
- if type_transaction == 'new_user':
- #Создание
- user_address = self.__json__.get_prop(message, 'data.transactions.sender')
- user_address_hash = hashlib.sha1((user_address).encode()).hexdigest()
- path_new_user_dir = '../../data/blockchain/users/' + user_address_hash
- self.__directorys__.create_directory(path_new_user_dir)
- #Запись
- path_new_user_file = path_new_user_dir + "/data"
- self.__files___.create_file(path_new_user_file)
- self.__json__.set_json_in_file( path_new_user_file, transaction)
- map_json = {"address": user_address,
- "hash_address": user_address_hash}
- path_map = "'../../data/blockchain/users/map"
- self.__json__.set_json_in_file(path_map, map_json)
- else:
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement