add negative height to getblock

This commit is contained in:
Alfredo Garcia 2019-12-31 09:16:29 -03:00
parent 5177d2e708
commit 3ac6dd5f5d
2 changed files with 16 additions and 2 deletions

View File

@ -97,5 +97,14 @@ class MerkleBlockTest(BitcoinTestFramework):
result = self.nodes[0].getblock(blockhash, 0)
assert(c in string.hexdigits for c in result) # verbosity 0 returns raw hex
# Test getblock heights including negatives relative to the head
assert_equal(self.nodes[0].getblock("0")["height"], 0)
assert_raises(JSONRPCException, self.nodes[0].getblock, ["108"])
assert_equal(self.nodes[0].getblock("107")["height"], 107)
assert_equal(self.nodes[0].getblock("106")["height"], 106)
assert_equal(self.nodes[0].getblock("-20")["height"], 87)
assert_equal(self.nodes[0].getblock("-107")["height"], 0)
assert_raises(JSONRPCException, self.nodes[0].getblock, ["-108"])
if __name__ == '__main__':
MerkleBlockTest().main()

View File

@ -660,7 +660,7 @@ UniValue getblock(const UniValue& params, bool fHelp)
"If verbosity is 1, returns an Object with information about the block.\n"
"If verbosity is 2, returns an Object with information about the block and information about each transaction. \n"
"\nArguments:\n"
"1. \"hash|height\" (string, required) The block hash or height\n"
"1. \"hash|height\" (string, required) The block hash or height. Height can be negative, relative to the head.\n"
"2. verbosity (numeric, optional, default=1) 0 for hex encoded data, 1 for a json object, and 2 for json object with transaction data\n"
"\nResult (for verbosity = 0):\n"
"\"data\" (string) A string that is serialized, hex-encoded data for the block.\n"
@ -706,7 +706,7 @@ UniValue getblock(const UniValue& params, bool fHelp)
// If height is supplied, find the hash
if (strHash.size() < (2 * sizeof(uint256))) {
// std::stoi allows characters, whereas we want to be strict
regex r("[[:digit:]]+");
regex r("-?[[:digit:]]+");
if (!regex_match(strHash, r)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid block height parameter");
}
@ -719,9 +719,14 @@ UniValue getblock(const UniValue& params, bool fHelp)
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid block height parameter");
}
if(nHeight < 0) {
nHeight += chainActive.Height();
}
if (nHeight < 0 || nHeight > chainActive.Height()) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range");
}
strHash = chainActive[nHeight]->GetBlockHash().GetHex();
}