Let's look at an example
Now that we're all set up, let's add a couple of transactions and then query both the state and the transactions to see how they interact.
First, we'll clear all data from our node to start with a fresh slate. Before running the command, if the Chromia Node is running, stop it with Ctrl+C
. Then, you can clear the data by running the following command:
chr node start --wipe
This command will remove all transactions from the blockchain and reset the current state. You can verify this by running:
chr query get_all_books
This should return an empty array, indicating that there are no books in the current state.
Next, let's add a new transaction to create a book, intentionally misspelling "Chromia" so that we can later correct this with an update:
chr tx create_book --secret .chromia/config "ISBN001" "Crhomia 101" "Alice" --await
The output should look like this:
transaction with rid DF94A07EACBA673CC4ECA4A43D70CDA4FD30E4335E124B7153FB12A78FDD8CE9 was posted CONFIRMED
After creating this transaction, you can query all books using the get_all_books
command:
chr query get_all_books
The result should look like this:
[
{
"author": "Alice",
"isbn": "ISBN001",
"title": "Crhomia 101"
}
]
This confirms that the state/table has been correctly updated with the new book information. Now, let's query the blockchain transactions:
chr query get_transactions
The result will be:
[
{
"signatures": [x"DB12DCDC62F8FB231C3CC7545AB5738C02E0CE29D912D07AAB865565B94324311D24BC1855FDB82446DCE92B9BC9F457DB2B471EDD71EBB2BE3A4A611C3E77EC"],
"tx_body": {
"ops": [
{
"op_args": ["ISBN001", "Crhomia 101", "Alice"],
"op_name": "create_book"
}
],
"signers": [x"02465E21B4297E2F665E39E44F5575DFDF03F542CE9252E22972CFF957E7ACFC1C"],
"tx_rid": x"B9BC06E8B8AAA65BDC4EE0A42EC01BAF92937E1FA8AF43C81031F3FCDFA8DF4B"
}
}
]
This indicates that the transaction has been successfully added to the blockchain and the state has been updated accordingly.
The following diagram illustrates the process when we run the query get_transactions
command:
Next, we'll add a new operation to update a book's title, allowing us to correct the misspelled title. To do this, we need to make the title mutable in our book entity:
entity book {
key isbn: text;
mutable title: text;
author: text;
}
Next, we can add the update operation:
operation update_book(isbn: text, title: text){
val adminPubkey = chain_context.args.admin_pubkey;
require(op_context.is_signer(adminPubkey), "Only admin can update books");
update book @ { .isbn == isbn }( .title = title );
}
Before testing this new operation, update the Chromia Node to ensure it includes the new operation by running the following command and waiting for the next block:
chr node update
After updating, we can test the new operation with a transaction to update a book's title:
chr tx update_book --secret .chromia/config "ISBN001" "Chromia 101" --await
After this transaction, we can query both the blockchain transactions and the state again:
chr query get_all_books
The result will now show the updated title:
[
{
"author": "Alice",
"isbn": "ISBN001",
"title": "Chromia 101"
}
]
Similarly, when querying the blockchain transactions:
chr query get_transactions
The result will be:
[
{
"signatures": [x"DB12DCDC62F8FB231C3CC7545AB5738C02E0CE29D912D07AAB865565B94324311D24BC1855FDB82446DCE92B9BC9F457DB2B471EDD71EBB2BE3A4A611C3E77EC"],
"tx_body": {
"ops": [
{
"op_args": ["ISBN001", "Crhomia 101", "Alice"],
"op_name": "create_book"
}
],
"signers": [x"02465E21B4297E2F665E39E44F5575DFDF03F542CE9252E22972CFF957E7ACFC1C"],
"tx_rid": x"B9BC06E8B8AAA65BDC4EE0A42EC01BAF92937E1FA8AF43C81031F3FCDFA8DF4B"
}
},
{
"signatures": [x"16EE4BA637E5D49FAAF1E3B6A78FA8AF63793AC92784914FC1890A54FC3365294F3FD5EC73AB9159EA61F9F8F1AE2650E0326C3B7A94739CDC0362212B731909"],
"tx_body": {
"ops": [
{
"op_args": ["ISBN001", "Chromia 101"],
"op_name": "update_book"
}
],
"signers": [x"02465E21B4297E2F665E39E44F5575DFDF03F542CE9252E22972CFF957E7ACFC1C"],
"tx_rid": x"B9BC06E8B8AAA65BDC4EE0A42EC01BAF92937E1FA8AF43C81031F3FCDFA8DF4B"
}
}
]
You’ll notice that there are two transactions on the blockchain: one for creating the book and another for updating its title.
When we examine the state, it now reflects the aggregation of these two transactions. This means that the state encompasses all the transactions related to this book, allowing us to backtrack through history and see exactly how the current state was established.
Understanding the interaction between Rell, Chromia, the blockchain, and the state is essential for effectively building and managing decentralized applications.