How to calculate transaction bandwidth
before broadcast transaction to TRON network. What is the formula or API function I can use?
Thanks
Bandwidth - count of bytes for send transaction to network (size calculated by protobuf protocol).
You can calculate by two ways:
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.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).