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 <calculator.h>6#include <init.capnp.h>7#include <init.capnp.proxy.h> // NOLINT(misc-include-cleaner) // IWYU pragma: keep9#include <charconv>10#include <cstring>11#include <fstream>12#include <functional>13#include <iostream>14#include <kj/async.h>15#include <kj/common.h>16#include <kj/memory.h>17#include <memory>18#include <mp/proxy-io.h>19#include <stdexcept>20#include <string>21#include <system_error>22#include <utility>24class CalculatorImpl : public Calculator25{26public:27CalculatorImpl(std::unique_ptr<Printer> printer) : m_printer(std::move(printer)) {}28void solveEquation(const std::string& eqn) override { m_printer->print("Wow " + eqn + ", that's a tough one.\n"); }29std::unique_ptr<Printer> m_printer;30};32class InitImpl : public Init33{34public:35std::unique_ptr<Calculator> makeCalculator(std::unique_ptr<Printer> printer) override36{37return std::make_unique<CalculatorImpl>(std::move(printer));38}39};41// Exercises deprecated log callback signature42static void LogPrint(bool raise, const std::string& message)43{44if (raise) throw std::runtime_error(message);45std::ofstream("debug.log", std::ios_base::app) << message << std::endl;46}48int main(int argc, char** argv)49{50if (argc != 2) {51std::cout << "Usage: mpcalculator <fd>\n";52return 1;53}54int fd;55if (std::from_chars(argv[1], argv[1] + strlen(argv[1]), fd).ec != std::errc{}) {56std::cerr << argv[1] << " is not a number or is larger than an int\n";57return 1;58}59mp::EventLoop loop("mpcalculator", LogPrint);60std::unique_ptr<Init> init = std::make_unique<InitImpl>();61mp::ServeStream<InitInterface>(loop, fd, *init);62loop.loop();63return 0;64}
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.