-
Notifications
You must be signed in to change notification settings - Fork 89
Added gettxoutproof RPC #160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
src/Features/Blockcore.Features.BlockStore/Api/Controllers/BlockStoreRPCController.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| using System.Collections.Generic; | ||
| using Blockcore.Consensus; | ||
| using Blockcore.Controllers; | ||
| using Blockcore.Features.RPC; | ||
| using Blockcore.Features.RPC.Exceptions; | ||
| using Blockcore.Interfaces; | ||
| using Blockcore.Primitives; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using NBitcoin; | ||
|
|
||
| namespace Blockcore.Features.BlockStore.Api.Controllers | ||
| { | ||
| /// <summary> | ||
| /// Controller providing RPC operations on a watch-only wallet. | ||
| /// </summary> | ||
| public class BlockStoreRPCController : FeatureController | ||
| { | ||
| /// <summary>Consensus manager class.</summary> | ||
| private readonly IConsensusManager consensusManager; | ||
|
|
||
| /// <summary>Thread safe access to the best chain of block headers from genesis.</summary> | ||
| private readonly ChainIndexer chainIndexer; | ||
|
|
||
| /// <summary>Provides access to the block store database.</summary> | ||
| private readonly IBlockStore blockStore; | ||
|
|
||
| /// <inheritdoc /> | ||
| public BlockStoreRPCController( | ||
| IFullNode fullNode, | ||
| IConsensusManager consensusManager, | ||
| ChainIndexer chainIndexer, | ||
| Network network, | ||
| IBlockStore blockStore) : base(fullNode: fullNode, consensusManager: consensusManager, chainIndexer: chainIndexer, network: network) | ||
| { | ||
| this.consensusManager = consensusManager; | ||
| this.chainIndexer = chainIndexer; | ||
| this.blockStore = blockStore; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// By default this function only works when there is an unspent output in the utxo for this transaction. | ||
| /// To make it work, you need to maintain a transaction index, using the -txindex command line option. | ||
| /// </summary> | ||
| /// <param name="txids">The txids to filter</param> | ||
| /// <param name="blockhash">If specified, looks for txid in the block with this hash</param> | ||
| /// <returns></returns> | ||
| [ActionName("gettxoutproof")] | ||
| [ActionDescription("Checks if transactions are within block. Returns proof of transaction inclusion (raw MerkleBlock).")] | ||
| 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; | ||
| } | ||
|
|
||
| internal ChainedHeader GetTransactionBlock(uint256 trxid) | ||
| { | ||
| ChainedHeader block = null; | ||
| uint256 blockid = this.blockStore?.GetBlockIdByTransactionId(trxid); | ||
| if (blockid != null) | ||
| { | ||
| block = this.chainIndexer?.GetHeader(blockid); | ||
| } | ||
| return block; | ||
| } | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/Features/Blockcore.Features.BlockStore/Api/Models/BlockStoreRouteEndPoint.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| namespace Blockcore.Features.BlockStore.Api.Models | ||
| { | ||
| public static class BlockStoreRouteEndPoint | ||
| { | ||
| public const string GetAddressesBalances = "getaddressesbalances"; | ||
| public const string GetVerboseAddressesBalances = "getverboseaddressesbalances"; | ||
| public const string GetAddressIndexerTip = "addressindexertip"; | ||
| public const string GetBlock = "block"; | ||
| public const string GetBlockCount = "GetBlockCount"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.