Monad 백서

Monad

원문

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

l time. In some future version of libc++ this will no longer be needed. </Warning> To build `monad-event-cli`, you will also need the [CLI11](https://github.com/CLIUtils/CLI11) C++ command-line parser library and the OpenSSL development files. Although it is optional, you should also install the development files for [GNU multiple-precision library](https://gmplib.org/) so that `uint256` values print in decimal form. The instructions also use the [ninja](https://ninja-build.org/) build tool. You can install everything on Ubuntu with: ``` $ sudo apt install libcli11-dev libssl-dev libgmp-dev ninja-build ``` Now clone the [execution repository](https://github.com/category-labs/monad.git) and check out the branch `release/exec-events-sdk-v1.1`, then build the CMake project rooted at `category/event`. Using clang-19 with libc++ and the above options: ```shell theme={null} $ git clone -b release/exec-events-sdk-v1.1 https://github.com/category-labs/monad.git \ ~/src/monad-event-cli $ CC=clang-19 CFLAGS="-march=x86-64-v4" \ CXX=clang++-19 CXXFLAGS="-stdlib=libc++ -fexperimental-library -march=x86-64-v4" cmake \ -S ~/src/monad-event-cli/category/event -B ~/build/monad-event-cli-v1.1-release -G Ninja \ -DCMAKE_BUILD_TYPE=RelWithDebInfo -DMONAD_EVENT_BUILD_CLI=ON $ cmake --build ~/build/monad-event-cli-v1.1-release ``` You should now be able to run: ```shell theme={null} $ ~/build/monad-event-cli-v1.1-release/monad-event-cli --help ``` <Note> The `-march=x86-64-v4` is needed to enable certain atomic operations at the CPU instruction level, to avoid needing to link with libatomic.a; without this, a performance warning is emitted, which becomes a compilation error due to `-Werror` </Note> To simplify running cmake with all these settings, you might want to create a CMake toolchain file instead of using environment variables. To do this, create a file called `clang19-libcxx.cmake` with these contents: ```CMake theme={null} set(CMAKE_C_COMPILER clang-19) set(CMAKE_CXX_COMPILER clang++-19) set(CMAKE_ASM_FLAGS_INIT -march=x86-64-v4) set(CMAKE_C_FLAGS_INIT -march=x86-64-v4) set(CMAKE_CXX_FLAGS_INIT "-march=x86-64-v4 -stdlib=libc++ -fexperimental-library") ``` Now can you run this slightly cleaner command: ```shell theme={null} $ cmake --toolchain <path-to-toolchain-file> -S ~/src/monad-event-cli/category/event \ -B ~/build/monad-event-cli-v1.1-release -G Ninja \ -DCMAKE_BUILD_TYPE=RelWithDebInfo -DMONAD_EVENT_BUILD_CLI=ON ``` # Getting started Source: https://docs.monad.xyz/execution-events/getting-started/index In this guide, we will: 1. Compile an example program, which will involve building code with the execution event SDK as a dependency. The SDK is offered for both the C and Rust programming languages. Each language has its own guide, so follow the instructions for your language of choice * [C guide](/execution-events/getting-started/c) * [Rust guide](/execution-events/getting-started/rust) 2. [Run the example program on some historical data](/execution-events/getting-started/snapshot), which prints ASCII representations of execution events to `stdout` 3. [Set up and run our own Monad node](/execution-events/getting-started/setup-node), so that we have a local execution process publishing real-time data 4. [Run the example program again, this time using our Monad node](/execution-events/getting-started/final); this will again print execution events to `stdout`, but this time the source will be real-time data from our local node <Info title="Linux required, but with some macOS support"> This guide has been tested on a clean Ubuntu 24.04 LTS install, but should work on any recent Linux distribution, although the names of the required packages might be different. The distribution will need to provide a recent enough C compiler, either `gcc-13` or `clang-19`. The first two steps of the guide, which involve looking at historical data instead of real-time data, will also work on a macOS installation that is configured for software development. This may make it easier for some developers to try out the SDK on a development workstation or laptop, without the need to set up a Linux host first. Unlike the SDK, the Monad node itself only runs on Linux so the later steps of the guide -- which actually consume real-time data -- require a full Linux host running your own Monad node. </Info> # Building the Rust example program Source: https://docs.monad.xyz/execution-events/getting-started/rust The execution event SDK is made up of two packages, called `monad-event-ring` and `monad-exec-events`. These are described in more detail in the [Rust API guide](/execution-events/rust-api), but for now it's enough to just know their names. In the future, Category Labs may publish these packages to [crates.io](https://crates.io), but that is not how the SDK is currently distributed. Instead, the user's `Cargo.toml` file declares the upstream source of these dependencies to be a particular release tag of the git repository where the SDK source code is located. Dependencies which are sourced from git rather than crates.io are explained in [this section](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#specifying-dependencies-from-git-repositories) of the Cargo Book. ## Where is the Rust SDK source code? The execution event Rust SDK lives in same source code repository as the execution daemon ([here](https://github.com/category-labs/monad)). Most of the code in this repository in written in either C or C++, the languages used by the execution daemon itself. The repository also contains a few Rust wrapper APIs, one of which is the Rust execution events SDK. The top-level `Cargo.toml` file for all the Rust components is located in the `rust/` subdirectory. Many of the functions in the execution events C SDK are reused by Rust, via an FFI interface. The main way this affects you as a Rust user is that you must ensure that a recent enough C compiler is selected when Cargo calls CMake and bindgen from the SDK's [`build.rs` build scripts](https://doc.rust-lang.org/cargo/reference/build-scripts.html). The C SDK uses some recent C23 language features, and requires either gcc-13 or clang-19. If you run `cc -v` and it reports an older compiler, you will need to set the `CC` environment variable to tell CMake to select a newer compiler. ## Building the example program ### Step 1: install prerequisite development packages You might already have some of these installed, but make sure you have at least the minimum required version (newer versions will probably work, but are not explicitly tested). In addition to a Rust toolchain, you will need: | Requirement | Ubuntu package name | Minimum version | What is it used for? | | - | - | - | - | | C compiler | gcc-13 or clang-19 | see package names | The core event library (`libmonad_event.a`) is written in C, and is used by the Rust library | | C++ compiler | g++-13 or clang-19 | see package names | The optional C++ components are not used by Rust, but the CMake configure step will report an error if it cannot find a C++ compiler | | CMake | cmake | CMake 3.23 | `libmonad_event.a` is built with CMake, via `build.rs` integration with cargo | | zstd library | libzstd-dev | any | Snapshot event ring files are compressed with zstd; this library is needed to decompress them | | libhugetlbfs | libhugetlbfs-dev | any | `libhugetlbfs` is used to locate the default hugetlbfs mount point that holds event ring shared memory files | | libclang | clang-19 | clang-19 | Rust's bindgen requires the a recent version of the libclang library | We will also need git and curl. #### 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. #### Dependencies quickstart To install all of the dependenices in one shot on Ubuntu 24.04 or higher, run this command (feel free to use more recent verions, e.g., `clang-20`): ```shell theme={null} $ sudo apt install git curl gcc g++ cmake clang-19 libzstd-dev libhugetlbfs-dev ``` <Info title="`libclang` and clang versions"> Even if you compile `libmonad_event.a` with gcc, the Rust [bindgen](https://rust-lang.github.io/rust-bindgen/) utility still uses the [libclang](https://clang.llvm.org/docs/Tooling.html#libclang) tool to programmatically generate Rust bindings to C code. Technically you should not need the full clang compiler, just the libclang package, but some users have reported trouble without installing it. The reason you need version 19 (or greater) is that clang-19 was the first version to support enough features from the C23 language standard to be able to compile the SDK. If you see errors that imply that bindgen cannot understand the `constexpr` keyword, then bindgen has automatically selected a libclang version that is too old. If you have multiple libclang versions on the system (the default clang is version 18 on Ubuntu 24.04), installing a newer version may not help, if the underlying problem is that bindgen is selecting the wrong one by default. Later on in this guide (during the `cargo build` step) we'll explain more about how to fix some common problems with libclang version selection. </Info> ### Step 2: create a new package and copy the example code into it First, create a new cargo package for the example program: ```shell theme={null} $ cargo new --bin event-sdk-example-rust $ cd event-sdk-example-rust ``` Next, we'll overwrite the default "Hello world" `main.rs` source file with the example program code, downloaded from github: ``` $ curl https://raw.githubusercontent.com/category-labs/monad/refs/tags/release/exec-events-sdk-v1.1/rust/crates/monad-exec-events/examples/eventwatch.rs > src/main.rs ``` ### Step 3: integrating with the SDK packages Create the following `Cargo.toml` file: ```Cargo.toml theme={null} [package] name = "event-sdk-example-rust" version = "0.1.0" edition = "2021" [dependencies] chrono = "0.4.34" clap = { version = "4.2", features = ["derive"] } lazy_static = "1.5.0" [dependencies.monad-event-ring] git = "https://github.com/category-labs/monad" tag = "release/exec-events-sdk-v1.1" [dependencies.monad-exec-events] git = "https://github.com/category-labs/monad" tag = "release/exec-events-sdk-v1.1" ``` ### Step 4: build the project ```shell theme={null} cargo build ``` The first time you build will be *very* slow, because it will fetch the monad repository and all transitive git submodules. None of them are needed for the SDK, but cargo checks them out and there is currently no way to override this (see [this issue](https://github.com/rust-lang/cargo/issues/4247)). Thankfully, the git cache is usually shared by all Cargo projects (in `$HOME/.cargo/git`) so this long download should only happen a single time, not once per project. You may need to tell CMake to use a more recent C compiler than the default one it detects, which you can do using the `CC` environment variable. The example below uses the bash terse syntax for setting environment variables in the scope of the next command to be run: ```shell theme={null} CC=clang-19 cargo build ``` If you encounter any build errors, check out the "trouble-shooting" section after this one. Otherwise, the build should produce a working executable file. Try running it with the `-h` flag to print the help: ```shell theme={null} cargo run -- -h ``` If all was successful, continue on to the [next step in the guide](/execution-events/getting-started/snapshot). ### Trouble-shooting common build errors #### libclang errors The most common source of errors when building is when bindgen selects an outdated libclang version, as explained earlier. This typically appears as either: * An error explicitly mentioning `libclang` OR * A message that includes the text "Unable to generate bindings" Setting the environment variable `CC=clang-19` only influences the compiler that CMake uses to build the C SDK library, `libmonad_event.a`. Namely, it does not the affect the libclang version that is used by bindgen to generate the bindings. `CC` specifies a particular binary program, whereas libclang is a shared library that is dynamically loaded by a `build.rs` build script. They are (for the most part) *not* affected by the same environment variables. There are a number of environment variables that control the behavior of how libclang is located and configured, and they're documented in the "Environment Variables" section of the [Rust libclang bindings library](https://crates.io/crates/clang-sys). Some advice we have found works well: * Setting `LLVM_CONFIG_PATH` to point to the full path to the `llvm-config` binary is the best option; this command bakes in a lot of details about how LLVM was built and installed on the system, to make it easier for users of LLVM (such as bindgen) to find the configuration they need * In some unusual cases, you may select the right libclang but it may be configured strangely, so that it cannot find the basic libc header files anymore (typical symptoms are claims that `stddef.h`, `assert.h`, or `string.h` are missing); you can can figure out the location of these files on the system and pass them as system include directories (the `-isystem` clang option) to libclang through bindgen using the `BINDGEN_EXTRA_CLANG_ARGS` environment variable; on macOS this looks like: ``` BINDGEN_EXTRA_CLANG_ARGS="-isystem $(xcrun --show-sdk-path)/usr/include" ``` and in a typical Linux setup it looks like this: ``` BINDGEN_EXTRA_CLANG_ARGS="-isystem /usr/include" ``` #### zstd library errors A common error with MacPorts on macOS is the inability of the C compiler to find the `zstd.h` header file, or the inability of the linker to find the `libzstd` library. This is because the system C compiler will not search in the `/opt/local` directory hierarchy by default. To fix this, export the `CFLAGS` and `RUSTFLAGS` environment variables as follows: ```shell theme={null} export CFLAGS=-I/opt/local/include export RUSTFLAGS=-L/opt/local/lib ``` Alternatively, you can set `CC` to a MacPorts-provided C compiler, which will know to search there. # Setting up your Monad node Source: https://docs.monad.xyz/execution-events/getting-started/setup-node The execution events SDK relies on a shared memory communication system, in which the node's EVM execution daemon acts a publisher. Thus, in order to use it, you need to (i) run your own Monad node and (ii) run your data processing application on the same host as the node, so it can read the data from shared memory. Follow the guides on the [node operations](/node-ops) page, which cover how to install and configure a full node, and how to reset it when problems occur. You will also need to follow some [extra configuration steps](/node-ops/events-and-websockets) to prepare the node for use with execution events. # Running the example program on snapshot data Source: https://docs.monad.xyz/execution-events/getting-started/snapshot The easiest way to get acquainted with the execution event system is to try out the example program and read the code, although you may want to read the [quick overview](/execution-events/overview) explaining the basic concepts first. If you're following this guide in order you should have already built one of the example programs. If you have not built one yet, choose the appropriate guide for your language or choice (either [C](/execution-events/getting-started/c) or [Rust](/execution-events/getting-started/rust)), and then return to this page. ## Live event rings vs. snapshot event rings The event ring's shared memory data structures typically live inside of a regular file. Any process that wants shared access to an event ring, first locates it via the filesystem, then maps a shared view of it into the process' virtual memory map using the [mmap(2)](https://man7.org/linux/man-pages/man2/mmap.2.html) system call. Event ring files come in two flavors: 1. **"Live" event ring files** -- these are the "normal" event ring files that are the source of real-time data. The whole point of the SDK is to read real-time events from these files, but they're not very convenient for most day-to-day software development tasks. Suppose, for example, you wanted to write a test for your data processing program. The SDK is mostly designed around *reading* events, so to test it with a live event ring, you'd need to write some dummy event publishing code just to have events to read. For execution events, the live event ring file is populated by the execution daemon, which we have not even installed at this point in the tutorial! A lot of development headaches are solved by the second kind of event ring file. 2. **"Snapshot" event ring files** -- these are compressed snapshots taken of a live event ring file as it existed at a particular moment in time. Typically they are "rewound" to the oldest event in the circular event queue, and are used to replay a fixed set of historical execution events. Snapshot files are useful for testing and development workflows, because you do not need to be running an active publisher to use them. Because they're so useful for development, snapshots are the first data source we'll use, before trying the example program on a live node. ## Running the example program on a snapshot file ### Step 1: download a snapshot file Run this command to download a snapshot: ```shell theme={null} $ curl https://raw.githubusercontent.com/category-labs/monad/refs/tags/release/exec-events-sdk-v1.1/rust/crates/monad-exec-events/test/data/exec-events-emn-30b-15m/snapshot.zst > /tmp/exec-events-emn-30b-15m.zst ``` The `emn-30b-15m` part of the filename means "Ethereum mainnet replay for 30 blocks starting after block 15 million". In other words, this contains the execution events emitted during a historical replay of the Ethereum blockchain (chain ID 1), from block 15,000,001 to block 15,000,031. The Category Labs execution daemon is able to execute blocks from Monad blockchains (the EVM chain ID 143 or any of its test networks), but also from other EVM-compatible networks. Historical replay of the Ethereum mainnet is used as an execution "conformance test", to make sure the node software remains as Ethereum compatible as possible. We use an Ethereum chain snapshot in the tutorial under the assumption that many developers are already familiar with the Ethereum ecosystem, but might be new to Monad. You can check that all of the data captured in the snapshot file matches the data published by your favorite Ethereum data provider. For example, you'll be able to check that the data shown here matches what is reported by websites like [Etherscan](https://etherscan.io/). <Warning title="Why `/tmp`?"> Our example `curl` command placed the snapshot file in `/tmp` for a reason. Although the file can be placed anywhere, we encourage users not to place it in the same directory they are already in, to ensure they won't encounter a confus
프로젝트로 돌아가기