Skip to content

Commit

Permalink
Rewrite getspentinfo method
Browse files Browse the repository at this point in the history
  • Loading branch information
volbil committed Jun 4, 2020
1 parent 29b9823 commit 37a137e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
1 change: 0 additions & 1 deletion src/rpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "rescanblockchain", 0, "start_height"},
{ "rescanblockchain", 1, "stop_height"},
{ "getaddressutxos", 1, "amount" },
{ "getspentinfo", 0, "txid_index"},
};

class CRPCConvertTable
Expand Down
42 changes: 25 additions & 17 deletions src/rpc/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ UniValue getblockhashes(const JSONRPCRequest& request)

UniValue getspentinfo(const JSONRPCRequest& request)
{
if (request.fHelp || request.params.size() != 1 || !request.params[0].isObject())
if (request.fHelp || request.params.size() != 1)
throw std::runtime_error(
"getspentinfo\n"
"\nReturns the txid and index where an output is spent.\n"
Expand All @@ -755,29 +755,37 @@ UniValue getspentinfo(const JSONRPCRequest& request)
+ HelpExampleRpc("getspentinfo", "{\"txid\": \"0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9\", \"index\": 0}")
);

UniValue txidValue = find_value(request.params[0].get_obj(), "txid");
UniValue indexValue = find_value(request.params[0].get_obj(), "index");
if (!request.params[0].isStr()) {
throw JSONRPCError(RPC_TYPE_ERROR, "Txid must be a string");
}

uint256 txid(ParseHashV(request.params[0].get_str(), "txid"));
UniValue result(UniValue::VARR);
CTransactionRef tx;
uint256 hashBlock;

if (!txidValue.isStr() || !indexValue.isNum()) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid txid or index");
if (!GetTransaction(txid, tx, Params().GetConsensus(), hashBlock)) {
throw JSONRPCError(RPC_INTERNAL_ERROR, std::string("Unexpected internal error (tx index seems corrupt)"));
}

uint256 txid = ParseHashV(txidValue, "txid");
int outputIndex = indexValue.get_int();
for (unsigned int i = 0; i < tx->vout.size(); ++i) {
UniValue obj(UniValue::VOBJ);
CSpentIndexKey key(txid, i);
CSpentIndexValue value;

CSpentIndexKey key(txid, outputIndex);
CSpentIndexValue value;
if (!GetSpentIndex(key, value)) {
obj.pushKV("spent", false);
} else {
obj.pushKV("spent", true);
obj.pushKV("txid", txid.GetHex());
obj.pushKV("height", value.blockHeight);
obj.pushKV("vin", (int)value.inputIndex);
}

if (!GetSpentIndex(key, value)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unable to get spent info");
result.push_back(obj);
}

UniValue obj(UniValue::VOBJ);
obj.pushKV("txid", value.txid.GetHex());
obj.pushKV("index", (int)value.inputIndex);
obj.pushKV("height", value.blockHeight);

return obj;
return result;
}

UniValue getaddresstxids(const JSONRPCRequest& request)
Expand Down

0 comments on commit 37a137e

Please sign in to comment.