void AttachedNode_MessageReceived(Node node, IncomingMessage message) { var filterload = message.Message.Payload as FilterLoadPayload; if (filterload != null) { _Filter = filterload.Object; } var getdata = message.Message.Payload as GetDataPayload; if (getdata != null) { foreach (var inv in getdata.Inventory) { if (inv.Type == InventoryType.MSG_FILTERED_BLOCK && _Filter != null) { var merkle = new MerkleBlock(_Blocks[inv.Hash], _Filter); AttachedNode.SendMessageAsync(new MerkleBlockPayload(merkle)); foreach (var tx in merkle.PartialMerkleTree.GetMatchedTransactions()) { if (_Known.TryAdd(tx, tx)) { AttachedNode.SendMessageAsync(new InvPayload(InventoryType.MSG_TX, tx)); } } } if (inv.Type == InventoryType.MSG_TX) { AttachedNode.SendMessageAsync(new TxPayload(_Transactions[inv.Hash])); } } } }
public void merkle_block_3_and_serialize() { // Random real block (000000000000dab0130bbcc991d3d7ae6b81aa6f50a798888dfe62337458dc45) // With one tx Block block = new Block(); block.ReadWrite(ParseHex("0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b4b8b0000000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f196367291b4d4c86041b8fa45d630101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff08044c86041b020a02ffffffff0100f2052a01000000434104ecd3229b0571c3be876feaac0442a9f13c5a572742927af1dc623353ecf8c202225f64868137a18cdd85cbbb4c74fbccfd4f49639cf1bdc94a5672bb15ad5d4cac00000000")); BloomFilter filter = new BloomFilter(10, 0.000001, 0, BloomFlags.UPDATE_ALL); // Match the only transaction filter.Insert(uint256.Parse("0x63194f18be0af63f2c6bc9dc0f777cbefed3d9415c4af83f3ee3a3d669c00cb5")); MerkleBlock merkleBlock = new MerkleBlock(block, filter); Assert.True(merkleBlock.Header.GetHash() == block.GetHash()); Assert.True(merkleBlock.PartialMerkleTree.Check(block.Header.HashMerkleRoot)); var vMatchedTxn = merkleBlock.PartialMerkleTree.GetMatchedTransactions().ToList(); Assert.True(vMatchedTxn.Count == 1); AssertMatch(block, vMatchedTxn, "0x63194f18be0af63f2c6bc9dc0f777cbefed3d9415c4af83f3ee3a3d669c00cb5", 0, 0); byte[] vch = ParseHex("0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b4b8b0000000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f196367291b4d4c86041b8fa45d630100000001b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f19630101"); AssertEx.CollectionEquals(merkleBlock.ToBytes(), vch); }
public ICollection <TransactionInformation> GetTransactions(Script scriptPubKey, bool withProof) { if (scriptPubKey == null) { throw new ArgumentNullException(nameof(scriptPubKey)); } var results = _Cache .GetEntriesFromScript(scriptPubKey) .Select(entry => new TransactionInformation() { Confirmations = entry.Confirmations, Transaction = entry.Transaction }).ToList(); if (withProof) { foreach (var tx in results.ToList()) { MerkleBlock proof = null; var result = RPCClient.SendCommandNoThrows("gettxoutproof", new JArray(tx.Transaction.GetHash().ToString())); if (result == null || result.Error != null) { results.Remove(tx); continue; } proof = new MerkleBlock(); proof.ReadWrite(Encoders.Hex.DecodeData(result.ResultString)); tx.MerkleProof = proof; } } return(results); }
public void Add(int height, Block block) { foreach (var spk in TrackedScriptPubKeys) { TrackIfFindRelatedTransactions(spk, height, block); } FullBlockBuffer.AddOrReplace(height, block); HashSet <uint256> notFoundTransactions = GetNotYetFoundTrackedTransactions(); HashSet <uint256> foundTransactions = new HashSet <uint256>(); foreach (var txid in notFoundTransactions) { if (block.Transactions.Any(x => x.GetHash().Equals(txid))) { foundTransactions.Add(txid); } } MerkleBlock merkleProof = foundTransactions.Count == 0 ? block.Filter() : block.Filter(foundTransactions.ToArray()); var partialBlock = new PartialBlock(height, merkleProof); foreach (var txid in foundTransactions) { foreach (var tx in block.Transactions) { if (tx.GetHash().Equals(txid)) { partialBlock.Transactions.Add(tx); } } } Chain.AddOrReplace(partialBlock.Height, partialBlock); }
public void merkle_block_4() { // Random real block (000000000000b731f2eef9e8c63173adfb07e41bd53eb0ef0a6b720d6cb6dea4) // With 7 txes Block block = new Block(); block.ReadWrite(ParseHex("0100000082bb869cf3a793432a66e826e05a6fc37469f8efb7421dc880670100000000007f16c5962e8bd963659c793ce370d95f093bc7e367117b3c30c1f8fdd0d9728776381b4d4c86041b554b85290701000000010000000000000000000000000000000000000000000000000000000000000000ffffffff07044c86041b0136ffffffff0100f2052a01000000434104eaafc2314def4ca98ac970241bcab022b9c1e1f4ea423a20f134c876f2c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce13ad1357231a2252247d97a46a91ac000000000100000001bcad20a6a29827d1424f08989255120bf7f3e9e3cdaaa6bb31b0737fe048724300000000494830450220356e834b046cadc0f8ebb5a8a017b02de59c86305403dad52cd77b55af062ea10221009253cd6c119d4729b77c978e1e2aa19f5ea6e0e52b3f16e32fa608cd5bab753901ffffffff02008d380c010000001976a9142b4b8072ecbba129b6453c63e129e643207249ca88ac0065cd1d000000001976a9141b8dd13b994bcfc787b32aeadf58ccb3615cbd5488ac000000000100000003fdacf9b3eb077412e7a968d2e4f11b9a9dee312d666187ed77ee7d26af16cb0b000000008c493046022100ea1608e70911ca0de5af51ba57ad23b9a51db8d28f82c53563c56a05c20f5a87022100a8bdc8b4a8acc8634c6b420410150775eb7f2474f5615f7fccd65af30f310fbf01410465fdf49e29b06b9a1582287b6279014f834edc317695d125ef623c1cc3aaece245bd69fcad7508666e9c74a49dc9056d5fc14338ef38118dc4afae5fe2c585caffffffff309e1913634ecb50f3c4f83e96e70b2df071b497b8973a3e75429df397b5af83000000004948304502202bdb79c596a9ffc24e96f4386199aba386e9bc7b6071516e2b51dda942b3a1ed022100c53a857e76b724fc14d45311eac5019650d415c3abb5428f3aae16d8e69bec2301ffffffff2089e33491695080c9edc18a428f7d834db5b6d372df13ce2b1b0e0cbcb1e6c10000000049483045022100d4ce67c5896ee251c810ac1ff9ceccd328b497c8f553ab6e08431e7d40bad6b5022033119c0c2b7d792d31f1187779c7bd95aefd93d90a715586d73801d9b47471c601ffffffff0100714460030000001976a914c7b55141d097ea5df7a0ed330cf794376e53ec8d88ac0000000001000000045bf0e214aa4069a3e792ecee1e1bf0c1d397cde8dd08138f4b72a00681743447000000008b48304502200c45de8c4f3e2c1821f2fc878cba97b1e6f8807d94930713aa1c86a67b9bf1e40221008581abfef2e30f957815fc89978423746b2086375ca8ecf359c85c2a5b7c88ad01410462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95ffffffffd669f7d7958d40fc59d2253d88e0f248e29b599c80bbcec344a83dda5f9aa72c000000008a473044022078124c8beeaa825f9e0b30bff96e564dd859432f2d0cb3b72d3d5d93d38d7e930220691d233b6c0f995be5acb03d70a7f7a65b6bc9bdd426260f38a1346669507a3601410462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95fffffffff878af0d93f5229a68166cf051fd372bb7a537232946e0a46f53636b4dafdaa4000000008c493046022100c717d1714551663f69c3c5759bdbb3a0fcd3fab023abc0e522fe6440de35d8290221008d9cbe25bffc44af2b18e81c58eb37293fd7fe1c2e7b46fc37ee8c96c50ab1e201410462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95ffffffff27f2b668859cd7f2f894aa0fd2d9e60963bcd07c88973f425f999b8cbfd7a1e2000000008c493046022100e00847147cbf517bcc2f502f3ddc6d284358d102ed20d47a8aa788a62f0db780022100d17b2d6fa84dcaf1c95d88d7e7c30385aecf415588d749afd3ec81f6022cecd701410462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95ffffffff0100c817a8040000001976a914b6efd80d99179f4f4ff6f4dd0a007d018c385d2188ac000000000100000001834537b2f1ce8ef9373a258e10545ce5a50b758df616cd4356e0032554ebd3c4000000008b483045022100e68f422dd7c34fdce11eeb4509ddae38201773dd62f284e8aa9d96f85099d0b002202243bd399ff96b649a0fad05fa759d6a882f0af8c90cf7632c2840c29070aec20141045e58067e815c2f464c6a2a15f987758374203895710c2d452442e28496ff38ba8f5fd901dc20e29e88477167fe4fc299bf818fd0d9e1632d467b2a3d9503b1aaffffffff0280d7e636030000001976a914f34c3e10eb387efe872acb614c89e78bfca7815d88ac404b4c00000000001976a914a84e272933aaf87e1715d7786c51dfaeb5b65a6f88ac00000000010000000143ac81c8e6f6ef307dfe17f3d906d999e23e0189fda838c5510d850927e03ae7000000008c4930460221009c87c344760a64cb8ae6685a3eec2c1ac1bed5b88c87de51acd0e124f266c16602210082d07c037359c3a257b5c63ebd90f5a5edf97b2ac1c434b08ca998839f346dd40141040ba7e521fa7946d12edbb1d1e95a15c34bd4398195e86433c92b431cd315f455fe30032ede69cad9d1e1ed6c3c4ec0dbfced53438c625462afb792dcb098544bffffffff0240420f00000000001976a9144676d1b820d63ec272f1900d59d43bc6463d96f888ac40420f00000000001976a914648d04341d00d7968b3405c034adc38d4d8fb9bd88ac00000000010000000248cc917501ea5c55f4a8d2009c0567c40cfe037c2e71af017d0a452ff705e3f1000000008b483045022100bf5fdc86dc5f08a5d5c8e43a8c9d5b1ed8c65562e280007b52b133021acd9acc02205e325d613e555f772802bf413d36ba807892ed1a690a77811d3033b3de226e0a01410429fa713b124484cb2bd7b5557b2c0b9df7b2b1fee61825eadc5ae6c37a9920d38bfccdc7dc3cb0c47d7b173dbc9db8d37db0a33ae487982c59c6f8606e9d1791ffffffff41ed70551dd7e841883ab8f0b16bf04176b7d1480e4f0af9f3d4c3595768d068000000008b4830450221008513ad65187b903aed1102d1d0c47688127658c51106753fed0151ce9c16b80902201432b9ebcb87bd04ceb2de66035fbbaf4bf8b00d1cfe41f1a1f7338f9ad79d210141049d4cf80125bf50be1709f718c07ad15d0fc612b7da1f5570dddc35f2a352f0f27c978b06820edca9ef982c35fda2d255afba340068c5035552368bc7200c1488ffffffff0100093d00000000001976a9148edb68822f1ad580b043c7b3df2e400f8699eb4888ac00000000")); BloomFilter filter = new BloomFilter(10, 0.000001, 0, BloomFlags.UPDATE_ALL); // Match the last transaction filter.Insert(uint256.Parse("0x0a2a92f0bda4727d0a13eaddf4dd9ac6b5c61a1429e6b2b818f19b15df0ac154")); MerkleBlock merkleBlock = new MerkleBlock(block, filter); Assert.True(merkleBlock.Header.GetHash() == block.GetHash()); var vMatchedTxn = merkleBlock.PartialMerkleTree.GetMatchedTransactions().ToList(); Assert.True(vMatchedTxn.Count == 1); AssertMatch(block, vMatchedTxn, "0x0a2a92f0bda4727d0a13eaddf4dd9ac6b5c61a1429e6b2b818f19b15df0ac154", 0, 6); // Also match the 4th transaction filter.Insert(uint256.Parse("0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041")); merkleBlock = new MerkleBlock(block, filter); Assert.True(merkleBlock.PartialMerkleTree.Check(block.Header.HashMerkleRoot)); Assert.True(merkleBlock.Header.GetHash() == block.GetHash()); vMatchedTxn = merkleBlock.PartialMerkleTree.GetMatchedTransactions().ToList(); Assert.True(vMatchedTxn.Count == 2); AssertMatch(block, vMatchedTxn, "0x0a2a92f0bda4727d0a13eaddf4dd9ac6b5c61a1429e6b2b818f19b15df0ac154", 1, 6); AssertMatch(block, vMatchedTxn, "0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041", 0, 3); }
private bool Notify(Transaction tx, MerkleBlock blk) { bool hit = false; if (blk == null) { hit = _Tracker.NotifyTransaction(tx); } else { var prev = _Chain.GetBlock(blk.Header.HashPrevBlock); if (prev != null) { var header = new ChainedBlock(blk.Header, null, prev); hit = _Tracker.NotifyTransaction(tx, header, blk); } else { hit = _Tracker.NotifyTransaction(tx); } } Interlocked.Increment(ref _TotalReceived); if (!hit) { Interlocked.Increment(ref _FalsePositiveCount); } return(hit); }
public async Task <ICollection <TransactionInformation> > GetTransactionsAsync(Script scriptPubKey, bool withProof) { if (scriptPubKey == null) { throw new ArgumentNullException(nameof(scriptPubKey)); } var results = _Cache .GetEntriesFromScript(scriptPubKey) .Select(entry => new TransactionInformation() { Confirmations = entry.Confirmations, Transaction = entry.Transaction }).ToList(); if (withProof) { foreach (var tx in results.ToList()) { var completion = new TaskCompletionSource <MerkleBlock>(); bool isRequester = true; var txid = tx.Transaction.GetHash(); _GettingProof.AddOrUpdate(txid, completion, (k, o) => { isRequester = false; completion = o; return(o); }); if (isRequester) { try { MerkleBlock proof = null; var result = await RPCClient.SendCommandNoThrowsAsync("gettxoutproof", new JArray(tx.Transaction.GetHash().ToString())).ConfigureAwait(false); if (result == null || result.Error != null) { completion.TrySetResult(null); continue; } proof = new MerkleBlock(); proof.ReadWrite(Encoders.Hex.DecodeData(result.ResultString)); tx.MerkleProof = proof; completion.TrySetResult(proof); } catch (Exception ex) { completion.TrySetException(ex); } finally { _GettingProof.TryRemove(txid, out completion); } } var merkleBlock = await completion.Task.ConfigureAwait(false); if (merkleBlock == null) { results.Remove(tx); } } } return(results); }
public TransactionInformation[] GetTransactions(Script scriptPubKey, bool withProof) { if (scriptPubKey == null) { throw new ArgumentNullException(nameof(scriptPubKey)); } var address = scriptPubKey.GetDestinationAddress(RPCClient.Network); if (address == null) { return(new TransactionInformation[0]); } var walletTransactions = _Cache.GetEntries(); List <TransactionInformation> results = Filter(walletTransactions, !withProof, address); if (withProof) { foreach (var tx in results.ToList()) { MerkleBlock proof = null; var result = RPCClient.SendCommandNoThrows("gettxoutproof", new JArray(tx.Transaction.GetHash().ToString())); if (result == null || result.Error != null) { results.Remove(tx); continue; } proof = new MerkleBlock(); proof.ReadWrite(Encoders.Hex.DecodeData(result.ResultString)); tx.MerkleProof = proof; } } return(results.ToArray()); }
private bool Notify(Transaction tx, MerkleBlock blk) { bool hit = false; if (blk == null) { hit = _Tracker.NotifyTransaction(tx); } else { var prev = _Chain.GetBlock(blk.Header.HashPrevBlock); if (prev != null) { var header = new ChainedBlock(blk.Header, null, prev); hit = _Tracker.NotifyTransaction(tx, header, blk); } else { hit = _Tracker.NotifyTransaction(tx); } } Interlocked.Increment(ref _TotalReceived); if (!hit) { Interlocked.Increment(ref _FalsePositiveCount); if (MaximumFalsePositiveRate != null && _TotalReceived > 100 && ActualFalsePostiveRate >= MaximumFalsePositiveRate.Value) { this.AttachedNode.DisconnectAsync("The actual false positive rate exceed MaximumFalsePositiveRate"); } } return(hit); }
private MerkleProof CreateMerkleProof(Block block, uint256 transactionHash) { MerkleBlock merkleBlock = new MerkleBlock(block, new[] { transactionHash }); return(new MerkleProof { MerkleRoot = block.Header.HashMerkleRoot, MerklePath = merkleBlock.PartialMerkleTree.Hashes }); }
public bool TrackPrunedTransaction(Transaction transaction, MerkleBlock merkleProof) { var result = RPCClient.SendCommandNoThrows("importprunedfunds", transaction.ToHex(), Encoders.Hex.EncodeData(merkleProof.ToBytes())); var success = result != null && result.Error == null; if (success) { _Cache.ImportTransaction(transaction, GetBlockConfirmations(merkleProof.Header.GetHash())); } return(success); }
public Task <MerkleBlock> GetTxOutProofAsync(string[] txids, string blockhash = "") { List <uint256> transactionIds = txids.Select(txString => uint256.Parse(txString)).ToList(); ChainedHeaderBlock block = null; if (!string.IsNullOrEmpty(blockhash)) { // We presume that all the transactions are supposed to be contained in the same specified block, so we only retrieve it once. uint256 hashBlock = uint256.Parse(blockhash); block = this.consensusManager?.GetBlockData(hashBlock); } else { if (!(this.storeSettings?.TxIndex ?? false)) { throw new RPCServerException(RPCErrorCode.RPC_INVALID_PARAMETER, "Cannot find block containing specified transactions if hash is not specified and -txindex is disabled"); } // Loop through txids and try to find which block they're in. Exit loop once a block is found, as they are supposed to all be in the same block. foreach (uint256 transactionId in transactionIds) { ChainedHeader chainedHeader = this.GetTransactionBlock(transactionId); if (chainedHeader?.BlockDataAvailability != null && chainedHeader.BlockDataAvailability == BlockDataAvailabilityState.BlockAvailable) { block = this.consensusManager?.GetBlockData(chainedHeader.HashBlock); break; } } } if (block == null) { throw new RPCServerException(RPCErrorCode.RPC_INVALID_PARAMETER, "Block not found"); } // Need to be able to lookup the transactions within the block efficiently. HashSet <uint256> transactionMap = block.Block.Transactions.Select(t => t.GetHash()).ToHashSet(); // Loop through txids and verify that every transaction is in the block. foreach (uint256 transactionId in transactionIds) { if (!transactionMap.Contains(transactionId)) { throw new RPCServerException(RPCErrorCode.RPC_INVALID_PARAMETER, "Not all transactions found in specified or retrieved block"); } } var result = new MerkleBlock(block.Block, transactionIds.ToArray()); return(Task.FromResult(result)); }
public async Task GetTxOutProof_TransactionInSameSpecifiedBlock_ReturnsProof() { ChainedHeader block = this.chain.GetHeader(2); Transaction tx = block.Block.Transactions.First(); this.consensusManager.Setup(b => b.GetBlockData(It.IsAny <uint256>())) .Returns(new ChainedHeaderBlock(block.Block, block)); MerkleBlock result = await this.controller.GetTxOutProofAsync(new [] { tx.GetHash().ToString() }, block.HashBlock.ToString()); Assert.NotNull(result); }
public bool TrackPrunedTransaction(Transaction transaction, MerkleBlock merkleProof) { var blockHash = this.tumblingState.blockStoreManager.BlockRepository?.GetTrxBlockIdAsync(transaction.GetHash()).Result; var chainBlock = this.tumblingState.chain.GetBlock(blockHash); var block = this.tumblingState.blockStoreManager.BlockRepository?.GetAsync(blockHash).Result; this.tumblingState.walletManager.ProcessTransaction(transaction, chainBlock.Height, block); _Cache.ImportTransaction(transaction, GetBlockConfirmations(merkleProof.Header.GetHash())); return(true); }
public static SmartMerkleBlock FromBytes(byte[] bytes) { var heightBytes = bytes.Take(4).ToArray(); var merkleBlockBytes = bytes.Skip(4).ToArray(); var height = new Height(BitConverter.ToInt32(heightBytes, startIndex: 0)); var merkleBlock = new MerkleBlock(); merkleBlock.FromBytes(merkleBlockBytes); return(new SmartMerkleBlock(height, merkleBlock)); }
public TransactionInformation GetTransaction(uint256 txId, bool withProof) { if (txId == null) { throw new ArgumentNullException(nameof(txId)); } try { //check in the wallet tx var result = RPCClient.SendCommandNoThrows("gettransaction", txId.ToString(), true); if (result == null || result.Error != null) { //check in the txindex result = RPCClient.SendCommandNoThrows("getrawtransaction", txId.ToString(), 1); if (result == null || result.Error != null) { return(null); } } var tx = new Transaction((string)result.Result["hex"]); var confirmations = result.Result["confirmations"]; var confCount = confirmations == null ? 0 : Math.Max(0, (int)confirmations); MerkleBlock proof = null; if (withProof) { if (confCount == 0) { return(null); } var result1 = RPCClient.SendCommandNoThrows("gettxoutproof", new JArray(tx.GetHash().ToString())); if (result1 == null || result1.Error != null) { return(null); } proof = new MerkleBlock(); proof.ReadWrite(Encoders.Hex.DecodeData(result1.ResultString)); } return(new TransactionInformation { Confirmations = confCount, Transaction = tx, MerkleProof = proof }); } catch (RPCException) { return(null); } }
void AttachedNode_MessageReceived(Node node, IncomingMessage message) { var filterload = message.Message.Payload as FilterLoadPayload; if (filterload != null) { _Filter = filterload.Object; } var filteradd = message.Message.Payload as FilterAddPayload; if (filteradd != null) { _Filter.Insert(filteradd.Data); } var getdata = message.Message.Payload as GetDataPayload; if (getdata != null) { foreach (var inv in getdata.Inventory) { if (inv.Type == InventoryType.MSG_FILTERED_BLOCK && _Filter != null) { var merkle = new MerkleBlock(_Blocks[inv.Hash], _Filter); AttachedNode.SendMessageAsync(new MerkleBlockPayload(merkle)); foreach (var tx in merkle.PartialMerkleTree.GetMatchedTransactions()) { if (_Known.TryAdd(tx, tx)) { AttachedNode.SendMessageAsync(new InvPayload(InventoryType.MSG_TX, tx)); } } } var found = FindTransaction(inv.Hash); if (inv.Type == InventoryType.MSG_TX && found != null) { AttachedNode.SendMessageAsync(new TxPayload(found)); } } } var mempool = message.Message.Payload as MempoolPayload; if (mempool != null) { foreach (var tx in _Builder.Mempool) { BroadcastCore(tx.Value); } } }
public async Task <IActionResult> GetTxOutProofAsync([FromQuery] string[] txids, string blockhash = "") { List <uint256> transactionIds = txids.Select(txString => uint256.Parse(txString)).ToList(); ChainedHeaderBlock block = null; if (!string.IsNullOrEmpty(blockhash)) { // We presume that all the transactions are supposed to be contained in the same specified block, so we only retrieve it once. uint256 hashBlock = uint256.Parse(blockhash); block = this.consensusManager?.GetBlockData(hashBlock); } else { // Loop through txids and try to find which block they're in. Exit loop once a block is found. foreach (uint256 transactionId in transactionIds) { ChainedHeader chainedHeader = this.GetTransactionBlock(transactionId); if (chainedHeader?.BlockDataAvailability != null && chainedHeader.BlockDataAvailability == BlockDataAvailabilityState.BlockAvailable) { block = this.consensusManager?.GetBlockData(chainedHeader.HashBlock); break; } } } if (block == null) { throw new Exception("Block not found"); } // Need to be able to lookup the transactions within the block efficiently. HashSet <uint256> transactionMap = block.Block.Transactions.Select(t => t.GetHash()).ToHashSet(); // Loop through txids and verify that every transaction is in the block. foreach (uint256 transactionId in transactionIds) { if (!transactionMap.Contains(transactionId)) { throw new Exception("Not all transactions found in specified or retrieved block"); } } var result = new MerkleBlock(block.Block, transactionIds.ToArray()); return(this.Json(result)); }
public Operation(Transaction transaction, ChainedBlock block, MerkleBlock proof) : this() { Transaction = transaction; if(block != null) { proof = proof.Clone(); proof.PartialMerkleTree = proof.PartialMerkleTree.Trim(transaction.GetHash()); Height = block.Height; BlockId = block.HashBlock; Proof = proof; } UnconfirmedSeen = DateTimeOffset.UtcNow; AddedDate = DateTimeOffset.UtcNow; }
public Operation(Transaction transaction, ChainedBlock block, MerkleBlock proof, ConcurrentDictionary <string, TrackedScript> trackedScripts) : this(trackedScripts) { Transaction = transaction; if (block != null) { proof = proof.Clone(); proof.PartialMerkleTree = proof.PartialMerkleTree.Trim(transaction.GetHash()); Height = block.Height; BlockId = block.HashBlock; Proof = proof; } UnconfirmedSeen = DateTimeOffset.UtcNow; AddedDate = DateTimeOffset.UtcNow; }
public MerkleBlock GetTxOutProof(string[] txids, string blockhash = "") { List <uint256> transactionIds = new List <uint256>(); ChainedHeaderBlock block = null; foreach (var txString in txids) { transactionIds.Add(uint256.Parse(txString)); } if (!string.IsNullOrEmpty(blockhash)) { // Loop through txids and veryify that the transaction is in the block. foreach (var transactionId in transactionIds) { var hashBlock = uint256.Parse(blockhash); ChainedHeader chainedHeader = this.GetTransactionBlock(transactionId); if (chainedHeader.HashBlock != hashBlock) { throw new RPCServerException(RPCErrorCode.RPC_INVALID_ADDRESS_OR_KEY, "Not all transactions found in specified or retrieved block"); } if (block == null && chainedHeader.BlockDataAvailability == BlockDataAvailabilityState.BlockAvailable) // Only get the block once. { block = this.consensusManager.GetBlockData(chainedHeader.HashBlock); } } } else { // Loop through txids and try to find which block they're in. Exit loop once a block is found. foreach (var transactionId in transactionIds) { ChainedHeader chainedHeader = this.GetTransactionBlock(transactionId); if (chainedHeader.BlockDataAvailability == BlockDataAvailabilityState.BlockAvailable) { block = this.consensusManager.GetBlockData(chainedHeader.HashBlock); break; } } } if (block == null) { throw new RPCServerException(RPCErrorCode.RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); } var result = new MerkleBlock(block.Block, transactionIds.ToArray()); return(result); }
public static SmartMerkleBlock FromBytes(byte[] bytes) { var heightBytes = bytes.Take(4).ToArray(); var merkleBlockBytes = bytes.Skip(4).ToArray(); var height = new Height(BitConverter.ToInt32(heightBytes, startIndex: 0)); // Bypass NBitcoin bug var merkleBlock = new MerkleBlock(); if (!merkleBlock.ToBytes().SequenceEqual(merkleBlockBytes)) // if not default MerkleBlock { merkleBlock.FromBytes(merkleBlockBytes); } return(new SmartMerkleBlock(height, merkleBlock)); }
public void merkle_block_2_with_update_none() { // Random real block (000000005a4ded781e667e06ceefafb71410b511fe0d5adc3e5a27ecbec34ae6) // With 4 txes Block block = new Block(); block.ReadWrite(ParseHex("0100000075616236cc2126035fadb38deb65b9102cc2c41c09cdf29fc051906800000000fe7d5e12ef0ff901f6050211249919b1c0653771832b3a80c66cea42847f0ae1d4d26e49ffff001d00f0a4410401000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0804ffff001d029105ffffffff0100f2052a010000004341046d8709a041d34357697dfcb30a9d05900a6294078012bf3bb09c6f9b525f1d16d5503d7905db1ada9501446ea00728668fc5719aa80be2fdfc8a858a4dbdd4fbac00000000010000000255605dc6f5c3dc148b6da58442b0b2cd422be385eab2ebea4119ee9c268d28350000000049483045022100aa46504baa86df8a33b1192b1b9367b4d729dc41e389f2c04f3e5c7f0559aae702205e82253a54bf5c4f65b7428551554b2045167d6d206dfe6a2e198127d3f7df1501ffffffff55605dc6f5c3dc148b6da58442b0b2cd422be385eab2ebea4119ee9c268d2835010000004847304402202329484c35fa9d6bb32a55a70c0982f606ce0e3634b69006138683bcd12cbb6602200c28feb1e2555c3210f1dddb299738b4ff8bbe9667b68cb8764b5ac17b7adf0001ffffffff0200e1f505000000004341046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac00180d8f000000004341044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad769f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875a390f67c1f6c94cfc617c0ea45afac0000000001000000025f9a06d3acdceb56be1bfeaa3e8a25e62d182fa24fefe899d1c17f1dad4c2028000000004847304402205d6058484157235b06028c30736c15613a28bdb768ee628094ca8b0030d4d6eb0220328789c9a2ec27ddaec0ad5ef58efded42e6ea17c2e1ce838f3d6913f5e95db601ffffffff5f9a06d3acdceb56be1bfeaa3e8a25e62d182fa24fefe899d1c17f1dad4c2028010000004a493046022100c45af050d3cea806cedd0ab22520c53ebe63b987b8954146cdca42487b84bdd6022100b9b027716a6b59e640da50a864d6dd8a0ef24c76ce62391fa3eabaf4d2886d2d01ffffffff0200e1f505000000004341046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac00180d8f000000004341046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac000000000100000002e2274e5fea1bf29d963914bd301aa63b64daaf8a3e88f119b5046ca5738a0f6b0000000048473044022016e7a727a061ea2254a6c358376aaa617ac537eb836c77d646ebda4c748aac8b0220192ce28bf9f2c06a6467e6531e27648d2b3e2e2bae85159c9242939840295ba501ffffffffe2274e5fea1bf29d963914bd301aa63b64daaf8a3e88f119b5046ca5738a0f6b010000004a493046022100b7a1a755588d4190118936e15cd217d133b0e4a53c3c15924010d5648d8925c9022100aaef031874db2114f2d869ac2de4ae53908fbfea5b2b1862e181626bb9005c9f01ffffffff0200e1f505000000004341044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad769f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875a390f67c1f6c94cfc617c0ea45afac00180d8f000000004341046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac00000000")); BloomFilter filter = new BloomFilter(10, 0.000001, 0, BloomFlags.UPDATE_NONE); // Match the first transaction filter.Insert(uint256.Parse("0xe980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df5b47aecb93b70")); MerkleBlock merkleBlock = new MerkleBlock(block, filter); Assert.True(merkleBlock.Header.GetHash() == block.GetHash()); Assert.True(merkleBlock.PartialMerkleTree.Check(block.Header.HashMerkleRoot)); var vMatchedTxn = merkleBlock.PartialMerkleTree.GetMatchedTransactions().ToList(); Assert.True(vMatchedTxn.Count == 1); AssertMatch(block, vMatchedTxn, "0xe980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df5b47aecb93b70", 0, 0); // Match an output from the second transaction (the pubkey for address 1DZTzaBHUDM7T3QvUKBz4qXMRpkg8jsfB5) // This should not match the third transaction though it spends the output matched // It will match the fourth transaction, which has another pay-to-pubkey output to the same address filter.Insert(ParseHex("044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad769f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875a390f67c1f6c94cfc617c0ea45af")); merkleBlock = new MerkleBlock(block, filter); Assert.True(merkleBlock.Header.GetHash() == block.GetHash()); Assert.True(merkleBlock.PartialMerkleTree.Check(block.Header.HashMerkleRoot)); vMatchedTxn = merkleBlock.PartialMerkleTree.GetMatchedTransactions().ToList(); Assert.True(vMatchedTxn.Count == 3); AssertMatch(block, vMatchedTxn, "0xe980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df5b47aecb93b70", 0, 0); AssertMatch(block, vMatchedTxn, "0x28204cad1d7fc1d199e8ef4fa22f182de6258a3eaafe1bbe56ebdcacd3069a5f", 1, 1); AssertMatch(block, vMatchedTxn, "0x3c1d7e82342158e4109df2e0b6348b6e84e403d8b4046d7007663ace63cddb23", 2, 3); }
private void Notify(Transaction tx, MerkleBlock blk) { if (blk == null) { _Tracker.NotifyTransaction(tx); } else { var prev = _Chain.GetBlock(blk.Header.HashPrevBlock); if (prev != null) { var header = new ChainedBlock(blk.Header, null, prev); _Tracker.NotifyTransaction(tx, header, blk); } else { _Tracker.NotifyTransaction(tx); } } }
public void merkle_block_1() { // Random real block (0000000000013b8ab2cd513b0261a14096412195a72a0c4827d229dcc7e0f7af) // With 9 txes Block block = new Block(); block.ReadWrite(ParseHex("0100000090f0a9f110702f808219ebea1173056042a714bad51b916cb6800000000000005275289558f51c9966699404ae2294730c3c9f9bda53523ce50e9b95e558da2fdb261b4d4c86041b1ab1bf930901000000010000000000000000000000000000000000000000000000000000000000000000ffffffff07044c86041b0146ffffffff0100f2052a01000000434104e18f7afbe4721580e81e8414fc8c24d7cfacf254bb5c7b949450c3e997c2dc1242487a8169507b631eb3771f2b425483fb13102c4eb5d858eef260fe70fbfae0ac00000000010000000196608ccbafa16abada902780da4dc35dafd7af05fa0da08cf833575f8cf9e836000000004a493046022100dab24889213caf43ae6adc41cf1c9396c08240c199f5225acf45416330fd7dbd022100fe37900e0644bf574493a07fc5edba06dbc07c311b947520c2d514bc5725dcb401ffffffff0100f2052a010000001976a914f15d1921f52e4007b146dfa60f369ed2fc393ce288ac000000000100000001fb766c1288458c2bafcfec81e48b24d98ec706de6b8af7c4e3c29419bfacb56d000000008c493046022100f268ba165ce0ad2e6d93f089cfcd3785de5c963bb5ea6b8c1b23f1ce3e517b9f022100da7c0f21adc6c401887f2bfd1922f11d76159cbc597fbd756a23dcbb00f4d7290141042b4e8625a96127826915a5b109852636ad0da753c9e1d5606a50480cd0c40f1f8b8d898235e571fe9357d9ec842bc4bba1827daaf4de06d71844d0057707966affffffff0280969800000000001976a9146963907531db72d0ed1a0cfb471ccb63923446f388ac80d6e34c000000001976a914f0688ba1c0d1ce182c7af6741e02658c7d4dfcd388ac000000000100000002c40297f730dd7b5a99567eb8d27b78758f607507c52292d02d4031895b52f2ff010000008b483045022100f7edfd4b0aac404e5bab4fd3889e0c6c41aa8d0e6fa122316f68eddd0a65013902205b09cc8b2d56e1cd1f7f2fafd60a129ed94504c4ac7bdc67b56fe67512658b3e014104732012cb962afa90d31b25d8fb0e32c94e513ab7a17805c14ca4c3423e18b4fb5d0e676841733cb83abaf975845c9f6f2a8097b7d04f4908b18368d6fc2d68ecffffffffca5065ff9617cbcba45eb23726df6498a9b9cafed4f54cbab9d227b0035ddefb000000008a473044022068010362a13c7f9919fa832b2dee4e788f61f6f5d344a7c2a0da6ae740605658022006d1af525b9a14a35c003b78b72bd59738cd676f845d1ff3fc25049e01003614014104732012cb962afa90d31b25d8fb0e32c94e513ab7a17805c14ca4c3423e18b4fb5d0e676841733cb83abaf975845c9f6f2a8097b7d04f4908b18368d6fc2d68ecffffffff01001ec4110200000043410469ab4181eceb28985b9b4e895c13fa5e68d85761b7eee311db5addef76fa8621865134a221bd01f28ec9999ee3e021e60766e9d1f3458c115fb28650605f11c9ac000000000100000001cdaf2f758e91c514655e2dc50633d1e4c84989f8aa90a0dbc883f0d23ed5c2fa010000008b48304502207ab51be6f12a1962ba0aaaf24a20e0b69b27a94fac5adf45aa7d2d18ffd9236102210086ae728b370e5329eead9accd880d0cb070aea0c96255fae6c4f1ddcce1fd56e014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffff02404b4c00000000001976a9142b6ba7c9d796b75eef7942fc9288edd37c32f5c388ac002d3101000000001976a9141befba0cdc1ad56529371864d9f6cb042faa06b588ac000000000100000001b4a47603e71b61bc3326efd90111bf02d2f549b067f4c4a8fa183b57a0f800cb010000008a4730440220177c37f9a505c3f1a1f0ce2da777c339bd8339ffa02c7cb41f0a5804f473c9230220585b25a2ee80eb59292e52b987dad92acb0c64eced92ed9ee105ad153cdb12d001410443bd44f683467e549dae7d20d1d79cbdb6df985c6e9c029c8d0c6cb46cc1a4d3cf7923c5021b27f7a0b562ada113bc85d5fda5a1b41e87fe6e8802817cf69996ffffffff0280651406000000001976a9145505614859643ab7b547cd7f1f5e7e2a12322d3788ac00aa0271000000001976a914ea4720a7a52fc166c55ff2298e07baf70ae67e1b88ac00000000010000000586c62cd602d219bb60edb14a3e204de0705176f9022fe49a538054fb14abb49e010000008c493046022100f2bc2aba2534becbdf062eb993853a42bbbc282083d0daf9b4b585bd401aa8c9022100b1d7fd7ee0b95600db8535bbf331b19eed8d961f7a8e54159c53675d5f69df8c014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffff03ad0e58ccdac3df9dc28a218bcf6f1997b0a93306faaa4b3a28ae83447b2179010000008b483045022100be12b2937179da88599e27bb31c3525097a07cdb52422d165b3ca2f2020ffcf702200971b51f853a53d644ebae9ec8f3512e442b1bcb6c315a5b491d119d10624c83014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffff2acfcab629bbc8685792603762c921580030ba144af553d271716a95089e107b010000008b483045022100fa579a840ac258871365dd48cd7552f96c8eea69bd00d84f05b283a0dab311e102207e3c0ee9234814cfbb1b659b83671618f45abc1326b9edcc77d552a4f2a805c0014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffffdcdc6023bbc9944a658ddc588e61eacb737ddf0a3cd24f113b5a8634c517fcd2000000008b4830450221008d6df731df5d32267954bd7d2dda2302b74c6c2a6aa5c0ca64ecbabc1af03c75022010e55c571d65da7701ae2da1956c442df81bbf076cdbac25133f99d98a9ed34c014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffffe15557cd5ce258f479dfd6dc6514edf6d7ed5b21fcfa4a038fd69f06b83ac76e010000008b483045022023b3e0ab071eb11de2eb1cc3a67261b866f86bf6867d4558165f7c8c8aca2d86022100dc6e1f53a91de3efe8f63512850811f26284b62f850c70ca73ed5de8771fb451014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffff01404b4c00000000001976a9142b6ba7c9d796b75eef7942fc9288edd37c32f5c388ac00000000010000000166d7577163c932b4f9690ca6a80b6e4eb001f0a2fa9023df5595602aae96ed8d000000008a4730440220262b42546302dfb654a229cefc86432b89628ff259dc87edd1154535b16a67e102207b4634c020a97c3e7bbd0d4d19da6aa2269ad9dded4026e896b213d73ca4b63f014104979b82d02226b3a4597523845754d44f13639e3bf2df5e82c6aab2bdc79687368b01b1ab8b19875ae3c90d661a3d0a33161dab29934edeb36aa01976be3baf8affffffff02404b4c00000000001976a9144854e695a02af0aeacb823ccbc272134561e0a1688ac40420f00000000001976a914abee93376d6b37b5c2940655a6fcaf1c8e74237988ac0000000001000000014e3f8ef2e91349a9059cb4f01e54ab2597c1387161d3da89919f7ea6acdbb371010000008c49304602210081f3183471a5ca22307c0800226f3ef9c353069e0773ac76bb580654d56aa523022100d4c56465bdc069060846f4fbf2f6b20520b2a80b08b168b31e66ddb9c694e240014104976c79848e18251612f8940875b2b08d06e6dc73b9840e8860c066b7e87432c477e9a59a453e71e6d76d5fe34058b800a098fc1740ce3012e8fc8a00c96af966ffffffff02c0e1e400000000001976a9144134e75a6fcb6042034aab5e18570cf1f844f54788ac404b4c00000000001976a9142b6ba7c9d796b75eef7942fc9288edd37c32f5c388ac00000000")); BloomFilter filter = new BloomFilter(10, 0.000001, 0, BloomFlags.UPDATE_ALL); // Match the last transaction filter.Insert(uint256.Parse("0x74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20")); MerkleBlock merkleBlock = block.Filter(filter); Assert.True(merkleBlock.Header.GetHash() == block.GetHash()); Assert.True(merkleBlock.PartialMerkleTree.Check(block.Header.HashMerkleRoot)); var vMatchedTxn = merkleBlock.PartialMerkleTree.GetMatchedTransactions().ToList(); Assert.True(vMatchedTxn.Count == 1); AssertMatch(block, vMatchedTxn, "0x74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20", 0, 8); // Also match the 8th transaction filter.Insert(uint256.Parse("0xdd1fd2a6fc16404faf339881a90adbde7f4f728691ac62e8f168809cdfae1053")); merkleBlock = block.Filter(filter); vMatchedTxn = merkleBlock.PartialMerkleTree.GetMatchedTransactions().ToList(); Assert.True(merkleBlock.PartialMerkleTree.Check(block.Header.HashMerkleRoot)); Assert.True(merkleBlock.Header.GetHash() == block.GetHash()); Assert.True(vMatchedTxn.Count == 2); AssertMatch(block, vMatchedTxn, "0x74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20", 1, 8); AssertMatch(block, vMatchedTxn, "0xdd1fd2a6fc16404faf339881a90adbde7f4f728691ac62e8f168809cdfae1053", 0, 7); }
// TODO: how do we deal with re-orgs? private void ScanForServiceNodeRegistrations(int height, Block block) { foreach (Transaction tx in block.Transactions.Where(RegistrationToken.HasMarker)) { this.logger.LogDebug("Received a new service node registration transaction: " + tx.GetHash()); try { var registrationToken = new RegistrationToken(); registrationToken.ParseTransaction(tx, this.network); if (!registrationToken.Validate(this.network)) { this.logger.LogDebug("Registration token failed validation"); continue; } // TODO: doesn't belong here var merkleBlock = new MerkleBlock(block, new[] { tx.GetHash() }); var registrationRecord = new RegistrationRecord(DateTime.Now, Guid.NewGuid(), tx.GetHash().ToString(), tx.ToHex(), registrationToken, merkleBlock.PartialMerkleTree, height); string collateralAddress = registrationRecord.Token.CollateralPubKeyHash.GetAddress(this.network).ToString(); this.logger.LogTrace("New Service Node Registration"); var serviceNode = new Redstone.ServiceNode.Models.ServiceNode(registrationRecord); this.serviceNodeManager.AddServiceNode(serviceNode); } catch (Exception e) { this.logger.LogDebug("Failed to parse registration transaction, exception: " + e); } } this.serviceNodeManager.SyncedHeight = height; this.serviceNodeManager.SyncedBlockHash = block.GetHash(); }
private void Notify(Transaction tx, MerkleBlock blk) { if(blk == null) { _Tracker.NotifyTransaction(tx); } else { var prev = _Chain.GetBlock(blk.Header.HashPrevBlock); if(prev != null) { var header = new ChainedBlock(blk.Header, null, prev); _Tracker.NotifyTransaction(tx, header, blk); } else { _Tracker.NotifyTransaction(tx); } } }
private void Received(TrackedScript match, IndexedTxOut txout, ChainedBlock block, MerkleBlock proof) { var operation = new Operation(txout.Transaction, block, proof); SetUnconfirmedSeenIfPossible(txout.Transaction, block, operation); var coin = new Coin(txout); operation.ReceivedCoins.Add(Tuple.Create(coin, match.GetId())); _Operations.AddOrUpdate(operation.GetId(), operation, (k, old) => old.Merge(operation)); var trackedOutpoint = new TrackedOutpoint() { Coin = coin, TrackedScriptId = match.GetId(), Filter = match.Filter }; _TrackedOutpoints.TryAdd(trackedOutpoint.GetId(), trackedOutpoint); }
private void Spent(TrackedScript metadata, IndexedTxIn txin, Coin coin, ChainedBlock block, MerkleBlock proof) { var operation = new Operation(txin.Transaction, block, proof); operation.SpentCoins.Add(Tuple.Create(coin, metadata.GetId())); SetUnconfirmedSeenIfPossible(txin.Transaction, block, operation); _Operations.AddOrUpdate(operation.GetId(), operation, (k, old) => old.Merge(operation)); }
public SmartMerkleBlock(Height height, MerkleBlock merkleBlock) { Height = height; MerkleBlock = merkleBlock; }
private Operation Spent(TrackedScript metadata, IndexedTxIn txin, Coin coin, ChainedBlock block, MerkleBlock proof) { var operation = new Operation(txin.Transaction, block, proof, _TrackedScripts); operation.SpentCoins.Add(Tuple.Create(coin, metadata.GetId())); SetUnconfirmedSeenIfPossible(txin.Transaction, block, operation); bool merged = false; var returned = _Operations.AddOrUpdate(operation.GetId(), operation, (k, old) => old.Merge(operation, out merged)); return (operation == returned || merged) ? operation : null; }
void AttachedNode_MessageReceived(Node node, IncomingMessage message) { var filterload = message.Message.Payload as FilterLoadPayload; if(filterload != null) { _Filter = filterload.Object; } var filteradd = message.Message.Payload as FilterAddPayload; if(filteradd != null) { _Filter.Insert(filteradd.Data); } var getdata = message.Message.Payload as GetDataPayload; if(getdata != null) { foreach(var inv in getdata.Inventory) { if(inv.Type == InventoryType.MSG_FILTERED_BLOCK && _Filter != null) { var merkle = new MerkleBlock(_Blocks[inv.Hash], _Filter); AttachedNode.SendMessageAsync(new MerkleBlockPayload(merkle)); foreach(var tx in merkle.PartialMerkleTree.GetMatchedTransactions()) { if(_Known.TryAdd(tx, tx)) { AttachedNode.SendMessageAsync(new InvPayload(InventoryType.MSG_TX, tx)); } } } var found = FindTransaction(inv.Hash); if(inv.Type == InventoryType.MSG_TX && found != null) AttachedNode.SendMessageAsync(new TxPayload(found)); } } var mempool = message.Message.Payload as MempoolPayload; if(mempool != null) { foreach(var tx in _Builder.Mempool) { BroadcastCore(tx.Value); } } var invs = message.Message.Payload as InvPayload; if(invs != null) { node.SendMessageAsync(new GetDataPayload(invs.ToArray())); } var txPayload = message.Message.Payload as TxPayload; if(txPayload != null) { if(!_ReceivedTransactions.TryAdd(txPayload.Object.GetHash(), txPayload.Object)) { node.SendMessageAsync(new RejectPayload() { Hash = txPayload.Object.GetHash(), Code = RejectCode.DUPLICATE, Message = "tx" }); } else { foreach(var other in Nodes.Where(n => n != node)) { other.SendMessageAsync(new InvPayload(txPayload.Object)); } } } }
private bool Notify(Transaction tx, MerkleBlock blk) { bool hit = false; if(blk == null) { hit = _Tracker.NotifyTransaction(tx); } else { var prev = _Chain.GetBlock(blk.Header.HashPrevBlock); if(prev != null) { var header = new ChainedBlock(blk.Header, null, prev); hit = _Tracker.NotifyTransaction(tx, header, blk); } else { hit = _Tracker.NotifyTransaction(tx); } } Interlocked.Increment(ref _TotalReceived); if(!hit) { Interlocked.Increment(ref _FalsePositiveCount); if(MaximumFalsePositiveRate != null && _TotalReceived > 100 && ActualFalsePostiveRate >= MaximumFalsePositiveRate.Value) { this.AttachedNode.DisconnectAsync("The actual false positive rate exceed MaximumFalsePositiveRate"); } } return hit; }
void AttachedNode_MessageReceived(Node node, IncomingMessage message) { var filterload = message.Message.Payload as FilterLoadPayload; if(filterload != null) { _Filter = filterload.Object; } var filteradd = message.Message.Payload as FilterAddPayload; if(filteradd != null) { _Filter.Insert(filteradd.Data); } var getdata = message.Message.Payload as GetDataPayload; if(getdata != null) { foreach(var inv in getdata.Inventory) { if(inv.Type == InventoryType.MSG_FILTERED_BLOCK && _Filter != null) { var merkle = new MerkleBlock(_Blocks[inv.Hash], _Filter); AttachedNode.SendMessageAsync(new MerkleBlockPayload(merkle)); foreach(var tx in merkle.PartialMerkleTree.GetMatchedTransactions()) { if(_Known.TryAdd(tx, tx)) { AttachedNode.SendMessageAsync(new InvPayload(InventoryType.MSG_TX, tx)); } } } var found = FindTransaction(inv.Hash); if(inv.Type == InventoryType.MSG_TX && found != null) AttachedNode.SendMessageAsync(new TxPayload(found)); } } var mempool = message.Message.Payload as MempoolPayload; if(mempool != null) { foreach(var tx in _Builder.Mempool) { BroadcastCore(tx.Value); } } }
public async Task <bool> TrackPrunedTransactionAsync(Transaction transaction, MerkleBlock merkleProof) { bool success = false; await _RPCBatch.WaitTransactionAsync(async batch => { var result = await batch.SendCommandNoThrowsAsync("importprunedfunds", transaction.ToHex(), Encoders.Hex.EncodeData(merkleProof.ToBytes())).ConfigureAwait(false); success = result != null && result.Error == null; if (success) { _Cache.ImportTransaction(transaction, GetBlockConfirmations(merkleProof.Header.GetHash())); } return(success); }).ConfigureAwait(false); return(success); }
private bool Notify(Transaction tx, MerkleBlock blk) { bool hit = false; if(blk == null) { hit = _Tracker.NotifyTransaction(tx); } else { var prev = _Chain.GetBlock(blk.Header.HashPrevBlock); if(prev != null) { var header = new ChainedBlock(blk.Header, null, prev); hit = _Tracker.NotifyTransaction(tx, header, blk); } else { hit = _Tracker.NotifyTransaction(tx); } } Interlocked.Increment(ref _TotalReceived); if(!hit) { Interlocked.Increment(ref _FalsePositiveCount); } return hit; }
/// <inheritdoc /> public void ProcessBlock(int height, Block block) { // Check for any server registration transactions if (block.Transactions != null) { foreach (Transaction tx in block.Transactions) { // Minor optimisation to disregard transactions that cannot be registrations if (tx.Outputs.Count < 2) { continue; } // Check if the transaction has the Breeze registration marker output (literal text BREEZE_REGISTRATION_MARKER) if (tx.Outputs[0].ScriptPubKey.ToHex().ToLower().Equals("6a1a425245455a455f524547495354524154494f4e5f4d41524b4552")) { this.logger.LogDebug("Received a new registration transaction: " + tx.GetHash()); try { RegistrationToken registrationToken = new RegistrationToken(); registrationToken.ParseTransaction(tx, this.network); if (!registrationToken.Validate(this.network)) { this.logger.LogDebug("Registration token failed validation"); continue; } MerkleBlock merkleBlock = new MerkleBlock(block, new uint256[] { tx.GetHash() }); RegistrationRecord registrationRecord = new RegistrationRecord(DateTime.Now, Guid.NewGuid(), tx.GetHash().ToString(), tx.ToHex(), registrationToken, merkleBlock.PartialMerkleTree, height); // Ignore protocol versions outside the accepted bounds if ((registrationRecord.Record.ProtocolVersion < MIN_PROTOCOL_VERSION) || (registrationRecord.Record.ProtocolVersion > MAX_PROTOCOL_VERSION)) { this.logger.LogDebug("Registration protocol version out of bounds " + tx.GetHash()); continue; } // If there were other registrations for this server previously, remove them and add the new one this.registrationStore.AddWithReplace(registrationRecord); this.logger.LogDebug("Registration transaction for server collateral address: " + registrationRecord.Record.ServerId); this.logger.LogDebug("Server Onion address: " + registrationRecord.Record.OnionAddress); this.logger.LogDebug("Server configuration hash: " + registrationRecord.Record.ConfigurationHash); // Add collateral address to watch only wallet so that any funding transactions can be detected this.watchOnlyWalletManager.WatchAddress(registrationRecord.Record.ServerId); } catch (Exception e) { this.logger.LogDebug("Failed to parse registration transaction, exception: " + e); } } } WatchOnlyWallet watchOnlyWallet = this.watchOnlyWalletManager.GetWatchOnlyWallet(); // TODO: Need to have 'current height' field in watch-only wallet so that we don't start rebalancing collateral balances before the latest block has been processed & incorporated // Perform watch-only wallet housekeeping - iterate through known servers foreach (RegistrationRecord record in this.registrationStore.GetAll()) { Script scriptToCheck = BitcoinAddress.Create(record.Record.ServerId, this.network).ScriptPubKey; this.logger.LogDebug("Recalculating collateral balance for server: " + record.Record.ServerId); if (!watchOnlyWallet.WatchedAddresses.ContainsKey(scriptToCheck.ToString())) { this.logger.LogDebug("Server address missing from watch-only wallet. Deleting stored registrations for server: " + record.Record.ServerId); this.registrationStore.DeleteAllForServer(record.Record.ServerId); continue; } Money serverCollateralBalance = this.watchOnlyWalletManager.GetRelativeBalance(scriptToCheck.ToString()); this.logger.LogDebug("Collateral balance for server " + record.Record.ServerId + " is " + serverCollateralBalance.ToString() + ", original registration height " + record.BlockReceived + ", current height " + height); if ((serverCollateralBalance < MASTERNODE_COLLATERAL_THRESHOLD) && ((height - record.BlockReceived) > WINDOW_PERIOD_BLOCK_COUNT)) { // Remove server registrations as funding has not been performed timeously, // or funds have been removed from the collateral address subsequent to the // registration being performed this.logger.LogDebug("Insufficient collateral within window period for server: " + record.Record.ServerId); this.logger.LogDebug("Deleting registration records for server: " + record.Record.ServerId); this.registrationStore.DeleteAllForServer(record.Record.ServerId); // TODO: Remove unneeded transactions from the watch-only wallet? // TODO: Need to make the TumbleBitFeature change its server address if this is the address it was using } } this.watchOnlyWalletManager.SaveWatchOnlyWallet(); } }
public void merkle_block_1() { // Random real block (0000000000013b8ab2cd513b0261a14096412195a72a0c4827d229dcc7e0f7af) // With 9 txes Block block = new Block(); block.ReadWrite(ParseHex("0100000090f0a9f110702f808219ebea1173056042a714bad51b916cb6800000000000005275289558f51c9966699404ae2294730c3c9f9bda53523ce50e9b95e558da2fdb261b4d4c86041b1ab1bf930901000000010000000000000000000000000000000000000000000000000000000000000000ffffffff07044c86041b0146ffffffff0100f2052a01000000434104e18f7afbe4721580e81e8414fc8c24d7cfacf254bb5c7b949450c3e997c2dc1242487a8169507b631eb3771f2b425483fb13102c4eb5d858eef260fe70fbfae0ac00000000010000000196608ccbafa16abada902780da4dc35dafd7af05fa0da08cf833575f8cf9e836000000004a493046022100dab24889213caf43ae6adc41cf1c9396c08240c199f5225acf45416330fd7dbd022100fe37900e0644bf574493a07fc5edba06dbc07c311b947520c2d514bc5725dcb401ffffffff0100f2052a010000001976a914f15d1921f52e4007b146dfa60f369ed2fc393ce288ac000000000100000001fb766c1288458c2bafcfec81e48b24d98ec706de6b8af7c4e3c29419bfacb56d000000008c493046022100f268ba165ce0ad2e6d93f089cfcd3785de5c963bb5ea6b8c1b23f1ce3e517b9f022100da7c0f21adc6c401887f2bfd1922f11d76159cbc597fbd756a23dcbb00f4d7290141042b4e8625a96127826915a5b109852636ad0da753c9e1d5606a50480cd0c40f1f8b8d898235e571fe9357d9ec842bc4bba1827daaf4de06d71844d0057707966affffffff0280969800000000001976a9146963907531db72d0ed1a0cfb471ccb63923446f388ac80d6e34c000000001976a914f0688ba1c0d1ce182c7af6741e02658c7d4dfcd388ac000000000100000002c40297f730dd7b5a99567eb8d27b78758f607507c52292d02d4031895b52f2ff010000008b483045022100f7edfd4b0aac404e5bab4fd3889e0c6c41aa8d0e6fa122316f68eddd0a65013902205b09cc8b2d56e1cd1f7f2fafd60a129ed94504c4ac7bdc67b56fe67512658b3e014104732012cb962afa90d31b25d8fb0e32c94e513ab7a17805c14ca4c3423e18b4fb5d0e676841733cb83abaf975845c9f6f2a8097b7d04f4908b18368d6fc2d68ecffffffffca5065ff9617cbcba45eb23726df6498a9b9cafed4f54cbab9d227b0035ddefb000000008a473044022068010362a13c7f9919fa832b2dee4e788f61f6f5d344a7c2a0da6ae740605658022006d1af525b9a14a35c003b78b72bd59738cd676f845d1ff3fc25049e01003614014104732012cb962afa90d31b25d8fb0e32c94e513ab7a17805c14ca4c3423e18b4fb5d0e676841733cb83abaf975845c9f6f2a8097b7d04f4908b18368d6fc2d68ecffffffff01001ec4110200000043410469ab4181eceb28985b9b4e895c13fa5e68d85761b7eee311db5addef76fa8621865134a221bd01f28ec9999ee3e021e60766e9d1f3458c115fb28650605f11c9ac000000000100000001cdaf2f758e91c514655e2dc50633d1e4c84989f8aa90a0dbc883f0d23ed5c2fa010000008b48304502207ab51be6f12a1962ba0aaaf24a20e0b69b27a94fac5adf45aa7d2d18ffd9236102210086ae728b370e5329eead9accd880d0cb070aea0c96255fae6c4f1ddcce1fd56e014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffff02404b4c00000000001976a9142b6ba7c9d796b75eef7942fc9288edd37c32f5c388ac002d3101000000001976a9141befba0cdc1ad56529371864d9f6cb042faa06b588ac000000000100000001b4a47603e71b61bc3326efd90111bf02d2f549b067f4c4a8fa183b57a0f800cb010000008a4730440220177c37f9a505c3f1a1f0ce2da777c339bd8339ffa02c7cb41f0a5804f473c9230220585b25a2ee80eb59292e52b987dad92acb0c64eced92ed9ee105ad153cdb12d001410443bd44f683467e549dae7d20d1d79cbdb6df985c6e9c029c8d0c6cb46cc1a4d3cf7923c5021b27f7a0b562ada113bc85d5fda5a1b41e87fe6e8802817cf69996ffffffff0280651406000000001976a9145505614859643ab7b547cd7f1f5e7e2a12322d3788ac00aa0271000000001976a914ea4720a7a52fc166c55ff2298e07baf70ae67e1b88ac00000000010000000586c62cd602d219bb60edb14a3e204de0705176f9022fe49a538054fb14abb49e010000008c493046022100f2bc2aba2534becbdf062eb993853a42bbbc282083d0daf9b4b585bd401aa8c9022100b1d7fd7ee0b95600db8535bbf331b19eed8d961f7a8e54159c53675d5f69df8c014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffff03ad0e58ccdac3df9dc28a218bcf6f1997b0a93306faaa4b3a28ae83447b2179010000008b483045022100be12b2937179da88599e27bb31c3525097a07cdb52422d165b3ca2f2020ffcf702200971b51f853a53d644ebae9ec8f3512e442b1bcb6c315a5b491d119d10624c83014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffff2acfcab629bbc8685792603762c921580030ba144af553d271716a95089e107b010000008b483045022100fa579a840ac258871365dd48cd7552f96c8eea69bd00d84f05b283a0dab311e102207e3c0ee9234814cfbb1b659b83671618f45abc1326b9edcc77d552a4f2a805c0014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffffdcdc6023bbc9944a658ddc588e61eacb737ddf0a3cd24f113b5a8634c517fcd2000000008b4830450221008d6df731df5d32267954bd7d2dda2302b74c6c2a6aa5c0ca64ecbabc1af03c75022010e55c571d65da7701ae2da1956c442df81bbf076cdbac25133f99d98a9ed34c014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffffe15557cd5ce258f479dfd6dc6514edf6d7ed5b21fcfa4a038fd69f06b83ac76e010000008b483045022023b3e0ab071eb11de2eb1cc3a67261b866f86bf6867d4558165f7c8c8aca2d86022100dc6e1f53a91de3efe8f63512850811f26284b62f850c70ca73ed5de8771fb451014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffff01404b4c00000000001976a9142b6ba7c9d796b75eef7942fc9288edd37c32f5c388ac00000000010000000166d7577163c932b4f9690ca6a80b6e4eb001f0a2fa9023df5595602aae96ed8d000000008a4730440220262b42546302dfb654a229cefc86432b89628ff259dc87edd1154535b16a67e102207b4634c020a97c3e7bbd0d4d19da6aa2269ad9dded4026e896b213d73ca4b63f014104979b82d02226b3a4597523845754d44f13639e3bf2df5e82c6aab2bdc79687368b01b1ab8b19875ae3c90d661a3d0a33161dab29934edeb36aa01976be3baf8affffffff02404b4c00000000001976a9144854e695a02af0aeacb823ccbc272134561e0a1688ac40420f00000000001976a914abee93376d6b37b5c2940655a6fcaf1c8e74237988ac0000000001000000014e3f8ef2e91349a9059cb4f01e54ab2597c1387161d3da89919f7ea6acdbb371010000008c49304602210081f3183471a5ca22307c0800226f3ef9c353069e0773ac76bb580654d56aa523022100d4c56465bdc069060846f4fbf2f6b20520b2a80b08b168b31e66ddb9c694e240014104976c79848e18251612f8940875b2b08d06e6dc73b9840e8860c066b7e87432c477e9a59a453e71e6d76d5fe34058b800a098fc1740ce3012e8fc8a00c96af966ffffffff02c0e1e400000000001976a9144134e75a6fcb6042034aab5e18570cf1f844f54788ac404b4c00000000001976a9142b6ba7c9d796b75eef7942fc9288edd37c32f5c388ac00000000")); BloomFilter filter = new BloomFilter(10, 0.000001, 0, BloomFlags.UPDATE_ALL); // Match the last transaction filter.Insert(new uint256("0x74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20")); MerkleBlock merkleBlock = new MerkleBlock(block, filter); Assert.True(merkleBlock.Header.GetHash() == block.GetHash()); Assert.True(merkleBlock.vMatchedTxn.Count == 1); var pair = merkleBlock.vMatchedTxn[0]; Assert.True(merkleBlock.vMatchedTxn[0].Item2 == new uint256("0x74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20")); Assert.True(merkleBlock.vMatchedTxn[0].Item1 == 8); List<uint256> vMatched = new List<uint256>(); Assert.True(merkleBlock.Txn.ExtractMatches(vMatched) == block.Header.HashMerkleRoot); Assert.True(vMatched.Count == merkleBlock.vMatchedTxn.Count); for(int i = 0 ; i < vMatched.Count ; i++) Assert.True(vMatched[i] == merkleBlock.vMatchedTxn[i].Item2); // Also match the 8th transaction filter.Insert(new uint256("0xdd1fd2a6fc16404faf339881a90adbde7f4f728691ac62e8f168809cdfae1053")); merkleBlock = new MerkleBlock(block, filter); Assert.True(merkleBlock.Header.GetHash() == block.GetHash()); Assert.True(merkleBlock.vMatchedTxn.Count == 2); Assert.True(TestUtils.TupleEquals(merkleBlock.vMatchedTxn[1], pair)); Assert.True(merkleBlock.vMatchedTxn[0].Item2 == new uint256("0xdd1fd2a6fc16404faf339881a90adbde7f4f728691ac62e8f168809cdfae1053")); Assert.True(merkleBlock.vMatchedTxn[0].Item1 == 7); Assert.True(merkleBlock.Txn.ExtractMatches(vMatched) == block.Header.HashMerkleRoot); Assert.True(vMatched.Count == merkleBlock.vMatchedTxn.Count); for(int i = 0 ; i < vMatched.Count ; i++) Assert.True(vMatched[i] == merkleBlock.vMatchedTxn[i].Item2); }
private Operation Received(TrackedScript match, IndexedTxOut txout, ChainedBlock block, MerkleBlock proof) { var operation = new Operation(txout.Transaction, block, proof, _TrackedScripts); SetUnconfirmedSeenIfPossible(txout.Transaction, block, operation); var coin = new Coin(txout); operation.ReceivedCoins.Add(Tuple.Create(coin, match.GetId())); bool merged = false; var returned = _Operations.AddOrUpdate(operation.GetId(), operation, (k, old) => old.Merge(operation, out merged)); var trackedOutpoint = new TrackedOutpoint() { Coin = coin, TrackedScriptId = match.GetId(), Filter = match.Filter }; _TrackedOutpoints.TryAdd(trackedOutpoint.GetId(), trackedOutpoint); return (operation == returned || merged) ? operation : null; }
void AttachedNode_MessageReceived(Node node, IncomingMessage message) { var filterload = message.Message.Payload as FilterLoadPayload; if(filterload != null) { _Filter = filterload.Object; } var getdata = message.Message.Payload as GetDataPayload; if(getdata != null) { foreach(var inv in getdata.Inventory) { if(inv.Type == InventoryType.MSG_FILTERED_BLOCK && _Filter != null) { var merkle = new MerkleBlock(_Blocks[inv.Hash], _Filter); AttachedNode.SendMessageAsync(new MerkleBlockPayload(merkle)); foreach(var tx in merkle.PartialMerkleTree.GetMatchedTransactions()) { if(_Known.TryAdd(tx, tx)) { AttachedNode.SendMessageAsync(new InvPayload(InventoryType.MSG_TX, tx)); } } } if(inv.Type == InventoryType.MSG_TX) AttachedNode.SendMessageAsync(new TxPayload(_Transactions[inv.Hash])); } } }
void AttachedNode_MessageReceived(Node node, IncomingMessage message) { var filterload = message.Message.Payload as FilterLoadPayload; if (filterload != null) { _Filter = filterload.Object; } var filteradd = message.Message.Payload as FilterAddPayload; if (filteradd != null) { _Filter.Insert(filteradd.Data); } var getdata = message.Message.Payload as GetDataPayload; if (getdata != null) { foreach (var inv in getdata.Inventory) { if (inv.Type == InventoryType.MSG_FILTERED_BLOCK && _Filter != null) { var merkle = new MerkleBlock(_Blocks[inv.Hash], _Filter); AttachedNode.SendMessageAsync(new MerkleBlockPayload(merkle)); foreach (var tx in merkle.PartialMerkleTree.GetMatchedTransactions()) { if (_Known.TryAdd(tx, tx)) { AttachedNode.SendMessageAsync(new InvPayload(InventoryType.MSG_TX, tx)); } } } var found = FindTransaction(inv.Hash); if (inv.Type == InventoryType.MSG_TX && found != null) { AttachedNode.SendMessageAsync(new TxPayload(found)); } } } var mempool = message.Message.Payload as MempoolPayload; if (mempool != null) { foreach (var tx in _Builder.Mempool) { BroadcastCore(tx.Value); } } var invs = message.Message.Payload as InvPayload; if (invs != null) { node.SendMessageAsync(new GetDataPayload(invs.ToArray())); } var txPayload = message.Message.Payload as TxPayload; if (txPayload != null) { if (!_ReceivedTransactions.TryAdd(txPayload.Object.GetHash(), txPayload.Object)) { node.SendMessageAsync(new RejectPayload() { Hash = txPayload.Object.GetHash(), Code = RejectCode.DUPLICATE, Message = "tx" }); } else { foreach (var other in Nodes.Where(n => n != node)) { other.SendMessageAsync(new InvPayload(txPayload.Object)); } } } }
public SmartMerkleBlock(int height, Block block, params uint256[] interestedTransactionIds) { Height = new Height(height); MerkleBlock = interestedTransactionIds == null || interestedTransactionIds.Length == 0 ? block.Filter() : block.Filter(interestedTransactionIds); }
public void merkle_block_4_test_update_none() { // Random real block (000000000000b731f2eef9e8c63173adfb07e41bd53eb0ef0a6b720d6cb6dea4) // With 7 txes Block block = new Block(); block.ReadWrite(ParseHex("0100000082bb869cf3a793432a66e826e05a6fc37469f8efb7421dc880670100000000007f16c5962e8bd963659c793ce370d95f093bc7e367117b3c30c1f8fdd0d9728776381b4d4c86041b554b85290701000000010000000000000000000000000000000000000000000000000000000000000000ffffffff07044c86041b0136ffffffff0100f2052a01000000434104eaafc2314def4ca98ac970241bcab022b9c1e1f4ea423a20f134c876f2c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce13ad1357231a2252247d97a46a91ac000000000100000001bcad20a6a29827d1424f08989255120bf7f3e9e3cdaaa6bb31b0737fe048724300000000494830450220356e834b046cadc0f8ebb5a8a017b02de59c86305403dad52cd77b55af062ea10221009253cd6c119d4729b77c978e1e2aa19f5ea6e0e52b3f16e32fa608cd5bab753901ffffffff02008d380c010000001976a9142b4b8072ecbba129b6453c63e129e643207249ca88ac0065cd1d000000001976a9141b8dd13b994bcfc787b32aeadf58ccb3615cbd5488ac000000000100000003fdacf9b3eb077412e7a968d2e4f11b9a9dee312d666187ed77ee7d26af16cb0b000000008c493046022100ea1608e70911ca0de5af51ba57ad23b9a51db8d28f82c53563c56a05c20f5a87022100a8bdc8b4a8acc8634c6b420410150775eb7f2474f5615f7fccd65af30f310fbf01410465fdf49e29b06b9a1582287b6279014f834edc317695d125ef623c1cc3aaece245bd69fcad7508666e9c74a49dc9056d5fc14338ef38118dc4afae5fe2c585caffffffff309e1913634ecb50f3c4f83e96e70b2df071b497b8973a3e75429df397b5af83000000004948304502202bdb79c596a9ffc24e96f4386199aba386e9bc7b6071516e2b51dda942b3a1ed022100c53a857e76b724fc14d45311eac5019650d415c3abb5428f3aae16d8e69bec2301ffffffff2089e33491695080c9edc18a428f7d834db5b6d372df13ce2b1b0e0cbcb1e6c10000000049483045022100d4ce67c5896ee251c810ac1ff9ceccd328b497c8f553ab6e08431e7d40bad6b5022033119c0c2b7d792d31f1187779c7bd95aefd93d90a715586d73801d9b47471c601ffffffff0100714460030000001976a914c7b55141d097ea5df7a0ed330cf794376e53ec8d88ac0000000001000000045bf0e214aa4069a3e792ecee1e1bf0c1d397cde8dd08138f4b72a00681743447000000008b48304502200c45de8c4f3e2c1821f2fc878cba97b1e6f8807d94930713aa1c86a67b9bf1e40221008581abfef2e30f957815fc89978423746b2086375ca8ecf359c85c2a5b7c88ad01410462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95ffffffffd669f7d7958d40fc59d2253d88e0f248e29b599c80bbcec344a83dda5f9aa72c000000008a473044022078124c8beeaa825f9e0b30bff96e564dd859432f2d0cb3b72d3d5d93d38d7e930220691d233b6c0f995be5acb03d70a7f7a65b6bc9bdd426260f38a1346669507a3601410462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95fffffffff878af0d93f5229a68166cf051fd372bb7a537232946e0a46f53636b4dafdaa4000000008c493046022100c717d1714551663f69c3c5759bdbb3a0fcd3fab023abc0e522fe6440de35d8290221008d9cbe25bffc44af2b18e81c58eb37293fd7fe1c2e7b46fc37ee8c96c50ab1e201410462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95ffffffff27f2b668859cd7f2f894aa0fd2d9e60963bcd07c88973f425f999b8cbfd7a1e2000000008c493046022100e00847147cbf517bcc2f502f3ddc6d284358d102ed20d47a8aa788a62f0db780022100d17b2d6fa84dcaf1c95d88d7e7c30385aecf415588d749afd3ec81f6022cecd701410462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95ffffffff0100c817a8040000001976a914b6efd80d99179f4f4ff6f4dd0a007d018c385d2188ac000000000100000001834537b2f1ce8ef9373a258e10545ce5a50b758df616cd4356e0032554ebd3c4000000008b483045022100e68f422dd7c34fdce11eeb4509ddae38201773dd62f284e8aa9d96f85099d0b002202243bd399ff96b649a0fad05fa759d6a882f0af8c90cf7632c2840c29070aec20141045e58067e815c2f464c6a2a15f987758374203895710c2d452442e28496ff38ba8f5fd901dc20e29e88477167fe4fc299bf818fd0d9e1632d467b2a3d9503b1aaffffffff0280d7e636030000001976a914f34c3e10eb387efe872acb614c89e78bfca7815d88ac404b4c00000000001976a914a84e272933aaf87e1715d7786c51dfaeb5b65a6f88ac00000000010000000143ac81c8e6f6ef307dfe17f3d906d999e23e0189fda838c5510d850927e03ae7000000008c4930460221009c87c344760a64cb8ae6685a3eec2c1ac1bed5b88c87de51acd0e124f266c16602210082d07c037359c3a257b5c63ebd90f5a5edf97b2ac1c434b08ca998839f346dd40141040ba7e521fa7946d12edbb1d1e95a15c34bd4398195e86433c92b431cd315f455fe30032ede69cad9d1e1ed6c3c4ec0dbfced53438c625462afb792dcb098544bffffffff0240420f00000000001976a9144676d1b820d63ec272f1900d59d43bc6463d96f888ac40420f00000000001976a914648d04341d00d7968b3405c034adc38d4d8fb9bd88ac00000000010000000248cc917501ea5c55f4a8d2009c0567c40cfe037c2e71af017d0a452ff705e3f1000000008b483045022100bf5fdc86dc5f08a5d5c8e43a8c9d5b1ed8c65562e280007b52b133021acd9acc02205e325d613e555f772802bf413d36ba807892ed1a690a77811d3033b3de226e0a01410429fa713b124484cb2bd7b5557b2c0b9df7b2b1fee61825eadc5ae6c37a9920d38bfccdc7dc3cb0c47d7b173dbc9db8d37db0a33ae487982c59c6f8606e9d1791ffffffff41ed70551dd7e841883ab8f0b16bf04176b7d1480e4f0af9f3d4c3595768d068000000008b4830450221008513ad65187b903aed1102d1d0c47688127658c51106753fed0151ce9c16b80902201432b9ebcb87bd04ceb2de66035fbbaf4bf8b00d1cfe41f1a1f7338f9ad79d210141049d4cf80125bf50be1709f718c07ad15d0fc612b7da1f5570dddc35f2a352f0f27c978b06820edca9ef982c35fda2d255afba340068c5035552368bc7200c1488ffffffff0100093d00000000001976a9148edb68822f1ad580b043c7b3df2e400f8699eb4888ac00000000")); BloomFilter filter = new BloomFilter(10, 0.000001, 0, BloomFlags.UPDATE_NONE); // Match the generation pubkey filter.Insert(ParseHex("04eaafc2314def4ca98ac970241bcab022b9c1e1f4ea423a20f134c876f2c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce13ad1357231a2252247d97a46a91")); // ...and the output address of the 4th transaction filter.Insert(ParseHex("b6efd80d99179f4f4ff6f4dd0a007d018c385d21")); MerkleBlock merkleBlock = new MerkleBlock(block, filter); Assert.True(merkleBlock.Header.GetHash() == block.GetHash()); // We shouldn't match any outpoints (UPDATE_NONE) Assert.True(!filter.Contains(new OutPoint(uint256.Parse("0x147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b"), 0))); Assert.True(!filter.Contains((new OutPoint(uint256.Parse("0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041"), 0)))); }
public bool NotifyTransaction(Transaction transaction, ChainedBlock chainedBlock, MerkleBlock proof) { if(chainedBlock != null) { if(proof == null) throw new ArgumentNullException("proof"); if(proof.Header.GetHash() != chainedBlock.Header.GetHash()) throw new InvalidOperationException("The chained block and the merkle block are different blocks"); if(!proof.PartialMerkleTree.Check(chainedBlock.Header.HashMerkleRoot)) throw new InvalidOperationException("The MerkleBlock does not have the expected merkle root"); if(!proof.PartialMerkleTree.GetMatchedTransactions().Contains(transaction.GetHash())) throw new InvalidOperationException("The MerkleBlock does not contains the input transaction"); } var interesting = false; lock(cs) { foreach(var txin in transaction.Inputs.AsIndexedInputs()) { var key = TrackedOutpoint.GetId(txin.PrevOut); TrackedOutpoint match; if(_TrackedOutpoints.TryGetValue(key, out match)) { TrackedScript parentMetadata; if(_TrackedScripts.TryGetValue(match.TrackedScriptId, out parentMetadata)) { interesting = true; Spent(parentMetadata, txin, match.Coin, chainedBlock, proof); } } } foreach(var txout in transaction.Outputs.AsIndexedOutputs()) { var key = TrackedScript.GetId(txout.TxOut.ScriptPubKey); TrackedScript match; if(_TrackedScripts.TryGetValue(key, out match)) { interesting = true; Received(match, txout, chainedBlock, proof); } } } return interesting; }