Exemplo n.º 1
0
    def last_unconfirmed_tx(self, data=None):
        logger.info('<<< API last_unconfirmed_tx call')

        addr = {'transactions': []}
        error = {'status': 'error', 'error': 'invalid argument', 'method': 'last_tx', 'parameter': data}

        if not data:
            data = 1
        try:
            n = int(data)
        except:
            return json_print_telnet(error)

        if n <= 0 or n > 20:
            return json_print_telnet(error)

        tx_num = len(self.factory.chain.transaction_pool)
        while tx_num > 0:
            tx_num -= 1
            tx = self.factory.chain.transaction_pool[tx_num]
            if tx.subtype != TX_SUBTYPE_TX:
                continue
            tmp_txn = {'txhash': bin2hstr(tx.txhash),
                       'block': 'unconfirmed',
                       'timestamp': 'unconfirmed',
                       'amount': self.factory.format_qrlamount(tx.amount),
                       'type': tx.subtype}

            tmp_txn['type'] = Transaction.tx_id_to_name(tmp_txn['type'])
            addr['transactions'].append(tmp_txn)

        addr['status'] = 'ok'
        return json_print_telnet(addr)
Exemplo n.º 2
0
    def reformat_txn(self, txn):
        if txn.subtype in (TX_SUBTYPE_TX, TX_SUBTYPE_COINBASE):
            txn.amount = self._format_qrlamount(txn.amount)
        txn.txhash = bin2hstr(txn.txhash)
        txn.pubhash = bin2hstr(txn.pubhash)
        txn.signature = bin2hstr(txn.signature)
        txn.PK = bin2hstr(txn.PK)
        if txn.subtype == TX_SUBTYPE_STAKE:
            txn.slave_public_key = bin2hstr(txn.slave_public_key)
            if txn.first_hash:
                txn.first_hash = bin2hstr(txn.first_hash)
            for j in range(len(txn.hash)):
                txn.hash[j] = bin2hstr(txn.hash[j])

        txn.subtype = Transaction.tx_id_to_name(txn.subtype)

        return txn
Exemplo n.º 3
0
    def _search_address(self, address):

        addr = {'transactions': []}

        txnhash_added = set()

        # FIXME: breaking encapsulation and accessing DB/cache directly from API
        if not self.factory.state.state_address_used(address):
            addr['status'] = 'error'
            addr['error'] = 'Address not found'
            addr['parameter'] = address
            return json_print_telnet(addr)

        # FIXME: This is a duplicate of balance
        # FIXME: breaking encapsulation and accessing DB/cache directly from API
        nonce, balance, pubhash_list = self.factory.state.state_get_address(
            address)
        addr['state'] = {}
        addr['state']['address'] = address
        addr['state']['balance'] = self._format_qrlamount(balance)
        addr['state']['nonce'] = nonce

        for s in self.factory.state.stake_list_get():
            if address == s[0]:
                addr['stake'] = {}
                addr['stake']['selector'] = s[2]
                # pubhashes used could be put here..

        tmp_transactions = []
        for tx in self.factory.chain.transaction_pool:
            if tx.subtype not in (TX_SUBTYPE_TX, TX_SUBTYPE_COINBASE):
                continue
            if tx.txto == address or tx.txfrom == address:
                logger.info('%s found in transaction pool', address)

                tmp_txn = {
                    'subtype': tx.subtype,
                    'txhash': bin2hstr(tx.txhash),
                    'block': 'unconfirmed',
                    'amount': self._format_qrlamount(tx.amount),
                    'nonce': tx.nonce,
                    'ots_key': tx.ots_key,
                    'txto': tx.txto,
                    'txfrom': tx.txfrom,
                    'timestamp': 'unconfirmed'
                }

                if tx.subtype == TX_SUBTYPE_TX:
                    tmp_txn['fee'] = self._format_qrlamount(tx.fee)

                tmp_txn.subtype = Transaction.tx_id_to_name(tx.subtype)

                tmp_transactions.append(tmp_txn)
                txnhash_added.add(tx.txhash)

        addr['transactions'] = tmp_transactions

        my_txn = []
        try:
            my_txn = self.factory.state.db.get('txn_' + address)
        except:
            pass

        for txn_hash in my_txn:
            txn_metadata = self.factory.state.db.get(txn_hash)
            dict_txn_metadata = json.loads(txn_metadata[0])
            if dict_txn_metadata['subtype'] == TX_SUBTYPE_TX:
                tx = SimpleTransaction().json_to_transaction(txn_metadata[0])
            elif dict_txn_metadata['subtype'] == TX_SUBTYPE_COINBASE:
                tx = CoinBase().json_to_transaction(txn_metadata[0])

            if (tx.txto == address or tx.txfrom
                    == address) and tx.txhash not in txnhash_added:
                logger.info('%s found in block %s', address,
                            str(txn_metadata[1]))

                tmp_txn = {
                    'subtype': tx.subtype,
                    'txhash': bin2hstr(tx.txhash),
                    'block': txn_metadata[1],
                    'timestamp': txn_metadata[2],
                    'amount': self._format_qrlamount(tx.amount),
                    'nonce': tx.nonce,
                    'ots_key': tx.ots_key,
                    'txto': tx.txto,
                    'txfrom': tx.txfrom
                }

                if tx.subtype == TX_SUBTYPE_TX:
                    tmp_txn['fee'] = self._format_qrlamount(tx.fee)

                tmp_txn['subtype'] = Transaction.tx_id_to_name(
                    tmp_txn['subtype'])

                addr['transactions'].append(tmp_txn)
                txnhash_added.add(tx.txhash)

        if len(addr['transactions']) > 0:
            addr['state']['transactions'] = len(addr['transactions'])

        if addr == {'transactions': {}}:
            addr = {
                'status': 'error',
                'error': 'address not found',
                'method': 'address',
                'parameter': address
            }
        else:
            addr['status'] = 'ok'

        return json_print_telnet(addr)