Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
using System.Threading.Tasks;
using Blockcore.Controllers;
using Blockcore.Controllers.Models;
using Blockcore.Features.BlockStore.Api.Contollers;
using Blockcore.Features.BlockStore.Api.Models;
using Microsoft.Extensions.Logging;

namespace Blockcore.Features.BlockStore.Controllers
namespace Blockcore.Features.BlockStore.Api
{
/// <summary>Rest client for <see cref="BlockStoreController"/>.</summary>
public interface IBlockStoreClient : IRestApiClientBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Blockcore.Base;
using Blockcore.Controllers.Models;
using Blockcore.Features.BlockStore.AddressIndexing;
using Blockcore.Features.BlockStore.Api.Models;
using Blockcore.Features.BlockStore.Models;
using Blockcore.Interfaces;
using Blockcore.Utilities;
Expand All @@ -12,16 +13,8 @@
using Microsoft.Extensions.Logging;
using NBitcoin;

namespace Blockcore.Features.BlockStore.Controllers
namespace Blockcore.Features.BlockStore.Api.Contollers
{
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";
}

/// <summary>Controller providing operations on a blockstore.</summary>
[ApiController]
Expand Down
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;
}
}
}
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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<ItemGroup>
<ProjectReference Include="..\..\External\NBitcoin\NBitcoin.csproj" />
<ProjectReference Include="..\..\Blockcore\Blockcore.csproj" />
<ProjectReference Include="..\Blockcore.Features.RPC\Blockcore.Features.RPC.csproj" />
Comment thread
dangershony marked this conversation as resolved.
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/Features/Blockcore.Features.RPC/RPCClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ blockchain getchaintips
blockchain getdifficulty
blockchain getmempoolinfo
blockchain getrawmempool Yes
blockchain gettxout Yes
blockchain gettxoutproof
blockchain gettxout Yes
blockchain gettxoutproof Yes
blockchain verifytxoutproof
blockchain gettxoutsetinfo
blockchain verifychain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Blockcore.Base;
using Blockcore.Controllers.Models;
using Blockcore.Features.BlockStore.AddressIndexing;
using Blockcore.Features.BlockStore.Controllers;
using Blockcore.Features.BlockStore.Api.Contollers;
using Blockcore.Features.BlockStore.Models;
using Blockcore.Interfaces;
using Blockcore.Tests.Common;
Expand Down