示例#1
0
    def to_str_list(self):
        """
        list of str so we can concat for batch db insert

        :return: list of string
        """
        return [
            "X'" + poscrypto.raw_to_hex(self.txid) + "'",
            str(self.block_height),
            str(self.timestamp), "'" + self.sender + "'",
            "'" + self.recipient + "'",
            str(self.what), "'" + self.params + "'",
            str(self.value), "X'" + poscrypto.raw_to_hex(self.pubkey) + "'",
            str(self.received)
        ]
示例#2
0
    def to_json(self):
        """
        Json readable version of the PosMessage instance content,
        with *binary content as hex encoded string*.

        :return: string
        """
        tx = self.to_list()
        if tx[0]:
            # hex for txid
            tx[0] = poscrypto.raw_to_hex(tx[0])
        if tx[8]:
            # and for pubkey
            tx[8] = poscrypto.raw_to_hex(tx[8])
        return json.dumps(tx)
示例#3
0
    def to_list(self, as_hex=False):
        """
        List representation of the PosMessage instance.

        :return: list()
        """
        if as_hex:
            return [
                poscrypto.raw_to_hex(self.txid), self.block_height,
                self.timestamp, self.sender, self.recipient, self.what,
                self.params, self.value,
                poscrypto.raw_to_hex(self.pubkey), self.received
            ]
        else:
            return [
                self.txid, self.block_height, self.timestamp, self.sender,
                self.recipient, self.what, self.params, self.value,
                self.pubkey, self.received
            ]
示例#4
0
    async def tx_exists(self, txid):
        """
        Tell is the given txid is in our mempool.

        :return: Boolean
        """
        exists = await self.async_fetchone(SQL_TXID_EXISTS, (txid,))
        if exists:
            if 'txdigest' in config.LOG:
                self.app_log.info("{}[...] already in our mempool".format(poscrypto.raw_to_hex(txid)[:16]))
            return True
        return False
示例#5
0
    async def async_del_txids(self, txids):
        """
        Delete a list of the txs from mempool. Optimized, more than 100x executemany()

        :param txids: binary txid (bytes)
        :return: True
        """
        #
        # FR: Slice to cut in reasonable batches (like 100 to 500)
        params = ["X'" + poscrypto.raw_to_hex(txid) + "'" for txid in txids]
        sql = SQL_REMOVE_TXID_IN + '(' + ', '.join(params) + ')'
        # print(params)
        await self.async_execute(sql, commit=True)
        return True
示例#6
0
    def to_dict(self, as_hex=False):
        """
        Converts the native object format to a dict representing a height status

        :param as_hex: convert raw buffers to hex string encoding so we can json_encode later.
        :return: dict()
        """
        # txs
        height_dict = dict()
        # Main block values
        for prop in self.props:
            height_dict[prop] = self.__dict__[prop]
            if as_hex and prop in self.hex_encodable:
                height_dict[prop] = poscrypto.raw_to_hex(height_dict[prop])
        return height_dict
示例#7
0
    def to_json(self):
        """
        Returns a json representation of the current block object
        with *binary content as hex encoded string*.

        :return: string
        """
        # Get a raw image of the datas
        block = self.to_dict()
        # Then convert the bin data to base64 for proper json encoding
        for key in self.hex_encodable:
            block[key] = poscrypto.raw_to_hex(block[key])
        # We need to_json to convert to hex, but then decode again to have a list we will json encode in the block.
        block['txs'] = [json.loads(tx.to_json()) for tx in self.txs]
        return json.dumps(block)
示例#8
0
    def to_dict(self, as_hex=False):
        """
        Converts the native object format to a dict representing a block.

        :param as_hex: if True, will convert binary fields to hex encoded string
        :return: dict()
        """
        # txs
        block_dict = {'txs': [tx.to_list(as_hex=as_hex) for tx in self.txs]}
        # Main block values
        for prop in self.props:
            if as_hex and prop in self.hex_encodable:
                block_dict[prop] = poscrypto.raw_to_hex(self.__dict__[prop])
            else:
                block_dict[prop] = self.__dict__[prop]
        return block_dict
示例#9
0
    async def tx_exists(self, txid):
        """
        Tell if the given txid is in our chain

        :return:
        """
        # TODO: WARNING, see binary blob sqlite3 and conversion. Seems ok, double check
        # self.app_log.warning("tx_exists?")
        # print(txid)  # debug
        # What is fed to this function? Bytes or hex string?
        exists = await self.async_fetchone(SQL_TXID_EXISTS, (txid,))
        if exists:
            if 'txdigest' in config.LOG:
                self.app_log.info("{}[...] already in our chain".format(poscrypto.raw_to_hex(txid)[:16]))
            return True
        return False