Skip to content
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

Added an endpoint to get a list of transactions hex by transaction ids #170

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
26 changes: 25 additions & 1 deletion src/Blockcore.Indexer.Core/Controllers/QueryController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Blockcore.Indexer.Core.Paging;
using Blockcore.Indexer.Core.Settings;
using Blockcore.Indexer.Core.Storage;
using Blockcore.Indexer.Core.Storage.Types;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -17,14 +21,16 @@ public class QueryController : Controller
{
private readonly IPagingHelper paging;
private readonly IStorage storage;
protected readonly IndexerSettings configuration;

/// <summary>
/// Initializes a new instance of the <see cref="QueryController"/> class.
/// </summary>
public QueryController(IPagingHelper paging, IStorage storage)
public QueryController(IPagingHelper paging, IStorage storage, IndexerSettings configuration)
{
this.paging = paging;
this.storage = storage;
this.configuration = configuration;
}

/// <summary>
Expand Down Expand Up @@ -118,6 +124,24 @@ public IActionResult GetTransactionHex(string transactionId)
return OkItem(storage.GetRawTransaction(transactionId));
}

/// <summary>
/// Get a list of transactions in hex format based on the transaction IDs (hash).
/// </summary>
/// <param name="transactionId"></param>
/// <returns></returns>
[HttpPost]
[Route("transactions/hex")]
public IActionResult GetTransactionsHex(IEnumerable<string> transactionIds)
{
if (!configuration.StoreRawTransactions)
return Forbid();

if (transactionIds.Count() > 20)
return BadRequest();

return OkItem(storage.GetRawTransactions(transactionIds));
}


/// <summary>
/// Returns blocks based on the offset and limit. The blocks are sorted from from lowest to highest index. You can use the "link" HTTP header to get dynamic paging links.
Expand Down
2 changes: 2 additions & 0 deletions src/Blockcore.Indexer.Core/Storage/IStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public interface IStorage

string GetRawTransaction(string transactionId);

IEnumerable<string> GetRawTransactions(IEnumerable<string> transactionId);

QueryTransaction GetTransaction(string transactionId);

QueryResult<SyncTransactionInfo> TransactionsByBlock(string hash, int offset, int limit);
Expand Down
12 changes: 12 additions & 0 deletions src/Blockcore.Indexer.Core/Storage/Mongo/MongoData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,18 @@ public string GetRawTransaction(string transactionId)
return null;
}

public IEnumerable<string> GetRawTransactions(IEnumerable<string> transactionId)
{
FilterDefinition<TransactionTable> filter = Builders<TransactionTable>.Filter.In(info => info.TransactionId, transactionId);

var transactions = mongoDb.TransactionTable.Find(filter)
.ToList();

return transactions.Any()
? transactions.Select(t => Encoders.Hex.EncodeData(t.RawTransaction))
: null;
}

public string GetRawBlock(string blockHash)
{
IBlockchainClient client = clientFactory.Create(syncConnection);
Expand Down