def __init__(self, *args): if len(args) != 0: logging.error('wrong number of args for {}'.format( ListAddressesCommand.NAME)) else: BlockchainLoader().process(self._show_balances)
def read_unconfirmed_payments(unconfirmed_payments): transactions = unconfirmed_payments.get_transactions() def read_blockchain_and_sync(blockchain): transactions_to_remove = [t for t in transactions if blockchain.has_transaction(t)] for transaction in transactions_to_remove: unconfirmed_payments.remove(transaction) if len(transactions): BlockchainLoader().process(read_blockchain_and_sync)
def _on_block_request(self, connection, request_bytes): block_id = bytes_to_text(request_bytes) logging.info('{} request for blocks following {}'.format( SERVICE_NAME, block_id)) new_blocks = BlockchainLoader().process( lambda blockchain: blockchain.get_blocks_following(block_id)) if new_blocks is not None: new_blocks_json = block_list_encode(new_blocks) new_blocks_bytes = text_to_bytes(new_blocks_json) connection.send(new_blocks_bytes) logging.info('{} sent {} new blocks'.format( SERVICE_NAME, len(new_blocks))) else: logging.info('{} unknown block requested: {}'.format( SERVICE_NAME, block_id)) connection.close()
def handle_status_update(self, blockchain_length, host, port): def request_blocks_after(blockchain, block_id): new_blocks = self._get_new_blocks(host, port, block_id) for new_block in new_blocks: blockchain.add_block(new_block) self.on_valid_block(new_block) logging.info('Received {} new blocks from {}:{}'.format(len(new_blocks), host, port)) return len(new_blocks) def update_blockchain(blockchain): if blockchain_length > len(blockchain.blocks): while not request_blocks_after(blockchain, blockchain.get_last_block_id()): blockchain.remove_last_block() else: logging.debug('Host {}:{} has {} blocks, ignoring because we already have {}'.format( host, port,blockchain_length, len(blockchain.blocks) )) BlockchainLoader().process(update_blockchain)
def __init__(self, *args): if len(args) != 3: logging.error('wrong number of args for {}'.format(SendCommand.NAME)) else: from_address_or_key, amount_txt, to_address_or_key = args crypto = Crypto() key = crypto.get_key(from_address_or_key) or crypto.get_key_by_address(from_address_or_key) if not key: logging.error('invalid from address/key') elif not (crypto.get_key(to_address_or_key) or self._is_valid_address_format(to_address_or_key)): logging.error('invalid to address/key') elif not self._is_valid_amount_format(amount_txt): logging.error('invalid amount') else: balance = BlockchainLoader().process(lambda b : b.get_balance_for_address(key.address)) amount = float(amount_txt) if balance < amount: logging.error('Insufficient funds, current balance for this address is {}'.format(balance)) else: to_address_key = crypto.get_key(to_address_or_key) if to_address_key: to_address = to_address_key.address else: to_address = to_address_or_key transaction = build_transaction(key.address, amount, to_address, key) UnconfirmedPaymentsLoader().process(lambda u_p : u_p.add(transaction)) SendCommand.send_transaction(transaction)
def _get_blockchain_status_value(self): return BlockchainLoader().process( lambda blockchain: len(blockchain.blocks))
def _on_new_block_mined(self, block): BlockchainLoader().process( lambda blockchain: blockchain.add_block(block))
def _is_transaction_already_in_blockchain(self, transaction): return BlockchainLoader().process(lambda blockchain : blockchain.has_transaction(transaction))
def _get_last_block_id_from_blockchain(self): return BlockchainLoader().process(lambda blockchain : blockchain.get_last_block_id())