| 916 | } |
| 917 | |
| 918 | static RPCMethod pruneblockchain() |
| 919 | { |
| 920 | return RPCMethod{"pruneblockchain", |
| 921 | "Attempts to delete block and undo data up to a specified height or timestamp, if eligible for pruning.\n" |
| 922 | "Requires `-prune` to be enabled at startup. While pruned data may be re-fetched in some cases (e.g., via `getblockfrompeer`), local deletion is irreversible.\n", |
| 923 | { |
| 924 | {"height", RPCArg::Type::NUM, RPCArg::Optional::NO, "The block height to prune up to. May be set to a discrete height, or to a " + UNIX_EPOCH_TIME + "\n" |
| 925 | " to prune blocks whose block time is at least 2 hours older than the provided timestamp."}, |
| 926 | }, |
| 927 | RPCResult{ |
| 928 | RPCResult::Type::NUM, "", "Height of the last block pruned"}, |
| 929 | RPCExamples{ |
| 930 | HelpExampleCli("pruneblockchain", "1000") |
| 931 | + HelpExampleRpc("pruneblockchain", "1000") |
| 932 | }, |
| 933 | [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue |
| 934 | { |
| 935 | ChainstateManager& chainman = EnsureAnyChainman(request.context); |
| 936 | if (!chainman.m_blockman.IsPruneMode()) { |
| 937 | throw JSONRPCError(RPC_MISC_ERROR, "Cannot prune blocks because node is not in prune mode."); |
| 938 | } |
| 939 | |
| 940 | LOCK(cs_main); |
| 941 | Chainstate& active_chainstate = chainman.ActiveChainstate(); |
| 942 | CChain& active_chain = active_chainstate.m_chain; |
| 943 | |
| 944 | int heightParam = request.params[0].getInt<int>(); |
| 945 | if (heightParam < 0) { |
| 946 | throw JSONRPCError(RPC_INVALID_PARAMETER, "Negative block height."); |
| 947 | } |
| 948 | |
| 949 | // Height value more than a billion is too high to be a block height, and |
| 950 | // too low to be a block time (corresponds to timestamp from Sep 2001). |
| 951 | if (heightParam > 1000000000) { |
| 952 | // Add a 2 hour buffer to include blocks which might have had old timestamps |
| 953 | const CBlockIndex* pindex = active_chain.FindEarliestAtLeast(heightParam - TIMESTAMP_WINDOW, 0); |
| 954 | if (!pindex) { |
| 955 | throw JSONRPCError(RPC_INVALID_PARAMETER, "Could not find block with at least the specified timestamp."); |
| 956 | } |
| 957 | heightParam = pindex->nHeight; |
| 958 | } |
| 959 | |
| 960 | unsigned int height = (unsigned int) heightParam; |
| 961 | unsigned int chainHeight = (unsigned int) active_chain.Height(); |
| 962 | if (chainHeight < chainman.GetParams().PruneAfterHeight()) { |
| 963 | throw JSONRPCError(RPC_MISC_ERROR, "Blockchain is too short for pruning."); |
| 964 | } else if (height > chainHeight) { |
| 965 | throw JSONRPCError(RPC_INVALID_PARAMETER, "Blockchain is shorter than the attempted prune height."); |
| 966 | } else if (height > chainHeight - MIN_BLOCKS_TO_KEEP) { |
| 967 | LogDebug(BCLog::RPC, "Attempt to prune blocks close to the tip. Retaining the minimum number of blocks.\n"); |
| 968 | height = chainHeight - MIN_BLOCKS_TO_KEEP; |
| 969 | } |
| 970 | |
| 971 | PruneBlockFilesManual(active_chainstate, height); |
| 972 | return GetPruneHeight(chainman.m_blockman, active_chain).value_or(-1); |
| 973 | }, |
| 974 | }; |
| 975 | } |
nothing calls this directly
no test coverage detected