MultiversX Tracker is Live!

How to accurately calculate the vsize of a transaction?

Bitcoin Stack Exchange

Bitcoin News / Bitcoin Stack Exchange 240 Views

It is not possible to calculate the size, weight, or vsize, purely from the JSON decoding of the transaction as the JSON decoding does not output the hex values of various counters, as well as the segwit marker and flag bytes. If you try to reconstruct the transaction with only the information that is output, you will end up with a total size that is less than it's actual size.

In your example, the transaction hex is

020000000001017fb11165fc5a6edf3bf06176c8915b22e20c3c48966d1d5c5673d4bc76a98c6e0100000000fdffffff024a140000000000001976a9145e3a929c6f941ad02c352b47d33a65bd160afe2f88accc8c0700000000001600149aba0ec86126b94b4dfdff48c4855ec49975c8e50247304402200615b90428b7e857d074ef17da69d5d533adc2c02fb739b04ecb88e38723afcd0220070f30781a8020ba0330a96b2a58358c5b5073ed391cee50ea1650e3972fcb0d012102030df0337a88e6f5c77f593eeb8b9d425742fbee78a4dcfeb66f90dec8e30bf25d560b00

Let's break this down into the separate fields so we can distinguish which are part of the base size.

  • 02000000 - Transaction version, base size and total size += 4
  • 00 - Segwit marker, total size += 1
  • 01 - Segwit flag, total size += 1
  • 01 - Input count, base size and total size += 1
  • 7fb11165fc5a6edf3bf06176c8915b22e20c3c48966d1d5c5673d4bc76a98c6e - Previous txid, base size and total size += 32
  • 01000000 - Output index, base size and total size += 4
  • 00 - Length of scriptSig, base size and total size += 1
  • fdffffff - Sequence number, base size and total size += 4
  • 02 - Output count, base size and total size += 1
  • 4a14000000000000 - Output amount, base size and total size += 8
  • 19 = scriptPubKey length, base size and total size += 1
  • 76a9145e3a929c6f941ad02c352b47d33a65bd160afe2f88ac - scriptPubKey, base size and total size += 25
  • cc8c070000000000 - Output amount, base size and total size += 8
  • 16 - scriptPubKey length, base size and total size += 1
  • 00149aba0ec86126b94b4dfdff48c4855ec49975c8e5 - scriptPubKey, base size and total size += 22
  • 02 - Witness stack item count, total size += 1
  • 47 - Witness stack item length, total size += 1
  • 304402200615b90428b7e857d074ef17da69d5d533adc2c02fb739b04ecb88e38723afcd022070f30781a8020ba0330a96b2a58358c5b5073ed391cee50ea1650e3972fcb0d01 - Witness stack item, total size += 71
  • 21 - Witness stack item length, total size += 1
  • 02030df0337a88e6f5c77f593eeb8b9d425742fbee78a4dcfeb66f90dec8e30bf2 - Witness stack item, total size += 33
  • 5d560b00 - Locktime, base size and total size += 4

So the base size comes out to be 116, the total size is 225. The weight is 3 * 116 + 225 = 573.

The vsize calculated from the weight is (573 + 4 - 1) / 4 = 144. Calculating the vsize from the sizes is ceil(116 + (225 - 116) / 4) = 144.


Get BONUS $200 for FREE!

You can get bonuses upto $100 FREE BONUS when you:
πŸ’° Install these recommended apps:
πŸ’² SocialGood - 100% Crypto Back on Everyday Shopping
πŸ’² xPortal - The DeFi For The Next Billion
πŸ’² CryptoTab Browser - Lightweight, fast, and ready to mine!
πŸ’° Register on these recommended exchanges:
🟑 Binance🟑 Bitfinex🟑 Bitmart🟑 Bittrex🟑 Bitget
🟑 CoinEx🟑 Crypto.com🟑 Gate.io🟑 Huobi🟑 Kucoin.



Comments