func TestFilterInsertUpdateNone(t *testing.T) { f := bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateNone) // Add the generation pubkey inputStr := "04eaafc2314def4ca98ac970241bcab022b9c1e1f4ea423a20f134c" + "876f2c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce13ad1357231a" + "2252247d97a46a91" inputBytes, err := hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterInsertUpdateNone DecodeString failed: %v", err) return } f.Add(inputBytes) // Add the output address for the 4th transaction inputStr = "b6efd80d99179f4f4ff6f4dd0a007d018c385d21" inputBytes, err = hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterInsertUpdateNone DecodeString failed: %v", err) return } f.Add(inputBytes) inputStr = "147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b" sha, err := wire.NewShaHashFromStr(inputStr) if err != nil { t.Errorf("TestFilterInsertUpdateNone NewShaHashFromStr failed: %v", err) return } outpoint := wire.NewOutPoint(sha, 0) if f.MatchesOutPoint(outpoint) { t.Errorf("TestFilterInsertUpdateNone matched outpoint %s", inputStr) return } inputStr = "02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041" sha, err = wire.NewShaHashFromStr(inputStr) if err != nil { t.Errorf("TestFilterInsertUpdateNone NewShaHashFromStr failed: %v", err) return } outpoint = wire.NewOutPoint(sha, 0) if f.MatchesOutPoint(outpoint) { t.Errorf("TestFilterInsertUpdateNone matched outpoint %s", inputStr) return } }
// This example demonstrates how to create a new bloom filter, add a transaction // hash to it, and check if the filter matches the transaction. func ExampleNewFilter() { rand.Seed(time.Now().UnixNano()) tweak := rand.Uint32() // Create a new bloom filter intended to hold 10 elements with a 0.01% // false positive rate and does not include any automatic update // functionality when transactions are matched. filter := bloom.NewFilter(10, tweak, 0.0001, wire.BloomUpdateNone) // Create a transaction hash and add it to the filter. This particular // trasaction is the first transaction in block 310,000 of the main // bitcoin block chain. txHashStr := "fd611c56ca0d378cdcd16244b45c2ba9588da3adac367c4ef43e808b280b8a45" txHash, err := wire.NewShaHashFromStr(txHashStr) if err != nil { fmt.Println(err) return } filter.AddShaHash(txHash) // Show that the filter matches. matches := filter.Matches(txHash.Bytes()) fmt.Println("Filter Matches?:", matches) // Output: // Filter Matches?: true }
// TestTx tests the API for Tx. func TestTx(t *testing.T) { testTx := Block100000.Transactions[0] tx := coinutil.NewTx(testTx) // Ensure we get the same data back out. if msgTx := tx.MsgTx(); !reflect.DeepEqual(msgTx, testTx) { t.Errorf("MsgTx: mismatched MsgTx - got %v, want %v", spew.Sdump(msgTx), spew.Sdump(testTx)) } // Ensure transaction index set and get work properly. wantIndex := 0 tx.SetIndex(0) if gotIndex := tx.Index(); gotIndex != wantIndex { t.Errorf("Index: mismatched index - got %v, want %v", gotIndex, wantIndex) } // Hash for block 100,000 transaction 0. wantShaStr := "8c14f0db3df150123e6f3dbbf30f8b955a8249b62ac1d1ff16284aefa3d06d87" wantSha, err := wire.NewShaHashFromStr(wantShaStr) if err != nil { t.Errorf("NewShaHashFromStr: %v", err) } // Request the sha multiple times to test generation and caching. for i := 0; i < 2; i++ { sha := tx.Sha() if !sha.IsEqual(wantSha) { t.Errorf("Sha #%d mismatched sha - got %v, want %v", i, sha, wantSha) } } }
func TestMerkleBlock3(t *testing.T) { blockStr := "0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b" + "4b8b0000000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdc" + "c96b2c3ff60abe184f196367291b4d4c86041b8fa45d630101000000010" + "00000000000000000000000000000000000000000000000000000000000" + "0000ffffffff08044c86041b020a02ffffffff0100f2052a01000000434" + "104ecd3229b0571c3be876feaac0442a9f13c5a572742927af1dc623353" + "ecf8c202225f64868137a18cdd85cbbb4c74fbccfd4f49639cf1bdc94a5" + "672bb15ad5d4cac00000000" blockBytes, err := hex.DecodeString(blockStr) if err != nil { t.Errorf("TestMerkleBlock3 DecodeString failed: %v", err) return } blk, err := coinutil.NewBlockFromBytes(blockBytes) if err != nil { t.Errorf("TestMerkleBlock3 NewBlockFromBytes failed: %v", err) return } f := bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr := "63194f18be0af63f2c6bc9dc0f777cbefed3d9415c4af83f3ee3a3d669c00cb5" sha, err := wire.NewShaHashFromStr(inputStr) if err != nil { t.Errorf("TestMerkleBlock3 NewShaHashFromStr failed: %v", err) return } f.AddShaHash(sha) mBlock, _ := bloom.NewMerkleBlock(blk, f) wantStr := "0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b4" + "b8b0000000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc" + "96b2c3ff60abe184f196367291b4d4c86041b8fa45d630100000001b50c" + "c069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f196" + "30101" want, err := hex.DecodeString(wantStr) if err != nil { t.Errorf("TestMerkleBlock3 DecodeString failed: %v", err) return } got := bytes.NewBuffer(nil) err = mBlock.BtcEncode(got, wire.ProtocolVersion) if err != nil { t.Errorf("TestMerkleBlock3 BtcEncode failed: %v", err) return } if !bytes.Equal(want, got.Bytes()) { t.Errorf("TestMerkleBlock3 failed merkle block comparison: "+ "got %v want %v", got.Bytes, want) return } }
// TestCalcSignatureHash runs the Bitcoin Core signature hash calculation tests // in sighash.json. // https://github.com/bitcoin/bitcoin/blob/master/src/test/data/sighash.json func TestCalcSignatureHash(t *testing.T) { file, err := ioutil.ReadFile("data/sighash.json") if err != nil { t.Errorf("TestCalcSignatureHash: %v\n", err) return } var tests [][]interface{} err = json.Unmarshal(file, &tests) if err != nil { t.Errorf("TestCalcSignatureHash couldn't Unmarshal: %v\n", err) return } for i, test := range tests { if i == 0 { // Skip first line -- contains comments only. continue } if len(test) != 5 { t.Fatalf("TestCalcSignatureHash: Test #%d has "+ "wrong length.", i) } tx := wire.NewMsgTx() rawTx, _ := hex.DecodeString(test[0].(string)) err := tx.Deserialize(bytes.NewReader(rawTx)) if err != nil { t.Errorf("TestCalcSignatureHash failed test #%d: "+ "Failed to parse transaction: %v", i, err) continue } subScript, _ := hex.DecodeString(test[1].(string)) parsedScript, err := TstParseScript(subScript) if err != nil { t.Errorf("TestCalcSignatureHash failed test #%d: "+ "Failed to parse sub-script: %v", i, err) continue } hash := TstCalcSignatureHash(parsedScript, SigHashType(test[3].(float64)), tx, int(test[2].(float64))) expectedHash, _ := wire.NewShaHashFromStr(test[4].(string)) if !bytes.Equal(hash, expectedHash.Bytes()) { t.Errorf("TestCalcSignatureHash failed test #%d: "+ "Signature hash mismatch.", i) } } }
// newShaHashFromStr converts the passed big-endian hex string into a // wire.ShaHash. It only differs from the one available in wire in that // it panics on an error since it will only (and must only) be called with // hard-coded, and therefore known good, hashes. func newShaHashFromStr(hexStr string) *wire.ShaHash { sha, err := wire.NewShaHashFromStr(hexStr) if err != nil { // Ordinarily I don't like panics in library code since it // can take applications down without them having a chance to // recover which is extremely annoying, however an exception is // being made in this case because the only way this can panic // is if there is an error in the hard-coded hashes. Thus it // will only ever potentially panic on init and therefore is // 100% predictable. panic(err) } return sha }
// TestBlockSha tests the ability to generate the hash of a block accurately. func TestBlockSha(t *testing.T) { // Block 1 hash. hashStr := "839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048" wantHash, err := wire.NewShaHashFromStr(hashStr) if err != nil { t.Errorf("NewShaHashFromStr: %v", err) } // Ensure the hash produced is expected. blockHash := blockOne.BlockSha() if !blockHash.IsEqual(wantHash) { t.Errorf("BlockSha: wrong hash - got %v, want %v", spew.Sprint(blockHash), spew.Sprint(wantHash)) } }
// parseBlock parses a btcws definition of the block a tx is mined it to the // Block structure of the wtxmgr package, and the block index. This is done // here since stcrpcclient doesn't parse this nicely for us. func parseBlock(block *btcjson.BlockDetails) (*wtxmgr.BlockMeta, error) { if block == nil { return nil, nil } blksha, err := wire.NewShaHashFromStr(block.Hash) if err != nil { return nil, err } blk := &wtxmgr.BlockMeta{ Block: wtxmgr.Block{ Height: block.Height, Hash: *blksha, }, Time: time.Unix(block.Time, 0), } return blk, nil }
// TestTxSha tests the ability to generate the hash of a transaction accurately. func TestTxSha(t *testing.T) { // Hash of first transaction from block 113875. hashStr := "f051e59b5e2503ac626d03aaeac8ab7be2d72ba4b7e97119c5852d70d52dcb86" wantHash, err := wire.NewShaHashFromStr(hashStr) if err != nil { t.Errorf("NewShaHashFromStr: %v", err) return } // First transaction from block 113875. msgTx := wire.NewMsgTx() txIn := wire.TxIn{ PreviousOutPoint: wire.OutPoint{ Hash: wire.ShaHash{}, Index: 0xffffffff, }, SignatureScript: []byte{0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62}, Sequence: 0xffffffff, } txOut := wire.TxOut{ Value: 5000000000, PkScript: []byte{ 0x41, // OP_DATA_65 0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5, 0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42, 0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1, 0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24, 0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97, 0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78, 0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20, 0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63, 0xa6, // 65-byte signature 0xac, // OP_CHECKSIG }, } msgTx.AddTxIn(&txIn) msgTx.AddTxOut(&txOut) msgTx.LockTime = 0 // Ensure the hash produced is expected. txHash := msgTx.TxSha() if !txHash.IsEqual(wantHash) { t.Errorf("TxSha: wrong hash - got %v, want %v", spew.Sprint(txHash), spew.Sprint(wantHash)) } }
// TestBlockTxShas tests the ability to generate a slice of all transaction // hashes from a block accurately. func TestBlockTxShas(t *testing.T) { // Block 1, transaction 1 hash. hashStr := "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098" wantHash, err := wire.NewShaHashFromStr(hashStr) if err != nil { t.Errorf("NewShaHashFromStr: %v", err) return } wantShas := []wire.ShaHash{*wantHash} shas, err := blockOne.TxShas() if err != nil { t.Errorf("TxShas: %v", err) } if !reflect.DeepEqual(shas, wantShas) { t.Errorf("TxShas: wrong transaction hashes - got %v, want %v", spew.Sdump(shas), spew.Sdump(wantShas)) } }
// TestHaveBlock tests the HaveBlock API to ensure proper functionality. func TestHaveBlock(t *testing.T) { // Load up blocks such that there is a side chain. // (genesis block) -> 1 -> 2 -> 3 -> 4 // \-> 3a testFiles := []string{ "blk_0_to_4.dat.bz2", "blk_3A.dat.bz2", } var blocks []*coinutil.Block for _, file := range testFiles { blockTmp, err := loadBlocks(file) if err != nil { t.Errorf("Error loading file: %v\n", err) return } for _, block := range blockTmp { blocks = append(blocks, block) } } // Create a new database and chain instance to run tests against. chain, teardownFunc, err := chainSetup("haveblock") if err != nil { t.Errorf("Failed to setup chain instance: %v", err) return } defer teardownFunc() // Since we're not dealing with the real block chain, disable // checkpoints and set the coinbase maturity to 1. chain.DisableCheckpoints(true) blockchain.TstSetCoinbaseMaturity(1) timeSource := blockchain.NewMedianTime() for i := 1; i < len(blocks); i++ { isOrphan, err := chain.ProcessBlock(blocks[i], timeSource, blockchain.BFNone) if err != nil { t.Errorf("ProcessBlock fail on block %v: %v\n", i, err) return } if isOrphan { t.Errorf("ProcessBlock incorrectly returned block %v "+ "is an orphan\n", i) return } } // Insert an orphan block. isOrphan, err := chain.ProcessBlock(coinutil.NewBlock(&Block100000), timeSource, blockchain.BFNone) if err != nil { t.Errorf("Unable to process block: %v", err) return } if !isOrphan { t.Errorf("ProcessBlock indicated block is an not orphan when " + "it should be\n") return } tests := []struct { hash string want bool }{ // Genesis block should be present (in the main chain). {hash: chaincfg.MainNetParams.GenesisHash.String(), want: true}, // Block 3a should be present (on a side chain). {hash: "00000000474284d20067a4d33f6a02284e6ef70764a3a26d6a5b9df52ef663dd", want: true}, // Block 100000 should be present (as an orphan). {hash: "000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506", want: true}, // Random hashes should not be availble. {hash: "123", want: false}, } for i, test := range tests { hash, err := wire.NewShaHashFromStr(test.hash) if err != nil { t.Errorf("NewShaHashFromStr: %v", err) continue } result, err := chain.HaveBlock(hash) if err != nil { t.Errorf("HaveBlock #%d unexpected error: %v", i, err) return } if result != test.want { t.Errorf("HaveBlock #%d got %v want %v", i, result, test.want) continue } } }
// TestGetBlocksWireErrors performs negative tests against wire encode and // decode of MsgGetBlocks to confirm error paths work correctly. func TestGetBlocksWireErrors(t *testing.T) { // Set protocol inside getheaders message. Use protocol version 60002 // specifically here instead of the latest because the test data is // using bytes encoded with that protocol version. pver := uint32(60002) wireErr := &wire.MessageError{} // Block 99499 hash. hashStr := "2710f40c87ec93d010a6fd95f42c59a2cbacc60b18cf6b7957535" hashLocator, err := wire.NewShaHashFromStr(hashStr) if err != nil { t.Errorf("NewShaHashFromStr: %v", err) } // Block 99500 hash. hashStr = "2e7ad7b9eef9479e4aabc65cb831269cc20d2632c13684406dee0" hashLocator2, err := wire.NewShaHashFromStr(hashStr) if err != nil { t.Errorf("NewShaHashFromStr: %v", err) } // Block 100000 hash. hashStr = "3ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506" hashStop, err := wire.NewShaHashFromStr(hashStr) if err != nil { t.Errorf("NewShaHashFromStr: %v", err) } // MsgGetBlocks message with multiple block locators and a stop hash. baseGetBlocks := wire.NewMsgGetBlocks(hashStop) baseGetBlocks.ProtocolVersion = pver baseGetBlocks.AddBlockLocatorHash(hashLocator2) baseGetBlocks.AddBlockLocatorHash(hashLocator) baseGetBlocksEncoded := []byte{ 0x62, 0xea, 0x00, 0x00, // Protocol version 60002 0x02, // Varint for number of block locator hashes 0xe0, 0xde, 0x06, 0x44, 0x68, 0x13, 0x2c, 0x63, 0xd2, 0x20, 0xcc, 0x69, 0x12, 0x83, 0xcb, 0x65, 0xbc, 0xaa, 0xe4, 0x79, 0x94, 0xef, 0x9e, 0x7b, 0xad, 0xe7, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, // Block 99500 hash 0x35, 0x75, 0x95, 0xb7, 0xf6, 0x8c, 0xb1, 0x60, 0xcc, 0xba, 0x2c, 0x9a, 0xc5, 0x42, 0x5f, 0xd9, 0x6f, 0x0a, 0x01, 0x3d, 0xc9, 0x7e, 0xc8, 0x40, 0x0f, 0x71, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, // Block 99499 hash 0x06, 0xe5, 0x33, 0xfd, 0x1a, 0xda, 0x86, 0x39, 0x1f, 0x3f, 0x6c, 0x34, 0x32, 0x04, 0xb0, 0xd2, 0x78, 0xd4, 0xaa, 0xec, 0x1c, 0x0b, 0x20, 0xaa, 0x27, 0xba, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, // Hash stop } // Message that forces an error by having more than the max allowed // block locator hashes. maxGetBlocks := wire.NewMsgGetBlocks(hashStop) for i := 0; i < wire.MaxBlockLocatorsPerMsg; i++ { maxGetBlocks.AddBlockLocatorHash(&mainNetGenesisHash) } maxGetBlocks.BlockLocatorHashes = append(maxGetBlocks.BlockLocatorHashes, &mainNetGenesisHash) maxGetBlocksEncoded := []byte{ 0x62, 0xea, 0x00, 0x00, // Protocol version 60002 0xfd, 0xf5, 0x01, // Varint for number of block loc hashes (501) } tests := []struct { in *wire.MsgGetBlocks // Value to encode buf []byte // Wire encoding pver uint32 // Protocol version for wire encoding max int // Max size of fixed buffer to induce errors writeErr error // Expected write error readErr error // Expected read error }{ // Force error in protocol version. {baseGetBlocks, baseGetBlocksEncoded, pver, 0, io.ErrShortWrite, io.EOF}, // Force error in block locator hash count. {baseGetBlocks, baseGetBlocksEncoded, pver, 4, io.ErrShortWrite, io.EOF}, // Force error in block locator hashes. {baseGetBlocks, baseGetBlocksEncoded, pver, 5, io.ErrShortWrite, io.EOF}, // Force error in stop hash. {baseGetBlocks, baseGetBlocksEncoded, pver, 69, io.ErrShortWrite, io.EOF}, // Force error with greater than max block locator hashes. {maxGetBlocks, maxGetBlocksEncoded, pver, 7, wireErr, wireErr}, } t.Logf("Running %d tests", len(tests)) for i, test := range tests { // Encode to wire format. w := newFixedWriter(test.max) err := test.in.BtcEncode(w, test.pver) if reflect.TypeOf(err) != reflect.TypeOf(test.writeErr) { t.Errorf("BtcEncode #%d wrong error got: %v, want: %v", i, err, test.writeErr) continue } // For errors which are not of type wire.MessageError, check // them for equality. if _, ok := err.(*wire.MessageError); !ok { if err != test.writeErr { t.Errorf("BtcEncode #%d wrong error got: %v, "+ "want: %v", i, err, test.writeErr) continue } } // Decode from wire format. var msg wire.MsgGetBlocks r := newFixedReader(test.max, test.buf) err = msg.BtcDecode(r, test.pver) if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) { t.Errorf("BtcDecode #%d wrong error got: %v, want: %v", i, err, test.readErr) continue } // For errors which are not of type wire.MessageError, check // them for equality. if _, ok := err.(*wire.MessageError); !ok { if err != test.readErr { t.Errorf("BtcDecode #%d wrong error got: %v, "+ "want: %v", i, err, test.readErr) continue } } } }
// newShaHashFromStr converts the passed big-endian hex string into a // wire.ShaHash. It only differs from the one available in wire in that it // ignores the error since it will only (and must only) be called with // hard-coded, and therefore known good, hashes. func newShaHashFromStr(hexStr string) *wire.ShaHash { sha, _ := wire.NewShaHashFromStr(hexStr) return sha }
// TestNewShaHashFromStr executes tests against the NewShaHashFromStr function. func TestNewShaHashFromStr(t *testing.T) { tests := []struct { in string want wire.ShaHash err error }{ // Genesis hash. { "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f", mainNetGenesisHash, nil, }, // Genesis hash with stripped leading zeros. { "19d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f", mainNetGenesisHash, nil, }, // Empty string. { "", wire.ShaHash{}, nil, }, // Single digit hash. { "1", wire.ShaHash([wire.HashSize]byte{ // Make go vet happy. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }), nil, }, // Block 203707 with stripped leading zeros. { "3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc", wire.ShaHash([wire.HashSize]byte{ // Make go vet happy. 0xdc, 0xe9, 0x69, 0x10, 0x94, 0xda, 0x23, 0xc7, 0xe7, 0x67, 0x13, 0xd0, 0x75, 0xd4, 0xa1, 0x0b, 0x79, 0x40, 0x08, 0xa6, 0x36, 0xac, 0xc2, 0x4b, 0x26, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }), nil, }, // Hash string that is too long. { "01234567890123456789012345678901234567890123456789012345678912345", wire.ShaHash{}, wire.ErrHashStrSize, }, // Hash string that is contains non-hex chars. { "abcdefg", wire.ShaHash{}, hex.InvalidByteError('g'), }, } unexpectedErrStr := "NewShaHashFromStr #%d failed to detect expected error - got: %v want: %v" unexpectedResultStr := "NewShaHashFromStr #%d got: %v want: %v" t.Logf("Running %d tests", len(tests)) for i, test := range tests { result, err := wire.NewShaHashFromStr(test.in) if err != test.err { t.Errorf(unexpectedErrStr, i, err, test.err) continue } else if err != nil { // Got expected error. Move on to the next test. continue } if !test.want.IsEqual(result) { t.Errorf(unexpectedResultStr, i, result, &test.want) continue } } }
func TestFilterBloomMatch(t *testing.T) { str := "01000000010b26e9b7735eb6aabdf358bab62f9816a21ba9ebdb719d5299e" + "88607d722c190000000008b4830450220070aca44506c5cef3a16ed519d7" + "c3c39f8aab192c4e1c90d065f37b8a4af6141022100a8e160b856c2d43d2" + "7d8fba71e5aef6405b8643ac4cb7cb3c462aced7f14711a0141046d11fee" + "51b0e60666d5049a9101a72741df480b96ee26488a4d3466b95c9a40ac5e" + "eef87e10a5cd336c19a84565f80fa6c547957b7700ff4dfbdefe76036c33" + "9ffffffff021bff3d11000000001976a91404943fdd508053c75000106d3" + "bc6e2754dbcff1988ac2f15de00000000001976a914a266436d296554760" + "8b9e15d9032a7b9d64fa43188ac00000000" strBytes, err := hex.DecodeString(str) if err != nil { t.Errorf("TestFilterBloomMatch DecodeString failure: %v", err) return } tx, err := coinutil.NewTxFromBytes(strBytes) if err != nil { t.Errorf("TestFilterBloomMatch NewTxFromBytes failure: %v", err) return } spendingTxBytes := []byte{0x01, 0x00, 0x00, 0x00, 0x01, 0x6b, 0xff, 0x7f, 0xcd, 0x4f, 0x85, 0x65, 0xef, 0x40, 0x6d, 0xd5, 0xd6, 0x3d, 0x4f, 0xf9, 0x4f, 0x31, 0x8f, 0xe8, 0x20, 0x27, 0xfd, 0x4d, 0xc4, 0x51, 0xb0, 0x44, 0x74, 0x01, 0x9f, 0x74, 0xb4, 0x00, 0x00, 0x00, 0x00, 0x8c, 0x49, 0x30, 0x46, 0x02, 0x21, 0x00, 0xda, 0x0d, 0xc6, 0xae, 0xce, 0xfe, 0x1e, 0x06, 0xef, 0xdf, 0x05, 0x77, 0x37, 0x57, 0xde, 0xb1, 0x68, 0x82, 0x09, 0x30, 0xe3, 0xb0, 0xd0, 0x3f, 0x46, 0xf5, 0xfc, 0xf1, 0x50, 0xbf, 0x99, 0x0c, 0x02, 0x21, 0x00, 0xd2, 0x5b, 0x5c, 0x87, 0x04, 0x00, 0x76, 0xe4, 0xf2, 0x53, 0xf8, 0x26, 0x2e, 0x76, 0x3e, 0x2d, 0xd5, 0x1e, 0x7f, 0xf0, 0xbe, 0x15, 0x77, 0x27, 0xc4, 0xbc, 0x42, 0x80, 0x7f, 0x17, 0xbd, 0x39, 0x01, 0x41, 0x04, 0xe6, 0xc2, 0x6e, 0xf6, 0x7d, 0xc6, 0x10, 0xd2, 0xcd, 0x19, 0x24, 0x84, 0x78, 0x9a, 0x6c, 0xf9, 0xae, 0xa9, 0x93, 0x0b, 0x94, 0x4b, 0x7e, 0x2d, 0xb5, 0x34, 0x2b, 0x9d, 0x9e, 0x5b, 0x9f, 0xf7, 0x9a, 0xff, 0x9a, 0x2e, 0xe1, 0x97, 0x8d, 0xd7, 0xfd, 0x01, 0xdf, 0xc5, 0x22, 0xee, 0x02, 0x28, 0x3d, 0x3b, 0x06, 0xa9, 0xd0, 0x3a, 0xcf, 0x80, 0x96, 0x96, 0x8d, 0x7d, 0xbb, 0x0f, 0x91, 0x78, 0xff, 0xff, 0xff, 0xff, 0x02, 0x8b, 0xa7, 0x94, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xba, 0xde, 0xec, 0xfd, 0xef, 0x05, 0x07, 0x24, 0x7f, 0xc8, 0xf7, 0x42, 0x41, 0xd7, 0x3b, 0xc0, 0x39, 0x97, 0x2d, 0x7b, 0x88, 0xac, 0x40, 0x94, 0xa8, 0x02, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xc1, 0x09, 0x32, 0x48, 0x3f, 0xec, 0x93, 0xed, 0x51, 0xf5, 0xfe, 0x95, 0xe7, 0x25, 0x59, 0xf2, 0xcc, 0x70, 0x43, 0xf9, 0x88, 0xac, 0x00, 0x00, 0x00, 0x00, 0x00} spendingTx, err := coinutil.NewTxFromBytes(spendingTxBytes) if err != nil { t.Errorf("TestFilterBloomMatch NewTxFromBytes failure: %v", err) return } f := bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr := "b4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b" sha, err := wire.NewShaHashFromStr(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch NewShaHashFromStr failed: %v\n", err) return } f.AddShaHash(sha) if !f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch didn't match sha %s", inputStr) } f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "6bff7fcd4f8565ef406dd5d63d4ff94f318fe82027fd4dc451b04474019f74b4" shaBytes, err := hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err) return } f.Add(shaBytes) if !f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch didn't match sha %s", inputStr) } f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "30450220070aca44506c5cef3a16ed519d7c3c39f8aab192c4e1c90d065" + "f37b8a4af6141022100a8e160b856c2d43d27d8fba71e5aef6405b8643" + "ac4cb7cb3c462aced7f14711a01" shaBytes, err = hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err) return } f.Add(shaBytes) if !f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch didn't match input signature %s", inputStr) } f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "046d11fee51b0e60666d5049a9101a72741df480b96ee26488a4d3466b95" + "c9a40ac5eeef87e10a5cd336c19a84565f80fa6c547957b7700ff4dfbdefe" + "76036c339" shaBytes, err = hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err) return } f.Add(shaBytes) if !f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch didn't match input pubkey %s", inputStr) } f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "04943fdd508053c75000106d3bc6e2754dbcff19" shaBytes, err = hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err) return } f.Add(shaBytes) if !f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch didn't match output address %s", inputStr) } if !f.MatchTxAndUpdate(spendingTx) { t.Errorf("TestFilterBloomMatch spendingTx didn't match output address %s", inputStr) } f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "a266436d2965547608b9e15d9032a7b9d64fa431" shaBytes, err = hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err) return } f.Add(shaBytes) if !f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch didn't match output address %s", inputStr) } f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b" sha, err = wire.NewShaHashFromStr(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch NewShaHashFromStr failed: %v\n", err) return } outpoint := wire.NewOutPoint(sha, 0) f.AddOutPoint(outpoint) if !f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch didn't match outpoint %s", inputStr) } f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "00000009e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436" sha, err = wire.NewShaHashFromStr(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch NewShaHashFromStr failed: %v\n", err) return } f.AddShaHash(sha) if f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch matched sha %s", inputStr) } f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "0000006d2965547608b9e15d9032a7b9d64fa431" shaBytes, err = hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err) return } f.Add(shaBytes) if f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch matched address %s", inputStr) } f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b" sha, err = wire.NewShaHashFromStr(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch NewShaHashFromStr failed: %v\n", err) return } outpoint = wire.NewOutPoint(sha, 1) f.AddOutPoint(outpoint) if f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch matched outpoint %s", inputStr) } f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "000000d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b" sha, err = wire.NewShaHashFromStr(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch NewShaHashFromStr failed: %v\n", err) return } outpoint = wire.NewOutPoint(sha, 0) f.AddOutPoint(outpoint) if f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch matched outpoint %s", inputStr) } }
// TestCheckTransactionStandard tests the checkTransactionStandard API. func TestCheckTransactionStandard(t *testing.T) { // Create some dummy, but otherwise standard, data for transactions. prevOutHash, err := wire.NewShaHashFromStr("01") if err != nil { t.Fatalf("NewShaHashFromStr: unexpected error: %v", err) } dummyPrevOut := wire.OutPoint{Hash: *prevOutHash, Index: 1} dummySigScript := bytes.Repeat([]byte{0x00}, 65) dummyTxIn := wire.TxIn{ PreviousOutPoint: dummyPrevOut, SignatureScript: dummySigScript, Sequence: wire.MaxTxInSequenceNum, } addrHash := [20]byte{0x01} addr, err := coinutil.NewAddressPubKeyHash(addrHash[:], &chaincfg.TestNet3Params) if err != nil { t.Fatalf("NewAddressPubKeyHash: unexpected error: %v", err) } dummyPkScript, err := txscript.PayToAddrScript(addr) if err != nil { t.Fatalf("PayToAddrScript: unexpected error: %v", err) } dummyTxOut := wire.TxOut{ Value: 100000000, // 1 BTC PkScript: dummyPkScript, } tests := []struct { name string tx wire.MsgTx height int32 isStandard bool code wire.RejectCode }{ { name: "Typical pay-to-pubkey-hash transaction", tx: wire.MsgTx{ Version: 1, TxIn: []*wire.TxIn{&dummyTxIn}, TxOut: []*wire.TxOut{&dummyTxOut}, LockTime: 0, }, height: 300000, isStandard: true, }, { name: "Transaction version too high", tx: wire.MsgTx{ Version: wire.TxVersion + 1, TxIn: []*wire.TxIn{&dummyTxIn}, TxOut: []*wire.TxOut{&dummyTxOut}, LockTime: 0, }, height: 300000, isStandard: false, code: wire.RejectNonstandard, }, { name: "Transaction is not finalized", tx: wire.MsgTx{ Version: 1, TxIn: []*wire.TxIn{{ PreviousOutPoint: dummyPrevOut, SignatureScript: dummySigScript, Sequence: 0, }}, TxOut: []*wire.TxOut{&dummyTxOut}, LockTime: 300001, }, height: 300000, isStandard: false, code: wire.RejectNonstandard, }, { name: "Transaction size is too large", tx: wire.MsgTx{ Version: 1, TxIn: []*wire.TxIn{&dummyTxIn}, TxOut: []*wire.TxOut{{ Value: 0, PkScript: bytes.Repeat([]byte{0x00}, maxStandardTxSize+1), }}, LockTime: 0, }, height: 300000, isStandard: false, code: wire.RejectNonstandard, }, { name: "Signature script size is too large", tx: wire.MsgTx{ Version: 1, TxIn: []*wire.TxIn{{ PreviousOutPoint: dummyPrevOut, SignatureScript: bytes.Repeat([]byte{0x00}, maxStandardSigScriptSize+1), Sequence: wire.MaxTxInSequenceNum, }}, TxOut: []*wire.TxOut{&dummyTxOut}, LockTime: 0, }, height: 300000, isStandard: false, code: wire.RejectNonstandard, }, { name: "Signature script that does more than push data", tx: wire.MsgTx{ Version: 1, TxIn: []*wire.TxIn{{ PreviousOutPoint: dummyPrevOut, SignatureScript: []byte{ txscript.OP_CHECKSIGVERIFY}, Sequence: wire.MaxTxInSequenceNum, }}, TxOut: []*wire.TxOut{&dummyTxOut}, LockTime: 0, }, height: 300000, isStandard: false, code: wire.RejectNonstandard, }, { name: "Valid but non standard public key script", tx: wire.MsgTx{ Version: 1, TxIn: []*wire.TxIn{&dummyTxIn}, TxOut: []*wire.TxOut{{ Value: 100000000, PkScript: []byte{txscript.OP_TRUE}, }}, LockTime: 0, }, height: 300000, isStandard: false, code: wire.RejectNonstandard, }, { name: "More than one nulldata output", tx: wire.MsgTx{ Version: 1, TxIn: []*wire.TxIn{&dummyTxIn}, TxOut: []*wire.TxOut{{ Value: 0, PkScript: []byte{txscript.OP_RETURN}, }, { Value: 0, PkScript: []byte{txscript.OP_RETURN}, }}, LockTime: 0, }, height: 300000, isStandard: false, code: wire.RejectNonstandard, }, { name: "Dust output", tx: wire.MsgTx{ Version: 1, TxIn: []*wire.TxIn{&dummyTxIn}, TxOut: []*wire.TxOut{{ Value: 0, PkScript: dummyPkScript, }}, LockTime: 0, }, height: 300000, isStandard: false, code: wire.RejectDust, }, { name: "One nulldata output with 0 amount (standard)", tx: wire.MsgTx{ Version: 1, TxIn: []*wire.TxIn{&dummyTxIn}, TxOut: []*wire.TxOut{{ Value: 0, PkScript: []byte{txscript.OP_RETURN}, }}, LockTime: 0, }, height: 300000, isStandard: true, }, } timeSource := blockchain.NewMedianTime() for _, test := range tests { // Ensure standardness is as expected. err := checkTransactionStandard(coinutil.NewTx(&test.tx), test.height, timeSource, defaultMinRelayTxFee) if err == nil && test.isStandard { // Test passes since function returned standard for a // transaction which is intended to be standard. continue } if err == nil && !test.isStandard { t.Errorf("checkTransactionStandard (%s): standard when "+ "it should not be", test.name) continue } if err != nil && test.isStandard { t.Errorf("checkTransactionStandard (%s): nonstandard "+ "when it should not be: %v", test.name, err) continue } // Ensure error type is a TxRuleError inside of a RuleError. rerr, ok := err.(RuleError) if !ok { t.Errorf("checkTransactionStandard (%s): unexpected "+ "error type - got %T", test.name, err) continue } txrerr, ok := rerr.Err.(TxRuleError) if !ok { t.Errorf("checkTransactionStandard (%s): unexpected "+ "error type - got %T", test.name, rerr.Err) continue } // Ensure the reject code is the expected one. if txrerr.RejectCode != test.code { t.Errorf("checkTransactionStandard (%s): unexpected "+ "error code - got %v, want %v", test.name, txrerr.RejectCode, test.code) continue } } }
// TestTxValidTests ensures all of the tests in tx_valid.json pass as expected. func TestTxValidTests(t *testing.T) { file, err := ioutil.ReadFile("data/tx_valid.json") if err != nil { t.Errorf("TestBitcoindInvalidTests: %v\n", err) return } var tests [][]interface{} err = json.Unmarshal(file, &tests) if err != nil { t.Errorf("TestBitcoindInvalidTests couldn't Unmarshal: %v\n", err) return } // form is either: // ["this is a comment "] // or: // [[[previous hash, previous index, previous scriptPubKey]...,] // serializedTransaction, verifyFlags] testloop: for i, test := range tests { inputs, ok := test[0].([]interface{}) if !ok { continue } if len(test) != 3 { t.Errorf("bad test (bad length) %d: %v", i, test) continue } serializedhex, ok := test[1].(string) if !ok { t.Errorf("bad test (arg 2 not string) %d: %v", i, test) continue } serializedTx, err := hex.DecodeString(serializedhex) if err != nil { t.Errorf("bad test (arg 2 not hex %v) %d: %v", err, i, test) continue } tx, err := coinutil.NewTxFromBytes(serializedTx) if err != nil { t.Errorf("bad test (arg 2 not msgtx %v) %d: %v", err, i, test) continue } verifyFlags, ok := test[2].(string) if !ok { t.Errorf("bad test (arg 3 not string) %d: %v", i, test) continue } flags, err := parseScriptFlags(verifyFlags) if err != nil { t.Errorf("bad test %d: %v", i, err) continue } prevOuts := make(map[wire.OutPoint][]byte) for j, iinput := range inputs { input, ok := iinput.([]interface{}) if !ok { t.Errorf("bad test (%dth input not array)"+ "%d: %v", j, i, test) continue } if len(input) != 3 { t.Errorf("bad test (%dth input wrong length)"+ "%d: %v", j, i, test) continue } previoustx, ok := input[0].(string) if !ok { t.Errorf("bad test (%dth input sha not string)"+ "%d: %v", j, i, test) continue } prevhash, err := wire.NewShaHashFromStr(previoustx) if err != nil { t.Errorf("bad test (%dth input sha not sha %v)"+ "%d: %v", j, err, i, test) continue } idxf, ok := input[1].(float64) if !ok { t.Errorf("bad test (%dth input idx not number)"+ "%d: %v", j, i, test) continue } idx := uint32(idxf) // (floor(idxf) == idxf?) oscript, ok := input[2].(string) if !ok { t.Errorf("bad test (%dth input script not "+ "string) %d: %v", j, i, test) continue } script, err := parseShortForm(oscript) if err != nil { t.Errorf("bad test (%dth input script doesn't "+ "parse %v) %d: %v", j, err, i, test) continue } prevOuts[*wire.NewOutPoint(prevhash, idx)] = script } for k, txin := range tx.MsgTx().TxIn { pkScript, ok := prevOuts[txin.PreviousOutPoint] if !ok { t.Errorf("bad test (missing %dth input) %d:%v", k, i, test) continue testloop } vm, err := NewEngine(pkScript, tx.MsgTx(), k, flags, nil) if err != nil { t.Errorf("test (%d:%v:%d) failed to create "+ "script: %v", i, test, k, err) continue } err = vm.Execute() if err != nil { t.Errorf("test (%d:%v:%d) failed to execute: "+ "%v", i, test, k, err) continue } } } }
// TestOutboundPeer tests that the outbound peer works as expected. func TestOutboundPeer(t *testing.T) { // Use a mock NewestBlock func to test errs var errBlockNotFound = errors.New("newest block not found") var mockNewestSha = func() (*wire.ShaHash, int32, error) { return nil, 0, errBlockNotFound } peerCfg := &peer.Config{ NewestBlock: mockNewestSha, UserAgentName: "peer", UserAgentVersion: "1.0", ChainParams: &chaincfg.MainNetParams, Services: 0, } r, w := io.Pipe() c := &conn{raddr: "10.0.0.1:6682", Writer: w, Reader: r} p, err := peer.NewOutboundPeer(peerCfg, "10.0.0.1:6682") if err != nil { t.Errorf("NewOutboundPeer: unexpected err - %v\n", err) return } // Test Connect err wantErr := errBlockNotFound if err := p.Connect(c); err != wantErr { t.Errorf("Connect: expected err %v, got %v\n", wantErr, err) return } // Test already connected if err := p.Connect(c); err != nil { t.Errorf("Connect: unexpected err %v\n", err) return } // Test already started if err := p.Start(); err != nil { t.Errorf("Start: unexpected err %v\n", err) return } // Test Queue Inv fakeBlockHash := &wire.ShaHash{0x00, 0x01} fakeInv := wire.NewInvVect(wire.InvTypeBlock, fakeBlockHash) p.QueueInventory(fakeInv) p.AddKnownInventory(fakeInv) p.QueueInventory(fakeInv) // Test Queue Message fakeMsg := wire.NewMsgVerAck() p.QueueMessage(fakeMsg, nil) done := make(chan struct{}) p.QueueMessage(fakeMsg, done) <-done p.Shutdown() // Test NewestBlock var newestBlock = func() (*wire.ShaHash, int32, error) { hashStr := "14a0810ac680a3eb3f82edc878cea25ec41d6b790744e5daeef" hash, err := wire.NewShaHashFromStr(hashStr) if err != nil { return nil, 0, err } return hash, 234439, nil } peerCfg.NewestBlock = newestBlock r1, w1 := io.Pipe() c1 := &conn{raddr: "10.0.0.1:6682", Writer: w1, Reader: r1} p1, err := peer.NewOutboundPeer(peerCfg, "10.0.0.1:6682") if err != nil { t.Errorf("NewOutboundPeer: unexpected err - %v\n", err) return } if err := p1.Connect(c1); err != nil { t.Errorf("Connect: unexpected err %v\n", err) return } // Test update latest block latestBlockSha, err := wire.NewShaHashFromStr("1a63f9cdff1752e6375c8c76e543a71d239e1a2e5c6db1aa679") if err != nil { t.Errorf("NewShaHashFromStr: unexpected err %v\n", err) return } p1.UpdateLastAnnouncedBlock(latestBlockSha) p1.UpdateLastBlockHeight(234440) if p1.LastAnnouncedBlock() != latestBlockSha { t.Errorf("LastAnnouncedBlock: wrong block - got %v, want %v", p1.LastAnnouncedBlock(), latestBlockSha) return } // Test Queue Inv after connection p1.QueueInventory(fakeInv) p1.Shutdown() // Test regression peerCfg.ChainParams = &chaincfg.RegressionNetParams peerCfg.Services = wire.SFNodeBloom r2, w2 := io.Pipe() c2 := &conn{raddr: "10.0.0.1:6682", Writer: w2, Reader: r2} p2, err := peer.NewOutboundPeer(peerCfg, "10.0.0.1:6682") if err != nil { t.Errorf("NewOutboundPeer: unexpected err - %v\n", err) return } if err := p2.Connect(c2); err != nil { t.Errorf("Connect: unexpected err %v\n", err) return } // Test PushXXX var addrs []*wire.NetAddress for i := 0; i < 5; i++ { na := wire.NetAddress{} addrs = append(addrs, &na) } if _, err := p2.PushAddrMsg(addrs); err != nil { t.Errorf("PushAddrMsg: unexpected err %v\n", err) return } if err := p2.PushGetBlocksMsg(nil, &wire.ShaHash{}); err != nil { t.Errorf("PushGetBlocksMsg: unexpected err %v\n", err) return } if err := p2.PushGetHeadersMsg(nil, &wire.ShaHash{}); err != nil { t.Errorf("PushGetHeadersMsg: unexpected err %v\n", err) return } p2.PushRejectMsg("block", wire.RejectMalformed, "malformed", nil, true) p2.PushRejectMsg("block", wire.RejectInvalid, "invalid", nil, false) // Test Queue Messages p2.QueueMessage(wire.NewMsgGetAddr(), nil) p2.QueueMessage(wire.NewMsgPing(1), nil) p2.QueueMessage(wire.NewMsgMemPool(), nil) p2.QueueMessage(wire.NewMsgGetData(), nil) p2.QueueMessage(wire.NewMsgGetHeaders(), nil) p2.Shutdown() }
// TestGetBlocksWire tests the MsgGetBlocks wire encode and decode for various // numbers of block locator hashes and protocol versions. func TestGetBlocksWire(t *testing.T) { // Set protocol inside getblocks message. pver := uint32(60002) // Block 99499 hash. hashStr := "2710f40c87ec93d010a6fd95f42c59a2cbacc60b18cf6b7957535" hashLocator, err := wire.NewShaHashFromStr(hashStr) if err != nil { t.Errorf("NewShaHashFromStr: %v", err) } // Block 99500 hash. hashStr = "2e7ad7b9eef9479e4aabc65cb831269cc20d2632c13684406dee0" hashLocator2, err := wire.NewShaHashFromStr(hashStr) if err != nil { t.Errorf("NewShaHashFromStr: %v", err) } // Block 100000 hash. hashStr = "3ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506" hashStop, err := wire.NewShaHashFromStr(hashStr) if err != nil { t.Errorf("NewShaHashFromStr: %v", err) } // MsgGetBlocks message with no block locators or stop hash. noLocators := wire.NewMsgGetBlocks(&wire.ShaHash{}) noLocators.ProtocolVersion = pver noLocatorsEncoded := []byte{ 0x62, 0xea, 0x00, 0x00, // Protocol version 60002 0x00, // Varint for number of block locator hashes 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Hash stop } // MsgGetBlocks message with multiple block locators and a stop hash. multiLocators := wire.NewMsgGetBlocks(hashStop) multiLocators.AddBlockLocatorHash(hashLocator2) multiLocators.AddBlockLocatorHash(hashLocator) multiLocators.ProtocolVersion = pver multiLocatorsEncoded := []byte{ 0x62, 0xea, 0x00, 0x00, // Protocol version 60002 0x02, // Varint for number of block locator hashes 0xe0, 0xde, 0x06, 0x44, 0x68, 0x13, 0x2c, 0x63, 0xd2, 0x20, 0xcc, 0x69, 0x12, 0x83, 0xcb, 0x65, 0xbc, 0xaa, 0xe4, 0x79, 0x94, 0xef, 0x9e, 0x7b, 0xad, 0xe7, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, // Block 99500 hash 0x35, 0x75, 0x95, 0xb7, 0xf6, 0x8c, 0xb1, 0x60, 0xcc, 0xba, 0x2c, 0x9a, 0xc5, 0x42, 0x5f, 0xd9, 0x6f, 0x0a, 0x01, 0x3d, 0xc9, 0x7e, 0xc8, 0x40, 0x0f, 0x71, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, // Block 99499 hash 0x06, 0xe5, 0x33, 0xfd, 0x1a, 0xda, 0x86, 0x39, 0x1f, 0x3f, 0x6c, 0x34, 0x32, 0x04, 0xb0, 0xd2, 0x78, 0xd4, 0xaa, 0xec, 0x1c, 0x0b, 0x20, 0xaa, 0x27, 0xba, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, // Hash stop } tests := []struct { in *wire.MsgGetBlocks // Message to encode out *wire.MsgGetBlocks // Expected decoded message buf []byte // Wire encoding pver uint32 // Protocol version for wire encoding }{ // Latest protocol version with no block locators. { noLocators, noLocators, noLocatorsEncoded, wire.ProtocolVersion, }, // Latest protocol version with multiple block locators. { multiLocators, multiLocators, multiLocatorsEncoded, wire.ProtocolVersion, }, // Protocol version BIP0035Version with no block locators. { noLocators, noLocators, noLocatorsEncoded, wire.BIP0035Version, }, // Protocol version BIP0035Version with multiple block locators. { multiLocators, multiLocators, multiLocatorsEncoded, wire.BIP0035Version, }, // Protocol version BIP0031Version with no block locators. { noLocators, noLocators, noLocatorsEncoded, wire.BIP0031Version, }, // Protocol version BIP0031Versionwith multiple block locators. { multiLocators, multiLocators, multiLocatorsEncoded, wire.BIP0031Version, }, // Protocol version NetAddressTimeVersion with no block locators. { noLocators, noLocators, noLocatorsEncoded, wire.NetAddressTimeVersion, }, // Protocol version NetAddressTimeVersion multiple block locators. { multiLocators, multiLocators, multiLocatorsEncoded, wire.NetAddressTimeVersion, }, // Protocol version MultipleAddressVersion with no block locators. { noLocators, noLocators, noLocatorsEncoded, wire.MultipleAddressVersion, }, // Protocol version MultipleAddressVersion multiple block locators. { multiLocators, multiLocators, multiLocatorsEncoded, wire.MultipleAddressVersion, }, } t.Logf("Running %d tests", len(tests)) for i, test := range tests { // Encode the message to wire format. var buf bytes.Buffer err := test.in.BtcEncode(&buf, test.pver) if err != nil { t.Errorf("BtcEncode #%d error %v", i, err) continue } if !bytes.Equal(buf.Bytes(), test.buf) { t.Errorf("BtcEncode #%d\n got: %s want: %s", i, spew.Sdump(buf.Bytes()), spew.Sdump(test.buf)) continue } // Decode the message from wire format. var msg wire.MsgGetBlocks rbuf := bytes.NewReader(test.buf) err = msg.BtcDecode(rbuf, test.pver) if err != nil { t.Errorf("BtcDecode #%d error %v", i, err) continue } if !reflect.DeepEqual(&msg, test.out) { t.Errorf("BtcDecode #%d\n got: %s want: %s", i, spew.Sdump(&msg), spew.Sdump(test.out)) continue } } }
// TestGetDataWireErrors performs negative tests against wire encode and decode // of MsgGetData to confirm error paths work correctly. func TestGetDataWireErrors(t *testing.T) { pver := wire.ProtocolVersion wireErr := &wire.MessageError{} // Block 203707 hash. hashStr := "3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc" blockHash, err := wire.NewShaHashFromStr(hashStr) if err != nil { t.Errorf("NewShaHashFromStr: %v", err) } iv := wire.NewInvVect(wire.InvTypeBlock, blockHash) // Base message used to induce errors. baseGetData := wire.NewMsgGetData() baseGetData.AddInvVect(iv) baseGetDataEncoded := []byte{ 0x02, // Varint for number of inv vectors 0x02, 0x00, 0x00, 0x00, // InvTypeBlock 0xdc, 0xe9, 0x69, 0x10, 0x94, 0xda, 0x23, 0xc7, 0xe7, 0x67, 0x13, 0xd0, 0x75, 0xd4, 0xa1, 0x0b, 0x79, 0x40, 0x08, 0xa6, 0x36, 0xac, 0xc2, 0x4b, 0x26, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Block 203707 hash } // Message that forces an error by having more than the max allowed inv // vectors. maxGetData := wire.NewMsgGetData() for i := 0; i < wire.MaxInvPerMsg; i++ { maxGetData.AddInvVect(iv) } maxGetData.InvList = append(maxGetData.InvList, iv) maxGetDataEncoded := []byte{ 0xfd, 0x51, 0xc3, // Varint for number of inv vectors (50001) } tests := []struct { in *wire.MsgGetData // Value to encode buf []byte // Wire encoding pver uint32 // Protocol version for wire encoding max int // Max size of fixed buffer to induce errors writeErr error // Expected write error readErr error // Expected read error }{ // Latest protocol version with intentional read/write errors. // Force error in inventory vector count {baseGetData, baseGetDataEncoded, pver, 0, io.ErrShortWrite, io.EOF}, // Force error in inventory list. {baseGetData, baseGetDataEncoded, pver, 1, io.ErrShortWrite, io.EOF}, // Force error with greater than max inventory vectors. {maxGetData, maxGetDataEncoded, pver, 3, wireErr, wireErr}, } t.Logf("Running %d tests", len(tests)) for i, test := range tests { // Encode to wire format. w := newFixedWriter(test.max) err := test.in.BtcEncode(w, test.pver) if reflect.TypeOf(err) != reflect.TypeOf(test.writeErr) { t.Errorf("BtcEncode #%d wrong error got: %v, want: %v", i, err, test.writeErr) continue } // For errors which are not of type wire.MessageError, check // them for equality. if _, ok := err.(*wire.MessageError); !ok { if err != test.writeErr { t.Errorf("BtcEncode #%d wrong error got: %v, "+ "want: %v", i, err, test.writeErr) continue } } // Decode from wire format. var msg wire.MsgGetData r := newFixedReader(test.max, test.buf) err = msg.BtcDecode(r, test.pver) if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) { t.Errorf("BtcDecode #%d wrong error got: %v, want: %v", i, err, test.readErr) continue } // For errors which are not of type wire.MessageError, check // them for equality. if _, ok := err.(*wire.MessageError); !ok { if err != test.readErr { t.Errorf("BtcDecode #%d wrong error got: %v, "+ "want: %v", i, err, test.readErr) continue } } } }
// TestGetDataWire tests the MsgGetData wire encode and decode for various // numbers of inventory vectors and protocol versions. func TestGetDataWire(t *testing.T) { // Block 203707 hash. hashStr := "3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc" blockHash, err := wire.NewShaHashFromStr(hashStr) if err != nil { t.Errorf("NewShaHashFromStr: %v", err) } // Transation 1 of Block 203707 hash. hashStr = "d28a3dc7392bf00a9855ee93dd9a81eff82a2c4fe57fbd42cfe71b487accfaf0" txHash, err := wire.NewShaHashFromStr(hashStr) if err != nil { t.Errorf("NewShaHashFromStr: %v", err) } iv := wire.NewInvVect(wire.InvTypeBlock, blockHash) iv2 := wire.NewInvVect(wire.InvTypeTx, txHash) // Empty MsgGetData message. NoInv := wire.NewMsgGetData() NoInvEncoded := []byte{ 0x00, // Varint for number of inventory vectors } // MsgGetData message with multiple inventory vectors. MultiInv := wire.NewMsgGetData() MultiInv.AddInvVect(iv) MultiInv.AddInvVect(iv2) MultiInvEncoded := []byte{ 0x02, // Varint for number of inv vectors 0x02, 0x00, 0x00, 0x00, // InvTypeBlock 0xdc, 0xe9, 0x69, 0x10, 0x94, 0xda, 0x23, 0xc7, 0xe7, 0x67, 0x13, 0xd0, 0x75, 0xd4, 0xa1, 0x0b, 0x79, 0x40, 0x08, 0xa6, 0x36, 0xac, 0xc2, 0x4b, 0x26, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Block 203707 hash 0x01, 0x00, 0x00, 0x00, // InvTypeTx 0xf0, 0xfa, 0xcc, 0x7a, 0x48, 0x1b, 0xe7, 0xcf, 0x42, 0xbd, 0x7f, 0xe5, 0x4f, 0x2c, 0x2a, 0xf8, 0xef, 0x81, 0x9a, 0xdd, 0x93, 0xee, 0x55, 0x98, 0x0a, 0xf0, 0x2b, 0x39, 0xc7, 0x3d, 0x8a, 0xd2, // Tx 1 of block 203707 hash } tests := []struct { in *wire.MsgGetData // Message to encode out *wire.MsgGetData // Expected decoded message buf []byte // Wire encoding pver uint32 // Protocol version for wire encoding }{ // Latest protocol version with no inv vectors. { NoInv, NoInv, NoInvEncoded, wire.ProtocolVersion, }, // Latest protocol version with multiple inv vectors. { MultiInv, MultiInv, MultiInvEncoded, wire.ProtocolVersion, }, // Protocol version BIP0035Version no inv vectors. { NoInv, NoInv, NoInvEncoded, wire.BIP0035Version, }, // Protocol version BIP0035Version with multiple inv vectors. { MultiInv, MultiInv, MultiInvEncoded, wire.BIP0035Version, }, // Protocol version BIP0031Version no inv vectors. { NoInv, NoInv, NoInvEncoded, wire.BIP0031Version, }, // Protocol version BIP0031Version with multiple inv vectors. { MultiInv, MultiInv, MultiInvEncoded, wire.BIP0031Version, }, // Protocol version NetAddressTimeVersion no inv vectors. { NoInv, NoInv, NoInvEncoded, wire.NetAddressTimeVersion, }, // Protocol version NetAddressTimeVersion with multiple inv vectors. { MultiInv, MultiInv, MultiInvEncoded, wire.NetAddressTimeVersion, }, // Protocol version MultipleAddressVersion no inv vectors. { NoInv, NoInv, NoInvEncoded, wire.MultipleAddressVersion, }, // Protocol version MultipleAddressVersion with multiple inv vectors. { MultiInv, MultiInv, MultiInvEncoded, wire.MultipleAddressVersion, }, } t.Logf("Running %d tests", len(tests)) for i, test := range tests { // Encode the message to wire format. var buf bytes.Buffer err := test.in.BtcEncode(&buf, test.pver) if err != nil { t.Errorf("BtcEncode #%d error %v", i, err) continue } if !bytes.Equal(buf.Bytes(), test.buf) { t.Errorf("BtcEncode #%d\n got: %s want: %s", i, spew.Sdump(buf.Bytes()), spew.Sdump(test.buf)) continue } // Decode the message from wire format. var msg wire.MsgGetData rbuf := bytes.NewReader(test.buf) err = msg.BtcDecode(rbuf, test.pver) if err != nil { t.Errorf("BtcDecode #%d error %v", i, err) continue } if !reflect.DeepEqual(&msg, test.out) { t.Errorf("BtcDecode #%d\n got: %s want: %s", i, spew.Sdump(msg), spew.Sdump(test.out)) continue } } }
func TestFilterInsertP2PubKeyOnly(t *testing.T) { blockStr := "0100000082bb869cf3a793432a66e826e05a6fc37469f8efb7421dc" + "880670100000000007f16c5962e8bd963659c793ce370d95f093bc7e367" + "117b3c30c1f8fdd0d9728776381b4d4c86041b554b85290701000000010" + "00000000000000000000000000000000000000000000000000000000000" + "0000ffffffff07044c86041b0136ffffffff0100f2052a0100000043410" + "4eaafc2314def4ca98ac970241bcab022b9c1e1f4ea423a20f134c876f2" + "c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce13ad1357231a22522" + "47d97a46a91ac000000000100000001bcad20a6a29827d1424f08989255" + "120bf7f3e9e3cdaaa6bb31b0737fe048724300000000494830450220356" + "e834b046cadc0f8ebb5a8a017b02de59c86305403dad52cd77b55af062e" + "a10221009253cd6c119d4729b77c978e1e2aa19f5ea6e0e52b3f16e32fa" + "608cd5bab753901ffffffff02008d380c010000001976a9142b4b8072ec" + "bba129b6453c63e129e643207249ca88ac0065cd1d000000001976a9141" + "b8dd13b994bcfc787b32aeadf58ccb3615cbd5488ac0000000001000000" + "03fdacf9b3eb077412e7a968d2e4f11b9a9dee312d666187ed77ee7d26a" + "f16cb0b000000008c493046022100ea1608e70911ca0de5af51ba57ad23" + "b9a51db8d28f82c53563c56a05c20f5a87022100a8bdc8b4a8acc8634c6" + "b420410150775eb7f2474f5615f7fccd65af30f310fbf01410465fdf49e" + "29b06b9a1582287b6279014f834edc317695d125ef623c1cc3aaece245b" + "d69fcad7508666e9c74a49dc9056d5fc14338ef38118dc4afae5fe2c585" + "caffffffff309e1913634ecb50f3c4f83e96e70b2df071b497b8973a3e7" + "5429df397b5af83000000004948304502202bdb79c596a9ffc24e96f438" + "6199aba386e9bc7b6071516e2b51dda942b3a1ed022100c53a857e76b72" + "4fc14d45311eac5019650d415c3abb5428f3aae16d8e69bec2301ffffff" + "ff2089e33491695080c9edc18a428f7d834db5b6d372df13ce2b1b0e0cb" + "cb1e6c10000000049483045022100d4ce67c5896ee251c810ac1ff9cecc" + "d328b497c8f553ab6e08431e7d40bad6b5022033119c0c2b7d792d31f11" + "87779c7bd95aefd93d90a715586d73801d9b47471c601ffffffff010071" + "4460030000001976a914c7b55141d097ea5df7a0ed330cf794376e53ec8" + "d88ac0000000001000000045bf0e214aa4069a3e792ecee1e1bf0c1d397" + "cde8dd08138f4b72a00681743447000000008b48304502200c45de8c4f3" + "e2c1821f2fc878cba97b1e6f8807d94930713aa1c86a67b9bf1e4022100" + "8581abfef2e30f957815fc89978423746b2086375ca8ecf359c85c2a5b7" + "c88ad01410462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf64852" + "61c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d" + "3ae37079b794a92d7ec95ffffffffd669f7d7958d40fc59d2253d88e0f2" + "48e29b599c80bbcec344a83dda5f9aa72c000000008a473044022078124" + "c8beeaa825f9e0b30bff96e564dd859432f2d0cb3b72d3d5d93d38d7e93" + "0220691d233b6c0f995be5acb03d70a7f7a65b6bc9bdd426260f38a1346" + "669507a3601410462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6" + "485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d87270e" + "fb1d3ae37079b794a92d7ec95fffffffff878af0d93f5229a68166cf051" + "fd372bb7a537232946e0a46f53636b4dafdaa4000000008c49304602210" + "0c717d1714551663f69c3c5759bdbb3a0fcd3fab023abc0e522fe6440de" + "35d8290221008d9cbe25bffc44af2b18e81c58eb37293fd7fe1c2e7b46f" + "c37ee8c96c50ab1e201410462bb73f76ca0994fcb8b4271e6fb7561f5c0" + "f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f" + "4d87270efb1d3ae37079b794a92d7ec95ffffffff27f2b668859cd7f2f8" + "94aa0fd2d9e60963bcd07c88973f425f999b8cbfd7a1e2000000008c493" + "046022100e00847147cbf517bcc2f502f3ddc6d284358d102ed20d47a8a" + "a788a62f0db780022100d17b2d6fa84dcaf1c95d88d7e7c30385aecf415" + "588d749afd3ec81f6022cecd701410462bb73f76ca0994fcb8b4271e6fb" + "7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018" + "ffd6238f4d87270efb1d3ae37079b794a92d7ec95ffffffff0100c817a8" + "040000001976a914b6efd80d99179f4f4ff6f4dd0a007d018c385d2188a" + "c000000000100000001834537b2f1ce8ef9373a258e10545ce5a50b758d" + "f616cd4356e0032554ebd3c4000000008b483045022100e68f422dd7c34" + "fdce11eeb4509ddae38201773dd62f284e8aa9d96f85099d0b002202243" + "bd399ff96b649a0fad05fa759d6a882f0af8c90cf7632c2840c29070aec" + "20141045e58067e815c2f464c6a2a15f987758374203895710c2d452442" + "e28496ff38ba8f5fd901dc20e29e88477167fe4fc299bf818fd0d9e1632" + "d467b2a3d9503b1aaffffffff0280d7e636030000001976a914f34c3e10" + "eb387efe872acb614c89e78bfca7815d88ac404b4c00000000001976a91" + "4a84e272933aaf87e1715d7786c51dfaeb5b65a6f88ac00000000010000" + "000143ac81c8e6f6ef307dfe17f3d906d999e23e0189fda838c5510d850" + "927e03ae7000000008c4930460221009c87c344760a64cb8ae6685a3eec" + "2c1ac1bed5b88c87de51acd0e124f266c16602210082d07c037359c3a25" + "7b5c63ebd90f5a5edf97b2ac1c434b08ca998839f346dd40141040ba7e5" + "21fa7946d12edbb1d1e95a15c34bd4398195e86433c92b431cd315f455f" + "e30032ede69cad9d1e1ed6c3c4ec0dbfced53438c625462afb792dcb098" + "544bffffffff0240420f00000000001976a9144676d1b820d63ec272f19" + "00d59d43bc6463d96f888ac40420f00000000001976a914648d04341d00" + "d7968b3405c034adc38d4d8fb9bd88ac00000000010000000248cc91750" + "1ea5c55f4a8d2009c0567c40cfe037c2e71af017d0a452ff705e3f10000" + "00008b483045022100bf5fdc86dc5f08a5d5c8e43a8c9d5b1ed8c65562e" + "280007b52b133021acd9acc02205e325d613e555f772802bf413d36ba80" + "7892ed1a690a77811d3033b3de226e0a01410429fa713b124484cb2bd7b" + "5557b2c0b9df7b2b1fee61825eadc5ae6c37a9920d38bfccdc7dc3cb0c4" + "7d7b173dbc9db8d37db0a33ae487982c59c6f8606e9d1791ffffffff41e" + "d70551dd7e841883ab8f0b16bf04176b7d1480e4f0af9f3d4c3595768d0" + "68000000008b4830450221008513ad65187b903aed1102d1d0c47688127" + "658c51106753fed0151ce9c16b80902201432b9ebcb87bd04ceb2de6603" + "5fbbaf4bf8b00d1cfe41f1a1f7338f9ad79d210141049d4cf80125bf50b" + "e1709f718c07ad15d0fc612b7da1f5570dddc35f2a352f0f27c978b0682" + "0edca9ef982c35fda2d255afba340068c5035552368bc7200c1488fffff" + "fff0100093d00000000001976a9148edb68822f1ad580b043c7b3df2e40" + "0f8699eb4888ac00000000" blockBytes, err := hex.DecodeString(blockStr) if err != nil { t.Errorf("TestFilterInsertP2PubKeyOnly DecodeString failed: %v", err) return } block, err := coinutil.NewBlockFromBytes(blockBytes) if err != nil { t.Errorf("TestFilterInsertP2PubKeyOnly NewBlockFromBytes failed: %v", err) return } f := bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateP2PubkeyOnly) // Generation pubkey inputStr := "04eaafc2314def4ca98ac970241bcab022b9c1e1f4ea423a20f134c" + "876f2c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce13ad1357231a" + "2252247d97a46a91" inputBytes, err := hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterInsertP2PubKeyOnly DecodeString failed: %v", err) return } f.Add(inputBytes) // Output address of 4th transaction inputStr = "b6efd80d99179f4f4ff6f4dd0a007d018c385d21" inputBytes, err = hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterInsertP2PubKeyOnly DecodeString failed: %v", err) return } f.Add(inputBytes) // Ignore return value -- this is just used to update the filter. _, _ = bloom.NewMerkleBlock(block, f) // We should match the generation pubkey inputStr = "147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b" sha, err := wire.NewShaHashFromStr(inputStr) if err != nil { t.Errorf("TestMerkleBlockP2PubKeyOnly NewShaHashFromStr failed: %v", err) return } outpoint := wire.NewOutPoint(sha, 0) if !f.MatchesOutPoint(outpoint) { t.Errorf("TestMerkleBlockP2PubKeyOnly didn't match the generation "+ "outpoint %s", inputStr) return } // We should not match the 4th transaction, which is not p2pk inputStr = "02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041" sha, err = wire.NewShaHashFromStr(inputStr) if err != nil { t.Errorf("TestMerkleBlockP2PubKeyOnly NewShaHashFromStr failed: %v", err) return } outpoint = wire.NewOutPoint(sha, 0) if f.MatchesOutPoint(outpoint) { t.Errorf("TestMerkleBlockP2PubKeyOnly matched outpoint %s", inputStr) return } }
// TestTx tests the MsgTx API. func TestTx(t *testing.T) { pver := wire.ProtocolVersion // Block 100000 hash. hashStr := "3ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506" hash, err := wire.NewShaHashFromStr(hashStr) if err != nil { t.Errorf("NewShaHashFromStr: %v", err) } // Ensure the command is expected value. wantCmd := "tx" msg := wire.NewMsgTx() if cmd := msg.Command(); cmd != wantCmd { t.Errorf("NewMsgAddr: wrong command - got %v want %v", cmd, wantCmd) } // Ensure max payload is expected value for latest protocol version. // Num addresses (varInt) + max allowed addresses. wantPayload := uint32(1000 * 1000) maxPayload := msg.MaxPayloadLength(pver) if maxPayload != wantPayload { t.Errorf("MaxPayloadLength: wrong max payload length for "+ "protocol version %d - got %v, want %v", pver, maxPayload, wantPayload) } // Ensure we get the same transaction output point data back out. // NOTE: This is a block hash and made up index, but we're only // testing package functionality. prevOutIndex := uint32(1) prevOut := wire.NewOutPoint(hash, prevOutIndex) if !prevOut.Hash.IsEqual(hash) { t.Errorf("NewOutPoint: wrong hash - got %v, want %v", spew.Sprint(&prevOut.Hash), spew.Sprint(hash)) } if prevOut.Index != prevOutIndex { t.Errorf("NewOutPoint: wrong index - got %v, want %v", prevOut.Index, prevOutIndex) } prevOutStr := fmt.Sprintf("%s:%d", hash.String(), prevOutIndex) if s := prevOut.String(); s != prevOutStr { t.Errorf("OutPoint.String: unexpected result - got %v, "+ "want %v", s, prevOutStr) } // Ensure we get the same transaction input back out. sigScript := []byte{0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62} txIn := wire.NewTxIn(prevOut, sigScript) if !reflect.DeepEqual(&txIn.PreviousOutPoint, prevOut) { t.Errorf("NewTxIn: wrong prev outpoint - got %v, want %v", spew.Sprint(&txIn.PreviousOutPoint), spew.Sprint(prevOut)) } if !bytes.Equal(txIn.SignatureScript, sigScript) { t.Errorf("NewTxIn: wrong signature script - got %v, want %v", spew.Sdump(txIn.SignatureScript), spew.Sdump(sigScript)) } // Ensure we get the same transaction output back out. txValue := int64(5000000000) pkScript := []byte{ 0x41, // OP_DATA_65 0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5, 0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42, 0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1, 0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24, 0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97, 0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78, 0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20, 0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63, 0xa6, // 65-byte signature 0xac, // OP_CHECKSIG } txOut := wire.NewTxOut(txValue, pkScript) if txOut.Value != txValue { t.Errorf("NewTxOut: wrong pk script - got %v, want %v", txOut.Value, txValue) } if !bytes.Equal(txOut.PkScript, pkScript) { t.Errorf("NewTxOut: wrong pk script - got %v, want %v", spew.Sdump(txOut.PkScript), spew.Sdump(pkScript)) } // Ensure transaction inputs are added properly. msg.AddTxIn(txIn) if !reflect.DeepEqual(msg.TxIn[0], txIn) { t.Errorf("AddTxIn: wrong transaction input added - got %v, want %v", spew.Sprint(msg.TxIn[0]), spew.Sprint(txIn)) } // Ensure transaction outputs are added properly. msg.AddTxOut(txOut) if !reflect.DeepEqual(msg.TxOut[0], txOut) { t.Errorf("AddTxIn: wrong transaction output added - got %v, want %v", spew.Sprint(msg.TxOut[0]), spew.Sprint(txOut)) } // Ensure the copy produced an identical transaction message. newMsg := msg.Copy() if !reflect.DeepEqual(newMsg, msg) { t.Errorf("Copy: mismatched tx messages - got %v, want %v", spew.Sdump(newMsg), spew.Sdump(msg)) } return }
"time" "github.com/conseweb/coinutil" "github.com/conseweb/stcd/blockchain" "github.com/conseweb/stcd/wire" "github.com/conseweb/stcwallet/walletdb" _ "github.com/conseweb/stcwallet/walletdb/bdb" . "github.com/conseweb/stcwallet/wtxmgr" ) // Received transaction output for mainnet outpoint // 61d3696de4c888730cbe06b0ad8ecb6d72d6108e893895aa9bc067bd7eba3fad:0 var ( TstRecvSerializedTx, _ = hex.DecodeString("010000000114d9ff358894c486b4ae11c2a8cf7851b1df64c53d2e511278eff17c22fb7373000000008c493046022100995447baec31ee9f6d4ec0e05cb2a44f6b817a99d5f6de167d1c75354a946410022100c9ffc23b64d770b0e01e7ff4d25fbc2f1ca8091053078a247905c39fce3760b601410458b8e267add3c1e374cf40f1de02b59213a82e1d84c2b94096e22e2f09387009c96debe1d0bcb2356ffdcf65d2a83d4b34e72c62eccd8490dbf2110167783b2bffffffff0280969800000000001976a914479ed307831d0ac19ebc5f63de7d5f1a430ddb9d88ac38bfaa00000000001976a914dadf9e3484f28b385ddeaa6c575c0c0d18e9788a88ac00000000") TstRecvTx, _ = coinutil.NewTxFromBytes(TstRecvSerializedTx) TstRecvTxSpendingTxBlockHash, _ = wire.NewShaHashFromStr("00000000000000017188b968a371bab95aa43522665353b646e41865abae02a4") TstRecvAmt = int64(10000000) TstRecvTxBlockDetails = &BlockMeta{ Block: Block{Hash: *TstRecvTxSpendingTxBlockHash, Height: 276425}, Time: time.Unix(1387737310, 0), } TstRecvCurrentHeight = int32(284498) // mainnet blockchain height at time of writing TstRecvTxOutConfirms = 8074 // hardcoded number of confirmations given the above block height TstSpendingSerializedTx, _ = hex.DecodeString("0100000003ad3fba7ebd67c09baa9538898e10d6726dcb8eadb006be0c7388c8e46d69d361000000006b4830450220702c4fbde5532575fed44f8d6e8c3432a2a9bd8cff2f966c3a79b2245a7c88db02210095d6505a57e350720cb52b89a9b56243c15ddfcea0596aedc1ba55d9fb7d5aa0012103cccb5c48a699d3efcca6dae277fee6b82e0229ed754b742659c3acdfed2651f9ffffffffdbd36173f5610e34de5c00ed092174603761595d90190f790e79cda3e5b45bc2010000006b483045022000fa20735e5875e64d05bed43d81b867f3bd8745008d3ff4331ef1617eac7c44022100ad82261fc57faac67fc482a37b6bf18158da0971e300abf5fe2f9fd39e107f58012102d4e1caf3e022757512c204bf09ff56a9981df483aba3c74bb60d3612077c9206ffffffff65536c9d964b6f89b8ef17e83c6666641bc495cb27bab60052f76cd4556ccd0d040000006a473044022068e3886e0299ffa69a1c3ee40f8b6700f5f6d463a9cf9dbf22c055a131fc4abc02202b58957fe19ff1be7a84c458d08016c53fbddec7184ac5e633f2b282ae3420ae012103b4e411b81d32a69fb81178a8ea1abaa12f613336923ee920ffbb1b313af1f4d2ffffffff02ab233200000000001976a91418808b2fbd8d2c6d022aed5cd61f0ce6c0a4cbb688ac4741f011000000001976a914f081088a300c80ce36b717a9914ab5ec8a7d283988ac00000000") TstSpendingTx, _ = coinutil.NewTxFromBytes(TstSpendingSerializedTx) TstSpendingTxBlockHeight = int32(279143) TstSignedTxBlockHash, _ = wire.NewShaHashFromStr("00000000000000017188b968a371bab95aa43522665353b646e41865abae02a4") TstSignedTxBlockDetails = &BlockMeta{ Block: Block{Hash: *TstSignedTxBlockHash, Height: TstSpendingTxBlockHeight},
// TestShaHash tests the ShaHash API. func TestShaHash(t *testing.T) { // Hash of block 234439. blockHashStr := "14a0810ac680a3eb3f82edc878cea25ec41d6b790744e5daeef" blockHash, err := wire.NewShaHashFromStr(blockHashStr) if err != nil { t.Errorf("NewShaHashFromStr: %v", err) } // Hash of block 234440 as byte slice. buf := []byte{ 0x79, 0xa6, 0x1a, 0xdb, 0xc6, 0xe5, 0xa2, 0xe1, 0x39, 0xd2, 0x71, 0x3a, 0x54, 0x6e, 0xc7, 0xc8, 0x75, 0x63, 0x2e, 0x75, 0xf1, 0xdf, 0x9c, 0x3f, 0xa6, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, } hash, err := wire.NewShaHash(buf) if err != nil { t.Errorf("NewShaHash: unexpected error %v", err) } // Ensure proper size. if len(hash) != wire.HashSize { t.Errorf("NewShaHash: hash length mismatch - got: %v, want: %v", len(hash), wire.HashSize) } // Ensure contents match. if !bytes.Equal(hash[:], buf) { t.Errorf("NewShaHash: hash contents mismatch - got: %v, want: %v", hash[:], buf) } // Ensure contents of hash of block 234440 don't match 234439. if hash.IsEqual(blockHash) { t.Errorf("IsEqual: hash contents should not match - got: %v, want: %v", hash, blockHash) } // Set hash from byte slice and ensure contents match. err = hash.SetBytes(blockHash.Bytes()) if err != nil { t.Errorf("SetBytes: %v", err) } if !hash.IsEqual(blockHash) { t.Errorf("IsEqual: hash contents mismatch - got: %v, want: %v", hash, blockHash) } // Invalid size for SetBytes. err = hash.SetBytes([]byte{0x00}) if err == nil { t.Errorf("SetBytes: failed to received expected err - got: nil") } // Invalid size for NewShaHash. invalidHash := make([]byte, wire.HashSize+1) _, err = wire.NewShaHash(invalidHash) if err == nil { t.Errorf("NewShaHash: failed to received expected err - got: nil") } }
func Test_dupTx(t *testing.T) { // Ignore db remove errors since it means we didn't have an old one. dbname := fmt.Sprintf("tstdbdup0") dbnamever := dbname + ".ver" _ = os.RemoveAll(dbname) _ = os.RemoveAll(dbnamever) db, err := database.CreateDB("leveldb", dbname) if err != nil { t.Errorf("Failed to open test database %v", err) return } defer os.RemoveAll(dbname) defer os.RemoveAll(dbnamever) defer func() { if err := db.Close(); err != nil { t.Errorf("Close: unexpected error: %v", err) } }() testdatafile := filepath.Join("testdata", "blocks1-256.bz2") blocks, err := loadBlocks(t, testdatafile) if err != nil { t.Errorf("Unable to load blocks from test data for: %v", err) return } var lastSha *wire.ShaHash // Populate with the fisrt 256 blocks, so we have blocks to 'mess with' err = nil out: for height := int32(0); height < int32(len(blocks)); height++ { block := blocks[height] // except for NoVerify which does not allow lookups check inputs mblock := block.MsgBlock() var txneededList []*wire.ShaHash for _, tx := range mblock.Transactions { for _, txin := range tx.TxIn { if txin.PreviousOutPoint.Index == uint32(4294967295) { continue } origintxsha := &txin.PreviousOutPoint.Hash txneededList = append(txneededList, origintxsha) exists, err := db.ExistsTxSha(origintxsha) if err != nil { t.Errorf("ExistsTxSha: unexpected error %v ", err) } if !exists { t.Errorf("referenced tx not found %v ", origintxsha) } _, err = db.FetchTxBySha(origintxsha) if err != nil { t.Errorf("referenced tx not found %v err %v ", origintxsha, err) } } } txlist := db.FetchUnSpentTxByShaList(txneededList) for _, txe := range txlist { if txe.Err != nil { t.Errorf("tx list fetch failed %v err %v ", txe.Sha, txe.Err) break out } } newheight, err := db.InsertBlock(block) if err != nil { t.Errorf("failed to insert block %v err %v", height, err) break out } if newheight != height { t.Errorf("height mismatch expect %v returned %v", height, newheight) break out } newSha, blkid, err := db.NewestSha() if err != nil { t.Errorf("failed to obtain latest sha %v %v", height, err) } if blkid != height { t.Errorf("height doe not match latest block height %v %v %v", blkid, height, err) } blkSha := block.Sha() if *newSha != *blkSha { t.Errorf("Newest block sha does not match freshly inserted one %v %v %v ", newSha, blkSha, err) } lastSha = blkSha } // generate a new block based on the last sha // these block are not verified, so there are a bunch of garbage fields // in the 'generated' block. var bh wire.BlockHeader bh.Version = 2 bh.PrevBlock = *lastSha // Bits, Nonce are not filled in mblk := wire.NewMsgBlock(&bh) hash, _ := wire.NewShaHashFromStr("df2b060fa2e5e9c8ed5eaf6a45c13753ec8c63282b2688322eba40cd98ea067a") po := wire.NewOutPoint(hash, 0) txI := wire.NewTxIn(po, []byte("garbage")) txO := wire.NewTxOut(50000000, []byte("garbageout")) var tx wire.MsgTx tx.AddTxIn(txI) tx.AddTxOut(txO) mblk.AddTransaction(&tx) blk := coinutil.NewBlock(mblk) fetchList := []*wire.ShaHash{hash} listReply := db.FetchUnSpentTxByShaList(fetchList) for _, lr := range listReply { if lr.Err != nil { t.Errorf("sha %v spent %v err %v\n", lr.Sha, lr.TxSpent, lr.Err) } } _, err = db.InsertBlock(blk) if err != nil { t.Errorf("failed to insert phony block %v", err) } // ok, did it 'spend' the tx ? listReply = db.FetchUnSpentTxByShaList(fetchList) for _, lr := range listReply { if lr.Err != database.ErrTxShaMissing { t.Errorf("sha %v spent %v err %v\n", lr.Sha, lr.TxSpent, lr.Err) } } txlist := blk.Transactions() for _, tx := range txlist { txsha := tx.Sha() txReply, err := db.FetchTxBySha(txsha) if err != nil { t.Errorf("fully spent lookup %v err %v\n", hash, err) } else { for _, lr := range txReply { if lr.Err != nil { t.Errorf("stx %v spent %v err %v\n", lr.Sha, lr.TxSpent, lr.Err) } } } } t.Logf("Dropping block") err = db.DropAfterBlockBySha(lastSha) if err != nil { t.Errorf("failed to drop spending block %v", err) } }
// TestInvVectWire tests the InvVect wire encode and decode for various // protocol versions and supported inventory vector types. func TestInvVectWire(t *testing.T) { // Block 203707 hash. hashStr := "3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc" baseHash, err := wire.NewShaHashFromStr(hashStr) if err != nil { t.Errorf("NewShaHashFromStr: %v", err) } // errInvVect is an inventory vector with an error. errInvVect := wire.InvVect{ Type: wire.InvTypeError, Hash: wire.ShaHash{}, } // errInvVectEncoded is the wire encoded bytes of errInvVect. errInvVectEncoded := []byte{ 0x00, 0x00, 0x00, 0x00, // InvTypeError 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // No hash } // txInvVect is an inventory vector representing a transaction. txInvVect := wire.InvVect{ Type: wire.InvTypeTx, Hash: *baseHash, } // txInvVectEncoded is the wire encoded bytes of txInvVect. txInvVectEncoded := []byte{ 0x01, 0x00, 0x00, 0x00, // InvTypeTx 0xdc, 0xe9, 0x69, 0x10, 0x94, 0xda, 0x23, 0xc7, 0xe7, 0x67, 0x13, 0xd0, 0x75, 0xd4, 0xa1, 0x0b, 0x79, 0x40, 0x08, 0xa6, 0x36, 0xac, 0xc2, 0x4b, 0x26, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Block 203707 hash } // blockInvVect is an inventory vector representing a block. blockInvVect := wire.InvVect{ Type: wire.InvTypeBlock, Hash: *baseHash, } // blockInvVectEncoded is the wire encoded bytes of blockInvVect. blockInvVectEncoded := []byte{ 0x02, 0x00, 0x00, 0x00, // InvTypeBlock 0xdc, 0xe9, 0x69, 0x10, 0x94, 0xda, 0x23, 0xc7, 0xe7, 0x67, 0x13, 0xd0, 0x75, 0xd4, 0xa1, 0x0b, 0x79, 0x40, 0x08, 0xa6, 0x36, 0xac, 0xc2, 0x4b, 0x26, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Block 203707 hash } tests := []struct { in wire.InvVect // NetAddress to encode out wire.InvVect // Expected decoded NetAddress buf []byte // Wire encoding pver uint32 // Protocol version for wire encoding }{ // Latest protocol version error inventory vector. { errInvVect, errInvVect, errInvVectEncoded, wire.ProtocolVersion, }, // Latest protocol version tx inventory vector. { txInvVect, txInvVect, txInvVectEncoded, wire.ProtocolVersion, }, // Latest protocol version block inventory vector. { blockInvVect, blockInvVect, blockInvVectEncoded, wire.ProtocolVersion, }, // Protocol version BIP0035Version error inventory vector. { errInvVect, errInvVect, errInvVectEncoded, wire.BIP0035Version, }, // Protocol version BIP0035Version tx inventory vector. { txInvVect, txInvVect, txInvVectEncoded, wire.BIP0035Version, }, // Protocol version BIP0035Version block inventory vector. { blockInvVect, blockInvVect, blockInvVectEncoded, wire.BIP0035Version, }, // Protocol version BIP0031Version error inventory vector. { errInvVect, errInvVect, errInvVectEncoded, wire.BIP0031Version, }, // Protocol version BIP0031Version tx inventory vector. { txInvVect, txInvVect, txInvVectEncoded, wire.BIP0031Version, }, // Protocol version BIP0031Version block inventory vector. { blockInvVect, blockInvVect, blockInvVectEncoded, wire.BIP0031Version, }, // Protocol version NetAddressTimeVersion error inventory vector. { errInvVect, errInvVect, errInvVectEncoded, wire.NetAddressTimeVersion, }, // Protocol version NetAddressTimeVersion tx inventory vector. { txInvVect, txInvVect, txInvVectEncoded, wire.NetAddressTimeVersion, }, // Protocol version NetAddressTimeVersion block inventory vector. { blockInvVect, blockInvVect, blockInvVectEncoded, wire.NetAddressTimeVersion, }, // Protocol version MultipleAddressVersion error inventory vector. { errInvVect, errInvVect, errInvVectEncoded, wire.MultipleAddressVersion, }, // Protocol version MultipleAddressVersion tx inventory vector. { txInvVect, txInvVect, txInvVectEncoded, wire.MultipleAddressVersion, }, // Protocol version MultipleAddressVersion block inventory vector. { blockInvVect, blockInvVect, blockInvVectEncoded, wire.MultipleAddressVersion, }, } t.Logf("Running %d tests", len(tests)) for i, test := range tests { // Encode to wire format. var buf bytes.Buffer err := wire.TstWriteInvVect(&buf, test.pver, &test.in) if err != nil { t.Errorf("writeInvVect #%d error %v", i, err) continue } if !bytes.Equal(buf.Bytes(), test.buf) { t.Errorf("writeInvVect #%d\n got: %s want: %s", i, spew.Sdump(buf.Bytes()), spew.Sdump(test.buf)) continue } // Decode the message from wire format. var iv wire.InvVect rbuf := bytes.NewReader(test.buf) err = wire.TstReadInvVect(rbuf, test.pver, &iv) if err != nil { t.Errorf("readInvVect #%d error %v", i, err) continue } if !reflect.DeepEqual(iv, test.out) { t.Errorf("readInvVect #%d\n got: %s want: %s", i, spew.Sdump(iv), spew.Sdump(test.out)) continue } } }
// InsertBlock inserts raw block and transaction data from a block into the // database. The first block inserted into the database will be treated as the // genesis block. Every subsequent block insert requires the referenced parent // block to already exist. func (db *LevelDb) InsertBlock(block *coinutil.Block) (height int32, rerr error) { db.dbLock.Lock() defer db.dbLock.Unlock() defer func() { if rerr == nil { rerr = db.processBatches() } else { db.lBatch().Reset() } }() blocksha := block.Sha() mblock := block.MsgBlock() rawMsg, err := block.Bytes() if err != nil { log.Warnf("Failed to obtain raw block sha %v", blocksha) return 0, err } txloc, err := block.TxLoc() if err != nil { log.Warnf("Failed to obtain raw block sha %v", blocksha) return 0, err } // Insert block into database newheight, err := db.insertBlockData(blocksha, &mblock.Header.PrevBlock, rawMsg) if err != nil { log.Warnf("Failed to insert block %v %v %v", blocksha, &mblock.Header.PrevBlock, err) return 0, err } // At least two blocks in the long past were generated by faulty // miners, the sha of the transaction exists in a previous block, // detect this condition and 'accept' the block. for txidx, tx := range mblock.Transactions { txsha, err := block.TxSha(txidx) if err != nil { log.Warnf("failed to compute tx name block %v idx %v err %v", blocksha, txidx, err) return 0, err } spentbuflen := (len(tx.TxOut) + 7) / 8 spentbuf := make([]byte, spentbuflen, spentbuflen) if len(tx.TxOut)%8 != 0 { for i := uint(len(tx.TxOut) % 8); i < 8; i++ { spentbuf[spentbuflen-1] |= (byte(1) << i) } } err = db.insertTx(txsha, newheight, txloc[txidx].TxStart, txloc[txidx].TxLen, spentbuf) if err != nil { log.Warnf("block %v idx %v failed to insert tx %v %v err %v", blocksha, newheight, &txsha, txidx, err) return 0, err } // Some old blocks contain duplicate transactions // Attempt to cleanly bypass this problem by marking the // first as fully spent. // http://blockexplorer.com/b/91812 dup in 91842 // http://blockexplorer.com/b/91722 dup in 91880 if newheight == 91812 { dupsha, err := wire.NewShaHashFromStr("d5d27987d2a3dfc724e359870c6644b40e497bdc0589a033220fe15429d88599") if err != nil { panic("invalid sha string in source") } if txsha.IsEqual(dupsha) { // marking TxOut[0] as spent po := wire.NewOutPoint(dupsha, 0) txI := wire.NewTxIn(po, []byte("garbage")) var spendtx wire.MsgTx spendtx.AddTxIn(txI) err = db.doSpend(&spendtx) if err != nil { log.Warnf("block %v idx %v failed to spend tx %v %v err %v", blocksha, newheight, &txsha, txidx, err) } } } if newheight == 91722 { dupsha, err := wire.NewShaHashFromStr("e3bf3d07d4b0375638d5f1db5255fe07ba2c4cb067cd81b84ee974b6585fb468") if err != nil { panic("invalid sha string in source") } if txsha.IsEqual(dupsha) { // marking TxOut[0] as spent po := wire.NewOutPoint(dupsha, 0) txI := wire.NewTxIn(po, []byte("garbage")) var spendtx wire.MsgTx spendtx.AddTxIn(txI) err = db.doSpend(&spendtx) if err != nil { log.Warnf("block %v idx %v failed to spend tx %v %v err %v", blocksha, newheight, &txsha, txidx, err) } } } err = db.doSpend(tx) if err != nil { log.Warnf("block %v idx %v failed to spend tx %v %v err %v", blocksha, newheight, txsha, txidx, err) return 0, err } } return newheight, nil }
// TestBlock tests the API for Block. func TestBlock(t *testing.T) { b := coinutil.NewBlock(&Block100000) // Ensure we get the same data back out. if msgBlock := b.MsgBlock(); !reflect.DeepEqual(msgBlock, &Block100000) { t.Errorf("MsgBlock: mismatched MsgBlock - got %v, want %v", spew.Sdump(msgBlock), spew.Sdump(&Block100000)) } // Ensure block height set and get work properly. wantHeight := int32(100000) b.SetHeight(wantHeight) if gotHeight := b.Height(); gotHeight != wantHeight { t.Errorf("Height: mismatched height - got %v, want %v", gotHeight, wantHeight) } // Hash for block 100,000. wantShaStr := "3ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506" wantSha, err := wire.NewShaHashFromStr(wantShaStr) if err != nil { t.Errorf("NewShaHashFromStr: %v", err) } // Request the sha multiple times to test generation and caching. for i := 0; i < 2; i++ { sha := b.Sha() if !sha.IsEqual(wantSha) { t.Errorf("Sha #%d mismatched sha - got %v, want %v", i, sha, wantSha) } } // Shas for the transactions in Block100000. wantTxShas := []string{ "8c14f0db3df150123e6f3dbbf30f8b955a8249b62ac1d1ff16284aefa3d06d87", "fff2525b8931402dd09222c50775608f75787bd2b87e56995a7bdd30f79702c4", "6359f0868171b1d194cbee1af2f16ea598ae8fad666d9b012c8ed2b79a236ec4", "e9a66845e05d5abc0ad04ec80f774a7e585c6e8db975962d069a522137b80c1d", } // Create a new block to nuke all cached data. b = coinutil.NewBlock(&Block100000) // Request sha for all transactions one at a time via Tx. for i, txSha := range wantTxShas { wantSha, err := wire.NewShaHashFromStr(txSha) if err != nil { t.Errorf("NewShaHashFromStr: %v", err) } // Request the sha multiple times to test generation and caching. for j := 0; j < 2; j++ { tx, err := b.Tx(i) if err != nil { t.Errorf("Tx #%d: %v", i, err) continue } sha := tx.Sha() if !sha.IsEqual(wantSha) { t.Errorf("Sha #%d mismatched sha - got %v, "+ "want %v", j, sha, wantSha) continue } } } // Create a new block to nuke all cached data. b = coinutil.NewBlock(&Block100000) // Request slice of all transactions multiple times to test generation // and caching. for i := 0; i < 2; i++ { transactions := b.Transactions() // Ensure we get the expected number of transactions. if len(transactions) != len(wantTxShas) { t.Errorf("Transactions #%d mismatched number of "+ "transactions - got %d, want %d", i, len(transactions), len(wantTxShas)) continue } // Ensure all of the shas match. for j, tx := range transactions { wantSha, err := wire.NewShaHashFromStr(wantTxShas[j]) if err != nil { t.Errorf("NewShaHashFromStr: %v", err) } sha := tx.Sha() if !sha.IsEqual(wantSha) { t.Errorf("Transactions #%d mismatched shas - "+ "got %v, want %v", j, sha, wantSha) continue } } } // Serialize the test block. var block100000Buf bytes.Buffer err = Block100000.Serialize(&block100000Buf) if err != nil { t.Errorf("Serialize: %v", err) } block100000Bytes := block100000Buf.Bytes() // Request serialized bytes multiple times to test generation and // caching. for i := 0; i < 2; i++ { serializedBytes, err := b.Bytes() if err != nil { t.Errorf("Bytes: %v", err) continue } if !bytes.Equal(serializedBytes, block100000Bytes) { t.Errorf("Bytes #%d wrong bytes - got %v, want %v", i, spew.Sdump(serializedBytes), spew.Sdump(block100000Bytes)) continue } } // Transaction offsets and length for the transaction in Block100000. wantTxLocs := []wire.TxLoc{ {TxStart: 81, TxLen: 135}, {TxStart: 216, TxLen: 259}, {TxStart: 475, TxLen: 257}, {TxStart: 732, TxLen: 225}, } // Ensure the transaction location information is accurate. txLocs, err := b.TxLoc() if err != nil { t.Errorf("TxLoc: %v", err) return } if !reflect.DeepEqual(txLocs, wantTxLocs) { t.Errorf("TxLoc: mismatched transaction location information "+ "- got %v, want %v", spew.Sdump(txLocs), spew.Sdump(wantTxLocs)) } }
// TestGetBlocks tests the MsgGetBlocks API. func TestGetBlocks(t *testing.T) { pver := wire.ProtocolVersion // Block 99500 hash. hashStr := "000000000002e7ad7b9eef9479e4aabc65cb831269cc20d2632c13684406dee0" locatorHash, err := wire.NewShaHashFromStr(hashStr) if err != nil { t.Errorf("NewShaHashFromStr: %v", err) } // Block 100000 hash. hashStr = "3ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506" hashStop, err := wire.NewShaHashFromStr(hashStr) if err != nil { t.Errorf("NewShaHashFromStr: %v", err) } // Ensure we get the same data back out. msg := wire.NewMsgGetBlocks(hashStop) if !msg.HashStop.IsEqual(hashStop) { t.Errorf("NewMsgGetBlocks: wrong stop hash - got %v, want %v", msg.HashStop, hashStop) } // Ensure the command is expected value. wantCmd := "getblocks" if cmd := msg.Command(); cmd != wantCmd { t.Errorf("NewMsgGetBlocks: wrong command - got %v want %v", cmd, wantCmd) } // Ensure max payload is expected value for latest protocol version. // Protocol version 4 bytes + num hashes (varInt) + max block locator // hashes + hash stop. wantPayload := uint32(16045) maxPayload := msg.MaxPayloadLength(pver) if maxPayload != wantPayload { t.Errorf("MaxPayloadLength: wrong max payload length for "+ "protocol version %d - got %v, want %v", pver, maxPayload, wantPayload) } // Ensure block locator hashes are added properly. err = msg.AddBlockLocatorHash(locatorHash) if err != nil { t.Errorf("AddBlockLocatorHash: %v", err) } if msg.BlockLocatorHashes[0] != locatorHash { t.Errorf("AddBlockLocatorHash: wrong block locator added - "+ "got %v, want %v", spew.Sprint(msg.BlockLocatorHashes[0]), spew.Sprint(locatorHash)) } // Ensure adding more than the max allowed block locator hashes per // message returns an error. for i := 0; i < wire.MaxBlockLocatorsPerMsg; i++ { err = msg.AddBlockLocatorHash(locatorHash) } if err == nil { t.Errorf("AddBlockLocatorHash: expected error on too many " + "block locator hashes not received") } return }