Bitcoin-Core

比特幣 - 如何辨識交易是否為隔離見證

  • January 4, 2022

下面是我從比特幣 blk 文件中解析交易的邏輯

  1. 通過獲取 uint32 拉取版本
  2. 讀取接下來的 2 個字節。如果它們是 0,1,那麼它就是 SegWit
  3. 否則再次讀取 varint 以提取輸入計數
  4. 如果輸入計數 = 0,則讀取一個字節並檢查它是否為 0x01。
  5. 如果不是 0x01 則出錯,因為我不知道輸入為 0 的交易類型
  6. 否則讀取 varint 以獲取輸入計數

我知道 blk?????.dat 文件沒有特別的順序,但是在解析前 1000 個文件中的塊時,我在 #5 中沒有遇到這個問題。

上述邏輯似乎有缺陷,因為我無法成功讀取塊。做過這個的人可以解釋一下嗎

還有一個問題,有沒有辦法確定 blk 文件中的塊是否是孤立的並且應該被忽略?

連結的原始數據。它有約 1400 筆交易

有時,我已經編寫了支持從原始數據庫解析見證數據的腳本。例如,使用我的區塊鏈解析器blk01896.dat解析文件(來自我的本地數據庫)。第一個block的解析結果為:0000000000000000000D9C6917E3E865812D419648349D85C40C5A8302842D79

該塊中的第一個事務BC1A08C90D142E4B475E844D16CDD796C2CFEAD15DC2D4C666131EB308CB739A具有解析結果:

transactionVersionNumber = 00000002
Witness activated >>
Inputs count = 01
TX from hash = 0000000000000000000000000000000000000000000000000000000000000000
N output = FFFFFFFF
Input script = 032D470904E181F45D455530322F4254432E434F4D2FFABE6D6D9832F682523CD8FEA4AF9A980D6CAA10B5847FD6CC8D8BE3C9BB5EBB89547F9908000000CEEED33D2425EE84AC19040000000000
sequenceNumber = FFFFFFFF
Outputs count = 3
Value = 000000004B116E87
Output script = 001497CFC76442FE717F2A3F0CC9C175F7561B661997
Value = 0000000000000000
Output script = 6A24AA21A9EDB6B23A8ABBC43D9D9F3005646F9C8FF7307722698A360142E6C40D593FF1931C
Value = 0000000000000000
Output script = 6A2952534B424C4F434B3AF7AD9B2A11A8CE87CF52B80DAF26B1F1AD45FE3CF1BAF3DB9A457874001CADBE
Witness 0 0 32 0000000000000000000000000000000000000000000000000000000000000000
Lock time = 00000000
TX hash = BC1A08C90D142E4B475E844D16CDD796C2CFEAD15DC2D4C666131EB308CB739A

第二筆交易EDDCB4AAD8DDC54E2A1B6FCD216CBF6C3BA2D4879AEE3570A0289C499BD8EF1B

transactionVersionNumber = 00000001
Inputs count = 01
TX from hash = 428AB83113FD33C26020635FC0DCC1B275E6E18DC1E3B4AB8D81610155149968
N output = 000000C2
Input script = 473044022047118E23692DAD799096751526BE098B81328A1763E26FE0AB56CC88691699D002205BF4469E44C975B5DE3971243E5B4925CF5C9A658B049AEA9993202232B88914014104FCF07BB1222F7925F2B7CC15183A40443C578E62EA17100AA3B44BA66905C95D4980AEC4CD2F6EB426D1B1EC45D76724F26901099416B9265B76BA67C8B0B73D
sequenceNumber = FFFFFFFF
Outputs count = 3
Value = 000000000000088E
Output script = 76A914FA0692278AFE508514B5FFEE8FE5E97732CE066988AC
Value = 0000000000000000
Output script = 6A146F6D6E69000000000000000300000000000000E9
Value = 000000000000021C
Output script = A914DE9F32C7F3CBFDCCDF6BF98F596F9B3F830DA56987
Lock time = 00000000
TX hash = EDDCB4AAD8DDC54E2A1B6FCD216CBF6C3BA2D4879AEE3570A0289C499BD8EF1B

第五筆交易B0E255196D16F244BDF552100C052F84DF304BF405CA2FCCB1FF55CB400A8CB2

transactionVersionNumber = 00000002
Witness activated >>
Inputs count = 01
TX from hash = C453DD9343B17E9D728F46EA550831334AD342B5AEA2FDBD1E57CF73A97EC280
N output = 00000001
Input script = 160014369C1A58C231BA8311E26B8DDE1624138F68E980
sequenceNumber = FEFFFFFF
Outputs count = 2
Value = 000000000005E498
Output script = 76A914D5AA6FF082BED43D64D890B9AEFFE67C872D0AFE88AC
Value = 00000000046F8F55
Output script = A914FC249B6158D6CC6A6AD1FB3EE71747B86DF76D1187
Witness 0 0 71 012231AA44DEAA9B391EBC81A7419B93CBB0F50292AF9445D0A1AFB114FB5E1927200232BAF193AF7273F3EE1EFDCA873D3BB524E11B39259AAFCBA7B03E2A30DCC76820024430
Witness 0 1 33 FD1F21A7C45348728989BF8CE4C9CF1B17496DD41B834297D1AA53A3E22CF85303
Lock time = 0009472C
TX hash = B0E255196D16F244BDF552100C052F84DF304BF405CA2FCCB1FF55CB400A8CB2

第一個和第五個 TX 有 Witness,第二個沒有。所以這就是差異。祝你好運!

PS您可以檢查交易的解析腳本部分並查看差異(哪些字節執行見證以及見證欄位在哪裡)。

引用自:https://bitcoin.stackexchange.com/questions/111509