Skip to content

Commit

Permalink
feat: improved support for debugging cardinal test app (#800)
Browse files Browse the repository at this point in the history
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
ezavada and coderabbitai[bot] authored Oct 16, 2024
1 parent fba71f2 commit 5b45feb
Show file tree
Hide file tree
Showing 8 changed files with 287 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .run/Cardinal Debug.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Cardinal Debug" type="GoRemoteDebugConfigurationType" factoryName="Go Remote" port="40000">
<option name="disconnectOption" value="STOP" />
<disconnect value="STOP" />
<method v="2" />
</configuration>
</component>
29 changes: 29 additions & 0 deletions .run/World Engine Docker - Test Game Debug.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="World Engine Docker - Test Game Debug" type="docker-deploy" factoryName="docker-compose.yml" server-name="Docker">
<deployment type="docker-compose.yml">
<settings>
<option name="envFilePath" value="" />
<option name="envVars">
<list>
<DockerEnvVarImpl>
<option name="name" value="CARDINAL_SERVICE" />
<option name="value" value="game-debug" />
</DockerEnvVarImpl>
</list>
</option>
<option name="services">
<list>
<option value="cockroachdb" />
<option value="nakama" />
<option value="redis" />
<option value="game-debug" />
</list>
</option>
<option name="sourceFilePath" value="docker-compose.yml" />
<option name="upDetach" value="false" />
<option name="upNoDeps" value="true" />
</settings>
</deployment>
<method v="2" />
</configuration>
</component>
29 changes: 29 additions & 0 deletions .run/World Engine Docker - Test Game.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="World Engine Docker - Test Game" type="docker-deploy" factoryName="docker-compose.yml" server-name="Docker">
<deployment type="docker-compose.yml">
<settings>
<option name="envFilePath" value="" />
<option name="envVars">
<list>
<DockerEnvVarImpl>
<option name="name" value="CARDINAL_SERVICE" />
<option name="value" value="game" />
</DockerEnvVarImpl>
</list>
</option>
<option name="services">
<list>
<option value="cockroachdb" />
<option value="nakama" />
<option value="redis" />
<option value="game" />
<option value="test_nakama" />
</list>
</option>
<option name="sourceFilePath" value="docker-compose.yml" />
<option name="upDetach" value="false" />
</settings>
</deployment>
<method v="2" />
</configuration>
</component>
143 changes: 143 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<div align="center"> <!-- markdownlint-disable-line first-line-heading -->
<img alt="World Engine" src="https://i.imgur.com/P6YpZCT.png" width=250 />
<br/>
The world’s first Gamechain SDK that utilizes Argus Labs’ novel sharded rollup architecture.
<br/>
<br/>
<a href="https://codecov.io/gh/Argus-Labs/world-engine" >
<img alt="Code Coverage" src="https://codecov.io/gh/Argus-Labs/world-engine/branch/main/graph/badge.svg?token=XMH4P082HZ"/>
</a>
<a href="https://goreportcard.com/report/pkg.world.dev/world-engine/cardinal">
<img src="https://goreportcard.com/badge/pkg.world.dev/world-engine/cardinal" alt="Go Report Card">
</a>
<a href="https://t.me/worldengine_dev" target="_blank">
<img alt="Telegram Chat" src="https://img.shields.io/endpoint?color=neon&logo=telegram&label=chat&url=https%3A%2F%2Ftg.sumanjay.workers.dev%2Fworldengine_dev">
</a>
<a href="https://pkg.go.dev/pkg.world.dev/world-engine/cardinal" target="_blank">
<img src="https://pkg.go.dev/badge/pkg.world.dev/world-engine/cardinal.svg" alt="Go Reference">
</a>
<a href="https://x.com/WorldEngineGG" target="_blank">
<img alt="Twitter Follow" src="https://img.shields.io/twitter/follow/WorldEngineGG">
</a>
</div>

## Info for World Engine Developers

If you are looking for info for developing games using the World Engine, try:

- the [README.md](./README.md)
- the World Engine [quickstart guide](https://world.dev/quickstart)
- or the online [documentation](https://world.dev).

## Dev Tools

Internal development is done using Jet Brains GoLand

## Directory Structure

<pre>
◢ ✦ ◣ World Engine ◢ ✦ ◣
├── <a href="./.run">.run</a>: Configurations for Running and Debugging in GoLand IDE
├── <a href="./assert">assert</a>: Custom testing package that includes stack traces in errors.
├── <a href="./cardinal">cardinal</a>: The first World Engine game shard implementation.
├── <a href="./e2e">e2e</a>: Test Games for End-to-End testing.
├── <a href="./evm">evm</a>: Rollkit and Polaris integrated Base Shard rollup.
├── <a href="./relay">relay</a>: Game Shard message relayer. Currently contains one implementation using Nakama.
├── <a href="./rift">rift</a>: Protobuf definitions and generated Go code for the World Engine's cross shard messaging protocol.
├── <a href="./scripts">script</a>: Scripts used for development.
├── <a href="./sign">sign</a>: Library to facilitate message signing and verification.
</pre>

## Running Tests from GoLand

From the Configurations menu at the top right of the GoLand window, choose `World Engine Docker - Test Game` and run it. You will see:

<pre>
/usr/local/bin/docker compose -f ./world-engine/docker-compose.yml -p world-engine up --no-deps cockroachdb nakama redis game-debug
[+] Running 6/5
✔ Network world-engine_world-engine Created 0.0s
✔ Volume "world-engine_data" Created 0.0s
✔ Container cockroachdb Created 0.0s
✔ Container redis Created 0.0s
✔ Container test_game Created 0.0s
✔ Container relay_nakama Created 0.0s
✔ Container test_nakama Created 0.0s
Attaching to cockroachdb, redis, relay_nakama, test_game, test_nakama
</pre>

After a short time, you will see `test_game` ticking, along with messages from `relay_nakama` and `test_nakama`:

<pre>
test_game | 7:19PM INF Tick completed duration=1.02933ms tick=1385 tx_count=0
</pre>

If you check the logs from the `test_nakama` container ( cmd-8 for Services, then click on
Docker > Docker-compose-world-engine > test_nakama > test_nakama). You should see test result like this:

<pre>
=== RUN TestEvents
2024-10-09T19:27:34.724721412Z --- PASS: TestEvents (1.26s)
2024-10-09T19:27:34.724799246Z === RUN TestReceipts
2024-10-09T19:27:36.722025883Z --- PASS: TestReceipts (2.00s)
2024-10-09T19:27:36.722052799Z === RUN TestTransactionAndCQLAndRead
2024-10-09T19:27:38.719295228Z --- PASS: TestTransactionAndCQLAndRead (2.00s)
2024-10-09T19:27:38.719301812Z === RUN TestCanShowPersona
2024-10-09T19:27:39.738128589Z --- PASS: TestCanShowPersona (1.02s)
2024-10-09T19:27:39.738162881Z === RUN TestDifferentUsersCannotClaimSamePersonaTag
2024-10-09T19:27:39.795977744Z --- PASS: TestDifferentUsersCannotClaimSamePersonaTag (0.06s)
2024-10-09T19:27:39.795986911Z === RUN TestConcurrentlyClaimSamePersonaTag
2024-10-09T19:27:39.898364637Z --- PASS: TestConcurrentlyClaimSamePersonaTag (0.10s)
2024-10-09T19:27:39.898377304Z === RUN TestCannotClaimAdditionalPersonATag
2024-10-09T19:27:40.736493221Z --- PASS: TestCannotClaimAdditionalPersonATag (0.84s)
2024-10-09T19:27:40.736809012Z === RUN TestPersonaTagFieldCannotBeEmpty
2024-10-09T19:27:40.752374922Z --- PASS: TestPersonaTagFieldCannotBeEmpty (0.02s)
2024-10-09T19:27:40.752464213Z === RUN TestPersonaTagsShouldBeCaseInsensitive
2024-10-09T19:27:41.734335605Z --- PASS: TestPersonaTagsShouldBeCaseInsensitive (0.98s)
2024-10-09T19:27:41.734346729Z === RUN TestReceiptsCanContainErrors
2024-10-09T19:27:43.719725487Z --- PASS: TestReceiptsCanContainErrors (1.98s)
2024-10-09T19:27:43.719743778Z === RUN TestInvalidPersonaTagsAreRejected
2024-10-09T19:27:45.719720074Z --- PASS: TestInvalidPersonaTagsAreRejected (2.00s)
2024-10-09T19:27:45.719746240Z === RUN TestAuthenticateSIWE
2024-10-09T19:27:45.772216412Z --- PASS: TestAuthenticateSIWE (0.05s)
2024-10-09T19:27:45.772261787Z PASS
2024-10-09T19:27:45.774864654Z ok github.com/argus-labs/world-engine/e2e/tests/nakama 12.328s</pre>

## Running Tests in the Debugger from GoLand

From the Configurations menu at the top right of the GoLand window, choose `World Engine Docker - Test Game Debug`
and run it (make sure to stop the `world-engine-game` and `world-engine-nakama` containers first if they were already
running. You will see:

<pre>
/usr/local/bin/docker compose -f ./world-engine/docker-compose.yml -p world-engine up --no-deps cockroachdb nakama redis game-debug
[+] Running 6/5
✔ Network world-engine_world-engine Created 0.0s
✔ Volume "world-engine_data" Created 0.0s
✔ Container cockroachdb Created 0.0s
✔ Container redis Created 0.0s
✔ Container test_game-debug Created 0.0s
✔ Container relay_nakama Created 0.0s
Attaching to cockroachdb, redis, relay_nakama, test_game-debug
[...]
test_game-debug | API server listening at: [::]:40000
test_game-debug | 2024-10-09T19:36:11Z warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted)
</pre>

Those lines near the top of the logs about API server and listening for remote connections show the debugger is ready.
There will also be a lot of warnings about `relay_nakama` failing to establish websocket connection. Those show attempts
by Nakama to attach to Cardinal, but Cardinal is waiting for the remote debugging session to start, so they will continue
until you complete the next step.

Now use the Configurations menu again to choose `Cardinal Debug`. Before you hit the debug icon beside it, try setting
a breakpoint in the `main()` function in `e2e/testgames/game/main.go`. Now hit the debug icon. You should hit that
breakpoint, and from there be able to use the debugger normally including stepping into World Engine code.

Unfortunately, you will NOT be able to debug the `relay` code, because that runs in the nakama container.

If you hit continue from that breakpoint, you can restart the `test_nakama` container with

```shell
docker compose test_nakama up
```

and watch those tests run again. You should get the same output.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ For an in-depth guide on how to use World Engine, visit our [documentation](http
├── <a href="./rift">rift</a>: Protobuf definitions and generated Go code for the World Engine's cross shard messaging protocol.
├── <a href="./sign">sign</a>: Library to facilitate message signing and verification.
</pre>

## World Engine Development

Check out [DEVELOPMENT.md](./DEVELOPMENT.md) for detailed instructions on setting up the development environment, running tests, and debugging.
48 changes: 43 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ services:
context: ./relay/nakama
target: nakama # Build the amd64 version of the image
depends_on:
cockroachdb:
condition: service_healthy
game:
condition: service_started
- "cockroachdb"
- "${CARDINAL_SERVICE:-game}"
environment:
- CARDINAL_ADDR=game:4040
- CARDINAL_ADDR=${CARDINAL_SERVICE:-game}:4040
- ENABLE_DEBUG=TRUE
- CARDINAL_NAMESPACE=testgame
- ENABLE_ALLOWLIST=${ENABLE_ALLOWLIST:-false}
Expand Down Expand Up @@ -113,6 +111,45 @@ services:
- "26659:26659"
- "9090"

game-debug:
container_name: test_game-debug
environment:
- CARDINAL_NAMESPACE=${CARDINAL_NAMESPACE:-testgame}
- CARDINAL_ROLLUP_ENABLED=${CARDINAL_ROLLUP_ENABLED:-false}
- CARDINAL_LOG_LEVEL=${CARDINAL_LOG_LEVEL:-debug}
- CARDINAL_LOG_PRETTY=${CARDINAL_LOG_PRETTY:-true}
- REDIS_ADDRESS=${REDIS_ADDRESS:-redis:6379}
- REDIS_PASSWORD=${REDIS_PASSWORD:-}
- BASE_SHARD_SEQUENCER_ADDRESS=${BASE_SHARD_SEQUENCER_ADDRESS:-chain:9601}
- BASE_SHARD_ROUTER_KEY=${BASE_SHARD_ROUTER_KEY:-abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ01}
- TELEMETRY_TRACE_ENABLED=${TELEMETRY_TRACE_ENABLED:-false}
- TELEMETRY_PROFILER_ENABLED=${TELEMETRY_PROFILER_ENABLED:-false}
- DD_AGENT_HOST=datadog
- DD_ENV=local
- DD_SERVICE=world-engine-cardinal-local
build:
context: ./
dockerfile: e2e/testgames/game/Dockerfile
target: runtime-debug
cap_add:
- SYS_PTRACE
restart: unless-stopped
expose:
- "4040"
- "40000"
ports:
- "4040:4040"
- "9020:9020"
- "40000:40000"
depends_on:
- redis
networks:
- world-engine
security_opt:
- "seccomp:unconfined"
stdin_open: true
tty: true

game:
container_name: test_game
environment:
Expand All @@ -132,6 +169,7 @@ services:
build:
context: ./
dockerfile: e2e/testgames/game/Dockerfile
target: runtime
restart: unless-stopped
expose:
- "4040"
Expand Down
33 changes: 31 additions & 2 deletions e2e/testgames/game/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,41 @@ RUN cd e2e/testgames/game && go build -v -o /go/bin/app


################################
# Runtime Image
# Runtime Image - Normal
################################
FROM gcr.io/distroless/base-debian12 AS runtime

# Copy the binary from the build image
COPY --from=build /go/bin/app /usr/bin

# Run the binary
CMD ["app"]
CMD ["app"]

################################
# Runtime Image - Debug
################################
FROM golang:1.22-bookworm AS runtime-debug

# Install delve
RUN go install github.com/go-delve/delve/cmd/dlv@latest

# Set the GOCACHE environment variable to /root/.cache/go-build to speed up build
ENV GOCACHE=/root/.cache/go-build

WORKDIR /go/src/app

# copy source directories in the order they are least likely to most likely to change, to improve cache utilization
COPY assert assert
COPY e2e/testgames e2e/testgames
COPY cardinal cardinal

# cardinal and assert are part of the go mods, so this must be done after they are copied
WORKDIR /go/src/app/e2e/testgames
RUN go mod download

# build the binary with debugging symbols
WORKDIR /go/src/app/e2e/testgames/game
RUN --mount=type=cache,target="/root/.cache/go-build" go build -gcflags="all=-N -l" -v -o /usr/bin/app

# Run the binary under the Delve debugger, with it listening for remote debug connections
CMD ["dlv", "--listen=:40000", "--headless=true", "--api-version=2", "--accept-multiclient", "exec", "/usr/bin/app"]
1 change: 1 addition & 0 deletions e2e/tests/nakama/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ FROM golang:1.22
WORKDIR /go/src/app

COPY evm evm
COPY cardinal cardinal

# Copy the go module files and download the dependencies
# We do this before copying the rest of the source code to avoid
Expand Down

0 comments on commit 5b45feb

Please sign in to comment.