计算交易的虚拟大小(SegWit 地址)

Calculate virtual size of a transaction (SegWit address)

本教程:https://bitzuma.com/posts/making-sense-of-bitcoin-transaction-fees/

提供以下公式:

v = (42 + 272×(number of inputs) + 128×(number of outputs))/4

但是,计算具有 1 个输入和 2 个输出的交易:

v = (42 + 272×(number of inputs) + 128×(number of outputs))/4
v = (42 + 272×(1) + 128×(3))/4
v = 174.5

这两个工具都不匹配:

此外,该公式没有考虑不同类型的输出交易:

注意,这里有一个比特币堆栈交换:https://bitcoin.stackexchange.com/

无论如何,让我们试试这个(在 Fish shell 中,即使不了解 Fish 也应该可以理解):

set addr (bitcoin-cli -regtest getnewaddress "" bech32)
set inputs "[]"
set outputs "[{\"$addr\": 1}]"
set unfunded (bitcoin-cli -regtest createrawtransaction $inputs $outputs)
set funded (bitcoin-cli -regtest fundrawtransaction $unfunded '{"change_type": "bech32"}' | jq .hex --raw-output )
set signed (bitcoin-cli -regtest signrawtransactionwithwallet $funded | jq .hex --raw-output )
bitcoin-cli -regtest decoderawtransaction $signed

这给了我:

{
  "txid": "50633d5ff3de40650a9d0ac4669f408736b8fd98cd8129767650637c11680559",
  "hash": "dc0147a282b811ef23d7325aede8ededbedcc2ecf73321fa7a74eb89521fcdf0",
  "version": 2,
  "size": 245,
  "vsize": 164,
  "weight": 653,
  "locktime": 0,
  "vin": [
    {
      "txid": "f113bb1dc07dc7adccc546870d98d38ac9a0dd22a4d7861b1eeb0ccceb9597d7",
      "vout": 0,
      "scriptSig": {
        "asm": "00146e90108bb6b936e5e2fd9a108ba6e9ca458033b1",
        "hex": "1600146e90108bb6b936e5e2fd9a108ba6e9ca458033b1"
      },
      "txinwitness": [
        "304402200d3ab9fa5ee232c23a5d29d8fec445ae7f868bec8146de8cedfad1fae567a60802202174dfc11bf46ed0ef5fcf17334fa62fdf56d4320b8a0c0b60fdd93983dd5cd301",
        "03a29c4e49ab8f54ba07dff57c2cf2a214135bfffef101549b5935d00612af849d"
      ],
      "sequence": 4294967294
    }
  ],
  "vout": [
    {
      "value": 1.00000000,
      "n": 0,
      "scriptPubKey": {
        "asm": "0 999e92cd7790d23740e8244e09c5397cd8debb67",
        "hex": "0014999e92cd7790d23740e8244e09c5397cd8debb67",
        "reqSigs": 1,
        "type": "witness_v0_keyhash",
        "addresses": [
          "bcrt1qnx0f9nthjrfrws8gy38qn3fe0nvdawm8vcw7lr"
        ]
      }
    },
    {
      "value": 48.99996720,
      "n": 1,
      "scriptPubKey": {
        "asm": "0 e88b5a93dd8082f792f24079ee9537af56a8dfc5",
        "hex": "0014e88b5a93dd8082f792f24079ee9537af56a8dfc5",
        "reqSigs": 1,
        "type": "witness_v0_keyhash",
        "addresses": [
          "bcrt1qaz944y7aszp00yhjgpu7a9fh4at23h79y0z3ht"
        ]
      }
    }
  ]
}

vsize这里是164,和上面工具给我的差别不大。

我认为大小与您发布的工具不同的原因是 public 密钥和签名的大小都是可变的。一般来说,如果不签署交易,就不可能准确知道交易的规模。