tron

tron: how to calculate transaction bandwidth before broadcast transaction to the network


How to calculate transaction bandwidth before broadcast transaction to TRON network. What is the formula or API function I can use?

Thanks


Solution

  • Bandwidth - count of bytes for send transaction to network (size calculated by protobuf protocol).

    You can calculate by two ways:

    1. Create transaction (without broadcast), and calculate by raw_data_hex. Raw data - data of transaction in HEX format. For calculate bytes, divide by 2. But, in this case, you MUST add few bytes because used by protobuf protocol. It's very easy way for calculate. But hard for implement, because you don't know about all bytes what used in protobuf.
    2. Create transaction in protobuf protocol, and calculate size. In this way, you must install protocol, install extension for compile protocol to you code (PHP, JS, etc...). But powerfull, because you always known about size of transaction.

    In our applications, we use first way, because we don't want to install additional extensions and protocols.

    Example on PHP:

    private const MAX_RESULT_SIZE     = 64;
    private const SIGNATURE_SIZE      = 67;
    private const PROTOBUF_EXTRA_SIZE = 3;
    
    $byteSizes = [
        \strlen($transactionData['raw_data_hex']) / 2,
        self::MAX_RESULT_SIZE,
        self::SIGNATURE_SIZE,
        self::PROTOBUF_EXTRA_SIZE,
    ];
    
    $bytes = \array_sum($byteSizes);
    

    But this solution in some cases can return wrong data. Because we hard core size of special data (signature as an example).