|
| 1 | +// This program connects to your local btcd and generates test vectors for |
| 2 | +// 5 blocks and collision space sizes of 1-32 bits. Change the RPC cert path |
| 3 | +// and credentials to run on your system. The program assumes you're running |
| 4 | +// a btcd with cfilter support, which mainline btcd doesn't have; in order to |
| 5 | +// circumvent this assumption, comment out the if block that checks for |
| 6 | +// filter size of DefaultP. |
| 7 | + |
| 8 | +package main |
| 9 | + |
| 10 | +import ( |
| 11 | + "bytes" |
| 12 | + "encoding/hex" |
| 13 | + "fmt" |
| 14 | + "io/ioutil" |
| 15 | + "os" |
| 16 | + "path" |
| 17 | + |
| 18 | + "github.com/roasbeef/btcd/chaincfg" |
| 19 | + "github.com/roasbeef/btcd/chaincfg/chainhash" |
| 20 | + "github.com/roasbeef/btcd/rpcclient" |
| 21 | + "github.com/roasbeef/btcd/wire" |
| 22 | + "github.com/roasbeef/btcutil/gcs" |
| 23 | + "github.com/roasbeef/btcutil/gcs/builder" |
| 24 | +) |
| 25 | + |
| 26 | +func main() { |
| 27 | + err := os.Mkdir("gcstestvectors", os.ModeDir|0755) |
| 28 | + if err != nil { // Don't overwrite existing output if any |
| 29 | + fmt.Println("Couldn't create directory: ", err) |
| 30 | + return |
| 31 | + } |
| 32 | + files := make([]*os.File, 33) |
| 33 | + prevBasicHeaders := make([]chainhash.Hash, 33) |
| 34 | + prevExtHeaders := make([]chainhash.Hash, 33) |
| 35 | + for i := 1; i <= 32; i++ { // Min 1 bit of collision space, max 32 |
| 36 | + var blockBuf bytes.Buffer |
| 37 | + fName := fmt.Sprintf("gcstestvectors/testnet-%02d.csv", i) |
| 38 | + file, err := os.Create(fName) |
| 39 | + if err != nil { |
| 40 | + fmt.Println("Error creating CSV file: ", err.Error()) |
| 41 | + return |
| 42 | + } |
| 43 | + _, err = file.WriteString("Block Height,Block Hash,Block,Previous Basic Header,Previous Ext Header,Basic Filter,Ext Filter,Basic Header,Ext Header\n") |
| 44 | + if err != nil { |
| 45 | + fmt.Println("Error writing to CSV file: ", err.Error()) |
| 46 | + return |
| 47 | + } |
| 48 | + files[i] = file |
| 49 | + basicFilter, err := buildBasicFilter( |
| 50 | + chaincfg.TestNet3Params.GenesisBlock, uint8(i)) |
| 51 | + if err != nil { |
| 52 | + fmt.Println("Error generating basic filter: ", err.Error()) |
| 53 | + return |
| 54 | + } |
| 55 | + prevBasicHeaders[i], err = builder.MakeHeaderForFilter(basicFilter, |
| 56 | + chaincfg.TestNet3Params.GenesisBlock.Header.PrevBlock) |
| 57 | + if err != nil { |
| 58 | + fmt.Println("Error generating header for filter: ", err.Error()) |
| 59 | + return |
| 60 | + } |
| 61 | + if basicFilter == nil { |
| 62 | + basicFilter = &gcs.Filter{} |
| 63 | + } |
| 64 | + extFilter, err := buildExtFilter( |
| 65 | + chaincfg.TestNet3Params.GenesisBlock, uint8(i)) |
| 66 | + if err != nil { |
| 67 | + fmt.Println("Error generating ext filter: ", err.Error()) |
| 68 | + return |
| 69 | + } |
| 70 | + prevExtHeaders[i], err = builder.MakeHeaderForFilter(extFilter, |
| 71 | + chaincfg.TestNet3Params.GenesisBlock.Header.PrevBlock) |
| 72 | + if err != nil { |
| 73 | + fmt.Println("Error generating header for filter: ", err.Error()) |
| 74 | + return |
| 75 | + } |
| 76 | + if extFilter == nil { |
| 77 | + extFilter = &gcs.Filter{} |
| 78 | + } |
| 79 | + err = chaincfg.TestNet3Params.GenesisBlock.Serialize(&blockBuf) |
| 80 | + if err != nil { |
| 81 | + fmt.Println("Error serializing block to buffer: ", err.Error()) |
| 82 | + return |
| 83 | + } |
| 84 | + bfBytes, err := basicFilter.NBytes() |
| 85 | + if err != nil { |
| 86 | + fmt.Println("Couldn't get NBytes(): ", err) |
| 87 | + return |
| 88 | + } |
| 89 | + efBytes, err := extFilter.NBytes() |
| 90 | + if err != nil { |
| 91 | + fmt.Println("Couldn't get NBytes(): ", err) |
| 92 | + return |
| 93 | + } |
| 94 | + err = writeCSVRow( |
| 95 | + file, |
| 96 | + 0, // Height |
| 97 | + *chaincfg.TestNet3Params.GenesisHash, |
| 98 | + blockBuf.Bytes(), |
| 99 | + chaincfg.TestNet3Params.GenesisBlock.Header.PrevBlock, |
| 100 | + chaincfg.TestNet3Params.GenesisBlock.Header.PrevBlock, |
| 101 | + bfBytes, |
| 102 | + efBytes, |
| 103 | + prevBasicHeaders[i], |
| 104 | + prevExtHeaders[i], |
| 105 | + ) |
| 106 | + if err != nil { |
| 107 | + fmt.Println("Error writing to CSV file: ", err.Error()) |
| 108 | + return |
| 109 | + } |
| 110 | + } |
| 111 | + cert, err := ioutil.ReadFile( |
| 112 | + path.Join(os.Getenv("HOME"), "/.btcd/rpc.cert")) |
| 113 | + if err != nil { |
| 114 | + fmt.Println("Couldn't read RPC cert: ", err.Error()) |
| 115 | + return |
| 116 | + } |
| 117 | + conf := rpcclient.ConnConfig{ |
| 118 | + Host: "127.0.0.1:18334", |
| 119 | + Endpoint: "ws", |
| 120 | + User: "kek", |
| 121 | + Pass: "kek", |
| 122 | + Certificates: cert, |
| 123 | + } |
| 124 | + client, err := rpcclient.New(&conf, nil) |
| 125 | + if err != nil { |
| 126 | + fmt.Println("Couldn't create a new client: ", err.Error()) |
| 127 | + return |
| 128 | + } |
| 129 | + for height := 1; height < 988000; height++ { |
| 130 | + fmt.Printf("Height: %d\n", height) |
| 131 | + blockHash, err := client.GetBlockHash(int64(height)) |
| 132 | + if err != nil { |
| 133 | + fmt.Println("Couldn't get block hash: ", err.Error()) |
| 134 | + return |
| 135 | + } |
| 136 | + block, err := client.GetBlock(blockHash) |
| 137 | + if err != nil { |
| 138 | + fmt.Println("Couldn't get block hash: ", err.Error()) |
| 139 | + return |
| 140 | + } |
| 141 | + var blockBuf bytes.Buffer |
| 142 | + err = block.Serialize(&blockBuf) |
| 143 | + if err != nil { |
| 144 | + fmt.Println("Error serializing block to buffer: ", err.Error()) |
| 145 | + return |
| 146 | + } |
| 147 | + blockBytes := blockBuf.Bytes() |
| 148 | + for i := 1; i <= 32; i++ { |
| 149 | + basicFilter, err := buildBasicFilter(block, uint8(i)) |
| 150 | + if err != nil { |
| 151 | + fmt.Println("Error generating basic filter: ", err.Error()) |
| 152 | + return |
| 153 | + } |
| 154 | + basicHeader, err := builder.MakeHeaderForFilter(basicFilter, |
| 155 | + prevBasicHeaders[i]) |
| 156 | + if err != nil { |
| 157 | + fmt.Println("Error generating header for filter: ", err.Error()) |
| 158 | + return |
| 159 | + } |
| 160 | + if basicFilter == nil { |
| 161 | + basicFilter = &gcs.Filter{} |
| 162 | + } |
| 163 | + extFilter, err := buildExtFilter(block, uint8(i)) |
| 164 | + if err != nil { |
| 165 | + fmt.Println("Error generating ext filter: ", err.Error()) |
| 166 | + return |
| 167 | + } |
| 168 | + extHeader, err := builder.MakeHeaderForFilter(extFilter, |
| 169 | + prevExtHeaders[i]) |
| 170 | + if err != nil { |
| 171 | + fmt.Println("Error generating header for filter: ", err.Error()) |
| 172 | + return |
| 173 | + } |
| 174 | + if extFilter == nil { |
| 175 | + extFilter = &gcs.Filter{} |
| 176 | + } |
| 177 | + if i == builder.DefaultP { // This is the default filter size so we can check against the server's info |
| 178 | + filter, err := client.GetCFilter(blockHash, wire.GCSFilterRegular) |
| 179 | + if err != nil { |
| 180 | + fmt.Println("Error getting basic filter: ", err.Error()) |
| 181 | + return |
| 182 | + } |
| 183 | + nBytes, err := basicFilter.NBytes() |
| 184 | + if err != nil { |
| 185 | + fmt.Println("Couldn't get NBytes(): ", err) |
| 186 | + return |
| 187 | + } |
| 188 | + if !bytes.Equal(filter.Data, nBytes) { |
| 189 | + // Don't error on empty filters |
| 190 | + fmt.Println("Basic filter doesn't match!\n", filter.Data, "\n", nBytes) |
| 191 | + return |
| 192 | + } |
| 193 | + filter, err = client.GetCFilter(blockHash, wire.GCSFilterExtended) |
| 194 | + if err != nil { |
| 195 | + fmt.Println("Error getting extended filter: ", err.Error()) |
| 196 | + return |
| 197 | + } |
| 198 | + nBytes, err = extFilter.NBytes() |
| 199 | + if err != nil { |
| 200 | + fmt.Println("Couldn't get NBytes(): ", err) |
| 201 | + return |
| 202 | + } |
| 203 | + if !bytes.Equal(filter.Data, nBytes) { |
| 204 | + fmt.Println("Extended filter doesn't match!") |
| 205 | + return |
| 206 | + } |
| 207 | + header, err := client.GetCFilterHeader(blockHash, wire.GCSFilterRegular) |
| 208 | + if err != nil { |
| 209 | + fmt.Println("Error getting basic header: ", err.Error()) |
| 210 | + return |
| 211 | + } |
| 212 | + if !bytes.Equal(header.PrevFilterHeader[:], basicHeader[:]) { |
| 213 | + fmt.Println("Basic header doesn't match!") |
| 214 | + return |
| 215 | + } |
| 216 | + header, err = client.GetCFilterHeader(blockHash, wire.GCSFilterExtended) |
| 217 | + if err != nil { |
| 218 | + fmt.Println("Error getting extended header: ", err.Error()) |
| 219 | + return |
| 220 | + } |
| 221 | + if !bytes.Equal(header.PrevFilterHeader[:], extHeader[:]) { |
| 222 | + fmt.Println("Extended header doesn't match!") |
| 223 | + return |
| 224 | + } |
| 225 | + fmt.Println("Verified against server") |
| 226 | + } |
| 227 | + switch height { |
| 228 | + case 1, 2, 3, 926485, 987876: // Blocks for test cases |
| 229 | + var bfBytes []byte |
| 230 | + var efBytes []byte |
| 231 | + if basicFilter.N() > 0 { |
| 232 | + bfBytes, err = basicFilter.NBytes() |
| 233 | + if err != nil { |
| 234 | + fmt.Println("Couldn't get NBytes(): ", err) |
| 235 | + return |
| 236 | + } |
| 237 | + } |
| 238 | + if extFilter.N() > 0 { // Exclude special case for block 987876 |
| 239 | + efBytes, err = extFilter.NBytes() |
| 240 | + if err != nil { |
| 241 | + fmt.Println("Couldn't get NBytes(): ", err) |
| 242 | + return |
| 243 | + } |
| 244 | + } |
| 245 | + writeCSVRow( |
| 246 | + files[i], |
| 247 | + height, |
| 248 | + *blockHash, |
| 249 | + blockBytes, |
| 250 | + prevBasicHeaders[i], |
| 251 | + prevExtHeaders[i], |
| 252 | + bfBytes, |
| 253 | + efBytes, |
| 254 | + basicHeader, |
| 255 | + extHeader) |
| 256 | + } |
| 257 | + prevBasicHeaders[i] = basicHeader |
| 258 | + prevExtHeaders[i] = extHeader |
| 259 | + } |
| 260 | + } |
| 261 | +} |
| 262 | + |
| 263 | +// writeCSVRow writes a test vector to a CSV file. |
| 264 | +func writeCSVRow(file *os.File, height int, blockHash chainhash.Hash, |
| 265 | + blockBytes []byte, prevBasicHeader, prevExtHeader chainhash.Hash, |
| 266 | + basicFilter, extFilter []byte, basicHeader, extHeader chainhash.Hash) error { |
| 267 | + row := fmt.Sprintf("%d,%s,%s,%s,%s,%s,%s,%s,%s\n", |
| 268 | + height, |
| 269 | + blockHash.String(), |
| 270 | + hex.EncodeToString(blockBytes), |
| 271 | + prevBasicHeader.String(), |
| 272 | + prevExtHeader.String(), |
| 273 | + hex.EncodeToString(basicFilter), |
| 274 | + hex.EncodeToString(extFilter), |
| 275 | + basicHeader.String(), |
| 276 | + extHeader.String(), |
| 277 | + ) |
| 278 | + _, err := file.WriteString(row) |
| 279 | + if err != nil { |
| 280 | + return err |
| 281 | + } |
| 282 | + return nil |
| 283 | +} |
| 284 | + |
| 285 | +// buildBasicFilter builds a basic GCS filter from a block. A basic GCS filter |
| 286 | +// will contain all the previous outpoints spent within a block, as well as the |
| 287 | +// data pushes within all the outputs created within a block. p is specified as |
| 288 | +// an argument in order to create test vectors with various values for p. |
| 289 | +func buildBasicFilter(block *wire.MsgBlock, p uint8) (*gcs.Filter, error) { |
| 290 | + blockHash := block.BlockHash() |
| 291 | + b := builder.WithKeyHashP(&blockHash, p) |
| 292 | + |
| 293 | + // If the filter had an issue with the specified key, then we force it |
| 294 | + // to bubble up here by calling the Key() function. |
| 295 | + _, err := b.Key() |
| 296 | + if err != nil { |
| 297 | + return nil, err |
| 298 | + } |
| 299 | + |
| 300 | + // In order to build a basic filter, we'll range over the entire block, |
| 301 | + // adding the outpoint data as well as the data pushes within the |
| 302 | + // pkScript. |
| 303 | + for i, tx := range block.Transactions { |
| 304 | + // First we'll compute the bash of the transaction and add that |
| 305 | + // directly to the filter. |
| 306 | + txHash := tx.TxHash() |
| 307 | + b.AddHash(&txHash) |
| 308 | + |
| 309 | + // Skip the inputs for the coinbase transaction |
| 310 | + if i != 0 { |
| 311 | + // Each each txin, we'll add a serialized version of |
| 312 | + // the txid:index to the filters data slices. |
| 313 | + for _, txIn := range tx.TxIn { |
| 314 | + b.AddOutPoint(txIn.PreviousOutPoint) |
| 315 | + } |
| 316 | + } |
| 317 | + |
| 318 | + // For each output in a transaction, we'll add each of the |
| 319 | + // individual data pushes within the script. |
| 320 | + for _, txOut := range tx.TxOut { |
| 321 | + b.AddEntry(txOut.PkScript) |
| 322 | + } |
| 323 | + } |
| 324 | + |
| 325 | + return b.Build() |
| 326 | +} |
| 327 | + |
| 328 | +// buildExtFilter builds an extended GCS filter from a block. An extended |
| 329 | +// filter supplements a regular basic filter by include all the _witness_ data |
| 330 | +// found within a block. This includes all the data pushes within any signature |
| 331 | +// scripts as well as each element of an input's witness stack. Additionally, |
| 332 | +// the _hashes_ of each transaction are also inserted into the filter. p is |
| 333 | +// specified as an argument in order to create test vectors with various values |
| 334 | +// for p. |
| 335 | +func buildExtFilter(block *wire.MsgBlock, p uint8) (*gcs.Filter, error) { |
| 336 | + blockHash := block.BlockHash() |
| 337 | + b := builder.WithKeyHashP(&blockHash, p) |
| 338 | + |
| 339 | + // If the filter had an issue with the specified key, then we force it |
| 340 | + // to bubble up here by calling the Key() function. |
| 341 | + _, err := b.Key() |
| 342 | + if err != nil { |
| 343 | + return nil, err |
| 344 | + } |
| 345 | + |
| 346 | + // In order to build an extended filter, we add the hash of each |
| 347 | + // transaction as well as each piece of witness data included in both |
| 348 | + // the sigScript and the witness stack of an input. |
| 349 | + for i, tx := range block.Transactions { |
| 350 | + // Skip the inputs for the coinbase transaction |
| 351 | + if i != 0 { |
| 352 | + // Next, for each input, we'll add the sigScript (if |
| 353 | + // it's present), and also the witness stack (if it's |
| 354 | + // present) |
| 355 | + for _, txIn := range tx.TxIn { |
| 356 | + if txIn.SignatureScript != nil { |
| 357 | + b.AddScript(txIn.SignatureScript) |
| 358 | + } |
| 359 | + |
| 360 | + if len(txIn.Witness) != 0 { |
| 361 | + b.AddWitness(txIn.Witness) |
| 362 | + } |
| 363 | + } |
| 364 | + } |
| 365 | + } |
| 366 | + |
| 367 | + return b.Build() |
| 368 | +} |
0 commit comments