Monad 백서

Monad

원문

bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 18쪽

, and `BLOCK_VERIFIED`) do not occur inside a block * Note that the block flow tag is *not* the block number. This is because at the time events are seen, the blocks are in the "proposed" state, and the consensus algorithm has not finished voting on whether or not the block will be included in the canonical blockchain (this is discussed extensively in the next section). Until a block becomes finalized, the only unambiguous way to refer to it is by its unique ID, which is 32-byte hash value (which can be read from the `BLOCK_START` payload); thus the block flow tag is also a form of compression * Having the sequence number allows us to rewind the iterator to the start of the block, if we start observing the event sequence in the middle of a block (e.g., if the reader starts up after the execution daemon). An example of this (and a detailed explanation of it) can be found in the `eventwatch.c` example program, in the `find_initial_iteration_point` function # Building the C example program Source: https://docs.monad.xyz/execution-events/getting-started/c The C and C++ languages do not have a standard package manager, so using a third-party library requires the programmer to come up with their own dependency management scheme. Before we perform the first step of the guide, we'll discuss the options the user has for integrating the SDK library dependency into their project. Then we'll pick one of these options and build the example program using that method. A second method will be briefly shown at the end. <Info> If you are not familiar with CMake, you may want to read CMake's ["Using Dependencies Guide"](https://cmake.org/cmake/help/latest/guide/using-dependencies/index.html) first </Info> ### Where is the SDK source code? The execution event C SDK lives in the same source code repository as the execution daemon ([here](https://github.com/category-labs/monad)), in the subdirectory [`category/event`](https://github.com/category-labs/monad/tree/main/category/event). It has a separate `CMakeLists.txt` file that can act as a top-level project file, so that users do not need to build the full execution project in order to compile it. The SDK's build system produces a library called `libmonad_event.a`, or `libmonad_event.so` if you prefer shared libraries. You will also need the public header files. ### How can my code use `libmonad_event.a`? Here are three different options: 1. **Precompiled library** - you could build the library yourself and store the library file (and its headers) somewhere, then import it into your build system manually. The SDK build system also creates a CMake "config" file for use with [`find_package`](https://cmake.org/cmake/help/latest/command/find_package.html) to help import it, if you are also using CMake The other two options assume your project is also using CMake: 2. **CMake subproject integration** - your CMake project can include the SDK as a subproject. In this case, you download the source code of the execution repository as part of your own project, and call the CMake function: ``` add_subdirectory(<path-to-monad-repo>/category/event) ``` This will add the SDK's library target (called `monad_event`) into your parent CMake project. One way to add the SDK code to your build is to use a [git submodule](https://git-scm.com/book/en/v2/Git-Tools-Submodules). Another way is to use CMake's [`FetchContent`](https://cmake.org/cmake/help/latest/module/FetchContent.html) module. The three main differences between these approaches are: 1. By default, `FetchContent` will clone a git repository into your CMake build tree at build configuration time, whereas a git submodule integrates into your source tree at the repository level 2. With `FetchContent`, the version you check out is specified by the `GIT_TAG` you specify in a `CMakeLists.txt` file; for `git submodule`, it is managed via git commands 3. If the content you are fetching has its own CMake buildsystem (as the C SDK does), `FetchContent` will automatically call `add_subdirectory` to add it to the current project; in the git submodule approach, you need to do this manually 3. **CMake `ExternalProject` integration** - CMake's [`ExternalProject`](https://cmake.org/cmake/help/latest/module/ExternalProject.html) module is similar to `FetchContent`, but is more isolated; this will build and install the SDK into a "staging" directory somewhere in your CMake build tree. This uses a completely separate CMake invocation, so it will not add the SDK's CMake project into your own. This means, for example, that you will not automatically have a `monad_event` library target in your own CMake project -- you would need to create one as an imported target. `ExternalProject` helps isolate your build system from the SDK's build system, ensuring that CMake configuration and variables from the SDK can't "leak" into your parent project In this guide, we'll use the `FetchContent` approach. This encapsulates the entire process as a simple, all-in-one `CMakeLists.txt` file, and we comment that file to explain everything you need to know. This is the clear best choice for our tiny "Getting started" example program, but it might not be best for your real project. At the end of this guide, an alternative method using `find_package` is briefly shown. ## Building the example program with `FetchContent` ### Step 1: install prerequisite development packages In addition to CMake (at least version 3.23) and git, we will need a recent C compiler and two third-party libraries. We will also use [curl](https://curl.se/docs/manpage.html) to download some files. #### Required C compiler The C SDK uses some recent features from C23, and requires either gcc-13 or clang-19. If the default compiler found by CMake is too old, you will need to specify an alternative C compiler by setting the `CC` environment variable or using a CMake [toolchain file](https://cmake.org/cmake/help/latest/variable/CMAKE_TOOLCHAIN_FILE.html). The default compiler chosen by CMake is usually the one reported by the output of the command `cc -v`. If you need to use a different one, you can use the bash syntax `VAR=VALUE <command>` for setting an environment variable in the scope of the following command, e.g.: ```shell theme={null} $ CC=gcc-15 cmake <args> ``` #### Required C++ compiler C++ is not used in this example, but there are some optional C++ components in the CMake project. Consequently, you must have a C++ compiler installed or the CMake configuration step will fail. <Info title="C++ in the SDK"> The SDK is written in pure C, except for some C++ header files that can be used to "pretty-print" event types using the `<format>` library. These are not part of the example program, and they require full C++23 support for formatting ranges (the `__cpp_lib_format_ranges` feature test macro) which were added to libstdc++ in gcc version 15.2. This is a recent release: the first time gcc 15.2 appeared in the Ubuntu package repositories was in Ubuntu 25.10. You can also use clang with libc++, the LLVM implementation of the C++ standard library, which has had range formatting support since version 19. An example of building a C++ program with clang-19 is shown as the optional last step of the "Getting started" guide, when we build the `monad-event-cli` utility. </Info> #### Required third-party libraries | Requirement | Ubuntu package name | What is it used for? | | - | - | - | | zstd library | libzstd-dev | Snapshot event ring files are compressed with zstd; `libzstd` is needed to decompress them | | libhugetlbfs | libhugetlbfs-dev | `libhugetlbfs` is used to locate the optimal hugetlbfs mount point to create event ring shared memory files | `libzstd` is a hard requirement; `libhugetlbfs` is optional but is expected by default on Linux. It can be turned off manually by setting the CMake option `MONAD_EVENT_USE_LIBHUGETLBFS` to `OFF`. #### macOS compatiblity Real-time data requires a Linux host (because the execution daemon itself does), but you can compile and run the example program on historical data using macOS. This allows you to "try before you buy" and explore the SDK before going through the trouble of setting up a Monad node on Linux. In this case, you do not need `libhugetlbfs` (which is a Linux-only library), but you will need a recent XCode toolchain, the `libzstd` compression library, and CMake. The last two are not included in the default XCode development tools, so you'll probably want to use [Homebrew](https://brew.sh/) or [MacPorts](https://www.macports.org/) to install these onto your system. As for XCode itself, any version since 16.3 should work, but it has only been tested with version 23.2 (the real requirement is for Apple Clang 17).[^1] [^1]: "Apple Clang" is based on, but different than, vanilla LLVM [Clang](https://clang.llvm.org/). Apple Clang 17 is based on LLVM/Clang 19, thus the discrepancy in the requirements table. ### Step 2: download the example program First, create a new directory and download the example program source file into it. We'll use the example directory `~/src/event-sdk-example-c` ```shell theme={null} $ mkdir -p ~/src/event-sdk-example-c $ cd ~/src/event-sdk-example-c $ curl -O https://raw.githubusercontent.com/category-labs/monad/refs/tags/release/exec-events-sdk-v1.1/category/event/example/eventwatch.c ``` You should now have a file called `eventwatch.c` in your new directory. ### Step 3: add a `CMakeLists.txt` build file Create a `CMakeLists.txt` file in the directory alongside `eventwatch.c` and copy these contents into it: ```CMakeLists.txt theme={null} cmake_minimum_required(VERSION 3.23) project(eventwatch LANGUAGES C) # # SDK setup # include(FetchContent) FetchContent_Declare(exec_events_c_sdk # The execution events C SDK is kept in the same git repository as the # execution daemon itself GIT_REPOSITORY https://github.com/category-labs/monad.git # The latest version of the SDK is available on a special release branch # of the execution repository GIT_TAG release/exec-events-sdk-v1.1 # This will only download the SDK branch GIT_SHALLOW TRUE # This will disable the checkout of all git submodules; they are needed # for the full execution daemon to build, but not the SDK GIT_SUBMODULES "" # The top-level CMakeLists.txt builds the entire execution daemon; we don't # want that, so we specify SOURCE_SUBDIR to choose a CMakeLists.txt file # in a sudirectory to treat as the "top-level" file for the external # project; this only creates the monad_event library SOURCE_SUBDIR category/event) # The SDK's build system also builds the same example we're building now, using # the same target name ('eventwatch'). This is done as a CI check to ensure # that the upstream project doesn't break the example program. We have to # disable it, because it will conflict with the eventwatch target we're going # to add below (CMake does not allow two targets with the same name) set(MONAD_EVENT_BUILD_EXAMPLE OFF CACHE INTERNAL "") # This will download the source code and call add_subdirectory, which will add # the `monad_event` library target; this is the SDK target we need to link FetchContent_MakeAvailable(exec_events_c_sdk) # # eventwatch example program target # add_executable(eventwatch eventwatch.c) target_compile_options(eventwatch PRIVATE -Wall -Wextra -Wconversion -Werror) target_link_libraries(eventwatch PRIVATE monad_event) ``` ### Step 4: run CMake and build #### CMake with `make` and the default compiler: ```shell theme={null} $ cmake -S ~/src/event-sdk-example-c -B ~/src/event-sdk-example-c/build $ cd ~/src/event-sdk-example-c/build $ make ``` #### CMake with `ninja` and an alternate compiler: Here is another possible invocation, which sets an alternative C compiler using the `CC` environment variable and uses the [Ninja](https://ninja-build.org/) build tool: ```shell theme={null} $ CC=clang-19 cmake -S ~/src/event-sdk-example-c -B ~/src/event-sdk-example-c/build -G Ninja $ cd ~/src/event-sdk-example-c/build $ ninja ``` The compilation should produce an executable file called `eventwatch`. Try running it with the `-h` flag to print the help. ```shell theme={null} $ ./eventwatch -h usage: eventwatch [-h] [<exec-event-ring>] execution event observer example program Options: -h | --help print this message Positional arguments: <exec-event-ring> path of execution event ring shared memory file [default: monad-exec-events] ``` If all was successful, continue on to the [next step in the guide](/execution-events/getting-started/snapshot) or if you are also interested in Rust, build the [Rust example program](/execution-events/getting-started/rust). The Rust example program prints more interesting output than the C version, thanks to Rust's [`#[derive(Debug)]`](https://doc.rust-lang.org/rust-by-example/hello/print/print_debug.html) attribute, so the "Getting started" experience is better in Rust. You can do an equivalent thing in C++ by using the aformentioned `std::formatter` specializations, but they're not in the tutorial. You can also continue on to the next section on this page, which shows a different way of integrating with the C library. ## Alternative: install locally, find with `find_package` Now that you have seen the "all-in-one" tutorial, which explains the source code organization, where the SDK's `CMakeLists.txt` file is, etc., it is easy to show an alternative kind of build system without as much commentary. In this section we will: * Install the SDK to the temporary directory `/tmp/sdk-install-demo`, which will have the traditional `include` and `lib` directory structure, but also a `lib/cmake/category-labs` directory containing the config files for CMake's `find_package` * Compile `eventwatch.c` again, this time using `find_package` which will be instructed to look in `/tmp/sdk-install-demo` ### Step 1: build and install `libmonad_event.a` ```shell theme={null} $ git clone -b release/exec-events-sdk-v1.1 https://github.com/category-labs/monad.git \ ~/src/monad-exec-events-sdk $ cmake -S ~/src/monad-exec-events-sdk/category/event \ -B ~/build/monad-exec-events-sdk-v1-release \ -DCMAKE_INSTALL_PREFIX=/tmp/sdk-install-demo -DCMAKE_BUILD_TYPE=RelWithDebInfo $ cmake --build ~/build/monad-exec-events-sdk-v1-release $ cmake --install ~/build/monad-exec-events-sdk-v1-release ``` If all is successful, you should have a populated `/tmp/sdk-install-demo` directory. ### Step 2: create a new directory and download `eventwatch.c` ```shell theme={null} $ mkdir -p ~/src/event-sdk-example-c-find-package $ cd ~/src/event-sdk-example-c-find-package $ curl -O https://raw.githubusercontent.com/category-labs/monad/refs/tags/release/exec-events-sdk-v1.1/category/event/example/eventwatch.c ``` ### Step 3: create `CMakeLists.txt` Add a `CMakeLists.txt` file with this content: ```CMakeLists.txt theme={null} cmake_minimum_required(VERSION 3.23) project(eventwatch LANGUAGES C) find_package(monad_exec_events_sdk REQUIRED PATHS /tmp/sdk-install-demo/lib/cmake/category-labs) add_executable(eventwatch eventwatch.c) target_compile_options(eventwatch PRIVATE -Wall -Wextra -Wconversion -Werror) target_link_libraries(eventwatch PRIVATE monad_event) ``` ### Step 4: build and run ```shell theme={null} $ cmake -S . -B build $ cmake --build build $ build/eventwatch -h ``` # Running the example program on live data, and next steps Source: https://docs.monad.xyz/execution-events/getting-started/final If you're following this guide in order, you should have already built one of the example programs (in [C](/execution-events/getting-started/c) or [Rust](/execution-events/getting-started/rust)), ran it with a [snapshot file](/execution-events/getting-started/snapshot), and installed your own local [Monad node](/execution-events/getting-started/setup-node). Now we'll run the example program again, but this time it will print the real-time events published by our local Monad node. ## Running with live data ### Step 1: preparing the Monad node Before running, make sure the execution daemon is running and that [execution events are enabled](/node-ops/events-and-websockets). in particular, make sure you have passed the command line argument `--exec-event-ring` to the execution daemon ### Step 2: run the example program In the snapshot example, we passed the name of the snapshot file to the program as a command line argument. In both the C and Rust example programs, if we do not pass any filename at all, it will use default filename used by the execution daemon, connecting us to the live event stream. * For C, run `eventwatch` with no arguments * For Rust, run the command `cargo run -- -d` You will see similar data to the snapshot case, but as it is being published by execution. If you stop the execution daemon, the example program will detect that the source of data is gone, and exit. ## Next steps This completes the getting started guide! If you're interested in developing your own real-time data processing software with the SDK, where should you go from here? Here is a recommended list of resources, in roughly the order that will be most helpful in developing real applications: 1. If you haven't already, read the [overview](/execution-events/overview) and the source code for the example program you just ran 2. Once you understand the basic ideas in the example, [the rest of the SDK documentation](/execution-events#execution-events-documentation) should be easy to follow 3. Before using the SDK, make sure you understand the [consensus events](/execution-events/consensus-events) and what they mean 4. Try out a more sophisticated program and look at the source code for it * For Rust, try the "Block Explorer" TUI example in the upstream [`monad`](https://github.com/category-labs/monad/tree/release/exec-events-sdk-v1.1/rust/crates/monad-exec-events/examples/explorer.rs) repository. You can run it with `cargo run -p monad-exec-events --example explorer` and then browse the source code in `explorer.rs` * For C, look at the code for the `monad-event-cli` program in the upstream [`monad`](https://github.com/category-labs/monad/tree/release/exec-events-sdk-v1.1/category/event/tools/event-cli) repository; this program is the "tcpdump" of the execution event system, and shows several different uses of the API. You may also want to read the next section about compiling this program ## Optional: build the `monad-event-cli` tool `monad-event-cli` is a useful utility for working with the event system. Like the Rust `eventwatch` example, it can decode execution event payloads into human-readable form. It does several other tasks which are useful in the developer workflow, e.g., recording captures of events and creating snapshot event ring files for test cases. <Warning> `monad-event-cli` requires gcc 15.2 or higher, and will not build with gcc 15.1. Currently, the only Ubuntu release that ships with gcc 15.2 in its package repositories is Ubuntu 25.10, which was recently released at the time this guide was written. If your Linux distribution does not provide gcc 15.2 and you do not want to install it manually, you can instead use clang-19 (or more recent) but *using libc++ instead of libstdc++*. The default on Linux is for clang to use the gcc C++ standard library (libstdc++). If you specify `-stdlib=libc++` it will use the LLVM standard library instead, which has the needed `<format>` support. You may have to install it, since in some distributions it is not part of the clang package. In Ubuntu, the clang-19 libc++ runtime and development packages will be added when you install `libc++-19-dev`. When using libc++-19, you must also specify the `-fexperimental-library` compiler flag to enable C++20 time zone support in `<chrono>`; the event CLI tool uses this for printing the event timestamp in loca
프로젝트로 돌아가기