Skip to content
This repository was archived by the owner on Mar 12, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions echo-js/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# syntax=docker.io/docker/dockerfile:1.4
FROM cartesi/toolchain:0.10.0 as dapp-build

WORKDIR /opt/cartesi/dapp
COPY . .
157 changes: 157 additions & 0 deletions echo-js/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
# echo-js DApp

echo-js is a customized DApp written in Javascript, which originally resembles the one provided by the sample [Echo Python DApp](https://github.com/cartesi/rollups-examples/tree/main/echo-python).

The documentation below reflects the original application code, and should also be used as a basis for documenting any DApp created with this mechanism.

## Requirements

Please refer to the [rollups-examples requirements](https://github.com/cartesi/rollups-examples/tree/main/README.md#requirements).

## Building

To build the application, run the following command:

```shell
docker buildx bake -f docker-bake.hcl -f docker-bake.override.hcl --load
```

## Running

To start the application, execute the following command:

```shell
docker compose -f ../docker-compose.yml -f ./docker-compose.override.yml up
```

The application can afterwards be shut down with the following command:

```shell
docker compose -f ../docker-compose.yml -f ./docker-compose.override.yml down -v
```

## Interacting with the application

We can use the rollups-examples [frontend-console](https://github.com/cartesi/rollups-examples/tree/main/frontend-console) application to interact with the DApp.
Ensure that the [application has already been built](https://github.com/cartesi/rollups-examples/tree/main/frontend-console/README.md#building) before using it.

From within the `frontend-console` directory, you can send an input as follows:

```shell
yarn start send --input "Hello there"
```

In order to verify the notices generated by your inputs, run the command:

```shell
yarn start notices
```

The response should be something like this:

```shell
[ { epoch: '0', input: '1', notice: '0', payload: 'Hello there' } ]
```

## Deploying to a testnet

Deploying the application to a blockchain requires creating a smart contract on that network, as well as running a validator node for the DApp.

The first step is to build the DApp's back-end machine, which will produce a hash that serves as a unique identifier.

```shell
docker buildx bake -f docker-bake.hcl -f docker-bake.override.hcl machine --load
```

Once the machine docker image is ready, we can use it to deploy a corresponding Rollups smart contract. This requires you to define a few environment variables to specify which network you are deploying to, which account to use, and which RPC gateway to use when submitting the deploy transaction.

```shell
export NETWORK=<network>
export MNEMONIC=<user sequence of twelve words>
export RPC_URL=<https://your.rpc.gateway>
```

For example, to deploy to the Goerli testnet using an Alchemy RPC node, you could execute:

```shell
export NETWORK=goerli
export MNEMONIC=<user sequence of twelve words>
export RPC_URL=https://eth-goerli.alchemyapi.io/v2/<USER_KEY>
```

With that in place, you can submit a deploy transaction to the Cartesi DApp Factory contract on the target network by executing the following command:

```shell
DAPP_NAME=echo-js docker compose -f ./deploy-testnet.yml up
```

This will create a file at `./deployments/<network>/echo-js.address` with the deployed contract's address.
Once the command finishes, it is advisable to stop the docker compose and remove the volumes created when executing it.

```shell
DAPP_NAME=echo-js docker compose -f ./deploy-testnet.yml down -v
```

After that, a corresponding Cartesi Validator Node must also be instantiated in order to interact with the deployed smart contract on the target network and handle the back-end logic of the DApp.
Aside from the environment variables defined above, the node will also need a secure websocket endpoint for the RPC gateway (WSS URL) and the chain ID of the target network.

For example, for Goerli and Alchemy, you would set the following additional variables:

```shell
export WSS_URL=wss://eth-goerli.alchemyapi.io/v2/<USER_KEY>
export CHAIN_ID=5
```

Then, the node itself can be started by running a docker compose as follows:

```shell
DAPP_NAME=mydapp docker compose -f ./docker-compose-testnet.yml -f ./docker-compose.override.yml up
```

## Interacting with the deployed application

With the node running, you can interact with the deployed DApp using the [frontend-console](https://github.com/cartesi/rollups-examples/tree/main/frontend-console), as described [previously](#interacting-with-the-application).
This time, however, you need to specify the appropriate connectivity configurations.

First of all, in the separate terminal for the frontend-console, define the `MNEMONIC` and `RPC_URL` variables as before:

```shell
export MNEMONIC=<user sequence of twelve words>
export RPC_URL=<https://your.rpc.gateway>
```

Then, inputs can be sent by specifying the DApp contract's address, as follows:

```shell
yarn start send --input "Hello there" --addressFile path/to/echo-js/deployments/<network>/echo-js.address
```

Resulting notices can then be retrieved by querying the local Cartesi Node, as before:

```shell
yarn start notices
```

## Running the back-end in host mode

When developing an application, it is often important to easily test and debug it. For that matter, it is possible to run the Cartesi Rollups environment in [host mode](https://github.com/cartesi/rollups-examples/tree/main/README.md#host-mode), so that the DApp's back-end can be executed directly on the host machine, allowing it to be debugged using regular development tools such as an IDE.

This DApp's back-end is written in `Javascript` that using `QuickJS` engine, so to run it in your machine you need to have `QuickJS` installed.

In order to start the back-end, run the following commands in a dedicated terminal:

```shell
# DIR ../echo-js/
ROLLUP_HTTP_SERVER_URL="http://127.0.0.1:5004" qjs server/src/echo-server.js
```

The final command will effectively run the back-end and send corresponding outputs to port `5004`.

After the back-end successfully starts, it should print an output like the following:

```log
HTTP rollup_server url is http://127.0.0.1:5004
Sending finish
```

After that, you can interact with the application normally [as explained above](#interacting-with-the-application).
10 changes: 10 additions & 0 deletions echo-js/config/bs-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[block_subscriber]

# max delay between retries in seconds
max_delay = 64

# max number of retries
max_retries = 5

# timeout for block subscriber in seconds
timeout = 120
15 changes: 15 additions & 0 deletions echo-js/config/indexer-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[indexer_config]

# unique session identifier for machine manager
session_id = "default_rollups_id"

# node starts syncing from inital epoch
initial_epoch = 0

# polling interval
interval = 10

# end points
postgres_endpoint = "postgres://postgres:password@database/postgres"
state_server_endpoint = "http://state_server:50051"
mm_endpoint = "http://server_manager:5001"
23 changes: 23 additions & 0 deletions echo-js/config/logic-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[logic_config]

# unique session identifier for machine manager
session_id = "default_rollups_id"

# node starts syncing from inital epoch
initial_epoch = 0

# gas estimation/price multiplier
gas_multiplier = 1
gas_price_multiplier = 1

# number of blocks before resubmting tx
rate = 20

# depth of blocks before considering tx finalized
confirmations = 10

# end points
provider_http_endpoint = "http://hardhat:8545"
ws_endpoint = "ws://hardhat:8545"
state_fold_grpc_endpoint = "http://state_server:50051"
mm_endpoint = "http://server_manager:5001"
13 changes: 13 additions & 0 deletions echo-js/config/sf-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[state_fold]

# concurrent events fetch for state fold access
concurrent_events_fetch = 16

# genesis block number for state fold access
genesis_block = "0x1"

# query limit error for state fold access
query_limit_error_codes = [-32005]

# number of blocks (depth) before considering state finalized
safety_margin = 0
10 changes: 10 additions & 0 deletions echo-js/config/tm-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[tx_manager]

# max delay between retries in seconds
max_delay = 64

# max number of retries
max_retries = 5

# timeout for a sent transaction in seconds
transaction_timeout = 5
8 changes: 8 additions & 0 deletions echo-js/dapp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"fs": {
"files": [
"server",
"entrypoint.sh"
]
}
}
32 changes: 32 additions & 0 deletions echo-js/deploy-testnet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
version: "3.9"

services:
machine:
image: cartesi/dapp:${DAPP_NAME:?undefined DAPP_NAME}-devel-machine
command: xxd -c 256 -p hash; sleep 3
volumes:
- machine:/opt/cartesi/share/dapp-bin

deployer:
image: cartesi/rollups-cli:0.3.0
depends_on:
machine:
condition: service_started
command:
[
"create",
"--rpc",
"${RPC_URL:?undefined RPC_URL}",
"--mnemonic",
"${MNEMONIC:?undefined MNEMONIC}",
"--templateHashFile",
"/opt/cartesi/share/dapp-bin/hash",
"--outputFile",
"/deployments/${NETWORK:?undefined NETWORK}/${DAPP_NAME:?undefined DAPP_NAME}.address",
]
volumes:
- machine:/opt/cartesi/share/dapp-bin:ro
- ./deployments:/deployments

volumes:
machine: {}
1 change: 1 addition & 0 deletions echo-js/deployments/localhost/.chainId
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
31337
Loading