Reading Bitcoin source codeOne

1. Enter the bitcoin source code directory, read and compile doc / build-unix.md first, check how bitcoin source code is compiled, understand bitcoin's dependent libraries, you can probably know which bitcoin is used, so that you have a spectrum!

Required libraries:

Library    | Purpose          | Description

------------|------------------|----------------------

libssl      | Crypto          | Random Number Generation, Elliptic Curve Cryptography(毕竟是加密货币,猜都猜到用openssl)

libboost    | Utility          | Library for threading, data structures, etc(武器弹药库)

libevent    | Networking      | OS independent asynchronous networking(异步网络编程必备,为什么还用这么老的库)

Optional dependencies:

Library    | Purpose          | Description

------------|------------------|----------------------

miniupnpc  | UPnP Support    | Firewall-jumping support(用于P2P吗)

libdb4.8    | Berkeley DB      | Wallet storage (only needed when wallet enabled)(钱包存储用的berkeley DB)

qt          | GUI              | GUI toolkit (only needed when GUI enabled)(qt,gui编程的老相好)

protobuf    | Payments in GUI  | Data interchange format used for payment protocol (only needed when GUI enabled)(用于支付协议,不是json吗,gui内部数据传输格式?)

libqrencode | QR codes in GUI  | Optional for generating QR codes (only needed when GUI enabled)(二维码库)

univalue    | Utility          | JSON parsing and encoding (bundled version will be used unless --with-system-univalue passed to configure)(json解析和编码)

libzmq3    | ZMQ notification | Optional, allows generating ZMQ notifications (requires ZMQ version >= 4.x)(用于通知,哪些消息需要用通知的方式?)

2, analyze the main function of src / bitcoind.cpp

int main(int argc, char* argv[])
{

    SetupEnvironment();

    // Connect bitcoind signal handlers

    noui_connect();

    return (AppInit(argc, argv) ? EXIT_SUCCESS : EXIT_FAILURE);

}

The initialization environment SetupEnvironment function is implemented in src / util.cpp

void SetupEnvironment()

{

#ifdef HAVE_MALLOPT_ARENA_MAX

    // glibc-specific: On 32-bit systems set the number of arenas to 1.

    // By default, since glibc 2.10, the C library will create up to two heap

    // arenas per core. This is known to cause excessive virtual address space

    // usage in our usage. Work around it by setting the maximum number of

    // arenas to 1.

    if (sizeof(void*) == 4) {

        mallopt(M_ARENA_MAX, 1);

    }


#endif

    // On most POSIX systems (e.g. Linux, but not BSD) the environment's locale

    // may be invalid, in which case the "C" locale is used as fallback.

#if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__)

    try {

        std::locale(""); // Raises a runtime error if current locale is invalid

    } catch (const std::runtime_error&) {

        setenv("LC_ALL", "C", 1);

    }

#endif

    // The path locale is lazy initialized and to avoid deinitialization errors

    // in multithreading environments, it is set explicitly by the main thread.

    // A dummy locale is used to extract the internal default locale, used by

    // fs::path, which is then used to explicitly imbue the path.

    std::locale loc = fs::path::imbue(std::locale::classic());

    fs::path::imbue(loc);

}

The glibc library will create 2 arenas for each core, and this will cause the problem of insufficient virtual address space for the 32-bit system, so here it is set to 1. The following locale (that is, the system locale setting, that is, the country or regional setting) will determine the current language encoding, date format, number format, and other regional-related settings used by the program. Whether the settings are correct will affect the string processing in the program (How to output wchar_t, format of strftime (), etc.). The last two lines are the localization settings of the file path. Std :: locale :: classic () gets a locale object representing C locale, which is mainly designed for the conversion between wide char and multi bytes .

Next is the noui_connect () function, implemented in the src / noui.cpp file,

void noui_connect()

{

    // Connect bitcoind signal handlers

    uiInterface.ThreadSafeMessageBox.connect(noui_ThreadSafeMessageBox);

    uiInterface.ThreadSafeQuestion.connect(noui_ThreadSafeQuestion);

    uiInterface.InitMessage.connect(noui_InitMessage);

}

Connect to noui_ThreadSafeMessageBox, noui_ThreadSafeQuestion and noui_InitMessage signal objects. When the signal is triggered, these connected functions will be called.

The next call is AppInit, where AppInitMain is the main function of the application, which will be expanded later!

Published 30 original articles · praised 74 · 230,000 views +

Guess you like

Origin blog.csdn.net/ruiyiin/article/details/104633215