Skip to content

Commit

Permalink
allow rest/block/height.json
Browse files Browse the repository at this point in the history
changes from review, added integration test
updated cache size


fixed nullptr from merge
  • Loading branch information
BrannonKing committed May 13, 2019
1 parent d7a092a commit d1293d8
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ AC_PREREQ([2.60])
define(_CLIENT_VERSION_MAJOR, 0)
define(_CLIENT_VERSION_MINOR, 12)
define(_CLIENT_VERSION_REVISION, 4)
define(_CLIENT_VERSION_BUILD, 0)
define(_CLIENT_VERSION_BUILD, 1)
define(_CLIENT_VERSION_IS_RELEASE, true)
define(_COPYRIGHT_YEAR, 2016)
define(_COPYRIGHT_HOLDERS,[The %s developers])
Expand Down
6 changes: 6 additions & 0 deletions doc/REST-interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ Given a transaction hash: returns a transaction in binary, hex-encoded binary, o
For full TX query capability, one must enable the transaction index via "txindex=1" command line / configuration option.

#### Blocks
`GET /rest/block/tip.<bin|hex|json>`
`GET /rest/block/<BLOCK-HASH>.<bin|hex|json>`
`GET /rest/block/<BLOCK-HEIGHT>.<bin|hex|json>`
`GET /rest/block/notxdetails/tip.<bin|hex|json>`
`GET /rest/block/notxdetails/<BLOCK-HASH>.<bin|hex|json>`
`GET /rest/block/notxdetails/<BLOCK-HEIGHT>.<bin|hex|json>`

Given a block hash: returns a block, in binary, hex-encoded binary or JSON formats.
You can give a block height instead of a hash. Height 0 is not available,
but can be negative to go back that many blocks from the tip.

The HTTP request and response are both handled entirely in-memory, thus making maximum memory usage at least 2.66MB (1 MB max block, plus hex encoding) per request.

Expand Down
2 changes: 1 addition & 1 deletion src/claimtrie.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ class CClaimTrie
{
public:
CClaimTrie(bool fMemory = false, bool fWipe = false, int nProportionalDelayFactor = 32)
: db(GetDataDir() / "claimtrie", 100, fMemory, fWipe, false), nCurrentHeight(0),
: db(GetDataDir() / "claimtrie", 20 * 1024 * 1024, fMemory, fWipe, false), nCurrentHeight(0),
nExpirationTime(Params().GetConsensus().nOriginalClaimExpirationTime),
nProportionalDelayFactor(nProportionalDelayFactor),
root(uint256S("0000000000000000000000000000000000000000000000000000000000000001"))
Expand Down
2 changes: 1 addition & 1 deletion src/clientversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#define CLIENT_VERSION_MAJOR 0
#define CLIENT_VERSION_MINOR 12
#define CLIENT_VERSION_REVISION 4
#define CLIENT_VERSION_BUILD 0
#define CLIENT_VERSION_BUILD 1

//! Set to true for release, false for prerelease or test build
#define CLIENT_VERSION_IS_RELEASE true
Expand Down
23 changes: 17 additions & 6 deletions src/rest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,17 +210,28 @@ static bool rest_block(HTTPRequest* req,
const RetFormat rf = ParseDataFormat(hashStr, strURIPart);

uint256 hash;
if (!ParseHashStr(hashStr, hash))
return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);

long int blockHeight = 0;
if (hashStr != "tip") {
blockHeight = hashStr.size() < 12 ? std::strtol(hashStr.c_str(), NULL, 10) : 0;
if (blockHeight == 0 && !ParseHashStr(hashStr, hash))
return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash or block height: " + hashStr);
}
CBlock block;
CBlockIndex* pblockindex = NULL;
{
LOCK(cs_main);
if (mapBlockIndex.count(hash) == 0)
if (blockHeight < 0) // negative block heights take us back from current tip
blockHeight += chainActive.Height();
if (blockHeight > 0 && blockHeight <= chainActive.Height())
pblockindex = chainActive[blockHeight];
else if (blockHeight != 0)
return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash or block height: " + hashStr);
else if (hashStr == "tip")
pblockindex = chainActive.Tip();
else if (mapBlockIndex.count(hash))
pblockindex = mapBlockIndex[hash];
if (!pblockindex)
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");

pblockindex = mapBlockIndex[hash];
if (fHavePruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0)
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not available (pruned data)");

Expand Down

0 comments on commit d1293d8

Please sign in to comment.