|
1 | 1 | --- |
2 | 2 | id: self-hosted |
3 | 3 | title: Self-hosted deployment |
4 | | -resources: |
5 | | - - url: https://www.codecademy.com/article/installing-and-using-postgresql-locally |
6 | | - title: Installing and Using PostgreSQL Locally |
7 | | - - url: https://docs.digitalocean.com/products/databases/postgresql/how-to/create/ |
8 | | - title: How to Create PostgreSQL Database Clusters |
9 | | - - url: https://www.amazonaws.cn/en/getting-started/tutorials/deploy-docker-containers/ |
10 | | - title: How to Deploy Docker Container - AWS |
11 | | - - url: https://www.digitalocean.com/solutions/docker-hosting |
12 | | - title: Deploy Docker Containers on Digital Ocean |
13 | | - - url: https://docs.docker.com/reference/cli/docker/image/tag/ |
14 | | - title: Docker Tag |
15 | 4 | --- |
16 | 5 |
|
17 | | -The self-hosted deployment involves running your infrastructure locally or on a remote cloud server to host your application node. |
| 6 | +This guide explains how to run a Cartesi Rollups node locally on your machine for development and testing purposes on **testnet**. |
18 | 7 |
|
19 | | -Here are the requirements: |
| 8 | +:::warning Production Warning |
| 9 | +**This self-hosted approach should NOT be used in *production*.** |
20 | 10 |
|
21 | | -- Wallet with sufficient funds on the chosen network. |
22 | | -- A cloud server |
23 | | -- A PostgreSQL database |
24 | | -- A web3 provider for interacting with the selected network |
| 11 | +While this setup works with testnet environments, it's designed exclusively for development purposes. It lacks critical production requirements such as: |
25 | 12 |
|
26 | | -## Initiating deployment |
27 | | - |
28 | | -1. Compile your application into RISC-V architecture and consequently build a Cartesi machine by running: |
29 | | - |
30 | | - ```shell |
31 | | - cartesi build |
32 | | - ``` |
33 | | - |
34 | | -2. Run the command below to start the deployment process. |
35 | | - |
36 | | - ```shell |
37 | | - cartesi deploy --hosting self-hosted --webapp https://sunodo.io/deploy |
38 | | - ``` |
39 | | - |
40 | | - The command generates a Docker image containing the rollups node and machine. You will be redirected to a web application to deploy the necessary smart contracts. |
41 | | - |
42 | | -  |
43 | | - |
44 | | -## Deploying the contracts |
45 | | - |
46 | | -On the deploy web interface, the hash of the Cartesi machine will be automatically configured. |
47 | | - |
48 | | -1. Connect your wallet to set the application chain’s base layer and deployer account. |
49 | | - |
50 | | -2. Create a wallet specifically for Cartesi rollups node transactions. The Cartesi rollups node will use this wallet to submit transactions to the base layer. Paste the public address of this wallet. |
51 | | - |
52 | | - :::note create a wallet |
53 | | - You can use [Cast](https://book.getfoundry.sh/reference/cast/cast-wallet-new-mnemonic) to create a new wallet by running `cast wallet new-mnemonic --words 24`. For increased security, you can use a wallet managed by [AWS KMS](https://aws.amazon.com/blogs/database/part1-use-aws-kms-to-securely-manage-ethereum-accounts/). |
54 | | - ::: |
55 | | - |
56 | | -3. After successful deployment, the node’s configuration is presented in a `.env` file and a `.toml` format. This config file includes the addresses of the deployed smart contracts and information on the base layer chain. |
57 | | - |
58 | | - You will need the `.env` when [hosting the node on the cloud provider](./self-hosted.md/#hosting-on-a-cloud-provider) and the `.toml` file when [hosting on Fly.io](./self-hosted.md/#hosting-on-flyio). |
59 | | - |
60 | | -<!-- <video width="100%" controls poster="/static/img/v1.3/deploy.png"> |
61 | | - <source src="/videos/Deploy_Success.mp4" type="video/mp4" /> |
62 | | - Your browser does not support video tags. |
63 | | -</video> --> |
64 | | - |
65 | | - |
66 | | -## Hosting the node |
67 | | - |
68 | | -You’ll need a server to host the application node and keep it operational 24/7. This server will expose a single port for client access to the rollups node APIs through GraphQL or Inspect requests. |
69 | | - |
70 | | - |
71 | | -The server requirements depend on your application's expected usage and the specifications of the Cartesi machine you're using, such as its RAM size and total capacity. Consider a minimum of 8GB of RAM, and adjust as needed. |
72 | | - |
73 | | - |
74 | | -The Cartesi rollups node is distributed as a Docker image. Any popular cloud provider, like AWS, GCP, Azure, Digital Ocean, or Linode, can run docker containers and hence can be used to host the rollups node. |
75 | | - |
76 | | -Alternatively, you can use a service like [Fly.io](https://fly.io/) to deploy your application node. |
77 | | - |
78 | | -### Hosting on a cloud provider |
79 | | - |
80 | | -1. Download the `.env` configuration file into the root directory of your application. |
81 | | - |
82 | | -1. Obtain HTTP and WebSocket URLs from a web3 provider for the `CARTESI_BLOCKCHAIN_HTTP_ENDPOINT` and `CARTESI_BLOCKCHAIN_WS_ENDPOINT` variables. |
83 | | - |
84 | | - Here is an example from [Alchemy](https://dashboard.alchemy.com/): |
85 | | - |
86 | | -  |
87 | | - |
88 | | - :::caution important |
89 | | - The web3 provider URLs and wallet mnemonic are sensitive information that can compromise your application and funds. You should keep it **secure** and **private** at all times. |
90 | | - ::: |
| 13 | +- Public snapshot verification |
| 14 | +- Proper security hardening |
| 15 | +- Production-grade infrastructure |
| 16 | +::: |
91 | 17 |
|
92 | | -1. Create a PostgreSQL database and configure the connection string in the `.env` file. |
| 18 | +## Prerequisites |
93 | 19 |
|
94 | | - The connection string for a PostgreSQL database must be configured at the `CARTESI_POSTGRES_ENDPOINT` variable. |
| 20 | +Before starting, ensure you have the following installed: |
95 | 21 |
|
96 | | - You can use any PostgreSQL database, whether managed by a cloud provider or set up on your local infrastructure. The key configuration required is the connection string, encompassing the database URL, username, password, and name. The node necessitates a PostgreSQL database to store the application state, which is accessible via the [GraphQL API](../api-reference/graphql/basics.md). |
| 22 | +- Cartesi CLI: An easy-to-use tool for developing and deploying your dApps. |
97 | 23 |
|
98 | | -1. With all the config variables set, here is how you can run the node on your local machine: |
| 24 | +- Docker Desktop 4.x: The required tool to distribute the Cartesi Rollups framework and its dependencies. |
99 | 25 |
|
100 | | - ``` |
101 | | - docker run --env-file <env-file> -p 10000:10000 <image-id> |
102 | | - ``` |
| 26 | +For more details about the installation process for each of these tools, please refer to the [this section](../development/installation.md). |
103 | 27 |
|
104 | | - Replace `<env-file>` and `<image-id>` with the `.env` file name and `sha256` hash of your Cartesi machine. |
| 28 | +## Configuration |
105 | 29 |
|
106 | | - The image can be tagged using [docker tag](https://docs.docker.com/reference/cli/docker/image/tag/). |
| 30 | +Before running the node, you need to configure your `.env` file with the following environment variables: |
107 | 31 |
|
108 | | - You can deploy your node with a cloud provider or use any managed container solution, like Kubernetes. |
| 32 | +```shell |
| 33 | +BLOCKCHAIN_ID=<blockchain-id> |
| 34 | +AUTH_KIND="private_key_file" |
| 35 | +PRIVATE_KEY_FILE="/run/secrets/pk" |
| 36 | +BLOCKCHAIN_WS_ENDPOINT="<ws-endpoint>" |
| 37 | +BLOCKCHAIN_HTTP_ENDPOINT="<http-endpoint>" |
| 38 | +``` |
109 | 39 |
|
110 | | -### Hosting on fly.io |
| 40 | +**Important notes:** |
111 | 41 |
|
112 | | -Fly.io is a platform where you can conveniently deploy applications packaged as Docker containers. |
| 42 | +| Variable | Description | |
| 43 | +|----------|-------------| |
| 44 | +| `BLOCKCHAIN_ID` | Replace `<blockchain-id>` with your blockchain network ID | |
| 45 | +| `BLOCKCHAIN_WS_ENDPOINT` | Replace `<ws-endpoint>` with your WebSocket endpoint | |
| 46 | +| `BLOCKCHAIN_HTTP_ENDPOINT` | Replace `<http-endpoint>` with your HTTP endpoint | |
| 47 | +| `PRIVATE_KEY_FILE` | Points to the private key file created in [**step 2**](#setting-up-the-local-node) | |
| 48 | +| `AUTH_KIND` | Set to `"private_key_file"` for local development | |
113 | 49 |
|
114 | | -:::caution important |
115 | | -If deploying to Fly.io from macOS with Apple Silicon, create a Docker image for `linux/amd64` with: `cartesi deploy build --platform linux/amd64` |
116 | | -::: |
| 50 | +## Setting up the local node |
117 | 51 |
|
118 | | -1. [Install the flyctl CLI](https://fly.io/docs/hands-on/install-flyctl/) |
| 52 | +1. **Download the Cartesi Rollups Node docker compose file in your project root:** |
119 | 53 |
|
120 | | -1. [Create an account](https://fly.io/docs/hands-on/sign-up-sign-in/) |
| 54 | + ```shell |
| 55 | + curl -L https://raw.githubusercontent.com/cartesi/docs/refs/heads/docs/deployment/cartesi-rollups_versioned_docs/version-2.0/deployment/src/compose.local.yaml -o compose.local.yaml |
| 56 | + ``` |
121 | 57 |
|
122 | | -1. Create an application: |
| 58 | +2. **Create a secret for private key storage:** |
123 | 59 |
|
124 | 60 | ```shell |
125 | | - $ fly app create <app-name> |
126 | | - New app created: <app-name> |
| 61 | + mkdir -p secrets |
| 62 | + echo "YOUR_PRIVATE_KEY" > secrets/pk |
127 | 63 | ``` |
128 | 64 |
|
129 | | -1. Create a Postgres database application: |
| 65 | + :::danger Security |
| 66 | + Ensure the `secrets/` directory is in a secure location and has restricted permissions, different from the project root to avoid leaking your private key. |
| 67 | + ::: |
| 68 | + |
| 69 | +3. **Build the application with the Cartesi CLI:** |
130 | 70 |
|
131 | 71 | ```shell |
132 | | - fly postgres create --initial-cluster-size 1 --name <app-name>-database --vm-size shared-cpu-1x --volume-size 1 |
| 72 | + cartesi build |
133 | 73 | ``` |
134 | 74 |
|
135 | | - Save the connection string provided by the command output. |
| 75 | + This command compiles your application into RISC-V architecture and creates a Cartesi machine snapshot locally. |
136 | 76 |
|
137 | | -1. Attach database to the node application: |
| 77 | +4. **Run the Cartesi Rollups Node with the application's initial snapshot attached:** |
138 | 78 |
|
139 | 79 | ```shell |
140 | | - fly postgres attach <app-name>-database -a <app-name> |
| 80 | + docker compose -f compose.local.yaml --env-file .env up -d |
141 | 81 | ``` |
142 | 82 |
|
143 | | -1. Download `fly.toml` file from deploying the contracts and move it to your application directory: |
| 83 | + This starts the local node using the configuration from your `.env` file. |
144 | 84 |
|
145 | | -  |
| 85 | +5. **Deploy and register the application to the node:** |
146 | 86 |
|
147 | | -1. Edit the `fly.toml` file to change all occurrences of `<app-name>` to the name of your application |
| 87 | + ```shell |
| 88 | + docker compose --project-name cartesi-rollups-node \ |
| 89 | + exec advancer cartesi-rollups-cli deploy application <app-name> /var/lib/cartesi-rollups-node/snapshot \ |
| 90 | + --epoch-length 720 \ |
| 91 | + --self-hosted \ |
| 92 | + --salt <salt> \ |
| 93 | + --json |
| 94 | + ``` |
148 | 95 |
|
149 | | -1. Create secrets for sensitive configuration with the actual values: |
| 96 | + Replace `<app-name>` with your application name and `<salt>` with a unique identifier. The salt must be unique for each deployment and cannot be repeated. You can generate a unique salt using: |
150 | 97 |
|
151 | 98 | ```shell |
152 | | - fly secrets set -a <app-name> CARTESI_BLOCKCHAIN_HTTP_ENDPOINT=<web3-provider-http-endpoint> |
153 | | - fly secrets set -a <app-name> CARTESI_BLOCKCHAIN_WS_ENDPOINT=<web3-provider-ws-endpoint> |
154 | | - fly secrets set -a <app-name> CARTESI_AUTH_MNEMONIC=`<mnemonic>` |
155 | | - fly secrets set -a <app-name> CARTESI_POSTGRES_ENDPOINT=<connection_string> |
| 99 | + cast keccak256 "your-unique-string" |
156 | 100 | ``` |
157 | 101 |
|
158 | | - Set value of the `connection_string` as provided by step 4. |
159 | | - |
160 | | -1. Deploy the node: |
| 102 | + After this process, you'll have your application deployed and registered to the node. |
161 | 103 |
|
162 | | - Tag the image produced at the beginning of the process and push it to the Fly.io registry: |
| 104 | +## Accessing the node |
163 | 105 |
|
164 | | - ```shell |
165 | | - flyctl auth docker |
166 | | - docker image tag <image-id> registry.fly.io/<app-name> |
167 | | - docker image push registry.fly.io/<app-name> |
168 | | - fly deploy -a <app-name> |
169 | | - ``` |
| 106 | +Once running, your local Cartesi Rollups Node will be accessible through the standard APIs: |
170 | 107 |
|
171 | | - |
| 108 | +- Inspect endpoint: `http://localhost:10012` |
| 109 | +- JSON-RPC endpoint: `http://localhost:10011` |
0 commit comments