def test_time(self): import datetime b = Block() b.timestamp = str(datetime.datetime.now()) time = "2018-07-16 17:00:00" result = b.compare_block(time) self.assertEqual(result, False)
def test_obtain_account(self): address = '0x32' account = Account.generate_account('TRG', '0x32', '323232', str(datetime.now()), "Agc") block = Block.generate_block( Block.genesis(root.get_root() + '/genesis.json')) block.accounts.setdefault( address, json.dumps(obj=account, default=Account.json_parse)) status, output = Account.obtain_account(address, block) self.assertEqual(status, 200) print(output)
def test_obtain(self): transaction = Transaction.generate_transaction(str(datetime.now()), "{dd:f}", 5, 2, 1) print(transaction.hash) block = Block.generate_block(Block.genesis(root.get_root() + '/genesis.json')) block.transactions.setdefault(transaction.hash, json.dumps(obj=transaction, default=Transaction.json_parse)) status, output = Transaction.obtain_transaction('', block) self.assertEqual(status, 400) print(output) status, output = Transaction.obtain_transaction(transaction.hash, block) self.assertEqual(status, 200) print(output) status, output = Transaction.obtain_transaction('562140165816165', block) self.assertEqual(status, 400) print(output)
def test_obtain(self): ac_transaction = AcTransaction.append_ac_transaction('0x121', str(datetime.now()), None, None, None, 5) b = Block() b.ac_transactions.setdefault(ac_transaction.address, {ac_transaction.hash: json.dumps(ac_transaction, default=AcTransaction.json_parse)}) status, output = AcTransaction.obtain_ac_transaction(ac_transaction.address, ac_transaction.hash, b) self.assertEqual(status, 200) print(output)
def test_json(self): file = root.get_root() + '/genesis.json' block = Block() print(file) with open(file, 'w') as f: json.dump(fp=f, obj=block, default=Block.json_parse) with open(file, 'r') as f: temp = json.load(fp=f, object_hook=Block.json_load) print(temp.__dict__)
def generate_block(pre_block): try: if pre_block: if isinstance(pre_block, Block): _variable['storage_block'] = Block.generate_block(pre_block) return raise BaseException(pre_block) except BaseException as e: logging.error('Generate block failed. Because previous block is error, please let me know, [email protected]!') logging.error('previous block ' + str(e.args[0])) sys.exit(1)
def start(): if _variable['local_node'].generate_mark: _variable['storage_block'].timestamp = str(datetime.now()) _variable['storage_block'].synchronized = True _variable['storage_block'].hash = _variable[ 'storage_block'].hash_block() _variable['synchronized_block'].append(_variable['storage_block']) logging.info('generate block well, and the block hash is {}...'.format( _variable['storage_block'].hash)) _variable['storage_block'] = Block.generate_block( _variable['synchronized_block'][-1]) # update status on block threading.Timer(3, start).start()
def genesis(genesis_path): genesis_file = os.path.join(genesis_path, 'genesis.json') try: if genesis_file: if os.path.isfile(genesis_file): block = Block.genesis(genesis_file) if isinstance(block, Block): block.synchronized = True _variable['synchronized_block'].append(block) return raise BaseException('Genesis is can\'t load, Because your file is be damaged!') except BaseException as e: logging.error(e.args[0]) sys.exit(1)
def init_variable(): try: node = Node() node.address = 'http://{}:{}'.format(_variable['ip'], _variable['port']) node.description = 'this is a common node with address is $address' node.timestamp = str(datetime.now()) node.status = 'agent' node.hash = node.node_hash() _variable['local_node'] = node _variable['agent_nodes'] = [_variable['local_node']] _variable['storage_block'] = Block.generate_block(_variable['synchronized_block'][-1]) return 200, 'all ok' except BaseException as e: sys.stdout.write(e.args[0]) sys.exit(1)
def test_genesis(self): file = root.get_root() block = Block.genesis(file + '/genesis.json') print(block.__dict__) self.assertEqual(block.version, "#version1", "Error")
def test_new_block(self): b = Block() block = Block.generate_block(b) return block