<p>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.</p>
<p>In your example, the transaction hex is</p>
<pre><code>020000000001017fb11165fc5a6edf3bf06176c8915b22e20c3c48966d1d5c5673d4bc76a98c6e0100000000fdffffff024a140000000000001976a9145e3a929c6f941ad02c352b47d33a65bd160afe2f88accc8c0700000000001600149aba0ec86126b94b4dfdff48c4855ec49975c8e50247304402200615b90428b7e857d074ef17da69d5d533adc2c02fb739b04ecb88e38723afcd0220070f30781a8020ba0330a96b2a58358c5b5073ed391cee50ea1650e3972fcb0d012102030df0337a88e6f5c77f593eeb8b9d425742fbee78a4dcfeb66f90dec8e30bf25d560b00
</code></pre>
<p>Let's break this down into the separate fields so we can distinguish which are part of the base size.</p>
<ul>
<li><code>02000000</code> - Transaction version, base size and total size += 4</li>
<li><code>00</code> - Segwit marker, total size += 1</li>
<li><code>01</code> - Segwit flag, total size += 1</li>
<li><code>01</code> - Input count, base size and total size += 1</li>
<li><code>7fb11165fc5a6edf3bf06176c8915b22e20c3c48966d1d5c5673d4bc76a98c6e</code> - Previous txid, base size and total size += 32</li>
<li><code>01000000</code> - Output index, base size and total size += 4</li>
<li><code>00</code> - Length of scriptSig, base size and total size += 1</li>
<li><code>fdffffff</code> - Sequence number, base size and total size += 4</li>
<li><code>02</code> - Output count, base size and total size += 1</li>
<li><code>4a14000000000000</code> - Output amount, base size and total size += 8</li>
<li><code>19</code> = scriptPubKey length, base size and total size += 1</li>
<li><code>76a9145e3a929c6f941ad02c352b47d33a65bd160afe2f88ac</code> - scriptPubKey, base size and total size += 25</li>
<li><code>cc8c070000000000</code> - Output amount, base size and total size += 8</li>
<li><code>16</code> - scriptPubKey length, base size and total size += 1</li>
<li><code>00149aba0ec86126b94b4dfdff48c4855ec49975c8e5</code> - scriptPubKey, base size and total size += 22</li>
<li><code>02</code> - Witness stack item count, total size += 1</li>
<li><code>47</code> - Witness stack item length, total size += 1</li>
<li><code>304402200615b90428b7e857d074ef17da69d5d533adc2c02fb739b04ecb88e38723afcd022070f30781a8020ba0330a96b2a58358c5b5073ed391cee50ea1650e3972fcb0d01</code> - Witness stack item, total size += 71</li>
<li><code>21</code> - Witness stack item length, total size += 1</li>
<li><code>02030df0337a88e6f5c77f593eeb8b9d425742fbee78a4dcfeb66f90dec8e30bf2</code>  - Witness stack item, total size += 33</li>
<li><code>5d560b00</code> - Locktime, base size and total size += 4</li>
</ul>
<p>So the base size comes out to be 116, the total size is 225. The weight is <code>3 * 116 + 225 = 573</code>.</p>
<p>The vsize calculated from the weight is <code>(573 + 4 - 1) / 4 = 144</code>. Calculating the vsize from the sizes is <code>ceil(116 + (225 - 116) / 4) = 144</code>.</p>

CC BY-SA 4.0
2022-07-08T01:32:53.720Z
very clear!.Thanks for your answer
noob
CC BY-SA 4.0
2022-07-07T15:07:30.797Z
2022-07-07T15:10:09.367Z
2022-07-07T15:10:09.367Z
Ava Chow
<p>there's a transaction txid is fa3467452918627ebd63a3e8570e70d38b0eefef683347510b204ba6962ebe44,
using bitcoin-cli we can see it's size,vsize and weight</p>
<p><a href="https://i.sstatic.net/2nvTR.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/2nvTR.png" alt="enter image description here" /></a></p>
<p>we can get it's hex and it's accurately 225.
using the formula vsize=(weight+4-1)/4=576/4=144,there's no problem</p>
<p>but when calculating the vsize bytes,it should be (573-225)/3=116,but the witness data in this transaction is
<a href="https://i.sstatic.net/WtN4V.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/WtN4V.png" alt="enter image description here" /></a></p>
<p>when i delete txinwitness hex data(selected in the following picture) from origin hex(225 bytes),i can't get the result 116.Where is wrong?Can anyone tell me how to calculate?much thanks.
<a href="https://i.sstatic.net/SPhOX.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/SPhOX.png" alt="enter image description here" /></a></p>

CC BY-SA 4.0
2022-07-07T03:54:47.210Z
In your second calculation, what are you trying to calculate? The first calculation is the correct way to calculate vsize. Whatever you are doing in the second completely ignores the witness which is incorrect for vsize calculation.
Ava Chow
CC BY-SA 4.0
2022-07-07T05:02:22.377Z
https://bitcoin.stackexchange.com/questions/80325/calculating-the-witness-size-of-a-transaction?r=SearchResults
like this question's example,i 'd like to calculate the base bytes.according to this posts,weight=3*base_bytes+total_bytes.now weight is 576,total bytes is 225,the base_bytes should be 116,and i wonder where is the 116 base bytes in the total hax 255byes?
noob
CC BY-SA 4.0
2022-07-07T05:08:11.917Z
or i can simplify my question.If I only know the transaction's origin hex 225 bytes.How to calculate the weight and vsize?
noob
<transactions><transaction-weight>
How to accurately calculate the vsize of a transaction?
noob
# How to accurately calculate the vsize of a transaction?

(Asked by: username_0 on 2022-07-07)

there's a transaction txid is fa3467452918627ebd63a3e8570e70d38b0eefef683347510b204ba6962ebe44,
using bitcoin-cli we can see it's size,vsize and weight

[![enter image description here](https://i.sstatic.net/2nvTR.png)](https://i.sstatic.net/2nvTR.png)

we can get it's hex and it's accurately 225.
using the formula vsize=(weight+4-1)/4=576/4=144,there's no problem

but when calculating the vsize bytes,it should be (573-225)/3=116,but the witness data in this transaction is
[![enter image description here](https://i.sstatic.net/WtN4V.png)](https://i.sstatic.net/WtN4V.png)

when i delete txinwitness hex data(selected in the following picture) from origin hex(225 bytes),i can't get the result 116.Where is wrong?Can anyone tell me how to calculate?much thanks.
[![enter image description here](https://i.sstatic.net/SPhOX.png)](https://i.sstatic.net/SPhOX.png)

**Tags:** transactions, transaction-weight

> **Comments:**
> **username_1**: In your second calculation, what are you trying to calculate? The first calculation is the correct way to calculate vsize. Whatever you are doing in the second completely ignores the witness which is incorrect for vsize calculation.  
> **username_0**: https://bitcoin.stackexchange.com/questions/80325/calculating-the-witness-size-of-a-transaction?r=SearchResults like this question's example,i 'd like to calculate the base bytes.according to this posts,weight=3*base_bytes+total_bytes.now weight is 576,total bytes is 225,the base_bytes should be 116,and i wonder where is the 116 base bytes in the total hax 255byes?  
> **username_0**: or i can simplify my question.If I only know the transaction's origin hex 225 bytes.How to calculate the weight and vsize?  

---

## 1 answers

### Answer by username_1 on 2022-07-07. Score: 5 | Accepted answer

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`.

> **Comments:**
> **username_0**: very clear!.Thanks for your answer  

---

