3

This answer states that SegWit doesn't reduce transaction size. If this is true, why is it cheaper to send a SegWit transaction?

8

Let's compare a 2 input and 2 output transaction for variants of pay-to-pubkeyhash. (Full data below.)

P2PKH has no witness, so raw size is equal to stripped size is equal to virtual size. A P2PKH transaction with two inputs and two outputs has 374 bytes (= 374 vBytes).

P2SH-P2WPKH (wrapped segwit) locks funds to a P2SH output in which it redirects to a witness program. A P2SH-P2WPKH transaction with two inputs and two outputs has a raw size of 496 bytes, a stripped size (removing witness data) of 226 bytes and a virtual size of 294 vBytes.

P2WPKH (native segwit) does not need the P2SH indirection, but directly resolves a witness program. A P2WPKH transaction with two inputs and two outputs has a raw size of 376 bytes, a stripped size of 154 bytes and a virtual size of 210 vBytes.

Data Footprint: This means that in actual raw hex data P2SH-P2WPKH is the biggest, followed by P2WPKH, and the legacy format P2PKH the smallest. The raw hex size is the data footprint of the transaction for people that store it or the bandwidth cost to transmit it.

Blockweight: However, after applying the weighing that the witness is subject to, P2WPKH has the smallest weight followed by P2SH-P2WPKH and the legacy format P2PKH counts as the heaviest. The virtual size corresponds to the blockchain space that you pay fees for.


P2PKH

TxHeader: 10 bytes, 40 WU/10 vBytes
Input: 148 bytes, 592 WU/148 vBytes
Output: 34 bytes

For legacy formats, the raw size, stripped size and virtual size are all equal.

Tx with 2 inputs and 2 outputs: 374 bytes (raw, stripped and virtual size), 1,496 WU

P2SH-P2WPKH

TxHeader: 12 bytes, 10 bytes stripped size, 2 bytes witness, 42 WU/10.5 vBytes
Input: 210 bytes, 76 bytes stripped size, 134 bytes witness, 438 WU/109.5 vBytes
Output: 32 bytes (outputs have no witness data)

Tx with 2 inputs and 2 outputs: 496 bytes raw size, 226 bytes stripped size, 1,174 WU/294 vBytes virtual size

P2WPKH

TxHeader: 12 bytes, 10 bytes stripped size, 2 bytes witness, 42 WU/10.5 vBytes
Input: 151 bytes, 41 bytes stripped size, 110 bytes witness, 274 WU/68.5 vBytes
Output: 31 bytes

Tx with 2 inputs and 2 outputs: 376 bytes, 154 bytes stripped size, 838 WU/210 vBytes

2

I compared to the following 9 types of transaction's weight. And then I found that No9 (P2WPKH => P2WPKH) was the lightest.

  • Transaction patterns
// 1 transaction consists of 1 txin and 1 txout

1. (in)P2PKH => (out)P2PKH [weight = 764]
2. (in)P2PKH => (out)P2SH-P2WPKH [weight = 756]
3. (in)P2PKH => (out)P2WPKH [weight = 752]
4. (in)P2SH-P2WPKH => (out)P2PKH [weight = 541]
5. (in)P2SH-P2WPKH => (out)P2SH-P2WPKH [weight = 533]
6. (in)P2SH-P2WPKH => (out)P2WPKH [weight = 529]
7. (in)P2WPKH => (out)P2PKH [weight = 449]
8. (in)P2WPKH => (out)P2SH-P2WPKH [weight = 441]
9. (in)P2WPKH => (out)P2WPKH [weight = 437]
  • Result
// I used bitcoin core rpc (v0.17.1) like createrawtransaction, signrawtransactionwithkey and decoderawtransaction.

[INPUT = P2PKH]

// P2PKH => P2PKH
"size": 191,
"vsize": 191,
"weight": 764,

// P2PKH => P2SH-P2WPKH
"size": 189,
"vsize": 189,
"weight": 756,

// P2PKH => P2WPKH
"size": 188,
"vsize": 188,
"weight": 752,

[INPUT = P2SH-P2WPKH]

// P2SH-P2WPKH => P2PKH
"size": 217,
"vsize": 136,
"weight": 541,

// P2SH-P2WPKH => P2SH-P2WPKH
"size": 215,
"vsize": 134,
"weight": 533,

// P2SH-P2WPKH => P2WPKH
"size": 214,
"vsize": 133,
"weight": 529,

[INPUT = P2WPKH]

// P2WPKH => P2PKH
"size": 194,
"vsize": 113,
"weight": 449,

// P2WPKH => P2SH-P2WPKH
"size": 192,
"vsize": 111,
"weight": 441,

// P2WPKH => P2WPKH
"size": 191,
"vsize": 110,
"weight": 437,
  • Detail Result (transaction hex)
[INPUT = P2PKH]

// P2PKH => P2PKH
0200000001d0e13f4319cb8eb82cb4fae204aa92e86f4b300b86443ac6ca7b5e5713a805d5000000006a47304402203f59c75dce2657e2ea0f555bda618f60b5fc7c29d06b388a7dfb9fdd152628170220722b1395dda99e3b09e45937ae768298ba337fe825e9bf799cf84e69b09c31420121034f40428cabea81e7e1f0c7bc11fc4cfff4c22136205d9fde14bce2de34c3c0b0ffffffff01c0e4022a010000001976a9148329298662e6202d231b1a060c9c31740f1abf0088ac00000000

// P2PKH => P2SH-P2WPKH
0200000001d0e13f4319cb8eb82cb4fae204aa92e86f4b300b86443ac6ca7b5e5713a805d5000000006a473044022063141301b08eebcd69fbbf0c9247c43f9bf5294e0f01bfb2da6630cbd4ca0e0802207595dd76e04fe3ba8b74ff82802cd6dd9083438b32ca180349e847ce16272b240121034f40428cabea81e7e1f0c7bc11fc4cfff4c22136205d9fde14bce2de34c3c0b0ffffffff01c0e4022a0100000017a9149900febf4619963e50167e4e8574d66e5b5066218700000000

// P2PKH => P2WPKH
0200000001d0e13f4319cb8eb82cb4fae204aa92e86f4b300b86443ac6ca7b5e5713a805d5000000006a47304402206329cb55c7bc23bb5f0a610d4aac24ced6bd97ace9ee657f5f7f278a2e55b05002204f9b1329dbea5249e1190414963a78af7bd764cfddf7826cb5a8e908a39e787b0121034f40428cabea81e7e1f0c7bc11fc4cfff4c22136205d9fde14bce2de34c3c0b0ffffffff01c0e4022a01000000160014265d42333ea7c83b142cbc3f5e90618f2815b97400000000

[INPUT = P2SH-P2WPKH]

// P2SH-P2WPKH => P2PKH
02000000000101ff516fbcb5ee39d86569b32399ba77f86d8c840a58e9c00bafe9401fac50ba8f000000001716001402e386ba4c3ab0337779e8cf53ee29be1f76e56affffffff01c0e4022a010000001976a9144bd165e45738f90715dd58c66e609e3420745c4088ac024730440220485fda83adf6674683a5dcb3640eb6fade65ff87d8b86b9ff7ad78d7ee2adda3022050901b7877f51931bd21dd042a951327974616ae80cfd045eb9daffa4e3d3a160121032fd7684e4355ea5bb5d690b36177ef0c476031f776b5207aaed8c6f2773dccf500000000

// P2SH-P2WPKH => P2SH-P2WPKH
02000000000101ff516fbcb5ee39d86569b32399ba77f86d8c840a58e9c00bafe9401fac50ba8f000000001716001402e386ba4c3ab0337779e8cf53ee29be1f76e56affffffff01c0e4022a0100000017a914ea78eae3d8b5265a564f60a2d35c92fd80340758870247304402205768fd4a1a43377d602ab071ffd6c10e9cd653f32352734407d5f570633ec8fa02204ec4a3979318654c282122e883ea28f6f345347c771fdd5d178512a0acb3e6680121032fd7684e4355ea5bb5d690b36177ef0c476031f776b5207aaed8c6f2773dccf500000000

// P2SH-P2WPKH => P2WPKH
02000000000101ff516fbcb5ee39d86569b32399ba77f86d8c840a58e9c00bafe9401fac50ba8f000000001716001402e386ba4c3ab0337779e8cf53ee29be1f76e56affffffff01c0e4022a01000000160014795fe27ac90977d005752d76c9b37e0f4709107c0247304402202cf3cc0567f2b4cdc095ba4de1b450bb09e4fe6e8a7e5a9fae2bffd0e200c2350220257ce6193a87b6addcb44fca4245f38ab4470ee8149207460d2712336948cce80121032fd7684e4355ea5bb5d690b36177ef0c476031f776b5207aaed8c6f2773dccf500000000

[INPUT = P2WPKH]

// P2WPKH => P2PKH
02000000000101a33652b7a2408b854878dbf2936b2a29208267d7b747d3b9d6d52aea894c329e0000000000ffffffff01606b042a010000001976a9146626b697454680b06e3c0e2d07272137a97e8be188ac0247304402203c6b03fdb2b5a6b12820bc420af9c0f35d96100aff02b93b9f05d389d1b0f200022059bb8a679febe999216f510149047413dac4b49f6e0d3aa9ef33853141815c2f01210242586c4ad4cb0d7fbe0ecfe902024f8f1581564f8f257a7cc52bf26275283ed200000000

// P2WPKH => P2SH-P2WPKH
02000000000101a33652b7a2408b854878dbf2936b2a29208267d7b747d3b9d6d52aea894c329e0000000000ffffffff01606b042a0100000017a9141548a74c39bf89a83812613bc0e65a933c7e10df870247304402207ed9314ec843a98acd21260760c93418303f25f46ff6861cdc751ce49381988f02203f41929d6668b64b98da22a871ff9d968ab08cf7b81e221f1fc957355c442b5201210242586c4ad4cb0d7fbe0ecfe902024f8f1581564f8f257a7cc52bf26275283ed200000000

// P2WPKH => P2WPKH
02000000000101a33652b7a2408b854878dbf2936b2a29208267d7b747d3b9d6d52aea894c329e0000000000ffffffff01606b042a01000000160014d1c5773209f4b660714ea92e1aa6d5b0338a68aa0247304402205a94a9e82f19d96868ab20d44aa3c0c50b66e103ac36868b3c8c31455b95479e02207200f59dcc0d7745c1524371ed0ff1d49e7df3c30d5416fe9589fd180bd471ed01210242586c4ad4cb0d7fbe0ecfe902024f8f1581564f8f257a7cc52bf26275283ed200000000
  • I think..

The current maximum allowed weight for a block is 4MB. Miners consider fee(in satoshi) per weight of each transaction in order to maximize their profit.

https://github.com/bitcoin/bitcoin/blob/master/src/consensus/consensus.h#L15

  • Thanks, I was looking at your answer and realized that I had some calculation errors. +1 – Murch Mar 23 at 2:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.