| 926 | } |
| 927 | |
| 928 | UniValue pruneblockchain(const UniValue& params, bool fHelp) |
| 929 | { |
| 930 | if (fHelp || params.size() != 1) |
| 931 | throw std::runtime_error( |
| 932 | "pruneblockchain\n" |
| 933 | "\nArguments:\n" |
| 934 | "1. \"height\" (numeric, required) The block height to prune up to. May be set to a discrete height, or a unix timestamp\n" |
| 935 | " to prune blocks whose block time is at least 2 hours older than the provided timestamp.\n" |
| 936 | "\nResult:\n" |
| 937 | "n (numeric) Height of the last block pruned.\n" |
| 938 | "\nExamples:\n" |
| 939 | + HelpExampleCli("pruneblockchain", "1000") |
| 940 | + HelpExampleRpc("pruneblockchain", "1000")); |
| 941 | |
| 942 | if (!fPruneMode) |
| 943 | throw JSONRPCError(RPC_MISC_ERROR, "Cannot prune blocks because node is not in prune mode."); |
| 944 | |
| 945 | LOCK(cs_main); |
| 946 | |
| 947 | int heightParam = params[0].get_int(); |
| 948 | if (heightParam < 0) |
| 949 | throw JSONRPCError(RPC_INVALID_PARAMETER, "Negative block height."); |
| 950 | |
| 951 | // Height value more than a billion is too high to be a block height, and |
| 952 | // too low to be a block time (corresponds to timestamp from Sep 2001). |
| 953 | if (heightParam > 1000000000) { |
| 954 | // Add a 2 hour buffer to include blocks which might have had old timestamps |
| 955 | CBlockIndex* pindex = chainActive.FindEarliestAtLeast(heightParam - TIMESTAMP_WINDOW); |
| 956 | if (!pindex) { |
| 957 | throw JSONRPCError(RPC_INVALID_PARAMETER, "Could not find block with at least the specified timestamp."); |
| 958 | } |
| 959 | heightParam = pindex->nHeight; |
| 960 | } |
| 961 | |
| 962 | unsigned int height = (unsigned int) heightParam; |
| 963 | unsigned int chainHeight = (unsigned int) chainActive.Height(); |
| 964 | if (height < Params().PruneAfterHeight() || chainHeight < Params().PruneAfterHeight()) |
| 965 | throw JSONRPCError(RPC_MISC_ERROR, "Blockchain is too short for pruning."); |
| 966 | else if (height > chainHeight) |
| 967 | throw JSONRPCError(RPC_INVALID_PARAMETER, "Blockchain is shorter than the attempted prune height."); |
| 968 | else if (height > chainHeight - MIN_BLOCKS_TO_KEEP) { |
| 969 | LogPrintf("pruneblockchain: %s\n", "Attempt to prune blocks close to the tip. Retaining the minimum number of blocks."); |
| 970 | height = chainHeight - MIN_BLOCKS_TO_KEEP; |
| 971 | } |
| 972 | |
| 973 | PruneBlockFilesManual(height); |
| 974 | return uint64_t(height); |
| 975 | } |
| 976 | |
| 977 | UniValue getaccountinfo(const UniValue& params, bool fHelp) |
| 978 | { |
nothing calls this directly
no test coverage detected