From 5b2f35167f3060826e310145a34bc55bb2c8d3c1 Mon Sep 17 00:00:00 2001 From: Chris Howie Date: Fri, 6 May 2011 12:14:35 -0400 Subject: [PATCH 1/2] Add listsinceblock command for retrieving all wallet transactions in blocks after the specified block --- src/rpc.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/rpc.cpp b/src/rpc.cpp index 6f951b743..c6aa68d21 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -1230,6 +1230,43 @@ Value listaccounts(const Array& params, bool fHelp) return ret; } +Value listsinceblock(const Array& params, bool fHelp) +{ + if (fHelp) + throw runtime_error( + "listsinceblock [blockid]\n" + "Get all transactions in blocks since block [blockid], or all transactions if omitted"); + + CBlockIndex *pindex = NULL; + + if (params.size() > 0) + { + uint256 blockId = 0; + + blockId.SetHex(params[0].get_str()); + pindex = CBlockLocator(blockId).GetBlockIndex(); + } + + int depth = pindex ? (1 + nBestHeight - pindex->nHeight) : -1; + + Array transactions; + + CRITICAL_BLOCK(pwalletMain->cs_mapWallet) + for (map::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); it++) + { + CWalletTx tx = (*it).second; + + if (depth == -1 || tx.GetDepthInMainChain() < depth) + ListTransactions(tx, "*", 1, true, transactions); + } + + Object ret; + ret.push_back(Pair("transactions", transactions)); + ret.push_back(Pair("lastblock", hashBestChain.GetHex())); + + return ret; +} + Value gettransaction(const Array& params, bool fHelp) { if (fHelp || params.size() != 1) @@ -1470,6 +1507,7 @@ pair pCallTable[] = make_pair("getwork", &getwork), make_pair("listaccounts", &listaccounts), make_pair("settxfee", &settxfee), + make_pair("listsinceblock", &listsinceblock), }; map mapCallTable(pCallTable, pCallTable + sizeof(pCallTable)/sizeof(pCallTable[0])); From 76aed0141cbc4827ef6f168b6af5b2894046ec0b Mon Sep 17 00:00:00 2001 From: Chris Howie Date: Wed, 6 Jul 2011 16:19:52 -0400 Subject: [PATCH 2/2] listsinceblock now shows txns with 0 confirms, as well as allows the lastblock return property to be targeted to the block with the specified depth --- src/rpc.cpp | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/rpc.cpp b/src/rpc.cpp index c6aa68d21..09be63203 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -1234,10 +1234,11 @@ Value listsinceblock(const Array& params, bool fHelp) { if (fHelp) throw runtime_error( - "listsinceblock [blockid]\n" + "listsinceblock [blockid] [target-confirmations]\n" "Get all transactions in blocks since block [blockid], or all transactions if omitted"); CBlockIndex *pindex = NULL; + int target_confirms = 1; if (params.size() > 0) { @@ -1247,6 +1248,14 @@ Value listsinceblock(const Array& params, bool fHelp) pindex = CBlockLocator(blockId).GetBlockIndex(); } + if (params.size() > 1) + { + target_confirms = params[1].get_int(); + + if (target_confirms < 1) + throw JSONRPCError(-8, "Invalid parameter"); + } + int depth = pindex ? (1 + nBestHeight - pindex->nHeight) : -1; Array transactions; @@ -1257,12 +1266,31 @@ Value listsinceblock(const Array& params, bool fHelp) CWalletTx tx = (*it).second; if (depth == -1 || tx.GetDepthInMainChain() < depth) - ListTransactions(tx, "*", 1, true, transactions); + ListTransactions(tx, "*", 0, true, transactions); + } + + uint256 lastblock; + + if (target_confirms == 1) + { + printf("oops!\n"); + lastblock = hashBestChain; + } + else + { + int target_height = pindexBest->nHeight + 1 - target_confirms; + + CBlockIndex *block; + for (block = pindexBest; + block && block->nHeight > target_height; + block = block->pprev); + + lastblock = block ? block->GetBlockHash() : 0; } Object ret; ret.push_back(Pair("transactions", transactions)); - ret.push_back(Pair("lastblock", hashBestChain.GetHex())); + ret.push_back(Pair("lastblock", lastblock.GetHex())); return ret; } @@ -2168,6 +2196,7 @@ int CommandLineRPC(int argc, char *argv[]) if (strMethod == "listtransactions" && n > 1) ConvertTo(params[1]); if (strMethod == "listtransactions" && n > 2) ConvertTo(params[2]); if (strMethod == "listaccounts" && n > 0) ConvertTo(params[0]); + if (strMethod == "listsinceblock" && n > 1) ConvertTo(params[1]); if (strMethod == "sendmany" && n > 1) { string s = params[1].get_str();