示例#1
0
    def minimum_tx_weight(self, tx: BaseTransaction) -> float:
        """ Returns the minimum weight for the param tx
            The minimum is calculated by the following function:

            w = alpha * log(size, 2) +       4.0         + 4.0
                                       ----------------
                                        1 + k / amount

            :param tx: tx to calculate the minimum weight
            :type tx: :py:class:`hathor.transaction.transaction.Transaction`

            :return: minimum weight for the tx
            :rtype: float
        """
        # In test mode we don't validate the minimum weight for tx
        # We do this to allow generating many txs for testing
        if self.test_mode & TestMode.TEST_TX_WEIGHT:
            return 1

        if tx.is_genesis:
            return self.min_tx_weight

        tx_size = len(tx.get_struct())

        # We need to take into consideration the decimal places because it is inside the amount.
        # For instance, if one wants to transfer 20 HTRs, the amount will be 2000.
        # Max below is preventing division by 0 when handling authority methods that have no outputs
        amount = max(1, tx.sum_outputs) / (10**settings.DECIMAL_PLACES)
        weight = (+self.min_tx_weight_coefficient * log(tx_size, 2) + 4 /
                  (1 + self.min_tx_weight_k / amount) + 4)

        # Make sure the calculated weight is at least the minimum
        weight = max(weight, self.min_tx_weight)

        return weight
示例#2
0
 def send_data(self, tx: BaseTransaction) -> None:
     """ Send a DATA message.
     """
     # self.log.debug('Sending {tx.hash_hex}...', tx=tx)
     payload = base64.b64encode(tx.get_struct()).decode('ascii')
     self.send_message(ProtocolMessages.DATA, payload)