kkrt-labs / kakarot-rpc

Kakarot ZK EVM Ethereum RPC adapter
MIT License
105 stars 84 forks source link

Kakarot RPC


Report a Bug - Request a Feature - Ask a Question

[![GitHub Workflow Status](https://github.com/sayajin-labs/kakarot-rpc/actions/workflows/test.yml/badge.svg)](https://github.com/sayajin-labs/kakarot-rpc/actions/workflows/test.yml) [![Project license](https://img.shields.io/github/license/sayajin-labs/kakarot-rpc.svg?style=flat-square)](LICENSE) [![Pull Requests welcome](https://img.shields.io/badge/PRs-welcome-ff69b4.svg?style=flat-square)](https://github.com/sayajin-labs/kakarot-rpc/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22)
Table of Contents - [Report a Bug](https://github.com/sayajin-labs/kakarot-rpc/issues/new?assignees=&labels=bug&template=01_BUG_REPORT.md&title=bug%3A+") - [Request a Feature](https://github.com/sayajin-labs/kakarot-rpc/issues/new?assignees=&labels=enhancement&template=02_FEATURE_REQUEST.md&title=feat%3A+≠≠≠≠≠≠≠) - [About](#about) - [Architecture](#architecture) - [High level](#high-level) - [Low level](#low-level) - [Getting Started](#getting-started) - [Prerequisites](#prerequisites) - [Installation](#installation) - [Setup the project](#setup-the-project) - [Build from source](#build-from-source) - [Environment variables](#environment-variables) - [Dev mode with Katana](#dev-mode-with-katana) - [Building a Docker Image](#building-a-docker-image) - [Sending transactions to RPC using forge script](#sending-transactions-to-rpc-using-forge-script) - [Configuration](#configuration) - [Running a Node in Various Environments](#running-a-node-in-various-environments) - [Local Environment](#local-environment) - [Staging Environment](#staging-environment) - [Production Environment](#production-environment) - [Potential Pitfalls, Caveats, and Requirements](#potential-pitfalls-caveats-and-requirements) - [Requirements](#requirements) - [Potential Pitfalls](#potential-pitfalls) - [Caveats](#caveats) - [API](#api) - [Testing](#testing) - [Rust tests](#rust-tests) - [Apibara indexer tests](#apibara-indexer-tests) - [Hive](#hive) - [Project assistance](#project-assistance) - [Contributing](#contributing) - [Glossary](#glossary) - [Authors \& contributors](#authors--contributors) - [Security](#security) - [License](#license) - [Acknowledgements](#acknowledgements) - [Benchmarks](#benchmarks) - [Contributors ✨](#contributors-)

About

Kakarot RPC fits in the three-part architecture of the Kakarot zkEVM rollup (Kakarot EVM Cairo Programs, Kakarot RPC, Kakarot Indexer). It is the implementation of the Ethereum JSON-RPC specification made to interact with Kakarot zkEVM in a fully Ethereum-compatible way.

Kakarot zkEVM architecture

The Kakarot RPC layer's goal is to receive and output EVM-compatible payloads & calls while interacting with an underlying StarknetOS client. This enables Kakarot zkEVM to interact with the usual Ethereum tooling: Metamask, Hardhat, Foundry, etc.

Note that this is necessary because Kakarot zkEVM is implemented as a set of Cairo Programs that run on an underlying CairoVM (so-called StarknetOS) chain.

This adapter layer is based on:

Architecture

High level

Here is a high level overview of the architecture of Kakarot RPC.

Kakarot RPC Adapter flow

Low level

Below is a lower level detailed overview of the internal architecture. Kakarot RPC Adapter flow

Getting Started

TL;DR:

Prerequisites

Installation

Setup the project

To set up the repository (pulling git submodule and building Cairo dependencies), run:

make setup

Caveats:

  1. the setup make command uses linux (MacOs compatible) commands to allow running the ./scripts/extract_abi.sh. This script is used to use strongly typed Rust bindings for Cairo programs. If you encounter problems when building the project, try running ./scripts/extract_abi.sh.
  2. the kakarot submodule uses Python to build and deploy Kakarot contracts. If you don't have the right version available, we recommend to use pyenv to install it.

Build from source

To build the project from source (in release mode):

cargo build --release

Note that there are sometimes issues with some dependencies (notably scarb or cairo related packages, there are sometimes needs to cargo clean and cargo build)

Environment variables

Copy the .env.example file to a .env file and populate each variable

cp .env.example .env

Meanwhile you can just use unit tests to dev.

make test

The binaries will be located in target/release/.

Dev mode with Katana

To run a local Starknet sequencer, you can use Katana. Katana, developed by the Dojo team, is a sequencer designed to aid in local development. It allows you to perform all Starknet-related activities in a local environment, making it an efficient platform for development and testing. To run Katana and deploy the Kakarot zkEVM (a set of Cairo smart contracts implementing the EVM):

make run-katana

This command will install Katana and generate a genesis file at .katana/genesis.json. Katana's genesis configuration feature is a way to define the initial state and settings of the Kakarot blockchain network locally, providing a customizable starting point for the chain. Among other things, it allows you to:

To deploy Kakarot Core EVM (set of Cairo Programs):

 make deploy-kakarot

To run the Kakarot RPC pointing to this local devnet:

STARKNET_NETWORK=katana make run-dev

Some notes on this local devnet:

Building a Docker Image

In order to build a Docker Image for the RPC, you can run the below command which will setup the local environment and compile the binary:

make docker-build

Sending transactions to RPC using forge script

An example script to run which uses a pre-funded EOA account with private key EVM_PRIVATE_KEY

forge script scripts/PlainOpcodes.s.sol --broadcast --legacy --slow

Configuration

Kakarot RPC is configurable through environment variables. Check out .env.example file to see the environment variables.

Running a Node in Various Environments

This section outlines how to run a complete node in different environments: local, staging, and production. Running a node involves several critical components to ensure the system operates effectively:

By correctly configuring these components, you can ensure that the node functions as a robust part of the system.

In the following sections we have tried to provide the most important parameters useful for understanding and configuring the node. However for the sake of brevity, certain parameters deemed less important are omitted and can all be found in the corresponding Docker compose files:

Local Environment

To start the entire infrastructure locally, use the following command:

make local-rpc-up

This command will use the docker-compose.yaml file to set up the whole infrastructure locally utilizing the following elements:

Staging Environment

To start the entire infrastructure in the staging environment, use the following command:

make staging-rpc-up

This command will use the docker-compose.staging.yaml file to set up the whole infrastructure in the staging configuration utilizing the following elements:

Production Environment

To start the entire infrastructure in the production environment, use the following command:

make testnet-rpc-up

This command will use the docker-compose.prod.yaml file to set up the whole infrastructure in the production configuration utilizing the following elements:

Potential Pitfalls, Caveats, and Requirements

When setting up the Kakarot node in any environment, it's important to be aware of the following:

Requirements

Potential Pitfalls

Caveats

API

You can take a look at rpc-call-examples directory. Please note the following:

Testing

Rust tests

In order to execute the Rust tests, follow the below instructions:

Apibara indexer tests

In order to run the Typescript unit tests, you will need to have Deno installed. Then you can run KAKAROT_ADDRESS=ADDRESS_YOU_WANT_TO_USE_FOR_KAKAROT deno test --allow-env.

Hive

The Hive end-to-end test suite is set up in the Github Continuous Integration (CI) flow of the repository. This ensures a safe guard when modifying the current RPC implementation and/or the execution layer.

Due to the current existing differences between the Kakarot EVM implementation which aims to be a type 2 ZK-EVM (see the blog post from Vitalik for more details), some of the Hive tests need to be skipped or slightly modified in order to pass.

For the hive rpc tests, all the websockets related tests are skipped as websockets aren't currently supported by the Kakarot RPC.

For the hive rpc compatibility tests, the following tests are skipped:

In addition to the tests we skip, some of the objects fields need to be ignored in the passing tests:

If you which to run our hive test suite locally, the following steps should be taken:

Project assistance

If you want to say thank you or/and support active development of Kakarot RPC:

Contributing

First off, thanks for taking the time to contribute! Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make will benefit everybody else and are greatly appreciated.

Please read our contribution guidelines, and thank you for being involved!

Glossary

Authors & contributors

For a full list of all authors and contributors, see the contributors page.

Security

Kakarot RPC follows good practices of security, but 100% security cannot be assured. Kakarot RPC is provided "as is" without any warranty. Use at your own risk.

For more information and to report security issues, please refer to our security documentation.

License

This project is licensed under the MIT license.

See LICENSE for more information.

Acknowledgements

We warmly thank all the people who made this project possible.

Benchmarks

For now, Kakarot RPC provides a minimal benchmarking methodology. You'll need Bun installed locally.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Abdel @ StarkWare
Abdel @ StarkWare

💻
etash
etash

💻
Mentor Reka
Mentor Reka

💻
Flydexo
Flydexo

💻
Elias Tazartes
Elias Tazartes

💻
greged93
greged93

💻
Harsh Bajpai
Harsh Bajpai

💻
ftupas
ftupas

💻
Clément Walter
Clément Walter

💻
johann bestowrous
johann bestowrous

💻
danilowhk
danilowhk

💻
Iris
Iris

💻
Aniket Prajapati
Aniket Prajapati

💻
Trunks @ Carbonable
Trunks @ Carbonable

💻
Alex Sumner
Alex Sumner

💻
Raphaël Deknop
Raphaël Deknop

💻
Bhavya Gosai
Bhavya Gosai

💻
apoorvsadana
apoorvsadana

💻
Paul-Henry Kajfasz
Paul-Henry Kajfasz

💻
Khaeljy
Khaeljy

💻
Tristan
Tristan

💻
glihm
glihm

💻
omahs
omahs

💻
valdo.carbonaboyz.stark
valdo.carbonaboyz.stark

💻
Damián Piñones
Damián Piñones

💻
zarboq
zarboq

💻
Santiago Galván (Dub)
Santiago Galván (Dub)

💻
Oak
Oak

💻
Add your contributions

This project follows the all-contributors specification. Contributions of any kind welcome!