Example #1
0
    def GetLatestData(self, request: xrd_pb2.GetLatestDataReq,
                      context) -> xrd_pb2.GetLatestDataResp:
        logger.debug("[PublicAPI] GetLatestData")
        response = xrd_pb2.GetLatestDataResp()

        all_requested = request.filter == xrd_pb2.GetLatestDataReq.ALL
        quantity = min(request.quantity, self.MAX_REQUEST_QUANTITY)

        if all_requested or request.filter == xrd_pb2.GetLatestDataReq.BLOCKHEADERS:
            result = []
            for blk in self.xrdnode.get_latest_blocks(offset=request.offset,
                                                      count=quantity):
                transaction_count = xrd_pb2.TransactionCount()
                for tx in blk.transactions:
                    transaction_count.count[CODEMAP[tx.WhichOneof(
                        'transactionType')]] += 1

                result.append(
                    xrd_pb2.BlockHeaderExtended(
                        header=blk.blockheader.pbdata,
                        transaction_count=transaction_count))
            response.blockheaders.extend(result)

        if all_requested or request.filter == xrd_pb2.GetLatestDataReq.TRANSACTIONS:
            result = []
            for tx in self.xrdnode.get_latest_transactions(
                    offset=request.offset, count=quantity):
                # FIXME: Improve this once we have a proper database schema
                block_index = self.xrdnode.get_blockidx_from_txhash(tx.txhash)
                block = self.xrdnode.get_block_from_index(block_index)
                header = None
                if block:
                    header = block.blockheader.pbdata
                txextended = xrd_pb2.TransactionExtended(
                    header=header,
                    tx=tx.pbdata,
                    addr_from=tx.addr_from,
                    size=tx.size)
                result.append(txextended)

            response.transactions.extend(result)

        if all_requested or request.filter == xrd_pb2.GetLatestDataReq.TRANSACTIONS_UNCONFIRMED:
            result = []
            for tx_info in self.xrdnode.get_latest_transactions_unconfirmed(
                    offset=request.offset, count=quantity):
                tx = tx_info.transaction
                txextended = xrd_pb2.TransactionExtended(
                    header=None,
                    tx=tx.pbdata,
                    addr_from=tx.addr_from,
                    size=tx.size,
                    timestamp_seconds=tx_info.timestamp)
                result.append(txextended)
            response.transactions_unconfirmed.extend(result)

        return response
Example #2
0
    def GetSlaveTxn(self, request: xrd_pb2.SlaveTxnReq,
                    context) -> xrd_pb2.TransferCoinsResp:
        logger.debug("[PublicAPI] GetSlaveTxn")
        tx = self.xrdnode.create_slave_tx(slave_pks=request.slave_pks,
                                          access_types=request.access_types,
                                          fee=request.fee,
                                          xmss_pk=request.xmss_pk,
                                          master_addr=request.master_addr)

        extended_transaction_unsigned = xrd_pb2.TransactionExtended(
            tx=tx.pbdata, addr_from=tx.addr_from, size=tx.size)
        return xrd_pb2.TransferCoinsResp(
            extended_transaction_unsigned=extended_transaction_unsigned)
Example #3
0
    def GetMessageTxn(self, request: xrd_pb2.MessageTxnReq,
                      context) -> xrd_pb2.TransferCoinsResp:
        logger.debug("[PublicAPI] GetMessageTxn")
        tx = self.xrdnode.create_message_txn(message_hash=request.message,
                                             addr_to=request.addr_to,
                                             fee=request.fee,
                                             xmss_pk=request.xmss_pk,
                                             master_addr=request.master_addr)

        extended_transaction_unsigned = xrd_pb2.TransactionExtended(
            tx=tx.pbdata, addr_from=tx.addr_from, size=tx.size)
        return xrd_pb2.TransferCoinsResp(
            extended_transaction_unsigned=extended_transaction_unsigned)
Example #4
0
    def GetLatticeTxn(self, request: xrd_pb2.LatticeTxnReq,
                      context) -> xrd_pb2.TransferCoinsResp:
        logger.debug("[PublicAPI] GetLatticeTxn")
        tx = self.xrdnode.create_lattice_tx(pk1=request.pk1,
                                            pk2=request.pk2,
                                            pk3=request.pk3,
                                            fee=request.fee,
                                            xmss_pk=request.xmss_pk,
                                            master_addr=request.master_addr)

        extended_transaction_unsigned = xrd_pb2.TransactionExtended(
            tx=tx.pbdata, addr_from=tx.addr_from, size=tx.size)
        return xrd_pb2.TransferCoinsResp(
            extended_transaction_unsigned=extended_transaction_unsigned)
Example #5
0
    def GetMultiSigVoteTxn(self, request: xrd_pb2.MultiSigVoteTxnReq,
                           context) -> xrd_pb2.TransferCoinsResp:
        logger.debug("[PublicAPI] GetMultiSigSpendTxnReq")
        tx = self.xrdnode.create_multi_sig_vote_txn(
            shared_key=request.shared_key,
            unvote=request.unvote,
            fee=request.fee,
            xmss_pk=request.xmss_pk,
            master_addr=request.master_addr)

        extended_transaction_unsigned = xrd_pb2.TransactionExtended(
            tx=tx.pbdata, addr_from=tx.addr_from, size=tx.size)
        return xrd_pb2.TransferCoinsResp(
            extended_transaction_unsigned=extended_transaction_unsigned)
Example #6
0
    def GetMultiSigCreateTxn(self, request: xrd_pb2.MultiSigCreateTxnReq,
                             context) -> xrd_pb2.TransferCoinsResp:
        logger.debug("[PublicAPI] GetMultiSigCreateTxnReq")
        tx = self.xrdnode.create_multi_sig_txn(signatories=request.signatories,
                                               weights=request.weights,
                                               threshold=request.threshold,
                                               fee=request.fee,
                                               xmss_pk=request.xmss_pk,
                                               master_addr=request.master_addr)

        extended_transaction_unsigned = xrd_pb2.TransactionExtended(
            tx=tx.pbdata, addr_from=tx.addr_from, size=tx.size)
        return xrd_pb2.TransferCoinsResp(
            extended_transaction_unsigned=extended_transaction_unsigned)
Example #7
0
    def TransferCoins(self, request: xrd_pb2.TransferCoinsReq,
                      context) -> xrd_pb2.TransferCoinsResp:
        logger.debug("[PublicAPI] TransferCoins")
        tx = self.xrdnode.create_send_tx(addrs_to=request.addresses_to,
                                         amounts=request.amounts,
                                         message_data=request.message_data,
                                         fee=request.fee,
                                         xmss_pk=request.xmss_pk,
                                         master_addr=request.master_addr)

        extended_transaction_unsigned = xrd_pb2.TransactionExtended(
            tx=tx.pbdata, addr_from=tx.addr_from, size=tx.size)
        return xrd_pb2.TransferCoinsResp(
            extended_transaction_unsigned=extended_transaction_unsigned)
Example #8
0
    def GetTransferTokenTxn(self, request: xrd_pb2.TransferTokenTxnReq,
                            context) -> xrd_pb2.TransferCoinsResp:
        logger.debug("[PublicAPI] GetTransferTokenTxn")
        bin_token_txhash = bytes(hstr2bin(request.token_txhash.decode()))
        tx = self.xrdnode.create_transfer_token_txn(
            addrs_to=request.addresses_to,
            token_txhash=bin_token_txhash,
            amounts=request.amounts,
            fee=request.fee,
            xmss_pk=request.xmss_pk,
            master_addr=request.master_addr)

        extended_transaction_unsigned = xrd_pb2.TransactionExtended(
            tx=tx.pbdata, addr_from=tx.addr_from, size=tx.size)
        return xrd_pb2.TransferCoinsResp(
            extended_transaction_unsigned=extended_transaction_unsigned)
Example #9
0
    def GetMultiSigSpendTxn(self, request: xrd_pb2.MultiSigSpendTxnReq,
                            context) -> xrd_pb2.TransferCoinsResp:
        logger.debug("[PublicAPI] GetMultiSigSpendTxnReq")
        tx = self.xrdnode.create_multi_sig_spend_txn(
            multi_sig_address=request.multi_sig_address,
            addrs_to=request.addrs_to,
            amounts=request.amounts,
            expiry_block_number=request.expiry_block_number,
            fee=request.fee,
            xmss_pk=request.xmss_pk,
            master_addr=request.master_addr)

        extended_transaction_unsigned = xrd_pb2.TransactionExtended(
            tx=tx.pbdata, addr_from=tx.addr_from, size=tx.size)
        return xrd_pb2.TransferCoinsResp(
            extended_transaction_unsigned=extended_transaction_unsigned)
Example #10
0
    def GetTokenTxn(self, request: xrd_pb2.TokenTxnReq,
                    context) -> xrd_pb2.TransferCoinsResp:
        logger.debug("[PublicAPI] GetTokenTxn")
        tx = self.xrdnode.create_token_txn(
            symbol=request.symbol,
            name=request.name,
            owner=request.owner,
            decimals=request.decimals,
            initial_balances=request.initial_balances,
            fee=request.fee,
            xmss_pk=request.xmss_pk,
            master_addr=request.master_addr)

        extended_transaction_unsigned = xrd_pb2.TransactionExtended(
            tx=tx.pbdata, addr_from=tx.addr_from, size=tx.size)
        return xrd_pb2.TransferCoinsResp(
            extended_transaction_unsigned=extended_transaction_unsigned)
Example #11
0
    def GetObject(self, request: xrd_pb2.GetObjectReq,
                  context) -> xrd_pb2.GetObjectResp:
        logger.debug("[PublicAPI] GetObject")
        answer = xrd_pb2.GetObjectResp()
        answer.found = False

        # FIXME: We need a unified way to access and validate data.
        query = bytes(
            request.query
        )  # query will be as a string, if Q is detected convert, etc.

        try:
            if AddressState.address_is_valid(query):
                if self.xrdnode.get_address_is_used(query):
                    address_state = self.xrdnode.get_optimized_address_state(
                        query)
                    if address_state is not None:
                        answer.found = True
                        answer.address_state.CopyFrom(address_state.pbdata)
                        return answer
        except ValueError:
            pass

        transaction_block_number = self.xrdnode.get_transaction(query)
        transaction = None
        blockheader = None
        if transaction_block_number:
            transaction, block_number = transaction_block_number
            answer.found = True
            block = self.xrdnode.get_block_from_index(block_number)
            blockheader = block.blockheader.pbdata
            timestamp = block.blockheader.timestamp
        else:
            transaction_timestamp = self.xrdnode.get_unconfirmed_transaction(
                query)
            if transaction_timestamp:
                transaction, timestamp = transaction_timestamp
                answer.found = True

        if transaction:
            txextended = xrd_pb2.TransactionExtended(
                header=blockheader,
                tx=transaction.pbdata,
                addr_from=transaction.addr_from,
                size=transaction.size,
                timestamp_seconds=timestamp)
            answer.transaction.CopyFrom(txextended)
            return answer

        # NOTE: This is temporary, indexes are accepted for blocks
        try:
            block = self.xrdnode.get_block_from_hash(query)
            if block is None or (block.block_number == 0
                                 and block.prev_headerhash !=
                                 config.user.genesis_prev_headerhash):
                query_str = query.decode()
                query_index = int(query_str)
                block = self.xrdnode.get_block_from_index(query_index)
                if not block:
                    return answer

            answer.found = True
            block_extended = xrd_pb2.BlockExtended()
            block_extended.header.CopyFrom(block.blockheader.pbdata)
            block_extended.size = block.size
            for transaction in block.transactions:
                tx = Transaction.from_pbdata(transaction)
                extended_tx = xrd_pb2.TransactionExtended(
                    tx=transaction,
                    addr_from=tx.addr_from,
                    size=tx.size,
                    timestamp_seconds=block.blockheader.timestamp)
                block_extended.extended_transactions.extend([extended_tx])
            answer.block_extended.CopyFrom(block_extended)
            return answer
        except Exception:
            pass

        return answer