Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <init.capnp.h>6#include <init.capnp.proxy.h>8#include <cstring> // IWYU pragma: keep9#include <filesystem>10#include <fstream>11#include <future>12#include <iostream>13#include <kj/async.h>14#include <kj/common.h>15#include <memory>16#include <mp/proxy-io.h>17#include <mp/util.h>18#include <stdexcept>19#include <string>20#include <thread>21#include <tuple>22#include <vector>24namespace fs = std::filesystem;26static auto Spawn(mp::EventLoop& loop, const std::string& process_argv0, const std::string& new_exe_name)27{28int pid;29const int fd = mp::SpawnProcess(pid, [&](int fd) -> std::vector<std::string> {30fs::path path = process_argv0;31path.remove_filename();32path.append(new_exe_name);33return {path.string(), std::to_string(fd)};34});35return std::make_tuple(mp::ConnectStream<InitInterface>(loop, fd), pid);36}38static void LogPrint(mp::LogMessage log_data)39{40if (log_data.level == mp::Log::Raise) throw std::runtime_error(log_data.message);41std::ofstream("debug.log", std::ios_base::app) << log_data.message << std::endl;42}44int main(int argc, char** argv)45{46if (argc != 1) {47std::cout << "Usage: mpexample\n";48return 1;49}51std::promise<mp::EventLoop*> promise;52std::thread loop_thread([&] {53mp::EventLoop loop("mpexample", LogPrint);54promise.set_value(&loop);55loop.loop();56});57mp::EventLoop* loop = promise.get_future().get();59auto [printer_init, printer_pid] = Spawn(*loop, argv[0], "mpprinter");60auto [calc_init, calc_pid] = Spawn(*loop, argv[0], "mpcalculator");61auto calc = calc_init->makeCalculator(printer_init->makePrinter());62while (true) {63std::string eqn;64std::cout << "Enter the equation, or \"exit\" to quit: ";65std::getline(std::cin, eqn);66if (eqn == "exit") break;67calc->solveEquation(eqn);68}69calc.reset();70calc_init.reset();71mp::WaitProcess(calc_pid);72printer_init.reset();73mp::WaitProcess(printer_pid);74loop_thread.join();75std::cout << "Bye!" << std::endl;76return 0;77}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.