JSON Blog
Overview
The JSON Blog application is a straightforward implementation that demonstrates how to manage blog posts using JSON-based inputs in the Crabrolls framework. It allows you to add, update, and delete posts. This document outlines how to use the JSON Blog application, including sending inputs and inspecting outputs. You can find the source code in the CrabRolls repository.
Usage
With the application running, you can manage blog posts by sending JSON-encoded messages. These messages can be sent using the send
command of the Cartesi CLI tool. The following examples demonstrate how to send different types of messages to the JSON Blog application.
Adding a New Post
To add a new post, use the following JSON input:
{ "kind": "AddPost", "payload": { "title": "A new post", "content": "My content of the post!" }}
Send this input using the send
command:
cartesi send generic --input='{ "kind": "AddPost", "payload": { "title": "A new post", "content": "My content of the post!" }}'
Updating an Existing Post
To update an existing post, use the following JSON input:
{ "kind": "UpdatePost", "payload": { "id": 1, "title": "Updated post!", "content": "My updated content!" }}
Send this input using the send
command:
cartesi send generic --input='{ "kind": "UpdatePost", "payload": { "id": 1, "title": "Updated post!", "content": "My updated content!" }}'
Deleting a Post
To delete a post, use the following JSON input:
{ "kind": "DeletePost", "payload": { "id": 1 }}
Send this input using the send
command:
cartesi send generic --input='{ "kind": "DeletePost", "payload": { "id": 1 }}'
Inspecting Posts
You can get all the posts stored in the application by inspecting the cartesian state. The following example demonstrates how to get the outputs of the application:
curl 'http://localhost:8080/inspect/state' | jq
Which will return response like this:
{ "exception_payload": "0x", "processed_input_count": 1, "reports": [ { "payload": "0x5b7b226964223a312c227469746c65223a2241206e657720706f7374222c22636f6e74656e74223a224d7920636f6e74656e74206f662074686520706f737421227d5d" } ], "status": "Accepted"}
Now you can see the posts stored in the application by decoding the payload of the report using the command cast from foundry
:
cast to-ascii 0x5b7b226964223a312c227469746c65223a2241206e657720706f7374222c22636f6e74656e74223a224d7920636f6e74656e74206f662074686520706f737421227d5d | jq
This will return the posts stored in the application:
[ { "id": 1, "title": "A new post", "content": "My content of the post!" }]
Inspecting Changes
The app send a notice of all the changes made to the state, so you can also inspect the notices to see the changes made to the state. The following command demonstrates how to get the notices of the application:
curl 'http://localhost:8080/graphql' -H 'Content-Type: application/json' -d '{"query":"query getLastNotices { inputs(last: 1) { edges { node { notices(last: 1) { edges { node { index payload } } } } } } }"}' | jq
Now you can see the last notices of the application, including the changes made to the state:
{ "data": { "inputs": { "edges": [ { "node": { "notices": { "edges": [ { "node": { "index": 0, "payload": "0x22416464656420706f73743a2041206e657720706f737422" } } ] } } } ] } }}
Decoding the payload of the notice will return the changes made to the state:
cast to-ascii 0x22416464656420706f73743a2041206e657720706f737422
This will return the changes made to the state:
"Added post: A new post"