|
| 1 | +using System.Collections.Generic; |
| 2 | +using Blockcore.Consensus; |
| 3 | +using Blockcore.Controllers; |
| 4 | +using Blockcore.Features.RPC; |
| 5 | +using Blockcore.Features.RPC.Exceptions; |
| 6 | +using Blockcore.Interfaces; |
| 7 | +using Blockcore.Primitives; |
| 8 | +using Microsoft.AspNetCore.Mvc; |
| 9 | +using NBitcoin; |
| 10 | + |
| 11 | +namespace Blockcore.Features.BlockStore.Api.Controllers |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Controller providing RPC operations on a watch-only wallet. |
| 15 | + /// </summary> |
| 16 | + public class BlockStoreRPCController : FeatureController |
| 17 | + { |
| 18 | + /// <summary>Consensus manager class.</summary> |
| 19 | + private readonly IConsensusManager consensusManager; |
| 20 | + |
| 21 | + /// <summary>Thread safe access to the best chain of block headers from genesis.</summary> |
| 22 | + private readonly ChainIndexer chainIndexer; |
| 23 | + |
| 24 | + /// <summary>Provides access to the block store database.</summary> |
| 25 | + private readonly IBlockStore blockStore; |
| 26 | + |
| 27 | + /// <inheritdoc /> |
| 28 | + public BlockStoreRPCController( |
| 29 | + IFullNode fullNode, |
| 30 | + IConsensusManager consensusManager, |
| 31 | + ChainIndexer chainIndexer, |
| 32 | + Network network, |
| 33 | + IBlockStore blockStore) : base(fullNode: fullNode, consensusManager: consensusManager, chainIndexer: chainIndexer, network: network) |
| 34 | + { |
| 35 | + this.consensusManager = consensusManager; |
| 36 | + this.chainIndexer = chainIndexer; |
| 37 | + this.blockStore = blockStore; |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// By default this function only works when there is an unspent output in the utxo for this transaction. |
| 42 | + /// To make it work, you need to maintain a transaction index, using the -txindex command line option. |
| 43 | + /// </summary> |
| 44 | + /// <param name="txids">The txids to filter</param> |
| 45 | + /// <param name="blockhash">If specified, looks for txid in the block with this hash</param> |
| 46 | + /// <returns></returns> |
| 47 | + [ActionName("gettxoutproof")] |
| 48 | + [ActionDescription("Checks if transactions are within block. Returns proof of transaction inclusion (raw MerkleBlock).")] |
| 49 | + public MerkleBlock GetTxOutProof(string[] txids, string blockhash = "") |
| 50 | + { |
| 51 | + List<uint256> transactionIds = new List<uint256>(); |
| 52 | + ChainedHeaderBlock block = null; |
| 53 | + foreach (var txString in txids) |
| 54 | + { |
| 55 | + transactionIds.Add(uint256.Parse(txString)); |
| 56 | + } |
| 57 | + |
| 58 | + if (!string.IsNullOrEmpty(blockhash)) |
| 59 | + { |
| 60 | + // Loop through txids and veryify that the transaction is in the block. |
| 61 | + foreach (var transactionId in transactionIds) |
| 62 | + { |
| 63 | + var hashBlock = uint256.Parse(blockhash); |
| 64 | + ChainedHeader chainedHeader = this.GetTransactionBlock(transactionId); |
| 65 | + if (chainedHeader.HashBlock != hashBlock) |
| 66 | + { |
| 67 | + throw new RPCServerException(RPCErrorCode.RPC_INVALID_ADDRESS_OR_KEY, "Not all transactions found in specified or retrieved block"); |
| 68 | + } |
| 69 | + if (block == null && chainedHeader.BlockDataAvailability == BlockDataAvailabilityState.BlockAvailable) // Only get the block once. |
| 70 | + { |
| 71 | + block = this.consensusManager.GetBlockData(chainedHeader.HashBlock); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + // Loop through txids and try to find which block they're in. Exit loop once a block is found. |
| 78 | + foreach (var transactionId in transactionIds) |
| 79 | + { |
| 80 | + ChainedHeader chainedHeader = this.GetTransactionBlock(transactionId); |
| 81 | + if (chainedHeader.BlockDataAvailability == BlockDataAvailabilityState.BlockAvailable) |
| 82 | + { |
| 83 | + block = this.consensusManager.GetBlockData(chainedHeader.HashBlock); |
| 84 | + break; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + if (block == null) |
| 89 | + { |
| 90 | + throw new RPCServerException(RPCErrorCode.RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); |
| 91 | + } |
| 92 | + var result = new MerkleBlock(block.Block, transactionIds.ToArray()); |
| 93 | + return result; |
| 94 | + } |
| 95 | + |
| 96 | + internal ChainedHeader GetTransactionBlock(uint256 trxid) |
| 97 | + { |
| 98 | + ChainedHeader block = null; |
| 99 | + uint256 blockid = this.blockStore?.GetBlockIdByTransactionId(trxid); |
| 100 | + if (blockid != null) |
| 101 | + { |
| 102 | + block = this.chainIndexer?.GetHeader(blockid); |
| 103 | + } |
| 104 | + return block; |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments